20
20
21
21
22
22
import json
23
- import os
24
- import sys
25
- import tempfile
26
- import unittest
27
- from textwrap import dedent
28
23
29
- import requests
30
24
import six
31
- import yaml
32
25
from mock import patch
33
26
34
- from hpecp import ContainerPlatformClient
35
- from hpecp .base_resource import ResourceList
36
- from hpecp .exceptions import APIItemNotFoundException
37
-
38
- from .base_test import BaseTestCase , MockResponse , mocked_login_post
39
-
40
-
41
- class TestCLI (BaseTestCase ):
42
- def mocked_login_post (* args , ** kwargs ):
43
- if args [0 ] == "https://127.0.0.1:8080/api/v1/login" :
44
- return MockResponse (
45
- json_data = {},
46
- status_code = 200 ,
47
- headers = {
48
- "location" : (
49
- "/api/v1/session/df1bfacb-xxxx-xxxx-xxxx-c8f57d8f3c71"
50
- )
51
- },
52
- )
53
- if args [0 ] == "https://127.0.0.1:8080/api/v2/hpelicense" :
54
- return MockResponse (
55
- json_data = {},
56
- status_code = 201 ,
57
- headers = {"location" : "/api/v2/hpeclicense/1" },
58
- )
59
- raise RuntimeError ("Unhandle POST request: " + args [0 ])
60
-
61
- def mocked_requests_get (* args , ** kwargs ):
62
- if args [0 ] == "https://127.0.0.1:8080/api/v1/license" :
63
- return MockResponse (
64
- json_data = {
65
- "_links" : {"self" : {"href" : "/api/v1/license" }},
66
- "state" : "unlicensed" ,
67
- "uuid" : "3c831f6e-f76f-410d-977c-ed13b0c817d1" ,
27
+ from .base_test import BaseTestCase , MockResponse
28
+
29
+ BaseTestCase .registerHttpPostHandler (
30
+ url = "https://127.0.0.1:8080/api/v2/hpelicense" ,
31
+ response = MockResponse (
32
+ json_data = {},
33
+ status_code = 201 ,
34
+ headers = {"location" : "/api/v2/hpeclicense/1" },
35
+ ),
36
+ )
37
+
38
+ BaseTestCase .registerHttpDeleteHandler (
39
+ url = "https://127.0.0.1:8080/api/v2/hpelicense/TEST_LICENSE_KEY/" ,
40
+ response = MockResponse (json_data = {}, status_code = 200 , headers = dict (),),
41
+ )
42
+
43
+ BaseTestCase .registerHttpGetHandler (
44
+ url = "https://127.0.0.1:8080/api/v1/license" ,
45
+ response = MockResponse (
46
+ json_data = {
47
+ "_links" : {"self" : {"href" : "/api/v1/license" }},
48
+ "state" : "unlicensed" ,
49
+ "uuid" : "3c831f6e-f76f-410d-977c-ed13b0c817d1" ,
50
+ },
51
+ status_code = 200 ,
52
+ headers = dict (),
53
+ ),
54
+ )
55
+
56
+ BaseTestCase .registerHttpGetHandler (
57
+ url = "https://127.0.0.1:8080/api/v2/hpelicense" ,
58
+ response = MockResponse (
59
+ json_data = {
60
+ "_links" : {"self" : {"href" : "/api/v2/hpelicense" }},
61
+ "Licenses" : [
62
+ {
63
+ "Label" : "The License" ,
64
+ "Feature" : "HPE Machine Learning Ops" ,
65
+ "Capacity" : 240 ,
66
+ "UnlimitedCapacity" : False ,
67
+ "Start" : 1566864000000 ,
68
+ "StartDisplay" : "2019-08-27T00:00:00Z" ,
69
+ "Expiration" : 1609286399000 ,
70
+ "ExpirationDisplay" : "2020-12-29T23:59:59Z" ,
71
+ "LicenseKey" : "TEST_LICENSE_KEY" ,
72
+ "DeviceID" : "1234 1234" ,
73
+ "Evaluation" : False ,
74
+ }
75
+ ],
76
+ "Summaries" : [
77
+ {
78
+ "Label" : "HPE Container Platform" ,
79
+ "UnlimitedCapacity" : False ,
80
+ "TotalCapacity" : 240 ,
81
+ "UsedCapacity" : 24 ,
82
+ "AvailableCapacity" : 216 ,
83
+ "NextExpiration" : 1609286399000 ,
84
+ "NextExpirationDisplay" : "2020-12-29T23:59:59Z" ,
85
+ "LatestExpiration" : 1609286399000 ,
86
+ "LatestExpirationDisplay" : "2020-12-29T23:59:59Z" ,
87
+ "Valid" : True ,
88
+ "ValidationTime" : 1594758782000 ,
89
+ "RevalidateTime" : 1609286400000 ,
68
90
},
69
- status_code = 200 ,
70
- headers = dict (),
71
- )
72
- if args [0 ] == "https://127.0.0.1:8080/api/v2/hpelicense" :
73
- return MockResponse (
74
- json_data = {
75
- "_links" : {"self" : {"href" : "/api/v2/hpelicense" }},
76
- "Licenses" : [
77
- {
78
- "Label" : "The License" ,
79
- "Feature" : "HPE Machine Learning Ops" ,
80
- "Capacity" : 240 ,
81
- "UnlimitedCapacity" : False ,
82
- "Start" : 1566864000000 ,
83
- "StartDisplay" : "2019-08-27T00:00:00Z" ,
84
- "Expiration" : 1609286399000 ,
85
- "ExpirationDisplay" : "2020-12-29T23:59:59Z" ,
86
- "LicenseKey" : "TEST_LICENSE_KEY" ,
87
- "DeviceID" : "1234 1234" ,
88
- "Evaluation" : False ,
89
- }
90
- ],
91
- "Summaries" : [
92
- {
93
- "Label" : "HPE Container Platform" ,
94
- "UnlimitedCapacity" : False ,
95
- "TotalCapacity" : 240 ,
96
- "UsedCapacity" : 24 ,
97
- "AvailableCapacity" : 216 ,
98
- "NextExpiration" : 1609286399000 ,
99
- "NextExpirationDisplay" : "2020-12-29T23:59:59Z" ,
100
- "LatestExpiration" : 1609286399000 ,
101
- "LatestExpirationDisplay" : "2020-12-29T23:59:59Z" ,
102
- "Valid" : True ,
103
- "ValidationTime" : 1594758782000 ,
104
- "RevalidateTime" : 1609286400000 ,
105
- },
106
- {
107
- "Label" : "HPE Machine Learning Ops" ,
108
- "UnlimitedCapacity" : False ,
109
- "TotalCapacity" : 240 ,
110
- "UsedCapacity" : 0 ,
111
- "AvailableCapacity" : 240 ,
112
- "NextExpiration" : 1609286399000 ,
113
- "NextExpirationDisplay" : "2020-12-29T23:59:59Z" ,
114
- "LatestExpiration" : 1609286399000 ,
115
- "LatestExpirationDisplay" : "2020-12-29T23:59:59Z" ,
116
- "Valid" : True ,
117
- "ValidationTime" : 1594758782000 ,
118
- "RevalidateTime" : 1609286400000 ,
119
- },
120
- ],
121
- "Messages" : [],
91
+ {
92
+ "Label" : "HPE Machine Learning Ops" ,
93
+ "UnlimitedCapacity" : False ,
94
+ "TotalCapacity" : 240 ,
95
+ "UsedCapacity" : 0 ,
96
+ "AvailableCapacity" : 240 ,
97
+ "NextExpiration" : 1609286399000 ,
98
+ "NextExpirationDisplay" : "2020-12-29T23:59:59Z" ,
99
+ "LatestExpiration" : 1609286399000 ,
100
+ "LatestExpirationDisplay" : "2020-12-29T23:59:59Z" ,
122
101
"Valid" : True ,
123
- "Enabled" : True ,
124
102
"ValidationTime" : 1594758782000 ,
125
103
"RevalidateTime" : 1609286400000 ,
126
104
},
127
- status_code = 200 ,
128
- headers = dict (),
129
- )
130
- raise RuntimeError ("Unhandle GET request: " + args [0 ])
105
+ ],
106
+ "Messages" : [],
107
+ "Valid" : True ,
108
+ "Enabled" : True ,
109
+ "ValidationTime" : 1594758782000 ,
110
+ "RevalidateTime" : 1609286400000 ,
111
+ },
112
+ status_code = 200 ,
113
+ headers = dict (),
114
+ ),
115
+ )
116
+
131
117
132
- @patch ("requests.post" , side_effect = mocked_login_post )
133
- @patch ("requests.get" , side_effect = mocked_requests_get )
118
+ class TestCLI (BaseTestCase ):
119
+ @patch ("requests.post" , side_effect = BaseTestCase .httpPostHandlers )
120
+ @patch ("requests.get" , side_effect = BaseTestCase .httpGetHandlers )
134
121
def test_list (self , mock_post , mock_get ):
135
122
136
123
try :
@@ -198,8 +185,8 @@ def test_list(self, mock_post, mock_get):
198
185
if six .PY2 :
199
186
self .assertEqual (stderr , expected_stderr )
200
187
201
- @patch ("requests.post" , side_effect = mocked_login_post )
202
- @patch ("requests.get" , side_effect = mocked_requests_get )
188
+ @patch ("requests.post" , side_effect = BaseTestCase . httpPostHandlers )
189
+ @patch ("requests.get" , side_effect = BaseTestCase . httpGetHandlers )
203
190
def test_list_license_key_only (self , mock_post , mock_get ):
204
191
205
192
try :
@@ -221,8 +208,8 @@ def test_list_license_key_only(self, mock_post, mock_get):
221
208
if six .PY2 :
222
209
self .assertEqual (stderr , expected_stderr )
223
210
224
- @patch ("requests.post" , side_effect = mocked_login_post )
225
- @patch ("requests.get" , side_effect = mocked_requests_get )
211
+ @patch ("requests.post" , side_effect = BaseTestCase . httpPostHandlers )
212
+ @patch ("requests.get" , side_effect = BaseTestCase . httpGetHandlers )
226
213
def test_list_output_json (self , mock_post , mock_get ):
227
214
228
215
self .maxDiff = None
@@ -302,8 +289,8 @@ def test_list_output_json(self, mock_post, mock_get):
302
289
if six .PY2 :
303
290
self .assertEqual (stderr , expected_stderr )
304
291
305
- @patch ("requests.post" , side_effect = mocked_login_post )
306
- @patch ("requests.get" , side_effect = mocked_requests_get )
292
+ @patch ("requests.post" , side_effect = BaseTestCase . httpPostHandlers )
293
+ @patch ("requests.get" , side_effect = BaseTestCase . httpGetHandlers )
307
294
def test_platform_id (self , mock_post , mock_get ):
308
295
309
296
try :
@@ -325,8 +312,8 @@ def test_platform_id(self, mock_post, mock_get):
325
312
if six .PY2 :
326
313
self .assertEqual (stderr , expected_stderr )
327
314
328
- @patch ("requests.post" , side_effect = mocked_login_post )
329
- @patch ("requests.get" , side_effect = mocked_requests_get )
315
+ @patch ("requests.post" , side_effect = BaseTestCase . httpPostHandlers )
316
+ @patch ("requests.get" , side_effect = BaseTestCase . httpGetHandlers )
330
317
def test_register (self , mock_post , mock_get ):
331
318
332
319
try :
@@ -348,16 +335,8 @@ def test_register(self, mock_post, mock_get):
348
335
if six .PY2 :
349
336
self .assertEqual (stderr , expected_stderr )
350
337
351
- def mocked_requests_delete (* args , ** kwargs ):
352
- if (
353
- args [0 ]
354
- == "https://127.0.0.1:8080/api/v2/hpelicense/TEST_LICENSE_KEY/"
355
- ):
356
- return MockResponse (json_data = {}, status_code = 200 , headers = dict (),)
357
- raise RuntimeError ("Unhandle DELETE request: " + args [0 ])
358
-
359
- @patch ("requests.post" , side_effect = mocked_login_post )
360
- @patch ("requests.delete" , side_effect = mocked_requests_delete )
338
+ @patch ("requests.post" , side_effect = BaseTestCase .httpPostHandlers )
339
+ @patch ("requests.delete" , side_effect = BaseTestCase .httpDeleteHandlers )
361
340
def test_delete (self , mock_post , mock_delete ):
362
341
363
342
with patch .dict ("os.environ" , {"LOG_LEVEL" : "DEBUG" }):
@@ -376,9 +355,9 @@ def test_delete(self, mock_post, mock_delete):
376
355
if six .PY2 :
377
356
self .assertEqual (stderr , expected_stderr )
378
357
379
- @patch ("requests.post" , side_effect = mocked_login_post )
380
- @patch ("requests.delete" , side_effect = mocked_requests_delete )
381
- @patch ("requests.get" , side_effect = mocked_requests_get )
358
+ @patch ("requests.post" , side_effect = BaseTestCase . httpPostHandlers )
359
+ @patch ("requests.delete" , side_effect = BaseTestCase . httpDeleteHandlers )
360
+ @patch ("requests.get" , side_effect = BaseTestCase . httpGetHandlers )
382
361
def test_delete_all (self , mock_post , mock_delete , mock_get ):
383
362
384
363
with patch .dict ("os.environ" , {"LOG_LEVEL" : "DEBUG" }):
0 commit comments