def test_is_valid_file(self): with utils.RecipeDir() as recipes_dir: self.write_script_to_repo(recipes_dir, "contents", "1.sh") self.write_script_to_repo(recipes_dir, "contents", "2.sh") self.assertFalse(file_utils.is_valid_file(recipes_dir, "no_exist")) self.assertTrue( file_utils.is_valid_file(recipes_dir, "scripts/1.sh")) self.assertTrue( file_utils.is_valid_file(recipes_dir, "scripts/2.sh")) fname = os.path.join('..', '1.sh') self.assertFalse(file_utils.is_valid_file(recipes_dir, fname)) if os.path.exists("/etc/passwd"): fname = os.path.join('..', '..', '..', 'etc', 'passwd') self.assertFalse(file_utils.is_valid_file(recipes_dir, fname))
def check_files_valid(self, key, desc): """ Check to see if a list of filenames are valid. Input: key: str: key into the recipes dict to get the list of files desc: str: Description used in the error message. Return: bool: True if all are valid, else False """ ret = True for fname in self.recipe[key]: if not file_utils.is_valid_file(self.recipe_dir, fname): self.error("Not a valid %s file in '%s': %s" % (desc, self.recipe["filename"], fname)) ret = False return ret
def check(self): """ Make sure the recipe is valid. Checks the existence of filenames in the repo but not whether they are valid. Return: bool: True if valid, otherwise False """ ret = True if not self.recipe.get("display_name"): self.recipe["display_name"] = self.recipe["name"] self.error("'display_name' not set, setting to '%s'" % self.recipe["name"]) if self.recipe["automatic"].lower() not in [ "manual", "automatic", "authorized" ]: self.error( "Bad value '%s' for automatic. Options are 'manual', 'automatic', or 'authorized'" % self.recipe["automatic"]) ret = False if (not self.recipe["trigger_pull_request"] and not self.recipe["trigger_push"] and not self.recipe["trigger_manual"] and not self.recipe["allow_on_pr"] and not self.recipe["trigger_release"]): self.error("self.recipe %s does not have any triggers set" % self.recipe["name"]) ret = False if self.recipe[ "trigger_push"] and not self.recipe["trigger_push_branch"]: self.error("Push trigger needs a branch") ret = False if self.recipe["trigger_pull_request"] and self.recipe["allow_on_pr"]: self.error("trigger_pull_request and allow_on_pr are both set!") ret = False if self.recipe[ "trigger_manual"] and not self.recipe["trigger_manual_branch"]: self.error("Manual trigger needs a branch") ret = False if len(self.recipe["sha"]) != 40 or len(self.recipe["repo_sha"]) != 40: self.error("Recipes need to be in a repo!") ret = False if len(self.recipe["build_configs"]) == 0: self.error("You need to specify a build config!") ret = False if (not self.recipe.get("repository_server") or not self.recipe.get("repository_owner") or not self.recipe.get("repository_name")): self.error("Invalid repository!") ret = False if not self.check_files_valid("global_sources", "global source"): ret = False if not self.check_files_valid("pullrequest_dependencies", "pullrequest dependency"): ret = False if not self.check_files_valid("push_dependencies", "push dependency"): ret = False if not self.check_files_valid("manual_dependencies", "manual dependency"): ret = False if not self.check_files_valid("release_dependencies", "release dependency"): ret = False if (self.filename in self.recipe["pullrequest_dependencies"] or self.filename in self.recipe["push_dependencies"] or self.filename in self.recipe["manual_dependencies"] or self.filename in self.recipe["release_dependencies"]): self.error("Can't have a a dependency on itself!") ret = False if len(self.recipe["steps"]) == 0: self.error("No steps specified!") ret = False for step in self.recipe["steps"]: if not file_utils.is_valid_file(self.recipe_dir, step["script"]): self.error("Not a valid step file: %s" % step["script"]) ret = False return ret