Esempio n. 1
0
 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)
     if display_result.find('\n\tgalaxy_info:') == -1:
         self.fail('Expected galaxy_info to be indented once')
Esempio n. 2
0
 def test_raise_without_ignore_without_flag(self):
     ''' tests that GalaxyCLI exits with the error specified if the --ignore-errors flag is not used '''
     gc = GalaxyCLI(args=self.default_args +
                    ["install", "--server=None", "testing.fake_role_name"])
     gc.parse()
     # testing that error expected is raised
     self.assertRaises(exceptions.GalaxyError, gc.run)
Esempio n. 3
0
 def test_exit_without_ignore_without_flag(self):
     ''' tests that GalaxyCLI exits with the error specified if the --ignore-errors flag is not used '''
     gc = GalaxyCLI(args=[
         "ansible-galaxy", "install", "--server=None", "fake_role_name"
     ])
     gc.parse()
     # testing that error expected is raised
     self.assertRaises(cli_exceptions.GalaxyCliError, gc.run)
Esempio n. 4
0
 def test_parse_search(self):
     ''' testing the options parswer when the action 'search' is given '''
     gc = GalaxyCLI(args=["ansible-galaxy", "search"])
     self.run_parse_common(gc, "search")
     self.assertEqual(gc.options.platforms, None)
     self.assertEqual(gc.options.galaxy_tags, None)
     self.assertEqual(gc.options.author, None)
Esempio n. 5
0
 def test_parse_install(self):
     ''' testing the options parser when the action 'install' is given '''
     gc = GalaxyCLI(args=self.default_args + ["install"])
     self.run_parse_common(gc, "install")
     self.assertEqual(gc.options.ignore_errors, False)
     self.assertEqual(gc.options.no_deps, False)
     self.assertEqual(gc.options.force, False)
Esempio n. 6
0
 def test_parse_import(self):
     ''' testing the options parser when the action 'import' is given '''
     gc = GalaxyCLI(args=["ansible-galaxy", "import"])
     self.run_parse_common(gc, "import")
     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)
Esempio n. 7
0
    def test_parse_setup(self):
        ''' testing the options parser when the action 'setup' is given '''
        gc = GalaxyCLI(args=["ansible-galaxy", "setup"])
        self.run_parse_common(gc, "setup")

        self.assertEqual(gc.options.verbosity, 0)
        self.assertEqual(gc.options.remove_id, None)
        self.assertEqual(gc.options.setup_list, False)
Esempio n. 8
0
 def test_parse_install(self):
     ''' testing the options parser when the action 'install' is given '''
     gc = GalaxyCLI(args=["ansible-galaxy", "install"])
     self.run_parse_common(gc, "install")
     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)
Esempio n. 9
0
    def setUpRole(cls, role_name, skeleton_path, galaxy_args=None):
        if galaxy_args is None:
            galaxy_args = []

        log.debug('skeleton_path arg: %s', skeleton_path)

        # FIXME: mo
        cls.role_skeleton_path = skeleton_path
        if '--role-skeleton' not in galaxy_args:
            galaxy_args += ['--role-skeleton', skeleton_path]
            log.debug('role_skeleton_path: %s', cls.role_skeleton_path)

        # Make temp directory for testing
        cls.test_dir = tempfile.mkdtemp()
        if not os.path.isdir(cls.test_dir):
            os.makedirs(cls.test_dir)
        log.debug('test_dir: %s', cls.test_dir)

        cls.role_dir = os.path.join(cls.test_dir, role_name)
        cls.role_name = role_name
        log.debug('role_dir: %s', cls.role_dir)
        log.debug('role_name: %s', cls.role_name)

        # create role using default skeleton
        gc_args = [
            'ansible-galaxy', 'init', '-c', '--offline'
        ] + galaxy_args + ['--init-path', cls.test_dir, cls.role_name]
        log.debug('gc_args: %s', gc_args)
        gc = GalaxyCLI(args=gc_args)
        gc.parse()
        gc.run()
        cls.gc = gc

        if skeleton_path is None:
            cls.role_skeleton_path = gc.galaxy.default_role_skeleton_path
