From aa6d585a44dd828362c821d6b938b0165af32e7e Mon Sep 17 00:00:00 2001 From: Thomas Leonard Date: Sat, 9 Jun 2012 09:19:56 +0100 Subject: [PATCH] Updated Policy tests to test Driver instead Also, trying to create a Policy now issues a warning. --- tests/{testautopolicy.py => testdriver.py} | 153 ++++++++++++++++------------- tests/testinstall.py | 6 +- tests/testlaunch.py | 12 ++- tests/testpolicy.py | 42 -------- tests/testrun.py | 15 +-- tests/testselections.py | 45 +++++---- zeroinstall/injector/policy.py | 4 +- 7 files changed, 131 insertions(+), 146 deletions(-) rename tests/{testautopolicy.py => testdriver.py} (70%) delete mode 100755 tests/testpolicy.py diff --git a/tests/testautopolicy.py b/tests/testdriver.py similarity index 70% rename from tests/testautopolicy.py rename to tests/testdriver.py index b637aca..8f0db9a 100755 --- a/tests/testautopolicy.py +++ b/tests/testdriver.py @@ -7,24 +7,25 @@ import unittest sys.path.insert(0, '..') from zeroinstall.injector import model, gpg, namespaces, reader, run, fetch -from zeroinstall.injector.policy import Policy -from zeroinstall.support import basedir +from zeroinstall.injector.requirements import Requirements +from zeroinstall.injector.driver import Driver +from zeroinstall.support import basedir, tasks import data foo_iface_uri = 'http://foo' logger = logging.getLogger() -def recalculate(policy): - policy.need_download() +def recalculate(driver): + driver.need_download() -def download_and_execute(policy, prog_args, main = None, dry_run = True): - downloaded = policy.solve_and_download_impls() +def download_and_execute(driver, prog_args, main = None, dry_run = True): + downloaded = driver.solve_and_download_impls() if downloaded: - policy.config.handler.wait_for_blocker(downloaded) - run.execute_selections(policy.solver.selections, prog_args, stores = policy.config.stores, main = main, dry_run = dry_run) + tasks.wait_for_blocker(downloaded) + run.execute_selections(driver.solver.selections, prog_args, stores = driver.config.stores, main = main, dry_run = dry_run) -class TestAutoPolicy(BaseTest): +class TestDriver(BaseTest): def setUp(self): BaseTest.setUp(self) stream = tempfile.TemporaryFile() @@ -41,13 +42,12 @@ class TestAutoPolicy(BaseTest): f.close() def testNoNeedDl(self): - policy = Policy(foo_iface_uri, config = self.config) - policy.freshness = 0 - assert policy.need_download() + driver = Driver(requirements = Requirements(foo_iface_uri), config = self.config) + assert driver.need_download() - policy = Policy(os.path.abspath('Foo.xml'), config = self.config) - assert not policy.need_download() - assert policy.ready + driver = Driver(requirements = Requirements(os.path.abspath('Foo.xml')), config = self.config) + assert not driver.need_download() + assert driver.solver.ready def testUnknownAlg(self): self.cache_iface(foo_iface_uri, @@ -63,11 +63,10 @@ class TestAutoPolicy(BaseTest): """ % foo_iface_uri) self.config.fetcher = fetch.Fetcher(self.config) - policy = Policy(foo_iface_uri, config = self.config) - policy.freshness = 0 + driver = Driver(requirements = Requirements(foo_iface_uri), config = self.config) try: - assert policy.need_download() - download_and_execute(policy, []) + assert driver.need_download() + download_and_execute(driver, []) except model.SafeException as ex: assert 'Unknown digest algorithm' in str(ex) @@ -84,9 +83,9 @@ class TestAutoPolicy(BaseTest): """) tmp.flush() - policy = Policy(tmp.name, config = self.config) + driver = Driver(requirements = Requirements(tmp.name), config = self.config) try: - download_and_execute(policy, ['Hello']) + download_and_execute(driver, ['Hello']) assert 0 except model.SafeException as ex: assert "ThisBetterNotExist" in str(ex) @@ -104,9 +103,9 @@ class TestAutoPolicy(BaseTest): """) tmp.flush() - policy = Policy(tmp.name, config = self.config) + driver = Driver(requirements = Requirements(tmp.name), config = self.config) try: - download_and_execute(policy, ['Hello']) + download_and_execute(driver, ['Hello']) assert 0 except model.SafeException as ex: assert "library" in str(ex), ex @@ -126,12 +125,11 @@ class TestAutoPolicy(BaseTest): """ % foo_iface_uri) - policy = Policy(foo_iface_uri, config = self.config) - policy.freshness = 0 - policy.network_use = model.network_full - recalculate(policy) - assert policy.need_download() - assert policy.ready + driver = Driver(requirements = Requirements(foo_iface_uri), config = self.config) + self.config.network_use = model.network_full + recalculate(driver) + assert driver.need_download() + assert driver.solver.ready def testBinding(self): local_impl = os.path.dirname(os.path.abspath(__file__)) @@ -139,7 +137,7 @@ class TestAutoPolicy(BaseTest): tmp.write( """ Bar Bar @@ -171,12 +169,12 @@ class TestAutoPolicy(BaseTest): cached_impl = basedir.save_cache_path('0install.net', 'implementations', 'sha1=123') - policy = Policy(tmp.name, config = self.config) - policy.network_use = model.network_offline + driver = Driver(requirements = Requirements(tmp.name), config = self.config) + self.config.network_use = model.network_offline os.environ['FOO_PATH'] = "old" old, sys.stdout = sys.stdout, StringIO() try: - download_and_execute(policy, ['Hello']) + download_and_execute(driver, ['Hello']) finally: sys.stdout = old self.assertEqual(cached_impl + '/.:old', @@ -194,7 +192,7 @@ class TestAutoPolicy(BaseTest): os.environ['BAR_PATH'] = '/old' old, sys.stdout = sys.stdout, StringIO() try: - download_and_execute(policy, ['Hello']) + download_and_execute(driver, ['Hello']) finally: sys.stdout = old self.assertEqual(cached_impl + '/.', @@ -228,13 +226,12 @@ class TestAutoPolicy(BaseTest): """ % foo_iface_uri) - policy = Policy(foo_iface_uri, config = self.config) - policy.freshness = 0 - policy.network_use = model.network_full - recalculate(policy) - assert policy.ready + driver = Driver(requirements = Requirements(foo_iface_uri), config = self.config) + self.config.network_use = model.network_full + recalculate(driver) + assert driver.solver.ready foo_iface = self.config.iface_cache.get_interface(foo_iface_uri) - self.assertEqual('sha1=123', policy.implementation[foo_iface].id) + self.assertEqual('sha1=123', driver.solver.selections[foo_iface].id) def testBadConfig(self): path = basedir.save_config_path(namespaces.config_site, @@ -246,7 +243,7 @@ class TestAutoPolicy(BaseTest): stream.close() logger.setLevel(logging.ERROR) - Policy(foo_iface_uri, config = self.config) + Driver(requirements = Requirements(foo_iface_uri), config = self.config) logger.setLevel(logging.WARN) def testNoLocal(self): @@ -278,17 +275,16 @@ class TestAutoPolicy(BaseTest): Foo """ % foo_iface_uri) - policy = Policy(foo_iface_uri, config = self.config) - policy.network_use = model.network_full - policy.freshness = 0 + driver = Driver(requirements = Requirements(foo_iface_uri), config = self.config) + self.config.network_use = model.network_full - assert policy.need_download() + assert driver.need_download() feed = self.config.iface_cache.get_feed(foo_iface_uri) feed.feeds = [model.Feed('/BadFeed', None, False)] logger.setLevel(logging.ERROR) - assert policy.need_download() # Triggers warning + assert driver.need_download() # Triggers warning logger.setLevel(logging.WARN) def testBestUnusable(self): @@ -302,12 +298,12 @@ class TestAutoPolicy(BaseTest): Foo """ % foo_iface_uri) - policy = Policy(foo_iface_uri, config = self.config) - policy.network_use = model.network_offline - recalculate(policy) - assert not policy.ready, policy.implementation + driver = Driver(requirements = Requirements(foo_iface_uri), config = self.config) + self.config.network_use = model.network_offline + recalculate(driver) + assert not driver.solver.ready, driver.implementation try: - download_and_execute(policy, []) + download_and_execute(driver, []) assert False except model.SafeException as ex: assert "has no usable implementations" in str(ex), ex @@ -323,10 +319,9 @@ class TestAutoPolicy(BaseTest): Foo """ % foo_iface_uri) - policy = Policy(foo_iface_uri, config = self.config) - policy.freshness = 0 - recalculate(policy) - assert not policy.ready + driver = Driver(requirements = Requirements(foo_iface_uri), config = self.config) + recalculate(driver) + assert not driver.solver.ready def testCycle(self): self.cache_iface(foo_iface_uri, @@ -344,9 +339,8 @@ class TestAutoPolicy(BaseTest): """ % (foo_iface_uri, foo_iface_uri)) - policy = Policy(foo_iface_uri, config = self.config) - policy.freshness = 0 - recalculate(policy) + driver = Driver(requirements = Requirements(foo_iface_uri), config = self.config) + recalculate(driver) def testConstraints(self): self.cache_iface('http://bar', @@ -384,27 +378,50 @@ class TestAutoPolicy(BaseTest): """ % foo_iface_uri) - policy = Policy(foo_iface_uri, config = self.config) - policy.network_use = model.network_full - policy.freshness = 0 + driver = Driver(requirements = Requirements(foo_iface_uri), config = self.config) + self.config.network_use = model.network_full #logger.setLevel(logging.DEBUG) - recalculate(policy) + recalculate(driver) #logger.setLevel(logging.WARN) foo_iface = self.config.iface_cache.get_interface(foo_iface_uri) bar_iface = self.config.iface_cache.get_interface('http://bar') - assert policy.implementation[bar_iface].id == 'sha1=200' + assert driver.solver.selections[bar_iface].id == 'sha1=200' - dep = policy.implementation[foo_iface].dependencies['http://bar'] + dep = driver.solver.selections[foo_iface].dependencies['http://bar'] assert len(dep.restrictions) == 1 restriction = dep.restrictions[0] restriction.before = model.parse_version('2.0') - recalculate(policy) - assert policy.implementation[bar_iface].id == 'sha1=100' + recalculate(driver) + assert driver.solver.selections[bar_iface].id == 'sha1=100' restriction.not_before = model.parse_version('1.5') - recalculate(policy) - assert policy.implementation[bar_iface].id == 'sha1=150' + recalculate(driver) + assert driver.solver.selections[bar_iface].id == 'sha1=150' + + def testSource(self): + iface_cache = self.config.iface_cache + + foo = iface_cache.get_interface('http://foo/Binary.xml') + self.import_feed(foo.uri, 'Binary.xml') + foo_src = iface_cache.get_interface('http://foo/Source.xml') + self.import_feed(foo_src.uri, 'Source.xml') + compiler = iface_cache.get_interface('http://foo/Compiler.xml') + self.import_feed(compiler.uri, 'Compiler.xml') + + self.config.freshness = 0 + self.config.network_use = model.network_full + driver = Driver(requirements = Requirements('http://foo/Binary.xml'), config = self.config) + tasks.wait_for_blocker(driver.solve_with_downloads()) + assert driver.solver.selections[foo].id == 'sha1=123' + + # Now ask for source instead + driver.requirements.source = True + driver.requirements.command = 'compile' + tasks.wait_for_blocker(driver.solve_with_downloads()) + assert driver.solver.ready, driver.solver.get_failure_reason() + assert driver.solver.selections[foo].id == 'sha1=234' # The source + assert driver.solver.selections[compiler].id == 'sha1=345' # A binary needed to compile it if __name__ == '__main__': unittest.main() diff --git a/tests/testinstall.py b/tests/testinstall.py index b89d77e..336f30a 100755 --- a/tests/testinstall.py +++ b/tests/testinstall.py @@ -6,7 +6,7 @@ import unittest sys.path.insert(0, '..') from zeroinstall import cmd -from zeroinstall.injector import model, selections, qdom, reader, policy, handler, gpg +from zeroinstall.injector import model, selections, qdom, reader, handler, gpg, config mydir = os.path.dirname(__file__) @@ -223,7 +223,7 @@ class TestInstall(BaseTest): out, err = self.run_0install(['config', 'help_with_testing']) assert out == 'False\n', out - file_config = policy.load_config(handler.Handler()) + file_config = config.load_config(handler.Handler()) def get_value(name): old_stdout = sys.stdout sys.stdout = StringIO() @@ -245,7 +245,7 @@ class TestInstall(BaseTest): assert file_config.network_use == model.network_minimal assert file_config.help_with_testing == True - file_config2 = policy.load_config(handler.Handler()) + file_config2 = config.load_config(handler.Handler()) assert file_config2.freshness == 5 * 60 assert file_config2.network_use == model.network_minimal assert file_config2.help_with_testing == True diff --git a/tests/testlaunch.py b/tests/testlaunch.py index 33f7955..06d331b 100755 --- a/tests/testlaunch.py +++ b/tests/testlaunch.py @@ -12,9 +12,11 @@ foo_iface_uri = 'http://foo' sys.path.insert(0, '..') from zeroinstall import SafeException -from zeroinstall.injector.policy import Policy +from zeroinstall.support import tasks from zeroinstall.injector import run, cli, namespaces, qdom, selections from zeroinstall.zerostore import Store; Store._add_with_helper = lambda *unused: False +from zeroinstall.injector.requirements import Requirements +from zeroinstall.injector.driver import Driver mydir = os.path.abspath(os.path.dirname(__file__)) @@ -121,12 +123,12 @@ class TestLaunch(BaseTest): """ % foo_iface_uri) tmp.flush() - policy = Policy(tmp.name, config = self.config) + driver = Driver(requirements = Requirements(tmp.name), config = self.config) try: - downloaded = policy.solve_and_download_impls() + downloaded = driver.solve_and_download_impls() if downloaded: - policy.handler.wait_for_blocker(downloaded) - run.execute_selections(policy.solver.selections, [], stores = policy.config.stores) + tasks.wait_for_blocker(downloaded) + run.execute_selections(driver.solver.selections, [], stores = self.config.stores) assert False except SafeException as ex: assert 'Command path must be relative' in str(ex), ex diff --git a/tests/testpolicy.py b/tests/testpolicy.py deleted file mode 100755 index 1e733f1..0000000 --- a/tests/testpolicy.py +++ /dev/null @@ -1,42 +0,0 @@ -#!/usr/bin/env python -from basetest import BaseTest -import sys -import unittest - -sys.path.insert(0, '..') -from zeroinstall.support import tasks -from zeroinstall.injector import model -from zeroinstall.injector.policy import Policy - -import warnings -import logging -logger = logging.getLogger() -#logger.setLevel(logging.DEBUG) - -class TestPolicy(BaseTest): - def testSource(self): - iface_cache = self.config.iface_cache - - foo = iface_cache.get_interface('http://foo/Binary.xml') - self.import_feed(foo.uri, 'Binary.xml') - foo_src = iface_cache.get_interface('http://foo/Source.xml') - self.import_feed(foo_src.uri, 'Source.xml') - compiler = iface_cache.get_interface('http://foo/Compiler.xml') - self.import_feed(compiler.uri, 'Compiler.xml') - - self.config.freshness = 0 - self.config.network_use = model.network_full - p = Policy('http://foo/Binary.xml', config = self.config) - tasks.wait_for_blocker(p.solve_with_downloads()) - assert p.implementation[foo].id == 'sha1=123' - - # Now ask for source instead - p.requirements.source = True - p.requirements.command = 'compile' - tasks.wait_for_blocker(p.solve_with_downloads()) - assert p.solver.ready, p.solver.get_failure_reason() - assert p.implementation[foo].id == 'sha1=234' # The source - assert p.implementation[compiler].id == 'sha1=345' # A binary needed to compile it - -if __name__ == '__main__': - unittest.main() diff --git a/tests/testrun.py b/tests/testrun.py index 7c05b95..00cfa88 100755 --- a/tests/testrun.py +++ b/tests/testrun.py @@ -9,8 +9,11 @@ sys.path.insert(0, '..') # (testing command support imports zeroinstall.injector._runenv in a sub-process) os.environ['PYTHONPATH'] = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -from zeroinstall.injector import policy, run, namespaces +from zeroinstall.support import tasks +from zeroinstall.injector import run, namespaces from zeroinstall import SafeException +from zeroinstall.injector.requirements import Requirements +from zeroinstall.injector.driver import Driver mydir = os.path.abspath(os.path.dirname(__file__)) local_0launch = os.path.join(os.path.dirname(mydir), '0launch') @@ -30,8 +33,8 @@ class TestRun(BaseTest): if 'SELF_COMMAND' in os.environ: del os.environ['SELF_COMMAND'] - p = policy.Policy(command_feed, config = self.config) - self.config.handler.wait_for_blocker(p.solve_with_downloads()) + p = Driver(requirements = Requirements(command_feed), config = self.config) + tasks.wait_for_blocker(p.solve_with_downloads()) old_stdout = sys.stdout try: sys.stdout = StringIO() @@ -42,7 +45,7 @@ class TestRun(BaseTest): assert 'SELF_COMMAND' in os.environ def testAbsMain(self): - p = policy.Policy(command_feed, config = self.config) + p = Driver(requirements = Requirements(command_feed), config = self.config) self.config.handler.wait_for_blocker(p.solve_with_downloads()) old_stdout = sys.stdout @@ -63,7 +66,7 @@ class TestRun(BaseTest): assert 'not-there' in unicode(ex) def testArgs(self): - p = policy.Policy(runnable, config = self.config) + p = Driver(requirements = Requirements(runnable), config = self.config) self.config.handler.wait_for_blocker(p.solve_with_downloads()) old_stdout = sys.stdout try: @@ -75,7 +78,7 @@ class TestRun(BaseTest): assert 'runner-arg' in out, out def testWrapper(self): - p = policy.Policy(runnable, config = self.config) + p = Driver(requirements = Requirements(runnable), config = self.config) self.config.handler.wait_for_blocker(p.solve_with_downloads()) old_stdout = sys.stdout try: diff --git a/tests/testselections.py b/tests/testselections.py index 79b242d..1483fe6 100755 --- a/tests/testselections.py +++ b/tests/testselections.py @@ -5,7 +5,9 @@ import sys, os import unittest sys.path.insert(0, '..') -from zeroinstall.injector import selections, model, policy, namespaces, qdom, driver, requirements +from zeroinstall.injector import selections, model, namespaces, qdom, requirements +from zeroinstall.injector.requirements import Requirements +from zeroinstall.injector.driver import Driver mydir = os.path.dirname(os.path.abspath(__file__)) runexec = os.path.join(mydir, 'runnable', 'RunExec.xml') @@ -13,17 +15,19 @@ runnable = os.path.join(mydir, 'runnable', 'Runnable.xml') class TestSelections(BaseTest): def testSelections(self): - p = policy.Policy('http://foo/Source.xml', src = True, config = self.config) + requirements = Requirements('http://foo/Source.xml') + requirements.source = True + requirements.command = 'compile' + driver = Driver(requirements = requirements, config = self.config) source = self.config.iface_cache.get_interface('http://foo/Source.xml') compiler = self.config.iface_cache.get_interface('http://foo/Compiler.xml') self.import_feed(source.uri, 'Source.xml') self.import_feed(compiler.uri, 'Compiler.xml') - p.freshness = 0 - p.network_use = model.network_full + self.config.network_use = model.network_full #import logging #logging.getLogger().setLevel(logging.DEBUG) - assert p.need_download() + assert driver.need_download() def assertSel(s): self.assertEqual('http://foo/Source.xml', s.interface) @@ -71,7 +75,8 @@ class TestSelections(BaseTest): self.assertEqual(["sha1=345"], sels[0].digests) - s1 = p.solver.selections + assert driver.solver.ready, driver.solver.get_failure_reason() + s1 = driver.solver.selections s1.selections['http://foo/Source.xml'].attrs['http://namespace foo'] = 'bar' assertSel(s1) @@ -85,10 +90,10 @@ class TestSelections(BaseTest): def testLocalPath(self): # 0launch --get-selections Local.xml iface = os.path.join(mydir, "Local.xml") - p = policy.Policy(iface, config = self.config) - p.need_download() - assert p.ready - s1 = p.solver.selections + driver = Driver(requirements = Requirements(iface), config = self.config) + driver.need_download() + assert driver.solver.ready + s1 = driver.solver.selections xml = s1.toDOM().toxml("utf-8") # Reload selections and check they're the same @@ -105,9 +110,9 @@ class TestSelections(BaseTest): impl.commands["run"] = model.Command(qdom.Element(namespaces.XMLNS_IFACE, 'command', {'path': 'dummy', 'name': 'run'}), None) impl.add_download_source('http://localhost/bar.tgz', 1000, None) feed.implementations = {impl.id: impl} - assert p.need_download() - assert p.ready, p.solver.get_failure_reason() - s1 = p.solver.selections + assert driver.need_download() + assert driver.solver.ready, driver.solver.get_failure_reason() + s1 = driver.solver.selections xml = s1.toDOM().toxml("utf-8") root = qdom.parse(StringIO(xml)) s2 = selections.Selections(root) @@ -119,19 +124,19 @@ class TestSelections(BaseTest): def testCommands(self): iface = os.path.join(mydir, "Command.xml") - p = policy.Policy(iface, config = self.config) - p.need_download() - assert p.ready + driver = Driver(requirements = Requirements(iface), config = self.config) + driver.need_download() + assert driver.solver.ready - impl = p.solver.selections[self.config.iface_cache.get_interface(iface)] + impl = driver.solver.selections[self.config.iface_cache.get_interface(iface)] assert impl.id == 'c' assert impl.main == 'test-gui' dep_impl_uri = impl.commands['run'].requires[0].interface - dep_impl = p.solver.selections[self.config.iface_cache.get_interface(dep_impl_uri)] + dep_impl = driver.solver.selections[self.config.iface_cache.get_interface(dep_impl_uri)] assert dep_impl.id == 'sha1=256' - s1 = p.solver.selections + s1 = driver.solver.selections assert s1.commands[0].path == 'test-gui' xml = s1.toDOM().toxml("utf-8") root = qdom.parse(StringIO(xml)) @@ -148,7 +153,7 @@ class TestSelections(BaseTest): dep_impl = s2.selections[dep_impl_uri] assert dep_impl.id == 'sha1=256' - d = driver.Driver(self.config, requirements.Requirements(runexec)) + d = Driver(self.config, requirements.Requirements(runexec)) need_download = d.need_download() assert need_download == False diff --git a/zeroinstall/injector/policy.py b/zeroinstall/injector/policy.py index 37fa2a1..b33826b 100644 --- a/zeroinstall/injector/policy.py +++ b/zeroinstall/injector/policy.py @@ -48,8 +48,8 @@ class Policy(object): @type config: L{config.Config} Note: all other arguments are deprecated (since 0launch 0.52) """ - #import warnings - #warnings.warn("Policy is deprecated; use a Driver instead", DeprecationWarning, 2) + import warnings + warnings.warn("Policy is deprecated; use a Driver instead", DeprecationWarning, 2) if requirements is None: from zeroinstall.injector.requirements import Requirements requirements = Requirements(root) -- 2.11.4.GIT