From 26e7aede495018e99074401d14e32dbff858042d Mon Sep 17 00:00:00 2001 From: ash Date: Thu, 1 Feb 2024 10:39:24 +0000 Subject: [PATCH] Tidy up environments that could not even be scheduled with the builder --- softpack_core/schemas/environment.py | 10 ++++------ tests/integration/test_environment.py | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/softpack_core/schemas/environment.py b/softpack_core/schemas/environment.py index 0cc7272..5e8b20d 100644 --- a/softpack_core/schemas/environment.py +++ b/softpack_core/schemas/environment.py @@ -251,11 +251,11 @@ def create(cls, env: EnvironmentInput) -> CreateResponse: # type: ignore if input_err is not None: return input_err - name = env.name + versionless_name = env.name version = 1 while True: - env.name = name + "-" + str(version) + env.name = versionless_name + "-" + str(version) response = cls.create_new_env( env, Artifacts.built_by_softpack_file ) @@ -267,8 +267,6 @@ def create(cls, env: EnvironmentInput) -> CreateResponse: # type: ignore version += 1 - env.name = name - # Send build request try: host = app.settings.builder.host @@ -276,7 +274,7 @@ def create(cls, env: EnvironmentInput) -> CreateResponse: # type: ignore r = httpx.post( f"http://{host}:{port}/environments/build", json={ - "name": f"{env.path}/{env.name}", + "name": f"{env.path}/{versionless_name}", "version": str(version), "model": { "description": env.description, @@ -292,7 +290,7 @@ def create(cls, env: EnvironmentInput) -> CreateResponse: # type: ignore ) r.raise_for_status() except Exception as e: - # TODO: clean up scheduled build in repo + cls.delete(env.name, env.path) return BuilderError( message="Connection to builder failed: " + "".join(format_exception_only(type(e), e)) diff --git a/tests/integration/test_environment.py b/tests/integration/test_environment.py index 78b1f57..df068f3 100644 --- a/tests/integration/test_environment.py +++ b/tests/integration/test_environment.py @@ -26,6 +26,7 @@ State, UpdateEnvironmentSuccess, WriteArtifactSuccess, + BuilderError, ) from tests.integration.utils import file_in_remote @@ -126,6 +127,22 @@ def test_create_path_invalid_disallowed(httpx_post, testable_env_input): assert isinstance(result, InvalidInputError) +def test_create_cleans_up_after_builder_failure(httpx_post, testable_env_input): + httpx_post.side_effect = Exception('could not contact builder') + result = Environment.create(testable_env_input) + assert isinstance(result, BuilderError) + + dir = Path( + Environment.artifacts.environments_root, + testable_env_input.path, + testable_env_input.name + "-1", + ) + builtPath = dir / Environment.artifacts.built_by_softpack_file + ymlPath = dir / Environment.artifacts.environments_file + assert not file_in_remote(builtPath) + assert not file_in_remote(ymlPath) + + def builder_called_correctly( post_mock, testable_env_input: EnvironmentInput ) -> None: