def test_find(self): forest = PackagesForest() p11 = generator.gen_package(name="package1", version=1) p12 = generator.gen_package(name="package1", version=2) p21 = generator.gen_package(name="package2", version=1) p22 = generator.gen_package(name="package2", version=2) p33 = generator.gen_package(name="package2", version=10) self._add_packages(forest.add_tree(priority=10), [p11, p22]) self._add_packages(forest.add_tree(priority=10), [p12, p21]) self._add_packages(forest.add_tree(priority=20), [p33]) self.assertEqual( p11, forest.find(generator.gen_relation("package1", ["=", 1])) ) self.assertEqual( p12, forest.find(generator.gen_relation("package1", [">=", 1])) ) self.assertEqual( p12, forest.find(generator.gen_relation("package1", [">", 1])) ) self.assertEqual(p22, forest.find(generator.gen_relation("package2"))) self.assertEqual( p21, forest.find(generator.gen_relation("package2", ["<", 2])) ) # select package from the repo with highest priority # p33 has version 10, but in the repo with lower priority self.assertEqual( p22, forest.find(generator.gen_relation("package2", [">=", 2])) )
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_ordering(self): self.check_ordering([ generator.gen_package(name="test1", version=1), generator.gen_package(name="test1", version=2), generator.gen_package(name="test2", version=1), generator.gen_package(name="test2", version=2) ])
def _generate_packages(self, forest): packages1 = [ generator.gen_package( name="package1", version=1, mandatory=True, requires=None ), generator.gen_package( name="package2", version=1, requires=None ), generator.gen_package( name="package3", version=1, requires=[generator.gen_relation("package5")] ) ] packages2 = [ generator.gen_package( name="package4", version=1, mandatory=True, requires=None ), generator.gen_package( name="package5", version=1, requires=[generator.gen_relation("package2")] ), ] self._add_packages(forest.add_tree(priority=10), packages1) self._add_packages(forest.add_tree(priority=10), packages2)
def test_find_with_specified_version(self): index = Index() p1 = gen_package(idx=1, version=1) p2 = gen_package(idx=1, version=2) index.add(p1) index.add(p2) self.assertItemsEqual([p1], index.find_all(p1.name, objects.VersionRange("=", p1.version))) self.assertItemsEqual([p2], index.find_all(p2.name, objects.VersionRange("=", p2.version)))
def test_hashable(self): self.check_hashable( generator.gen_package(name="test1", version=1), generator.gen_package(name="test2", version=1), ) self.check_hashable( generator.gen_package(name="test1", version=1), generator.gen_package(name="test1", version=2), )
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 test_find_down_up(self): index = Index() p1 = gen_package(version=1) p2 = gen_package(version=2) index.add(p1) index.add(p2) self.assertEqual([p2], index.find_all(p1.name, objects.VersionRange(">=", 2))) self.assertEqual([p2], index.find_all(p1.name, objects.VersionRange(">", 1))) self.assertEqual([], index.find_all(p1.name, objects.VersionRange(">", 2)))
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_find_package(self): tree = PackagesTree() p1 = generator.gen_package(idx=1, version=1) p2 = generator.gen_package(idx=1, version=2) tree.add(p1) tree.add(p2) self.assertIs(p1, tree.find(p1.name, VersionRange("<", p2.version))) self.assertIs(p2, tree.find(p1.name, VersionRange(">=", p1.version))) self.assertIsNone(tree.find(p1.name, VersionRange(">", p2.version)))
def test_add(self): index = Index() package1 = gen_package(version=1) index.add(package1) self.assertIn(package1.name, index.packages) self.assertEqual([(1, package1)], list(index.packages[package1.name].items())) package2 = gen_package(version=2) index.add(package2) self.assertEqual(1, len(index.packages)) self.assertEqual([(1, package1), (2, package2)], list(index.packages[package1.name].items()))
def test_mandatory_packages_always_included(self): ptree = PackagesTree() ptree.add(generator.gen_package(1, requires=None, mandatory=True)) ptree.add(generator.gen_package(2, requires=None)) ptree.add(generator.gen_package(3, requires=None)) unresolved = set([generator.gen_relation("package3")]) resolved = ptree.get_minimal_subset(None, unresolved) self.assertItemsEqual( ["package3", "package1"], (x.name for x in resolved) )
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_find_accurate(self): index = Index() p1 = gen_package(version=1) p2 = gen_package(version=2) index.add(p1) index.add(p2) self.assertIs( p1, index.find("package1", objects.VersionRange("eq", 1)) ) self.assertIsNone( index.find("package1", objects.VersionRange("eq", 3)) )
def test_find_provides(self): index = Index() p1 = gen_package(version=1) p2 = gen_package(version=2) index.add(p1) index.add(p2) self.assertIs( p2, index.find("provides1", objects.VersionRange("ge", 2)) ) self.assertIsNone( index.find("provides1", objects.VersionRange("gt", 2)) )
def test_get_unresolved_dependencies(self): tree = PackagesTree() tree.add(generator.gen_package( 1, requires=[generator.gen_relation("unresolved")])) tree.add(generator.gen_package(2, requires=None)) tree.add(generator.gen_package( 3, requires=[generator.gen_relation("package1")] )) unresolved = tree.get_unresolved_dependencies() self.assertItemsEqual( ["unresolved"], (x.name for x in unresolved) )
def test_get_minimal_subset_without_master(self): ptree = PackagesTree() ptree.add(generator.gen_package(1, requires=None)) ptree.add(generator.gen_package(2, requires=None)) ptree.add(generator.gen_package( 3, requires=[generator.gen_relation("package1")] )) unresolved = set([generator.gen_relation("package3")]) resolved = ptree.get_minimal_subset(None, unresolved) self.assertItemsEqual( ["package3", "package1"], (x.name for x in resolved) )
def test_add(self): index = Index() index.add(gen_package(version=1)) self.assertIn("package1", index.packages) self.assertIn(1, index.packages["package1"]) self.assertIn("obsoletes1", index.obsoletes) self.assertIn("provides1", index.provides) index.add(gen_package(version=2)) self.assertEqual(1, len(index.packages)) self.assertIn(1, index.packages["package1"]) self.assertIn(2, index.packages["package1"]) self.assertEqual(1, len(index.obsoletes)) self.assertEqual(1, len(index.provides))
def test_len(self): index = Index() for i in six.moves.range(3): index.add(gen_package(idx=i + 1)) self.assertEqual(3, len(index)) for i in six.moves.range(3): index.add(gen_package(idx=i + 1, version=2)) self.assertEqual(6, len(index)) self.assertEqual(3, len(index.packages)) for i in six.moves.range(3): index.add(gen_package(idx=i + 1, version=2)) self.assertEqual(6, len(index)) self.assertEqual(3, len(index.packages))
def test_add(self): tree = PackagesTree() pkg = generator.gen_package(version=1, mandatory=True) tree.add(pkg) self.assertIs(pkg, tree.find(pkg.name, VersionRange('=', pkg.version))) self.assertIs( pkg.obsoletes[0], tree.obsoletes[pkg.obsoletes[0].name][(pkg.name, pkg.version)] ) self.assertIs( pkg.provides[0], tree.provides[pkg.provides[0].name][(pkg.name, pkg.version)] ) tree.add(generator.gen_package(version=1, mandatory=False)) self.assertItemsEqual([pkg], tree.mandatory_packages)
def test_find_with_specified_version(self): index = Index() p1 = gen_package(idx=1, version=1) p2 = gen_package(idx=1, version=2) index.add(p1) index.add(p2) self.assertItemsEqual([p1], index.find_all( p1.name, objects.VersionRange("=", p1.version))) self.assertItemsEqual([p2], index.find_all( p2.name, objects.VersionRange("=", p2.version)))
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_find_newest_package(self): index = Index() p1 = gen_package(idx=1, version=2) p2 = gen_package(idx=2, version=2) p2.obsoletes.append( gen_relation(p1.name, ["lt", p1.version]) ) index.add(p1) index.add(p2) self.assertIs( p1, index.find(p1.name, objects.VersionRange("eq", p1.version)) ) self.assertIs( p2, index.find(p1.name, objects.VersionRange("eq", 1)) )
def _generate_packages(self): self.packages = [ generator.gen_package(idx=1, repository=self.repo, requires=None), generator.gen_package(idx=2, repository=self.repo, requires=None), generator.gen_package( idx=3, repository=self.repo, mandatory=True, requires=[generator.gen_relation("package2")] ), generator.gen_package( idx=4, repository=self.repo, mandatory=False, requires=[generator.gen_relation("package1")] ), generator.gen_package( idx=5, repository=self.repo, requires=[generator.gen_relation("package6")]) ] self.controller.load_packages.return_value = self.packages
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_find_obsolete(self): tree = PackagesTree() p1 = generator.gen_package( version=1, obsoletes=[generator.gen_relation('obsolete', ('<', 2))] ) p2 = generator.gen_package( version=2, obsoletes=[generator.gen_relation('obsolete', ('<', 2))] ) tree.add(p1) tree.add(p2) self.assertEqual( [p1, p2], tree.find_all("obsolete", VersionRange("<=", 2)) ) self.assertIsNone( tree.find("obsolete", VersionRange(">", 2)) )
def test_find_top_down(self): index = Index() p1 = gen_package(version=1) p2 = gen_package(version=2) index.add(p1) index.add(p2) self.assertIs( p2, index.find("package1", objects.VersionRange("le", 2)) ) self.assertIs( p1, index.find("package1", objects.VersionRange("lt", 2)) ) self.assertIsNone( index.find("package1", objects.VersionRange("lt", 1)) )
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_find(self): forest = PackagesForest() p11 = generator.gen_package(name="package1", version=1) p12 = generator.gen_package(name="package1", version=2) p21 = generator.gen_package(name="package2", version=1) p22 = generator.gen_package(name="package2", version=2) self._add_packages(forest.add_tree(), [p11, p22]) self._add_packages(forest.add_tree(), [p12, p21]) self.assertEqual( p11, forest.find(generator.gen_relation("package1", [">=", 1])) ) self.assertEqual( p12, forest.find(generator.gen_relation("package1", [">", 1])) ) self.assertEqual(p22, forest.find(generator.gen_relation("package2"))) self.assertEqual( p21, forest.find(generator.gen_relation("package2", ["<", 2])) )
def test_find_all(self): index = Index() p11 = gen_package(idx=1, version=1) p12 = gen_package(idx=1, version=2) p21 = gen_package(idx=2, version=1) p22 = gen_package(idx=2, version=2) index.add(p11) index.add(p12) index.add(p21) index.add(p22) self.assertItemsEqual( [p11, p12], index.find_all("package1", objects.VersionRange()) ) self.assertItemsEqual( [p21, p22], index.find_all("package2", objects.VersionRange("le", 2)) )
def test_get_unresolved_dependencies(self): ptree = PackagesTree() ptree.add(generator.gen_package( 1, requires=[generator.gen_relation("unresolved")])) ptree.add(generator.gen_package(2, requires=None)) ptree.add(generator.gen_package( 3, requires=[generator.gen_relation("package1")] )) ptree.add(generator.gen_package( 4, requires=[generator.gen_relation("loop")], obsoletes=[generator.gen_relation("loop", ["le", 1])] )) unresolved = ptree.get_unresolved_dependencies() self.assertItemsEqual( ["loop", "unresolved"], (x.name for x in unresolved) )
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_warning_if_unresolved(self): ptree = PackagesTree() ptree.add(generator.gen_package( 1, requires=None)) with warnings.catch_warnings(record=True) as log: ptree.get_minimal_subset( None, [generator.gen_relation("package2")] ) self.assertIn("package2", str(log[0]))
def _generate_packages(self): self.packages = [ generator.gen_package(idx=1, repository=self.repo, requires=None), generator.gen_package(idx=2, repository=self.repo, requires=None), generator.gen_package( idx=3, repository=self.repo, mandatory=True, requires=[generator.gen_relation("package2")]), generator.gen_package( idx=4, repository=self.repo, mandatory=False, requires=[generator.gen_relation("package1")]), generator.gen_package( idx=5, repository=self.repo, requires=[generator.gen_relation("package6")]) ] self.controller.load_packages.return_value = self.packages
def test_find_obsolete(self): index = Index() p1 = gen_package(version=1) index.add(p1) self.assertIs( p1, index.find("obsoletes1", objects.VersionRange("le", 2)) ) self.assertIsNone( index.find("obsoletes1", objects.VersionRange("gt", 2)) )
def _generate_packages(self): self.packages = [[ generator.gen_package(name='{0}_1'.format(r.name), repository=r, requires=None), generator.gen_package(name='{0}_2'.format(r.name), repository=r, requires=None), generator.gen_package( name='{0}_3'.format(r.name), repository=r, mandatory=True, requires=[generator.gen_relation("{0}_2".format(r.name))]), generator.gen_package( name='{0}_4'.format(r.name), repository=r, mandatory=False, requires=[generator.gen_relation("{0}_1".format(r.name))]), generator.gen_package( name='{0}_5'.format(r.name), repository=r, requires=[generator.gen_relation("unresolved")]), generator.gen_package(name='package10', repository=r, mandatory=True, requires=None, version=counter + 10) ] for counter, r in enumerate(self.repos)] self.controller.load_packages.side_effect = self.packages
def test_load_filter(self): filter_data = [ { "name": "p1", "group": "g1" }, { "name": "p2" }, { "group": "g3" }, { "name": "/^.5/", "group": "/^.*3/" }, { "group": "/^.*4/" }, ] filters = loaders.load_filters(filter_data) cases = [ (True, (generator.gen_package(name='p1', group='g1'), )), (True, (generator.gen_package(name="p2", group="g1"), )), (False, (generator.gen_package(name="p3", group="g2"), )), (True, (generator.gen_package(name="p4", group="g3"), )), (True, (generator.gen_package(name="p5", group="g3"), )), (True, (generator.gen_package(name="p6", group="g4"), )), ] self._check_cases(self.assertIs, cases, filters) self.assertFalse(loaders.load_filters([])(cases[0][1][0]))
def test_find_provides(self): tree = PackagesTree() p1 = generator.gen_package( version=1, obsoletes=[generator.gen_relation('provide', ('<', 2))] ) tree.add(p1) self.assertIs( p1, tree.find("provide", VersionRange("<=", 2)) ) self.assertIsNone( tree.find("provide", VersionRange(">", 2)) )
def test_find(self): forest = PackagesForest() p11 = generator.gen_package(name="package1", version=1) p12 = generator.gen_package(name="package1", version=2) p21 = generator.gen_package(name="package2", version=1) p22 = generator.gen_package(name="package2", version=2) p33 = generator.gen_package(name="package2", version=10) self._add_packages(forest.add_tree(priority=10), [p11, p22]) self._add_packages(forest.add_tree(priority=10), [p12, p21]) self._add_packages(forest.add_tree(priority=20), [p33]) self.assertEqual( p11, forest.find(generator.gen_relation("package1", ["=", 1]))) self.assertEqual( p12, forest.find(generator.gen_relation("package1", [">=", 1]))) self.assertEqual( p12, forest.find(generator.gen_relation("package1", [">", 1]))) self.assertEqual(p22, forest.find(generator.gen_relation("package2"))) self.assertEqual( p21, forest.find(generator.gen_relation("package2", ["<", 2]))) # select package from the repo with highest priority # p33 has version 10, but in the repo with lower priority self.assertEqual( p22, forest.find(generator.gen_relation("package2", [">=", 2])))
def _generate_packages(self, forest): packages1 = [ generator.gen_package(name="package1", version=1, mandatory=True, requires=None), generator.gen_package(name="package2", version=1, requires=None), generator.gen_package( name="package3", version=1, requires=[generator.gen_relation("package5")]) ] packages2 = [ generator.gen_package(name="package4", version=1, mandatory=True, requires=None), generator.gen_package( name="package5", version=1, requires=[generator.gen_relation("package2")]), ] self._add_packages(forest.add_tree(priority=10), packages1) self._add_packages(forest.add_tree(priority=10), packages2)
def test_add_packages(self, os, debfile, deb822, fcntl, gzip, utils, open): package = gen_package(name="test", repository=self.repo) os.path.join = lambda *x: "/".join(x) utils.get_path_from_url = lambda x: x[7:] files = [ mock.MagicMock(), # Packages, w mock.MagicMock(), # Release, a+b mock.MagicMock(), # Packages, rb mock.MagicMock(), # Release, rb mock.MagicMock() # Packages.gz, rb ] open.side_effect = files self.driver.add_packages(self.connection, self.repo, {package}) open.assert_any_call("/repo/dists/trusty/main/binary-amd64/Packages", "wb") gzip.open.assert_called_once_with( "/repo/dists/trusty/main/binary-amd64/Packages.gz", "wb") debfile.DebFile.assert_called_once_with("/repo/test.pkg")
def test_get_packages_cmd(self, api_mock, read_file_mock, stdout_mock): read_file_mock.return_value = [{"name": "repo"}] api_instance = self.get_api_instance_mock(api_mock) api_instance.get_packages.return_value = [ gen_package(name="test1", filesize=1, requires=None, obsoletes=None, provides=None) ] self.start_cmd(packages, self.packages_argv) read_file_mock.assert_called_with("repositories.yaml") api_mock.create.assert_called_once_with(mock.ANY, "deb", "x86_64") self.check_common_config(api_mock.create.call_args[0][0]) api_instance.get_packages.assert_called_once_with([{ "name": "repo" }], None) self.assertIn("test1; test1.pkg", stdout_mock.write.call_args_list[3][0][0])
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 test_load_filter(self, jsonschema_mock): self.api._validate_filter_data = mock.Mock() filter_data = [ { "name": "p1", "group": "g1" }, { "name": "p2" }, { "group": "g3" }, { "name": "/^.5/", "group": "/^.*3/" }, { "group": "/^.*4/" }, ] exclude_filter = self.api._load_filter(filter_data) p1 = generator.gen_package(name="p1", group="g1") p2 = generator.gen_package(name="p2", group="g1") p3 = generator.gen_package(name="p3", group="g2") p4 = generator.gen_package(name="p4", group="g3") p5 = generator.gen_package(name="p5", group="g3") p6 = generator.gen_package(name="p6", group="g4") cases = [ (True, (p1, )), (True, (p2, )), (False, (p3, )), (True, (p4, )), (True, (p5, )), (True, (p6, )), ] self._check_cases(self.assertEqual, cases, exclude_filter)
def test_copy(self): self.check_copy(generator.gen_package(name="test1"))
def test_equal(self): self.check_equal( generator.gen_package(name="test1", version=1), generator.gen_package(name="test1", version=1), generator.gen_package(name="test2", version=1) )