Skip to content

Commit 1e76308

Browse files
author
Elias Nygren
authored
Merge pull request #33 from UpCloudLtd/provision-ip-addresses
Provision ip addresses
2 parents 3555739 + 66f6d31 commit 1e76308

File tree

12 files changed

+174
-36
lines changed

12 files changed

+174
-36
lines changed

test/conftest.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,19 @@ def manager():
2424
return upcloud_api.CloudManager("testuser", "mock-api-password")
2525

2626

27+
def read_from_file(filename):
28+
filename = filename.replace("/", "_")
29+
cwd = os.path.dirname(__file__)
30+
f = open(cwd + '/json_data/'+filename, 'r')
31+
return f.read()
32+
33+
2734
class Mock(object):
2835
base_url = 'https://api.upcloud.com/1.2'
2936

3037
@staticmethod
3138
def read_from_file(filename):
32-
33-
filename = filename.replace("/", "_")
34-
35-
cwd = os.path.dirname(__file__)
36-
f = open(cwd + '/json_data/'+filename, 'r')
37-
return f.read()
39+
return read_from_file(filename)
3840

3941
@staticmethod
4042
def mock_get(target, response_file=None):

test/helpers/infra.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from __future__ import division
44
from __future__ import absolute_import
55

6-
from upcloud_api import CloudManager, Storage, FirewallRule, ZONE, Tag
6+
from upcloud_api import CloudManager, Storage, FirewallRule, ZONE, Tag, IPAddress
77
from upcloud_api.server import Server, login_user_block
88

99

@@ -28,6 +28,9 @@
2828
storage_devices=[
2929
Storage(os='Ubuntu 14.04', size=10),
3030
Storage(size=10, tier='maxiops'),
31+
],
32+
ip_addresses=[
33+
IPAddress(family='IPv6', access='public')
3134
]),
3235

3336
'db': Server(

test/json_data/ip_address.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,21 @@
44
{
55
"access": "private",
66
"address": "10.1.0.101",
7+
"family": "IPv4",
78
"ptr_record": "",
89
"server": "008c365d-d307-4501-8efc-cd6d3bb0e494"
910
},
1011
{
1112
"access": "private",
1213
"address": "10.1.0.115",
14+
"family": "IPv4",
1315
"ptr_record": "",
1416
"server": "00887721-4fad-4e9c-b781-6f2265753f11"
1517
},
1618
{
1719
"access": "private",
1820
"address": "10.1.1.97",
21+
"family": "IPv4",
1922
"ptr_record": "",
2023
"server": "00ee2cf4-91c9-4e7a-89b9-a8087e32896b"
2124
}

test/json_data/ip_address_10.1.0.101.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
"ip_address": {
33
"access": "private",
44
"address": "10.1.0.101",
5+
"family": "IPv4",
6+
"part_of_plan": "yes",
57
"ptr_record": "a.ptr.record",
68
"server": "008c365d-d307-4501-8efc-cd6d3bb0e494"
79
}

test/json_data/ip_address_post.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"ip_address": {
33
"access": "private",
44
"address": "10.1.0.101",
5+
"family": "IPv4",
56
"ptr_record": "a.ptr.record",
67
"server": "00798b85-efdc-41ca-8021-f6ef457b8531"
78
}

test/json_data/server_00798b85-efdc-41ca-8021-f6ef457b8531.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
"ip_address" : [
99
{
1010
"access" : "private",
11-
"address" : "10.0.0.0"
11+
"address" : "10.0.0.0",
12+
"family": "IPv4"
1213
},
1314
{
1415
"access" : "public",
15-
"address" : "0.0.0.0"
16+
"address" : "0.0.0.0",
17+
"family": "IPv4"
1618
}
1719
]
1820
},

