Ejemplo n.º 1
0
    def test_get(self):
        """Test that it is possible to get a sub suite from the sub suite endpoint.

        Approval criteria:
            - The sub suite endpoint shall return a sub suite if one exists.

        Test steps:
            1. Add a sub suite to the database.
            2. Send a fake request to the sub suite endpoint.
            3. Verify that the sub suite endpoint responds with a sub suite.
        """
        self.logger.info("STEP: Add a sub suite to the database.")
        database = FakeDatabase()
        suite_id = "thetestiestofsuites"
        sub_suite = {"test": "suite"}
        database.write(suite_id, json.dumps(sub_suite))

        self.logger.info(
            "STEP: Send a fake request to the sub suite endpoint.")
        request = FakeRequest()
        request.fake_params["id"] = suite_id
        response = FakeResponse()
        SubSuite(database).on_get(request, response)

        self.logger.info(
            "STEP: Verify that the sub suite endpoint responds with a sub suite."
        )
        self.assertDictEqual(response.fake_responses.get("media"), sub_suite)
    def test_register_log_area_provider(self):
        """Test that the register backend can register log area providers.

        Approval criteria:
            - The register backend shall be able to register a log area provider.

        Test steps:
            1. Register a log area provider with the register backend.
            2. Verify that the log area provider was stored in the database.
        """
        fake_database = FakeDatabase()
        etos = ETOS("testing_etos", "testing_etos", "testing_etos")
        jsontas = JsonTas()
        provider = {
            "log": {
                "id": "log_area_provider_test",
                "list": {"available": [], "possible": []},
            }
        }
        provider_registry = ProviderRegistry(etos, jsontas, fake_database)
        self.logger.info(
            "STEP: Register a log area provider with the register backend."
        )
        response = register(provider_registry, log_area_provider=provider)

        self.logger.info(
            "STEP: Verify that the log area provider was stored in the database."
        )
        stored_provider = json.loads(
            fake_database.reader.hget(
                "EnvironmentProvider:LogAreaProviders", "log_area_provider_test"
            )
        )
        self.assertDictEqual(stored_provider, provider)
        self.assertTrue(response)
Ejemplo n.º 3
0
    def test_get_configuration_missing(self):
        """Test that if a configuration is missing, a partial result is returned.

        Approval criteria:
            - The configure backend shall return a partial configuration if configuration
              is missing.

        Test steps:
            1. Store a faulty configuration into the database.
            2. Verify that it is possible to get the partial configuration.
        """
        database = FakeDatabase()
        test_suite_id = "ca51601e-6c9a-4b5d-8038-7dc2561283d2"
        test_dataset = {"dataset": "test"}
        self.logger.info(
            "STEP: Store a faulty configuration into the database.")
        database.writer.hset(f"EnvironmentProvider:{test_suite_id}", "Dataset",
                             json.dumps(test_dataset))

        self.logger.info(
            "STEP: Verify that it is possible to get the partial configuration."
        )
        registry = ProviderRegistry(ETOS("", "", ""), JsonTas(), database)
        stored_configuration = get_configuration(registry, test_suite_id)
        self.assertDictEqual(
            stored_configuration,
            {
                "dataset": test_dataset,
                "iut_provider": None,
                "execution_space_provider": None,
                "log_area_provider": None,
            },
        )
    def test_get_environment(self, get_environment_mock):
        """Test that it is possible to get environments from the environment provider.

        Approval criteria:
            - It shall be possible to get an environment from the environment provider.

        Note:
            - This test is mocked due to not wanting to run celery tasks.

        Test steps:
            1. Send a request for an environment.
            2. Verify that the environment provider gets an environment.
        """
        task_id = "f3286e6e-946c-4510-a935-abd7c7bdbe17"
        database = FakeDatabase()
        get_environment_mock.delay.return_value = Task(task_id)
        celery_worker = FakeCelery(task_id, "", {})
        suite_id = "ca950c50-03d3-4a3c-8507-b4229dd3f8ea"
        request = FakeRequest()
        request.fake_params = {"suite_id": suite_id}
        response = FakeResponse()

        self.logger.info("STEP: Send a request for an environment.")
        environment = Webserver(database, celery_worker)
        environment.on_post(request, response)

        self.logger.info(
            "STEP: Verify that the environment provider gets an environment.")
        self.assertEqual(response.media, {
            "result": "success",
            "data": {
                "id": task_id
            }
        })
        get_environment_mock.delay.assert_called_once_with(suite_id)
    def test_get_environment_status(self):
        """Test that it is possible to get status from an environment.

        Approval criteria:
            - It shall be possible to get status from environment that is being checked out.

        Test steps:
            1. Store a PENDING environment request in a celery task.
            2. Send a status request for that environment.
            3. Verify that the status for the environment was returned.
        """
        task_id = "d9689ea5-837b-48c1-87b1-3de122b3f2fe"
        database = FakeDatabase()
        request = FakeRequest()
        request.fake_params = {"id": task_id}
        response = FakeResponse()
        test_result = {"this": "is", "results": ":)"}
        test_status = "PENDING"

        self.logger.info(
            "STEP: Store a PENDING environment request in a celery task.")
        celery_worker = FakeCelery(task_id, test_status, test_result)

        self.logger.info("STEP: Send a status request for that environment.")
        environment = Webserver(database, celery_worker)
        environment.on_get(request, response)

        self.logger.info(
            "STEP: Verify that the status for the environment was returned.")
        self.assertEqual(response.status, falcon.HTTP_200)
        self.assertDictEqual(response.media, {
            "status": test_status,
            "result": test_result
        })
    def test_release_environment_no_task_result(self):
        """Test that it is not possible to release an environment without task results.

        Approval criteria:
            - The environment provider shall not attempt to release environments with results.

        Test steps:
            1. Attempt to release an environment without a task ID.
            2. Verify that the release fails.
        """
        database = FakeDatabase()
        test_release_id = "ce63f53e-1797-42bb-ae72-861a0b6b7ef6"
        worker = FakeCelery("thisdoesnotexist", "SUCCESS", {})
        jsontas = JsonTas()
        etos = ETOS("", "", "")
        registry = ProviderRegistry(etos, jsontas, database)
        self.logger.info(
            "STEP: Attempt to release an environment without a task ID.")
        success, _ = release_environment(
            etos,
            jsontas,
            registry,
            worker.AsyncResult(test_release_id),
            test_release_id,
        )

        self.logger.info("STEP: Verify that the release fails.")
        self.assertFalse(success)
