예제 #1
0
    def test_run(self, mock_execute, mock_write_cfg):

        mock_execute.return_value = ('', '')

        action = ansible.AnsiblePlaybookAction(playbook=self.playbook,
                                               limit_hosts=self.limit_hosts,
                                               remote_user=self.remote_user,
                                               become=self.become,
                                               become_user=self.become_user,
                                               extra_vars=self.extra_vars,
                                               verbosity=self.verbosity)
        ansible_config_path = os.path.join(action.work_dir, 'ansible.cfg')
        mock_write_cfg.return_value = ansible_config_path

        action.run(self.ctx)

        pb = os.path.join(action.work_dir, 'playbook.yaml')
        env = {'HOME': action.work_dir, 'ANSIBLE_CONFIG': ansible_config_path}

        mock_execute.assert_called_once_with(
            'ansible-playbook',
            '-v',
            pb,
            '--user',
            self.remote_user,
            '--become',
            '--become-user',
            self.become_user,
            '--extra-vars',
            json.dumps(self.extra_vars),
            env_variables=env,
            cwd=action.work_dir,
            log_errors=processutils.LogErrors.ALL)
예제 #2
0
    def test_run(self, mock_execute, mock_write_cfg):

        mock_execute.return_value = ('', '')

        action = ansible.AnsiblePlaybookAction(
            playbook=self.playbook, limit_hosts=self.limit_hosts,
            remote_user=self.remote_user, become=self.become,
            become_user=self.become_user, extra_vars=self.extra_vars,
            verbosity=self.verbosity)
        ansible_config_path = os.path.join(action.work_dir, 'ansible.cfg')
        mock_write_cfg.return_value = ansible_config_path

        action.run(self.ctx)

        mock_write_cfg.assert_called_once_with(action.work_dir,
                                               self.remote_user,
                                               ssh_private_key=None,
                                               override_ansible_cfg=None)

        pb = os.path.join(action.work_dir, 'playbook.yaml')
        env = {
            'HOME': action.work_dir,
            'ANSIBLE_LOCAL_TEMP': action.work_dir,
            'ANSIBLE_CONFIG': ansible_config_path,
            'ANSIBLE_CALLBACK_WHITELIST': 'profile_tasks',
            'PROFILE_TASKS_TASK_OUTPUT_LIMIT': '0',
        }

        mock_execute.assert_called_once_with(
            'ansible-playbook', '-v', pb, '--become', '--become-user',
            self.become_user, '--extra-vars', json.dumps(self.extra_vars),
            env_variables=env, cwd=action.work_dir,
            log_errors=processutils.LogErrors.ALL)
예제 #3
0
    def run_modify_playbook(modify_role, modify_vars,
                            source_image, target_image, append_tag):
        vars = {}
        if modify_vars:
            vars.update(modify_vars)
        vars['source_image'] = source_image
        vars['target_image'] = target_image
        vars['modified_append_tag'] = append_tag
        LOG.debug('Playbook variables: \n%s' % yaml.safe_dump(
            vars, default_flow_style=False))
        playbook = [{
            'hosts': 'localhost',
            'tasks': [{
                'name': 'Import role %s' % modify_role,
                'import_role': {
                    'name': modify_role
                },
                'vars': vars
            }]
        }]
        LOG.debug('Playbook: \n%s' % yaml.safe_dump(
            playbook, default_flow_style=False))
        work_dir = tempfile.mkdtemp(prefix='tripleo-modify-image-playbook-')
        try:
            action = ansible.AnsiblePlaybookAction(
                playbook=playbook,
                work_dir=work_dir
            )
            result = action.run(None)
            LOG.debug(result.get('stdout', ''))

        finally:
            shutil.rmtree(work_dir)
예제 #4
0
    def test_post_message(self, mock_execute, mock_write_cfg):

        action = ansible.AnsiblePlaybookAction(
            playbook=self.playbook,
            limit_hosts=self.limit_hosts,
            remote_user=self.remote_user,
            become=self.become,
            become_user=self.become_user,
            extra_vars=self.extra_vars,
            verbosity=self.verbosity,
            max_message_size=self.max_message_size)
        ansible_config_path = os.path.join(action.work_dir, 'ansible.cfg')
        mock_write_cfg.return_value = ansible_config_path

        message_size = int(self.max_message_size * 0.9)

        # Message equal to max_message_size
        queue = mock.Mock()
        message = ''.join([
            string.ascii_letters[int(random.random() * 26)]
            for x in range(1024)
        ])
        action.post_message(queue, message)
        self.assertEqual(queue.post.call_count, 2)
        self.assertEqual(
            queue.post.call_args_list[0],
            mock.call(action.format_message(message[:message_size])))
        self.assertEqual(
            queue.post.call_args_list[1],
            mock.call(action.format_message(message[message_size:])))

        # Message less than max_message_size
        queue = mock.Mock()
        message = ''.join([
            string.ascii_letters[int(random.random() * 26)] for x in range(512)
        ])
        action.post_message(queue, message)
        self.assertEqual(queue.post.call_count, 1)
        self.assertEqual(queue.post.call_args_list[0],
                         mock.call(action.format_message(message)))

        # Message double max_message_size
        queue = mock.Mock()
        message = ''.join([
            string.ascii_letters[int(random.random() * 26)]
            for x in range(2048)
        ])
        action.post_message(queue, message)
        self.assertEqual(queue.post.call_count, 3)
        self.assertEqual(
            queue.post.call_args_list[0],
            mock.call(action.format_message(message[:message_size])))
        self.assertEqual(
            queue.post.call_args_list[1],
            mock.call(
                action.format_message(message[message_size:message_size * 2])))
        self.assertEqual(
            queue.post.call_args_list[2],
            mock.call(action.format_message(message[message_size * 2:2048])))
예제 #5
0
    def test_work_dir_cleanup(self, mock_execute, mock_write_cfg, mock_rmtree):

        mock_execute.return_value = ('', '')

        action = ansible.AnsiblePlaybookAction(playbook=self.playbook,
                                               limit_hosts=self.limit_hosts,
                                               remote_user=self.remote_user,
                                               become=self.become,
                                               become_user=self.become_user,
                                               extra_vars=self.extra_vars,
                                               verbosity=0)

        try:
            action.run(self.ctx)
            mock_rmtree.assert_called_once_with(action.work_dir)
        finally:
            # Since we mocked the delete we need to manually cleanup.
            shutil.rmtree(action.work_dir)
예제 #6
0
 def run_modify_playbook(modify_role, modify_vars, source_image,
                         target_image, append_tag):
     vars = {}
     if modify_vars:
         vars.update(modify_vars)
     vars['source_image'] = source_image
     vars['target_image'] = target_image
     vars['modified_append_tag'] = append_tag
     LOG.info('Playbook variables: \n%s' %
              yaml.safe_dump(vars, default_flow_style=False))
     playbook = [{
         'hosts':
         'localhost',
         'tasks': [{
             'name': 'Import role %s' % modify_role,
             'import_role': {
                 'name': modify_role
             },
             'vars': vars
         }]
     }]
     LOG.info('Playbook: \n%s' %
              yaml.safe_dump(playbook, default_flow_style=False))
     work_dir = tempfile.mkdtemp(prefix='tripleo-modify-image-playbook-')
     try:
         action = ansible.AnsiblePlaybookAction(playbook=playbook,
                                                work_dir=work_dir,
                                                verbosity=3)
         result = action.run(None)
         log_path = result.get('log_path')
         if log_path and os.path.isfile(log_path):
             with open(log_path) as f:
                 LOG.info(f.read())
         shutil.rmtree(work_dir)
     except processutils.ProcessExecutionError as e:
         LOG.error('%s\nError running playbook in directory: %s' %
                   (e.stdout, work_dir))
         raise ImageUploaderException('Modifying image %s failed' %
                                      target_image)
예제 #7
0
    def test_work_dir_no_cleanup(self, mock_execute, mock_write_cfg,
                                 mock_rmtree):

        mock_execute.return_value = ('', '')

        # Specity a work_dir, this should not be deleted automatically.
        work_dir = tempfile.mkdtemp()
        try:
            action = ansible.AnsiblePlaybookAction(
                playbook=self.playbook,
                limit_hosts=self.limit_hosts,
                remote_user=self.remote_user,
                become=self.become,
                become_user=self.become_user,
                extra_vars=self.extra_vars,
                verbosity=self.verbosity,
                work_dir=work_dir)

            action.run(self.ctx)

            # verify the rmtree is not called
            mock_rmtree.assert_not_called()
        finally:
            shutil.rmtree(work_dir)