2016-03-26 08:22:37 +01:00
|
|
|
# (c) 2016, Adrian Likins <alikins@redhat.com>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
# Make coding more python3-ish
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2016-07-11 19:54:29 +02:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import tarfile
|
2016-07-12 22:01:47 +02:00
|
|
|
import tempfile
|
2016-07-11 19:54:29 +02:00
|
|
|
|
2016-07-07 20:25:08 +02:00
|
|
|
from ansible.compat.six import PY3
|
|
|
|
from ansible.compat.tests import unittest
|
2016-09-13 19:50:05 +02:00
|
|
|
from ansible.compat.tests.mock import call, patch
|
2016-07-07 20:25:08 +02:00
|
|
|
|
|
|
|
import ansible
|
2016-07-22 17:45:56 +02:00
|
|
|
from ansible.errors import AnsibleError, AnsibleOptionsError
|
|
|
|
|
2016-03-26 08:22:37 +01:00
|
|
|
from ansible.cli.galaxy import GalaxyCLI
|
|
|
|
|
2016-08-25 16:30:03 +02:00
|
|
|
|
2016-03-26 08:22:37 +01:00
|
|
|
class TestGalaxy(unittest.TestCase):
|
2016-07-11 19:54:29 +02:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
'''creating prerequisites for installing a role; setUpClass occurs ONCE whereas setUp occurs with every method tested.'''
|
|
|
|
# class data for easy viewing: role_dir, role_tar, role_name, role_req, role_path
|
2016-07-07 20:25:08 +02:00
|
|
|
|
2016-07-11 19:54:29 +02:00
|
|
|
if os.path.exists("./delete_me"):
|
|
|
|
shutil.rmtree("./delete_me")
|
2016-07-07 20:25:08 +02:00
|
|
|
|
2016-07-11 19:54:29 +02:00
|
|
|
# creating framework for a role
|
2016-09-14 20:49:54 +02:00
|
|
|
gc = GalaxyCLI(args=["init", "-c", "--offline", "delete_me"])
|
|
|
|
gc.parse()
|
2016-07-11 19:54:29 +02:00
|
|
|
gc.run()
|
|
|
|
cls.role_dir = "./delete_me"
|
|
|
|
cls.role_name = "delete_me"
|
2016-07-12 22:01:47 +02:00
|
|
|
|
|
|
|
# making a temp dir for role installation
|
|
|
|
cls.role_path = os.path.join(tempfile.mkdtemp(), "roles")
|
|
|
|
if not os.path.isdir(cls.role_path):
|
|
|
|
os.makedirs(cls.role_path)
|
2016-07-11 19:54:29 +02:00
|
|
|
|
|
|
|
# creating a tar file name for class data
|
|
|
|
cls.role_tar = './delete_me.tar.gz'
|
|
|
|
cls.makeTar(cls.role_tar, cls.role_dir)
|
|
|
|
|
|
|
|
# creating a temp file with installation requirements
|
|
|
|
cls.role_req = './delete_me_requirements.yml'
|
|
|
|
fd = open(cls.role_req, "w")
|
|
|
|
fd.write("- 'src': '%s'\n 'name': '%s'\n 'path': '%s'" % (cls.role_tar, cls.role_name, cls.role_path))
|
|
|
|
fd.close()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def makeTar(cls, output_file, source_dir):
|
|
|
|
''' used for making a tarfile from a role directory '''
|
|
|
|
# adding directory into a tar file
|
|
|
|
try:
|
|
|
|
tar = tarfile.open(output_file, "w:gz")
|
|
|
|
tar.add(source_dir, arcname=os.path.basename(source_dir))
|
|
|
|
except AttributeError: # tarfile obj. has no attribute __exit__ prior to python 2. 7
|
|
|
|
pass
|
|
|
|
finally: # ensuring closure of tarfile obj
|
|
|
|
tar.close()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
'''After tests are finished removes things created in setUpClass'''
|
|
|
|
# deleting the temp role directory
|
|
|
|
if os.path.exists(cls.role_dir):
|
|
|
|
shutil.rmtree(cls.role_dir)
|
|
|
|
if os.path.exists(cls.role_req):
|
|
|
|
os.remove(cls.role_req)
|
|
|
|
if os.path.exists(cls.role_tar):
|
|
|
|
os.remove(cls.role_tar)
|
2016-07-12 22:01:47 +02:00
|
|
|
if os.path.isdir(cls.role_path):
|
|
|
|
shutil.rmtree(cls.role_path)
|
2016-07-11 19:54:29 +02:00
|
|
|
|
2016-03-26 08:22:37 +01:00
|
|
|
def setUp(self):
|
|
|
|
self.default_args = []
|
|
|
|
|
|
|
|
def test_init(self):
|
|
|
|
galaxy_cli = GalaxyCLI(args=self.default_args)
|
|
|
|
self.assertTrue(isinstance(galaxy_cli, GalaxyCLI))
|
|
|
|
|
|
|
|
def test_display_min(self):
|
|
|
|
gc = GalaxyCLI(args=self.default_args)
|
|
|
|
role_info = {'name': 'some_role_name'}
|
|
|
|
display_result = gc._display_role_info(role_info)
|
|
|
|
self.assertTrue(display_result.find('some_role_name') >-1)
|
|
|
|
|
|
|
|
def test_display_galaxy_info(self):
|
|
|
|
gc = GalaxyCLI(args=self.default_args)
|
|
|
|
galaxy_info = {}
|
|
|
|
role_info = {'name': 'some_role_name',
|
|
|
|
'galaxy_info': galaxy_info}
|
|
|
|
display_result = gc._display_role_info(role_info)
|
2016-07-21 21:02:19 +02:00
|
|
|
if display_result.find('\n\tgalaxy_info:') == -1:
|
|
|
|
self.fail('Expected galaxy_info to be indented once')
|
2016-07-11 19:54:29 +02:00
|
|
|
|
2016-07-13 20:58:39 +02:00
|
|
|
def test_run(self):
|
|
|
|
''' verifies that the GalaxyCLI object's api is created and that execute() is called. '''
|
|
|
|
gc = GalaxyCLI(args=["install"])
|
|
|
|
with patch('sys.argv', ["-c", "-v", '--ignore-errors', 'imaginary_role']):
|
|
|
|
galaxy_parser = gc.parse()
|
|
|
|
with patch.object(ansible.cli.CLI, "execute", return_value=None) as mock_ex:
|
|
|
|
with patch.object(ansible.cli.CLI, "run", return_value=None) as mock_run:
|
|
|
|
gc.run()
|
2017-01-28 00:45:23 +01:00
|
|
|
|
2016-07-13 20:58:39 +02:00
|
|
|
# testing
|
|
|
|
self.assertEqual(mock_run.call_count, 1)
|
|
|
|
self.assertTrue(isinstance(gc.api, ansible.galaxy.api.GalaxyAPI))
|
|
|
|
self.assertEqual(mock_ex.call_count, 1)
|
|
|
|
|
2016-07-11 19:54:29 +02:00
|
|
|
def test_execute_remove(self):
|
|
|
|
# installing role
|
2016-09-14 20:49:54 +02:00
|
|
|
gc = GalaxyCLI(args=["install", "--offline", "-p", self.role_path, "-r", self.role_req])
|
|
|
|
galaxy_parser = gc.parse()
|
2016-07-11 19:54:29 +02:00
|
|
|
gc.run()
|
2016-07-07 20:25:08 +02:00
|
|
|
|
2016-07-11 19:54:29 +02:00
|
|
|
# checking that installation worked
|
|
|
|
role_file = os.path.join(self.role_path, self.role_name)
|
|
|
|
self.assertTrue(os.path.exists(role_file))
|
|
|
|
|
|
|
|
# removing role
|
2016-09-14 20:49:54 +02:00
|
|
|
gc = GalaxyCLI(args=["remove", "-c", "-p", self.role_path, self.role_name])
|
|
|
|
galaxy_parser = gc.parse()
|
2016-07-11 19:54:29 +02:00
|
|
|
super(GalaxyCLI, gc).run()
|
|
|
|
gc.api = ansible.galaxy.api.GalaxyAPI(gc.galaxy)
|
|
|
|
completed_task = gc.execute_remove()
|
|
|
|
|
|
|
|
# testing role was removed
|
|
|
|
self.assertTrue(completed_task == 0)
|
|
|
|
self.assertTrue(not os.path.exists(role_file))
|
2016-07-07 20:25:08 +02:00
|
|
|
|
|
|
|
def test_exit_without_ignore(self):
|
|
|
|
''' tests that GalaxyCLI exits with the error specified unless the --ignore-errors flag is used '''
|
2016-11-02 15:25:09 +01:00
|
|
|
gc = GalaxyCLI(args=["install", "--server=None", "-c", "fake_role_name"])
|
2016-07-07 20:25:08 +02:00
|
|
|
|
|
|
|
# testing without --ignore-errors flag
|
2016-09-14 20:49:54 +02:00
|
|
|
galaxy_parser = gc.parse()
|
2016-07-07 20:25:08 +02:00
|
|
|
with patch.object(ansible.utils.display.Display, "display", return_value=None) as mocked_display:
|
|
|
|
# testing that error expected is raised
|
|
|
|
self.assertRaises(AnsibleError, gc.run)
|
|
|
|
self.assertTrue(mocked_display.called_once_with("- downloading role 'fake_role_name', owned by "))
|
|
|
|
|
|
|
|
# testing with --ignore-errors flag
|
2016-11-02 15:25:09 +01:00
|
|
|
gc = GalaxyCLI(args=["install", "--server=None", "-c", "fake_role_name", "--ignore-errors"])
|
2016-09-14 20:49:54 +02:00
|
|
|
galalxy_parser = gc.parse()
|
2016-07-07 20:25:08 +02:00
|
|
|
with patch.object(ansible.utils.display.Display, "display", return_value=None) as mocked_display:
|
|
|
|
# testing that error expected is not raised with --ignore-errors flag in use
|
|
|
|
gc.run()
|
|
|
|
self.assertTrue(mocked_display.called_once_with("- downloading role 'fake_role_name', owned by "))
|
2016-07-22 17:45:56 +02:00
|
|
|
|
|
|
|
def run_parse_common(self, galaxycli_obj, action):
|
|
|
|
with patch.object(ansible.cli.SortedOptParser, "set_usage") as mocked_usage:
|
2016-09-29 23:14:02 +02:00
|
|
|
galaxycli_obj.parse()
|
2016-09-14 20:49:54 +02:00
|
|
|
|
|
|
|
# checking that the common results of parse() for all possible actions have been created/called
|
2016-09-29 23:14:02 +02:00
|
|
|
self.assertIsInstance(galaxycli_obj.parser, ansible.cli.SortedOptParser)
|
|
|
|
self.assertIsInstance(galaxycli_obj.galaxy, ansible.galaxy.Galaxy)
|
2016-09-14 20:49:54 +02:00
|
|
|
if action in ['import', 'delete']:
|
|
|
|
formatted_call = 'usage: %prog ' + action + ' [options] github_user github_repo'
|
|
|
|
elif action == 'info':
|
|
|
|
formatted_call = 'usage: %prog ' + action + ' [options] role_name[,version]'
|
|
|
|
elif action == 'init':
|
|
|
|
formatted_call = 'usage: %prog ' + action + ' [options] role_name'
|
|
|
|
elif action == 'install':
|
|
|
|
formatted_call = 'usage: %prog ' + action + ' [options] [-r FILE | role_name(s)[,version] | scm+role_repo_url[,version] | tar_file(s)]'
|
|
|
|
elif action == 'list':
|
|
|
|
formatted_call = 'usage: %prog ' + action + ' [role_name]'
|
|
|
|
elif action == 'login':
|
|
|
|
formatted_call = 'usage: %prog ' + action + ' [options]'
|
|
|
|
elif action == 'remove':
|
|
|
|
formatted_call = 'usage: %prog ' + action + ' role1 role2 ...'
|
|
|
|
elif action == 'search':
|
|
|
|
formatted_call = 'usage: %prog ' + action + ' [searchterm1 searchterm2] [--galaxy-tags galaxy_tag1,galaxy_tag2] [--platforms platform1,platform2] [--author username]'
|
|
|
|
elif action == 'setup':
|
|
|
|
formatted_call = 'usage: %prog ' + action + ' [options] source github_user github_repo secret'
|
|
|
|
calls = [call('usage: %prog [delete|import|info|init|install|list|login|remove|search|setup] [--help] [options] ...'), call(formatted_call)]
|
|
|
|
mocked_usage.assert_has_calls(calls)
|
2016-07-22 17:45:56 +02:00
|
|
|
|
|
|
|
def test_parse(self):
|
|
|
|
''' systematically testing that the expected options parser is created '''
|
|
|
|
# testing no action given
|
2016-09-14 20:49:54 +02:00
|
|
|
gc = GalaxyCLI(args=["-c"])
|
|
|
|
self.assertRaises(AnsibleOptionsError, gc.parse)
|
2016-07-22 17:45:56 +02:00
|
|
|
|
|
|
|
# testing action that doesn't exist
|
2016-09-14 20:49:54 +02:00
|
|
|
gc = GalaxyCLI(args=["NOT_ACTION", "-c"])
|
|
|
|
self.assertRaises(AnsibleOptionsError, gc.parse)
|
2016-07-22 17:45:56 +02:00
|
|
|
|
|
|
|
# testing action 'delete'
|
2016-09-14 20:49:54 +02:00
|
|
|
gc = GalaxyCLI(args=["delete", "-c"])
|
2016-07-22 17:45:56 +02:00
|
|
|
self.run_parse_common(gc, "delete")
|
2016-09-29 23:14:02 +02:00
|
|
|
self.assertEqual(gc.options.verbosity, 0)
|
2016-07-22 17:45:56 +02:00
|
|
|
|
|
|
|
# testing action 'import'
|
2016-09-14 20:49:54 +02:00
|
|
|
gc = GalaxyCLI(args=["import", "-c"])
|
2016-07-22 17:45:56 +02:00
|
|
|
self.run_parse_common(gc, "import")
|
2016-09-29 23:14:02 +02:00
|
|
|
self.assertEqual(gc.options.wait, True)
|
|
|
|
self.assertEqual(gc.options.reference, None)
|
|
|
|
self.assertEqual(gc.options.check_status, False)
|
|
|
|
self.assertEqual(gc.options.verbosity, 0)
|
2016-07-22 17:45:56 +02:00
|
|
|
|
|
|
|
# testing action 'info'
|
2016-09-14 20:49:54 +02:00
|
|
|
gc = GalaxyCLI(args=["info", "-c"])
|
2016-07-22 17:45:56 +02:00
|
|
|
self.run_parse_common(gc, "info")
|
2016-09-29 23:14:02 +02:00
|
|
|
self.assertEqual(gc.options.offline, False)
|
2016-07-22 17:45:56 +02:00
|
|
|
|
|
|
|
# testing action 'init'
|
2016-09-14 20:49:54 +02:00
|
|
|
gc = GalaxyCLI(args=["init", "-c"])
|
2016-07-22 17:45:56 +02:00
|
|
|
self.run_parse_common(gc, "init")
|
2016-09-29 23:14:02 +02:00
|
|
|
self.assertEqual(gc.options.offline, False)
|
|
|
|
self.assertEqual(gc.options.force, False)
|
2016-07-22 17:45:56 +02:00
|
|
|
|
|
|
|
# testing action 'install'
|
2016-09-14 20:49:54 +02:00
|
|
|
gc = GalaxyCLI(args=["install", "-c"])
|
2016-07-22 17:45:56 +02:00
|
|
|
self.run_parse_common(gc, "install")
|
2016-09-29 23:14:02 +02:00
|
|
|
self.assertEqual(gc.options.ignore_errors, False)
|
|
|
|
self.assertEqual(gc.options.no_deps, False)
|
|
|
|
self.assertEqual(gc.options.role_file, None)
|
|
|
|
self.assertEqual(gc.options.force, False)
|
2016-07-22 17:45:56 +02:00
|
|
|
|
|
|
|
# testing action 'list'
|
2016-09-14 20:49:54 +02:00
|
|
|
gc = GalaxyCLI(args=["list", "-c"])
|
2016-07-22 17:45:56 +02:00
|
|
|
self.run_parse_common(gc, "list")
|
2016-09-29 23:14:02 +02:00
|
|
|
self.assertEqual(gc.options.verbosity, 0)
|
2016-07-22 17:45:56 +02:00
|
|
|
|
|
|
|
# testing action 'login'
|
2016-09-14 20:49:54 +02:00
|
|
|
gc = GalaxyCLI(args=["login", "-c"])
|
2016-07-22 17:45:56 +02:00
|
|
|
self.run_parse_common(gc, "login")
|
2016-09-29 23:14:02 +02:00
|
|
|
self.assertEqual(gc.options.verbosity, 0)
|
|
|
|
self.assertEqual(gc.options.token, None)
|
2016-07-22 17:45:56 +02:00
|
|
|
|
|
|
|
# testing action 'remove'
|
2016-09-14 20:49:54 +02:00
|
|
|
gc = GalaxyCLI(args=["remove", "-c"])
|
2016-07-22 17:45:56 +02:00
|
|
|
self.run_parse_common(gc, "remove")
|
2016-09-29 23:14:02 +02:00
|
|
|
self.assertEqual(gc.options.verbosity, 0)
|
2016-07-22 17:45:56 +02:00
|
|
|
|
|
|
|
# testing action 'search'
|
2016-09-14 20:49:54 +02:00
|
|
|
gc = GalaxyCLI(args=["search", "-c"])
|
2016-07-22 17:45:56 +02:00
|
|
|
self.run_parse_common(gc, "search")
|
2016-09-29 23:14:02 +02:00
|
|
|
self.assertEqual(gc.options.platforms, None)
|
|
|
|
self.assertEqual(gc.options.galaxy_tags, None)
|
|
|
|
self.assertEqual(gc.options.author, None)
|
2016-07-22 17:45:56 +02:00
|
|
|
|
|
|
|
# testing action 'setup'
|
2016-09-14 20:49:54 +02:00
|
|
|
gc = GalaxyCLI(args=["setup", "-c"])
|
2016-07-22 17:45:56 +02:00
|
|
|
self.run_parse_common(gc, "setup")
|
2016-09-29 23:14:02 +02:00
|
|
|
self.assertEqual(gc.options.verbosity, 0)
|
|
|
|
self.assertEqual(gc.options.remove_id, None)
|
|
|
|
self.assertEqual(gc.options.setup_list, False)
|