Ejemplo n.º 7
0
    def test_sub_suite(self):
        """Test that the subsuite backend can return the sub suite registered in database.

        Approval criteria:
            - The subsuite backend shall be able to return sub suites registered in the database.

        Test steps:
            1. Add a sub suite to the database.
            2. Get sub suite from the subsuite backend.
            3. Verify that the sub suite is the one stored in the database.
        """
        database = FakeDatabase()
        self.logger.info("STEP: Add a sub suite to the database.")
        test_suite = {"testing": "subsuites"}
        database.write("mysuite", json.dumps(test_suite))
        self.logger.info("STEP: Get the sub suite from the subsuite backend.")
        response_suite = get_sub_suite(database, "mysuite")
        self.logger.info(
            "STEP: Verify that the sub suite is the one stored in the database."
        )
        self.assertDictEqual(test_suite, response_suite)
Ejemplo n.º 8
0
    def test_sub_suite_does_not_exist(self):
        """Test that the subsuite backend return None when there is no sub suite in database.

        Approval criteria:
            - The subsuite backend shall return None when there is no sub suite in database.

        Test steps:
            1. Get sub suite from the subsuite backend.
            2. Verify that the sub suite returned is None.
        """
        self.logger.info("STEP: Get the sub suite from the subsuite backend.")
        suite = get_sub_suite(FakeDatabase(), 1)
        self.logger.info("STEP: Verify that the sub suite returned is None.")
        self.assertIsNone(suite)
    def test_register_no_providers(self):
        """Test that it is not possible to register no providers.

        Approval criteria:
            - It shall not be possible to register no providers.

        Test steps:
            1. Send a register request with no providers.
            2. Verify that a 400 Bad Request is returned.
        """
        fake_database = FakeDatabase()
        fake_request = FakeRequest()
        fake_response = FakeResponse()
        self.logger.info("STEP: Send a register request with no providers.")
        with self.assertRaises(falcon.HTTPBadRequest):
            self.logger.info(
                "STEP: Verify that a 400 Bad Request is returned.")
            Register(fake_database).on_post(fake_request, fake_response)
    def test_get_configuration_no_suite_id(self):
        """Test that it is not possible to get a configuration without suite id.

        Approval criteria:
            - The configure endpoint shall return BadRequest when missing suite id.

        Test steps:
            1. Send a GET request to the configure endpoint without suite id.
            2. Verify that a BadRequest is returned.
        """
        database = FakeDatabase()
        response = FakeResponse()
        request = FakeRequest()
        self.logger.info(
            "STEP: Send a GET request to the configure endpoint without suite id."
        )
        with self.assertRaises(falcon.HTTPBadRequest):
            self.logger.info("STEP: Verify that a BadRequest is returned.")
            Configure(database).on_get(request, response)
    def test_register_provider_none(self):
        """Test that the register backend return false if no provider is supplied.

        Approval criteria:
            - The register backend shall return False if no provider is supplied.

        Test steps:
            1. Register no provider with the register backend.
            2. Verify that the register backend return False.
        """
        fake_database = FakeDatabase()
        etos = ETOS("testing_etos", "testing_etos", "testing_etos")
        jsontas = JsonTas()
        provider_registry = ProviderRegistry(etos, jsontas, fake_database)

        self.logger.info("STEP: Register no provider with the register backend.")
        response = register(provider_registry)

        self.logger.info("STEP: Verify that the register backend return False.")
        self.assertFalse(response)
