From c2fe33f9f4ed527a47326464e414a5914ba656a0 Mon Sep 17 00:00:00 2001 From: Will Thames Date: Thu, 14 Aug 2014 20:20:10 +1000 Subject: [PATCH] Tidied up a little, added tests Moved repo_url_to_role_name to common method in ansible.utils Added unit test for repo_url_to_role_name Added integration tests for galaxy --- bin/ansible-galaxy | 9 +++++---- lib/ansible/playbook/play.py | 4 ++-- lib/ansible/utils/__init__.py | 6 ++++++ test/integration/Makefile | 14 +++++++++++++- test/integration/galaxy_playbook.yml | 5 +++++ test/integration/galaxy_rolesfile | 2 ++ test/units/TestUtils.py | 5 +++++ 7 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 test/integration/galaxy_playbook.yml create mode 100644 test/integration/galaxy_rolesfile diff --git a/bin/ansible-galaxy b/bin/ansible-galaxy index e35c4bef88..a5fd6e1ae6 100755 --- a/bin/ansible-galaxy +++ b/bin/ansible-galaxy @@ -40,6 +40,7 @@ from jinja2 import Environment from optparse import OptionParser import ansible.constants as C +import ansible.utils default_meta_template = """--- galaxy_info: @@ -174,7 +175,7 @@ def build_option_parser(action): help='The path in which the skeleton role will be created. ' 'The default is the current working directory.') elif action == "install": - parser.set_usage("usage: %prog install [options] [-r FILE | role_name(s)[,version] | tar_file(s)]") + parser.set_usage("usage: %prog install [options] [-r FILE | role_name(s)[,version] | scm+role_repo_url[,version] | tar_file(s)]") parser.add_option( '-i', '--ignore-errors', dest='ignore_errors', action='store_true', default=False, help='Ignore errors and continue with the next specified role.') @@ -336,7 +337,7 @@ def scm_archive_role(scm, role_url, role_version): print "SCM %s is not currently supported" % scm return False tempdir = tempfile.mkdtemp() - role_name = role_url.split('/')[-1] + role_name = ansible.utils.repo_url_to_role_name(role_url) clone_cmd = [scm, 'clone', role_url] with open('/dev/null', 'w') as devnull: popen = subprocess.Popen(clone_cmd, cwd=tempdir, stdout=devnull, stderr=devnull) @@ -728,8 +729,8 @@ def execute_install(args, options, parser): exit_without_ignore(options) else: if '+' in role_name: - (scm, role_url) = role_name.split('+') - role_name = role_name.split('/')[-1] + (scm, role_url) = role_name.split('+',1) + role_name = ansible.utils.repo_url_to_role_name(role_name) # create tar file from scm url tmp_file = scm_archive_role(scm, role_url, role_version) role_data = None diff --git a/lib/ansible/playbook/play.py b/lib/ansible/playbook/play.py index bf27bfc857..cedf6ffba4 100644 --- a/lib/ansible/playbook/play.py +++ b/lib/ansible/playbook/play.py @@ -181,7 +181,7 @@ class Play(object): if '+' in orig_path and '://' in orig_path: # dependency name pointing to SCM URL # assume role name is last part of the URL - orig_path = orig_path.split('/')[-1] + orig_path = utils.repo_url_to_role_name(orig_path) role_vars = {} if type(orig_path) == dict: @@ -417,7 +417,7 @@ class Play(object): else: role_name = role if '+' in role_name and '://' in role_name: - role_name = role_name.split('/')[-1] + role_name = utils.repo_url_to_role_name(role_name) role_names.append(role_name) if os.path.isfile(task): diff --git a/lib/ansible/utils/__init__.py b/lib/ansible/utils/__init__.py index 3a3c5e5494..9c9dcadda7 100644 --- a/lib/ansible/utils/__init__.py +++ b/lib/ansible/utils/__init__.py @@ -352,6 +352,12 @@ def path_dwim_relative(original, dirname, source, playbook_base, check=True): raise errors.AnsibleError("input file not found at %s or %s" % (source2, obvious_local_path)) return source2 # which does not exist +def repo_url_to_role_name(repo_url): + trailing_path = repo_url.split('/')[-1] + if trailing_path.endswith('.git'): + trailing_path = trailing_path[:-4] + return trailing_path + def json_loads(data): ''' parse a JSON string and return a data structure ''' diff --git a/test/integration/Makefile b/test/integration/Makefile index 5ee75e97ee..e31b820e8b 100644 --- a/test/integration/Makefile +++ b/test/integration/Makefile @@ -14,9 +14,12 @@ else CREDENTIALS_ARG = endif +# http://unix.stackexchange.com/questions/30091/fix-or-alternative-for-mktemp-in-os-x +TMPDIR = $(shell mktemp -d 2>/dev/null || mktemp -d -t 'mytmpdir') + VAULT_PASSWORD_FILE = vault-password -all: non_destructive destructive includes unicode test_var_precedence check_mode test_hash test_handlers test_group_by test_vault parsing +all: non_destructive destructive includes unicode test_var_precedence check_mode test_hash test_handlers test_group_by test_vault parsing test_galaxy parsing: ansible-playbook bad_parsing.yml -i $(INVENTORY) -e @$(VARS_FILE) $(CREDENTIALS_ARG) -vvv $(TEST_FLAGS) --tags common,scenario1; [ $$? -eq 3 ] @@ -101,3 +104,12 @@ rackspace: $(CREDENTIALS_FILE) RC=$$? ; \ CLOUD_RESOURCE_PREFIX="$(CLOUD_RESOURCE_PREFIX)" make rackspace_cleanup ; \ exit $$RC; + +test_galaxy: + mytmpdir=$(TMPDIR) ; \ + ansible-galaxy install -r galaxy_rolesfile -p $$mytmpdir/roles ; \ + cp galaxy_playbook.yml $$mytmpdir ; \ + ansible-playbook -i $(INVENTORY) $$mytmpdir/galaxy_playbook.yml -v $(TEST_FLAGS) ; \ + RC=$$? ; \ + rm -rf $$mytmpdir ; \ + exit $$RC diff --git a/test/integration/galaxy_playbook.yml b/test/integration/galaxy_playbook.yml new file mode 100644 index 0000000000..1d9b03b22a --- /dev/null +++ b/test/integration/galaxy_playbook.yml @@ -0,0 +1,5 @@ +- hosts: localhost + connection: local + + roles: + - "git-ansible-galaxy" diff --git a/test/integration/galaxy_rolesfile b/test/integration/galaxy_rolesfile new file mode 100644 index 0000000000..cf1933e6e5 --- /dev/null +++ b/test/integration/galaxy_rolesfile @@ -0,0 +1,2 @@ +git+http://bitbucket.org/willthames/git-ansible-galaxy,v1.2 +hg+ssh://hg@bitbucket.org/willthames/hg-ansible-galaxy diff --git a/test/units/TestUtils.py b/test/units/TestUtils.py index feaab36bbb..ca4cbc07db 100644 --- a/test/units/TestUtils.py +++ b/test/units/TestUtils.py @@ -778,3 +778,8 @@ class TestUtils(unittest.TestCase): assert 'msg' not in data assert data['censored'] == 'results hidden due to no_log parameter' + def test_repo_url_to_role_name(self): + tests = [("http://git.example.com/repos/repo.git", "repo"), + ("ssh://git@git.example.com:repos/role-name", "role-name")] + for (url, result) in tests: + self.assertEqual(ansible.utils.repo_url_to_role_name(url), result)