|
| 1 | +import mock |
| 2 | +import pytest |
| 3 | + |
| 4 | +from hcloud.networks.client import BoundNetwork |
| 5 | +from hcloud.networks.domain import Network, NetworkSubnet, NetworkRoute |
| 6 | + |
| 7 | + |
| 8 | +class TestBoundNetwork(object): |
| 9 | + |
| 10 | + @pytest.fixture() |
| 11 | + def bound_network(self, hetzner_client): |
| 12 | + return BoundNetwork(client=hetzner_client.networks, data=dict(id=4711)) |
| 13 | + |
| 14 | + @pytest.fixture() |
| 15 | + def network_subnet(self): |
| 16 | + return NetworkSubnet(type="server", ip_range="10.0.1.0/24", network_zone="eu-central") |
| 17 | + |
| 18 | + @pytest.fixture() |
| 19 | + def network_route(self): |
| 20 | + return NetworkRoute(destination="10.100.1.0/24", gateway="10.0.1.1") |
| 21 | + |
| 22 | + def test_get_actions_list(self, bound_network): |
| 23 | + result = bound_network.get_actions_list() |
| 24 | + actions = result.actions |
| 25 | + |
| 26 | + assert len(actions) == 1 |
| 27 | + assert actions[0].id == 13 |
| 28 | + assert actions[0].command == "add_subnet" |
| 29 | + |
| 30 | + def test_update(self, bound_network): |
| 31 | + network = bound_network.update(name="new-name", labels={}) |
| 32 | + assert network.id == 4711 |
| 33 | + assert network.name == "new-name" |
| 34 | + |
| 35 | + def test_delete(self, bound_network): |
| 36 | + resp = bound_network.delete() |
| 37 | + assert resp is True |
| 38 | + |
| 39 | + def test_change_protection(self, bound_network): |
| 40 | + action = bound_network.change_protection(True) |
| 41 | + |
| 42 | + assert action.id == 13 |
| 43 | + assert action.command == "change_protection" |
| 44 | + |
| 45 | + def test_add_subnet(self, bound_network, network_subnet): |
| 46 | + action = bound_network.add_subnet(network_subnet) |
| 47 | + |
| 48 | + assert action.id == 13 |
| 49 | + assert action.command == "add_subnet" |
| 50 | + |
| 51 | + def test_delete_subnet(self, bound_network, network_subnet): |
| 52 | + action = bound_network.delete_subnet(network_subnet) |
| 53 | + |
| 54 | + assert action.id == 13 |
| 55 | + assert action.command == "delete_subnet" |
| 56 | + |
| 57 | + def test_add_route(self, bound_network, network_route): |
| 58 | + action = bound_network.add_route(network_route) |
| 59 | + |
| 60 | + assert action.id == 13 |
| 61 | + assert action.command == "add_route" |
| 62 | + |
| 63 | + def test_delete_route(self, bound_network, network_route): |
| 64 | + action = bound_network.delete_route(network_route) |
| 65 | + |
| 66 | + assert action.id == 13 |
| 67 | + assert action.command == "delete_route" |
| 68 | + |
| 69 | + def test_change_ip_range(self, bound_network, ): |
| 70 | + action = bound_network.change_ip_range("10.0.0.0/12") |
| 71 | + |
| 72 | + assert action.id == 13 |
| 73 | + assert action.command == "change_ip_range" |
| 74 | + |
| 75 | + |
| 76 | +class TestNetworksClient(object): |
| 77 | + |
| 78 | + @pytest.fixture() |
| 79 | + def network_subnet(self): |
| 80 | + return NetworkSubnet(type="server", ip_range="10.0.1.0/24", network_zone="eu-central") |
| 81 | + |
| 82 | + @pytest.fixture() |
| 83 | + def network_route(self): |
| 84 | + return NetworkRoute(destination="10.100.1.0/24", gateway="10.0.1.1") |
| 85 | + |
| 86 | + def test_get_by_id(self, hetzner_client): |
| 87 | + network = hetzner_client.networks.get_by_id(4711) |
| 88 | + assert network.id == 4711 |
| 89 | + assert network.name == "mynet" |
| 90 | + assert network.ip_range == "10.0.0.0/16" |
| 91 | + assert len(network.subnets) == 2 |
| 92 | + assert len(network.routes) == 1 |
| 93 | + assert len(network.servers) == 1 |
| 94 | + assert network.protection['delete'] is False |
| 95 | + |
| 96 | + def test_get_by_name(self, hetzner_client): |
| 97 | + network = hetzner_client.networks.get_by_name("mynet") |
| 98 | + assert network.id == 4711 |
| 99 | + assert network.name == "mynet" |
| 100 | + assert network.ip_range == "10.0.0.0/16" |
| 101 | + assert len(network.subnets) == 2 |
| 102 | + assert len(network.routes) == 1 |
| 103 | + assert len(network.servers) == 1 |
| 104 | + assert network.protection['delete'] is False |
| 105 | + |
| 106 | + def test_get_list(self, hetzner_client): |
| 107 | + result = hetzner_client.networks.get_list() |
| 108 | + networks = result.networks |
| 109 | + assert networks[0].id == 4711 |
| 110 | + assert networks[0].name == "mynet" |
| 111 | + assert networks[0].ip_range == "10.0.0.0/16" |
| 112 | + assert len(networks[0].subnets) == 2 |
| 113 | + assert len(networks[0].routes) == 1 |
| 114 | + assert len(networks[0].servers) == 1 |
| 115 | + assert networks[0].protection['delete'] is False |
| 116 | + |
| 117 | + def test_get_all(self, hetzner_client): |
| 118 | + networks = hetzner_client.networks.get_all() |
| 119 | + assert networks[0].id == 4711 |
| 120 | + assert networks[0].name == "mynet" |
| 121 | + assert networks[0].ip_range == "10.0.0.0/16" |
| 122 | + assert len(networks[0].subnets) == 2 |
| 123 | + assert len(networks[0].routes) == 1 |
| 124 | + assert len(networks[0].servers) == 1 |
| 125 | + assert networks[0].protection['delete'] is False |
| 126 | + |
| 127 | + def test_create(self, hetzner_client, network_subnet, network_route): |
| 128 | + network = hetzner_client.networks.create(name="mynet", ip_range="10.0.0.0/16", subnets=[network_subnet], |
| 129 | + routes=[network_route]) |
| 130 | + assert network.id == 4711 |
| 131 | + assert network.name == "mynet" |
| 132 | + assert network.ip_range == "10.0.0.0/16" |
| 133 | + assert len(network.subnets) == 1 |
| 134 | + assert len(network.routes) == 1 |
| 135 | + assert len(network.servers) == 1 |
| 136 | + assert network.protection['delete'] is False |
| 137 | + |
| 138 | + @pytest.mark.parametrize("network", [Network(id=4711), BoundNetwork(mock.MagicMock(), dict(id=4711))]) |
| 139 | + def test_get_actions_list(self, hetzner_client, network): |
| 140 | + result = hetzner_client.networks.get_actions_list(network) |
| 141 | + actions = result.actions |
| 142 | + |
| 143 | + assert len(actions) == 1 |
| 144 | + assert actions[0].id == 13 |
| 145 | + assert actions[0].command == "add_subnet" |
| 146 | + |
| 147 | + @pytest.mark.parametrize("network", [Network(id=4711), BoundNetwork(mock.MagicMock(), dict(id=4711))]) |
| 148 | + def test_update(self, hetzner_client, network): |
| 149 | + network = hetzner_client.networks.update(network, name="new-name", labels={}) |
| 150 | + |
| 151 | + assert network.id == 4711 |
| 152 | + assert network.name == "new-name" |
| 153 | + |
| 154 | + @pytest.mark.parametrize("network", [Network(id=4711), BoundNetwork(mock.MagicMock(), dict(id=4711))]) |
| 155 | + def test_delete(self, hetzner_client, network): |
| 156 | + result = hetzner_client.networks.delete(network) |
| 157 | + |
| 158 | + assert result is True |
| 159 | + |
| 160 | + @pytest.mark.parametrize("network", [Network(id=4711), BoundNetwork(mock.MagicMock(), dict(id=4711))]) |
| 161 | + def test_change_protection(self, hetzner_client, network): |
| 162 | + action = hetzner_client.networks.change_protection(network, delete=True) |
| 163 | + |
| 164 | + assert action.id == 13 |
| 165 | + assert action.command == "change_protection" |
| 166 | + |
| 167 | + @pytest.mark.parametrize("network", [Network(id=4711), BoundNetwork(mock.MagicMock(), dict(id=4711))]) |
| 168 | + def test_add_subnet(self, hetzner_client, network, network_subnet): |
| 169 | + action = hetzner_client.networks.add_subnet(network, network_subnet) |
| 170 | + |
| 171 | + assert action.id == 13 |
| 172 | + assert action.command == "add_subnet" |
| 173 | + |
| 174 | + @pytest.mark.parametrize("network", [Network(id=4711), BoundNetwork(mock.MagicMock(), dict(id=4711))]) |
| 175 | + def test_delete_subnet(self, hetzner_client, network, network_subnet): |
| 176 | + action = hetzner_client.networks.delete_subnet(network, network_subnet) |
| 177 | + |
| 178 | + assert action.id == 13 |
| 179 | + assert action.command == "delete_subnet" |
| 180 | + |
| 181 | + @pytest.mark.parametrize("network", [Network(id=4711), BoundNetwork(mock.MagicMock(), dict(id=4711))]) |
| 182 | + def test_add_route(self, hetzner_client, network, network_route): |
| 183 | + action = hetzner_client.networks.add_route(network, network_route) |
| 184 | + |
| 185 | + assert action.id == 13 |
| 186 | + assert action.command == "add_route" |
| 187 | + |
| 188 | + @pytest.mark.parametrize("network", [Network(id=4711), BoundNetwork(mock.MagicMock(), dict(id=4711))]) |
| 189 | + def test_delete_route(self, hetzner_client, network, network_route): |
| 190 | + action = hetzner_client.networks.delete_route(network, network_route) |
| 191 | + |
| 192 | + assert action.id == 13 |
| 193 | + assert action.command == "delete_route" |
| 194 | + |
| 195 | + @pytest.mark.parametrize("network", [Network(id=4711), BoundNetwork(mock.MagicMock(), dict(id=4711))]) |
| 196 | + def test_change_ip_range(self, hetzner_client, network): |
| 197 | + action = hetzner_client.networks.change_ip_range(network, "10.0.0.0/12") |
| 198 | + |
| 199 | + assert action.id == 13 |
| 200 | + assert action.command == "change_ip_range" |
0 commit comments