Exemple #1
0
    def test_provider_stop_many(self):
        """Test that it is possible to checkin an external execution space provider with many
           execution spaces.

        Approval criteria:
            - It shall be possible to send stop to an external execution space provider
              with multiple execution spaces.

        Test steps::
            1. Initialize an external provider.
            2. Send a stop request for multiple execution spaces.
            3. Verify that the stop endpoint is called.
        """
        etos = ETOS("testing_etos", "testing_etos", "testing_etos")
        etos.config.set("WAIT_FOR_EXECUTION_SPACE_TIMEOUT", 1)
        jsontas = JsonTas()
        execution_spaces = [
            ExecutionSpace(test_execution_space=1),
            ExecutionSpace(test_execution_space=2),
        ]
        jsontas.dataset.merge({
            "identity":
            PackageURL.from_string("pkg:testing/etos"),
            "artifact_id":
            "artifactid",
            "artifact_created":
            "artifactcreated",
            "artifact_published":
            "artifactpublished",
            "tercc":
            "tercc",
            "dataset": {},
            "context":
            "context",
            "execution_spaces":
            execution_spaces,
        })
        dict_execution_spaces = [
            execution_space.as_dict for execution_space in execution_spaces
        ]

        with FakeServer("no_content", {}) as server:
            ruleset = {
                "id": "test_provider_stop_many",
                "stop": {
                    "host": server.host
                }
            }
            self.logger.info("STEP: Initialize an external provider.")
            provider = Provider(etos, jsontas, ruleset)
            self.logger.info(
                "STEP: Send a stop request for multiple execution spaces.")
            provider.checkin_all()
            self.logger.info("STEP: Verify that the stop endpoint is called.")
            self.assertEqual(server.nbr_of_requests, 1)
            self.assertEqual(server.requests, [dict_execution_spaces])
    def build_execution_spaces(self, response):
        """Build execution space objects from external execution space provider response.

        :param response: The response from the external execution space provider.
        :type response: dict
        :return: A list of execution spaces.
        :rtype: list
        """
        return [
            ExecutionSpace(provider_id=self.id, **execution_space)
            for execution_space in response.get("execution_spaces", [])
        ]
Exemple #3
0
    def test_provider_stop_failed(self):
        """Test that the checkin method raises an ExecutionSpaceCheckinFailed exception.

        Approval criteria:
            - The checkin method shall fail with an ExecutionSpaceCheckinFailed exception.

        Test steps::
            1. Initialize an external provider.
            2. Send a stop request that fails.
            3. Verify that the checkin method raises ExecutionSpaceCheckinFailed exception.
        """
        etos = ETOS("testing_etos", "testing_etos", "testing_etos")
        etos.config.set("WAIT_FOR_EXECUTION_SPACE_TIMEOUT", 1)
        jsontas = JsonTas()
        jsontas.dataset.merge({
            "identity":
            PackageURL.from_string("pkg:testing/etos"),
            "artifact_id":
            "artifactid",
            "artifact_created":
            "artifactcreated",
            "artifact_published":
            "artifactpublished",
            "tercc":
            "tercc",
            "dataset": {},
            "context":
            "context",
        })
        execution_space = ExecutionSpace(test_execution_space=1)

        with FakeServer("bad_request", {"error": "no"}) as server:
            ruleset = {
                "id": "test_provider_stop_failed",
                "stop": {
                    "host": server.host
                }
            }
            self.logger.info("STEP: Initialize an external provider.")
            provider = Provider(etos, jsontas, ruleset)
            self.logger.info("STEP: Send a stop request that fails.")
            with self.assertRaises(ExecutionSpaceCheckinFailed):
                self.logger.info(
                    "STEP: Verify that the checkin method raises ExecutionSpaceCheckinFailed "
                    "exception.")
                provider.checkin(execution_space)
    def release(response, task_id):
        """Release an environment.

        :param response: Response object to edit and return.
        :type response: :obj:`falcon.response`
        :param task_id: Task to release.
        :type task_id: str
        """
        try:
            task_result = APP.AsyncResult(task_id)
            result = {
                "status": task_result.status,
            }
            response.status = falcon.HTTP_200
            if task_result.result:
                etos = ETOS(
                    "ETOS Environment Provider",
                    os.getenv("HOSTNAME"),
                    "Environment Provider",
                )
                jsontas = JsonTas()
                registry = ProviderRegistry(etos, jsontas)
                failure = None
                for suite in task_result.result.get("suites", []):
                    try:
                        iut = suite.get("iut")
                        ruleset = registry.get_iut_provider_by_id(
                            iut.get("provider_id"))
                        provider = IutProvider(etos, jsontas, ruleset["iut"])
                        provider.checkin(Iut(**iut))
                    except Exception as exception:  # pylint:disable=broad-except
                        failure = exception

                    try:
                        executor = suite.get("executor")
                        ruleset = registry.get_execution_space_provider_by_id(
                            executor.get("provider_id"))
                        provider = ExecutionSpaceProvider(
                            etos, jsontas, ruleset["execution_space"])
                        provider.checkin(ExecutionSpace(**executor))
                    except Exception as exception:  # pylint:disable=broad-except
                        failure = exception

                    try:
                        log_area = suite.get("log_area")
                        ruleset = registry.get_log_area_provider_by_id(
                            log_area.get("provider_id"))
                        provider = LogAreaProvider(etos, jsontas,
                                                   ruleset["log"])
                        provider.checkin(LogArea(**log_area))
                    except Exception as exception:  # pylint:disable=broad-except
                        failure = exception
                task_result.forget()
                if failure:
                    raise failure
                response.media = {**result}
            else:
                response.media = {
                    "warning": f"Nothing to release with task_id '{task_id}'",
                    **result,
                }
        except Exception as exception:  # pylint:disable=broad-except
            traceback.print_exc()
            response.media = {
                "error": str(exception),
                "details": traceback.format_exc(),
                **result,
            }
