Skip to content

Commit

Permalink
Merge pull request #33 from wtsi-hgi/autoincrement-versions
Browse files Browse the repository at this point in the history
Autoincrement versions for environments.
Remove update method.
  • Loading branch information
sb10 authored Oct 25, 2023
2 parents df4bfdf + d4c103b commit b302a01
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 101 deletions.
72 changes: 18 additions & 54 deletions softpack_core/schemas/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,16 +234,31 @@ def create(cls, env: EnvironmentInput) -> CreateResponse: # type: ignore
if result is not None:
return result

response = cls.create_new_env(env, Artifacts.built_by_softpack_file)
if not isinstance(response, CreateEnvironmentSuccess):
return response
name = env.name
version = 1

while True:
env.name = name + "-" + str(version)
response = cls.create_new_env(
env, Artifacts.built_by_softpack_file
)
if isinstance(response, CreateEnvironmentSuccess):
break

if not isinstance(response, EnvironmentAlreadyExistsError):
return response

version += 1

env.name = name

# TODO: remove hard-coding of URL.
# Send build request
httpx.post(
"http://0.0.0.0:7080/environments/build",
json={
"name": f"{env.path}/{env.name}",
"version": str(version),
"model": {
"description": env.description,
"packages": [f"{pkg.name}" for pkg in env.packages],
Expand Down Expand Up @@ -308,56 +323,6 @@ def create_new_env(
message="Successfully created environment in artifacts repo"
)

@classmethod
def update(
cls,
env: EnvironmentInput,
current_path: str,
current_name: str,
) -> UpdateResponse: # type: ignore
"""Update an Environment.
Args:
env: Details of the updated environment
path: The path of the current environment
name: The name of the current environment
Returns:
A message confirming the success or failure of the operation.
"""
result = env.validate()
if result is not None:
return result

if current_name == "" or current_path == "":
return InvalidInputError(message="current values must be supplied")

if env.path != current_path or env.name != current_name:
return InvalidInputError(
message=("change of name or path not currently supported")
)

result2 = cls.check_env_exists(Path(current_path, current_name))
if result2 is not None:
return result2

httpx.post(
"http://0.0.0.0:7080/environments/build",
json={
"name": f"{env.path}/{env.name}",
"model": {
"description": env.description,
"packages": [pkg.name for pkg in env.packages or []],
},
},
)

# TODO: validate the post worked

return UpdateEnvironmentSuccess(
message="Successfully updated environment"
)

@classmethod
def check_env_exists(
cls, path: Path
Expand Down Expand Up @@ -632,7 +597,6 @@ class Mutation:
"""GraphQL mutation schema."""

createEnvironment: CreateResponse = Environment.create # type: ignore
updateEnvironment: UpdateResponse = Environment.update # type: ignore
deleteEnvironment: DeleteResponse = Environment.delete # type: ignore
# writeArtifact: WriteArtifactResponse = ( # type: ignore
# Environment.write_artifact
Expand Down
67 changes: 20 additions & 47 deletions tests/integration/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,21 @@ def test_create(httpx_post, testable_env_input: EnvironmentInput) -> None:
path = Path(
Environment.artifacts.environments_root,
testable_env_input.path,
testable_env_input.name,
testable_env_input.name + "-1",
Environment.artifacts.built_by_softpack_file,
)
assert file_in_remote(path)

result = Environment.create(testable_env_input)
assert isinstance(result, EnvironmentAlreadyExistsError)
assert isinstance(result, CreateEnvironmentSuccess)

path = Path(
Environment.artifacts.environments_root,
testable_env_input.path,
testable_env_input.name + "-2",
Environment.artifacts.built_by_softpack_file,
)
assert file_in_remote(path)

orig_name = testable_env_input.name
testable_env_input.name = ""
Expand All @@ -79,6 +87,7 @@ def builder_called_correctly(
"http://0.0.0.0:7080/environments/build",
json={
"name": f"{testable_env_input.path}/{testable_env_input.name}",
"version": "1",
"model": {
"description": testable_env_input.description,
"packages": [
Expand All @@ -89,42 +98,6 @@ def builder_called_correctly(
)


def test_update(httpx_post, testable_env_input) -> None:
result = Environment.create(testable_env_input)
assert isinstance(result, CreateEnvironmentSuccess)
httpx_post.assert_called_once()

testable_env_input.description = "updated description"
result = Environment.update(
testable_env_input,
testable_env_input.path,
testable_env_input.name,
)
assert isinstance(result, UpdateEnvironmentSuccess)

builder_called_correctly(httpx_post, testable_env_input)

result = Environment.update(
testable_env_input, "invalid/path", "invalid_name"
)
assert isinstance(result, InvalidInputError)

testable_env_input.name = ""
result = Environment.update(
testable_env_input,
testable_env_input.path,
testable_env_input.name,
)
assert isinstance(result, InvalidInputError)

testable_env_input.name = "invalid_name"
testable_env_input.path = "invalid/path"
result = Environment.update(
testable_env_input, "invalid/path", "invalid_name"
)
assert isinstance(result, EnvironmentNotFoundError)


def test_delete(httpx_post, testable_env_input) -> None:
result = Environment.delete(
testable_env_input.name, testable_env_input.path
Expand All @@ -138,13 +111,13 @@ def test_delete(httpx_post, testable_env_input) -> None:
path = Path(
Environment.artifacts.environments_root,
testable_env_input.path,
testable_env_input.name,
testable_env_input.name + "-1",
Artifacts.built_by_softpack_file,
)
assert file_in_remote(path)

result = Environment.delete(
testable_env_input.name, testable_env_input.path
testable_env_input.name + "-1", testable_env_input.path
)
assert isinstance(result, DeleteEnvironmentSuccess)

Expand All @@ -157,7 +130,7 @@ async def test_write_artifact(httpx_post, testable_env_input):

result = await Environment.write_artifact(
file=upload,
folder_path=f"{testable_env_input.path}/{testable_env_input.name}",
folder_path=f"{testable_env_input.path}/{testable_env_input.name}-1",
file_name=upload.filename,
)
assert isinstance(result, InvalidInputError)
Expand All @@ -168,15 +141,15 @@ async def test_write_artifact(httpx_post, testable_env_input):

result = await Environment.write_artifact(
file=upload,
folder_path=f"{testable_env_input.path}/{testable_env_input.name}",
folder_path=f"{testable_env_input.path}/{testable_env_input.name}-1",
file_name=upload.filename,
)
assert isinstance(result, WriteArtifactSuccess)

path = Path(
Environment.artifacts.environments_root,
testable_env_input.path,
testable_env_input.name,
testable_env_input.name + "-1",
upload.filename,
)
assert file_in_remote(path)
Expand Down Expand Up @@ -209,12 +182,12 @@ async def test_states(httpx_post, testable_env_input):

result = await Environment.write_artifact(
file=upload,
folder_path=f"{testable_env_input.path}/{testable_env_input.name}",
folder_path=f"{testable_env_input.path}/{testable_env_input.name}-1",
file_name=upload.filename,
)
assert isinstance(result, WriteArtifactSuccess)

env = get_env_from_iter(testable_env_input.name)
env = get_env_from_iter(testable_env_input.name + "-1")
assert env is not None
assert any(p.name == "zlib" for p in env.packages)
assert any(p.version == "v1.1" for p in env.packages)
Expand All @@ -227,12 +200,12 @@ async def test_states(httpx_post, testable_env_input):

result = await Environment.write_artifact(
file=upload,
folder_path=f"{testable_env_input.path}/{testable_env_input.name}",
folder_path=f"{testable_env_input.path}/{testable_env_input.name}-1",
file_name=upload.filename,
)
assert isinstance(result, WriteArtifactSuccess)

env = get_env_from_iter(testable_env_input.name)
env = get_env_from_iter(testable_env_input.name + "-1")
assert env is not None
assert env.type == Artifacts.built_by_softpack
assert env.state == State.ready
Expand Down

0 comments on commit b302a01

Please sign in to comment.