Esempio n. 10
0
    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

        if os.path.exists("./delete_me"):
            shutil.rmtree("./delete_me")

        # creating framework for a role
        gc = GalaxyCLI(
            args=["ansible-galaxy", "init", "--offline", "delete_me"])
        gc.parse()
        gc.run()
        cls.role_dir = "./delete_me"
        cls.role_name = "delete_me"

        # add a meta/main.yml
        os.makedirs(os.path.join(cls.role_dir, 'meta'))
        fd = open(os.path.join(cls.role_dir, 'meta/main.yml'), 'w')
        fd.write('galaxy_info: {}\ndependencies: {}')
        fd.close()
        # 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)

        # 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()
Esempio n. 11
0
 def test_exit_without_ignore_with_flag(self):
     ''' tests that GalaxyCLI exits without the error specified if the --ignore-errors flag is used  '''
     # testing with --ignore-errors flag
     gc = GalaxyCLI(args=[
         "ansible-galaxy", "install", "--server=None", "fake_role_name",
         "--ignore-errors"
     ])
     gc.parse()
     gc.run()
Esempio n. 12
0
 def test_raise_without_ignore_with_flag(self):
     ''' tests that GalaxyCLI exits without the error specified if the --ignore-errors flag is used  '''
     # testing with --ignore-errors flag
     gc = GalaxyCLI(args=self.default_args + [
         "install", "--server=None", "testing.fake_role_name",
         "--ignore-errors"
     ])
     gc.parse()
     gc.run()
Esempio n. 13
0
    def test_run(self):
        ''' verifies that the GalaxyCLI object's api is created and that execute() is called. '''
        gc = GalaxyCLI(args=["ansible-galaxy", "install", "--ignore-errors", "imaginary_role"])
        gc.parse()
        with patch.object(ansible_galaxy_cli.cli.CLI, "execute", return_value=None) as mock_ex:
            with patch.object(ansible_galaxy_cli.cli.CLI, "run", return_value=None) as mock_run:
                gc.run()

                # testing
                self.assertEqual(mock_run.call_count, 1)
                self.assertTrue(isinstance(gc.api, ansible_galaxy.rest_api.GalaxyAPI))
                self.assertEqual(mock_ex.call_count, 1)
Esempio n. 14
0
    def test_run(self):
        ''' verifies that the GalaxyCLI object's api is created and that execute() is called. '''
        # gc = GalaxyCLI(args=self.default_args + ["install", "--ignore-errors", "imaginary_role"])
        gc = GalaxyCLI(args=["mazer", "install", "--ignore-errors", "imaginary_role"])
        gc.parse()
        with patch.object(ansible_galaxy_cli.cli.CLI, "execute", return_value=None) as mock_ex:
            with patch.object(ansible_galaxy_cli.cli.CLI, "run", return_value=None) as mock_run:
                gc.run()

                # testing
                self.assertEqual(mock_run.call_count, 1)
                self.assertEqual(mock_ex.call_count, 1)
Esempio n. 15
0
def test_execute_remove(galaxy_context, mocker):

    mock_remove = mocker.patch(
        'ansible_galaxy.actions.remove.repository.remove',
        return_value='REMOVE_RESULT?',
    )

    # Remove the default user collection testing.some_collection, except repository.remove is defused
    args = ["mazer", "remove", 'testing.some_collection']
    log.debug('args: %s', args)

    gc = GalaxyCLI(args=args)
    gc.parse()
    gc.run()

    remove_args, dummy = mock_remove.call_args_list[0]
    first_arg = remove_args[0]

    assert first_arg.repository_spec.namespace == 'testing'
    assert first_arg.repository_spec.name == 'some_collection'
