示例#1
0
    def test_process_main(self, _create_package_index, makedirs,
                          mock_get_packages, mock_open):
        """
        Assert all the correct calls from process_main().
        """
        step = steps.PublishMetadataStep()
        conduit = mock.MagicMock()
        mock_get_packages.return_value = _GET_PACKAGES_RETURN
        step.get_conduit = mock.MagicMock(return_value=conduit)
        step.parent = mock.MagicMock()
        step.parent.web_working_dir = '/some/path/'

        step.process_main()

        # Assert correct usage of various mocked items
        step.get_conduit.assert_called_once_with()
        mock_get_packages.assert_called_once_with(conduit.repo_id)
        makedirs.assert_called_once_with(
            os.path.join(step.parent.web_working_dir, 'simple'))
        mock_open.assert_called_once_with(
            os.path.join(step.parent.web_working_dir, 'simple', 'index.html'),
            'w')

        # Assert that the two calls to _create_package_index for each package name are correct
        self.assertEqual(_create_package_index.call_count, 2)
        expected_packages_by_name = steps._get_packages(conduit)
        for call in _create_package_index.mock_calls:
            expected_packages = expected_packages_by_name[call[1][0]]
            self.assertEqual(
                call[1][1], os.path.join(step.parent.web_working_dir,
                                         'simple'))
            self.assertEqual(call[1][2], expected_packages)
            del expected_packages_by_name[call[1][0]]
        self.assertEqual(expected_packages_by_name, {})

        # Assert that the resulting HTML index is correct
        write = mock_open.return_value.__enter__.return_value
        index_html = write.mock_calls[0][1][0]
        html = ElementTree.fromstring(index_html)
        head = html.find('head')
        title = head.find('title')
        self.assertEqual(title.text, 'Simple Index')
        meta = head.find('meta')
        self.assertEqual(meta.get('name'), 'api-version')
        self.assertEqual(meta.get('value'), '2')
        body = html.find('body')
        # There should be four subelements, two anchors and two breaks
        self.assertEqual(len(body.findall('br')), 2)
        self.assertEqual(len(body.findall('a')), 2)
        anchors = body.findall('a')
        self.assertEqual(set([a.get('href') for a in anchors]),
                         set(['nectar', 'pulp_python_plugins']))
        self.assertEqual(set([a.text for a in anchors]),
                         set(['nectar', 'pulp_python_plugins']))
示例#2
0
    def test__get_packages(self, mock_get_querysets):
        """
        Assert the correct return value from _get_packages() with the _GET_UNITS_RETURN data set.
        """
        qs = mock.MagicMock()
        qs.only.return_value = _PACKAGES
        mock_get_querysets.return_value = [qs]

        packages = steps._get_packages('repo1')

        expected_packages = _GET_PACKAGES_RETURN

        self.assertEqual(packages, expected_packages)
示例#3
0
    def test__get_packages(self, mock_get_querysets):
        """
        Assert the correct return value from _get_packages() with the _GET_UNITS_RETURN data set.
        """
        qs = mock.MagicMock()
        qs.only.return_value = _PACKAGES
        mock_get_querysets.return_value = [qs]

        packages = steps._get_packages('repo1')

        expected_packages = _GET_PACKAGES_RETURN

        self.assertEqual(packages, expected_packages)