Ejemplo n.º 12
0
    def test_configure_missing_parameter(self):
        """Test that the configure backend does not configure if any parameter is missing.

        Approval criteria:
            - The configure backend shall return False and not configure if any parameter
              is missing.

        Test steps:
            1. Attempt to configure the environment provider without any parameters.
            2. Verify that False was returned and no configuration was made.
        """
        database = FakeDatabase()
        self.logger.info(
            "STEP: Attempt to configure the environment provider without any parameters."
        )
        registry = ProviderRegistry(ETOS("", "", ""), JsonTas(), database)
        success, _ = configure(registry, None, None, None, None, None)

        self.logger.info(
            "STEP: Verify that False was returned and no configuration was made."
        )
        self.assertFalse(success)
        self.assertDictEqual(database.db_dict, {})
    def test_register_execution_space_provider(self):
        """Test that the register backend can register execution space providers.

        Approval criteria:
            - The register backend shall be able to register an execution space provider.

        Test steps:
            1. Register an execution space provider with the register backend.
            2. Verify that the execution space provider was stored in the database.
        """
        fake_database = FakeDatabase()
        etos = ETOS("testing_etos", "testing_etos", "testing_etos")
        jsontas = JsonTas()
        provider = {
            "execution_space": {
                "id": "execution_space_provider_test",
                "list": {"available": [{"identifier": "123"}], "possible": []},
            }
        }
        provider_registry = ProviderRegistry(etos, jsontas, fake_database)
        self.logger.info(
            "STEP: Register an execution space provider with the register backend."
        )
        response = register(provider_registry, execution_space_provider=provider)

        self.logger.info(
            "STEP: Verify that the execution space provider was stored in the database."
        )
        stored_provider = json.loads(
            fake_database.reader.hget(
                "EnvironmentProvider:ExecutionSpaceProviders",
                "execution_space_provider_test",
            )
        )
        self.assertDictEqual(stored_provider, provider)
        self.assertTrue(response)
    def test_release_environment(self):
        """Test that it is possible to release an environment.

        Approval criteria:
            - It shall be possible to release an environment.

        Note:
            - This is not perfectly testable today due to how the providers are used
              when checking in provider items.

        Test steps:
            1. Attempt to release an environment.
            2. Verify that it was possible to release that environment.
        """
        database = FakeDatabase()
        test_iut_provider = OrderedDict({
            "iut": {
                "id": "iut_provider_test",
                "list": {
                    "available": [],
                    "possible": []
                },
            }
        })
        test_execution_space_provider = OrderedDict({
            "execution_space": {
                "id": "execution_space_provider_test",
                "list": {
                    "available": [{
                        "identifier": "123"
                    }],
                    "possible": []
                },
            }
        })
        test_log_area_provider = OrderedDict({
            "log": {
                "id": "log_area_provider_test",
                "list": {
                    "available": [],
                    "possible": []
                },
            }
        })
        database.writer.hset(
            "EnvironmentProvider:ExecutionSpaceProviders",
            test_execution_space_provider["execution_space"]["id"],
            json.dumps(test_execution_space_provider),
        )
        database.writer.hset(
            "EnvironmentProvider:IUTProviders",
            test_iut_provider["iut"]["id"],
            json.dumps(test_iut_provider),
        )
        database.writer.hset(
            "EnvironmentProvider:LogAreaProviders",
            test_log_area_provider["log"]["id"],
            json.dumps(test_log_area_provider),
        )
        iut = {"id": "test_iut", "provider_id": test_iut_provider["iut"]["id"]}
        executor = {
            "id": "test_executor",
            "provider_id":
            test_execution_space_provider["execution_space"]["id"],
        }
        log_area = {
            "id": "test_log_area",
            "provider_id": test_log_area_provider["log"]["id"],
        }
        jsontas = JsonTas()
        etos = ETOS("", "", "")
        registry = ProviderRegistry(etos, jsontas, database)

        test_release_id = "ce63f53e-1797-42bb-ae72-861a0b6b7ef6"
        worker = FakeCelery(
            test_release_id,
            "SUCCESS",
            {
                "suites": [{
                    "iut": iut,
                    "executor": executor,
                    "log_area": log_area,
                }]
            },
        )
        self.logger.info("STEP: Attempt to release an environment.")
        success, _ = release_environment(
            etos,
            jsontas,
            registry,
            worker.AsyncResult(test_release_id),
            test_release_id,
        )

        self.logger.info(
            "STEP: Verify that it was possible to release that environment.")
        self.assertTrue(success)
        self.assertIsNone(worker.AsyncResult(test_release_id))
    def test_register_all_providers(self):
        """Test that the register backend can register all providers.

        Approval criteria:
            - The register backend shall be able to register all providers.

        Test steps:
            1. Register one of each provider with the register backend.
            2. Verify that the providers were stored in the database.
        """
        fake_database = FakeDatabase()
        etos = ETOS("testing_etos", "testing_etos", "testing_etos")
        jsontas = JsonTas()
        test_iut_provider = {
            "iut": {
                "id": "iut_provider_test",
                "list": {"available": [], "possible": []},
            }
        }
        test_execution_space_provider = {
            "execution_space": {
                "id": "execution_space_provider_test",
                "list": {"available": [{"identifier": "123"}], "possible": []},
            }
        }
        test_log_area_provider = {
            "log": {
                "id": "log_area_provider_test",
                "list": {"available": [], "possible": []},
            }
        }
        provider_registry = ProviderRegistry(etos, jsontas, fake_database)
        self.logger.info(
            "STEP: Register one of each provider with the register backend."
        )
        response = register(
            provider_registry,
            iut_provider=test_iut_provider,
            log_area_provider=test_log_area_provider,
            execution_space_provider=test_execution_space_provider,
        )

        self.logger.info("STEP: Verify that the providers were stored in the database.")
        stored_execution_space_provider = json.loads(
            fake_database.reader.hget(
                "EnvironmentProvider:ExecutionSpaceProviders",
                "execution_space_provider_test",
            )
        )
        self.assertDictEqual(
            stored_execution_space_provider,
            test_execution_space_provider,
        )
        stored_log_area_provider = json.loads(
            fake_database.reader.hget(
                "EnvironmentProvider:LogAreaProviders", "log_area_provider_test"
            )
        )
        self.assertDictEqual(stored_log_area_provider, test_log_area_provider)
        stored_iut_provider = json.loads(
            fake_database.reader.hget(
                "EnvironmentProvider:IUTProviders", "iut_provider_test"
            )
        )
        self.assertDictEqual(stored_iut_provider, test_iut_provider)
        self.assertTrue(response)
    def test_release_environment(self):
        """Test that it is possible to release an environment.

        Approval criteria:
            - It shall be possible to release an environment.

        Test steps:
            1. Store an environment i celery task.
            2. Send a release request for that environment.
            3. Verify that the environment was released.
        """
        database = FakeDatabase()
        test_iut_provider = {
            "iut": {
                "id": "iut_provider_test",
                "list": {
                    "available": [],
                    "possible": []
                },
            }
        }
        test_execution_space_provider = {
            "execution_space": {
                "id": "execution_space_provider_test",
                "list": {
                    "available": [{
                        "identifier": "123"
                    }],
                    "possible": []
                },
            }
        }
        test_log_area_provider = {
            "log": {
                "id": "log_area_provider_test",
                "list": {
                    "available": [],
                    "possible": []
                },
            }
        }
        database.writer.hset(
            "EnvironmentProvider:ExecutionSpaceProviders",
            test_execution_space_provider["execution_space"]["id"],
            json.dumps(test_execution_space_provider),
        )
        database.writer.hset(
            "EnvironmentProvider:IUTProviders",
            test_iut_provider["iut"]["id"],
            json.dumps(test_iut_provider),
        )
        database.writer.hset(
            "EnvironmentProvider:LogAreaProviders",
            test_log_area_provider["log"]["id"],
            json.dumps(test_log_area_provider),
        )
        task_id = "d9689ea5-837b-48c1-87b1-3de122b3f2fe"
        request = FakeRequest()
        request.fake_params = {"release": task_id}
        response = FakeResponse()

        iut = {"id": "test_iut", "provider_id": test_iut_provider["iut"]["id"]}
        executor = {
            "id": "test_executor",
            "provider_id":
            test_execution_space_provider["execution_space"]["id"],
        }
        log_area = {
            "id": "test_log_area",
            "provider_id": test_log_area_provider["log"]["id"],
        }

        self.logger.info("STEP: Store an environment i celery task.")
        test_status = "SUCCESS"
        worker = FakeCelery(
            task_id,
            test_status,
            {
                "suites": [{
                    "iut": iut,
                    "executor": executor,
                    "log_area": log_area,
                }]
            },
        )

        self.logger.info("STEP: Send a release request for that environment.")
        environment = Webserver(database, worker)
        environment.on_get(request, response)

        self.logger.info("STEP: Verify that the environment was released.")
        self.assertDictEqual(response.media, {"status": test_status})
        self.assertIsNone(worker.results.get(task_id))
    def test_configure(self):
        """Test that it is possible to configure the environment provider for a suite.

        Approval criteria:
            - It shall be possible to configure the environment provider.
            - The configure endpoint shall return with the configured IUT, execution space &
              log area provider.

        Test steps:
            1. Store some providers in the database.
            2. Send a configure request to use those providers.
            3. Verify that the configuration matches the providers in database.
        """
        database = FakeDatabase()
        test_iut_provider = {
            "iut": {
                "id": "iut_provider_test",
                "list": {
                    "available": [],
                    "possible": []
                },
            }
        }
        test_execution_space_provider = {
            "execution_space": {
                "id": "execution_space_provider_test",
                "list": {
                    "available": [{
                        "identifier": "123"
                    }],
                    "possible": []
                },
            }
        }
        test_log_area_provider = {
            "log": {
                "id": "log_area_provider_test",
                "list": {
                    "available": [],
                    "possible": []
                },
            }
        }
        test_suite_id = "2a4cb06d-4ebf-4aaa-a53b-1293194827d8"
        self.logger.info("STEP: Store some providers in the database.")
        database.writer.hset(
            "EnvironmentProvider:ExecutionSpaceProviders",
            test_execution_space_provider["execution_space"]["id"],
            json.dumps(test_execution_space_provider),
        )
        database.writer.hset(
            "EnvironmentProvider:IUTProviders",
            test_iut_provider["iut"]["id"],
            json.dumps(test_iut_provider),
        )
        database.writer.hset(
            "EnvironmentProvider:LogAreaProviders",
            test_log_area_provider["log"]["id"],
            json.dumps(test_log_area_provider),
        )

        response = FakeResponse()
        request = FakeRequest()
        request.fake_params = {
            "iut_provider":
            test_iut_provider["iut"]["id"],
            "execution_space_provider":
            test_execution_space_provider["execution_space"]["id"],
            "log_area_provider":
            test_log_area_provider["log"]["id"],
            "dataset": {},
            "suite_id":
            test_suite_id,
        }
        self.logger.info(
            "STEP: Send a configure request to use those providers.")
        Configure(database).on_post(request, response)

        self.logger.info(
            "STEP: Verify that the configuration matches the providers in database."
        )
        self.assertEqual(response.status, falcon.HTTP_200)
        stored_iut_provider = json.loads(
            database.reader.hget(f"EnvironmentProvider:{test_suite_id}",
                                 "IUTProvider"))
        self.assertDictEqual(stored_iut_provider, test_iut_provider)
        stored_execution_space_provider = json.loads(
            database.reader.hget(f"EnvironmentProvider:{test_suite_id}",
                                 "ExecutionSpaceProvider"))
        self.assertDictEqual(stored_execution_space_provider,
                             test_execution_space_provider)
        stored_log_area_provider = json.loads(
            database.reader.hget(f"EnvironmentProvider:{test_suite_id}",
                                 "LogAreaProvider"))
        self.assertDictEqual(stored_log_area_provider, test_log_area_provider)
        stored_dataset = json.loads(
            database.reader.hget(f"EnvironmentProvider:{test_suite_id}",
                                 "Dataset"))
        self.assertDictEqual(stored_dataset, {})