Exemple #5
0
    def test_request_and_wait(self):
        """Test that the external execution space provider can checkout execution spaces.

        Approval criteria:
            - The external execution space provider shall request an external provider and
              checkout execution spaces.

        Test steps::
            1. Initialize an external provider.
            2. Send a checkout request via the external execution space provider.
            3. Verify that the provider returns a list of checked out execution spaces.
        """
        etos = ETOS("testing_etos", "testing_etos", "testing_etos")
        etos.config.set("WAIT_FOR_EXECUTION_SPACE_TIMEOUT", 10)
        jsontas = JsonTas()
        identity = PackageURL.from_string("pkg:testing/etos")
        jsontas.dataset.merge({
            "identity": identity,
            "artifact_id": "artifactid",
            "artifact_created": "artifactcreated",
            "artifact_published": "artifactpublished",
            "tercc": "tercc",
            "dataset": {},
            "context": "context",
        })
        start_id = "1"
        test_id = "executionspace123"
        provider_id = "test_request_and_wait"
        # First request is 'start'.
        # Second request is 'status'.
        # Third request is 'stop' which should not be requested in this test.
        with FakeServer(
            ["ok", "ok", "no_content"],
            [
                {
                    "id": start_id
                },
                {
                    "execution_spaces": [{
                        "test_id": test_id
                    }],
                    "status": "DONE"
                },
                {},
            ],
        ) as server:
            ruleset = {
                "id": provider_id,
                "status": {
                    "host": server.host
                },
                "start": {
                    "host": server.host
                },
                "stop": {
                    "host": server.host
                },
            }
            self.logger.info("STEP: Initialize an external provider.")
            provider = Provider(etos, jsontas, ruleset)
            self.logger.info(
                "STEP: Send a checkout request via the external execution space provider."
            )
            execution_spaces = provider.request_and_wait_for_execution_spaces()
            self.logger.info(
                "STEP: Verify that the provider returns a list of checked out execution spaces."
            )
            dict_execution_spaces = [
                execution_space.as_dict for execution_space in execution_spaces
            ]
            test_execution_spaces = [
                ExecutionSpace(provider_id=provider_id,
                               test_id=test_id).as_dict
            ]
            self.assertEqual(dict_execution_spaces, test_execution_spaces)
Exemple #6
0
def release_environment(etos, jsontas, provider_registry, task_result,
                        release_id):  # pylint:disable=too-many-locals
    """Release an already requested environment.

    :param etos: ETOS library instance.
    :type etos: :obj:`etos_lib.ETOS`
    :param jsontas: JSONTas instance.
    :type jsontas: :obj:`jsontas.jsontas.JsonTas`
    :param provider_registry: The provider registry to get environments from.
    :type provider_registry: :obj:`environment_provider.lib.registry.ProviderRegistry`
    :param celery_worker: The worker holding the task results.
    :type celery_worker: :obj:`celery.Celery`
    :param release_id: The environment ID to release.
    :type release_id: str
    :return: Whether or not the release was successful together with
             a message should the release not be successful.
    :rtype tuple
    """
    if task_result is None or not task_result.result:
        return False, f"Nothing to release with task_id {release_id}"
    failure = None
    for suite in task_result.result.get("suites", []):
        etos.config.set("SUITE_ID", suite.get("suite_id"))
        iut = suite.get("iut")
        iut_ruleset = provider_registry.get_iut_provider_by_id(
            iut.get("provider_id")).get("iut")
        executor = suite.get("executor")
        executor_ruleset = provider_registry.get_execution_space_provider_by_id(
            executor.get("provider_id")).get("execution_space")
        log_area = suite.get("log_area")
        log_area_ruleset = provider_registry.get_log_area_provider_by_id(
            log_area.get("provider_id")).get("log")

        if iut_ruleset.get("type", "jsontas") == "external":
            success, exception = checkin_provider(
                Iut(**iut), ExternalIutProvider(etos, jsontas, iut_ruleset))
        else:
            success, exception = checkin_provider(
                Iut(**iut), IutProvider(etos, jsontas, iut_ruleset))
        if not success:
            failure = exception

        success, exception = checkin_provider(
            LogArea(**log_area),
            LogAreaProvider(etos, jsontas, log_area_ruleset))
        if not success:
            failure = exception
        success, exception = checkin_provider(
            ExecutionSpace(**executor),
            ExecutionSpaceProvider(etos, jsontas, executor_ruleset),
        )
        if not success:
            failure = exception
    task_result.forget()
    if failure:
        # Return the traceback from exception stored in failure.
        return False, "".join(
            traceback.format_exception(failure,
                                       value=failure,
                                       tb=failure.__traceback__))
    return True, ""