lorax-composer: deleting an unknown workspace should return an error

This changes the workspace delete behavior to match osbuild-composer's,
returning an error if the workspace doesn't exist.
This commit is contained in:
Brian C. Lane 2020-05-28 11:52:32 -07:00
parent 74f8cd4f34
commit 9a76c20c6b
3 changed files with 50 additions and 3 deletions

View File

@ -71,7 +71,7 @@ from pylorax.api.recipes import tag_recipe_commit, recipe_diff, RecipeFileError
from pylorax.api.regexes import VALID_API_STRING, VALID_BLUEPRINT_NAME
import pylorax.api.toml as toml
from pylorax.api.utils import take_limits, blueprint_exists
from pylorax.api.workspace import workspace_read, workspace_write, workspace_delete
from pylorax.api.workspace import workspace_read, workspace_write, workspace_delete, workspace_exists
# The API functions don't actually get called by any code here
# pylint: disable=unused-variable
@ -481,6 +481,9 @@ def v0_blueprints_delete_workspace(blueprint_name):
try:
with api.config["GITLOCK"].lock:
if not workspace_exists(api.config["GITLOCK"].repo, branch, blueprint_name):
raise Exception("Unknown blueprint: %s" % blueprint_name)
workspace_delete(api.config["GITLOCK"].repo, branch, blueprint_name)
except Exception as e:
log.error("(v0_blueprints_delete_workspace) %s", str(e))

View File

@ -81,6 +81,37 @@ def workspace_write(repo, branch, recipe):
open(filename, 'wb').write(recipe.toml().encode("UTF-8"))
def workspace_filename(repo, branch, recipe_name):
"""Return the path and filename of the workspace recipe
:param repo: Open repository
:type repo: Git.Repository
:param branch: Branch name
:type branch: str
:param recipe_name: The name of the recipe
:type recipe_name: str
:returns: workspace recipe path and filename
:rtype: str
"""
ws_dir = workspace_dir(repo, branch)
return joinpaths(ws_dir, recipe_filename(recipe_name))
def workspace_exists(repo, branch, recipe_name):
"""Return true of the workspace recipe exists
:param repo: Open repository
:type repo: Git.Repository
:param branch: Branch name
:type branch: str
:param recipe_name: The name of the recipe
:type recipe_name: str
:returns: True if the file exists
:rtype: bool
"""
return os.path.exists(workspace_filename(repo, branch, recipe_name))
def workspace_delete(repo, branch, recipe_name):
"""Delete the recipe from the workspace
@ -93,7 +124,6 @@ def workspace_delete(repo, branch, recipe_name):
:returns: None
:raises: IO related errors
"""
ws_dir = workspace_dir(repo, branch)
filename = joinpaths(ws_dir, recipe_filename(recipe_name))
filename = workspace_filename(repo, branch, recipe_name)
if os.path.exists(filename):
os.unlink(filename)

View File

@ -401,6 +401,13 @@ class ServerAPIV0TestCase(unittest.TestCase):
self.assertEqual(len(changes), 1)
self.assertEqual(changes[0], {"name":"example-glusterfs", "changed":True})
def test_09_blueprints_unknown_ws_delete(self):
"""Test DELETE /api/v0/blueprints/workspace/missing-blueprint"""
resp = self.server.delete("/api/v0/blueprints/workspace/missing-blueprint")
self.assertEqual(resp.status_code, 400)
data = json.loads(resp.data)
self.assertEqual(data["status"], False)
def test_09_blueprints_ws_delete(self):
"""Test DELETE /api/v0/blueprints/workspace/<blueprint_name>"""
# Write to the workspace first, just use the test_blueprints_ws_json test for this
@ -2042,6 +2049,13 @@ class ServerAPIV1TestCase(unittest.TestCase):
self.assertEqual(len(changes), 1)
self.assertEqual(changes[0], {"name":"example-glusterfs", "changed":True})
def test_09_blueprints_unknown_ws_delete(self):
"""Test DELETE /api/v1/blueprints/workspace/missing-blueprint"""
resp = self.server.delete("/api/v1/blueprints/workspace/missing-blueprint")
self.assertEqual(resp.status_code, 400)
data = json.loads(resp.data)
self.assertEqual(data["status"], False)
def test_09_blueprints_ws_delete(self):
"""Test DELETE /api/v1/blueprints/workspace/<blueprint_name>"""
# Write to the workspace first, just use the test_blueprints_ws_json test for this