Skip to content

Commit 29998c6

Browse files
committed
all tests passing
1 parent e324a77 commit 29998c6

File tree

9 files changed

+230
-39
lines changed

9 files changed

+230
-39
lines changed

cloudshell/sandbox_rest/sandbox_api.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -392,18 +392,21 @@ def get_execution_details(self, execution_id: str) -> dict:
392392
raise SandboxRestException(f"Failed to get execution details for '{execution_id}'", response)
393393
return response.json()
394394

395-
def delete_execution(self, execution_id: str) -> dict:
395+
def delete_execution(self, execution_id: str) -> None:
396+
"""
397+
API returns dict with single key on successful deletion of execution
398+
{"result": "success"}
399+
"""
396400
self._validate_auth_headers()
397401
response = requests.delete(f"{self._versioned_url}/executions/{execution_id}", headers=self._auth_headers)
398402
if not response.ok:
399403
raise SandboxRestException(f"Failed to delete execution for '{execution_id}'", response)
400-
return response.json()
404+
response_dict = response.json()
405+
if not response_dict["result"] == "success":
406+
raise SandboxRestException(f"Failed execution deletion of id {execution_id}\n"
407+
f"Response: {response_dict}")
401408

402409

403410
if __name__ == "__main__":
404411
admin_api = SandboxRestApiSession(host="localhost", username="admin", password="admin", domain="end_users")
405412
user_token = admin_api.get_token_for_target_user("end_user")
406-
user_api = SandboxRestApiSession(host="localhost", token=user_token)
407-
response = user_api.start_sandbox(blueprint_id="end user bp")
408-
commands = user_api.get_sandbox_command_details()
409-
pass

tests/common.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import json
2+
from cloudshell.sandbox_rest.sandbox_api import SandboxRestApiSession
23

34

45
def pretty_print_response(dict_response):
56
json_str = json.dumps(dict_response, indent=4)
67
print(f"\n{json_str}")
8+
9+
10+
def get_blueprint_id_from_name(api: SandboxRestApiSession, bp_name: str):
11+
res = api.get_blueprint_details(bp_name)
12+
return res["id"]

tests/conftest.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,29 @@
22

33
import pytest
44
from env_settings import *
5+
from constants import *
56

67
from cloudshell.sandbox_rest.sandbox_api import SandboxRestApiSession
78

89

910
@pytest.fixture(scope="session")
1011
def admin_session() -> SandboxRestApiSession:
11-
with SandboxRestApiSession(CLOUDSHELL_SERVER, CLOUDSHELL_ADMIN_USER, CLOUDSHELL_ADMIN_PASSWORD) as api:
12+
with SandboxRestApiSession(host=CLOUDSHELL_SERVER,
13+
username=CLOUDSHELL_ADMIN_USER,
14+
password=CLOUDSHELL_ADMIN_PASSWORD,
15+
domain=CLOUDSHELL_DOMAIN) as api:
1216
yield api
1317
time.sleep(2)
1418
print("admin session token revoked")
19+
20+
21+
@pytest.fixture(scope="session")
22+
def empty_blueprint():
23+
return DEFAULT_EMPTY_BLUEPRINT
24+
25+
26+
@pytest.fixture(scope="session")
27+
def dut_blueprint():
28+
return DUT_BLUEPRINT
29+
30+

tests/constants.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
DEFAULT_EMPTY_BLUEPRINT = "CloudShell Sandbox Template"
2+
BLUEPRINT_SETUP_COMMAND = "Setup"
3+
4+
# DUT blueprint constants
5+
DUT_MODEL = "Putshell"
6+
DUT_COMMAND = "health_check"
7+
DUT_BLUEPRINT = "DUT Blueprint Test"