Ejemplo n.º 18
0
    def test_get_configuration(self):
        """Test that it is possible to get a stored configuration.

        Approval criteria:
            - It shall be possible to get a stored configuration.

        Test steps:
            1. Store a configuration into the database.
            2. Verify that it is possible to get the stored configuration.
        """
        database = FakeDatabase()
        test_suite_id = "8d9344e3-a246-43ec-92b4-fc81ea31067a"
        test_dataset = {"dataset": "test"}
        test_iut_provider = OrderedDict({
            "iut": {
                "id": "iut_provider_test",
                "list": {
                    "available": [],
                    "possible": []
                },
            }
        })
        test_execution_space_provider = OrderedDict({
            "execution_space": {
                "id": "execution_space_provider_test",
                "list": {
                    "available": [{
                        "identifier": "123"
                    }],
                    "possible": []
                },
            }
        })
        test_log_area_provider = OrderedDict({
            "log": {
                "id": "log_area_provider_test",
                "list": {
                    "available": [],
                    "possible": []
                },
            }
        })
        self.logger.info("STEP: Store a configuration into the database.")
        database.writer.hset(f"EnvironmentProvider:{test_suite_id}", "Dataset",
                             json.dumps(test_dataset))
        database.writer.hset(
            f"EnvironmentProvider:{test_suite_id}",
            "IUTProvider",
            json.dumps(test_iut_provider),
        )
        database.writer.hset(
            f"EnvironmentProvider:{test_suite_id}",
            "ExecutionSpaceProvider",
            json.dumps(test_execution_space_provider),
        )
        database.writer.hset(
            f"EnvironmentProvider:{test_suite_id}",
            "LogAreaProvider",
            json.dumps(test_log_area_provider),
        )

        self.logger.info(
            "STEP: Verify that it is possible to get the stored configuration."
        )
        registry = ProviderRegistry(ETOS("", "", ""), JsonTas(), database)
        stored_configuration = get_configuration(registry, test_suite_id)
        self.assertDictEqual(
            stored_configuration,
            {
                "iut_provider":
                test_iut_provider["iut"],
                "execution_space_provider":
                test_execution_space_provider["execution_space"],
                "log_area_provider":
                test_log_area_provider["log"],
                "dataset":
                test_dataset,
            },
        )
