diff --git a/projects/etos_suite_runner/src/etos_suite_runner/esr.py b/projects/etos_suite_runner/src/etos_suite_runner/esr.py index 16ef298..210dc60 100644 --- a/projects/etos_suite_runner/src/etos_suite_runner/esr.py +++ b/projects/etos_suite_runner/src/etos_suite_runner/esr.py @@ -235,8 +235,10 @@ def run_suites(self, triggered: EiffelActivityTriggeredEvent) -> list[str]: runner.start_suites_and_wait(suites) return [id for id, _ in suites] except EnvironmentProviderException as exc: - self.logger.info("Release test environment.") - self._release_environment() + # Not running as part of controller + if os.getenv("IDENTIFIER") is None: + self.logger.info("Release test environment.") + self._release_environment() self._record_exception(exc) raise exc diff --git a/projects/etos_suite_runner/src/etos_suite_runner/lib/runner.py b/projects/etos_suite_runner/src/etos_suite_runner/lib/runner.py index 929192e..0065d24 100644 --- a/projects/etos_suite_runner/src/etos_suite_runner/lib/runner.py +++ b/projects/etos_suite_runner/src/etos_suite_runner/lib/runner.py @@ -14,6 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. """ETOS suite runner executor.""" +import os import logging from multiprocessing.pool import ThreadPool @@ -86,8 +87,10 @@ def start_suites_and_wait(self, suites: list[tuple[str, Suite]]): self._record_exception(exc) raise exc finally: - self.logger.info("Release the full test environment.") - self._release_environment() + # Not running as part of controller + if os.getenv("IDENTIFIER") is None: + self.logger.info("Release the full test environment.") + self._release_environment() def run(self, test_suite): """Run test suite runner. diff --git a/projects/etos_suite_runner/src/etos_suite_runner/lib/suite.py b/projects/etos_suite_runner/src/etos_suite_runner/lib/suite.py index 7b332c2..3db4e7e 100644 --- a/projects/etos_suite_runner/src/etos_suite_runner/lib/suite.py +++ b/projects/etos_suite_runner/src/etos_suite_runner/lib/suite.py @@ -14,6 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. """Test suite handler.""" +import os import json import logging import threading @@ -26,6 +27,7 @@ from etos_lib import ETOS from etos_lib.logging.logger import FORMAT_CONFIG from etos_lib.opentelemetry.semconv import Attributes as SemConvAttributes +from etos_lib.kubernetes import Kubernetes, Environment from jsontas.jsontas import JsonTas import opentelemetry from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator @@ -153,14 +155,17 @@ def start(self, identifier: str, otel_context_carrier: dict) -> None: finally: opentelemetry.context.detach(otel_context_token) - def release(self, testrun_id) -> None: - """Release this sub suite.""" + def _delete_environment(self) -> bool: + """Delete environment from Kubernetes.""" + environment_name = self.environment.get("executor", {}).get("id") + environment_client = Environment(Kubernetes(), strict=True) + return environment_client.delete(environment_name) + + def _release_environment(self, testrun_id: str): + """Release environment manually via the environment provider.""" # TODO: This whole method is now a bit of a hack that needs to be cleaned up. # Most cleanup is required in the environment provider so this method will stay until an # update has been made there. - self.logger.info( - "Check in test environment %r", self.environment["id"], extra={"user_log": True} - ) jsontas = JsonTas() registry = ProviderRegistry(etos=self.etos, jsontas=jsontas, suite_id=testrun_id) @@ -180,13 +185,28 @@ def release(self, testrun_id) -> None: span.set_attribute(SemConvAttributes.TESTRUN_ID, testrun_id) span.set_attribute(SemConvAttributes.ENVIRONMENT, json.dumps(self.environment)) if failure is not None: - self.logger.exception( - "Failed to check in %r", self.environment["id"], extra={"user_log": True} - ) self._record_exception(failure) - return - self.logger.info("Checked in %r", self.environment["id"], extra={"user_log": True}) - self.released = True + return False + return True + + def release(self, testrun_id) -> None: + """Release this sub suite.""" + self.logger.info( + "Check in test environment %r", self.environment["id"], extra={"user_log": True} + ) + # Running as part of ETOS controller + if os.getenv("IDENTIFIER") is not None: + success = self._delete_environment() + else: + success = self._release_environment(testrun_id) + + if not success: + self.logger.exception( + "Failed to check in %r", self.environment["id"], extra={"user_log": True} + ) + return + self.logger.info("Checked in %r", self.environment["id"], extra={"user_log": True}) + self.released = True class TestSuite(OpenTelemetryBase): # pylint:disable=too-many-instance-attributes