Skip to content

Commit e908399

Browse files
committed
Run Black
1 parent 8d453af commit e908399

38 files changed

+585
-506
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ def get_version(rel_path):
2424
long_description = f.read()
2525

2626
if __name__ == "__main__":
27-
setup(version=get_version('upcloud_api/__init__.py'))
27+
setup(version=get_version('upcloud_api/__init__.py'))

test/conftest.py

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ def pytest_addoption(parser):
1717
@pytest.fixture(scope='module')
1818
def manager():
1919
import upcloud_api
20+
2021
return upcloud_api.CloudManager("testuser", "mock-api-password")
2122

2223

2324
def read_from_file(filename):
2425
filename = filename.replace("/", "_")
2526
cwd = os.path.dirname(__file__)
26-
f = open(cwd + '/json_data/'+filename)
27+
f = open(cwd + '/json_data/' + filename)
2728
return f.read()
2829

2930

@@ -40,14 +41,19 @@ def mock_get(target, response_file=None):
4041
response_file = target + '.json'
4142

4243
data = Mock.read_from_file(response_file)
43-
responses.add(responses.GET, Mock.base_url + '/' + target,
44-
body=data,
45-
status=200,
46-
content_type='application/json')
44+
responses.add(
45+
responses.GET,
46+
Mock.base_url + '/' + target,
47+
body=data,
48+
status=200,
49+
content_type='application/json',
50+
)
4751
return data
4852

4953
@staticmethod
50-
def __put_patch_post_callback(request, target, data, ignore_data_field=False, empty_payload=False):
54+
def __put_patch_post_callback(
55+
request, target, data, ignore_data_field=False, empty_payload=False
56+
):
5157
data_field = target.split("/")[0]
5258

5359
if not empty_payload:
@@ -57,61 +63,74 @@ def __put_patch_post_callback(request, target, data, ignore_data_field=False, em
5763
for field in data[data_field]:
5864
if field in payload[data_field]:
5965
data[data_field][field] = payload[data_field][field]
60-
return(200, {}, json.dumps(data))
66+
return (200, {}, json.dumps(data))
6167

6268
@staticmethod
6369
def mock_post(target, empty_content=False, ignore_data_field=False, empty_payload=False):
64-
6570
def callback(request):
6671
if not empty_content:
6772
data = json.loads(Mock.read_from_file(target + '_post.json'))
68-
return Mock.__put_patch_post_callback(request, target, data, ignore_data_field, empty_payload)
73+
return Mock.__put_patch_post_callback(
74+
request, target, data, ignore_data_field, empty_payload
75+
)
6976
else:
70-
return(200, {}, '{}')
77+
return (200, {}, '{}')
7178

72-
responses.add_callback(responses.POST, Mock.base_url + '/' + target,
73-
callback=callback,
74-
content_type='application/json')
79+
responses.add_callback(
80+
responses.POST,
81+
Mock.base_url + '/' + target,
82+
callback=callback,
83+
content_type='application/json',
84+
)
7585

7686
@staticmethod
7787
def mock_put(target, ignore_data_field=False, empty_payload=False, call_api=True):
7888
data = json.loads(Mock.read_from_file(target + '.json'))
7989

8090
def callback(request):
81-
return Mock.__put_patch_post_callback(request, target, data, ignore_data_field, empty_payload)
91+
return Mock.__put_patch_post_callback(
92+
request, target, data, ignore_data_field, empty_payload
93+
)
8294

8395
url = Mock.base_url + '/' + target if call_api else target
84-
responses.add_callback(responses.PUT, url,
85-
callback=callback,
86-
content_type='application/json')
96+
responses.add_callback(
97+
responses.PUT, url, callback=callback, content_type='application/json'
98+
)
8799

88100
@staticmethod
89101
def mock_patch(target, ignore_data_field=False, empty_payload=False, call_api=True):
90102
data = json.loads(Mock.read_from_file(target + '.json'))
91103

92104
def callback(request):
93-
return Mock.__put_patch_post_callback(request, target, data, ignore_data_field, empty_payload)
105+
return Mock.__put_patch_post_callback(
106+
request, target, data, ignore_data_field, empty_payload
107+
)
94108

95109
url = Mock.base_url + '/' + target if call_api else target
96-
responses.add_callback(responses.PATCH, url,
97-
callback=callback,
98-
content_type='application/json')
110+
responses.add_callback(
111+
responses.PATCH, url, callback=callback, content_type='application/json'
112+
)
99113

100114
@staticmethod
101115
def mock_delete(target):
102-
responses.add(responses.DELETE, Mock.base_url + '/' + target,
103-
status=204)
116+
responses.add(responses.DELETE, Mock.base_url + '/' + target, status=204)
104117

105118
@staticmethod
106119
def mock_server_operation(target):
107120
# drop third (last) part of a string divided by two slashes ("/"); e.g "this/is/string" -> "this/is"
108121
targetsplit = target.split('/')
109-
targetfile = '/'.join( targetsplit[:2] )
122+
targetfile = '/'.join(targetsplit[:2])
110123

111-
data = json.loads( Mock.read_from_file(targetfile + '.json') )
124+
data = json.loads(Mock.read_from_file(targetfile + '.json'))
112125

113126
# API will always respond state: "started", see: Server.stop, Server.start, Server,restart
114127
data['server']['state'] = 'started'
115128

116-
data = json.dumps( data )
117-
responses.add(responses.POST, Mock.base_url + "/" + target, status=200, body = data, content_type='application/json')
129+
data = json.dumps(data)
130+
responses.add(
131+
responses.POST,
132+
Mock.base_url + "/" + target,
133+
status=200,
134+
body=data,
135+
content_type='application/json',
136+
)

test/helpers/infra.py

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
password_delivery='none',
1212
storage_devices=[
1313
Storage(os='01000000-0000-4000-8000-000030060200', size=10),
14-
Storage(size=10, tier='maxiops')
15-
]),
16-
14+
Storage(size=10, tier='maxiops'),
15+
],
16+
),
1717
'web2': Server(
1818
core_number=1,
1919
memory_amount=1024,
@@ -24,10 +24,8 @@
2424
Storage(os='01000000-0000-4000-8000-000030060200', size=10),
2525
Storage(size=10, tier='maxiops'),
2626
],
27-
ip_addresses=[
28-
IPAddress(family='IPv6', access='public')
29-
]),
30-
27+
ip_addresses=[IPAddress(family='IPv6', access='public')],
28+
),
3129
'db': Server(
3230
core_number=1,
3331
memory_amount=1024,
@@ -38,20 +36,20 @@
3836
Storage(os='01000000-0000-4000-8000-000050010300', size=10),
3937
Storage(size=10),
4038
],
41-
login_user=login_user_block('testuser', ['ssh-rsa AAAAB3NzaC1yc2EAA[...]ptshi44x user@some.host'], True),
39+
login_user=login_user_block(
40+
'testuser', ['ssh-rsa AAAAB3NzaC1yc2EAA[...]ptshi44x user@some.host'], True
4241
),
43-
44-
42+
),
4543
'lb': Server(
46-
plan= '1xCPU-1GB',
44+
plan='1xCPU-1GB',
4745
hostname='balancer.example.com',
4846
zone='uk-lon1',
4947
password_delivery='none',
50-
storage_devices=[
51-
Storage(os='Debian 10.0', size=30)
52-
],
53-
login_user=login_user_block('testuser', ['ssh-rsa AAAAB3NzaC1yc2EAA[...]ptshi44x user@some.host'], True),
54-
)
48+
storage_devices=[Storage(os='Debian 10.0', size=30)],
49+
login_user=login_user_block(
50+
'testuser', ['ssh-rsa AAAAB3NzaC1yc2EAA[...]ptshi44x user@some.host'], True
51+
),
52+
),
5553
}
5654

