Skip to content

Commit 7248b22

Browse files
author
Elias Nygren
committed
drop future, add flake8, improve code style
1 parent 1cdf53d commit 7248b22

29 files changed

+1784
-1788
lines changed

requirements-dev.txt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
pytest==2.9.2
2-
py==1.4.26
3-
mock==1.0.1
4-
responses==0.3.0
1+
pytest>=2.9.2
2+
py>=1.4.26
3+
mock>=1.0.1
4+
responses>=0.3.0
5+
6+
# flake8
7+
flake8>=2.6.2
8+
flake8-docstrings>=0.2.8
9+
hacking>=0.11.0

requirements.txt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
future==0.14.3
2-
requests==2.6.0
3-
six==1.9.0
4-
wheel==0.24.0
1+
requests>=2.6.0
2+
six>=1.9.0

test/conftest.py

Lines changed: 78 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
from __future__ import division
44
from __future__ import absolute_import
55
from builtins import object, open
6-
from future import standard_library
7-
standard_library.install_aliases()
8-
9-
import json, os, pytest, responses, sys
6+
import json
7+
import os
8+
import pytest
9+
import responses
10+
import sys
1011

1112

1213
# make files under helpers available for import
@@ -25,76 +26,76 @@ def manager():
2526

2627

2728
class Mock(object):
28-
base_url = 'https://api.upcloud.com/1.2'
29-
30-
@staticmethod
31-
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()
38-
39-
@staticmethod
40-
def mock_get(target, response_file=None):
41-
if not response_file:
42-
response_file = target + '.json'
43-
44-
data = Mock.read_from_file(response_file)
45-
responses.add(responses.GET, Mock.base_url + '/' + target,
46-
body=data,
47-
status=200,
48-
content_type='application/json')
49-
return data
50-
51-
@staticmethod
52-
def __put_post_callback(request, target, data):
53-
data_field = target.split("/")[0]
54-
payload = json.loads(request.body)
55-
56-
for field in data[data_field]:
57-
if field in payload[data_field]:
58-
data[data_field][field] = payload[data_field][field]
59-
return(200, {}, json.dumps(data))
60-
61-
@staticmethod
62-
def mock_post(target):
63-
data = json.loads( Mock.read_from_file(target + '_post.json') )
64-
65-
def callback(request):
66-
return Mock.__put_post_callback(request, target, data)
67-
68-
responses.add_callback(responses.POST, Mock.base_url + '/' + target,
69-
callback=callback,
70-
content_type='application/json')
71-
72-
@staticmethod
73-
def mock_put(target):
74-
data = json.loads( Mock.read_from_file(target + '.json') )
75-
76-
def callback(request):
77-
return Mock.__put_post_callback(request, target, data)
78-
79-
responses.add_callback(responses.PUT, Mock.base_url + '/' + target,
80-
callback=callback,
81-
content_type='application/json')
82-
@staticmethod
83-
def mock_delete(target):
84-
# print(Mock.base_url + "/" + target)
85-
responses.add(responses.DELETE, Mock.base_url + "/" + target,
86-
status = 204 )
87-
88-
@staticmethod
89-
def mock_server_operation(target):
90-
# drop third (last) part of a string divided by two slashes ("/"); e.g "this/is/string" -> "this/is"
91-
targetsplit = target.split("/")
92-
targetfile = "/".join( targetsplit[:2] )
93-
94-
data = json.loads( Mock.read_from_file(targetfile + '.json') )
95-
96-
# API will always respond state: "started", see: Server.stop, Server.start, Server,restart
97-
data['server']['state'] = 'started'
98-
99-
data = json.dumps( data )
100-
responses.add(responses.POST, Mock.base_url + "/" + target, status=200, body = data, content_type='application/json')
29+
base_url = 'https://api.upcloud.com/1.2'
30+
31+
@staticmethod
32+
def read_from_file(filename):
33+
34+
filename = filename.replace("/", "_")
35+
36+
cwd = os.path.dirname(__file__)
37+
f = open(cwd + '/json_data/'+filename, 'r')
38+
return f.read()
39+
40+
@staticmethod
41+
def mock_get(target, response_file=None):
42+
if not response_file:
43+
response_file = target + '.json'
44+
45+
data = Mock.read_from_file(response_file)
46+
responses.add(responses.GET, Mock.base_url + '/' + target,
47+
body=data,
48+
status=200,
49+
content_type='application/json')
50+
return data
51+
52+
@staticmethod
53+
def __put_post_callback(request, target, data):
54+
data_field = target.split("/")[0]
55+
payload = json.loads(request.body)
56+
57+
for field in data[data_field]:
58+
if field in payload[data_field]:
59+
data[data_field][field] = payload[data_field][field]
60+
return(200, {}, json.dumps(data))
61+
62+
@staticmethod
63+
def mock_post(target):
64+
data = json.loads(Mock.read_from_file(target + '_post.json'))
65+
66+
def callback(request):
67+
return Mock.__put_post_callback(request, target, data)
68+
69+
responses.add_callback(responses.POST, Mock.base_url + '/' + target,
70+
callback=callback,
71+
content_type='application/json')
72+
73+
@staticmethod
74+
def mock_put(target):
75+
data = json.loads(Mock.read_from_file(target + '.json'))
76+
77+
def callback(request):
78+
return Mock.__put_post_callback(request, target, data)
79+
80+
responses.add_callback(responses.PUT, Mock.base_url + '/' + target,
81+
callback=callback,
82+
content_type='application/json')
83+
84+
@staticmethod
85+
def mock_delete(target):
86+
responses.add(responses.DELETE, Mock.base_url + '/' + target,
87+
status=204)
88+
89+
@staticmethod
90+
def mock_server_operation(target):
91+
# drop third (last) part of a string divided by two slashes ("/"); e.g "this/is/string" -> "this/is"
92+
targetsplit = target.split('/')
93+
targetfile = '/'.join( targetsplit[:2] )
94+
95+
data = json.loads( Mock.read_from_file(targetfile + '.json') )
96+
97+
# API will always respond state: "started", see: Server.stop, Server.start, Server,restart
98+
data['server']['state'] = 'started'
99+
100+
data = json.dumps( data )
101+
responses.add(responses.POST, Mock.base_url + "/" + target, status=200, body = data, content_type='application/json')

