2 from basetest
import BaseTest
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
12 logger
= logging
.getLogger()
13 #logger.setLevel(logging.DEBUG)
15 class TestSolver(BaseTest
):
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
)
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
))
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
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
))
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
)
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
)
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
)
108 assert s
.selections
[foo
].machine
== 'x86_64'
109 assert s
.selections
[lib
].machine
== 'x86_64'
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')
133 s
.solve(ranking
, binary_arch
)
136 impl
= s
.selections
[iface
]
137 selected
.append(impl
.get_version() + ' ' + impl
.arch
)
138 impl
.arch
= 'Foo-odd' # prevent reselection
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
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
)
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
)
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
)
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
)
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
)
182 self
.assertEquals('sha1=13', s
.selections
[iface
].id)
184 def check(target_arch
, langs
, expected
):
186 binary_arch
= arch
.get_architecture(None, target_arch
)
187 s
.solve('http://foo/Langs.xml', binary_arch
)
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')
203 locale
.setlocale(locale
.LC_ALL
, '')
205 if __name__
== '__main__':