Skip to content

Commit ba3e9f5

Browse files
LKaemmerlingLD250
authored andcommitted
Add all integration tests
They currently only work with the NEW mock server (because there we can choose the apib download location)
1 parent 271e9aa commit ba3e9f5

4 files changed

Lines changed: 299 additions & 4 deletions

File tree

hcloud/networks/client.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class BoundNetwork(BoundModelBase):
1212
def __init__(self, client, data, complete=True):
1313
subnets = data.get("subnets", [])
1414
if subnets is not None:
15-
subnets = [NetworkSubnet(**subnet) for subnet in subnets]
15+
subnets = [NetworkSubnet(type=subnet['type'], ip_range=subnet['ip_range'], gateway=subnet['gateway'], network_zone=subnet['network_zone']) for subnet in subnets]
1616
data['subnets'] = subnets
1717

1818
routes = data.get("routes", [])
@@ -78,22 +78,52 @@ def get_actions(self, status=None, sort=None):
7878

7979
def add_subnet(self, subnet):
8080
# type: (NetworkSubnet) -> List[BoundAction]
81+
"""Adds a subnet entry to a network.
82+
83+
:param subnet: :class:`NetworkSubnet <hcloud.networks.domain.NetworkSubnet>`
84+
The NetworkSubnet you want to add to the Network
85+
:return: :class:`BoundAction <hcloud.actions.client.BoundAction>`
86+
"""
8187
return self._client.add_subnet(self, subnet=subnet)
8288

8389
def delete_subnet(self, subnet):
8490
# type: (NetworkSubnet) -> List[BoundAction]
91+
"""Removes a subnet entry from a network
92+
93+
:param subnet: :class:`NetworkSubnet <hcloud.networks.domain.NetworkSubnet>`
94+
The NetworkSubnet you want to remove from the Network
95+
:return: :class:`BoundAction <hcloud.actions.client.BoundAction>`
96+
"""
8597
return self._client.delete_subnet(self, subnet=subnet)
8698

8799
def add_route(self, route):
88100
# type: (NetworkRoute) -> List[BoundAction]
101+
"""Adds a route entry to a network.
102+
103+
:param route: :class:`NetworkRoute <hcloud.networks.domain.NetworkRoute>`
104+
The NetworkRoute you want to add to the Network
105+
:return: :class:`BoundAction <hcloud.actions.client.BoundAction>`
106+
"""
89107
return self._client.add_route(self, route=route)
90108

91109
def delete_route(self, route):
92110
# type: (NetworkRoute) -> List[BoundAction]
111+
"""Removes a route entry to a network.
112+
113+
:param route: :class:`NetworkRoute <hcloud.networks.domain.NetworkRoute>`
114+
The NetworkRoute you want to remove from the Network
115+
:return: :class:`BoundAction <hcloud.actions.client.BoundAction>`
116+
"""
93117
return self._client.delete_route(self, route=route)
94118

95119
def change_ip_range(self, ip_range):
96120
# type: (str) -> List[BoundAction]
121+
"""Changes the IP range of a network.
122+
123+
:param ip_range: str
124+
The new prefix for the whole network.
125+
:return: :class:`BoundAction <hcloud.actions.client.BoundAction>`
126+
"""
97127
return self._client.change_ip_range(self, ip_range=ip_range)
98128

99129
def change_protection(self, delete=None):

