def test_hashable(self): self.check_hashable( generator.gen_repository(name="test1", url="file:///repo"), generator.gen_repository(name="test1", url="file:///repo", section=("a", "b")), )
def test_copy_minimal_subset_of_repository(self): controller = CallbacksAdapter() repo1 = generator.gen_repository(name="repo1") repo2 = generator.gen_repository(name="repo2") repo3 = generator.gen_repository(name="repo3") mirror1 = generator.gen_repository(name="mirror1") mirror2 = generator.gen_repository(name="mirror2") pkg_group1 = [ generator.gen_package( idx=1, requires=None, repository=repo1 ), generator.gen_package( idx=1, version=2, requires=None, repository=repo1 ), generator.gen_package( idx=2, requires=None, repository=repo1 ) ] pkg_group2 = [ generator.gen_package( idx=4, requires=[generator.gen_relation("package1")], repository=repo2, mandatory=True, ) ] pkg_group3 = [ generator.gen_package( idx=3, requires=None, repository=repo1 ) ] controller.load_repositories.side_effect = [[repo1, repo2], repo3] controller.load_packages.side_effect = [ pkg_group1 + pkg_group2 + pkg_group3, generator.gen_package( idx=6, repository=repo3, requires=[generator.gen_relation("package2")] ) ] controller.clone_repositories.return_value = { repo1: mirror1, repo2: mirror2 } controller.copy_packages.return_value = 1 api = RepositoryApi(controller) api.clone_repositories( ["file:///repo1", "file:///repo2"], "/mirror", ["file:///repo3"], keep_existing=True ) controller.copy_packages.assert_any_call( mirror1, set(pkg_group1), True ) controller.copy_packages.assert_any_call( mirror2, set(pkg_group2), True ) self.assertEqual(2, controller.copy_packages.call_count)
def test_str(self): self.assertEqual("/a/b/", str(generator.gen_repository(name="a", url="/a/b/"))) self.assertEqual( "/a/b/ c", str(generator.gen_repository(name="a", url="/a/b/", section="c"))) self.assertEqual( "/a/b/ c d", str( generator.gen_repository(name="a", url="/a/b/", section=("c", "d"))))
def test_str(self): self.assertEqual( "a.b", str(generator.gen_repository(name=("a", "b"))) ) self.assertEqual( "/a/b/", str(generator.gen_repository(name="", url="/a/b/")) ) self.assertEqual( "a", str(generator.gen_repository(name="a", url="/a/b/")) )
def test_str(self): self.assertEqual( "/a/b/", str(generator.gen_repository(name="a", url="/a/b/")) ) self.assertEqual( "/a/b/ c", str(generator.gen_repository(name="a", url="/a/b/", section="c")) ) self.assertEqual( "/a/b/ c d", str(generator.gen_repository( name="a", url="/a/b/", section=("c", "d"))) )
def test_clone_repository(self, os): os.path.abspath.return_value = "/root/repo" repos = [ gen_repository(name="test1"), gen_repository(name="test2") ] clones = [copy.copy(x) for x in repos] self.driver.fork_repository.side_effect = clones mirrors = self.ctrl.clone_repositories(repos, "./repo") for r in repos: self.driver.fork_repository.assert_any_call( self.context.connection, r, "/root/repo", False, False ) self.assertEqual(mirrors, dict(zip(repos, clones)))
def test_fork_repository_to_existing_destination( self, ensure_dir_exists_mock, open_mock, os_mock ): self.configure_streams() tmp_file = mock.MagicMock() tmp_file.name = "/tmp/groups.gz" open_mock.return_value.__enter__.return_value = tmp_file repo = gen_repository("os", url="http://localhost/os/x86_64/") md_gen = mock.MagicMock() self.createrepo.MetaDataGenerator.return_value = md_gen md_gen.doFinalMove.side_effect = [None] md_gen.outputdir = "/repo/os/x86_64" md_gen.tempdir = "tmp" md_gen.finaldir = "repodata" md_config = mock.MagicMock() self.createrepo.MetaDataConfig.return_value = md_config os_mock.path.join.side_effect = [ "/repo/os/x86_64/repodata/repomd.xml", tmp_file.name, "/repo/os/x86_64/tmp" ] os_mock.path.exists.return_value = True groups_xml = mock.MagicMock() with mock.patch.object(self.driver, "_load_groups") as load_mock: load_mock.side_effect = [None, groups_xml] self.driver.fork_repository( self.connection, repo, "/repo/os/x86_64", None ) self.assertEqual(tmp_file.name, md_config.groupfile) os_mock.unlink.assert_called_once_with(tmp_file.name) groups_xml.write.assert_called_once_with(tmp_file)
def test_assign_packages(self, os): repo = gen_repository(url="/test/repo") packages = [ gen_package(name="test1", repository=repo), gen_package(name="test2", repository=repo) ] existed_packages = [ gen_package(name="test3", repository=repo), gen_package(name="test2", repository=repo) ] os.path.join = lambda *x: "/".join(x) self.driver.get_packages = CallbacksAdapter() self.driver.get_packages.return_value = existed_packages self.ctrl.assign_packages(repo, packages, True) os.remove.assert_not_called() all_packages = set(packages + existed_packages) self.driver.rebuild_repository.assert_called_once_with( repo, all_packages ) self.driver.rebuild_repository.reset_mock() self.ctrl.assign_packages(repo, packages, False) self.driver.rebuild_repository.assert_called_once_with( repo, set(packages) ) os.remove.assert_called_once_with("/test/repo/test3.pkg")
def test_create_repository(self): repository_data = { "name": "Test", "uri": "file:///repo/", "section": ("trusty", "main"), "origin": "Test" } repo = gen_repository(**repository_data) packages_list = ['/tmp/test1.pkg'] packages = [gen_package(name="test2", repository=repo)] self.driver.create_repository.return_value = repo self.driver.load_package_from_file.side_effect = packages self.driver.get_relative_path.side_effect = ["pool/t/test1.pkg"] self.ctrl.create_repository(repository_data, packages_list) self.driver.create_repository.assert_called_once_with( repository_data, self.ctrl.arch ) self.driver.get_relative_path.assert_called_once_with( repo, "test1.pkg" ) self.context.connection.retrieve.assert_any_call( "/tmp/test1.pkg", "/repo/pool/t/test1.pkg" ) self.driver.load_package_from_file.assert_called_once_with( repo, "pool/t/test1.pkg" ) self.driver.add_packages.assert_called_once_with( self.ctrl.context.connection, repo, set(packages) )
def test_copy_packages(self): repo = gen_repository(url="file:///repo/") packages = [ gen_package(name="test1", repository=repo, filesize=10), gen_package(name="test2", repository=repo, filesize=-1) ] target = gen_repository(url="/test/repo/") self.context.connection.retrieve.side_effect = [0, 10] observer = mock.MagicMock() self.ctrl._copy_packages(target, packages, observer) observer.assert_any_call(0) observer.assert_any_call(10) self.context.connection.retrieve.assert_any_call( "file:///repo/test1.pkg", "/test/repo/test1.pkg", size=10) self.context.connection.retrieve.assert_any_call( "file:///repo/test2.pkg", "/test/repo/test2.pkg", size=-1)
def test_add_packages_clean_metadata_on_error( self, open_mock, shutil_mock, os_mock ): self.configure_streams() tmp_file = mock.MagicMock() tmp_file.name = "/tmp/groups.gz" open_mock.return_value.__enter__.return_value = tmp_file self.createrepo.MDError = ValueError md_gen = mock.MagicMock() self.createrepo.MetaDataGenerator.return_value = md_gen md_gen.doFinalMove.side_effect = self.createrepo.MDError() repo = gen_repository("test", url="file:///repo/os/x86_64") self.createrepo.MetaDataConfig().outputdir = "/repo/os/x86_64" self.createrepo.MetaDataConfig().tempdir = "tmp" self.createrepo.MetaDataConfig().finaldir = "repodata" os_mock.path.exists.return_value = True os_mock.path.join.side_effect = [ "/repo/os/x86_64/tmp", tmp_file.name, "/repo/os/x86_64/tmp" ] with self.assertRaises(RuntimeError): self.driver.add_packages(self.connection, repo, set()) shutil_mock.rmtree.assert_called_once_with( "/repo/os/x86_64/tmp", ignore_errors=True ) os_mock.unlink.assert_called_once_with(tmp_file.name)
def test_load_package_from_file(self, debfile, os, open, utils): fake_repo = gen_repository( name=("trusty", "main"), url="file:///repo" ) file_info = ("/test.rpm", 2, [3, 4, 5]) utils.get_size_and_checksum_for_files.return_value = [file_info] debfile.DebFile().control.get_content.return_value = { "package": "Test", "version": "2.7.9-1", "depends": "test1 (>= 2.2.1)", "replaces": "test2 (<< 2.2.1)", "recommends": "test3 (>> 2.2.1)", "provides": "test4 (>> 2.2.1)" } pkg = self.driver.load_package_from_file( fake_repo, "pool/main/t/test.deb" ) self.assertEqual("Test", pkg.name) self.assertEqual("2.7.9-1", pkg.version) self.assertEqual("pool/main/t/test.deb", pkg.filename) self.assertEqual((3, 4, 5), pkg.checksum) self.assertEqual(2, pkg.filesize) self.assertItemsEqual( ['test1 (>= 2.2.1)', 'test3 (> 2.2.1)'], (str(x) for x in pkg.requires) ) self.assertItemsEqual( ['test4 (> 2.2.1)'], (str(x) for x in pkg.provides) ) self.assertEqual([], pkg.obsoletes) self.assertFalse(pkg.mandatory)
def test_add_packages_to_existing(self, open_mock, os_mock): self.configure_streams() tmp_file = mock.MagicMock() tmp_file.name = "/tmp/groups.gz" open_mock.return_value.__enter__.return_value = tmp_file repo = gen_repository("test", url="file:///repo/os/x86_64") md_gen = mock.MagicMock() self.createrepo.MetaDataGenerator.return_value = md_gen md_gen.outputdir = "/repo/os/x86_64" md_gen.tempdir = "tmp" md_gen.finaldir = "repodata" os_mock.path.exists.return_value = True os_mock.path.join.side_effect = [ "/repo/os/x86_64/tmp", tmp_file.name, "/repo/os/x86_64/tmp" ] self.driver.add_packages(self.connection, repo, set()) self.assertEqual( "/repo/os/x86_64", self.createrepo.MetaDataConfig().directory ) self.assertTrue(self.createrepo.MetaDataConfig().update) md_gen.doPkgMetadata.assert_called_once_with() md_gen.doRepoMetadata.assert_called_once_with() md_gen.doFinalMove.assert_called_once_with() self.assertGreater(tmp_file.write.call_count, 0) self.assertEqual( tmp_file.name, self.createrepo.MetaDataConfig().groupfile ) os_mock.unlink.assert_called_once_with(tmp_file.name)
def test_rebuild_repository(self, shutil): self.createrepo.MDError = ValueError self.createrepo.MetaDataGenerator().doFinalMove.side_effect = [ None, self.createrepo.MDError() ] repo = gen_repository("test", url="file:///repo/os/x86_64") self.createrepo.MetaDataConfig().outputdir = "/repo/os/x86_64" self.createrepo.MetaDataConfig().tempdir = "tmp" self.driver.rebuild_repository(repo, set()) self.assertEqual( "/repo/os/x86_64", self.createrepo.MetaDataConfig().directory ) self.assertTrue(self.createrepo.MetaDataConfig().update) self.createrepo.MetaDataGenerator()\ .doPkgMetadata.assert_called_once_with() self.createrepo.MetaDataGenerator()\ .doRepoMetadata.assert_called_once_with() self.createrepo.MetaDataGenerator()\ .doFinalMove.assert_called_once_with() with self.assertRaises(RuntimeError): self.driver.rebuild_repository(repo, set()) shutil.rmtree.assert_called_once_with( "/repo/os/x86_64/tmp", ignore_errors=True )
def test_get_packages(self): self.configure_streams() packages = [] self.driver.get_packages( self.connection, gen_repository("test", url="http://host/centos/os/x86_64/"), packages.append) self.connection.open_stream.assert_any_call( "http://host/centos/os/x86_64/repodata/repomd.xml") self.connection.open_stream.assert_any_call( "http://host/centos/os/x86_64/repodata/groups.xml.gz") self.connection.open_stream.assert_any_call( "http://host/centos/os/x86_64/repodata/primary.xml.gz") self.assertEqual(2, len(packages)) package = packages[0] self.assertEqual("test1", package.name) self.assertEqual("1.1.1.1-1.el7", package.version) self.assertEqual(100, package.filesize) self.assertEqual( FileChecksum( None, None, 'e8ed9e0612e813491ed5e7c10502a39e' '43ec665afd1321541dea211202707a65'), package.checksum) self.assertEqual("Packages/test1.rpm", package.filename) self.assertItemsEqual(['test2 (= 0:1.1.1.1-1.el7)'], (str(x) for x in package.requires)) self.assertItemsEqual( ["file (any)", "/usr/bin/test1 (any)", "/usr/bin/test1-bin (any)"], (str(x) for x in package.provides)) self.assertItemsEqual(["test-old (any)"], (str(x) for x in package.obsoletes)) self.assertTrue(package.mandatory) self.assertFalse(packages[1].mandatory)
def test_get_packages(self): streams = [] for conv, fname in zip( (lambda x: six.BytesIO(x.read()), get_compressed, get_compressed), (REPOMD, GROUPS_DB, PRIMARY_DB) ): with open(fname, "rb") as s: streams.append(conv(s)) packages = [] self.connection.open_stream.side_effect = streams self.driver.get_packages( self.connection, gen_repository("test", url="http://host/centos/os/x86_64/"), packages.append ) self.connection.open_stream.assert_any_call("http://host/centos/os/x86_64/repodata/repomd.xml") self.connection.open_stream.assert_any_call("http://host/centos/os/x86_64/repodata/groups.xml.gz") self.connection.open_stream.assert_any_call("http://host/centos/os/x86_64/repodata/primary.xml.gz") self.assertEqual(2, len(packages)) package = packages[0] self.assertEqual("test1", package.name) self.assertEqual("1.1.1.1-1.el7", package.version) self.assertEqual(100, package.filesize) self.assertEqual( FileChecksum(None, None, "e8ed9e0612e813491ed5e7c10502a39e" "43ec665afd1321541dea211202707a65"), package.checksum, ) self.assertEqual("Packages/test1.rpm", package.filename) self.assertItemsEqual(["test2 (= 0-1.1.1.1-1.el7)"], (str(x) for x in package.requires)) self.assertItemsEqual(["file (any)"], (str(x) for x in package.provides)) self.assertItemsEqual(["test-old (any)"], (str(x) for x in package.obsoletes)) self.assertTrue(package.mandatory) self.assertFalse(packages[1].mandatory)
def test_load_package_from_file(self, utils): file_info = ("/test.rpm", 2, [3, 4, 5]) utils.get_size_and_checksum_for_files.return_value = [file_info] utils.get_path_from_url.return_value = "/repo/x86_64/test.rpm" rpm_mock = mock.MagicMock( requires=[("test1", "EQ", ("0", "1.2.3", "1.el5"))], provides=[("test2", None, (None, None, None))], obsoletes=[], ) self.createrepo.yumbased.YumLocalPackage.return_value = rpm_mock rpm_mock.returnLocalHeader.return_value = { "name": "Test", "epoch": 1, "version": "1.2.3", "release": "1", "size": "10", } repo = gen_repository("Test", url="file:///repo/os/x86_64/") pkg = self.driver.load_package_from_file(repo, "test.rpm") utils.get_path_from_url.assert_called_once_with("file:///repo/os/x86_64/test.rpm") self.createrepo.yumbased.YumLocalPackage.assert_called_once_with(filename="/repo/x86_64/test.rpm") utils.get_size_and_checksum_for_files.assert_called_once_with(["/repo/x86_64/test.rpm"], mock.ANY) self.assertEqual("Test", pkg.name) self.assertEqual("1-1.2.3-1", str(pkg.version)) self.assertEqual("test.rpm", pkg.filename) self.assertEqual((3, 4, 5), pkg.checksum) self.assertEqual(10, pkg.filesize) self.assertItemsEqual(["test1 (= 0-1.2.3-1.el5)"], (str(x) for x in pkg.requires)) self.assertItemsEqual(["test2 (any)"], (str(x) for x in pkg.provides)) self.assertEqual([], pkg.obsoletes) self.assertEqual(pkg.mandatory, False)
def test_fork_repository_to_empty_destination(self, m_ensure_dir_exists, m_open, m_os): self.configure_streams() tmp_file = mock.MagicMock() tmp_file.name = "/tmp/groups.gz" m_open.return_value.__enter__.return_value = tmp_file repo = gen_repository("os", url="http://localhost/os/x86_64/") md_gen = mock.MagicMock() self.createrepo.MetaDataGenerator.return_value = md_gen md_gen.doFinalMove.side_effect = [None] md_gen.outputdir = "/repo/os/x86_64" md_gen.tempdir = "tmp" md_gen.finaldir = "repodata" md_config = mock.MagicMock() self.createrepo.MetaDataConfig.return_value = md_config m_os.path.join.side_effect = [ "/repo/os/x86_64/repodata/repomd.xml", tmp_file.name, "/repo/os/x86_64/tmp" ] m_os.path.exists.return_value = False new_repo = self.driver.fork_repository(self.connection, repo, "/repo/os/x86_64") m_ensure_dir_exists.assert_called_once_with("/repo/os/x86_64") self.assertEqual(repo.name, new_repo.name) self.assertEqual(repo.architecture, new_repo.architecture) self.assertEqual("file:///repo/os/x86_64/", new_repo.url) md_gen.doFinalMove.assert_called_once_with() self.assertGreater(tmp_file.write.call_count, 0) self.assertEqual(["*"], md_config.excludes) self.assertFalse(md_config.update) self.assertEqual(tmp_file.name, md_config.groupfile) m_open.assert_called_once_with(tmp_file.name, "w") m_os.unlink.assert_called_once_with(tmp_file.name)
def test_fork_repository(self, deb822, gzip, open, os, utils): os.path.sep = "/" os.path.join = lambda *x: "/".join(x) utils.get_path_from_url = lambda x: x utils.localize_repo_url = localize_repo_url repo = gen_repository( name=("trusty", "main"), url="http://localhost/test/" ) files = [ mock.MagicMock(), mock.MagicMock() ] open.side_effect = files new_repo = self.driver.fork_repository(self.connection, repo, "/root") self.assertEqual(repo.name, new_repo.name) self.assertEqual(repo.architecture, new_repo.architecture) self.assertEqual(repo.origin, new_repo.origin) self.assertEqual("/root/test/", new_repo.url) utils.ensure_dir_exist.assert_called_once_with(os.path.dirname()) open.assert_any_call( "/root/test/dists/trusty/main/binary-amd64/Release", "wb" ) open.assert_any_call( "/root/test/dists/trusty/main/binary-amd64/Packages", "ab" ) gzip.open.assert_called_once_with( "/root/test/dists/trusty/main/binary-amd64/Packages.gz", "ab" )
def test_get_packages_traverse(self): data = [{"name": "r1", "excludes": [{"name": "p1"}]}] repo = generator.gen_repository(name="r1") repo2 = generator.gen_repository(name="r2") packages = [ generator.gen_package(name="p1", version=1, repository=repo), generator.gen_package(name="p2", version=2, repository=repo), generator.gen_package(name="p3", version=2, repository=repo2), generator.gen_package(name="p4", version=2, repository=repo2), ] actual = [] traverse = loaders.get_packages_traverse(data, lambda x: actual.append(str(x))) for p in packages: traverse(p) expected = [str(generator.gen_relation("p2", ["=", "2"]))] self.assertItemsEqual(expected, actual)
def test_get_packages_traverse(self): data = [{'name': 'r1', 'excludes': [{'name': 'p1'}]}] repo = generator.gen_repository(name='r1') repo2 = generator.gen_repository(name='r2') packages = [ generator.gen_package(name='p1', version=1, repository=repo), generator.gen_package(name='p2', version=2, repository=repo), generator.gen_package(name='p3', version=2, repository=repo2), generator.gen_package(name='p4', version=2, repository=repo2) ] actual = [] traverse = loaders.get_packages_traverse( data, lambda x: actual.append(str(x))) for p in packages: traverse(p) expected = [str(generator.gen_relation('p2', ['=', '2']))] self.assertItemsEqual(expected, actual)
def test_copy_packages_does_not_affect_packages_in_same_repo(self): repo = gen_repository(url="file:///repo/") packages = [ gen_package(name="test1", repository=repo, filesize=10), gen_package(name="test2", repository=repo, filesize=-1) ] observer = mock.MagicMock() self.ctrl._copy_packages(repo, packages, observer) self.assertFalse(self.context.connection.retrieve.called)
def _generate_repositories(self, count=1): self.repos_data = [{ "name": "repo{0}".format(i), "uri": "file:///repo{0}".format(i) } for i in range(count)] self.repos = [ generator.gen_repository(**data) for data in self.repos_data ] self.controller.load_repositories.return_value = self.repos
def _generate_repositories(self, count=1): self.repos_data = [ {"name": "repo{0}".format(i), "uri": "file:///repo{0}".format(i)} for i in range(count) ] self.repos = [ generator.gen_repository(**data) for data in self.repos_data ] self.controller.load_repositories.return_value = self.repos
def test_assign_packages(self, os): repo = gen_repository(url="/test/repo") packages = { gen_package(name="test1", repository=repo), gen_package(name="test2", repository=repo) } os.path.join = lambda *x: "/".join(x) self.ctrl.assign_packages(repo, packages) self.driver.add_packages.assert_called_once_with( self.ctrl.context.connection, repo, packages)
def test_load_repositories(self): repo_data = {"name": "test", "uri": "file:///test1"} repo = gen_repository(**repo_data) self.driver.get_repository = CallbacksAdapter() self.driver.get_repository.side_effect = [repo] repos = self.ctrl.load_repositories([repo_data]) self.driver.get_repository.assert_called_once_with( self.context.connection, repo_data, self.ctrl.arch) self.assertEqual([repo], repos)
def test_copy_free_package(self): repo = gen_repository(url="file:///repo/") package = gen_package(name="test1", filename="file:///root/test.pkg", repository=None, filesize=10) self.driver.get_relative_path.side_effect = ["pool/t/test1.pkg"] self.ctrl._copy_package(repo, package, None) self.context.connection.retrieve.assert_called_once_with( "file:///root/test.pkg", "/repo/pool/t/test1.pkg", size=10)
def test_copy_free_package(self): repo = gen_repository(url="file:///repo/") package = gen_package(name="test1", filename="file:///root/test.pkg", repository=None, filesize=10) self.driver.get_relative_path.side_effect = ["pool/t/test1.pkg"] self.ctrl._copy_package(repo, package, None) self.context.connection.retrieve.assert_called_once_with( "file:///root/test.pkg", "/repo/pool/t/test1.pkg", size=10 )
def test_load_repositories(self): repo_data = {"name": "test", "uri": "file:///test1"} repo = gen_repository(**repo_data) self.driver.get_repository = CallbacksAdapter() self.driver.get_repository.side_effect = [repo] repos = self.ctrl.load_repositories([repo_data]) self.driver.get_repository.assert_called_once_with( self.context.connection, repo_data, self.ctrl.arch ) self.assertEqual([repo], repos)
def test_assign_packages(self, os): repo = gen_repository(url="/test/repo") packages = { gen_package(name="test1", repository=repo), gen_package(name="test2", repository=repo) } os.path.join = lambda *x: "/".join(x) self.ctrl.assign_packages(repo, packages) self.driver.add_packages.assert_called_once_with( self.ctrl.context.connection, repo, packages )
def setUp(self): self.controller = CallbacksAdapter() self.api = RepositoryApi(self.controller) self.repo_data = {"name": "repo1", "uri": "file:///repo1"} self.requirements_data = [ {"name": "test1"}, {"name": "test2", "versions": ["< 3", "> 1"]} ] self.schema = {} self.repo = generator.gen_repository(**self.repo_data) self.controller.load_repositories.return_value = [self.repo] self.controller.get_repository_data_schema.return_value = self.schema self._generate_packages()
def test_get_packages_if_group_not_gzipped(self): self.configure_streams(False) packages = [] self.driver.get_packages( self.connection, gen_repository("test", url="http://host/centos/os/x86_64/"), packages.append) self.connection.open_stream.assert_any_call( "http://host/centos/os/x86_64/repodata/groups.xml") self.assertEqual(2, len(packages)) package = packages[0] self.assertTrue(package.mandatory)
def test_fork_repository(self, ensure_dir_exists_mock): repo = gen_repository("os", url="http://localhost/os/x86_64/") self.createrepo.MetaDataGenerator().doFinalMove.side_effect = [None] self.createrepo.MetaDataConfig().outputdir = "/repo/os/x86_64" self.createrepo.MetaDataConfig().tempdir = "tmp" self.createrepo.MetaDataConfig().finaldir = "repodata" new_repo = self.driver.fork_repository(self.connection, repo, "/repo/os/x86_64") ensure_dir_exists_mock.assert_called_once_with("/repo/os/x86_64") self.assertEqual(repo.name, new_repo.name) self.assertEqual(repo.architecture, new_repo.architecture) self.assertEqual("file:///repo/os/x86_64/", new_repo.url) self.createrepo.MetaDataGenerator().doFinalMove.assert_called_once_with()
def test_clone_repositories_as_is(self): controller = CallbacksAdapter() repo = generator.gen_repository(name="repo1") packages = [ generator.gen_package(name="test1", repository=repo), generator.gen_package(name="test2", repository=repo) ] mirror = generator.gen_repository(name="mirror") controller.load_repositories.return_value = repo controller.load_packages.return_value = packages controller.clone_repositories.return_value = {repo: mirror} controller.copy_packages.return_value = [0, 1] api = RepositoryApi(controller) stats = api.clone_repositories( ["file:///repo1"], "/mirror", keep_existing=True ) self.assertEqual(2, stats.total) self.assertEqual(1, stats.copied) controller.copy_packages.assert_called_once_with( mirror, set(packages), True )
def test_copy_packages(self): repo = gen_repository(url="file:///repo/") packages = [ gen_package(name="test1", repository=repo, filesize=10), gen_package(name="test2", repository=repo, filesize=-1) ] target = gen_repository(url="/test/repo/") self.context.connection.retrieve.side_effect = [0, 10] observer = mock.MagicMock() self.ctrl._copy_packages(target, packages, observer) observer.assert_any_call(0) observer.assert_any_call(10) self.context.connection.retrieve.assert_any_call( "file:///repo/test1.pkg", "/test/repo/test1.pkg", size=10 ) self.context.connection.retrieve.assert_any_call( "file:///repo/test2.pkg", "/test/repo/test2.pkg", size=-1 )
def test_clone_cmd(self, stdout, RepositoryController, **kwargs): ctrl = RepositoryController.load() ctrl.copy_packages = CallbacksAdapter() ctrl.load_repositories = CallbacksAdapter() ctrl.load_packages = CallbacksAdapter() ctrl.copy_packages.return_value = [1, 0] repo = gen_repository() ctrl.load_repositories.side_effect = [repo, gen_repository()] ctrl.load_packages.side_effect = [ gen_package(repository=repo), gen_package() ] self.start_cmd(clone, self.clone_argv) RepositoryController.load.assert_called_with( mock.ANY, "deb", "x86_64" ) self.check_context( RepositoryController.load.call_args[0][0], **kwargs ) stdout.write.assert_called_once_with( "Packages copied: 1/2.\n" )
def test_get_packages(self): self.configure_streams() packages = [] self.driver.get_packages( self.connection, gen_repository("test", url="http://host/centos/os/x86_64/"), packages.append ) self.connection.open_stream.assert_any_call( "http://host/centos/os/x86_64/repodata/repomd.xml" ) self.connection.open_stream.assert_any_call( "http://host/centos/os/x86_64/repodata/groups.xml.gz" ) self.connection.open_stream.assert_any_call( "http://host/centos/os/x86_64/repodata/primary.xml.gz" ) self.assertEqual(2, len(packages)) package = packages[0] self.assertEqual("test1", package.name) self.rpmUtils.miscutils.stringToVersion.return_value = ( "0", "1.1.1.1", "1.el7" ) self.rpmUtils.miscutils.compareEVR.return_value = 0 self.assertEqual("1.1.1.1-1.el7", package.version) self.assertEqual(100, package.filesize) self.assertEqual( FileChecksum( None, None, 'e8ed9e0612e813491ed5e7c10502a39e' '43ec665afd1321541dea211202707a65'), package.checksum ) self.assertEqual( "Packages/test1.rpm", package.filename ) self.assertItemsEqual( ['test2 (= 0:1.1.1.1-1.el7)'], (str(x) for x in package.requires) ) self.assertItemsEqual( ["file (any)", "/usr/bin/test1 (any)", "/usr/bin/test1-bin (any)"], (str(x) for x in package.provides) ) self.assertItemsEqual( ["test-old (any)"], (str(x) for x in package.obsoletes) ) self.assertTrue(package.mandatory) self.assertFalse(packages[1].mandatory)
def test_fork_repository(self, os): os.path.join.side_effect = lambda *args: "/".join(args) repo = gen_repository(name="test1", url="file:///test") clone = copy.copy(repo) clone.url = "/root/repo" self.driver.fork_repository.return_value = clone self.context.connection.retrieve.side_effect = [0, 10] self.assertIs(clone, self.ctrl.fork_repository(repo, "./repo", None)) self.driver.fork_repository.assert_called_once_with( self.context.connection, repo, "./repo/test", None) repo.path = "os" self.assertIs(clone, self.ctrl.fork_repository(repo, "./repo", None)) self.driver.fork_repository.assert_called_with(self.context.connection, repo, "./repo/os", None)
def test_get_packages_if_group_not_gzipped(self): self.configure_streams(False) packages = [] self.driver.get_packages( self.connection, gen_repository("test", url="http://host/centos/os/x86_64/"), packages.append ) self.connection.open_stream.assert_any_call( "http://host/centos/os/x86_64/repodata/groups.xml" ) self.assertEqual(2, len(packages)) package = packages[0] self.assertTrue(package.mandatory)
def test_copy_packages(self): repo = gen_repository(url="file:///repo/") packages = [ gen_package(name="test1", repository=repo, filesize=10), gen_package(name="test2", repository=repo, filesize=-1) ] target = gen_repository(url="/test/repo") self.context.connection.retrieve.side_effect = [0, 10] observer = mock.MagicMock() self.ctrl.copy_packages(target, packages, True, observer) observer.assert_has_calls([mock.call(0), mock.call(10)]) self.context.connection.retrieve.assert_any_call( "file:///repo/test1.pkg", "/test/repo/test1.pkg", size=10 ) self.context.connection.retrieve.assert_any_call( "file:///repo/test2.pkg", "/test/repo/test2.pkg", size=-1 ) self.driver.rebuild_repository.assert_called_once_with( target, set(packages) )
def setUp(self): self.controller = CallbacksAdapter() self.api = RepositoryApi(self.controller) self.repo_data = {"name": "repo1", "uri": "file:///repo1"} self.requirements_data = [{ "name": "test1" }, { "name": "test2", "versions": ["< 3", "> 1"] }] self.schema = {} self.repo = generator.gen_repository(**self.repo_data) self.controller.load_repositories.return_value = [self.repo] self.controller.get_repository_data_schema.return_value = self.schema self._generate_packages()
def test_fork_repository(self, utils): repo = gen_repository("os", url="http://localhost/os/x86_64/") utils.localize_repo_url = localize_repo_url new_repo = self.driver.fork_repository( self.connection, repo, "/repo" ) utils.ensure_dir_exist.assert_called_once_with("/repo/os/x86_64/") self.assertEqual(repo.name, new_repo.name) self.assertEqual(repo.architecture, new_repo.architecture) self.assertEqual("/repo/os/x86_64/", new_repo.url) self.createrepo.MetaDataGenerator()\ .doFinalMove.assert_called_once_with()
def test_fork_repository(self, os): os.path.join.side_effect = lambda *args: "".join(args) repo = gen_repository(name="test1", url="file:///test") clone = copy.copy(repo) clone.url = "/root/repo" self.driver.fork_repository.return_value = clone self.context.connection.retrieve.side_effect = [0, 10] self.ctrl.fork_repository(repo, "./repo", False, False) self.driver.fork_repository.assert_called_once_with( self.context.connection, repo, "./repo/test", False, False ) repo.path = "os" self.ctrl.fork_repository(repo, "./repo/", False, False) self.driver.fork_repository.assert_called_with( self.context.connection, repo, "./repo/os", False, False )
def test_load_package_from_file(self, utils): file_info = ("/test.rpm", 2, [3, 4, 5]) utils.get_size_and_checksum_for_files.return_value = [file_info] utils.get_path_from_url.return_value = "/repo/x86_64/test.rpm" rpm_mock = mock.MagicMock( requires=[('test1', 'EQ', ('0', '1.2.3', '1.el5'))], provides=[('test2', None, (None, None, None))], obsoletes=[] ) rpm_mock.returnFileEntries.return_value = ["/usr/bin/test"] self.createrepo.yumbased.YumLocalPackage.return_value = rpm_mock rpm_mock.returnLocalHeader.return_value = { "name": "Test", "epoch": "1", "version": "1.2.3", "release": "1", "size": "10", "group": "Group" } repo = gen_repository("Test", url="file:///repo/os/x86_64/") pkg = self.driver.load_package_from_file(repo, "test.rpm") utils.get_path_from_url.assert_called_once_with( "file:///repo/os/x86_64/test.rpm" ) self.createrepo.yumbased.YumLocalPackage.assert_called_once_with( filename="/repo/x86_64/test.rpm" ) utils.get_size_and_checksum_for_files.assert_called_once_with( ["/repo/x86_64/test.rpm"], mock.ANY ) self.assertEqual("Test", pkg.name) self.assertEqual("1:1.2.3-1", str(pkg.version)) self.assertEqual("test.rpm", pkg.filename) self.assertEqual((3, 4, 5), pkg.checksum) self.assertEqual("Group", pkg.group) self.assertEqual(10, pkg.filesize) self.assertItemsEqual( ['test1 (= 0:1.2.3-1.el5)'], (str(x) for x in pkg.requires) ) self.assertItemsEqual( ['test2 (any)', "/usr/bin/test (any)"], (str(x) for x in pkg.provides) ) self.assertEqual([], pkg.obsoletes) self.assertEqual(pkg.mandatory, False)
def test_create_cmd(self, read_file_in_create_mock, api_mock, read_file_mock, stdout_mock): read_file_in_create_mock.side_effect = [ [{ "name": "repo" }], ["/test1.deb", "/test2.deb", "/test3.deb"], ] api_instance = self.get_api_instance_mock(api_mock) api_instance.create_repository.return_value = gen_repository() self.start_cmd(create, self.create_argv) api_mock.create.assert_called_once_with(mock.ANY, "deb", "x86_64") self.check_common_config(api_mock.create.call_args[0][0]) read_file_in_create_mock.assert_any_call("repository.yaml") read_file_in_create_mock.assert_any_call("package-files.yaml") api_instance.create_repository.assert_called_once_with([{ 'name': 'repo' }], ['/test1.deb', '/test2.deb', '/test3.deb']) stdout_mock.write.assert_called_once_with("Successfully completed.")
def test_create_repository(self): repository_data = { "name": "Test", "uri": "file:///repo/", "section": ("trusty", "main"), "origin": "Test" } repo = gen_repository(**repository_data) packages_list = ['/tmp/test1.pkg'] packages = [gen_package(name="test2", repository=repo)] self.driver.create_repository.return_value = repo self.driver.load_package_from_file.side_effect = packages self.driver.get_relative_path.side_effect = ["pool/t/test1.pkg"] self.ctrl.create_repository(repository_data, packages_list) self.driver.create_repository.assert_called_once_with( self.context.connection, repository_data, self.ctrl.arch) self.driver.get_relative_path.assert_called_once_with( repo, "test1.pkg") self.context.connection.retrieve.assert_any_call( "/tmp/test1.pkg", "/repo/pool/t/test1.pkg") self.driver.load_package_from_file.assert_called_once_with( repo, "pool/t/test1.pkg") self.driver.add_packages.assert_called_once_with( self.ctrl.context.connection, repo, set(packages))
def setUp(self): self.connection = mock.MagicMock() self.connection.HTTPError = HTTPError self.repo = gen_repository( name="trusty", section=("trusty", "main"), url="file:///repo" )
def test_get_relative_path(self): repo = gen_repository( "test", "file://repo", section=("trusty", "main") ) rel_path = self.driver.get_relative_path(repo, "test.pkg") self.assertEqual("packages/test.pkg", rel_path)
def test_copy(self): self.check_copy(generator.gen_repository())