Skip to content

Commit 038cd3f

Browse files
committed
commiting tests
1 parent c67b632 commit 038cd3f

File tree

8 files changed

+53
-50
lines changed

8 files changed

+53
-50
lines changed

.pre-commit-config.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ repos:
3636
language: system
3737
types: [python]
3838
args: [
39-
--max-line-length=127,
40-
--max-public-methods=32,
41-
--max-args=8,
39+
# --max-line-length=127,
40+
# --max-public-methods=32,
41+
# --max-args=8,
4242
'--disable=too-few-public-methods,logging-fstring-interpolation,too-many-instance-attributes,no-else-return,too-many-locals,no-self-use,duplicate-code,broad-except,logging-not-lazy,unspecified-encoding',
4343
'--good-names=ip,rc,eval'
4444
]

cloudshell/sandbox_rest/exceptions.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
class SandboxRestException(Exception):
55
""" Base Exception Class inside Rest client class """
6+
67
def __init__(self, msg: str, response: Response = None):
78
self.msg = msg
89
self.response = response
@@ -18,11 +19,14 @@ def _format_err_msg(self, custom_err_msg="Failed Api Call", response: Response =
1819

1920
@staticmethod
2021
def _format_response_msg(response: Response):
21-
return (f"Response: {response.status_code}, Reason: {response.reason}\n"
22-
f"Request URL: {response.request.url}\n"
23-
f"Request Headers: {response.request.headers}")
22+
return (
23+
f"Response: {response.status_code}, Reason: {response.reason}\n"
24+
f"Request URL: {response.request.url}\n"
25+
f"Request Headers: {response.request.headers}"
26+
)
2427

2528

2629
class SandboxRestAuthException(SandboxRestException):
2730
""" Failed auth action """
31+
2832
pass

cloudshell/sandbox_rest/sandbox_api.py

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import json
22
from dataclasses import asdict, dataclass
33
from typing import List
4+
45
import requests
5-
from cloudshell.sandbox_rest.exceptions import SandboxRestException, SandboxRestAuthException
6+
7+
from cloudshell.sandbox_rest.exceptions import SandboxRestAuthException, SandboxRestException
68

79

810
@dataclass
@@ -22,8 +24,9 @@ class SandboxRestApiSession:
2224
View http://<API_SERVER>/api/v2/explore to see schemas of return json values
2325
"""
2426

25-
def __init__(self, host: str, username: str, password="", token="", domain="Global", port=82, is_https=False,
26-
api_version="v2"):
27+
def __init__(
28+
self, host: str, username: str, password="", token="", domain="Global", port=82, is_https=False, api_version="v2"
29+
):
2730
""" login to api and store headers for future requests """
2831
_protocol = "https" if is_https else "http"
2932
self._base_url = f"{_protocol}://{host}:{port}/api"
@@ -106,12 +109,12 @@ def delete_token(self, token_id: str) -> None:
106109

107110
# SANDBOX POST REQUESTS
108111
def start_sandbox(
109-
self,
110-
blueprint_id: str,
111-
sandbox_name="",
112-
duration="PT2H0M",
113-
bp_params: List[InputParam] = None,
114-
permitted_users: List[str] = None,
112+
self,
113+
blueprint_id: str,
114+
sandbox_name="",
115+
duration="PT2H0M",
116+
bp_params: List[InputParam] = None,
117+
permitted_users: List[str] = None,
115118
) -> dict:
116119
"""
117120
Create a sandbox from the provided blueprint id
@@ -139,8 +142,7 @@ def start_sandbox(
139142
return response.json()
140143

141144
def start_persistent_sandbox(
142-
self, blueprint_id: str, sandbox_name="", bp_params: List[InputParam] = None,
143-
permitted_users: List[str] = None
145+
self, blueprint_id: str, sandbox_name="", bp_params: List[InputParam] = None, permitted_users: List[str] = None
144146
) -> dict:
145147
""" Create a persistent sandbox from the provided blueprint id """
146148
self._validate_auth_headers()
@@ -159,8 +161,9 @@ def start_persistent_sandbox(
159161
raise SandboxRestException(err_msg, response)
160162
return response.json()
161163

162-
def run_sandbox_command(self, sandbox_id: str, command_name: str, params: List[InputParam] = None,
163-
print_output=True) -> dict:
164+
def run_sandbox_command(
165+
self, sandbox_id: str, command_name: str, params: List[InputParam] = None, print_output=True
166+
) -> dict:
164167
""" Run a sandbox level command """
165168
self._validate_auth_headers()
166169
url = f"{self._versioned_url}/sandboxes/{sandbox_id}/commands/{command_name}/start"
@@ -173,8 +176,7 @@ def run_sandbox_command(self, sandbox_id: str, command_name: str, params: List[I
173176
return response.json()
174177

175178
def run_component_command(
176-
self, sandbox_id: str, component_id: str, command_name: str, params: List[InputParam] = None,
177-
print_output: bool = True
179+
self, sandbox_id: str, component_id: str, command_name: str, params: List[InputParam] = None, print_output: bool = True
178180
) -> dict:
179181
""" Start a command on sandbox component """
180182
self._validate_auth_headers()
@@ -231,8 +233,9 @@ def get_sandbox_details(self, sandbox_id: str) -> dict:
231233
raise SandboxRestException(exc_msg, response)
232234
return response.json()
233235

234-
def get_sandbox_activity(self, sandbox_id: str, error_only=False, since="", from_event_id: int = None,
235-
tail: int = None) -> dict:
236+
def get_sandbox_activity(
237+
self, sandbox_id: str, error_only=False, since="", from_event_id: int = None, tail: int = None
238+
) -> dict:
236239
"""
237240
Get list of sandbox activity
238241
'since' - format must be a valid 'ISO 8601'. (e.g 'PT23H' or 'PT4H2M')
@@ -298,9 +301,7 @@ def get_sandbox_component_details(self, sandbox_id: str, component_id: str) -> d
298301
)
299302
if not response.ok:
300303
custom_err_msg = (
301-
f"Failed to get sandbox component details.\n"
302-
f"component id: '{component_id}'\n"
303-
f"sandbox id: '{sandbox_id}'"
304+
f"Failed to get sandbox component details.\n" f"component id: '{component_id}'\n" f"sandbox id: '{sandbox_id}'"
304305
)
305306
raise SandboxRestException(custom_err_msg, response)
306307
return response.json()
@@ -309,14 +310,12 @@ def get_sandbox_component_commands(self, sandbox_id: str, component_id: str) ->
309310
""" Get list of commands for a particular component in sandbox """
310311
self._validate_auth_headers()
311312
response = requests.get(
312-
f"{self._versioned_url}/sandboxes/{sandbox_id}/components/{component_id}/commands",
313-
headers=self._auth_headers
313+
f"{self._versioned_url}/sandboxes/{sandbox_id}/components/{component_id}/commands", headers=self._auth_headers
314314
)
315315
if not response.ok:
316316
custom_err_msg = (
317-
f"Failed to get component commands.\n"
318-
f"component id: '{component_id}'\n"
319-
f"sandbox id: '{sandbox_id}'")
317+
f"Failed to get component commands.\n" f"component id: '{component_id}'\n" f"sandbox id: '{sandbox_id}'"
318+
)
320319
raise SandboxRestException(custom_err_msg, response)
321320
return response.json()
322321

@@ -328,24 +327,22 @@ def get_sandbox_component_command_details(self, sandbox_id: str, component_id: s
328327
headers=self._auth_headers,
329328
)
330329
if not response.ok:
331-
custom_err_msg = (f"Failed to get command details.\n "
332-
f"component id: '{component_id}'\n"
333-
f"sandbox id: '{sandbox_id}'")
330+
custom_err_msg = (
331+
f"Failed to get command details.\n " f"component id: '{component_id}'\n" f"sandbox id: '{sandbox_id}'"
332+
)
334333
raise SandboxRestException(custom_err_msg, response)
335334
return response.json()
336335

337336
def get_sandbox_instructions(self, sandbox_id: str) -> str:
338337
""" pull the instruction text of sandbox """
339338
self._validate_auth_headers()
340-
response = requests.get(f"{self._versioned_url}/sandboxes/{sandbox_id}/instructions",
341-
headers=self._auth_headers)
339+
response = requests.get(f"{self._versioned_url}/sandboxes/{sandbox_id}/instructions", headers=self._auth_headers)
342340
if not response.ok:
343341
err_msg = f"Failed to get sandbox instructions for '{sandbox_id}'"
344342
raise SandboxRestException(err_msg, response)
345343
return response.json()
346344

347-
def get_sandbox_output(self, sandbox_id: str, tail: int = None, from_entry_id: int = None,
348-
since: str = None) -> dict:
345+
def get_sandbox_output(self, sandbox_id: str, tail: int = None, from_entry_id: int = None, since: str = None) -> dict:
349346
""" Get list of sandbox output """
350347
self._validate_auth_headers()
351348
url = f"{self._versioned_url}/sandboxes/{sandbox_id}/output"

test-requirements.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
pytest
2-
python-dotenv
2+
python-dotenv
3+
flake8
4+
pylint

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import pytest
44
from env_settings import *
5+
56
from cloudshell.sandbox_rest.sandbox_api import SandboxRestApiSession
67

78

@@ -11,4 +12,3 @@ def admin_session() -> SandboxRestApiSession:
1112
yield api
1213
time.sleep(2)
1314
print("admin session token revoked")
14-

tests/env_settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"""
77

88
import os
9+
910
from dotenv import load_dotenv
1011

1112
load_dotenv()
@@ -19,4 +20,3 @@
1920
DUT_RESOURCE = "DUT_1"
2021
DEFAULT_BLUEPRINT_TEMPLATE = "CloudShell Sandbox Template"
2122
HEALTH_CHECK_COMMAND = "health_check"
22-

tests/test_api _no_sandbox.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
- get blueprint by id
66
- get token + delete token
77
"""
8-
from cloudshell.sandbox_rest.sandbox_api import SandboxRestApiSession
98
from env_settings import DEFAULT_BLUEPRINT_TEMPLATE
109

10+
from cloudshell.sandbox_rest.sandbox_api import SandboxRestApiSession
11+
1112

1213
def test_get_sandboxes(admin_session: SandboxRestApiSession):
1314
res = admin_session.get_sandboxes()

tests/test_api_empty_sandbox.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22
Test the api methods that require a live sandbox against default
33
- start sandbox
44
"""
5-
from cloudshell.sandbox_rest.sandbox_api import SandboxRestApiSession
6-
from env_settings import DEFAULT_BLUEPRINT_TEMPLATE
75
import pytest
6+
from env_settings import DEFAULT_BLUEPRINT_TEMPLATE
7+
8+
from cloudshell.sandbox_rest.sandbox_api import SandboxRestApiSession
89

910

1011
@pytest.fixture(scope="module")
1112
def sandbox_id(admin_session: SandboxRestApiSession):
1213
# start sandbox
13-
start_res = admin_session.start_sandbox(blueprint_id=DEFAULT_BLUEPRINT_TEMPLATE,
14-
sandbox_name="Pytest empty blueprint test")
14+
start_res = admin_session.start_sandbox(
15+
blueprint_id=DEFAULT_BLUEPRINT_TEMPLATE, sandbox_name="Pytest empty blueprint test"
16+
)
1517
sandbox_id = start_res["id"]
1618
print(f"Sandbox started: {sandbox_id}")
1719
yield sandbox_id
@@ -39,8 +41,7 @@ def test_get_sandbox_commands(admin_session, sandbox_id):
3941
sb_commands = admin_session.get_sandbox_commands(sandbox_id)
4042
print(f"Sandbox commands: {[x['name'] for x in sb_commands]}")
4143
first_sb_command = admin_session.get_sandbox_command_details(sandbox_id, sb_commands[0]["name"])
42-
print(f"SB command name: {first_sb_command['name']}\n"
43-
f"description: {first_sb_command['description']}")
44+
print(f"SB command name: {first_sb_command['name']}\n" f"description: {first_sb_command['description']}")
4445

4546

4647
def test_get_sandbox_events(admin_session, sandbox_id):
@@ -63,5 +64,3 @@ def test_get_instructions(admin_session, sandbox_id):
6364
def test_extend_sandbox(admin_session, sandbox_id):
6465
extend_response = admin_session.extend_sandbox(sandbox_id, "PT0H10M")
6566
print(f"extended sandbox. Remaining time: {extend_response['remaining_time']}")
66-
67-

0 commit comments

Comments
 (0)