Ejemplo n.º 19
0
    def test_configure_empty_dataset(self):
        """Test that it is possible to configure the environment provider if dataset is empty.

        Approval criteria:
            - It shall be possible to configure using an empty dataset.

        Test steps:
            1. Add providers into the database.
            2. Attempt to configure the environment provider with an empty dataset.
            3. Verify that the configuration was stored in the database.
        """
        database = FakeDatabase()
        test_iut_provider = {
            "iut": {
                "id": "iut_provider_test",
                "list": {
                    "available": [],
                    "possible": []
                },
            }
        }
        test_execution_space_provider = {
            "execution_space": {
                "id": "execution_space_provider_test",
                "list": {
                    "available": [{
                        "identifier": "123"
                    }],
                    "possible": []
                },
            }
        }
        test_log_area_provider = {
            "log": {
                "id": "log_area_provider_test",
                "list": {
                    "available": [],
                    "possible": []
                },
            }
        }
        self.logger.info("STEP: Add providers into the database.")
        database.writer.hset(
            "EnvironmentProvider:ExecutionSpaceProviders",
            test_execution_space_provider["execution_space"]["id"],
            json.dumps(test_execution_space_provider),
        )
        database.writer.hset(
            "EnvironmentProvider:IUTProviders",
            test_iut_provider["iut"]["id"],
            json.dumps(test_iut_provider),
        )
        database.writer.hset(
            "EnvironmentProvider:LogAreaProviders",
            test_log_area_provider["log"]["id"],
            json.dumps(test_log_area_provider),
        )

        test_suite_id = "740d1e2a-2309-4c53-beda-569da70c315c"
        test_dataset = {}
        registry = ProviderRegistry(ETOS("", "", ""), JsonTas(), database)
        self.logger.info(
            "STEP: Attempt to configure the environment provider with an empty dataset."
        )
        success, _ = configure(
            registry,
            test_iut_provider["iut"]["id"],
            test_execution_space_provider["execution_space"]["id"],
            test_log_area_provider["log"]["id"],
            test_dataset,
            test_suite_id,
        )

        self.logger.info(
            "STEP: Verify that the configuration was stored in the database.")
        self.assertTrue(success)
        stored_iut_provider = json.loads(
            database.reader.hget(f"EnvironmentProvider:{test_suite_id}",
                                 "IUTProvider"))
        self.assertDictEqual(stored_iut_provider, test_iut_provider)
        stored_execution_space_provider = json.loads(
            database.reader.hget(f"EnvironmentProvider:{test_suite_id}",
                                 "ExecutionSpaceProvider"))
        self.assertDictEqual(stored_execution_space_provider,
                             test_execution_space_provider)
        stored_log_area_provider = json.loads(
            database.reader.hget(f"EnvironmentProvider:{test_suite_id}",
                                 "LogAreaProvider"))
        self.assertDictEqual(stored_log_area_provider, test_log_area_provider)
        stored_dataset = json.loads(
            database.reader.hget(f"EnvironmentProvider:{test_suite_id}",
                                 "Dataset"))
        self.assertDictEqual(stored_dataset, test_dataset)
    def test_configure_missing_parameters(self):
        """Test that it is not possible to configure the environment provider without providers.

        Approval criteria:
            - It shall not be possible to configure the environment provider when
              missing parameters.

        Test steps:
            1. Store some providers in the database.
            2. For each parameter:
                2.1. Send a configure request missing that parameter.
                2.2. Verify that it was not possible to configure.
        """
        database = FakeDatabase()
        test_iut_provider = {
            "iut": {
                "id": "iut_provider_test",
                "list": {
                    "available": [],
                    "possible": []
                },
            }
        }
        test_execution_space_provider = {
            "execution_space": {
                "id": "execution_space_provider_test",
                "list": {
                    "available": [{
                        "identifier": "123"
                    }],
                    "possible": []
                },
            }
        }
        test_log_area_provider = {
            "log": {
                "id": "log_area_provider_test",
                "list": {
                    "available": [],
                    "possible": []
                },
            }
        }
        self.logger.info("STEP: Store some providers in the database.")
        database.writer.hset(
            "EnvironmentProvider:ExecutionSpaceProviders",
            test_execution_space_provider["execution_space"]["id"],
            json.dumps(test_execution_space_provider),
        )
        database.writer.hset(
            "EnvironmentProvider:IUTProviders",
            test_iut_provider["iut"]["id"],
            json.dumps(test_iut_provider),
        )
        database.writer.hset(
            "EnvironmentProvider:LogAreaProviders",
            test_log_area_provider["log"]["id"],
            json.dumps(test_log_area_provider),
        )

        response = FakeResponse()
        request = FakeRequest()
        test_params = {
            "iut_provider":
            test_iut_provider["iut"]["id"],
            "execution_space_provider":
            test_execution_space_provider["execution_space"]["id"],
            "log_area_provider":
            test_log_area_provider["log"]["id"],
            "dataset": {},
        }
        self.logger.info("STEP: For each parameter:")
        for parameter in (
                "iut_provider",
                "log_area_provider",
                "execution_space_provider",
                "dataset",
                "suite_id",
        ):
            self.logger.info("Missing parameter: %s", parameter)
            # Make sure we get a new suite id for each test.
            # that way we don't have to clear the database every time.
            test_suite_id = str(uuid4())
            test_params["suite_id"] = test_suite_id

            request.fake_params = test_params.copy()
            request.fake_params.pop(parameter)

            self.logger.info(
                "STEP: Send a configure request missing that parameter.")
            with self.assertRaises(falcon.HTTPBadRequest):
                Configure(database).on_post(request, response)

            self.logger.info(
                "STEP: Verify that it was not possible to configure.")
            self.assertIsNone(
                database.reader.hget(f"EnvironmentProvider:{test_suite_id}",
                                     "IUTProvider"))
            self.assertIsNone(
                database.reader.hget(f"EnvironmentProvider:{test_suite_id}",
                                     "ExecutionSpaceProvider"))
            self.assertIsNone(
                database.reader.hget(f"EnvironmentProvider:{test_suite_id}",
                                     "LogAreaProvider"))
            self.assertIsNone(
                database.reader.hget(f"EnvironmentProvider:{test_suite_id}",
                                     "Dataset"))
    def test_get_configuration(self):
        """Test that it is possible to get a stored configuration.

        Approval criteria:
            - It shall be possible to get a stored configuration.

        Test steps:
            1. Store a configuration in the database.
            2. Send a GET request to the configure endpoint.
            3. Verify that the configuration is the same as in the database.
        """
        database = FakeDatabase()
        test_suite_id = "5ef5a01c-8ff9-448d-9ac5-21836a2fa6ff"
        test_dataset = {"dataset": "test"}
        test_iut_provider = {
            "iut": {
                "id": "iut_provider_test",
                "list": {
                    "available": [],
                    "possible": []
                },
            }
        }
        test_execution_space_provider = {
            "execution_space": {
                "id": "execution_space_provider_test",
                "list": {
                    "available": [{
                        "identifier": "123"
                    }],
                    "possible": []
                },
            }
        }
        test_log_area_provider = {
            "log": {
                "id": "log_area_provider_test",
                "list": {
                    "available": [],
                    "possible": []
                },
            }
        }
        self.logger.info("STEP: Store a configuration in the database.")
        database.writer.hset(f"EnvironmentProvider:{test_suite_id}", "Dataset",
                             json.dumps(test_dataset))
        database.writer.hset(
            f"EnvironmentProvider:{test_suite_id}",
            "IUTProvider",
            json.dumps(test_iut_provider),
        )
        database.writer.hset(
            f"EnvironmentProvider:{test_suite_id}",
            "ExecutionSpaceProvider",
            json.dumps(test_execution_space_provider),
        )
        database.writer.hset(
            f"EnvironmentProvider:{test_suite_id}",
            "LogAreaProvider",
            json.dumps(test_log_area_provider),
        )

        response = FakeResponse()
        request = FakeRequest()
        request.fake_params["suite_id"] = test_suite_id
        self.logger.info("STEP: Send a GET request to the configure endpoint.")
        Configure(database).on_get(request, response)

        self.logger.info(
            "STEP: Verify that the configuration is the same as in the database."
        )
        self.assertEqual(response.status, falcon.HTTP_200)
        self.assertDictEqual(response.media.get("iut_provider", {}),
                             test_iut_provider["iut"])
        self.assertDictEqual(response.media.get("log_area_provider", {}),
                             test_log_area_provider["log"])
        self.assertDictEqual(
            response.media.get("execution_space_provider", {}),
            test_execution_space_provider["execution_space"],
        )
        self.assertDictEqual(response.media.get("dataset", {}), test_dataset)
    def test_release_environment_failure(self):
        """Test that a failure is returned when there is a problem with releasing.

        Approval criteria:
            - The environment provider shall return failure if one provider failed.

        Note:
            - This is not perfectly testable today due to how the providers are used
              when checking in provider items.

        Test steps:
            1. Release an environment where one provider will fail to check in.
            2. Verify that the release return failure.
        """
        database = FakeDatabase()
        test_iut_provider = {
            "iut": {
                "id": "iut_provider_test",
                "list": {
                    "available": [],
                    "possible": []
                },
            }
        }
        test_execution_space_provider = {
            "execution_space": {
                "id": "execution_space_provider_test",
                "list": {
                    "available": [{
                        "identifier": "123"
                    }],
                    "possible": []
                },
                "checkin": False,
            }
        }
        test_log_area_provider = {
            "log": {
                "id": "log_area_provider_test",
                "list": {
                    "available": [],
                    "possible": []
                },
            }
        }
        database.writer.hset(
            "EnvironmentProvider:ExecutionSpaceProviders",
            test_execution_space_provider["execution_space"]["id"],
            json.dumps(test_execution_space_provider),
        )
        database.writer.hset(
            "EnvironmentProvider:IUTProviders",
            test_iut_provider["iut"]["id"],
            json.dumps(test_iut_provider),
        )
        database.writer.hset(
            "EnvironmentProvider:LogAreaProviders",
            test_log_area_provider["log"]["id"],
            json.dumps(test_log_area_provider),
        )
        registry = ProviderRegistry(ETOS("", "", ""), JsonTas(), database)

        iut = {"id": "test_iut", "provider_id": test_iut_provider["iut"]["id"]}
        executor = {
            "id": "test_executor",
            "provider_id":
            test_execution_space_provider["execution_space"]["id"],
        }
        log_area = {
            "id": "test_log_area",
            "provider_id": test_log_area_provider["log"]["id"],
        }
        jsontas = JsonTas()
        etos = ETOS("", "", "")
        registry = ProviderRegistry(etos, jsontas, database)

        test_release_id = "ce63f53e-1797-42bb-ae72-861a0b6b7ef6"
        worker = FakeCelery(
            test_release_id,
            "SUCCESS",
            {
                "suites": [{
                    "iut": iut,
                    "executor": executor,
                    "log_area": log_area,
                }]
            },
        )

        self.logger.info(
            "STEP: Release an environment where one provider will fail to check in."
        )
        success, _ = release_environment(
            etos,
            jsontas,
            registry,
            worker.AsyncResult(test_release_id),
            test_release_id,
        )

        self.logger.info("STEP: Verify that the release return failure.")
        self.assertFalse(success)
        self.assertIsNone(worker.AsyncResult(test_release_id))
    def test_register_all_providers(self):
        """Test that it is possible to register providers via the register endpoint

        Approval criteria:
            - It shall be possible to register providers using the endpoint.

        Test steps:
            1. Send a register request for new providers.
            2. Verify that the new providers were registered in the database.
        """
        fake_database = FakeDatabase()
        test_iut_provider = {
            "iut": {
                "id": "iut_provider_test",
                "list": {
                    "available": [],
                    "possible": []
                },
            }
        }
        test_execution_space_provider = {
            "execution_space": {
                "id": "execution_space_provider_test",
                "list": {
                    "available": [{
                        "identifier": "123"
                    }],
                    "possible": []
                },
            }
        }
        test_log_area_provider = {
            "log": {
                "id": "log_area_provider_test",
                "list": {
                    "available": [],
                    "possible": []
                },
            }
        }
        fake_request = FakeRequest()
        fake_request.fake_params = {
            "iut_provider": json.dumps(test_iut_provider),
            "execution_space_provider":
            json.dumps(test_execution_space_provider),
            "log_area_provider": json.dumps(test_log_area_provider),
        }
        fake_response = FakeResponse()
        self.logger.info("STEP: Send a register request for new providers.")
        Register(fake_database).on_post(fake_request, fake_response)

        self.logger.info(
            "STEP: Verify that the new providers were registered in the database."
        )
        self.assertEqual(fake_response.fake_responses.get("status"),
                         falcon.HTTP_204)
        stored_execution_space_provider = json.loads(
            fake_database.reader.hget(
                "EnvironmentProvider:ExecutionSpaceProviders",
                "execution_space_provider_test",
            ))
        self.assertDictEqual(
            stored_execution_space_provider,
            test_execution_space_provider,
        )
        stored_log_area_provider = json.loads(
            fake_database.reader.hget("EnvironmentProvider:LogAreaProviders",
                                      "log_area_provider_test"))
        self.assertDictEqual(stored_log_area_provider, test_log_area_provider)
        stored_iut_provider = json.loads(
            fake_database.reader.hget("EnvironmentProvider:IUTProviders",
                                      "iut_provider_test"))
        self.assertDictEqual(stored_iut_provider, test_iut_provider)