From c294b7747d3378e0f54eef9e292f6552d0261930 Mon Sep 17 00:00:00 2001 From: "Brian C. Lane" Date: Mon, 13 Nov 2017 12:42:59 -0800 Subject: [PATCH] Add DELETE /recipes/delete/ route and tests Also add numbering to the tests so that they are executed in the expected order. --- src/pylorax/api/v0.py | 14 +++++++++++++- tests/pylorax/test_server.py | 33 +++++++++++++++++++++++---------- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/src/pylorax/api/v0.py b/src/pylorax/api/v0.py index e1d49dcf..1278231f 100644 --- a/src/pylorax/api/v0.py +++ b/src/pylorax/api/v0.py @@ -22,7 +22,7 @@ from pykickstart.version import makeVersion, RHEL7 from pylorax.api.crossdomain import crossdomain from pylorax.api.recipes import list_branch_files, read_recipe_commit, recipe_filename, list_commits -from pylorax.api.recipes import recipe_from_dict, recipe_from_toml, commit_recipe +from pylorax.api.recipes import recipe_from_dict, recipe_from_toml, commit_recipe, delete_recipe from pylorax.api.workspace import workspace_read, workspace_write, workspace_delete from pylorax.creator import DRACUT_DEFAULT, mount_boot_part_over_root from pylorax.creator import make_appliance, make_image, make_livecd, make_live_images @@ -164,6 +164,18 @@ def v0_api(api): else: return jsonify(status=True) + @api.route("/api/v0/recipes/delete/", methods=["DELETE"]) + @crossdomain(origin="*") + def v0_recipes_delete(recipe_name): + """Delete a recipe from git""" + try: + with api.config["GITLOCK"].lock: + delete_recipe(api.config["GITLOCK"].repo, "master", recipe_name) + except Exception as e: + return jsonify(status=False, error={"msg":str(e)}), 400 + else: + return jsonify(status=True) + @api.route("/api/v0/recipes/workspace", methods=["POST"]) @crossdomain(origin="*") def v0_recipes_workspace(): diff --git a/tests/pylorax/test_server.py b/tests/pylorax/test_server.py index c9913ca2..fc7a387a 100644 --- a/tests/pylorax/test_server.py +++ b/tests/pylorax/test_server.py @@ -46,14 +46,14 @@ class ServerTestCase(unittest.TestCase): def tearDownClass(self): shutil.rmtree(server.config["REPO_DIR"]) - def test_status(self): + def test_01_status(self): """Test the /api/v0/status route""" status_dict = {"build":"devel", "api":"0", "db_version":"0", "schema_version":"0", "db_supported":False} resp = self.server.get("/api/v0/status") data = json.loads(resp.data) self.assertEqual(data, status_dict) - def test_recipes_list(self): + def test_02_recipes_list(self): """Test the /api/v0/recipes/list route""" list_dict = {"recipes":["atlas", "development", "glusterfs", "http-server", "jboss", "kubernetes"], "limit":20, "offset":0, "total":6} @@ -61,7 +61,7 @@ class ServerTestCase(unittest.TestCase): data = json.loads(resp.data) self.assertEqual(data, list_dict) - def test_recipes_info(self): + def test_03_recipes_info(self): """Test the /api/v0/recipes/info route""" info_dict_1 = {"changes":[{"changed":False, "name":"http-server"}], "errors":[], @@ -113,7 +113,7 @@ class ServerTestCase(unittest.TestCase): data = json.loads(resp.data) self.assertEqual(data, info_dict_3) - def test_recipes_changes(self): + def test_04_recipes_changes(self): """Test the /api/v0/recipes/changes route""" resp = self.server.get("/api/v0/recipes/changes/http-server") data = json.loads(resp.data) @@ -127,7 +127,7 @@ class ServerTestCase(unittest.TestCase): self.assertEqual(data["recipes"][0]["name"], "http-server") self.assertEqual(len(data["recipes"][0]["changes"]), 1) - def test_recipes_new_json(self): + def test_05_recipes_new_json(self): """Test the /api/v0/recipes/new route with json recipe""" test_recipe = {"description": "An example GlusterFS server with samba", "name":"glusterfs", @@ -150,7 +150,7 @@ class ServerTestCase(unittest.TestCase): self.assertEqual(len(recipes), 1) self.assertEqual(recipes[0], test_recipe) - def test_recipes_new_toml(self): + def test_06_recipes_new_toml(self): """Test the /api/v0/recipes/new route with toml recipe""" test_recipe = open(joinpaths(self.examples_path, "glusterfs.toml"), "rb").read() resp = self.server.post("/api/v0/recipes/new", @@ -171,7 +171,7 @@ class ServerTestCase(unittest.TestCase): self.assertEqual(recipes[0], test_recipe) - def test_recipes_ws_json(self): + def test_07_recipes_ws_json(self): """Test the /api/v0/recipes/workspace route with json recipe""" test_recipe = {"description": "An example GlusterFS server with samba, ws version", "name":"glusterfs", @@ -197,7 +197,7 @@ class ServerTestCase(unittest.TestCase): self.assertEqual(len(changes), 1) self.assertEqual(changes[0], {"name":"glusterfs", "changed":True}) - def test_recipes_ws_toml(self): + def test_08_recipes_ws_toml(self): """Test the /api/v0/recipes/workspace route with toml recipe""" test_recipe = {"description": "An example GlusterFS server with samba, ws version", "name":"glusterfs", @@ -223,10 +223,10 @@ class ServerTestCase(unittest.TestCase): self.assertEqual(len(changes), 1) self.assertEqual(changes[0], {"name":"glusterfs", "changed":True}) - def test_recipes_ws_delete(self): + def test_09_recipes_ws_delete(self): """Test DELETE /api/v0/recipes/workspace/""" # Write to the workspace first, just use the test_recipes_ws_json test for this - self.test_recipes_ws_json() + self.test_07_recipes_ws_json() # Delete it resp = self.server.delete("/api/v0/recipes/workspace/glusterfs") @@ -243,3 +243,16 @@ class ServerTestCase(unittest.TestCase): changes = data.get("changes") self.assertEqual(len(changes), 1) self.assertEqual(changes[0], {"name":"glusterfs", "changed":False}) + + def test_10_recipes_delete(self): + """Test DELETE /api/v0/recipes/delete/""" + resp = self.server.delete("/api/v0/recipes/delete/glusterfs") + data = json.loads(resp.data) + self.assertEqual(data, {"status":True}) + + # Make sure glusterfs is no longer in the list of recipes + resp = self.server.get("/api/v0/recipes/list") + data = json.loads(resp.data) + self.assertNotEqual(data, None) + recipes = data.get("recipes") + self.assertEqual("glusterfs" in recipes, False)