From e1608f1a28054d6315e259e6bee0851529eed6bf Mon Sep 17 00:00:00 2001 From: Vitalii Melnychuk Date: Thu, 24 Mar 2022 09:06:31 +0000 Subject: [PATCH] feat: add workspace_id for Run, RunConfiguration (#302) * fix: add unique email user validation * feat: add workspace_id for Run, RunConfiguration --- web/api/maestro_api/api_routes.py | 2 + web/api/maestro_api/controllers/run.py | 11 +- .../controllers/run_configuration.py | 16 ++- web/api/maestro_api/controllers/workspace.py | 8 ++ web/api/maestro_api/db/models/run.py | 2 + .../db/models/run_configuration.py | 2 + web/api/maestro_api/db/repo/run.py | 14 +++ web/api/maestro_api/swagger/template.yml | 18 +++ web/api/maestro_api/validation_schemas.py | 23 ++++ web/api/tests/routes/test_run.py | 41 ++++++- web/api/tests/routes/test_run_agent.py | 4 + .../tests/routes/test_run_configuration.py | 105 ++++++++++++------ web/api/tests/routes/test_run_metric.py | 10 ++ web/api/tests/routes/test_run_status.py | 14 +++ web/api/tests/test_scheduler.py | 4 + 15 files changed, 228 insertions(+), 46 deletions(-) diff --git a/web/api/maestro_api/api_routes.py b/web/api/maestro_api/api_routes.py index 4bc8197e..a16383c3 100644 --- a/web/api/maestro_api/api_routes.py +++ b/web/api/maestro_api/api_routes.py @@ -17,6 +17,7 @@ create_run_schema, update_run_schema, run_all_schema, + run_configuration_all_schema, run_configuration_create_schema, run_metric_all_schema, run_agent_update_schema, @@ -124,6 +125,7 @@ def run_configuration_create_one(*args, **kwargs): return run_configuration_controller.create_one(*args, **kwargs) @flask_app.route("/run_configurations", methods=["GET"]) + @validate_request(run_configuration_all_schema) @requires_auth() def run_configuration_all(*args, **kwargs): return run_configuration_controller.all(*args, **kwargs) diff --git a/web/api/maestro_api/controllers/run.py b/web/api/maestro_api/controllers/run.py index 871ecf49..86088db4 100644 --- a/web/api/maestro_api/controllers/run.py +++ b/web/api/maestro_api/controllers/run.py @@ -2,8 +2,7 @@ from maestro_api.db.models.run import Run from maestro_api.db.repo.run import RunRepository -from maestro_api.db.models.run_agent import RunAgent -from maestro_api.db.models.run_metric import RunMetric + from maestro_api.db.models.run_configuration import RunConfiguration from maestro_api.libs.flask.utils import ( @@ -25,9 +24,7 @@ def delete_one(self, run_id, user): run = get_obj_or_404(Run, id=run_id) - RunMetric.objects(run_id=run.id).delete() - RunAgent.objects(run_id=run.id).delete() - run.delete() + self.run_repo.delete_with_related(run) return jsonify(run.to_dict()) @@ -41,6 +38,7 @@ def get_one(self, run_id, user): def all(self, data, user): "Get all Run objects" + workspace_id = data.get("workspace_id", None) labels = data.get("labels", None) run_status = data.get("run_status", None) skip = int(data.get("skip", 0)) @@ -52,6 +50,9 @@ def all(self, data, user): if labels is not None: filter_query = filter_query & Q(labels__all=str_to_list(labels)) + if workspace_id is not None: + filter_query = filter_query & Q(workspace_id=workspace_id) + if run_status is not None: filter_query = filter_query & Q(run_status__in=str_to_list(run_status)) diff --git a/web/api/maestro_api/controllers/run_configuration.py b/web/api/maestro_api/controllers/run_configuration.py index 02daa71d..6cb074fd 100644 --- a/web/api/maestro_api/controllers/run_configuration.py +++ b/web/api/maestro_api/controllers/run_configuration.py @@ -1,7 +1,10 @@ +from mongoengine import Q + from maestro_api.db.models.agent import Agent from maestro_api.db.models.run_plan import RunPlan from maestro_api.db.models.custom_data import CustomData from maestro_api.db.models.run_configuration import RunConfiguration +from maestro_api.db.models.workspace import Workspace from maestro_api.libs.flask.utils import get_obj_or_404, jsonify_list_of_docs, jsonify @@ -16,6 +19,7 @@ def _get_create_update_data(self, data): ] run_plan = get_obj_or_404(RunPlan, id=data.get("run_plan_id")) + workspace = get_obj_or_404(Workspace, id=data.get("workspace_id")) custom_data_ids = [ get_obj_or_404(CustomData, id=custom_data_id).id @@ -34,6 +38,7 @@ def _get_create_update_data(self, data): "title": title, "agent_ids": agent_ids, "run_plan_id": run_plan.id, + "workspace_id": workspace.id, "hosts": hosts, "custom_data_ids": custom_data_ids, "custom_properties": custom_properties, @@ -54,10 +59,17 @@ def get_one(self, run_configuration_id, user): return jsonify(run_configuration.to_dict()) - def all(self, user): + def all(self, data, user): "Get all RunConfiguration objects" - run_configurations = RunConfiguration.objects() + workspace_id = data.get("workspace_id", None) + + filter_query = Q() + + if workspace_id is not None: + filter_query = filter_query & Q(workspace_id=workspace_id) + + run_configurations = RunConfiguration.objects.filter(filter_query) return jsonify_list_of_docs(run_configurations) diff --git a/web/api/maestro_api/controllers/workspace.py b/web/api/maestro_api/controllers/workspace.py index b3840830..f5fe77db 100644 --- a/web/api/maestro_api/controllers/workspace.py +++ b/web/api/maestro_api/controllers/workspace.py @@ -1,5 +1,8 @@ from maestro_api.db.models.workspace import Workspace from maestro_api.db.models.user import User, UserRole +from maestro_api.db.repo.run import RunRepository +from maestro_api.db.models.run import Run +from maestro_api.db.models.run_configuration import RunConfiguration from maestro_api.libs.flask.utils import ( @@ -13,6 +16,7 @@ class WorkspaceController: def __init__(self, flask_app=None): self.flask_app = flask_app + self.run_repo = RunRepository() def create_or_update_workspace(self, workspace, data): "Create Owrkspace and share it with Users" @@ -79,6 +83,10 @@ def delete_one(self, workspace_id, user): User.objects(workspace_ids__in=[workspace.id]).update( pull__workspace_ids=workspace.id ) + runs = Run.objects(workspace_id=workspace.id) + for run in runs: + self.run_repo.delete_with_related(run) + RunConfiguration.objects(workspace_id=workspace.id).delete() workspace.delete() diff --git a/web/api/maestro_api/db/models/run.py b/web/api/maestro_api/db/models/run.py index ab4553e9..eaa129b1 100644 --- a/web/api/maestro_api/db/models/run.py +++ b/web/api/maestro_api/db/models/run.py @@ -47,6 +47,7 @@ class Run(CreatedUpdatedDocumentMixin): choices=RunStatus.list(), ) run_plan_id = ObjectIdField(required=True) + workspace_id = ObjectIdField(required=True) agent_ids = ListField( required=True, @@ -91,6 +92,7 @@ def to_dict(self): "id": str(self.id), "run_configuration_id": str(self.run_configuration_id), "run_plan_id": str(self.run_plan_id), + "workspace_id": str(self.workspace_id), "agent_ids": [str(agent_id) for agent_id in self.agent_ids], "custom_data_ids": [ str(custom_data_id) for custom_data_id in self.custom_data_ids diff --git a/web/api/maestro_api/db/models/run_configuration.py b/web/api/maestro_api/db/models/run_configuration.py index 87c0b326..2172b10c 100644 --- a/web/api/maestro_api/db/models/run_configuration.py +++ b/web/api/maestro_api/db/models/run_configuration.py @@ -36,6 +36,7 @@ class RunConfigurationLoadProfile(EmbeddedDocument): class RunConfiguration(CreatedUpdatedDocumentMixin): title = StringField(required=True) run_plan_id = ObjectIdField(required=True) + workspace_id = ObjectIdField(required=True) agent_ids = ListField( required=True, field=ObjectIdField(), @@ -60,6 +61,7 @@ def to_dict(self): return { "id": str(self.id), "run_plan_id": str(self.run_plan_id), + "workspace_id": str(self.workspace_id), "agent_ids": [str(agent_id) for agent_id in self.agent_ids], "custom_data_ids": [ str(custom_data_id) for custom_data_id in self.custom_data_ids diff --git a/web/api/maestro_api/db/repo/run.py b/web/api/maestro_api/db/repo/run.py index 92a0ecbb..b7a82f8f 100644 --- a/web/api/maestro_api/db/repo/run.py +++ b/web/api/maestro_api/db/repo/run.py @@ -2,6 +2,8 @@ from maestro_api.db.models.run_configuration import RunConfiguration from maestro_api.db.models.agent import Agent from maestro_api.db.models.run_agent import RunAgent +from maestro_api.db.models.run_metric import RunMetric +from maestro_api.db.models.run_metric_label import RunMetricLabel class RunRepository: @@ -24,6 +26,7 @@ def create_run(self, run_configuration: RunConfiguration): new_run = Run( title=run_configuration.title, run_configuration_id=run_configuration.id, + workspace_id=run_configuration.workspace_id, agent_ids=run_configuration.agent_ids, run_plan_id=run_configuration.run_plan_id, custom_data_ids=run_configuration.custom_data_ids, @@ -46,3 +49,14 @@ def create_run(self, run_configuration: RunConfiguration): RunAgent.objects.insert(run_agents) return new_run + + def delete_with_related(self, run: Run): + "Delete Run and all related documents" + + RunMetric.objects(run_id=run.id).delete() + RunMetricLabel.objects(run_id=run.id).delete() + RunAgent.objects(run_id=run.id).delete() + + run.delete() + + return run diff --git a/web/api/maestro_api/swagger/template.yml b/web/api/maestro_api/swagger/template.yml index f2329841..a71b7079 100644 --- a/web/api/maestro_api/swagger/template.yml +++ b/web/api/maestro_api/swagger/template.yml @@ -468,6 +468,10 @@ paths: produces: - "application/json" parameters: + - in: query + name: workspace_id + description: "Wokspace ID" + type: string - in: query name: labels description: "List of Run labels should be included" @@ -665,6 +669,9 @@ paths: run_plan_id: type: string description: "Run Plan identifier" + workspace_id: + type: string + description: "Workspace identifier" agent_ids: type: array description: "List of server Agents IDs should be used in test" @@ -750,6 +757,7 @@ paths: description: "Returns list of RunConfiguration objects" 400: description: "Bad request" + /run_configuration/{run_configuration_id}: get: tags: @@ -902,6 +910,7 @@ paths: description: "Returns list of Run objects" 400: description: "Bad request" + /run_configurations: get: tags: @@ -913,6 +922,11 @@ paths: - "application/json" produces: - "application/json" + parameters: + - in: query + name: workspace_id + description: "Wokspace ID" + type: string responses: 200: schema: @@ -1668,6 +1682,8 @@ definitions: $ref: "#/components/schemas/RunStatus" run_plan_id: type: string + workspace_id: + type: string agent_ids: type: array items: string @@ -1760,6 +1776,8 @@ definitions: type: string run_plan_id: type: string + workspace_id: + type: string agent_ids: type: array items: string diff --git a/web/api/maestro_api/validation_schemas.py b/web/api/maestro_api/validation_schemas.py index edbac423..e505f3f9 100644 --- a/web/api/maestro_api/validation_schemas.py +++ b/web/api/maestro_api/validation_schemas.py @@ -42,6 +42,11 @@ run_all_schema = { "type": "object", "properties": { + "workspace_id": { + "type": "string", + "minLength": 12, + "maxLength": 24, + }, "run_status": {"type": "string", "enum": RunStatus.list()}, "notes": {"type": "string"}, "labels": { @@ -60,6 +65,18 @@ "additionalProperties": False, } +run_configuration_all_schema = { + "type": "object", + "properties": { + "workspace_id": { + "type": "string", + "minLength": 12, + "maxLength": 24, + }, + }, + "additionalProperties": False, +} + run_configuration_create_schema = { "type": "object", "properties": { @@ -69,6 +86,11 @@ "minLength": 12, "maxLength": 24, }, + "workspace_id": { + "type": "string", + "minLength": 12, + "maxLength": 24, + }, "agent_ids": { "type": "array", "items": {"type": "string", "minLength": 12, "maxLength": 24}, @@ -148,6 +170,7 @@ }, }, "required": [ + "workspace_id", "run_plan_id", "agent_ids", ], diff --git a/web/api/tests/routes/test_run.py b/web/api/tests/routes/test_run.py index 4aaec28c..e72e4343 100644 --- a/web/api/tests/routes/test_run.py +++ b/web/api/tests/routes/test_run.py @@ -10,6 +10,7 @@ def test_create_run(client): + workspace_id = "6076d1e3a216ff15b6e95e9a" run_plan_id = "6076d1e3a216ff15b6e95e9d" run_configuration_id = "6326d1e3a216ff15b6e95e9d" agent_ids = ["6076d1bfb28b871d6bdb6095"] @@ -23,12 +24,14 @@ def test_create_run(client): "title": title, "run_configuration_id": run_configuration_id, "run_plan_id": run_plan_id, + "workspace_id": workspace_id, "agent_ids": agent_ids, "labels": labels, } RunConfiguration( id=run_configuration_id, + workspace_id=workspace_id, title=title, run_plan_id=run_plan_id, agent_ids=agent_ids, @@ -55,6 +58,7 @@ def test_create_run(client): def test_create_run_with_agent_runs(client): + workspace_id = "6076d1e3a216ff15b6e95e9a" run_plan_id = "6076d1e3a216ff15b6e95e9d" run_configuration_id = "6326d1e3a216ff15b6e95e9d" @@ -66,6 +70,7 @@ def test_create_run_with_agent_runs(client): RunConfiguration( id=run_configuration_id, + workspace_id=workspace_id, title=title, run_plan_id=run_plan_id, agent_ids=[agent["id"] for agent in agents], @@ -101,6 +106,7 @@ def test_create_run_with_agent_runs(client): def test_create_run_with_hosts(client): + workspace_id = "6076d1e3a216ff15b6e95e9a" run_plan_id = "6076d1e3a216ff15b6e95e9d" agent_ids = ["6076d1bfb28b871d6bdb6095"] agent_hostname = "agent1.net" @@ -112,6 +118,7 @@ def test_create_run_with_hosts(client): available_in_response = { "title": title, + "workspace_id": workspace_id, "run_configuration_id": run_configuration_id, "run_status": RunStatus.PENDING.value, "run_plan_id": run_plan_id, @@ -122,6 +129,7 @@ def test_create_run_with_hosts(client): RunConfiguration( id=run_configuration_id, + workspace_id=workspace_id, title=title, run_plan_id=run_plan_id, agent_ids=agent_ids, @@ -166,6 +174,7 @@ def test_create_test_with_not_found_configuration(client): def test_get_run(client): run_configuration_id = "6326d1e3a216ff15b6e95e9d" + workspace_id = "6076d1e3a216ff15b6e95e9a" title = "some example title" run_id = "6076d1e3a216ff15b6e95e1f" run_plan_id = "6076d1e3a216ff15b6e95e9d" @@ -174,6 +183,7 @@ def test_get_run(client): data_to_create = { "id": run_id, "run_configuration_id": run_configuration_id, + "workspace_id": workspace_id, "title": title, "run_plan_id": run_plan_id, "agent_ids": agent_ids, @@ -236,7 +246,7 @@ def test_get_run_with_not_found_response(client): ], ) def test_udpate_run_status(client, run_status, started_at, finished_at): - + workspace_id = "6076d1e3a216ff15b6e95e9a" run_configuration_id = "6326d1e3a216ff15b6e95e9d" title = "some example title" run_id = "6076d1e3a216ff15b6e95e1f" @@ -245,6 +255,7 @@ def test_udpate_run_status(client, run_status, started_at, finished_at): Run( id=run_id, + workspace_id=workspace_id, run_configuration_id=run_configuration_id, title=title, run_plan_id=run_plan_id, @@ -275,7 +286,7 @@ def test_udpate_run_status(client, run_status, started_at, finished_at): def test_udpate_run_notes(client): - + workspace_id = "6076d1e3a216ff15b6e95e9a" run_configuration_id = "6326d1e3a216ff15b6e95e9d" title = "some example title" run_id = "6076d1e3a216ff15b6e95e1f" @@ -285,6 +296,7 @@ def test_udpate_run_notes(client): Run( id=run_id, + workspace_id=workspace_id, run_configuration_id=run_configuration_id, title=title, run_plan_id=run_plan_id, @@ -311,6 +323,7 @@ def test_udpate_run_notes(client): def test_udpate_run_labels(client): + workspace_id = "6076d1e3a216ff15b6e95e9a" run_configuration_id = "6326d1e3a216ff15b6e95e9d" title = "some example title" run_id = "6076d1e3a216ff15b6e95e1f" @@ -320,6 +333,7 @@ def test_udpate_run_labels(client): Run( id=run_id, + workspace_id=workspace_id, run_configuration_id=run_configuration_id, title=title, run_plan_id=run_plan_id, @@ -360,6 +374,7 @@ def test_update_run_with_not_found_response(client): def test_list_runs(client): "Returns list of test runs from the DB" + workspace_id = "6076d1e3a216ff15b6e95e9a" run_id = "6076d1e3a216ff15b6e95e1f" run_configuration_id = "6326d1e3a216ff15b6e95e9d" title = "some example title" @@ -369,6 +384,7 @@ def test_list_runs(client): run = Run( id=run_id, + workspace_id=workspace_id, run_configuration_id=run_configuration_id, title=title, run_plan_id=run_plan_id, @@ -394,7 +410,7 @@ def test_list_runs(client): def test_create_run_with_custom_properties(client): "Returns single test run when test run with properties created" - + workspace_id = "6076d1e3a216ff15b6e95e9a" run_configuration_id = "6326d1e3a216ff15b6e95e9d" title = "some example title" run_plan_id = "6076d1e3a216ff15b6e95e9d" @@ -412,6 +428,7 @@ def test_create_run_with_custom_properties(client): RunConfiguration( id=run_configuration_id, + workspace_id=workspace_id, title=title, run_plan_id=run_plan_id, agent_ids=agent_ids, @@ -437,6 +454,7 @@ def test_create_run_with_custom_properties(client): def test_delete_one(client): + workspace_id = "6076d1e3a216ff15b6e95e9a" run_configuration_id = "6326d1e3a216ff15b6e95e9d" title = "some example title" run_id = "6076d1e3a216ff15b6e95e1f" @@ -446,6 +464,7 @@ def test_delete_one(client): data_to_create = { "id": run_id, "run_configuration_id": run_configuration_id, + "workspace_id": workspace_id, "title": title, "run_plan_id": run_plan_id, "agent_ids": agent_ids, @@ -475,33 +494,38 @@ def test_run_delete_with_run_not_found(client): assert response.status_code == 404 -default_agents_data = [ +default_runs_data = [ dict( run_id="6076d69ba216ff15b6e95ea1", + workspace_id="6076d1e3a216ff15b6e95e9a", labels=["label1", "label2"], run_status=RunStatus.PENDING.value, started_at=datetime(2019, 1, 1), ), dict( run_id="6076d69ba216ff15b6e95ea2", + workspace_id="6076d1e3a216ff15b6e95e9a", labels=["label1"], run_status=RunStatus.RUNNING.value, started_at=datetime(2019, 2, 1), ), dict( run_id="6076d69ba216ff15b6e95ea3", + workspace_id="6076d1e3a216ff15b6e95e9a", labels=[], run_status=RunStatus.RUNNING.value, started_at=datetime(2019, 3, 1), ), dict( run_id="6076d69ba216ff15b6e95ea4", + workspace_id="6076d1e3a216ff15b6e95e9a", labels=["label2"], run_status=RunStatus.RUNNING.value, started_at=datetime(2019, 4, 1), ), dict( run_id="6076d69ba216ff15b6e95ea5", + workspace_id="6076d1e3a216ff15b6e95e8a", labels=["label1", "label2"], run_status=RunStatus.FINISHED.value, started_at=datetime(2019, 5, 1), @@ -511,7 +535,7 @@ def test_run_delete_with_run_not_found(client): @pytest.mark.parametrize( "db_data", - [default_agents_data], + [default_runs_data], ) @pytest.mark.parametrize( "input_params,extected_ids", @@ -551,6 +575,12 @@ def test_run_delete_with_run_not_found(client): "6076d69ba216ff15b6e95ea4", ], ), + ( + "?workspace_id=6076d1e3a216ff15b6e95e8a", + [ + "6076d69ba216ff15b6e95ea5", + ], + ), ], ) def test_run_list(client, db_data, input_params, extected_ids): @@ -562,6 +592,7 @@ def test_run_list(client, db_data, input_params, extected_ids): Run( id=document["run_id"], + workspace_id=document["workspace_id"], run_configuration_id=run_configuration_id, title=title, run_plan_id=run_plan_id, diff --git a/web/api/tests/routes/test_run_agent.py b/web/api/tests/routes/test_run_agent.py index 5caab9c1..067a37c0 100644 --- a/web/api/tests/routes/test_run_agent.py +++ b/web/api/tests/routes/test_run_agent.py @@ -136,6 +136,7 @@ def test_run_agent_list_bad_response(client): ) def test_run_agent_update(client, agent_status, error_message): run_agent_id = "6076d69ba216ff15b6e95ea2" + workspace_id = "6076d1e3a216ff15b6e95e9a" agent_id = "6076d1c5b28b871d6bdb609c" run_id = "6076d69ba216ff15b6e95ea2" @@ -145,6 +146,7 @@ def test_run_agent_update(client, agent_status, error_message): Run( id=run_id, + workspace_id=workspace_id, run_configuration_id=run_configuration_id, title="Some test", run_plan_id=run_plan_id, @@ -258,12 +260,14 @@ def test_run_agent_update_with_run_status_update( client, run_agents, update_data, expected_run_status ): run_id = "6076d69ba216ff15b6e95ea2" + workspace_id = "6076d1e3a216ff15b6e95e9a" run_configuration_id = "7076d69ba216ff15b6e95ea2" run_plan_id = "8076d69ba216ff15b6e95ea2" agent_ids = ["5076d69ba216ff15b6e95ea2"] Run( id=run_id, + workspace_id=workspace_id, run_configuration_id=run_configuration_id, title="Some test", run_plan_id=run_plan_id, diff --git a/web/api/tests/routes/test_run_configuration.py b/web/api/tests/routes/test_run_configuration.py index 0441b375..e41a13ce 100644 --- a/web/api/tests/routes/test_run_configuration.py +++ b/web/api/tests/routes/test_run_configuration.py @@ -5,6 +5,7 @@ from maestro_api.db.models.run_plan import RunPlan from maestro_api.db.models.run_configuration import RunConfiguration from maestro_api.db.models.custom_data import CustomData +from maestro_api.db.models.workspace import Workspace from maestro_api.enums import DaysOfTheWeek @@ -28,6 +29,7 @@ ], ) def test_create_run_configuration(client, optional_params): + workspace_id = "6076d1e3a216ff15b6e95e9a" run_plan_id = "6076d1e3a216ff15b6e95e9d" agent_ids = ["6076d1bfb28b871d6bdb6095"] run_plan_title = "Example test plan" @@ -36,12 +38,14 @@ def test_create_run_configuration(client, optional_params): available_in_response = { "title": run_configuration_title, + "workspace_id": workspace_id, "run_plan_id": run_plan_id, "agent_ids": agent_ids, "custom_data_ids": custom_data_ids, **optional_params, } + Workspace(id=workspace_id, name="Workpace 1").save() RunPlan(id=run_plan_id, title=run_plan_title).save() for agent_id in agent_ids: Agent(id=agent_id, hostname="host_%s" % agent_id, ip="test_ip").save() @@ -51,6 +55,7 @@ def test_create_run_configuration(client, optional_params): request_data = { "title": run_configuration_title, "run_plan_id": run_plan_id, + "workspace_id": workspace_id, "agent_ids": agent_ids, "custom_data_ids": custom_data_ids, **optional_params, @@ -90,6 +95,7 @@ def test_create_run_configuration(client, optional_params): ) def test_update_run_configuration(client, optional_params): run_configuration_id = "6106d1e3a216ff15b6e95e9d" + workspace_id = "6076d1e3a216ff15b6e95e9a" run_plan_id = "6076d1e3a216ff15b6e95e9d" agent_ids = ["6076d1bfb28b871d6bdb6095"] run_plan_title = "Example test plan" @@ -97,17 +103,19 @@ def test_update_run_configuration(client, optional_params): available_in_response = { "title": run_configuration_title, + "workspace_id": workspace_id, "run_plan_id": run_plan_id, "agent_ids": agent_ids, **optional_params, } - + Workspace(id=workspace_id, name="Workpace 1").save() RunPlan(id=run_plan_id, title=run_plan_title).save() for agent_id in agent_ids: Agent(id=agent_id, hostname="host_%s" % agent_id, ip="test_ip").save() RunConfiguration( id=run_configuration_id, + workspace_id=workspace_id, title=run_configuration_title, agent_ids=agent_ids, run_plan_id=run_plan_id, @@ -120,6 +128,7 @@ def test_update_run_configuration(client, optional_params): request_data = { "title": run_configuration_title, + "workspace_id": workspace_id, "run_plan_id": run_plan_id, "agent_ids": agent_ids, **optional_params, @@ -139,6 +148,7 @@ def test_update_run_configuration(client, optional_params): def test_delete_run_configuration(client): + workspace_id = "6076d1e3a216ff15b6e95e9a" run_configuration_id = "6106d1e3a216ff15b6e95e9d" run_plan_id = "6076d1e3a216ff15b6e95e9d" agent_ids = ["6076d1bfb28b871d6bdb6095"] @@ -146,6 +156,7 @@ def test_delete_run_configuration(client): RunConfiguration( id=run_configuration_id, + workspace_id=workspace_id, title=run_configuration_title, agent_ids=agent_ids, run_plan_id=run_plan_id, @@ -184,6 +195,7 @@ def test_delete_run_configuration_with_not_found(client): def test_get_run_configuration(client): run_configuration_id = "6106d1e3a216ff15b6e95e9d" + workspace_id = "6076d1e3a216ff15b6e95e9a" run_plan_id = "6076d1e3a216ff15b6e95e9d" agent_ids = ["6076d1bfb28b871d6bdb6095"] run_configuration_title = "Example test plan" @@ -193,6 +205,7 @@ def test_get_run_configuration(client): RunConfiguration( id=run_configuration_id, + workspace_id=workspace_id, title=run_configuration_title, agent_ids=agent_ids, run_plan_id=run_plan_id, @@ -211,6 +224,7 @@ def test_get_run_configuration(client): assert "created_at" in res_json assert "updated_at" in res_json assert res_json["id"] == run_configuration_id + assert res_json["workspace_id"] == workspace_id assert res_json["title"] == run_configuration_title assert res_json["run_plan_id"] == run_plan_id assert res_json["agent_ids"] == agent_ids @@ -230,39 +244,62 @@ def test_get_run_configuration_with_not_found(client): assert response.status_code == 404 -def test_list_run_configurations(client): - run_configuration_id = "6106d1e3a216ff15b6e95e9d" - run_plan_id = "6076d1e3a216ff15b6e95e9d" - agent_ids = ["6076d1bfb28b871d6bdb6095"] - run_configuration_title = "Example test plan" - hosts = [{"host": "test", "ip": "127.0.0.3"}] - custom_properties = [{"name": "testProperty", "value": "123"}] - - RunConfiguration( - id=run_configuration_id, - title=run_configuration_title, - agent_ids=agent_ids, - run_plan_id=run_plan_id, - hosts=hosts, - custom_data_ids=[], - custom_properties=custom_properties, - ).save() +@pytest.mark.parametrize( + "db_data", + [ + [ + dict( + run_configuration_id="6076d69ba216ff15b6e95ea1", + workspace_id="6076d1e3a216ff15b6e95e9a", + ), + dict( + run_configuration_id="6076d69ba216ff15b6e95ea2", + workspace_id="6076d1e3a216ff15b6e95e8a", + ), + ] + ], +) +@pytest.mark.parametrize( + "input_params,extected_ids", + [ + ( + "", + [ + "6076d69ba216ff15b6e95ea1", + "6076d69ba216ff15b6e95ea2", + ], + ), + ( + "?workspace_id=6076d1e3a216ff15b6e95e8a", + [ + "6076d69ba216ff15b6e95ea2", + ], + ), + ], +) +def test_run_configuration_list(client, db_data, input_params, extected_ids): + for document in db_data: + run_configuration_id = document["run_configuration_id"] + workspace_id = document["workspace_id"] + run_plan_id = "6076d1e3a216ff15b6e95e9d" + agent_ids = ["6076d1bfb28b871d6bdb6095"] + run_configuration_title = "Example test plan" + hosts = [{"host": "test", "ip": "127.0.0.3"}] + custom_properties = [{"name": "testProperty", "value": "123"}] + Workspace(id=workspace_id, name="Workspace").save() + RunConfiguration( + id=run_configuration_id, + workspace_id=workspace_id, + title=run_configuration_title, + agent_ids=agent_ids, + run_plan_id=run_plan_id, + hosts=hosts, + custom_data_ids=[], + custom_properties=custom_properties, + ).save() + + response = client.get("/run_configurations%s" % input_params) - response = client.get( - "/run_configurations", - ) res_json = json.loads(response.data) - item = res_json[0] - assert response.status_code == 200 - assert len(res_json) == 1 - - assert "created_at" in item - assert "updated_at" in item - assert item["id"] == run_configuration_id - assert item["title"] == run_configuration_title - assert item["run_plan_id"] == run_plan_id - assert item["agent_ids"] == agent_ids - assert item["custom_properties"] == custom_properties - assert item["hosts"] == hosts - assert item["custom_data_ids"] == [] + assert [e["id"] for e in res_json] == extected_ids diff --git a/web/api/tests/routes/test_run_metric.py b/web/api/tests/routes/test_run_metric.py index 802c014d..87bcaabb 100644 --- a/web/api/tests/routes/test_run_metric.py +++ b/web/api/tests/routes/test_run_metric.py @@ -37,6 +37,7 @@ def get_metrics(min_datetime, next_datetime): def test_run_metric_all(client): run_configuration_id = "6326d1e3a216ff15b6e95e9d" + workspace_id = "6076d1e3a216ff15b6e95e9a" title = "some example title" run_id = "6076d1e3a216ff15b6e95e1f" run_plan_id = "6076d1e3a216ff15b6e95e9d" @@ -94,6 +95,7 @@ def test_run_metric_all(client): Run( id=run_id, + workspace_id=workspace_id, title=title, run_configuration_id=run_configuration_id, run_plan_id=run_plan_id, @@ -154,6 +156,7 @@ def test_run_metric_all(client): def test_run_metric_all_with_show_labels_param(client): run_configuration_id = "6326d1e3a216ff15b6e95e9d" + workspace_id = "6076d1e3a216ff15b6e95e9a" title = "some example title" run_id = "6076d1e3a216ff15b6e95e1f" run_plan_id = "6076d1e3a216ff15b6e95e9d" @@ -168,6 +171,7 @@ def test_run_metric_all_with_show_labels_param(client): Run( id=run_id, + workspace_id=workspace_id, title=title, run_configuration_id=run_configuration_id, run_plan_id=run_plan_id, @@ -191,6 +195,7 @@ def test_run_metric_all_with_show_labels_param(client): def test_run_metric_all_with_show_labels_and_zero_time_interval(client): + workspace_id = "6076d1e3a216ff15b6e95e9a" run_configuration_id = "6326d1e3a216ff15b6e95e9d" title = "some example title" run_id = "6076d1e3a216ff15b6e95e1f" @@ -206,6 +211,7 @@ def test_run_metric_all_with_show_labels_and_zero_time_interval(client): Run( id=run_id, + workspace_id=workspace_id, title=title, run_configuration_id=run_configuration_id, run_plan_id=run_plan_id, @@ -229,6 +235,7 @@ def test_run_metric_all_with_show_labels_and_zero_time_interval(client): def test_run_metrics_create_many(client): + workspace_id = "6076d1e3a216ff15b6e95e9a" run_configuration_id = "6326d1e3a216ff15b6e95e9d" run_id = "6076d1e3a216ff15b6e95e1f" title = "some example title" @@ -277,6 +284,7 @@ def test_run_metrics_create_many(client): Run( id=run_id, + workspace_id=workspace_id, title=title, run_configuration_id=run_configuration_id, run_plan_id=run_plan_id, @@ -302,6 +310,7 @@ def test_run_metrics_create_many(client): def test_run_metrics_download(client): run_configuration_id = "6326d1e3a216ff15b6e95e9d" + workspace_id = "6076d1e3a216ff15b6e95e9a" title = "some example title" run_id = "6076d1e3a216ff15b6e95e1f" run_plan_id = "6076d1e3a216ff15b6e95e9d" @@ -336,6 +345,7 @@ def test_run_metrics_download(client): Run( id=run_id, + workspace_id=workspace_id, title=title, run_configuration_id=run_configuration_id, run_plan_id=run_plan_id, diff --git a/web/api/tests/routes/test_run_status.py b/web/api/tests/routes/test_run_status.py index 9b11ca60..23e2dc2d 100644 --- a/web/api/tests/routes/test_run_status.py +++ b/web/api/tests/routes/test_run_status.py @@ -9,6 +9,7 @@ def test_run_status_start(client): run_configuration_id = "6326d1e3a216ff15b6e95e9d" + workspace_id = "6076d1e3a216ff15b6e95e9a" title = "some example title" run_id = "6076d1e3a216ff15b6e95e1f" run_plan_id = "6076d1e3a216ff15b6e95e9d" @@ -16,6 +17,7 @@ def test_run_status_start(client): Run( id=run_id, + workspace_id=workspace_id, run_configuration_id=run_configuration_id, title=title, run_plan_id=run_plan_id, @@ -41,6 +43,7 @@ def test_run_status_start(client): [RunStatus.FINISHED.value, RunStatus.STOPPED.value, RunStatus.ERROR.value], ) def test_run_status_restart(client, run_status): + workspace_id = "6076d1e3a216ff15b6e95e9a" run_configuration_id = "6326d1e3a216ff15b6e95e9d" title = "some example title" run_id = "6076d1e3a216ff15b6e95e1f" @@ -49,6 +52,7 @@ def test_run_status_restart(client, run_status): Run( id=run_id, + workspace_id=workspace_id, run_configuration_id=run_configuration_id, title=title, run_plan_id=run_plan_id, @@ -76,6 +80,7 @@ def test_run_status_restart(client, run_status): ) def test_run_status_restart_with_bad_request(client, run_status): run_configuration_id = "6326d1e3a216ff15b6e95e9d" + workspace_id = "6076d1e3a216ff15b6e95e9a" title = "some example title" run_id = "6076d1e3a216ff15b6e95e1f" run_plan_id = "6076d1e3a216ff15b6e95e9d" @@ -83,6 +88,7 @@ def test_run_status_restart_with_bad_request(client, run_status): Run( id=run_id, + workspace_id=workspace_id, run_configuration_id=run_configuration_id, title=title, run_plan_id=run_plan_id, @@ -101,6 +107,7 @@ def test_run_status_restart_with_bad_request(client, run_status): def test_run_status_restart_with_reset_to_default_fields(client): run_configuration_id = "6326d1e3a216ff15b6e95e9d" + workspace_id = "6076d1e3a216ff15b6e95e9a" title = "some example title" diff_run_id = "6076d1e3a216ff15b6e95e2f" run_id = "6076d1e3a216ff15b6e95e1f" @@ -110,6 +117,7 @@ def test_run_status_restart_with_reset_to_default_fields(client): Run( id=run_id, + workspace_id=workspace_id, run_configuration_id=run_configuration_id, title=title, run_plan_id=run_plan_id, @@ -153,6 +161,7 @@ def test_run_status_restart_with_reset_to_default_fields(client): def test_run_status_start_with_running_status(client): run_configuration_id = "6326d1e3a216ff15b6e95e9d" + workspace_id = "6076d1e3a216ff15b6e95e9a" title = "some example title" run_id = "6076d1e3a216ff15b6e95e1f" run_plan_id = "6076d1e3a216ff15b6e95e9d" @@ -160,6 +169,7 @@ def test_run_status_start_with_running_status(client): Run( id=run_id, + workspace_id=workspace_id, run_configuration_id=run_configuration_id, title=title, run_plan_id=run_plan_id, @@ -176,6 +186,7 @@ def test_run_status_start_with_running_status(client): def test_run_status_stop(client): run_configuration_id = "6326d1e3a216ff15b6e95e9d" + workspace_id = "6076d1e3a216ff15b6e95e9a" title = "some example title" run_id = "6076d1e3a216ff15b6e95e1f" run_plan_id = "6076d1e3a216ff15b6e95e9d" @@ -183,6 +194,7 @@ def test_run_status_stop(client): Run( id=run_id, + workspace_id=workspace_id, run_configuration_id=run_configuration_id, title=title, run_plan_id=run_plan_id, @@ -210,6 +222,7 @@ def test_run_status_stop(client): ) def test_run_status_stop_with_bad_request_response(client, run_status): run_configuration_id = "6326d1e3a216ff15b6e95e9d" + workspace_id = "6076d1e3a216ff15b6e95e9a" title = "some example title" run_id = "6076d1e3a216ff15b6e95e1f" run_plan_id = "6076d1e3a216ff15b6e95e9d" @@ -217,6 +230,7 @@ def test_run_status_stop_with_bad_request_response(client, run_status): Run( id=run_id, + workspace_id=workspace_id, run_configuration_id=run_configuration_id, title=title, run_plan_id=run_plan_id, diff --git a/web/api/tests/test_scheduler.py b/web/api/tests/test_scheduler.py index 767239fe..061bafbc 100644 --- a/web/api/tests/test_scheduler.py +++ b/web/api/tests/test_scheduler.py @@ -67,12 +67,14 @@ @freeze_time("2012-01-02 10:02:00") def test_start_scheduled_run(app, run_configurations, created_runs): run_plan_id = "6076d1e3a216ff15b6e95e9d" + workspace_id = "6076d1e3a216ff15b6e95e9a" agent_ids = ["6076d1e3a216ff15b6e95e8d"] RunPlan(id=run_plan_id, title="Example Plan").save() for agent_id in agent_ids: Agent(id=agent_id, hostname="host_%s" % agent_id, ip="test_ip").save() for run_configuration in run_configurations: RunConfiguration( + workspace_id=workspace_id, title="Example test plan", run_plan_id=run_plan_id, agent_ids=agent_ids, @@ -91,12 +93,14 @@ def test_start_scheduled_run(app, run_configurations, created_runs): @freeze_time("2012-01-02 10:02:00") def test_start_scheduled_run_second_time(app): run_plan_id = "6076d1e3a216ff15b6e95e9d" + workspace_id = "6076d1e3a216ff15b6e95e9a" agent_ids = ["6076d1e3a216ff15b6e95e8d"] RunPlan(id=run_plan_id, title="Example Plan").save() for agent_id in agent_ids: Agent(id=agent_id, hostname="host_%s" % agent_id, ip="test_ip").save() RunConfiguration( + workspace_id=workspace_id, title="Example test plan", run_plan_id=run_plan_id, agent_ids=agent_ids,