Skip to content

Commit

Permalink
testing done
Browse files Browse the repository at this point in the history
  • Loading branch information
nforsg committed Aug 8, 2023
1 parent db51313 commit dfa00da
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Routes and sub-resources for the /simulations resource
"""
import base64
import logging
from typing import Tuple

import csle_common.constants.constants as constants
Expand All @@ -13,8 +12,6 @@
import csle_rest_api.constants.constants as api_constants
import csle_rest_api.util.rest_api_util as rest_api_util

logger = logging.getLogger()

# Creates a blueprint "sub application" of the main REST app
simulations_bp = Blueprint(
api_constants.MGMT_WEBAPP.SIMULATIONS_RESOURCE, __name__,
Expand Down Expand Up @@ -80,24 +77,26 @@ def simulation_ids() -> Tuple[Response, int]:
api_constants.MGMT_WEBAPP.HTTP_REST_DELETE])
def get_simulation(simulation_id: int) -> Tuple[Response, int]:
"""
The /simulations/id resource. Gets or delets a simulation with a given id.
The /simulations/id resource. Gets or deletes a simulation with a given id.
:param simulation_id: the id of the simulation
:return: the simulation or deletes the simulation
"""
requires_admin = False
if request.method == api_constants.MGMT_WEBAPP.HTTP_REST_DELETE:
requires_admin = True

authorized = rest_api_util.check_if_user_is_authorized(request=request, requires_admin=requires_admin)
if authorized is not None:
return authorized

simulation = MetastoreFacade.get_simulation(simulation_id)
sim_name_img = MetastoreFacade.get_simulation_image(simulation_name=simulation.name)
sim_name, img = sim_name_img
simulation.image = base64.b64encode(img).decode()
response = jsonify({})

if simulation is not None:

if request.method == api_constants.MGMT_WEBAPP.HTTP_REST_GET:
response = jsonify(simulation.to_dict())
elif request.method == api_constants.MGMT_WEBAPP.HTTP_REST_DELETE:
Expand Down
184 changes: 159 additions & 25 deletions simulation-system/libs/csle-rest-api/tests/test_resources_simulations.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import base64
import json
import logging
from typing import List, Tuple

import csle_common.constants.constants as constants
import pytest
import pytest_mock
from csle_cluster.cluster_manager.cluster_manager_pb2 import NodeStatusDTO
from csle_common.dao.emulation_config.config import Config
from csle_common.dao.simulation_config.action import Action
from csle_common.dao.simulation_config.action_space_config import ActionSpaceConfig
from csle_common.dao.simulation_config.env_parameter import EnvParameter
Expand Down Expand Up @@ -48,7 +46,6 @@
import csle_rest_api.constants.constants as api_constants
from csle_rest_api.rest_api import create_app

logger = logging.getLogger()

class TestResourcesSimulationsSuite:
"""
Expand Down Expand Up @@ -138,7 +135,10 @@ def get_ex_sim_env() -> SimulationEnvConfig:
player_id=1, observation_component_name_to_index={"JohnDoe": 1},
observation_id_to_observation_id_vector={1: ["null"]},
observation_id_to_observation_vector={1: ["null"]},
component_observations={"JohnDoe": [Observation(id=1, val=10, descr="null")]})
component_observations={"JohnDoe": [Observation(id=1, val=10, descr="null")
]})
of_config = ObservationFunctionConfig(observation_tensor=[], component_observation_tensors={"null": []})
isd_config = InitialStateDistributionConfig(initial_state_distribution=[1.0])
sim_env = SimulationEnvConfig(name="JohnDoe", descr="null", version="null",
gym_env_name="null",
simulation_env_input_config=SimulationEnvInputConfig,
Expand All @@ -156,14 +156,12 @@ def get_ex_sim_env() -> SimulationEnvConfig:
time_step_type=TimeStepType.DISCRETE.value,
reward_function_config=RewardFunctionConfig(reward_tensor=[]),
transition_operator_config=TransitionOperatorConfig(transition_tensor=[]),
observation_function_config=ObservationFunctionConfig(observation_tensor=[],
component_observation_tensors={
"null": []}
),
initial_state_distribution_config=InitialStateDistributionConfig(initial_state_distribution=[1.0]),
observation_function_config=of_config,
initial_state_distribution_config=isd_config,
env_parameters_config=EnvParametersConfig(parameters=[EnvParameter(id=1,
name="JohnDoe",
descr="null")]),
descr="null")
]),
plot_transition_probabilities=False,
plot_observation_function=False, plot_reward_function=False)
return sim_env
Expand All @@ -178,6 +176,8 @@ def get_sim_im(self, mocker):
"""
def get_simulation_image(simulation_name: str) -> Tuple[str, bytes]:
return ("null", b'null')
get_simulation_image_mocker = mocker.MagicMock(side_effect=get_simulation_image)
return get_simulation_image_mocker

def test_simulations_get(self, mocker, flask_app, list_sim, list_sim_im,
uninstall, list_sim_id, get_sim, get_sim_im,
Expand Down Expand Up @@ -218,13 +218,19 @@ def test_simulations_get(self, mocker, flask_app, list_sim, list_sim_im,
ex_data = TestResourcesSimulationsSuite.get_ex_sim_env()
ex_data_dict = ex_data.to_dict()

response_data_dict["joint_observation_space_config"]["observation_spaces"][0]['observation_id_to_observation_id_vector'][1] = \
response_data_dict["joint_observation_space_config"]["observation_spaces"][0]['observation_id_to_observation_id_vector']['1']
del response_data_dict["joint_observation_space_config"]["observation_spaces"][0]['observation_id_to_observation_id_vector']['1']
response_data_dict["joint_observation_space_config"]["observation_spaces"][0][
'observation_id_to_observation_id_vector'][1] = \
response_data_dict["joint_observation_space_config"]["observation_spaces"][0][
'observation_id_to_observation_id_vector']['1']
del response_data_dict["joint_observation_space_config"]["observation_spaces"][0][
'observation_id_to_observation_id_vector']['1']

response_data_dict["joint_observation_space_config"]["observation_spaces"][0]['observation_id_to_observation_vector'][1] = \
response_data_dict["joint_observation_space_config"]["observation_spaces"][0]['observation_id_to_observation_vector']['1']
del response_data_dict["joint_observation_space_config"]["observation_spaces"][0]['observation_id_to_observation_vector']['1']
response_data_dict["joint_observation_space_config"]["observation_spaces"][0][
'observation_id_to_observation_vector'][1] = \
response_data_dict["joint_observation_space_config"]["observation_spaces"][0][
'observation_id_to_observation_vector']['1']
del response_data_dict["joint_observation_space_config"]["observation_spaces"][0][
'observation_id_to_observation_vector']['1']
for k in response_data_dict:
assert response_data_dict[k] == ex_data_dict[k]
mocker.patch("csle_common.metastore.metastore_facade.MetastoreFacade.list_simulation_ids",
Expand All @@ -244,13 +250,19 @@ def test_simulations_get(self, mocker, flask_app, list_sim, list_sim_im,
response_data_list = json.loads(response_data)
response_data_dict = response_data_list[0]
assert response.status_code == constants.HTTPS.OK_STATUS_CODE
response_data_dict["joint_observation_space_config"]["observation_spaces"][0]['observation_id_to_observation_id_vector'][1] = \
response_data_dict["joint_observation_space_config"]["observation_spaces"][0]['observation_id_to_observation_id_vector']['1']
del response_data_dict["joint_observation_space_config"]["observation_spaces"][0]['observation_id_to_observation_id_vector']['1']
response_data_dict["joint_observation_space_config"]["observation_spaces"][0][
'observation_id_to_observation_id_vector'][1] = \
response_data_dict["joint_observation_space_config"]["observation_spaces"][0][
'observation_id_to_observation_id_vector']['1']
del response_data_dict["joint_observation_space_config"]["observation_spaces"][0][
'observation_id_to_observation_id_vector']['1']

response_data_dict["joint_observation_space_config"]["observation_spaces"][0]['observation_id_to_observation_vector'][1] = \
response_data_dict["joint_observation_space_config"]["observation_spaces"][0]['observation_id_to_observation_vector']['1']
del response_data_dict["joint_observation_space_config"]["observation_spaces"][0]['observation_id_to_observation_vector']['1']
response_data_dict["joint_observation_space_config"]["observation_spaces"][0][
'observation_id_to_observation_vector'][1] = \
response_data_dict["joint_observation_space_config"]["observation_spaces"][0][
'observation_id_to_observation_vector']['1']
del response_data_dict["joint_observation_space_config"]["observation_spaces"][0][
'observation_id_to_observation_vector']['1']
for k in response_data_dict:
assert response_data_dict[k] == ex_data_dict[k]
response = flask_app.test_client().get(f"{api_constants.MGMT_WEBAPP.SIMULATIONS_RESOURCE}"
Expand All @@ -266,7 +278,7 @@ def test_simulations_delete(self, mocker, flask_app, list_sim, list_sim_im,
uninstall, list_sim_id, get_sim, get_sim_im,
not_logged_in, logged_in, logged_in_as_admin) -> None:
"""
Testing the GET HTTPS method of the /simulations resource
Testing the DELETE HTTPS method of the /simulations resource
:param mocker: the pytest mocker object
:param flask_app: the flask_app fixture
Expand Down Expand Up @@ -319,4 +331,126 @@ def test_simulations_delete(self, mocker, flask_app, list_sim, list_sim_im,

def test_simulations_get_id(self, mocker, flask_app, list_sim, list_sim_im,
uninstall, list_sim_id, get_sim, get_sim_im,
not_logged_in, logged_in, logged_in_as_admin):
not_logged_in, logged_in, logged_in_as_admin):
"""
Testing the GET HTTPS method of the /simulations/id resource
:param mocker: the pytest mocker object
:param flask_app: the flask_app fixture
:param list_sim: the list_sim fixture
:param list_sim_im: the list_sim_im fixture
:param uninstall: the uninstall fixture
:param list_sim_id: the list_sim_id fixture
:param get_sim: the get_sim fixture
:param get_sim_sim: the get_sim_im fixture
:return: None
"""
mocker.patch("csle_common.metastore.metastore_facade.MetastoreFacade.get_simulation",
side_effect=get_sim)
mocker.patch("csle_common.metastore.metastore_facade.MetastoreFacade.get_simulation_image",
side_effect=get_sim_im)
mocker.patch("csle_common.controllers.simulation_env_controller.SimulationEnvController.uninstall_simulation",
side_effect=uninstall)
mocker.patch("csle_common.metastore.metastore_facade.MetastoreFacade.list_simulation_ids",
side_effect=list_sim_id)
mocker.patch("csle_rest_api.util.rest_api_util.check_if_user_is_authorized",
side_effect=not_logged_in)
response = flask_app.test_client().get(f"{api_constants.MGMT_WEBAPP.SIMULATIONS_RESOURCE}/-1)")
response_data = response.data.decode("utf-8")
response_data_dict = json.loads(response_data)
assert response.status_code == constants.HTTPS.UNAUTHORIZED_STATUS_CODE
assert response_data_dict == {}
mocker.patch("csle_rest_api.util.rest_api_util.check_if_user_is_authorized",
side_effect=logged_in)
response = flask_app.test_client().get(f"{api_constants.MGMT_WEBAPP.SIMULATIONS_RESOURCE}/10")
response_data = response.data.decode("utf-8")
response_data_dict = json.loads(response_data)
ex_data = TestResourcesSimulationsSuite.get_ex_sim_env()
ex_data.image = base64.b64encode(b'null').decode()
ex_data_dict = ex_data.to_dict()
assert response.status_code == constants.HTTPS.OK_STATUS_CODE
response_data_dict["joint_observation_space_config"]["observation_spaces"][0][
'observation_id_to_observation_id_vector'][1] = \
response_data_dict["joint_observation_space_config"]["observation_spaces"][0][
'observation_id_to_observation_id_vector']['1']
del response_data_dict["joint_observation_space_config"]["observation_spaces"][0][
'observation_id_to_observation_id_vector']['1']