test/helpers/infra.py

Lines changed: 48 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,85 +2,83 @@
22
from __future__ import unicode_literals
33
from __future__ import division
44
from __future__ import absolute_import
5-
from future import standard_library
6-
standard_library.install_aliases()
75

86
from upcloud_api import CloudManager, Storage, FirewallRule, ZONE, Tag
97
from upcloud_api.server import Server, login_user_block
108

119

1210
CLUSTER = {
1311
'web1': Server(
14-
core_number = 1,
15-
memory_amount = 512,
16-
hostname = 'web1.example.com',
17-
zone = ZONE.London,
18-
password_delivery = 'none',
19-
storage_devices = [
20-
Storage(os = 'Ubuntu 14.04', size=10),
12+
core_number=1,
13+
memory_amount=512,
14+
hostname='web1.example.com',
15+
zone=ZONE.London,
16+
password_delivery='none',
17+
storage_devices=[
18+
Storage(os='Ubuntu 14.04', size=10),
2119
Storage(size=10, tier='maxiops')
2220
]),
2321

2422
'web2': Server(
25-
core_number = 1,
26-
memory_amount = 512,
27-
hostname = 'web2.example.com',
28-
zone = ZONE.London,
29-
password_delivery = 'none',
30-
storage_devices = [
31-
Storage(os = 'Ubuntu 14.04', size=10),
23+
core_number=1,
24+
memory_amount=512,
25+
hostname='web2.example.com',
26+
zone=ZONE.London,
27+
password_delivery='none',
28+
storage_devices=[
29+
Storage(os='Ubuntu 14.04', size=10),
3230
Storage(size=10, tier='maxiops'),
3331
]),
3432

35-
'db': Server(
36-
core_number = 1,
37-
memory_amount = 512,
38-
hostname = 'db.example.com',
39-
zone = ZONE.London,
40-
password_delivery = 'none',
41-
storage_devices = [
42-
Storage(os = 'CentOS 7.0', size=10),
33+
'db': Server(
34+
core_number=1,
35+
memory_amount=512,
36+
hostname='db.example.com',
37+
zone=ZONE.London,
38+
password_delivery='none',
39+
storage_devices=[
40+
Storage(os='CentOS 7.0', size=10),
4341
Storage(size=10),
4442
],
45-
login_user = login_user_block('testuser', ['ssh-rsa AAAAB3NzaC1yc2EAA[...]ptshi44x user@some.host'], True),
43+
login_user=login_user_block('testuser', ['ssh-rsa AAAAB3NzaC1yc2EAA[...]ptshi44x user@some.host'], True),
4644
),
4745

4846

49-
'lb': Server(
47+
'lb': Server(
5048
plan= '1xCPU-1GB',
51-
hostname = 'balancer.example.com',
52-
zone = ZONE.London,
53-
password_delivery = 'none',
54-
storage_devices = [
55-
Storage(os = 'Debian 7.8', size=30)
49+
hostname='balancer.example.com',
50+
zone=ZONE.London,
51+
password_delivery='none',
52+
storage_devices=[
53+
Storage(os='Debian 7.8', size=30)
5654
],
57-
login_user = login_user_block('testuser', ['ssh-rsa AAAAB3NzaC1yc2EAA[...]ptshi44x user@some.host'], True),
55+
login_user=login_user_block('testuser', ['ssh-rsa AAAAB3NzaC1yc2EAA[...]ptshi44x user@some.host'], True),
5856
)
5957
}
6058

6159

6260
FIREWALL_RULES = [
6361
FirewallRule(
64-
position = '1',
65-
direction = 'in',
66-
family = 'IPv4',
67-
protocol = 'tcp',
68-
source_address_start = '192.168.1.1',
69-
source_address_end = '192.168.1.255',
70-
destination_port_start = '22',
71-
destination_port_end = '22',
72-
action = 'accept'
62+
position='1',
63+
direction='in',
64+
family='IPv4',
65+
protocol='tcp',
66+
source_address_start='192.168.1.1',
67+
source_address_end='192.168.1.255',
68+
destination_port_start='22',
69+
destination_port_end='22',
70+
action='accept'
7371
),
7472
FirewallRule(
75-
position = '2',
76-
direction = 'in',
77-
family = 'IPv4',
78-
protocol = 'tcp',
79-
source_address_start = '192.168.1.1',
80-
source_address_end = '192.168.1.255',
81-
destination_port_start = '21',
82-
destination_port_end = '21',
83-
action = 'accept'
73+
position='2',
74+
direction='in',
75+
family='IPv4',
76+
protocol='tcp',
77+
source_address_start='192.168.1.1',
78+
source_address_end='192.168.1.255',
79+
destination_port_start='21',
80+
destination_port_end='21',
81+
action='accept'
8482
)
8583
]
8684

test/helpers/infra_helpers.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
from __future__ import unicode_literals
33
from __future__ import division
44
from __future__ import absolute_import
5-
from future import standard_library
6-
standard_library.install_aliases()
75

86
from upcloud_api import ZONE, Tag
97

8+
109
def create_cluster(manager, cluster):
1110
"""Create all servers in cluster."""
1211
for server in cluster:

0 commit comments

Comments
 (0)