Fixed GUI to display recursive runners
[zeroinstall.git] / tests / testsolver.py
blobb479e6b920f6a7598133823cd9b10204eaa0adfd
1 #!/usr/bin/env python
2 from basetest import BaseTest
3 import sys, os, locale
4 import unittest
6 sys.path.insert(0, '..')
7 from zeroinstall.zerostore import Stores
8 from zeroinstall.injector import solver, reader, arch, model
9 from zeroinstall.injector.iface_cache import iface_cache
11 import logging
12 logger = logging.getLogger()
13 #logger.setLevel(logging.DEBUG)
15 class TestSolver(BaseTest):
16 def testSimple(self):
17 s = solver.DefaultSolver(model.network_full, iface_cache, Stores())
19 foo = iface_cache.get_interface('http://foo/Binary.xml')
20 reader.update(foo, 'Binary.xml')
21 foo_src = iface_cache.get_interface('http://foo/Source.xml')
22 reader.update(foo_src, 'Source.xml')
23 compiler = iface_cache.get_interface('http://foo/Compiler.xml')
24 reader.update(compiler, 'Compiler.xml')
26 binary_arch = arch.Architecture({None: 1}, {None: 1})
27 assert str(binary_arch).startswith("<Arch")
28 s.solve('http://foo/Binary.xml', binary_arch)
30 assert s.ready
31 assert s.feeds_used == set([foo.uri]), s.feeds_used
32 assert s.selections[foo].id == 'sha1=123'
34 # Now ask for source instead
35 s.solve('http://foo/Binary.xml',
36 arch.SourceArchitecture(binary_arch),
37 command_name = 'compile')
38 assert s.ready, s.get_failure_reason()
39 assert s.feeds_used == set([foo.uri, foo_src.uri, compiler.uri]), s.feeds_used
40 assert s.selections[foo].id == 'sha1=234' # The source
41 assert s.selections[compiler].id == 'sha1=345' # A binary needed to compile it
43 assert not s.details
45 def testDetails(self):
46 s = solver.DefaultSolver(model.network_full, iface_cache, Stores())
48 foo = iface_cache.get_interface('http://foo/Binary.xml')
49 reader.update(foo, 'Binary.xml')
50 foo_src = iface_cache.get_interface('http://foo/Source.xml')
51 reader.update(foo_src, 'Source.xml')
52 compiler = iface_cache.get_interface('http://foo/Compiler.xml')
53 reader.update(compiler, 'Compiler.xml')
55 binary_arch = arch.Architecture({None: 1}, {None: 1})
56 s.record_details = True
57 s.solve('http://foo/Binary.xml', arch.SourceArchitecture(binary_arch), command_name = 'compile')
58 assert s.ready, s.get_failure_reason()
60 foo_src_impls = iface_cache.get_feed(foo_src.uri).implementations
61 foo_impls = iface_cache.get_feed(foo.uri).implementations
62 compiler_impls = iface_cache.get_feed(compiler.uri).implementations
64 assert len(s.details) == 2
65 self.assertEquals([(foo_src_impls['sha1=234'], None),
66 (foo_impls['sha1=123'], 'Unsupported machine type')],
67 sorted(s.details[foo]))
68 assert s.details[compiler] == [(compiler_impls['sha1=345'], None)]
70 def testRecursive(self):
71 s = solver.DefaultSolver(model.network_full, iface_cache, Stores())
73 foo = iface_cache.get_interface('http://foo/Recursive.xml')
74 reader.update(foo, 'Recursive.xml')
76 binary_arch = arch.Architecture({None: 1}, {None: 1})
77 s.record_details = True
78 s.solve('http://foo/Recursive.xml', binary_arch)
79 assert s.ready
81 foo_impls = iface_cache.get_feed(foo.uri).implementations
83 assert len(s.details) == 1
84 assert s.details[foo] == [(foo_impls['sha1=abc'], None)]
86 def testMultiArch(self):
87 s = solver.DefaultSolver(model.network_full, iface_cache, Stores())
89 foo = iface_cache.get_interface('http://foo/MultiArch.xml')
90 reader.update(foo, 'MultiArch.xml')
91 lib = iface_cache.get_interface('http://foo/MultiArchLib.xml')
92 reader.update(lib, 'MultiArchLib.xml')
94 # On an i686 system we can only use the i486 implementation
96 binary_arch = arch.get_architecture('Linux', 'i686')
97 s.solve('http://foo/MultiArch.xml', binary_arch)
98 assert s.ready
99 assert s.selections[foo].machine == 'i486'
100 assert s.selections[lib].machine == 'i486'
102 # On an 64 bit system we could use either, but we prefer the 64
103 # bit implementation. The i486 version of the library is newer,
104 # but we must pick one that is compatible with the main binary.
106 binary_arch = arch.get_architecture('Linux', 'x86_64')
107 s.solve('http://foo/MultiArch.xml', binary_arch)
108 assert s.ready
109 assert s.selections[foo].machine == 'x86_64'
110 assert s.selections[lib].machine == 'x86_64'
112 def testArch(self):
113 host_arch = arch.get_host_architecture()
114 host_arch2 = arch.get_architecture(None, None)
115 self.assertEquals(host_arch.os_ranks, host_arch2.os_ranks)
116 self.assertEquals(host_arch.machine_ranks, host_arch2.machine_ranks)
118 other = arch.get_architecture('FooBar', 'i486')
119 self.assertEquals(2, len(other.os_ranks))
121 assert 'FooBar' in other.os_ranks
122 assert None in other.os_ranks
123 assert 'i486' in other.machine_ranks
124 assert 'ppc' not in other.machine_ranks
126 def testRanking(self):
127 s = solver.DefaultSolver(model.network_full, iface_cache, Stores())
128 ranking = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'Ranking.xml')
129 iface = iface_cache.get_interface(ranking)
131 binary_arch = arch.get_architecture('Linux', 'x86_64')
132 selected = []
133 while True:
134 s.solve(ranking, binary_arch)
135 if not s.ready:
136 break
137 impl = s.selections[iface]
138 selected.append(impl.get_version() + ' ' + impl.arch)
139 impl.arch = 'Foo-odd' # prevent reselection
140 self.assertEquals([
141 '0.2 Linux-i386', # poor arch, but newest version
142 '0.1 Linux-x86_64', # 64-bit is best match for host arch
143 '0.1 Linux-i686', '0.1 Linux-i586', '0.1 Linux-i486'], # ordering of x86 versions
144 selected)
146 def testLangs(self):
147 try:
148 locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
150 s = solver.DefaultSolver(model.network_full, iface_cache, Stores())
151 iface = iface_cache.get_interface('http://foo/Langs.xml')
152 reader.update(iface, 'Langs.xml')
154 # 1 is the oldest, but the only one in our language
155 binary_arch = arch.get_architecture(None, 'arch_1')
156 s.solve('http://foo/Langs.xml', binary_arch)
157 assert s.ready
158 self.assertEquals('sha1=1', s.selections[iface].id)
160 # 6 is the newest, and close enough, even though not
161 # quite the right locale
162 binary_arch = arch.get_architecture(None, 'arch_2')
163 s.solve('http://foo/Langs.xml', binary_arch)
164 assert s.ready
165 self.assertEquals('sha1=6', s.selections[iface].id)
167 # 9 is the newest, although 7 is a closer match
168 binary_arch = arch.get_architecture(None, 'arch_3')
169 s.solve('http://foo/Langs.xml', binary_arch)
170 assert s.ready
171 self.assertEquals('sha1=9', s.selections[iface].id)
173 # 11 is the newest we understand
174 binary_arch = arch.get_architecture(None, 'arch_4')
175 s.solve('http://foo/Langs.xml', binary_arch)
176 assert s.ready
177 self.assertEquals('sha1=11', s.selections[iface].id)
179 # 13 is the newest we understand
180 binary_arch = arch.get_architecture(None, 'arch_5')
181 s.solve('http://foo/Langs.xml', binary_arch)
182 assert s.ready
183 self.assertEquals('sha1=13', s.selections[iface].id)
185 def check(target_arch, langs, expected):
186 s.langs = langs
187 binary_arch = arch.get_architecture(None, target_arch)
188 s.solve('http://foo/Langs.xml', binary_arch)
189 assert s.ready
190 self.assertEquals(expected, s.selections[iface].id)
192 # We don't understand any, so pick the newest
193 check('arch_2', ['es_ES'], 'sha1=6')
195 # These two have the same version number. Choose the
196 # one most appropriate to our country
197 check('arch_6', ['zh_CN'], 'sha1=15')
198 check('arch_6', ['zh_TW'], 'sha1=16')
200 # Same, but one doesn't have a country code
201 check('arch_7', ['bn'], 'sha1=17')
202 check('arch_7', ['bn_IN'], 'sha1=18')
203 finally:
204 locale.setlocale(locale.LC_ALL, '')
206 if __name__ == '__main__':
207 unittest.main()