hcloud/networks/domain.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,17 @@ def __init__(
6262

6363

6464
class NetworkSubnet(BaseDomain):
65+
"""Network Subnet Domain
66+
67+
:param type: str
68+
Type of sub network.
69+
:param ip_range: str
70+
Range to allocate IPs from.
71+
:param network_zone: str
72+
Name of network zone.
73+
:param gateway: str
74+
Gateway for the route.
75+
"""
6576
__slots__ = ("type", "ip_range", "network_zone", "gateway")
6677

6778
def __init__(self, type=None, ip_range=None, network_zone=None, gateway=None):
@@ -72,7 +83,15 @@ def __init__(self, type=None, ip_range=None, network_zone=None, gateway=None):
7283

7384

7485
class NetworkRoute(BaseDomain):
75-
__slots__ = ("destination", "gateway", "network_zone")
86+
"""Network Route Domain
87+
88+
:param destination: str
89+
Destination network or host of this route.
90+
:param gateway: str
91+
Gateway for the route.
92+
"""
93+
94+
__slots__ = ("destination", "gateway")
7695

7796
def __init__(self, destination=None, gateway=None):
7897
self.destination = destination
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
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"

tests/integration/servers/test_servers.py

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import mock
22
import pytest
33

4+
from hcloud.networks.client import BoundNetwork
5+
from hcloud.networks.domain import Network
46
from hcloud.servers.client import BoundServer
57
from hcloud.servers.domain import Server
68
from hcloud.ssh_keys.domain import SSHKey
@@ -140,6 +142,27 @@ def test_request_console(self, bound_server):
140142
assert response.wss_url == "wss://console.hetzner.cloud/?server_id=1&token=3db32d15-af2f-459c-8bf8-dee1fd05f49c"
141143
assert response.password == "9MQaTg2VAGI0FIpc10k3UpRXcHj2wQ6x"
142144

145+
@pytest.mark.parametrize("network", [Network(id=4711), BoundNetwork(mock.MagicMock(), dict(id=4711))])
146+
def test_attach_to_network(self, bound_server, network):
147+
action = bound_server.attach_to_network(network, ip="10.0.1.1", alias_ips=["10.0.1.2"])
148+
149+
assert action.id == 13
150+
assert action.command == "attach_to_network"
151+
152+
@pytest.mark.parametrize("network", [Network(id=4711), BoundNetwork(mock.MagicMock(), dict(id=4711))])
153+
def test_detach_from_network(self, bound_server, network):
154+
action = bound_server.detach_from_network(network)
155+
156+
assert action.id == 13
157+
assert action.command == "detach_from_network"
158+
159+
@pytest.mark.parametrize("network", [Network(id=4711), BoundNetwork(mock.MagicMock(), dict(id=4711))])
160+
def test_change_alias_ips(self, bound_server, network):
161+
action = bound_server.change_alias_ips(network, alias_ips=["10.0.1.2"])
162+
163+
assert action.id == 13
164+
assert action.command == "change_alias_ips"
165+
143166

144167
class TestServersClient(object):
145168

@@ -161,7 +184,7 @@ def test_get_by_name(self, hetzner_client):
161184
assert server.image.id == 4711
162185

163186
def test_get_list(self, hetzner_client):
164-
result = hetzner_client.servers.get_list(42)
187+
result = hetzner_client.servers.get_list()
165188
servers = result.servers
166189
assert servers[0].id == 42
167190
assert servers[0].volumes == []
@@ -170,7 +193,6 @@ def test_get_list(self, hetzner_client):
170193
assert servers[0].image.id == 4711
171194

172195
def test_create(self, hetzner_client):
173-
174196
response = hetzner_client.servers.create(
175197
"my-server",
176198
server_type=ServerType(name="cx11"),
@@ -355,3 +377,27 @@ def test_request_console(self, hetzner_client, server):
355377
assert response.action.command == "request_console"
356378
assert response.wss_url == "wss://console.hetzner.cloud/?server_id=1&token=3db32d15-af2f-459c-8bf8-dee1fd05f49c"
357379
assert response.password == "9MQaTg2VAGI0FIpc10k3UpRXcHj2wQ6x"
380+
381+
@pytest.mark.parametrize("server", [Server(id=1), BoundServer(mock.MagicMock(), dict(id=1))])
382+
@pytest.mark.parametrize("network", [Network(id=4711), BoundNetwork(mock.MagicMock(), dict(id=4711))])
383+
def test_attach_to_network(self, hetzner_client, server, network):
384+
action = hetzner_client.servers.attach_to_network(server, network, ip="10.0.1.1", alias_ips=["10.0.1.2"])
385+
386+
assert action.id == 13
387+
assert action.command == "attach_to_network"
388+
389+
@pytest.mark.parametrize("server", [Server(id=1), BoundServer(mock.MagicMock(), dict(id=1))])
390+
@pytest.mark.parametrize("network", [Network(id=4711), BoundNetwork(mock.MagicMock(), dict(id=4711))])
391+
def test_detach_from_network(self, hetzner_client, server, network):
392+
action = hetzner_client.servers.detach_from_network(server, network)
393+
394+
assert action.id == 13
395+
assert action.command == "detach_from_network"
396+
397+
@pytest.mark.parametrize("server", [Server(id=1), BoundServer(mock.MagicMock(), dict(id=1))])
398+
@pytest.mark.parametrize("network", [Network(id=4711), BoundNetwork(mock.MagicMock(), dict(id=4711))])
399+
def test_change_alias_ips(self, hetzner_client, server, network):
400+
action = hetzner_client.servers.change_alias_ips(server, network, alias_ips=["10.0.1.2"])
401+
402+
assert action.id == 13
403+
assert action.command == "change_alias_ips"

0 commit comments

Comments
 (0)