diff --git a/src/pylorax/api/v0.py b/src/pylorax/api/v0.py index 9f7295dc..deb3be50 100644 --- a/src/pylorax/api/v0.py +++ b/src/pylorax/api/v0.py @@ -23,7 +23,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, delete_recipe, revert_recipe -from pylorax.api.recipes import tag_recipe_commit +from pylorax.api.recipes import tag_recipe_commit, recipe_diff 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 @@ -232,3 +232,33 @@ def v0_api(api): return jsonify(status=False, error={"msg":str(e)}), 400 else: return jsonify(status=True) + + @api.route("/api/v0/recipes/diff///") + @crossdomain(origin="*") + def v0_recipes_diff(recipe_name, from_commit, to_commit): + """Return the differences between two commits of a recipe""" + try: + if from_commit == "NEWEST": + with api.config["GITLOCK"].lock: + old_recipe = read_recipe_commit(api.config["GITLOCK"].repo, "master", recipe_name) + else: + with api.config["GITLOCK"].lock: + old_recipe = read_recipe_commit(api.config["GITLOCK"].repo, "master", recipe_name, from_commit) + except Exception as e: + return jsonify(status=False, error={"msg":str(e)}), 400 + + try: + if to_commit == "WORKSPACE": + with api.config["GITLOCK"].lock: + new_recipe = workspace_read(api.config["GITLOCK"].repo, "master", recipe_name) + elif to_commit == "NEWEST": + with api.config["GITLOCK"].lock: + new_recipe = read_recipe_commit(api.config["GITLOCK"].repo, "master", recipe_name) + else: + with api.config["GITLOCK"].lock: + new_recipe = read_recipe_commit(api.config["GITLOCK"].repo, "master", recipe_name, to_commit) + except Exception as e: + return jsonify(status=False, error={"msg":str(e)}), 400 + + diff = recipe_diff(old_recipe, new_recipe) + return jsonify(diff=diff) diff --git a/tests/pylorax/test_server.py b/tests/pylorax/test_server.py index 29f13482..a199bf39 100644 --- a/tests/pylorax/test_server.py +++ b/tests/pylorax/test_server.py @@ -303,3 +303,52 @@ class ServerTestCase(unittest.TestCase): changes = recipes[0].get("changes") self.assertEqual(len(changes) > 1, True) self.assertEqual(changes[0]["revision"], 1) + + def test_13_recipes_diff(self): + """Test /api/v0/recipes/diff///""" + resp = self.server.get("/api/v0/recipes/changes/glusterfs") + data = json.loads(resp.data) + self.assertNotEqual(data, None) + recipes = data.get("recipes") + self.assertNotEqual(recipes, None) + changes = recipes[0].get("changes") + self.assertEqual(len(changes) >= 2, True) + + from_commit = changes[1].get("commit") + self.assertNotEqual(from_commit, None) + to_commit = changes[0].get("commit") + self.assertNotEqual(to_commit, None) + + # Get the differences between the two commits + resp = self.server.get("/api/v0/recipes/diff/glusterfs/%s/%s" % (from_commit, to_commit)) + data = json.loads(resp.data) + self.assertNotEqual(data, None) + self.assertEqual(data, {"diff": [{"new": {"Version": "0.0.1"}, "old": {"Version": "0.2.1"}}]}) + + # Write to the workspace and check the diff + test_recipe = {"description": "An example GlusterFS server with samba, ws version", + "name":"glusterfs", + "version": "0.3.0", + "modules":[{"name":"glusterfs", "version":"3.7.*"}, + {"name":"glusterfs-cli", "version":"3.7.*"}], + "packages":[{"name":"samba", "version":"4.2.*"}, + {"name":"tmux", "version":"2.2"}]} + + resp = self.server.post("/api/v0/recipes/workspace", + data=json.dumps(test_recipe), + content_type="application/json") + data = json.loads(resp.data) + self.assertEqual(data, {"status":True}) + + # Get the differences between the newest commit and the workspace + resp = self.server.get("/api/v0/recipes/diff/glusterfs/NEWEST/WORKSPACE") + data = json.loads(resp.data) + self.assertNotEqual(data, None) + print(data) + result = {"diff": [{"new": {"Description": "An example GlusterFS server with samba, ws version"}, + "old": {"Description": "An example GlusterFS server with samba"}}, + {"new": {"Version": "0.3.0"}, + "old": {"Version": "0.0.1"}}, + {"new": {"Package": {"version": "2.2", "name": "tmux"}}, + "old": None}]} + self.assertEqual(data, result)