From 697233c14a2d73bbfa1d2201708d07dbd7995e69 Mon Sep 17 00:00:00 2001 From: "Brian C. Lane" Date: Thu, 26 Sep 2019 11:37:12 -0700 Subject: [PATCH] lorax-composer: Handle RecipeError in commit_recipe_directory A recipe that is valid TOML can still be an invalid recipe (eg. missing the 'name' field) so this should also catch RecipeError. Also added tests for this, as well as making sure commit_recipe_file() raises the correct errors. Resolves: rhbz#1755068 --- src/pylorax/api/recipes.py | 2 +- tests/pylorax/test_recipes.py | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/pylorax/api/recipes.py b/src/pylorax/api/recipes.py index 546da524..c0ab55b4 100644 --- a/src/pylorax/api/recipes.py +++ b/src/pylorax/api/recipes.py @@ -841,7 +841,7 @@ def commit_recipe_directory(repo, branch, directory): # Skip files with errors, but try the others try: commit_recipe_file(repo, branch, joinpaths(directory, f)) - except (RecipeFileError, toml.TomlError): + except (RecipeError, RecipeFileError, toml.TomlError): pass def tag_recipe_commit(repo, branch, recipe_name): diff --git a/tests/pylorax/test_recipes.py b/tests/pylorax/test_recipes.py index 0b4f20f2..58b83b02 100644 --- a/tests/pylorax/test_recipes.py +++ b/tests/pylorax/test_recipes.py @@ -648,6 +648,16 @@ version = "0.0.1" [[packages]] name = "python" version = "2.7.*" +""") + + def _create_bad_toml_file(self): + open(self.new_recipe, 'w').write("""customizations] +hostname = "testing-bad-recipe" +""") + + def _create_bad_recipe(self): + open(self.new_recipe, 'w').write("""[customizations] +hostname = "testing-bad-recipe" """) def test_01_repo_creation(self): @@ -690,6 +700,18 @@ version = "2.7.*" with self.assertRaises(recipes.RecipeFileError): recipes.commit_recipe_file(self.repo, "master", recipe_path) + def test_04_commit_recipe_file_bad_toml(self): + """Test committing an invalid TOML file""" + self._create_bad_toml_file() + with self.assertRaises(TomlError): + recipes.commit_recipe_file(self.repo, "master", self.new_recipe) + + def test_04_commit_recipe_file_bad_recipe(self): + """Test committing an invalid recipe file""" + self._create_bad_recipe() + with self.assertRaises(recipes.RecipeError): + recipes.commit_recipe_file(self.repo, "master", self.new_recipe) + def test_05_commit_toml_dir(self): """Test committing a directory of TOML files""" # first verify that the newly created file isn't present @@ -716,6 +738,10 @@ version = "2.7.*" # then create it and commit the entire directory self._create_another_recipe() + # try to commit while raising RecipeError + with mock.patch('pylorax.api.recipes.commit_recipe_file', side_effect=recipes.RecipeError('TESTING')): + recipes.commit_recipe_directory(self.repo, "master", self.examples_path) + # try to commit while raising RecipeFileError with mock.patch('pylorax.api.recipes.commit_recipe_file', side_effect=recipes.RecipeFileError('TESTING')): recipes.commit_recipe_directory(self.repo, "master", self.examples_path)