Updated Swedish translation
[zeroinstall.git] / tests / testsolver.py
blob643f2bcd2c4e7786ce39b83996fcdca62c75e18b
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 assert s.ready
38 assert s.feeds_used == set([foo.uri, foo_src.uri, compiler.uri]), s.feeds_used
39 assert s.selections[foo].id == 'sha1=234' # The source
40 assert s.selections[compiler].id == 'sha1=345' # A binary needed to compile it
42 assert not s.details
44 def testDetails(self):
45 s = solver.DefaultSolver(model.network_full, iface_cache, Stores())
47 foo = iface_cache.get_interface('http://foo/Binary.xml')
48 reader.update(foo, 'Binary.xml')
49 foo_src = iface_cache.get_interface('http://foo/Source.xml')
50 reader.update(foo_src, 'Source.xml')
51 compiler = iface_cache.get_interface('http://foo/Compiler.xml')
52 reader.update(compiler, 'Compiler.xml')
54 binary_arch = arch.Architecture({None: 1}, {None: 1})
55 s.record_details = True
56 s.solve('http://foo/Binary.xml', arch.SourceArchitecture(binary_arch))
57 assert s.ready
59 foo_src_impls = iface_cache.get_feed(foo_src.uri).implementations
60 foo_impls = iface_cache.get_feed(foo.uri).implementations
61 compiler_impls = iface_cache.get_feed(compiler.uri).implementations
63 assert len(s.details) == 2
64 self.assertEquals([(foo_src_impls['sha1=234'], None),
65 (foo_impls['sha1=123'], 'Unsupported machine type')],
66 sorted(s.details[foo]))
67 assert s.details[compiler] == [(compiler_impls['sha1=345'], None)]
69 def testRecursive(self):
70 s = solver.DefaultSolver(model.network_full, iface_cache, Stores())
72 foo = iface_cache.get_interface('http://foo/Recursive.xml')
73 reader.update(foo, 'Recursive.xml')
75 binary_arch = arch.Architecture({None: 1}, {None: 1})
76 s.record_details = True
77 s.solve('http://foo/Recursive.xml', binary_arch)
78 assert s.ready
80 foo_impls = iface_cache.get_feed(foo.uri).implementations
82 assert len(s.details) == 1
83 assert s.details[foo] == [(foo_impls['sha1=abc'], None)]
85 def testMultiArch(self):
86 s = solver.DefaultSolver(model.network_full, iface_cache, Stores())
88 foo = iface_cache.get_interface('http://foo/MultiArch.xml')
89 reader.update(foo, 'MultiArch.xml')
90 lib = iface_cache.get_interface('http://foo/MultiArchLib.xml')
91 reader.update(lib, 'MultiArchLib.xml')
93 # On an i686 system we can only use the i486 implementation
95 binary_arch = arch.get_architecture('Linux', 'i686')
96 s.solve('http://foo/MultiArch.xml', binary_arch)
97 assert s.ready
98 assert s.selections[foo].machine == 'i486'
99 assert s.selections[lib].machine == 'i486'
101 # On an 64 bit system we could use either, but we prefer the 64
102 # bit implementation. The i486 version of the library is newer,
103 # but we must pick one that is compatible with the main binary.
105 binary_arch = arch.get_architecture('Linux', 'x86_64')
106 s.solve('http://foo/MultiArch.xml', binary_arch)
107 assert s.ready
108 assert s.selections[foo].machine == 'x86_64'
109 assert s.selections[lib].machine == 'x86_64'
111 def testArch(self):
112 host_arch = arch.get_host_architecture()
113 host_arch2 = arch.get_architecture(None, None)
114 self.assertEquals(host_arch.os_ranks, host_arch2.os_ranks)
115 self.assertEquals(host_arch.machine_ranks, host_arch2.machine_ranks)
117 other = arch.get_architecture('FooBar', 'i486')
118 self.assertEquals(2, len(other.os_ranks))
120 assert 'FooBar' in other.os_ranks
121 assert None in other.os_ranks
122 assert 'i486' in other.machine_ranks
123 assert 'ppc' not in other.machine_ranks
125 def testRanking(self):
126 s = solver.DefaultSolver(model.network_full, iface_cache, Stores())
127 ranking = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'Ranking.xml')
128 iface = iface_cache.get_interface(ranking)
130 binary_arch = arch.get_architecture('Linux', 'x86_64')
131 selected = []
132 while True:
133 s.solve(ranking, binary_arch)
134 if not s.ready:
135 break
136 impl = s.selections[iface]
137 selected.append(impl.get_version() + ' ' + impl.arch)
138 impl.arch = 'Foo-odd' # prevent reselection
139 self.assertEquals([
140 '0.2 Linux-i386', # poor arch, but newest version
141 '0.1 Linux-x86_64', # 64-bit is best match for host arch
142 '0.1 Linux-i686', '0.1 Linux-i586', '0.1 Linux-i486'], # ordering of x86 versions
143 selected)
145 def testLangs(self):
146 try:
147 locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
149 s = solver.DefaultSolver(model.network_full, iface_cache, Stores())
150 iface = iface_cache.get_interface('http://foo/Langs.xml')
151 reader.update(iface, 'Langs.xml')
153 # 1 is the oldest, but the only one in our language
154 binary_arch = arch.get_architecture(None, 'arch_1')
155 s.solve('http://foo/Langs.xml', binary_arch)
156 assert s.ready
157 self.assertEquals('sha1=1', s.selections[iface].id)
159 # 6 is the newest, and close enough, even though not
160 # quite the right locale
161 binary_arch = arch.get_architecture(None, 'arch_2')
162 s.solve('http://foo/Langs.xml', binary_arch)
163 assert s.ready
164 self.assertEquals('sha1=6', s.selections[iface].id)
166 # 9 is the newest, although 7 is a closer match
167 binary_arch = arch.get_architecture(None, 'arch_3')
168 s.solve('http://foo/Langs.xml', binary_arch)
169 assert s.ready
170 self.assertEquals('sha1=9', s.selections[iface].id)
172 # 11 is the newest we understand
173 binary_arch = arch.get_architecture(None, 'arch_4')
174 s.solve('http://foo/Langs.xml', binary_arch)
175 assert s.ready
176 self.assertEquals('sha1=11', s.selections[iface].id)
178 # 13 is the newest we understand
179 binary_arch = arch.get_architecture(None, 'arch_5')
180 s.solve('http://foo/Langs.xml', binary_arch)
181 assert s.ready
182 self.assertEquals('sha1=13', s.selections[iface].id)
184 def check(target_arch, langs, expected):
185 s.langs = langs
186 binary_arch = arch.get_architecture(None, target_arch)
187 s.solve('http://foo/Langs.xml', binary_arch)
188 assert s.ready
189 self.assertEquals(expected, s.selections[iface].id)
191 # We don't understand any, so pick the newest
192 check('arch_2', ['es_ES'], 'sha1=6')
194 # These two have the same version number. Choose the
195 # one most appropriate to our country
196 check('arch_6', ['zh_CN'], 'sha1=15')
197 check('arch_6', ['zh_TW'], 'sha1=16')
199 # Same, but one doesn't have a country code
200 check('arch_7', ['bn'], 'sha1=17')
201 check('arch_7', ['bn_IN'], 'sha1=18')
202 finally:
203 locale.setlocale(locale.LC_ALL, '')
205 if __name__ == '__main__':
206 unittest.main()