33from __future__ import division
44from __future__ import absolute_import
55from builtins import object , open
6- from future import standard_library
7- standard_library .install_aliases ()
6+ import json
7+ import os
8+ import pytest
9+ import responses
10+ import sys
11+
12+
13+ # make files under helpers available for import
14+ HELPERS_PATH = os .path .join (os .path .dirname (__file__ ), 'helpers' )
15+ sys .path .append (HELPERS_PATH )
16+
17+
18+ def pytest_addoption (parser ):
19+ parser .addoption ('--integration-tests' , action = 'store_true' , help = 'run integration tests' )
820
9- import json , os , pytest , responses
1021
1122@pytest .fixture (scope = 'module' )
1223def manager ():
@@ -15,80 +26,76 @@ def manager():
1526
1627
1728class Mock (object ):
18- base_url = 'https://api.upcloud.com/1.2'
19-
20- @staticmethod
21- def read_from_file (filename ):
22-
23- filename = filename .replace ("/" , "_" )
24-
25- cwd = os .path .dirname (__file__ )
26- f = open (cwd + '/json_data/' + filename , 'r' )
27- return f .read ()
29+ base_url = 'https://api.upcloud.com/1.2'
2830
29- @staticmethod
30- def mock_get (target , response_file = None ):
31- if not response_file :
32- response_file = target + '.json'
31+ @staticmethod
32+ def read_from_file (filename ):
3333
34- data = Mock .read_from_file (response_file )
35- responses .add (responses .GET , Mock .base_url + '/' + target ,
36- body = data ,
37- status = 200 ,
38- content_type = 'application/json' )
39- return data
34+ filename = filename .replace ("/" , "_" )
4035
41- @staticmethod
42- def __put_post_callback (request , target , data ):
43- data_field = target .split ("/" )[0 ]
44- payload = json .loads (request .body )
36+ cwd = os .path .dirname (__file__ )
37+ f = open (cwd + '/json_data/' + filename , 'r' )
38+ return f .read ()
4539
46- for field in data [ data_field ]:
47- if field in payload [ data_field ] :
48- data [ data_field ][ field ] = payload [ data_field ][ field ]
49- return ( 200 , {}, json . dumps ( data ))
40+ @ staticmethod
41+ def mock_get ( target , response_file = None ) :
42+ if not response_file :
43+ response_file = target + '.json'
5044
51- @staticmethod
52- def mock_post (target ):
53- data = json .loads ( Mock .read_from_file (target + '_post.json' ) )
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
5451
55- def callback (request ):
56- return Mock .__put_post_callback (request , target , data )
52+ @staticmethod
53+ def __put_post_callback (request , target , data ):
54+ data_field = target .split ("/" )[0 ]
55+ payload = json .loads (request .body )
5756
58- responses .add_callback (responses .POST , Mock .base_url + '/' + target ,
59- callback = callback ,
60- content_type = 'application/json' )
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 ))
6161
62- @staticmethod
63- def mock_put (target ):
64- data = json .loads ( Mock .read_from_file (target + '.json' ) )
62+ @staticmethod
63+ def mock_post (target ):
64+ data = json .loads (Mock .read_from_file (target + '_post .json' ))
6565
66- def callback (request ):
67- return Mock .__put_post_callback (request , target , data )
66+ def callback (request ):
67+ return Mock .__put_post_callback (request , target , data )
6868
69- responses .add_callback (responses .PUT , Mock .base_url + '/' + target ,
70- callback = callback ,
71- content_type = 'application/json' )
72- @staticmethod
73- def mock_delete (target ):
74- # print(Mock.base_url + "/" + target)
75- responses .add (responses .DELETE , Mock .base_url + "/" + target ,
76- status = 204 )
69+ responses .add_callback (responses .POST , Mock .base_url + '/' + target ,
70+ callback = callback ,
71+ content_type = 'application/json' )
7772
78- @staticmethod
79- def mock_server_operation (target ):
80- # drop third (last) part of a string divided by two slashes ("/"); e.g "this/is/string" -> "this/is"
81- targetsplit = target .split ("/" )
82- targetfile = "/" .join ( targetsplit [:2 ] )
73+ @staticmethod
74+ def mock_put (target ):
75+ data = json .loads (Mock .read_from_file (target + '.json' ))
8376
84- data = json .loads ( Mock .read_from_file (targetfile + '.json' ) )
77+ def callback (request ):
78+ return Mock .__put_post_callback (request , target , data )
8579
86- # API will always respond state: "started", see: Server.stop, Server.start, Server,restart
87- data ["server" ]["state" ] = "started"
80+ responses .add_callback (responses .PUT , Mock .base_url + '/' + target ,
81+ callback = callback ,
82+ content_type = 'application/json' )
8883
89- data = json .dumps ( data )
90- responses .add (responses .POST , Mock .base_url + "/" + target , status = 200 , body = data , content_type = 'application/json' )
84+ @staticmethod
85+ def mock_delete (target ):
86+ responses .add (responses .DELETE , Mock .base_url + '/' + target ,
87+ status = 204 )
9188
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 ] )
9294
95+ data = json .loads ( Mock .read_from_file (targetfile + '.json' ) )
9396
97+ # API will always respond state: "started", see: Server.stop, Server.start, Server,restart
98+ data ['server' ]['state' ] = 'started'
9499
100+ data = json .dumps ( data )
101+ responses .add (responses .POST , Mock .base_url + "/" + target , status = 200 , body = data , content_type = 'application/json' )
0 commit comments