Ejemplo n.º 1
0
    def test_import_modules_failed(self, *mocks):
        """
        Test that when there was some failure in a previous step, _import_modules does not
        overwrite the failed state
        """
        mock_conduit = Mock()
        mock_conduit.get_units.return_value = []

        method = SynchronizeWithDirectory(mock_conduit, Mock())
        method.started_fetch_modules = 0
        metadata = {'name': 'j-p', 'author': 'J', 'version': '1.1'}
        method._extract_metadata = Mock(return_value=metadata)
        method.report = Mock()
        method.report.modules_total_count = 1
        method.report.modules_finished_count = 0
        method.report.modules_state = constants.STATE_FAILED

        # test
        method._import_modules(['/path1'])

        # validation
        self.assertEquals(constants.STATE_FAILED, method.report.modules_state)
        self.assertEquals(2, method.report.update_progress.call_count)
        self.assertEquals(1, method.report.modules_total_count)
        self.assertEquals(1, method.report.modules_finished_count)
Ejemplo n.º 2
0
    def test_import_modules_failed(self):
        """
        Test that when there was some failure in a previous step, _import_modules does not
        overwrite the failed state
        """
        config = {}
        mock_conduit = Mock()

        mock_inventory = Mock()
        method = SynchronizeWithDirectory(mock_conduit, config)
        method.started_fetch_modules = 0
        method._extract_metadata = Mock(return_value={'name': 'j-p', 'author': 'J', 'version': '1.1'})
        method.report = Mock()
        method.report.modules_total_count = 1
        method.report.modules_finished_count = 0
        method.report.modules_state = constants.STATE_FAILED

        # test
        imported_modules = method._import_modules(mock_inventory, ['/path1'])

        # validation
        self.assertEquals(constants.STATE_FAILED, method.report.modules_state)
        self.assertEquals(1, method.report.update_progress.call_count)
        self.assertEquals(0, method.report.modules_total_count)
        self.assertEquals(0, method.report.modules_finished_count)
        self.assertEquals([], imported_modules)
Ejemplo n.º 3
0
    def test_extract_metadata(self, *mocks):
        mock_mkdtemp, mock_tarfile, mock_json, mock_shutil, mock_open = mocks
        Member = namedtuple('Member', ['name'])
        module_path = '/build/modules/puppet-module.tar.gz'
        mock_mkdtemp.return_value = '/tmp/xx'
        members = [
            Member(name='puppet-module'),
            Member(name='puppet-module/manifests'),
            Member(name='puppet-module/spec'),
            Member(name='puppet-module/templates'),
            Member(name='puppet-module/tests'),
            Member(name='puppet-module/CHANGELOG'),
            Member(name='puppet-module/%s' %
                   constants.MODULE_METADATA_FILENAME),
            Member(name='puppet-module/README'),
        ]
        tarball = Mock()
        tarball.getmembers = Mock(return_value=members)
        mock_tarfile.open = Mock(return_value=tarball)

        mock_fp = Mock()
        mock_fp.__enter__ = Mock(return_value=mock_fp)
        mock_fp.__exit__ = Mock()
        mock_open.return_value = mock_fp
        mock_json.load.return_value = '12345'

        # test

        SynchronizeWithDirectory._extract_metadata(module_path)

        # validation

        mock_mkdtemp.assert_called_with(dir=os.path.dirname(module_path))
        mock_tarfile.open.assert_called_with(module_path)
        tarball.getmembers.assert_called_with()
        tarball.extract.assert_called_with(members[6], mock_mkdtemp())
        mock_open.assert_called_with(
            os.path.join(mock_mkdtemp(), members[6].name))
        mock_json.load.assert_called_with(mock_fp)
        mock_shutil.rmtree.assert_called_with(mock_mkdtemp())
Ejemplo n.º 4
0
    def test_extract_metadata(self, *mocks):
        mock_mkdtemp, mock_tarfile, mock_json, mock_shutil, mock_open = mocks
        Member = namedtuple('Member', ['name'])
        module_path = '/build/modules/puppet-module.tar.gz'
        mock_mkdtemp.return_value = '/tmp/xx'
        members = [
            Member(name='puppet-module'),
            Member(name='puppet-module/manifests'),
            Member(name='puppet-module/spec'),
            Member(name='puppet-module/templates'),
            Member(name='puppet-module/tests'),
            Member(name='puppet-module/CHANGELOG'),
            Member(name='puppet-module/%s' % constants.MODULE_METADATA_FILENAME),
            Member(name='puppet-module/README'),
        ]
        tarball = Mock()
        tarball.getmembers = Mock(return_value=members)
        mock_tarfile.open = Mock(return_value=tarball)

        mock_fp = Mock()
        mock_fp.__enter__ = Mock(return_value=mock_fp)
        mock_fp.__exit__ = Mock()
        mock_open.return_value = mock_fp
        mock_json.load.return_value = '12345'

        # test

        SynchronizeWithDirectory._extract_metadata(module_path)

        # validation

        mock_mkdtemp.assert_called_with(dir=os.path.dirname(module_path))
        mock_tarfile.open.assert_called_with(module_path)
        tarball.getmembers.assert_called_with()
        tarball.extract.assert_called_with(members[6], mock_mkdtemp())
        mock_open.assert_called_with(os.path.join(mock_mkdtemp(), members[6].name))
        mock_json.load.assert_called_with(mock_fp)
        mock_shutil.rmtree.assert_called_with(mock_mkdtemp())