test/test_apidoc/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
These tests cover cases from the UpCloud API Documentation.
3+
The client should follow the official doc as closely as possible.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from __future__ import unicode_literals
2+
from __future__ import print_function
3+
from __future__ import division
4+
from __future__ import absolute_import
5+
6+
7+
from upcloud_api import IPAddress, Server
8+
from conftest import Mock, read_from_file
9+
import json
10+
import responses
11+
12+
class TestIP(object):
13+
14+
def test_ip_in_server_creation(self):
15+
"""IPAddress in server creation.
16+
17+
https://www.upcloud.com/api/8-servers/#create-server
18+
"""
19+
ip1 = IPAddress(family='IPv4', access='public')
20+
ip2 = IPAddress(family='IPv6', access='private')
21+
assert ip1.to_dict() == {'family': 'IPv4', 'access': 'public'}
22+
assert ip2.to_dict() == {'family': 'IPv6', 'access': 'private'}
23+
24+
def test_ip_in_server_details(self):
25+
"""IPAddress in server details.
26+
27+
https://www.upcloud.com/api/8-servers/#get-server-details
28+
"""
29+
ip = IPAddress(access='private', address='10.0.0.0', family='IPv4')
30+
assert ip.to_dict() == {
31+
'access': 'private',
32+
'address': '10.0.0.0',
33+
'family' : 'IPv4'
34+
}
35+
36+
data = read_from_file('server_00798b85-efdc-41ca-8021-f6ef457b8531.json')
37+
s = Server(**json.loads(data))
38+
for ip in s.ip_addresses:
39+
assert set(ip.to_dict().keys()) == set(['address', 'family', 'access'])
40+
41+
def test_ip_details(self):
42+
"""IPAdress LIST/GET.
43+
44+
https://www.upcloud.com/api/10-ip-addresses/#list-ip-addresses
45+
"""
46+
ip = IPAddress(**json.loads(read_from_file('ip_address_10.1.0.101.json'))['ip_address'])
47+
assert ip.to_dict() == {
48+
'access': 'private',
49+
'address': '10.1.0.101',
50+
'family': 'IPv4',
51+
'part_of_plan': 'yes',
52+
'ptr_record': 'a.ptr.record',
53+
'server': '008c365d-d307-4501-8efc-cd6d3bb0e494'
54+
}

test/test_integration/test_integration_test.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from __future__ import absolute_import
55
import os
66
import pytest
7+
import multiprocessing
78

89
from upcloud_api import CloudManager
910

@@ -30,17 +31,25 @@
3031
CREATED_SERVERS = []
3132
CREATED_TAGS = []
3233

34+
35+
def destroy_server(server):
36+
"""Destroy a server and it's storages."""
37+
server.stop_and_destroy()
38+
39+
def delete_tag(tag):
40+
"""Destroy a tag (only works if the tag is not in use)."""
41+
tag.destroy()
42+
43+
3344
@integration_test
3445
def teardown_module(module):
3546
manager = CloudManager(USERNAME, PASSWORD, timeout=120)
3647

3748
# if we are at CIRCLECI, clean up everything
3849
if os.environ.get('CIRCLECI', False):
39-
for server in manager.get_servers():
40-
server.stop_and_destroy()
41-
42-
for tag in manager.get_tags():
43-
tag.destroy()
50+
pool = multiprocessing.Pool()
51+
pool.map(destroy_server, manager.get_servers())
52+
pool.map(delete_tag, manager.get_tags())
4453
else:
4554
print('removing {}'.format(CREATED_SERVERS))
4655
for server in CREATED_SERVERS:
@@ -90,6 +99,14 @@ def test_infra_ops():
9099
if not found:
91100
raise Exception('server {} not found in all_servers'.format(cs.uuid))
92101

102+
# assert servers' states
103+
# TODO(elnygren): add more assertions here
104+
105+
# web2 non default IP configuration
106+
web2 = CLUSTER['web2']
107+
assert len(web2.ip_addresses) == 1
108+
assert web2.ip_addresses[0].family == 'IPv6'
109+
93110

94111

95112
test_server = CLUSTER['web1']

test/test_server_creation.py

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from __future__ import division
44
from __future__ import absolute_import
55