5755

@@ -65,7 +63,7 @@
6563
source_address_end='192.168.1.255',
6664
destination_port_start='22',
6765
destination_port_end='22',
68-
action='accept'
66+
action='accept',
6967
),
7068
FirewallRule(
7169
position='2',
@@ -76,13 +74,9 @@
7674
source_address_end='192.168.1.255',
7775
destination_port_start='21',
7876
destination_port_end='21',
79-
action='accept'
80-
)
77+
action='accept',
78+
),
8179
]
8280

8381

84-
TAGS = [
85-
Tag('testlb'),
86-
Tag('testdb'),
87-
Tag('testweb')
88-
]
82+
TAGS = [Tag('testlb'), Tag('testdb'), Tag('testweb')]

test/helpers/infra_helpers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def server_test(manager, server):
5252

5353
server.start()
5454

55-
#sync new info from API and assert the changes from above have happened
55+
# sync new info from API and assert the changes from above have happened
5656
server.populate()
5757
assert server.core_number == '3'
5858
assert server.memory_amount == '1024'
@@ -70,7 +70,7 @@ def tag_servers_test(manager, tags, cluster):
7070

7171
cluster['web1'].add_tags(['testweb'])
7272
cluster['web2'].add_tags(['testweb'])
73-
cluster['lb'].add_tags([tags[1]]) # tags[1] is 'db'
73+
cluster['lb'].add_tags([tags[1]]) # tags[1] is 'db'
7474
cluster['db'].add_tags(['testlb'])
7575

7676
fetched_servers = manager.get_servers(tags_has_one=['testlb'])

test/test_apidoc/test_10_ip_addresses.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import json
44
import responses
55

6-
class TestIP:
76

