Example #1
0
    def test_bound_load_balancer_init(self, response_load_balancer):
        bound_load_balancer = BoundLoadBalancer(
            client=mock.MagicMock(),
            data=response_load_balancer['load_balancer'])

        assert bound_load_balancer.id == 4711
        assert bound_load_balancer.name == 'Web Frontend'
Example #2
0
class TestLoadBalancerslient(object):
    @pytest.fixture()
    def load_balancers_client(self):
        return LoadBalancersClient(client=mock.MagicMock())

    def test_get_by_id(self, load_balancers_client, response_load_balancer):
        load_balancers_client._client.request.return_value = response_load_balancer
        bound_load_balancer = load_balancers_client.get_by_id(1)
        load_balancers_client._client.request.assert_called_with(
            url="/load_balancers/1", method="GET")
        assert bound_load_balancer._client is load_balancers_client
        assert bound_load_balancer.id == 4711
        assert bound_load_balancer.name == "Web Frontend"
        assert bound_load_balancer.outgoing_traffic == 123456
        assert bound_load_balancer.ingoing_traffic == 123456
        assert bound_load_balancer.included_traffic == 654321

    @pytest.mark.parametrize("params", [{
        'name': "load_balancer1",
        'label_selector': "label1",
        'page': 1,
        'per_page': 10
    }, {
        'name': ""
    }, {}])
    def test_get_list(self, load_balancers_client,
                      response_simple_load_balancers, params):
        load_balancers_client._client.request.return_value = response_simple_load_balancers
        result = load_balancers_client.get_list(**params)
        load_balancers_client._client.request.assert_called_with(
            url="/load_balancers", method="GET", params=params)

        bound_load_balancers = result.load_balancers
        assert result.meta is None

        assert len(bound_load_balancers) == 2

        bound_load_balancer1 = bound_load_balancers[0]
        bound_load_balancer2 = bound_load_balancers[1]

        assert bound_load_balancer1._client is load_balancers_client
        assert bound_load_balancer1.id == 4711
        assert bound_load_balancer1.name == "Web Frontend"

        assert bound_load_balancer2._client is load_balancers_client
        assert bound_load_balancer2.id == 4712
        assert bound_load_balancer2.name == "Web Frontend2"

    @pytest.mark.parametrize("params", [{
        'name': "loadbalancer1",
        'label_selector': "label1"
    }, {}])
    def test_get_all(self, load_balancers_client,
                     response_simple_load_balancers, params):
        load_balancers_client._client.request.return_value = response_simple_load_balancers
        bound_load_balancers = load_balancers_client.get_all(**params)

        params.update({'page': 1, 'per_page': 50})

        load_balancers_client._client.request.assert_called_with(
            url="/load_balancers", method="GET", params=params)

        assert len(bound_load_balancers) == 2

        bound_load_balancer1 = bound_load_balancers[0]
        bound_load_balancer2 = bound_load_balancers[1]

        assert bound_load_balancer1._client is load_balancers_client
        assert bound_load_balancer1.id == 4711
        assert bound_load_balancer1.name == "Web Frontend"

        assert bound_load_balancer2._client is load_balancers_client
        assert bound_load_balancer2.id == 4712
        assert bound_load_balancer2.name == "Web Frontend2"

    def test_get_by_name(self, load_balancers_client,
                         response_simple_load_balancers):
        load_balancers_client._client.request.return_value = response_simple_load_balancers
        bound_load_balancer = load_balancers_client.get_by_name("Web Frontend")

        params = {'name': "Web Frontend"}

        load_balancers_client._client.request.assert_called_with(
            url="/load_balancers", method="GET", params=params)

        assert bound_load_balancer._client is load_balancers_client
        assert bound_load_balancer.id == 4711
        assert bound_load_balancer.name == "Web Frontend"

    def test_create(self, load_balancers_client,
                    response_create_load_balancer):
        load_balancers_client._client.request.return_value = response_create_load_balancer
        response = load_balancers_client.create(
            "my-balancer",
            load_balancer_type=LoadBalancerType(name="lb11"),
            location=Location(id=1))
        load_balancers_client._client.request.assert_called_with(
            url="/load_balancers",
            method="POST",
            json={
                'name': "my-balancer",
                'load_balancer_type': "lb11",
                'location': 1
            })

        bound_load_balancer = response.load_balancer

        assert bound_load_balancer._client is load_balancers_client
        assert bound_load_balancer.id == 1
        assert bound_load_balancer.name == "my-balancer"

    @pytest.mark.parametrize(
        "load_balancer",
        [LoadBalancer(id=1),
         BoundLoadBalancer(mock.MagicMock(), dict(id=1))])
    def test_change_type_with_load_balancer_type_name(self,
                                                      load_balancers_client,
                                                      load_balancer,
                                                      generic_action):
        load_balancers_client._client.request.return_value = generic_action
        action = load_balancers_client.change_type(
            load_balancer, LoadBalancerType(name="lb11"))
        load_balancers_client._client.request.assert_called_with(
            url="/load_balancers/1/actions/change_type",
            method="POST",
            json={"load_balancer_type": "lb11"})

        assert action.id == 1
        assert action.progress == 0

    @pytest.mark.parametrize(
        "load_balancer",
        [LoadBalancer(id=1),
         BoundLoadBalancer(mock.MagicMock(), dict(id=1))])
    def test_change_type_with_load_balancer_type_id(self,
                                                    load_balancers_client,
                                                    load_balancer,
                                                    generic_action):
        load_balancers_client._client.request.return_value = generic_action
        action = load_balancers_client.change_type(load_balancer,
                                                   LoadBalancerType(id=1))
        load_balancers_client._client.request.assert_called_with(
            url="/load_balancers/1/actions/change_type",
            method="POST",
            json={"load_balancer_type": 1})

        assert action.id == 1
        assert action.progress == 0
