Skip to content

Commit

Permalink
Delete environment when running with controller
Browse files Browse the repository at this point in the history
  • Loading branch information
t-persson committed Aug 16, 2024
1 parent 1a723ea commit f1fd4bc
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
6 changes: 4 additions & 2 deletions projects/etos_suite_runner/src/etos_suite_runner/esr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down
42 changes: 31 additions & 11 deletions projects/etos_suite_runner/src/etos_suite_runner/lib/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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
Expand Down

0 comments on commit f1fd4bc

Please sign in to comment.