def test_gendoc(self): all_metas = {} idempotency_tag = "_idem" relative_readme_path = "README.rst" relative_meta_path = os.path.join("meta", "main.yml") role_names = th.create_roles(self.test_path, 3) # ---------------------------------------------------------------- # add meta files to the roles # ---------------------------------------------------------------- for i, role in enumerate(role_names): defaults_path = os.path.join(self.test_path, role, "defaults", "main.yml") tasks_path = os.path.join(self.test_path, role, "tasks", "main.yml") utils.string_to_file(defaults_path, th.DEFAULTS_TEMPLATE) utils.string_to_file(tasks_path, th.TASKS_TEMPLATE) # ---------------------------------------------------------------- # run the genmeta command and check its output # ---------------------------------------------------------------- (out, err) = utils.capture_shell( "ansigenome genmeta {0}".format(self.test_path)) if os.path.exists(relative_meta_path): th.populate_dict_with_files(self.test_path, role_names, all_metas, relative_readme_path) th.print_out("Genmeta output without terminal colors:", out) self.assertIn("meta files 0 ok 0 skipped " + "3 changed", out) # ---------------------------------------------------------------- # run the genmeta command on the same roles to test idempotency # ---------------------------------------------------------------- (out, err) = utils.capture_shell( "ansigenome genmeta {0}".format(self.test_path)) if os.path.exists(relative_meta_path): th.populate_dict_with_files(self.test_path, role_names, all_metas, relative_readme_path, idempotency_tag) # run a diff to help debug idempotency issues th.run_diff_on(all_metas, relative_meta_path, idempotency_tag) th.print_out("Genmeta output idempotency test:", out) self.assertIn("meta files 0 ok 3 skipped " + "0 changed", out)
def create_roles(path, number=2): """ Create a number of roles. """ role_names = [] for i in range(number): full_path = os.path.join(path, random_string()) utils.capture_shell("ansigenome init {0} -c system".format(full_path)) role_names.append(os.path.basename(full_path)) return role_names
def test_reqs_yml(self): out_dir = os.path.join(self.test_path, "requirements") out_path = os.path.join(out_dir, "requirements.yml") role_names = th.create_roles(self.test_path) cli_flags = "-f yml" (out, err) = utils.capture_shell( "ansigenome export -t reqs {0} {2} -o {1}".format(self.test_path, out_path, cli_flags)) self.assertTrue(os.path.exists(out_path)) requirements = utils.file_to_string(out_path) for role in role_names: self.assertIn(role, requirements) self.assertIn("/ansible-", requirements) self.assertIn("testuser.", requirements) self.assertIn("'git'", requirements) self.assertIn("master", requirements) self.assertEqual(out, "") self.assertEqual(err, "")
def test_dump(self): out_file = os.path.join(self.test_path, "dump.json") role_names = th.create_roles(self.test_path) for role in role_names: defaults_path = os.path.join(self.test_path, role, "defaults", "main.yml") tasks_path = os.path.join(self.test_path, role, "tasks", "main.yml") meta_path = os.path.join(self.test_path, role, "meta", "main.yml") utils.string_to_file(defaults_path, th.DEFAULTS_TEMPLATE) utils.string_to_file(tasks_path, th.TASKS_TEMPLATE) utils.string_to_file(meta_path, th.META_TEMPLATE) (out, err) = utils.capture_shell( "ansigenome export -t dump {0} -o {1}".format(self.test_path, out_file)) self.assertTrue(os.path.exists(out_file)) json_contents = utils.file_to_string(out_file) contents = th.json_to_dict(json_contents) self.assertIn("roles", contents) self.assertIn("totals", contents) self.assertIn("dependencies", json_contents) self.assertIsInstance(contents, dict) self.assertEqual(err, "")
def assert_run(self, args, run_output): th.create_roles(self.test_path) (out, err) = utils.capture_shell("ansigenome run {0} {1}".format(self.test_path, args)) self.assertIn(run_output, out) self.assertEqual(err, "")
def test_scan(self): role_names = th.create_roles(self.test_path, 3) for i, role in enumerate(role_names): defaults_path = os.path.join(self.test_path, role, "defaults", "main.yml") tasks_path = os.path.join(self.test_path, role, "tasks", "main.yml") meta_path = os.path.join(self.test_path, role, "meta", "main.yml") utils.string_to_file(defaults_path, th.DEFAULTS_TEMPLATE) utils.string_to_file(tasks_path, th.TASKS_TEMPLATE) # remove 1 of the meta files if i == 2: os.remove(meta_path) (out, err) = utils.capture_shell("ansigenome scan {0}".format( self.test_path)) th.print_out("Scan output without terminal colors:", out) self.assertIn("3 roles", out) self.assertIn("3 defaults", out) self.assertIn("9 defaults", out) self.assertIn("2 facts", out) self.assertIn("6 facts", out) self.assertIn("8 files", out) self.assertIn("26 files", out) self.assertIn("88 lines", out) self.assertIn("230 lines", out) self.assertIn( "0 ok 3 missing readme(s) " + "0 missing meta(s)", out) self.assertEqual(err, "")
def assert_run(self, args, run_output): th.create_roles(self.test_path) (out, err) = utils.capture_shell("ansigenome run {0} {1}".format( self.test_path, args)) self.assertIn(run_output, out) self.assertEqual(err, "")
def test_graph_png(self): out_dir = os.path.join(self.test_path, "graph") out_path = os.path.join(out_dir, "test.png") th.create_roles(self.test_path) (out, err) = utils.capture_shell( "ansigenome export {0} -o {1}".format(self.test_path, out_path)) self.assertTrue(os.path.exists(out_path)) self.assertEqual(out, "") self.assertEqual(err, "")
def test_templates(self): role = th.create_roles(self.test_path, 1) role_name = role[0] role_path = os.path.join(self.test_path, role_name) readme_path = os.path.join(role_path, "README.rst") defaults_path = os.path.join(self.test_path, role_name, "defaults", "main.yml") tasks_path = os.path.join(self.test_path, role_name, "tasks", "main.yml") meta_path = os.path.join(self.test_path, role_name, "meta", "main.yml") utils.string_to_file(defaults_path, th.DEFAULTS_TEMPLATE) utils.string_to_file(tasks_path, th.TASKS_TEMPLATE) utils.string_to_file(meta_path, th.META_TEMPLATE_FULL) (out, err) = utils.capture_shell( "ansigenome gendoc {0}".format(self.test_path)) readme = utils.file_to_string(readme_path) print print "README compiled template:" print readme print self.assertIn("tear.drinker", readme) self.assertIn("Chuck Norris", readme) self.assertIn("Travis", readme) self.assertIn("poop", readme) self.assertIn("imposes", readme) self.assertIn("fists", readme) self.assertIn("telekinesis", readme) self.assertIn(role_name, readme) self.assertIn("testuser." + role_name, readme) self.assertIn("ansible-" + role_name, readme) self.assertIn("theuniverse", readme) self.assertIn("foo: bar", readme) self.assertIn("unix_is_cool", readme) self.assertIn("ansigenome", readme) self.assertIn("galaxy.ansible.com", readme) self.assertNotIn("BETA", readme) self.assertNotIn("beta", readme) self.assertNotIn("deprecated", readme) self.assertNotIn("galaxy-install", readme) self.assertNotIn("twitter", readme)
def test_templates(self): role = th.create_roles(self.test_path, 1) role_name = role[0] role_path = os.path.join(self.test_path, role_name) readme_path = os.path.join(role_path, "README.rst") defaults_path = os.path.join(self.test_path, role_name, "defaults", "main.yml") tasks_path = os.path.join(self.test_path, role_name, "tasks", "main.yml") meta_path = os.path.join(self.test_path, role_name, "meta", "main.yml") utils.string_to_file(defaults_path, th.DEFAULTS_TEMPLATE) utils.string_to_file(tasks_path, th.TASKS_TEMPLATE) utils.string_to_file(meta_path, th.META_TEMPLATE_FULL) (out, err) = utils.capture_shell( "ansigenome gendoc {0}".format(self.test_path)) readme = utils.file_to_string(readme_path) print() print("README compiled template:") print(readme) print self.assertIn("tear.drinker", readme) self.assertIn("Chuck Norris", readme) self.assertIn("Travis", readme) self.assertIn("poop", readme) self.assertIn("imposes", readme) self.assertIn("fists", readme) self.assertIn("telekinesis", readme) self.assertIn(role_name, readme) self.assertIn("testuser." + role_name, readme) self.assertIn("ansible-" + role_name, readme) self.assertIn("theuniverse", readme) self.assertIn("foo: bar", readme) self.assertIn("unix_is_cool", readme) self.assertIn("ansigenome", readme) self.assertIn("galaxy.ansible.com", readme) self.assertNotIn("BETA", readme) self.assertNotIn("beta", readme) self.assertNotIn("deprecated", readme) self.assertNotIn("galaxy-install", readme) self.assertNotIn("twitter", readme)
def assert_role(self, args=""): role_name = th.random_string() path = os.path.join(self.test_path, role_name) meta_path = os.path.join(path, "meta", "main.yml") (out, err) = utils.capture_shell("ansigenome init {0} {1}".format( path, args)) for folder in c.ANSIBLE_FOLDERS: assert os.path.exists(os.path.join(path, folder)), "expected " + \ "'{0}' to be created".format(folder) if folder == "tests": inventory_path = os.path.join(path, folder, "inventory") assert os.path.exists(os.path.join(path, folder, "test")), "expected " + \ "'{0}' to be created".format(folder) assert os.path.exists(os.path.join(inventory_path, "hosts")), "expected " + \ "'{0}' to be created".format(folder) assert os.path.exists(os.path.join(path, "VERSION")) meta = utils.file_to_string(meta_path) print print "Meta file:" print meta print self.assertIn("Test User", meta) self.assertIn("1 sentence", meta) self.assertIn(role_name, meta) self.assertIn("Debian", meta) self.assertIn("categories: [foo,bar,baz]", meta) self.assertIn("ansigenome_info", meta) self.assertIn("galaxy_id", meta) self.assertIn("travis: True", meta) self.assertIn("Describe", meta) self.assertIn("custom", meta) self.assertEqual(out, "") self.assertEqual(err, "")
def assert_role(self, args=""): role_name = th.random_string() path = os.path.join(self.test_path, role_name) meta_path = os.path.join(path, "meta", "main.yml") (out, err) = utils.capture_shell( "ansigenome init {0} {1}".format(path, args)) for folder in c.ANSIBLE_FOLDERS: assert os.path.exists(os.path.join(path, folder)), "expected " + \ "'{0}' to be created".format(folder) if folder == "tests": inventory_path = os.path.join(path, folder, "inventory") assert os.path.exists(os.path.join(path, folder, "test")), "expected " + \ "'{0}' to be created".format(folder) assert os.path.exists(os.path.join(inventory_path, "hosts")), "expected " + \ "'{0}' to be created".format(folder) assert os.path.exists(os.path.join(path, "VERSION")) meta = utils.file_to_string(meta_path) print print "Meta file:" print meta print self.assertIn("Test User", meta) self.assertIn("1 sentence", meta) self.assertIn(role_name, meta) self.assertIn("Debian", meta) self.assertIn("categories: [foo,bar,baz]", meta) self.assertIn("ansigenome_info", meta) self.assertIn("galaxy_id", meta) self.assertIn("travis: True", meta) self.assertIn("Describe", meta) self.assertIn("custom", meta) self.assertEqual(out, "") self.assertEqual(err, "")
def test_reqs_txt(self): out_dir = os.path.join(self.test_path, "requirements") out_path = os.path.join(out_dir, "requirements.txt") role_names = th.create_roles(self.test_path) (out, err) = utils.capture_shell( "ansigenome export -t reqs {0} -o {1}".format(self.test_path, out_path)) self.assertTrue(os.path.exists(out_path)) requirements = utils.file_to_string(out_path) for role in role_names: self.assertIn("testuser.", requirements) self.assertIn(role, requirements) self.assertIn(",", requirements) self.assertEqual(out, "") self.assertEqual(err, "")
def test_scan(self): role_names = th.create_roles(self.test_path, 3) for i, role in enumerate(role_names): defaults_path = os.path.join(self.test_path, role, "defaults", "main.yml") tasks_path = os.path.join(self.test_path, role, "tasks", "main.yml") meta_path = os.path.join(self.test_path, role, "meta", "main.yml") utils.string_to_file(defaults_path, th.DEFAULTS_TEMPLATE) utils.string_to_file(tasks_path, th.TASKS_TEMPLATE) # remove 1 of the meta files if i == 2: os.remove(meta_path) (out, err) = utils.capture_shell( "ansigenome scan {0}".format(self.test_path)) th.print_out("Scan output without terminal colors:", out) self.assertIn("3 roles", out) self.assertIn("3 defaults", out) self.assertIn("9 defaults", out) self.assertIn("2 facts", out) self.assertIn("6 facts", out) self.assertIn("8 files", out) self.assertIn("26 files", out) self.assertIn("88 lines", out) self.assertIn("230 lines", out) self.assertIn("0 ok 3 missing readme(s) " + "0 missing meta(s)", out) self.assertEqual(err, "")
def test_graph_dot(self): out_dir = os.path.join(self.test_path, "graph") out_path = os.path.join(out_dir, "test.dot") role_names = th.create_roles(self.test_path) (out, err) = utils.capture_shell( "ansigenome export {0} -f dot -o {1}".format(self.test_path, out_path)) self.assertTrue(os.path.exists(out_path)) dot = utils.file_to_string(out_path) for role in role_names: self.assertIn(role, dot) self.assertIn("size", dot) self.assertIn("dpi", dot) self.assertIn("node", dot) self.assertIn("edge", dot) self.assertNotIn("->", dot) self.assertEqual(out, "") self.assertEqual(err, "")
def test_gendoc(self): all_readmes = {} idempotency_tag = "_idem" relative_readme_path = "README.rst" relative_meta_path = os.path.join("meta", "main.yml") role_names = th.create_roles(self.test_path, 3) # ---------------------------------------------------------------- # add readme files to the roles # ---------------------------------------------------------------- for i, role in enumerate(role_names): defaults_path = os.path.join(self.test_path, role, "defaults", "main.yml") tasks_path = os.path.join(self.test_path, role, "tasks", "main.yml") utils.string_to_file(defaults_path, th.DEFAULTS_TEMPLATE) utils.string_to_file(tasks_path, th.TASKS_TEMPLATE) # ---------------------------------------------------------------- # run the gendoc command and check its output # ---------------------------------------------------------------- (out, err) = utils.capture_shell("ansigenome gendoc {0}".format( self.test_path)) if os.path.exists(relative_meta_path): th.populate_dict_with_files(self.test_path, role_names, all_readmes, relative_readme_path) th.print_out("Gendoc output without terminal colors:", out) self.assertIn( "readme files 3 ok 0 skipped " + "0 changed", out) # ---------------------------------------------------------------- # run the gendoc command on the same roles to test idempotency # ---------------------------------------------------------------- (out, err) = utils.capture_shell("ansigenome gendoc {0}".format( self.test_path)) if os.path.exists(relative_meta_path): th.populate_dict_with_files(self.test_path, role_names, all_readmes, relative_readme_path, idempotency_tag) # run a diff to help debug idempotency issues th.run_diff_on(all_readmes, relative_readme_path, idempotency_tag) th.print_out("Gendoc output idempotency test:", out) self.assertIn( "readme files 0 ok 3 skipped " + "0 changed", out) # ---------------------------------------------------------------- # run the gendoc command on the same roles to test failures # ---------------------------------------------------------------- broken_meta = "{{" first_role = role_names[0] fail_path = os.path.join(self.test_path, first_role, relative_meta_path) utils.string_to_file(fail_path, broken_meta) (out, err) = utils.capture_shell("ansigenome gendoc {0}".format( self.test_path)) th.print_out("Gendoc output failed test:", out) self.assertIn("^", out) assert err == "", "expected empty err but got:\n{0}".format(err)