6-
from upcloud_api import ZONE, Server, Storage, login_user_block
6+
from upcloud_api import ZONE, Server, Storage, IPAddress, login_user_block
77

88
from conftest import Mock
99
import json
@@ -82,7 +82,7 @@ def test_server_prepare_post_body(self):
8282
assert body['server']['zone'] == 'us-chi1'
8383

8484
def test_server_prepare_post_body_optional_attributes(self):
85-
server = Server(
85+
server1 = Server(
8686
core_number=2,
8787
memory_amount=1024,
8888
hostname='my.example.com',
@@ -97,26 +97,62 @@ def test_server_prepare_post_body_optional_attributes(self):
9797
password_delivery='email',
9898
login_user=login_user_block('upclouduser', ['this-is-a-SSH-key']),
9999
avoid_host='12345678',
100-
user_data='https://my.script.com/some_script.py'
100+
user_data='https://my.script.com/some_script.py',
101+
ip_addresses = [
102+
IPAddress(family='IPv4', access='public'),
103+
IPAddress(family='IPv6', access='public')
104+
]
101105
)
102106

103-
body = server.prepare_post_body()
104-
assert body['server']['title'] == 'my.example.com'
105-
assert body['server']['core_number'] == 2
106-
assert body['server']['memory_amount'] == 1024
107-
assert body['server']['hostname'] == server.title
108-
assert body['server']['zone'] == 'us-chi1'
109-
assert body['server']['vnc_password'] == 'my-passwd'
110-
assert body['server']['password_delivery'] == 'email'
111-
assert body['server']['login_user'] == {
112-
'username': 'upclouduser',
113-
'create_password': 'yes',
114-
'ssh_keys': {
115-
'ssh_key': ['this-is-a-SSH-key']
116-
}
107+
108+
server2_dict = {
109+
'core_number':2,
110+
'memory_amount':1024,
111+
'hostname':'my.example.com',
112+
'zone': ZONE.Chicago,
113+
'storage_devices':[
114+
{'os': 'Ubuntu 14.04', 'size': 10}
115+
],
116+
'vnc_password': 'my-passwd',
117+
'password_delivery': 'email',
118+
'login_user': login_user_block('upclouduser', ['this-is-a-SSH-key']),
119+
'avoid_host': '12345678',
120+
'user_data': 'https://my.script.com/some_script.py',
121+
'ip_addresses': [
122+
{'family':'IPv4', 'access':'public'},
123+
{'family':'IPv6', 'access':'public'}
124+
]
117125
}
118-
assert body['server']['avoid_host'] == '12345678'
119-
assert body['server']['user_data'] == 'https://my.script.com/some_script.py'
126+
server2 = Server._create_server_obj(server2_dict, cloud_manager=self)
127+
128+
body1 = server1.prepare_post_body()
129+
body2 = server2.prepare_post_body()
130+
131+
132+
for body in [body1, body2]:
133+
assert body['server']['title'] == 'my.example.com'
134+
assert body['server']['core_number'] == 2
135+
assert body['server']['memory_amount'] == 1024
136+
assert body['server']['hostname'] == server1.title
137+
assert body['server']['zone'] == 'us-chi1'
138+
assert body['server']['vnc_password'] == 'my-passwd'
139+
assert body['server']['password_delivery'] == 'email'
140+
assert body['server']['login_user'] == {
141+
'username': 'upclouduser',
142+
'create_password': 'yes',
143+
'ssh_keys': {
144+
'ssh_key': ['this-is-a-SSH-key']
145+
}
146+
}
147+
assert body['server']['avoid_host'] == '12345678'
148+
assert body['server']['user_data'] == 'https://my.script.com/some_script.py'
149+
assert body['server']['ip_addresses'] == {
150+
'ip_address': [
151+
{'family': 'IPv4', 'access': 'public'},
152+
{'family': 'IPv6', 'access': 'public'}
153+
]
154+
}
155+
120156

121157
@responses.activate
122158
def test_create_server(self, manager):

0 commit comments

Comments
 (0)