Esempio n. 16
0
    def setUpRole(cls, role_name, skeleton_path, galaxy_args=None):
        if galaxy_args is None:
            galaxy_args = []

        log.debug('skeleton_path arg: %s', skeleton_path)

        # TODO: mock out role_skeleton path instead of always testing
        #       with --role-skeleton path to avoid issues like
        #       https://github.com/ansible/galaxy-cli/issues/20
        cls.role_skeleton_path = skeleton_path
        if '--role-skeleton' not in galaxy_args:
            galaxy_args += ['--role-skeleton', skeleton_path]
            log.debug('role_skeleton_path: %s', cls.role_skeleton_path)

        # Make temp directory for testing
        cls.test_dir = tempfile.mkdtemp()
        ensure_dir(cls.test_dir)
        log.debug('test_dir: %s', cls.test_dir)

        cls.role_dir = os.path.join(cls.test_dir, role_name)
        cls.role_name = role_name
        log.debug('role_dir: %s', cls.role_dir)
        log.debug('role_name: %s', cls.role_name)

        # create role using default skeleton
        gc_args = [
            'ansible-galaxy', 'init', '-c', '--offline'
        ] + galaxy_args + ['--init-path', cls.test_dir, cls.role_name]
        log.debug('gc_args: %s', gc_args)
        gc = GalaxyCLI(args=gc_args)
        gc.parse()
        gc.run()
        cls.gc = gc

        if skeleton_path is None:
            cls.role_skeleton_path = gc.galaxy.default_role_skeleton_path
Esempio n. 17
0
 def test_parse_version(self):
     ''' testing the options parser when the action 'version' is given '''
     gc = GalaxyCLI(args=["ansible-galaxy", "version"])
     self.run_parse_common(gc, "version")
     self.assertEqual(gc.options.verbosity, 0)
Esempio n. 18
0
 def test_parse_info(self):
     ''' testing the options parser when the action 'info' is given '''
     gc = GalaxyCLI(args=["ansible-galaxy", "info"])
     self.run_parse_common(gc, "info")
     self.assertEqual(gc.options.offline, False)
Esempio n. 19
0
 def test_parse_invalid_action(self):
     ''' testing the options parser when an invalid action is given '''
     gc = GalaxyCLI(args=["ansible-galaxy", "NOT_ACTION"])
     self.assertRaises(cli_exceptions.CliOptionsError, gc.parse)
Esempio n. 20
0
 def test_parse_no_action(self):
     ''' testing the options parser when no action is given '''
     gc = GalaxyCLI(args=["ansible-galaxy", ""])
     self.assertRaises(cli_exceptions.CliOptionsError, gc.parse)
Esempio n. 21
0
    def test_execute_remove(self):
        # installing role
        log.debug('self.role_path: %s', self.role_path)
        log.debug('self.role_req: %s', self.role_req)

        gc = GalaxyCLI(args=["ansible-galaxy", "install", "--content-path", self.role_path, "-r", self.role_req, '--force'])
        gc.parse()
        gc.run()

        log.debug('self.role_name: %s', self.role_name)
        # location where the role was installed
        role_file = os.path.join(self.role_path, self.role_name)

        log.debug('role_file: %s', role_file)

        # removing role
        # args = ["ansible-galaxy", "remove", role_file, self.role_name]
        args = ["ansible-galaxy", "remove", self.role_name]
        log.debug('args: %s', args)
        gc = GalaxyCLI(args=args)
        gc.parse()
        gc.run()

        # testing role was removed
        removed_role = not os.path.exists(role_file)
        self.assertTrue(removed_role)
Esempio n. 22
0
 def test_parse_publish(self):
     ''' testing the options parser when the action 'publish' is given '''
     gc = GalaxyCLI(args=self.default_args + ["publish"])
     self.run_parse_common(gc, "publish")
     self.assertEqual(gc.options.verbosity, 0)
Esempio n. 23
0
 def test_parse_remove(self):
     ''' testing the options parser when the action 'remove' is given '''
     gc = GalaxyCLI(args=self.default_args + ["remove"])
     self.run_parse_common(gc, "remove")
     self.assertEqual(gc.options.verbosity, 0)
Esempio n. 24
0
 def test_init(self):
     galaxy_cli = GalaxyCLI(args=self.default_args)
     self.assertTrue(isinstance(galaxy_cli, GalaxyCLI))
Esempio n. 25
0
 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)
Esempio n. 26
0
    def test_execute_remove(self):
        # installing role
        gc = GalaxyCLI(args=[
            "ansible-galaxy", "install", "-p", self.role_path, "-r",
            self.role_req, '--force'
        ])
        gc.parse()
        gc.run()

        # location where the role was installed
        role_file = os.path.join(self.role_path, self.role_name)

        # removing role
        gc = GalaxyCLI(
            args=["ansible-galaxy", "remove", role_file, self.role_name])
        gc.parse()
        gc.run()

        # testing role was removed
        removed_role = not os.path.exists(role_file)
        self.assertTrue(removed_role)