7+
class TestIP:
88
def test_ip_in_server_creation(self):
99
"""IPAddress in server creation.
1010
@@ -21,11 +21,7 @@ def test_ip_in_server_details(self):
2121
https://www.upcloud.com/api/8-servers/#get-server-details
2222
"""
2323
ip = IPAddress(access='private', address='10.0.0.0', family='IPv4')
24-
assert ip.to_dict() == {
25-
'access': 'private',
26-
'address': '10.0.0.0',
27-
'family' : 'IPv4'
28-
}
24+
assert ip.to_dict() == {'access': 'private', 'address': '10.0.0.0', 'family': 'IPv4'}
2925

3026
data = read_from_file('server_00798b85-efdc-41ca-8021-f6ef457b8531.json')
3127
s = Server(**json.loads(data))
@@ -44,5 +40,5 @@ def test_ip_details(self):
4440
'family': 'IPv4',
4541
'part_of_plan': 'yes',
4642
'ptr_record': 'a.ptr.record',
47-
'server': '008c365d-d307-4501-8efc-cd6d3bb0e494'
43+
'server': '008c365d-d307-4501-8efc-cd6d3bb0e494',
4844
}

test/test_firewall.py

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,15 @@ def firewall_rule_callback(request):
1111
Returns the same body with 201 Created.
1212
"""
1313
required_fields = [
14-
'position', 'direction', 'family', 'protocol',
15-
'source_address_start', 'source_address_end',
16-
'destination_port_start', 'destination_port_end',
17-
'action'
14+
'position',
15+
'direction',
16+
'family',
17+
'protocol',
18+
'source_address_start',
19+
'source_address_end',
20+
'destination_port_start',
21+
'destination_port_end',
22+
'action',
1823
]
1924

2025
request_body = json.loads(request.body)
@@ -33,8 +38,7 @@ def check_fields(body):
3338
else:
3439
check_fields(request_body)
3540

36-
return(201, {}, json.dumps(request_body))
37-
41+
return (201, {}, json.dumps(request_body))
3842

3943

4044
class TestFirewall:
@@ -47,27 +51,28 @@ def test_add_firewall_rule(self, manager):
4751
responses.POST,
4852
Mock.base_url + '/server/00798b85-efdc-41ca-8021-f6ef457b8531/firewall_rule',
4953
content_type='application/json',
50-
callback=firewall_rule_callback
54+
callback=firewall_rule_callback,
5155
)
5256

53-
returned_firewall = server.add_firewall_rule(FirewallRule(
54-
position='1',
55-
direction='in',
56-
family='IPv4',
57-
protocol='tcp',
58-
source_address_start='192.168.1.1',
59-
source_address_end='192.168.1.255',
60-
destination_port_start='22',
61-
destination_port_end='22',
62-
action='accept'
63-
))
57+
returned_firewall = server.add_firewall_rule(
58+
FirewallRule(
59+
position='1',
60+
direction='in',
61+
family='IPv4',
62+
protocol='tcp',
63+
source_address_start='192.168.1.1',
64+
source_address_end='192.168.1.255',
65+
destination_port_start='22',
66+
destination_port_end='22',
67+
action='accept',
68+
)
69+
)
6470

6571
# everything should run without errors, returned created object
6672
assert returned_firewall.position == '1'
6773
assert returned_firewall.direction == 'in'
6874
assert returned_firewall.source_address_end == '192.168.1.255'
6975

70-
7176
@responses.activate
7277
def test_remove_firewall_rule(self, manager):
7378
Mock.mock_get('server/00798b85-efdc-41ca-8021-f6ef457b8531')
@@ -96,7 +101,6 @@ def test_list_and_get_firewall_rules(self, manager):
96101

97102
assert firewall_rules[0].position == '1'
98103

99-
100104
@responses.activate
101105
def test_configure_firewall(self, manager):
102106
Mock.mock_get('server/00798b85-efdc-41ca-8021-f6ef457b8531')
@@ -106,7 +110,7 @@ def test_configure_firewall(self, manager):
106110
responses.POST,
107111
Mock.base_url + '/server/00798b85-efdc-41ca-8021-f6ef457b8531/firewall_rule',
108112
content_type='application/json',
109-
callback=firewall_rule_callback
113+
callback=firewall_rule_callback,
110114
)
111115

112116
returned_firewall = server.configure_firewall(
@@ -120,7 +124,7 @@ def test_configure_firewall(self, manager):
120124
source_address_end='192.168.1.255',
121125
destination_port_start='22',
122126
destination_port_end='22',
123-
action='accept'
127+
action='accept',
124128
),
125129
FirewallRule(
126130
position='2',
@@ -131,8 +135,8 @@ def test_configure_firewall(self, manager):
131135
source_address_end='192.168.1.255',
132136
destination_port_start='22',
133137
destination_port_end='22',
134-
action='accept'
135-
)
138+
action='accept',
139+
),
136140
]
137141
)
138142

0 commit comments

Comments
 (0)