response_data_dict["joint_observation_space_config"]["observation_spaces"][0][
'observation_id_to_observation_vector'][1] = \
response_data_dict["joint_observation_space_config"]["observation_spaces"][0][
'observation_id_to_observation_vector']['1']
del response_data_dict["joint_observation_space_config"]["observation_spaces"][0][
'observation_id_to_observation_vector']['1']
for k in response_data_dict:
assert response_data_dict[k] == ex_data_dict[k]
mocker.patch("csle_rest_api.util.rest_api_util.check_if_user_is_authorized",
side_effect=logged_in_as_admin)
response = flask_app.test_client().get(f"{api_constants.MGMT_WEBAPP.SIMULATIONS_RESOURCE}/10")
response_data = response.data.decode("utf-8")
response_data_dict = json.loads(response_data)
ex_data = TestResourcesSimulationsSuite.get_ex_sim_env()
ex_data.image = base64.b64encode(b'null').decode()
ex_data_dict = ex_data.to_dict()
assert response.status_code == constants.HTTPS.OK_STATUS_CODE
response_data_dict["joint_observation_space_config"]["observation_spaces"][0][
'observation_id_to_observation_id_vector'][1] = \
response_data_dict["joint_observation_space_config"]["observation_spaces"][0][
'observation_id_to_observation_id_vector']['1']
del response_data_dict["joint_observation_space_config"]["observation_spaces"][0][
'observation_id_to_observation_id_vector']['1']

