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_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_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, {})
    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_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)