tests/env_settings.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
CLOUDSHELL_ADMIN_USER=admin
44
CLOUDSHELL_ADMIN_PASSWORD=admin
55
CLOUDSHELL_SERVER=localhost
6+
CLOUDSHELL_DOMAIN=Global
67
"""
78

89
import os
@@ -15,8 +16,4 @@
1516
CLOUDSHELL_ADMIN_USER = os.environ.get("CLOUDSHELL_ADMIN_USER")
1617
CLOUDSHELL_ADMIN_PASSWORD = os.environ.get("CLOUDSHELL_ADMIN_PASSWORD")
1718
CLOUDSHELL_SERVER = os.environ.get("CLOUDSHELL_SERVER")
18-
19-
20-
DUT_RESOURCE = "DUT_1"
21-
DEFAULT_BLUEPRINT_TEMPLATE = "CloudShell Sandbox Template"
22-
HEALTH_CHECK_COMMAND = "health_check"
19+
CLOUDSHELL_DOMAIN = os.environ.get("CLOUDSHELL_DOMAIN")

tests/test_api _no_sandbox.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
11
"""
2-
Functionally test the api methods that do not require a live sandbox
3-
- get all blueprints
4-
- get all sandboxes
5-
- get blueprint by id
6-
- get token + delete token
2+
Test the api methods that do NOT require a blueprint
3+
Live Cloudshell server is still a dependency
74
"""
8-
from env_settings import DEFAULT_BLUEPRINT_TEMPLATE
5+
from constants import DEFAULT_EMPTY_BLUEPRINT
96

107
from cloudshell.sandbox_rest.sandbox_api import SandboxRestApiSession
118

129

1310
def test_get_sandboxes(admin_session: SandboxRestApiSession):
1411
res = admin_session.get_sandboxes()
12+
assert(type(res) is list)
1513
print(f"Sandbox count found in system: {len(res)}")
1614

1715

1816
def test_get_blueprints(admin_session: SandboxRestApiSession):
1917
bp_res = admin_session.get_blueprints()
18+
assert(type(bp_res) is list)
2019
print(f"Blueprint count found in system: '{len(bp_res)}'")
2120

2221

2322
def test_get_default_blueprint(admin_session: SandboxRestApiSession):
24-
bp_res = admin_session.get_blueprint_details(DEFAULT_BLUEPRINT_TEMPLATE)
23+
bp_res = admin_session.get_blueprint_details(DEFAULT_EMPTY_BLUEPRINT)
24+
assert(type(bp_res) is dict)
2525
bp_name = bp_res["name"]
2626
print(f"Pulled details for '{bp_name}'")
2727

2828

2929
def test_get_and_delete_token(admin_session: SandboxRestApiSession):
3030
""" get token for admin user """
3131
token_res = admin_session.get_token_for_target_user("admin")
32+
assert(type(token_res) is str)
3233
print(f"Token response: '{token_res}'")
33-
admin_session.delete_token(token_res)
34-
print("deleted token")

tests/test_api_empty_sandbox.py

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
"""
2-
Test the api methods that require a live sandbox against default
3-
- start sandbox
2+
Test the api methods that require an empty, PUBLIC blueprint
43
"""
5-
import pytest
6-
from env_settings import DEFAULT_BLUEPRINT_TEMPLATE
4+
import time
75

6+
import pytest
87
from cloudshell.sandbox_rest.sandbox_api import SandboxRestApiSession
8+
from constants import *
9+
from common import *
10+
11+
12+
@pytest.fixture(scope="module")
13+
def blueprint_id(admin_session: SandboxRestApiSession, empty_blueprint):
14+
res_id = get_blueprint_id_from_name(admin_session, empty_blueprint)
15+
assert(type(res_id) is str)
16+
return res_id
917

1018

1119
@pytest.fixture(scope="module")
12-
def sandbox_id(admin_session: SandboxRestApiSession):
20+
def sandbox_id(admin_session: SandboxRestApiSession, blueprint_id):
1321
# start sandbox
1422
start_res = admin_session.start_sandbox(
15-
blueprint_id=DEFAULT_BLUEPRINT_TEMPLATE, sandbox_name="Pytest empty blueprint test"
23+
blueprint_id=blueprint_id, sandbox_name="Pytest empty blueprint test"
1624
)
1725
sandbox_id = start_res["id"]
1826
print(f"Sandbox started: {sandbox_id}")
@@ -21,46 +29,77 @@ def sandbox_id(admin_session: SandboxRestApiSession):
2129
print(f"\nSandbox ended: {sandbox_id}")
2230

2331

32+
@pytest.fixture(scope="module")
33+
def setup_execution_id(admin_session: SandboxRestApiSession, sandbox_id: str):
34+
polling_minutes = 2
35+
counter = 0
36+
while True:
37+
if counter > polling_minutes:
38+
raise Exception("Timeout waiting for setup to end")
39+
state = admin_session.get_sandbox_details(sandbox_id)["state"]
40+
if state == "Ready":
41+
break
42+
time.sleep(60)
43+
counter += 1
44+
45+
print("Rerunning Setup...")
46+
res = admin_session.run_sandbox_command(sandbox_id=sandbox_id,
47+
command_name=BLUEPRINT_SETUP_COMMAND)
48+
assert (type(res) is dict)
49+
print("Setup re-run execution response")
50+
pretty_print_response(res)
51+
execution_id = res["executionId"]
52+
return execution_id
53+
54+
2455
def test_start_stop(admin_session, sandbox_id):
25-
pass
56+
assert (type(sandbox_id) is str)
57+
print(f"Sandbox ID: {sandbox_id}")
2658

2759

2860
def test_get_sandbox_details(admin_session, sandbox_id):
2961
details_res = admin_session.get_sandbox_details(sandbox_id)
62+
assert (type(details_res) is dict)
3063
sb_name = details_res["name"]
3164
print(f"Pulled details for sandbox '{sb_name}'")
3265

3366

3467
def test_get_components(admin_session, sandbox_id):
35-
sb_components = admin_session.get_sandbox_components(sandbox_id)
36-
component_count = len(sb_components)
68+
components_res = admin_session.get_sandbox_components(sandbox_id)
69+
assert (type(components_res) is list)
70+
component_count = len(components_res)
3771
print(f"component count found: {component_count}")
3872

3973

4074
def test_get_sandbox_commands(admin_session, sandbox_id):
41-
sb_commands = admin_session.get_sandbox_commands(sandbox_id)
42-
print(f"Sandbox commands: {[x['name'] for x in sb_commands]}")
43-
first_sb_command = admin_session.get_sandbox_command_details(sandbox_id, sb_commands[0]["name"])
75+
commands_res = admin_session.get_sandbox_commands(sandbox_id)
76+
assert (type(commands_res) is list)
77+
print(f"Sandbox commands: {[x['name'] for x in commands_res]}")
78+
first_sb_command = admin_session.get_sandbox_command_details(sandbox_id, commands_res[0]["name"])
4479
print(f"SB command name: {first_sb_command['name']}\n" f"description: {first_sb_command['description']}")
4580

4681

4782
def test_get_sandbox_events(admin_session, sandbox_id):
48-
activity = admin_session.get_sandbox_activity(sandbox_id)
49-
events = activity["events"]
83+
activity_res = admin_session.get_sandbox_activity(sandbox_id)
84+
assert (type(activity_res) is dict and "events" in activity_res)
85+
events = activity_res["events"]
5086
print(f"activity events count: {len(events)}")
5187

5288

5389
def test_get_console_output(admin_session, sandbox_id):
54-
sb_output = admin_session.get_sandbox_output(sandbox_id)
55-
entries = sb_output["entries"]
90+
output_res = admin_session.get_sandbox_output(sandbox_id)
91+
assert (type(output_res) is dict and "entries" in output_res)
92+
entries = output_res["entries"]
5693
print(f"Sandbox output entries count: {len(entries)}")
5794

5895

5996
def test_get_instructions(admin_session, sandbox_id):
60-
instructions = admin_session.get_sandbox_instructions(sandbox_id)
61-
print(f"Pulled sandbox instructions: '{instructions}'")
97+
instructions_res = admin_session.get_sandbox_instructions(sandbox_id)
98+
assert (type(instructions_res) is str)
99+
print(f"Pulled sandbox instructions: '{instructions_res}'")
62100

63101

64102
def test_extend_sandbox(admin_session, sandbox_id):
65103
extend_response = admin_session.extend_sandbox(sandbox_id, "PT0H10M")
104+
assert (type(extend_response) is dict and "remaining_time" in extend_response)
66105
print(f"extended sandbox. Remaining time: {extend_response['remaining_time']}")
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"""
2+
Test the api methods against blueprint with a resource containing a command
3+
- Putshell mock can be used - https://community.quali.com/repos/3318/put-shell-mock
4+
- DUT model / command can be referenced in constants.py (Putshell / health_check)
5+
- Assumed that only one DUT is in blueprint
6+
"""
7+
import time
8+
9+
import pytest
10+
from cloudshell.sandbox_rest.sandbox_api import SandboxRestApiSession
11+
from constants import *
12+
from common import *
13+
14+
15+
@pytest.fixture(scope="module")
16+
def blueprint_id(admin_session: SandboxRestApiSession, dut_blueprint):
17+
res_id = get_blueprint_id_from_name(admin_session, dut_blueprint)
18+
assert(type(res_id) is str)
19+
return res_id
20+
21+
22+
@pytest.fixture(scope="module")
23+
def sandbox_id(admin_session: SandboxRestApiSession, blueprint_id):
24+
# start sandbox
25+
start_res = admin_session.start_sandbox(
26+
blueprint_id=blueprint_id, sandbox_name="Pytest DUT blueprint test"
27+
)
28+
sandbox_id = start_res["id"]
29+
print(f"Sandbox started: {sandbox_id}")
30+
yield sandbox_id
31+
admin_session.stop_sandbox(sandbox_id)
32+
print(f"\nSandbox ended: {sandbox_id}")
33+
34+
35+
@pytest.fixture(scope="module")
36+
def component_id(admin_session: SandboxRestApiSession, sandbox_id: str, dut_blueprint: str):
37+
components = admin_session.get_sandbox_components(sandbox_id)
38+
component_filter = [x for x in components if x["component_type"] == DUT_MODEL]
39+
assert component_filter
40+
return component_filter[0]["id"]
41+
42+
43+
@pytest.fixture(scope="module")
44+
def execution_id(admin_session: SandboxRestApiSession, sandbox_id: str, component_id: str):
45+
print("Starting DUT Command...")
46+
res = admin_session.run_component_command(sandbox_id=sandbox_id,
47+
component_id=component_id,
48+
command_name=DUT_COMMAND)
49+
assert(type(res) is dict)
50+
print("Started execution response")
51+
pretty_print_response(res)
52+
execution_id = res["executionId"]
53+
return execution_id
54+
55+
56+
@pytest.fixture(scope="module")
57+
def test_get_execution_details(admin_session, execution_id):
58+
res = admin_session.get_execution_details(execution_id)
59+
assert(type(res) is dict)
60+
print("Execution Details")
61+
pretty_print_response(res)
62+
63+
64+
def test_delete_execution(admin_session, execution_id, test_get_execution_details):
65+
print("Stopping execution...")
66+
time.sleep(1)
67+
admin_session.delete_execution(execution_id)
68+
print("Execution deleted")
69+
70+

tests/test_rerun_setup.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""
2+
Test re-running setup to validate blueprint level commands, getting details, and then ending setup execution, finally end sandbox
3+
"""
4+
import time
5+
6+
import pytest
7+
from cloudshell.sandbox_rest.sandbox_api import SandboxRestApiSession
8+
from constants import *
9+
from common import *
10+
11+
12+
@pytest.fixture(scope="module")
13+
def sandbox_id(admin_session: SandboxRestApiSession, empty_blueprint):
14+
# start sandbox
15+
start_res = admin_session.start_sandbox(
16+
blueprint_id=empty_blueprint, sandbox_name="Pytest empty blueprint test"
17+
)
18+
sandbox_id = start_res["id"]
19+
print(f"Sandbox started: {sandbox_id}")
20+
yield sandbox_id
21+
admin_session.stop_sandbox(sandbox_id)
22+
print(f"\nSandbox ended: {sandbox_id}")
23+
24+
25+
@pytest.fixture(scope="module")
26+
def setup_execution_id(admin_session: SandboxRestApiSession, sandbox_id: str):
27+
polling_minutes = 2
28+
counter = 0
29+
while True:
30+
if counter > polling_minutes:
31+
raise Exception("Timeout waiting for setup to end")
32+
state = admin_session.get_sandbox_details(sandbox_id)["state"]
33+
if state == "Ready":
34+
break
35+
time.sleep(60)
36+
counter += 1
37+
38+
print("Rerunning Setup...")
39+
res = admin_session.run_sandbox_command(sandbox_id=sandbox_id,
40+
command_name=BLUEPRINT_SETUP_COMMAND)
41+
assert (type(res) is dict)
42+
print("Setup re-run execution response")
43+
pretty_print_response(res)
44+
execution_id = res["executionId"]
45+
return execution_id
46+
47+
48+
def test_cancel_setup(admin_session, setup_execution_id):
49+
print("Setup Execution Details")
50+
res = admin_session.get_execution_details(setup_execution_id)
51+
pretty_print_response(res)
52+
print("Ending setup execution")
53+
admin_session.delete_execution(setup_execution_id)
54+
print("Setup execution cancelled")

0 commit comments

Comments
 (0)