Example #3
0
 def bound_load_balancer(self, hetzner_client):
     return BoundLoadBalancer(client=hetzner_client.load_balancers,
                              data=dict(id=14))
Example #4
0
class TestLoadBalancersClient(object):
    def test_get_by_id(self, hetzner_client):
        load_balancer = hetzner_client.load_balancers.get_by_id(4711)
        assert load_balancer.id == 4711

    def test_get_by_name(self, hetzner_client):
        load_balancer = hetzner_client.load_balancers.get_by_name("Web Frontend")
        assert load_balancer.id == 4711
        assert load_balancer.name == "Web Frontend"

    def test_get_list(self, hetzner_client):
        result = hetzner_client.load_balancers.get_list()
        load_balancers = result.load_balancers
        assert load_balancers[0].id == 4711

    def test_create(self, hetzner_client):
        resp = hetzner_client.load_balancers.create(
            "Web Frontend",
            load_balancer_type=LoadBalancerType(name="lb1"),
            algorithm=LoadBalancerAlgorithm("round_robin"))
        load_balancer = resp.load_balancer
        assert load_balancer.id == 4711
        assert load_balancer.name == "Web Frontend"

    @pytest.mark.parametrize("load_balancer", [LoadBalancer(id=4711), BoundLoadBalancer(mock.MagicMock(), dict(id=1))])
    def test_get_actions_list(self, hetzner_client, load_balancer):
        result = hetzner_client.load_balancers.get_actions_list(load_balancer)
        actions = result.actions

        assert len(actions) == 1
        assert actions[0].id == 13
        assert actions[0].command == "add_service"

    @pytest.mark.parametrize("load_balancer", [LoadBalancer(id=4711), BoundLoadBalancer(mock.MagicMock(), dict(id=1))])
    def test_update(self, hetzner_client, load_balancer):
        lb = hetzner_client.load_balancers.update(load_balancer, name="new-name", labels={})

        assert lb.id == 4711
        assert lb.name == "new-name"

    @pytest.mark.parametrize("load_balancer", [LoadBalancer(id=4711), BoundLoadBalancer(mock.MagicMock(), dict(id=1))])
    def test_delete(self, hetzner_client, load_balancer):
        response = hetzner_client.load_balancers.delete(load_balancer)
        assert response is True

    @pytest.mark.parametrize("load_balancer", [LoadBalancer(id=4711), BoundLoadBalancer(mock.MagicMock(), dict(id=1))])
    def test_add_target(self, hetzner_client, load_balancer):
        action = hetzner_client.load_balancers.add_target(load_balancer, LoadBalancerTarget(type="server", server=Server(id=1)))
        assert action.id == 13
        assert action.command == "add_target"

    @pytest.mark.parametrize("load_balancer", [LoadBalancer(id=4711), BoundLoadBalancer(mock.MagicMock(), dict(id=1))])
    def test_remove_target(self, hetzner_client, load_balancer):
        action = hetzner_client.load_balancers.remove_target(load_balancer,
                                                             LoadBalancerTarget(type="server", server=Server(id=1)))
        assert action.id == 13
        assert action.command == "remove_target"

    @pytest.mark.parametrize("load_balancer", [LoadBalancer(id=4711), BoundLoadBalancer(mock.MagicMock(), dict(id=1))])
    def test_change_algorithm(self, hetzner_client, load_balancer):
        action = hetzner_client.load_balancers.change_algorithm(load_balancer, LoadBalancerAlgorithm(type="round_robin"))
        assert action.id == 13
        assert action.command == "change_algorithm"

    @pytest.mark.parametrize("load_balancer", [LoadBalancer(id=4711), BoundLoadBalancer(mock.MagicMock(), dict(id=1))])
    def test_add_service(self, hetzner_client, load_balancer):
        action = hetzner_client.load_balancers.add_service(load_balancer,
                                                           LoadBalancerService(protocol="http", listen_port=123, destination_port=124, proxyprotocol=False))
        assert action.id == 13
        assert action.command == "add_service"

    @pytest.mark.parametrize("load_balancer", [LoadBalancer(id=4711), BoundLoadBalancer(mock.MagicMock(), dict(id=1))])
    def test_delete_service(self, hetzner_client, load_balancer):
        action = hetzner_client.load_balancers.delete_service(load_balancer,
                                                              LoadBalancerService(protocol="http", listen_port=123,
                                                                                  destination_port=124,
                                                                                  proxyprotocol=False))
        assert action.id == 13
        assert action.command == "delete_service"

    @pytest.mark.parametrize("load_balancer", [LoadBalancer(id=4711), BoundLoadBalancer(mock.MagicMock(), dict(id=1))])
    def test_update_service(self, hetzner_client, load_balancer):
        action = hetzner_client.load_balancers.update_service(load_balancer,
                                                              LoadBalancerService(protocol="http", listen_port=123,
                                                                                  destination_port=124,
                                                                                  proxyprotocol=False,
                                                                                  health_check=LoadBalancerHealthCheck(protocol='http', port=123, interval=1, timeout=1, retries=1)))
        assert action.id == 13
        assert action.command == "update_service"

    @pytest.mark.parametrize("load_balancer", [LoadBalancer(id=4711), BoundLoadBalancer(mock.MagicMock(), dict(id=1))])
    def test_change_protection(self, hetzner_client, load_balancer):
        action = hetzner_client.load_balancers.change_protection(load_balancer, {"delete": True})
        assert action.command == "change_protection"