示例#4
0
    def test_process_main(self, _create_package_index, makedirs, mock_open):
        """
        Assert all the correct calls from process_main().
        """
        step = steps.PublishMetadataStep()
        conduit = mock.MagicMock()
        conduit.get_units.return_value = _GET_UNITS_RETURN
        step.get_conduit = mock.MagicMock(return_value=conduit)
        step.parent = mock.MagicMock()
        step.parent.web_working_dir = '/some/path/'

        step.process_main()

        # Assert correct usage of various mocked items
        step.get_conduit.assert_called_once_with()
        conduit.get_units.assert_called_once_with()
        makedirs.assert_called_once_with(os.path.join(step.parent.web_working_dir, 'simple'))
        mock_open.assert_called_once_with(
            os.path.join(step.parent.web_working_dir, 'simple', 'index.html'), 'w')

        # Assert that the two calls to _create_package_index for each package name are correct
        self.assertEqual(_create_package_index.call_count, 2)
        expected_packages_by_name = steps._get_packages(conduit)
        for call in _create_package_index.mock_calls:
            expected_packages = expected_packages_by_name[call[1][0]]
            self.assertEqual(call[1][1], os.path.join(step.parent.web_working_dir, 'simple'))
            self.assertEqual(call[1][2], expected_packages)
            del expected_packages_by_name[call[1][0]]
        self.assertEqual(expected_packages_by_name, {})

        # Assert that the resulting HTML index is correct
        write = mock_open.return_value.__enter__.return_value
        index_html = write.mock_calls[0][1][0]
        html = ElementTree.fromstring(index_html)
        head = html.find('head')
        title = head.find('title')
        self.assertEqual(title.text, 'Simple Index')
        meta = head.find('meta')
        self.assertEqual(meta.get('name'), 'api-version')
        self.assertEqual(meta.get('value'), '2')
        body = html.find('body')
        # There should be four subelements, two anchors and two breaks
        self.assertEqual(len(body.findall('br')), 2)
        self.assertEqual(len(body.findall('a')), 2)
        anchors = body.findall('a')
        self.assertEqual(set([a.get('href') for a in anchors]),
                         set(['nectar', 'pulp_python_plugins']))
        self.assertEqual(set([a.text for a in anchors]), set(['nectar', 'pulp_python_plugins']))
示例#5
0
    def test__get_packages(self):
        """
        Assert the correct return value from _get_packages() with the _GET_UNITS_RETURN data set.
        """
        conduit = mock.MagicMock()
        conduit.get_units.return_value = _GET_UNITS_RETURN

        packages = steps._get_packages(conduit)

        expected_packages = {
            'pulp_python_plugins': [
                {'checksum': 'klmno', 'checksum_type': 'made_up', 'version': '0.0.0',
                 'storage_path': '/path/to/pulp_python_plugins-0.0.0.tar.gz',
                 'filename': 'pulp_python_plugins-0.0.0.tar.gz'}],
            'nectar': [
                {'checksum': 'abcde', 'checksum_type': 'made_up', 'version': '1.2.0',
                 'storage_path': '/path/to/nectar-1.2.0.tar.gz', 'filename': 'nectar-1.2.0.tar.gz'},
                {'checksum': 'fghij', 'checksum_type': 'made_up', 'version': '1.3.1',
                 'storage_path': '/path/to/nectar-1.3.1.tar.gz',
                 'filename': 'nectar-1.3.1.tar.gz'}]}
        self.assertEqual(packages, expected_packages)
示例#6
0
    def test__get_packages(self):
        """
        Assert the correct return value from _get_packages() with the _GET_UNITS_RETURN data set.
        """
        conduit = mock.MagicMock()
        conduit.get_units.return_value = _GET_UNITS_RETURN

        packages = steps._get_packages(conduit)

        expected_packages = {
            'pulp_python_plugins': [{
                'checksum':
                'klmno',
                'checksum_type':
                'made_up',
                'version':
                '0.0.0',
                'storage_path':
                '/path/to/pulp_python_plugins-0.0.0.tar.gz',
                'filename':
                'pulp_python_plugins-0.0.0.tar.gz'
            }],
            'nectar': [{
                'checksum': 'abcde',
                'checksum_type': 'made_up',
                'version': '1.2.0',
                'storage_path': '/path/to/nectar-1.2.0.tar.gz',
                'filename': 'nectar-1.2.0.tar.gz'
            }, {
                'checksum': 'fghij',
                'checksum_type': 'made_up',
                'version': '1.3.1',
                'storage_path': '/path/to/nectar-1.3.1.tar.gz',
                'filename': 'nectar-1.3.1.tar.gz'
            }]
        }
        self.assertEqual(packages, expected_packages)