response_data_dict["joint_observation_space_config"]["observation_spaces"][0][
'observation_id_to_observation_vector'][1] = \
response_data_dict["joint_observation_space_config"]["observation_spaces"][0][
'observation_id_to_observation_vector']['1']
del response_data_dict["joint_observation_space_config"]["observation_spaces"][0][
'observation_id_to_observation_vector']['1']
for k in response_data_dict:
assert response_data_dict[k] == ex_data_dict[k]

def test_simulations_delete_id(self, mocker, flask_app, list_sim, list_sim_im,
uninstall, list_sim_id, get_sim, get_sim_im,
not_logged_in, logged_in, logged_in_as_admin):
"""
Testing the DELETE HTTPS method of the /simulations/id resource
:param mocker: the pytest mocker object
:param flask_app: the flask_app fixture
:param list_sim: the list_sim fixture
:param list_sim_im: the list_sim_im fixture
:param uninstall: the uninstall fixture
:param list_sim_id: the list_sim_id fixture
:param get_sim: the get_sim fixture
:param get_sim_sim: the get_sim_im fixture
:return: None
"""
mocker.patch("csle_common.metastore.metastore_facade.MetastoreFacade.get_simulation",
side_effect=get_sim)
mocker.patch("csle_common.metastore.metastore_facade.MetastoreFacade.get_simulation_image",
side_effect=get_sim_im)
mocker.patch("csle_common.controllers.simulation_env_controller.SimulationEnvController.uninstall_simulation",
side_effect=uninstall)
mocker.patch("csle_common.metastore.metastore_facade.MetastoreFacade.list_simulation_ids",
side_effect=list_sim_id)
mocker.patch("csle_rest_api.util.rest_api_util.check_if_user_is_authorized",
side_effect=not_logged_in)
response = flask_app.test_client().delete(f"{api_constants.MGMT_WEBAPP.SIMULATIONS_RESOURCE}/-1)")
response_data = response.data.decode("utf-8")
response_data_dict = json.loads(response_data)
assert response.status_code == constants.HTTPS.UNAUTHORIZED_STATUS_CODE
assert response_data_dict == {}
mocker.patch("csle_rest_api.util.rest_api_util.check_if_user_is_authorized",
side_effect=logged_in)
response = flask_app.test_client().delete(f"{api_constants.MGMT_WEBAPP.SIMULATIONS_RESOURCE}/-1)")
response_data = response.data.decode("utf-8")
response_data_dict = json.loads(response_data)
assert response.status_code == constants.HTTPS.UNAUTHORIZED_STATUS_CODE
assert response_data_dict == {}
mocker.patch("csle_rest_api.util.rest_api_util.check_if_user_is_authorized",
side_effect=logged_in_as_admin)
response = flask_app.test_client().delete(f"{api_constants.MGMT_WEBAPP.SIMULATIONS_RESOURCE}/-1)")
response_data = response.data.decode("utf-8")
response_data_dict = json.loads(response_data)
assert response.status_code == constants.HTTPS.OK_STATUS_CODE
assert response_data_dict == {}

0 comments on commit dfa00da

Please sign in to comment.