def test_process_main(self, symlink, exists, makedirs, mock_get_packages): """ Assert correct operation from the process_main() method with our _GET_UNITS_RETURN data. """ _seen_paths = [] def mock_exists(path): """ This mocks the return value of exists to return False the first time a path is given to it, and True every time thereafter for that same path. """ if path not in _seen_paths: _seen_paths.append(path) return False return True exists.side_effect = mock_exists step = steps.PublishContentStep() mock_get_packages.return_value = _GET_PACKAGES_RETURN conduit = mock.MagicMock() step.get_conduit = mock.MagicMock(return_value=conduit) step.parent = mock.MagicMock() step.parent.web_working_dir = '/some/path/' step.process_main() step.get_conduit.assert_called_once_with() mock_get_packages.assert_called_once_with(conduit.repo_id) # os.path.exists should have been called once for each Unit. It also gets called for a lot # of locale stuff, so we'll need to filter those out. pulp_exists_calls = [ c for c in exists.mock_calls if 'locale' not in c[1][0] ] self.assertEqual(len(pulp_exists_calls), 3) expected_symlink_args = [(u.storage_path, steps._get_package_path(u.name, u._filename)) for u in _PACKAGES] expected_symlink_args = [(a[0], os.path.join(step.parent.web_working_dir, a[1])) for a in expected_symlink_args] expected_exists_call_args = [(os.path.dirname(a[1]), ) for a in expected_symlink_args] actual_exists_call_args = [c[1] for c in pulp_exists_calls] self.assertEqual(set(actual_exists_call_args), set(expected_exists_call_args)) # os.makedirs should only have been called twice, since there are two versions of Nectar and # they share a directory. This is also going to be the same set as the exists set. self.assertEqual(makedirs.call_count, 2) makedirs_call_args = [c[1] for c in makedirs.mock_calls] self.assertEqual(set(makedirs_call_args), set(expected_exists_call_args)) # Lastly, three calls to symlink should have been made, one for each Unit. self.assertEqual(symlink.call_count, 3) actual_mock_call_args = [c[1] for c in symlink.mock_calls] self.assertEqual(set(actual_mock_call_args), set(expected_symlink_args))
def test__get_package_path(self): """ Assert the correct return value from _get_package_path(). """ name = 'test_package' filename = 'test_package-1.2.3.tar.gz' path = steps._get_package_path(name, filename) self.assertEqual(path, os.path.join('packages', 'source', 't', name, filename))
def test__get_package_path(self): """ Assert the correct return value from _get_package_path(). """ name = 'test_package' filename = 'test_package-1.2.3.tar.gz' path = steps._get_package_path(name, filename) self.assertEqual( path, os.path.join('packages', 'source', 't', name, filename))
def test__create_package_index(self, makedirs, mock_open): """ Assert all the correct calls from _create_package_index(). """ step = steps.PublishMetadataStep() name = 'test_package' simple_path = os.path.join('/', 'path', 'to', 'simple') packages = [ { 'version': '2.4.3', 'filename': 'test_package-2.4.3.tar.gz', 'checksum': 'sum', 'checksum_type': 'barlow' }, { 'version': '2.5.0', 'filename': 'test_package-2.5.0.tar.gz', 'checksum': 'different', 'checksum_type': 'barlow' }, ] step._create_package_index(name, simple_path, packages) # Assert the right files and directories are made makedirs.assert_called_once_with(os.path.join(simple_path, name)) mock_open.assert_called_once_with( os.path.join(simple_path, name, 'index.html'), 'w') # 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, 'Links for %s' % name) 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') hrefs = [ os.path.join('..', '..', steps._get_package_path(name, p['filename'])) + '#%s=%s' % (p['checksum_type'], p['checksum']) for p in packages ] self.assertEqual(set([a.get('href') for a in anchors]), set(hrefs)) self.assertEqual(set([a.text for a in anchors]), set([p['filename'] for p in packages]))
def test_process_main(self, symlink, exists, makedirs): """ Assert correct operation from the process_main() method with our _GET_UNITS_RETURN data. """ _seen_paths = [] def mock_exists(path): """ This mocks the return value of exists to return False the first time a path is given to it, and True every time thereafter for that same path. """ if path not in _seen_paths: _seen_paths.append(path) return False return True exists.side_effect = mock_exists step = steps.PublishContentStep() 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() step.get_conduit.assert_called_once_with() conduit.get_units.assert_called_once_with() # os.path.exists should have been called once for each Unit. It also gets called for a lot # of locale stuff, so we'll need to filter those out. pulp_exists_calls = [c for c in exists.mock_calls if 'locale' not in c[1][0]] self.assertEqual(len(pulp_exists_calls), 3) expected_symlink_args = [ (u.storage_path, steps._get_package_path(u.unit_key['name'], u.metadata['_filename'])) for u in _GET_UNITS_RETURN] expected_symlink_args = [(a[0], os.path.join(step.parent.web_working_dir, a[1])) for a in expected_symlink_args] expected_exists_call_args = [(os.path.dirname(a[1]),) for a in expected_symlink_args] actual_exists_call_args = [c[1] for c in pulp_exists_calls] self.assertEqual(set(actual_exists_call_args), set(expected_exists_call_args)) # os.makedirs should only have been called twice, since there are two versions of Nectar and # they share a directory. This is also going to be the same set as the exists set. self.assertEqual(makedirs.call_count, 2) makedirs_call_args = [c[1] for c in makedirs.mock_calls] self.assertEqual(set(makedirs_call_args), set(expected_exists_call_args)) # Lastly, three calls to symlink should have been made, one for each Unit. self.assertEqual(symlink.call_count, 3) actual_mock_call_args = [c[1] for c in symlink.mock_calls] self.assertEqual(set(actual_mock_call_args), set(expected_symlink_args))
def test__create_package_index(self, makedirs, mock_open): """ Assert all the correct calls from _create_package_index(). """ step = steps.PublishMetadataStep() name = 'test_package' simple_path = os.path.join('/', 'path', 'to', 'simple') packages = [ {'version': '2.4.3', 'filename': 'test_package-2.4.3.tar.gz', 'checksum': 'sum', 'checksum_type': 'barlow'}, {'version': '2.5.0', 'filename': 'test_package-2.5.0.tar.gz', 'checksum': 'different', 'checksum_type': 'barlow'}, ] step._create_package_index(name, simple_path, packages) # Assert the right files and directories are made makedirs.assert_called_once_with(os.path.join(simple_path, name)) mock_open.assert_called_once_with( os.path.join(simple_path, name, 'index.html'), 'w') # 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, 'Links for %s' % name) 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') hrefs = [ os.path.join('..', '..', steps._get_package_path(name, p['filename'])) + '#%s=%s' % (p['checksum_type'], p['checksum']) for p in packages] self.assertEqual(set([a.get('href') for a in anchors]), set(hrefs)) self.assertEqual(set([a.text for a in anchors]), set([p['filename'] for p in packages]))