From 9ee11257a37b71187f326b166837150b95a802a9 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Tue, 5 Sep 2006 21:08:44 +0200 Subject: * tests/test_aptsources.py: - added unittest code and implement some basic tests for the aptsources.py code * UpdateManager/Common/aptsources.py: - added __eq__ method to SourceEntry --- tests/data/sources.list | 6 ++++ tests/test_aptsources.py | 85 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 tests/data/sources.list create mode 100644 tests/test_aptsources.py (limited to 'tests') diff --git a/tests/data/sources.list b/tests/data/sources.list new file mode 100644 index 00000000..5481d4f0 --- /dev/null +++ b/tests/data/sources.list @@ -0,0 +1,6 @@ +# comment 1 +deb http://de.archive.ubuntu.com/ubuntu/ edgy main +# comment 2 +deb http://de.archive.ubuntu.com/ubuntu/ edgy restricted +# comment 3 +deb http://de.archive.ubuntu.com/ubuntu/ edgy universe \ No newline at end of file diff --git a/tests/test_aptsources.py b/tests/test_aptsources.py new file mode 100644 index 00000000..4d7e6a0b --- /dev/null +++ b/tests/test_aptsources.py @@ -0,0 +1,85 @@ +#!/usr/bin/env python + +import UpdateManager.Common.aptsources as aptsources +import unittest +import apt_pkg +import os +import copy + +class TestAptSources(unittest.TestCase): + def __init__(self, methodName): + unittest.TestCase.__init__(self, methodName) + apt_pkg.init() + + def testAddComponent(self): + pass + + def testIsMirror(self): + self.assertTrue(aptsources.is_mirror("http://archive.ubuntu.com", + "http://de.archive.ubuntu.com")) + self.assertFalse(aptsources.is_mirror("http://archive.ubuntu.com", + "http://ftp.debian.org")) + + def testSourcesListReading(self): + sources = aptsources.SourcesList() + # test refresh + apt_pkg.Config.Set("Dir::Etc", os.getcwd()) + apt_pkg.Config.Set("Dir::Etc::sourcelist","data/sources.list") + apt_pkg.Config.Set("Dir::Etc::sourceparts",".") + sources.refresh() + self.assertEqual(len(sources.list), 6) + # test load + sources.list = [] + sources.load("data/sources.list") + self.assertEqual(len(sources.list), 6) + # test to add something that is already there (main) + before = copy.deepcopy(sources) + sources.add("deb","http://de.archive.ubuntu.com/ubuntu/", + "edgy", + ["main"]) + self.assertTrue(sources.list == before.list) + # test to add something that is already there (restricted) + before = copy.deepcopy(sources) + sources.add("deb","http://de.archive.ubuntu.com/ubuntu/", + "edgy", + ["restricted"]) + self.assertTrue(sources.list == before.list) + # test to add something new: universe + before = copy.deepcopy(sources) + sources.add("deb","http://de.archive.ubuntu.com/ubuntu/", + "edgy", + ["multiverse"]) + found = False + for entry in sources: + if (entry.type == "deb" and + entry.uri == "http://de.archive.ubuntu.com/ubuntu/" and + entry.dist == "edgy" and + "multiverse" in entry.comps): + found = True + self.assertTrue(found) + # test to add something new: multiverse *and* + # something that is already there + before = copy.deepcopy(sources) + sources.add("deb","http://de.archive.ubuntu.com/ubuntu/", + "edgy", + ["universe", "something"]) + found_universe = 0 + found_something = 0 + for entry in sources: + if (entry.type == "deb" and + entry.uri == "http://de.archive.ubuntu.com/ubuntu/" and + entry.dist == "edgy"): + for c in entry.comps: + print c + if c == "universe": + found_universe += 1 + if c == "something": + found_something += 1 + self.assertEqual(found_something, 1) + self.assertEqual(found_universe, 1) + + + + +if __name__ == "__main__": + unittest.main() -- cgit v1.2.3 From eafcfb0385b7183f8ad02b4d258549975ed5e350 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Wed, 6 Sep 2006 13:09:26 +0200 Subject: * UpdateManager/Common/aptsources.py, tests/test_aptsources.py: - fix test-case-failure in aptsources.py when compents are added --- UpdateManager/Common/aptsources.py | 12 ++++++++++++ tests/test_aptsources.py | 4 ++-- 2 files changed, 14 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/UpdateManager/Common/aptsources.py b/UpdateManager/Common/aptsources.py index b5fa050d..f8ce474b 100644 --- a/UpdateManager/Common/aptsources.py +++ b/UpdateManager/Common/aptsources.py @@ -252,6 +252,18 @@ class SourcesList: The method will search for existing matching repos and will try to reuse them as far as possible """ + # check if we have this source already in the sources.list + for source in self.list: + if source.disabled == False and source.invalid == False and \ + source.type == type and uri == source.uri and \ + source.dist == dist: + for new_comp in comps: + if new_comp in source.comps: + # we have this component already, delete it from the new_comps + # list + del comps[comps.index(new_comp)] + if len(comps) == 0: + return source for source in self.list: # if there is a repo with the same (type, uri, dist) just add the # components diff --git a/tests/test_aptsources.py b/tests/test_aptsources.py index 4d7e6a0b..e8b97263 100644 --- a/tests/test_aptsources.py +++ b/tests/test_aptsources.py @@ -44,8 +44,7 @@ class TestAptSources(unittest.TestCase): "edgy", ["restricted"]) self.assertTrue(sources.list == before.list) - # test to add something new: universe - before = copy.deepcopy(sources) + # test to add something new: multiverse sources.add("deb","http://de.archive.ubuntu.com/ubuntu/", "edgy", ["multiverse"]) @@ -75,6 +74,7 @@ class TestAptSources(unittest.TestCase): found_universe += 1 if c == "something": found_something += 1 + #print "\n".join([s.str() for s in sources]) self.assertEqual(found_something, 1) self.assertEqual(found_universe, 1) -- cgit v1.2.3 From d754e704d6e42bff9f1f8485e134fd4d04cb3a24 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Wed, 6 Sep 2006 13:37:56 +0200 Subject: * tests/test_aptsources.py: - added another test for the "Distribution()" class --- UpdateManager/Common/aptsources.py | 5 ++++- tests/data/sources.list.testDistribution | 14 +++++++++++++ tests/test_aptsources.py | 34 ++++++++++++++++++++++---------- 3 files changed, 42 insertions(+), 11 deletions(-) create mode 100644 tests/data/sources.list.testDistribution (limited to 'tests') diff --git a/UpdateManager/Common/aptsources.py b/UpdateManager/Common/aptsources.py index f8ce474b..8963fd15 100644 --- a/UpdateManager/Common/aptsources.py +++ b/UpdateManager/Common/aptsources.py @@ -635,12 +635,15 @@ class Distribution: def enable_component(self, sourceslist, comp): """ - Disable a component in all main, child and source code sources + Enable a component in all main, child and source code sources (excluding cdrom based sources) sourceslist: an aptsource.sources_list comp: the component that should be enabled """ + # FIXME: we can't just unconditionally add stuff to each line, + # otherwise we need up with multiple components for the + # same repository (see tests/test_aptsources.py for details) sources = [] sources.extend(self.main_sources) sources.extend(self.child_sources) diff --git a/tests/data/sources.list.testDistribution b/tests/data/sources.list.testDistribution new file mode 100644 index 00000000..dd900645 --- /dev/null +++ b/tests/data/sources.list.testDistribution @@ -0,0 +1,14 @@ +# comment 1 +deb http://de.archive.ubuntu.com/ubuntu/ edgy main +# comment 2 +deb http://de.archive.ubuntu.com/ubuntu/ edgy restricted +# comment 3 +deb http://de.archive.ubuntu.com/ubuntu/ edgy universe +# comment 4 +deb http://de.archive.ubuntu.com/ubuntu/ edgy-updates universe multiverse +# comment 5 +deb http://de.archive.ubuntu.com/ubuntu/ edgy-security main +# comment 6 +deb http://ftp.debian.org/debian sid main +# comment 7 +deb http://de.archive.ubuntu.com/ubuntu/ breezy main \ No newline at end of file diff --git a/tests/test_aptsources.py b/tests/test_aptsources.py index e8b97263..0479076c 100644 --- a/tests/test_aptsources.py +++ b/tests/test_aptsources.py @@ -10,9 +10,8 @@ class TestAptSources(unittest.TestCase): def __init__(self, methodName): unittest.TestCase.__init__(self, methodName) apt_pkg.init() - - def testAddComponent(self): - pass + apt_pkg.Config.Set("Dir::Etc", os.getcwd()) + apt_pkg.Config.Set("Dir::Etc::sourceparts",".") def testIsMirror(self): self.assertTrue(aptsources.is_mirror("http://archive.ubuntu.com", @@ -21,17 +20,17 @@ class TestAptSources(unittest.TestCase): "http://ftp.debian.org")) def testSourcesListReading(self): - sources = aptsources.SourcesList() - # test refresh - apt_pkg.Config.Set("Dir::Etc", os.getcwd()) apt_pkg.Config.Set("Dir::Etc::sourcelist","data/sources.list") - apt_pkg.Config.Set("Dir::Etc::sourceparts",".") - sources.refresh() + sources = aptsources.SourcesList() self.assertEqual(len(sources.list), 6) # test load sources.list = [] sources.load("data/sources.list") self.assertEqual(len(sources.list), 6) + + def testSourcesListAdding(self): + apt_pkg.Config.Set("Dir::Etc::sourcelist","data/sources.list") + sources = aptsources.SourcesList() # test to add something that is already there (main) before = copy.deepcopy(sources) sources.add("deb","http://de.archive.ubuntu.com/ubuntu/", @@ -69,7 +68,6 @@ class TestAptSources(unittest.TestCase): entry.uri == "http://de.archive.ubuntu.com/ubuntu/" and entry.dist == "edgy"): for c in entry.comps: - print c if c == "universe": found_universe += 1 if c == "something": @@ -78,7 +76,23 @@ class TestAptSources(unittest.TestCase): self.assertEqual(found_something, 1) self.assertEqual(found_universe, 1) - + def testDistribution(self): + apt_pkg.Config.Set("Dir::Etc::sourcelist","data/sources.list.testDistribution") + sources = aptsources.SourcesList() + distro = aptsources.Distribution() + distro.get_sources(sources) + comp = "restricted" + distro.enable_component(sources, comp) + found = 0 + for entry in sources: + if (entry.type == "deb" and + entry.uri == "http://de.archive.ubuntu.com/ubuntu/" and + entry.dist == "edgy"): + for c in entry.comps: + if c == comp: + found += 1 + print "".join([s.str() for s in sources]) + assertEqual(found, 1) if __name__ == "__main__": -- cgit v1.2.3