Fixed proxy handling
[zeroinstall.git] / tests / testselections.py
blobff83f0ae8694bc9c14bd81bf05d022063ceebdf1
1 #!/usr/bin/env python
2 from basetest import BaseTest
3 from StringIO import StringIO
4 import sys, os
5 import unittest
7 sys.path.insert(0, '..')
8 from zeroinstall.injector import selections, model, policy, namespaces, qdom, driver, requirements
10 mydir = os.path.dirname(os.path.abspath(__file__))
11 runexec = os.path.join(mydir, 'runnable', 'RunExec.xml')
12 runnable = os.path.join(mydir, 'runnable', 'Runnable.xml')
14 class TestSelections(BaseTest):
15 def testSelections(self):
16 p = policy.Policy('http://foo/Source.xml', src = True, config = self.config)
17 source = self.config.iface_cache.get_interface('http://foo/Source.xml')
18 compiler = self.config.iface_cache.get_interface('http://foo/Compiler.xml')
19 self.import_feed(source.uri, 'Source.xml')
20 self.import_feed(compiler.uri, 'Compiler.xml')
22 p.freshness = 0
23 p.network_use = model.network_full
24 #import logging
25 #logging.getLogger().setLevel(logging.DEBUG)
26 assert p.need_download()
28 def assertSel(s):
29 self.assertEquals('http://foo/Source.xml', s.interface)
30 self.assertEquals(2, len(s.selections))
32 sels = [(sel.interface, sel) for sel in s.selections.values()]
33 sels.sort()
34 sels = [sel for uri,sel in sels]
36 self.assertEquals('http://foo/Compiler.xml', sels[0].interface)
37 self.assertEquals('http://foo/Source.xml', sels[1].interface)
39 self.assertEquals("sha1=345", sels[0].id)
40 self.assertEquals("1.0", sels[0].version)
42 self.assertEquals('sha1=234', sels[1].id)
43 self.assertEquals("1.0", sels[1].version)
44 self.assertEquals("bar", sels[1].attrs['http://namespace foo'])
45 self.assertEquals("1.0", sels[1].attrs['version'])
46 assert 'version-modifier' not in sels[1].attrs
48 self.assertEquals(0, len(sels[0].bindings))
49 self.assertEquals(0, len(sels[0].dependencies))
51 self.assertEquals(1, len(sels[1].bindings))
52 self.assertEquals('.', sels[1].bindings[0].insert)
54 self.assertEquals(1, len(sels[1].dependencies))
55 dep = sels[1].dependencies[0]
56 self.assertEquals('http://foo/Compiler.xml', dep.interface)
57 self.assertEquals(3, len(dep.bindings))
58 self.assertEquals('bin', dep.bindings[0].insert)
59 self.assertEquals('PATH', dep.bindings[0].name)
60 self.assertEquals('prepend', dep.bindings[0].mode)
61 assert dep.bindings[0].separator in ';:'
63 self.assertEquals('bin', dep.bindings[1].value)
64 self.assertEquals('NO_PATH', dep.bindings[1].name)
65 self.assertEquals(',', dep.bindings[1].separator)
67 self.assertEquals('bin', dep.bindings[2].insert)
68 self.assertEquals('BINDIR', dep.bindings[2].name)
69 self.assertEquals('replace', dep.bindings[2].mode)
71 self.assertEquals(["sha1=345"], sels[0].digests)
73 s1 = p.solver.selections
74 s1.selections['http://foo/Source.xml'].attrs['http://namespace foo'] = 'bar'
75 assertSel(s1)
77 xml = s1.toDOM().toxml("utf-8")
78 root = qdom.parse(StringIO(xml))
79 self.assertEquals(namespaces.XMLNS_IFACE, root.uri)
81 s2 = selections.Selections(root)
82 assertSel(s2)
84 def testLocalPath(self):
85 # 0launch --get-selections Local.xml
86 iface = os.path.join(mydir, "Local.xml")
87 p = policy.Policy(iface, config = self.config)
88 p.need_download()
89 assert p.ready
90 s1 = p.solver.selections
91 xml = s1.toDOM().toxml("utf-8")
93 # Reload selections and check they're the same
94 root = qdom.parse(StringIO(xml))
95 s2 = selections.Selections(root)
96 local_path = s2.selections[iface].local_path
97 assert os.path.isdir(local_path), local_path
98 assert not s2.selections[iface].digests, s2.selections[iface].digests
100 # Add a newer implementation and try again
101 feed = self.config.iface_cache.get_feed(iface)
102 impl = model.ZeroInstallImplementation(feed, "foo bar=123", local_path = None)
103 impl.version = model.parse_version('1.0')
104 impl.commands["run"] = model.Command(qdom.Element(namespaces.XMLNS_IFACE, 'command', {'path': 'dummy', 'name': 'run'}), None)
105 impl.add_download_source('http://localhost/bar.tgz', 1000, None)
106 feed.implementations = {impl.id: impl}
107 assert p.need_download()
108 assert p.ready, p.solver.get_failure_reason()
109 s1 = p.solver.selections
110 xml = s1.toDOM().toxml("utf-8")
111 root = qdom.parse(StringIO(xml))
112 s2 = selections.Selections(root)
113 xml = s2.toDOM().toxml("utf-8")
114 qdom.parse(StringIO(xml))
115 assert s2.selections[iface].local_path is None
116 assert not s2.selections[iface].digests, s2.selections[iface].digests
117 assert s2.selections[iface].id == 'foo bar=123'
119 def testCommands(self):
120 iface = os.path.join(mydir, "Command.xml")
121 p = policy.Policy(iface, config = self.config)
122 p.need_download()
123 assert p.ready
125 impl = p.solver.selections[self.config.iface_cache.get_interface(iface)]
126 assert impl.id == 'c'
127 assert impl.main == 'test-gui'
129 dep_impl_uri = impl.commands['run'].requires[0].interface
130 dep_impl = p.solver.selections[self.config.iface_cache.get_interface(dep_impl_uri)]
131 assert dep_impl.id == 'sha1=256'
133 s1 = p.solver.selections
134 assert s1.commands[0].path == 'test-gui'
135 xml = s1.toDOM().toxml("utf-8")
136 root = qdom.parse(StringIO(xml))
137 s2 = selections.Selections(root)
139 assert s2.commands[0].path == 'test-gui'
140 impl = s2.selections[iface]
141 assert impl.id == 'c'
143 assert s2.commands[0].qdom.attrs['http://custom attr'] == 'namespaced'
144 custom_element = s2.commands[0].qdom.childNodes[0]
145 assert custom_element.name == 'child'
147 dep_impl = s2.selections[dep_impl_uri]
148 assert dep_impl.id == 'sha1=256'
150 d = driver.Driver(self.config, requirements.Requirements(runexec))
151 need_download = d.need_download()
152 assert need_download == False
154 xml = d.solver.selections.toDOM().toxml("utf-8")
155 root = qdom.parse(StringIO(xml))
156 s3 = selections.Selections(root)
157 runnable_impl = s3.selections[runnable]
158 assert 'foo' in runnable_impl.commands
159 assert 'run' in runnable_impl.commands
161 def testOldCommands(self):
162 command_feed = os.path.join(mydir, 'old-selections.xml')
163 with open(command_feed) as stream:
164 s1 = selections.Selections(qdom.parse(stream))
165 self.assertEquals("run", s1.command)
166 self.assertEquals(2, len(s1.commands))
167 self.assertEquals("bin/java", s1.commands[1].path)
169 xml = s1.toDOM().toxml("utf-8")
170 root = qdom.parse(StringIO(xml))
171 s2 = selections.Selections(root)
173 self.assertEquals("run", s2.command)
174 self.assertEquals(2, len(s2.commands))
175 self.assertEquals("bin/java", s2.commands[1].path)
178 if __name__ == '__main__':
179 unittest.main()