blob: ddf6e97439b57939c71aa8f6d75406327140e350 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#
2# BitBake Tests for the Fetcher (fetch2/)
3#
4# Copyright (C) 2012 Richard Purdie
5#
Brad Bishopc342db32019-05-15 21:57:59 -04006# SPDX-License-Identifier: GPL-2.0-only
Patrick Williamsc124f4f2015-09-15 14:41:29 -05007#
8
9import unittest
Brad Bishop316dfdd2018-06-25 12:45:53 -040010import hashlib
Patrick Williamsc124f4f2015-09-15 14:41:29 -050011import tempfile
Patrick Williamsc0f7c042017-02-23 20:41:17 -060012import collections
Patrick Williamsc124f4f2015-09-15 14:41:29 -050013import os
14from bb.fetch2 import URI
15from bb.fetch2 import FetchMethod
16import bb
Andrew Geissler82c905d2020-04-13 13:39:40 -050017from bb.tests.support.httpserver import HTTPService
Patrick Williamsc124f4f2015-09-15 14:41:29 -050018
Brad Bishopd7bf8c12018-02-25 22:55:05 -050019def skipIfNoNetwork():
20 if os.environ.get("BB_SKIP_NETTESTS") == "yes":
21 return unittest.skip("Network tests being skipped")
22 return lambda f: f
23
Patrick Williamsc124f4f2015-09-15 14:41:29 -050024class URITest(unittest.TestCase):
25 test_uris = {
26 "http://www.google.com/index.html" : {
27 'uri': 'http://www.google.com/index.html',
28 'scheme': 'http',
29 'hostname': 'www.google.com',
30 'port': None,
31 'hostport': 'www.google.com',
32 'path': '/index.html',
33 'userinfo': '',
34 'username': '',
35 'password': '',
36 'params': {},
37 'query': {},
38 'relative': False
39 },
40 "http://www.google.com/index.html;param1=value1" : {
41 'uri': 'http://www.google.com/index.html;param1=value1',
42 'scheme': 'http',
43 'hostname': 'www.google.com',
44 'port': None,
45 'hostport': 'www.google.com',
46 'path': '/index.html',
47 'userinfo': '',
48 'username': '',
49 'password': '',
50 'params': {
51 'param1': 'value1'
52 },
53 'query': {},
54 'relative': False
55 },
56 "http://www.example.org/index.html?param1=value1" : {
57 'uri': 'http://www.example.org/index.html?param1=value1',
58 'scheme': 'http',
59 'hostname': 'www.example.org',
60 'port': None,
61 'hostport': 'www.example.org',
62 'path': '/index.html',
63 'userinfo': '',
64 'username': '',
65 'password': '',
66 'params': {},
67 'query': {
68 'param1': 'value1'
69 },
70 'relative': False
71 },
72 "http://www.example.org/index.html?qparam1=qvalue1;param2=value2" : {
73 'uri': 'http://www.example.org/index.html?qparam1=qvalue1;param2=value2',
74 'scheme': 'http',
75 'hostname': 'www.example.org',
76 'port': None,
77 'hostport': 'www.example.org',
78 'path': '/index.html',
79 'userinfo': '',
80 'username': '',
81 'password': '',
82 'params': {
83 'param2': 'value2'
84 },
85 'query': {
86 'qparam1': 'qvalue1'
87 },
88 'relative': False
89 },
Andrew Geisslerd1e89492021-02-12 15:35:20 -060090 # Check that trailing semicolons are handled correctly
91 "http://www.example.org/index.html?qparam1=qvalue1;param2=value2;" : {
92 'uri': 'http://www.example.org/index.html?qparam1=qvalue1;param2=value2',
93 'scheme': 'http',
94 'hostname': 'www.example.org',
95 'port': None,
96 'hostport': 'www.example.org',
97 'path': '/index.html',
98 'userinfo': '',
99 'username': '',
100 'password': '',
101 'params': {
102 'param2': 'value2'
103 },
104 'query': {
105 'qparam1': 'qvalue1'
106 },
107 'relative': False
108 },
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500109 "http://www.example.com:8080/index.html" : {
110 'uri': 'http://www.example.com:8080/index.html',
111 'scheme': 'http',
112 'hostname': 'www.example.com',
113 'port': 8080,
114 'hostport': 'www.example.com:8080',
115 'path': '/index.html',
116 'userinfo': '',
117 'username': '',
118 'password': '',
119 'params': {},
120 'query': {},
121 'relative': False
122 },
123 "cvs://anoncvs@cvs.handhelds.org/cvs;module=familiar/dist/ipkg" : {
124 'uri': 'cvs://anoncvs@cvs.handhelds.org/cvs;module=familiar/dist/ipkg',
125 'scheme': 'cvs',
126 'hostname': 'cvs.handhelds.org',
127 'port': None,
128 'hostport': 'cvs.handhelds.org',
129 'path': '/cvs',
130 'userinfo': 'anoncvs',
131 'username': 'anoncvs',
132 'password': '',
133 'params': {
134 'module': 'familiar/dist/ipkg'
135 },
136 'query': {},
137 'relative': False
138 },
139 "cvs://anoncvs:anonymous@cvs.handhelds.org/cvs;tag=V0-99-81;module=familiar/dist/ipkg": {
140 'uri': 'cvs://anoncvs:anonymous@cvs.handhelds.org/cvs;tag=V0-99-81;module=familiar/dist/ipkg',
141 'scheme': 'cvs',
142 'hostname': 'cvs.handhelds.org',
143 'port': None,
144 'hostport': 'cvs.handhelds.org',
145 'path': '/cvs',
146 'userinfo': 'anoncvs:anonymous',
147 'username': 'anoncvs',
148 'password': 'anonymous',
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600149 'params': collections.OrderedDict([
150 ('tag', 'V0-99-81'),
151 ('module', 'familiar/dist/ipkg')
152 ]),
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500153 'query': {},
154 'relative': False
155 },
156 "file://example.diff": { # NOTE: Not RFC compliant!
157 'uri': 'file:example.diff',
158 'scheme': 'file',
159 'hostname': '',
160 'port': None,
161 'hostport': '',
162 'path': 'example.diff',
163 'userinfo': '',
164 'username': '',
165 'password': '',
166 'params': {},
167 'query': {},
168 'relative': True
169 },
170 "file:example.diff": { # NOTE: RFC compliant version of the former
171 'uri': 'file:example.diff',
172 'scheme': 'file',
173 'hostname': '',
174 'port': None,
175 'hostport': '',
176 'path': 'example.diff',
177 'userinfo': '',
178 'userinfo': '',
179 'username': '',
180 'password': '',
181 'params': {},
182 'query': {},
183 'relative': True
184 },
185 "file:///tmp/example.diff": {
186 'uri': 'file:///tmp/example.diff',
187 'scheme': 'file',
188 'hostname': '',
189 'port': None,
190 'hostport': '',
191 'path': '/tmp/example.diff',
192 'userinfo': '',
193 'userinfo': '',
194 'username': '',
195 'password': '',
196 'params': {},
197 'query': {},
198 'relative': False
199 },
200 "git:///path/example.git": {
201 'uri': 'git:///path/example.git',
202 'scheme': 'git',
203 'hostname': '',
204 'port': None,
205 'hostport': '',
206 'path': '/path/example.git',
207 'userinfo': '',
208 'userinfo': '',
209 'username': '',
210 'password': '',
211 'params': {},
212 'query': {},
213 'relative': False
214 },
215 "git:path/example.git": {
216 'uri': 'git:path/example.git',
217 'scheme': 'git',
218 'hostname': '',
219 'port': None,
220 'hostport': '',
221 'path': 'path/example.git',
222 'userinfo': '',
223 'userinfo': '',
224 'username': '',
225 'password': '',
226 'params': {},
227 'query': {},
228 'relative': True
229 },
230 "git://example.net/path/example.git": {
231 'uri': 'git://example.net/path/example.git',
232 'scheme': 'git',
233 'hostname': 'example.net',
234 'port': None,
235 'hostport': 'example.net',
236 'path': '/path/example.git',
237 'userinfo': '',
238 'userinfo': '',
239 'username': '',
240 'password': '',
241 'params': {},
242 'query': {},
243 'relative': False
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500244 },
Andrew Geisslerc3d88e42020-10-02 09:45:00 -0500245 "git://tfs-example.org:22/tfs/example%20path/example.git": {
246 'uri': 'git://tfs-example.org:22/tfs/example%20path/example.git',
247 'scheme': 'git',
248 'hostname': 'tfs-example.org',
249 'port': 22,
250 'hostport': 'tfs-example.org:22',
251 'path': '/tfs/example path/example.git',
252 'userinfo': '',
253 'userinfo': '',
254 'username': '',
255 'password': '',
256 'params': {},
257 'query': {},
258 'relative': False
259 },
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500260 "http://somesite.net;someparam=1": {
261 'uri': 'http://somesite.net;someparam=1',
262 'scheme': 'http',
263 'hostname': 'somesite.net',
264 'port': None,
265 'hostport': 'somesite.net',
266 'path': '',
267 'userinfo': '',
268 'userinfo': '',
269 'username': '',
270 'password': '',
271 'params': {"someparam" : "1"},
272 'query': {},
273 'relative': False
274 },
275 "file://somelocation;someparam=1": {
276 'uri': 'file:somelocation;someparam=1',
277 'scheme': 'file',
278 'hostname': '',
279 'port': None,
280 'hostport': '',
281 'path': 'somelocation',
282 'userinfo': '',
283 'userinfo': '',
284 'username': '',
285 'password': '',
286 'params': {"someparam" : "1"},
287 'query': {},
288 'relative': True
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500289 }
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500290
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500291 }
292
293 def test_uri(self):
294 for test_uri, ref in self.test_uris.items():
295 uri = URI(test_uri)
296
297 self.assertEqual(str(uri), ref['uri'])
298
299 # expected attributes
300 self.assertEqual(uri.scheme, ref['scheme'])
301
302 self.assertEqual(uri.userinfo, ref['userinfo'])
303 self.assertEqual(uri.username, ref['username'])
304 self.assertEqual(uri.password, ref['password'])
305
306 self.assertEqual(uri.hostname, ref['hostname'])
307 self.assertEqual(uri.port, ref['port'])
308 self.assertEqual(uri.hostport, ref['hostport'])
309
310 self.assertEqual(uri.path, ref['path'])
311 self.assertEqual(uri.params, ref['params'])
312
313 self.assertEqual(uri.relative, ref['relative'])
314
315 def test_dict(self):
316 for test in self.test_uris.values():
317 uri = URI()
318
319 self.assertEqual(uri.scheme, '')
320 self.assertEqual(uri.userinfo, '')
321 self.assertEqual(uri.username, '')
322 self.assertEqual(uri.password, '')
323 self.assertEqual(uri.hostname, '')
324 self.assertEqual(uri.port, None)
325 self.assertEqual(uri.path, '')
326 self.assertEqual(uri.params, {})
327
328
329 uri.scheme = test['scheme']
330 self.assertEqual(uri.scheme, test['scheme'])
331
332 uri.userinfo = test['userinfo']
333 self.assertEqual(uri.userinfo, test['userinfo'])
334 self.assertEqual(uri.username, test['username'])
335 self.assertEqual(uri.password, test['password'])
336
337 # make sure changing the values doesn't do anything unexpected
338 uri.username = 'changeme'
339 self.assertEqual(uri.username, 'changeme')
340 self.assertEqual(uri.password, test['password'])
341 uri.password = 'insecure'
342 self.assertEqual(uri.username, 'changeme')
343 self.assertEqual(uri.password, 'insecure')
344
345 # reset back after our trickery
346 uri.userinfo = test['userinfo']
347 self.assertEqual(uri.userinfo, test['userinfo'])
348 self.assertEqual(uri.username, test['username'])
349 self.assertEqual(uri.password, test['password'])
350
351 uri.hostname = test['hostname']
352 self.assertEqual(uri.hostname, test['hostname'])
353 self.assertEqual(uri.hostport, test['hostname'])
354
355 uri.port = test['port']
356 self.assertEqual(uri.port, test['port'])
357 self.assertEqual(uri.hostport, test['hostport'])
358
359 uri.path = test['path']
360 self.assertEqual(uri.path, test['path'])
361
362 uri.params = test['params']
363 self.assertEqual(uri.params, test['params'])
364
365 uri.query = test['query']
366 self.assertEqual(uri.query, test['query'])
367
368 self.assertEqual(str(uri), test['uri'])
369
370 uri.params = {}
371 self.assertEqual(uri.params, {})
372 self.assertEqual(str(uri), (str(uri).split(";"))[0])
373
374class FetcherTest(unittest.TestCase):
375
376 def setUp(self):
377 self.origdir = os.getcwd()
378 self.d = bb.data.init()
379 self.tempdir = tempfile.mkdtemp()
380 self.dldir = os.path.join(self.tempdir, "download")
381 os.mkdir(self.dldir)
382 self.d.setVar("DL_DIR", self.dldir)
383 self.unpackdir = os.path.join(self.tempdir, "unpacked")
384 os.mkdir(self.unpackdir)
385 persistdir = os.path.join(self.tempdir, "persistdata")
386 self.d.setVar("PERSISTENT_DIR", persistdir)
387
388 def tearDown(self):
389 os.chdir(self.origdir)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600390 if os.environ.get("BB_TMPDIR_NOCLEAN") == "yes":
391 print("Not cleaning up %s. Please remove manually." % self.tempdir)
392 else:
393 bb.utils.prunedir(self.tempdir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500394
395class MirrorUriTest(FetcherTest):
396
397 replaceuris = {
398 ("git://git.invalid.infradead.org/mtd-utils.git;tag=1234567890123456789012345678901234567890", "git://.*/.*", "http://somewhere.org/somedir/")
399 : "http://somewhere.org/somedir/git2_git.invalid.infradead.org.mtd-utils.git.tar.gz",
400 ("git://git.invalid.infradead.org/mtd-utils.git;tag=1234567890123456789012345678901234567890", "git://.*/([^/]+/)*([^/]*)", "git://somewhere.org/somedir/\\2;protocol=http")
401 : "git://somewhere.org/somedir/mtd-utils.git;tag=1234567890123456789012345678901234567890;protocol=http",
402 ("git://git.invalid.infradead.org/foo/mtd-utils.git;tag=1234567890123456789012345678901234567890", "git://.*/([^/]+/)*([^/]*)", "git://somewhere.org/somedir/\\2;protocol=http")
403 : "git://somewhere.org/somedir/mtd-utils.git;tag=1234567890123456789012345678901234567890;protocol=http",
404 ("git://git.invalid.infradead.org/foo/mtd-utils.git;tag=1234567890123456789012345678901234567890", "git://.*/([^/]+/)*([^/]*)", "git://somewhere.org/\\2;protocol=http")
405 : "git://somewhere.org/mtd-utils.git;tag=1234567890123456789012345678901234567890;protocol=http",
406 ("git://someserver.org/bitbake;tag=1234567890123456789012345678901234567890", "git://someserver.org/bitbake", "git://git.openembedded.org/bitbake")
407 : "git://git.openembedded.org/bitbake;tag=1234567890123456789012345678901234567890",
408 ("file://sstate-xyz.tgz", "file://.*", "file:///somewhere/1234/sstate-cache")
409 : "file:///somewhere/1234/sstate-cache/sstate-xyz.tgz",
410 ("file://sstate-xyz.tgz", "file://.*", "file:///somewhere/1234/sstate-cache/")
411 : "file:///somewhere/1234/sstate-cache/sstate-xyz.tgz",
412 ("http://somewhere.org/somedir1/somedir2/somefile_1.2.3.tar.gz", "http://.*/.*", "http://somewhere2.org/somedir3")
413 : "http://somewhere2.org/somedir3/somefile_1.2.3.tar.gz",
414 ("http://somewhere.org/somedir1/somefile_1.2.3.tar.gz", "http://somewhere.org/somedir1/somefile_1.2.3.tar.gz", "http://somewhere2.org/somedir3/somefile_1.2.3.tar.gz")
415 : "http://somewhere2.org/somedir3/somefile_1.2.3.tar.gz",
416 ("http://www.apache.org/dist/subversion/subversion-1.7.1.tar.bz2", "http://www.apache.org/dist", "http://archive.apache.org/dist")
417 : "http://archive.apache.org/dist/subversion/subversion-1.7.1.tar.bz2",
418 ("http://www.apache.org/dist/subversion/subversion-1.7.1.tar.bz2", "http://.*/.*", "file:///somepath/downloads/")
419 : "file:///somepath/downloads/subversion-1.7.1.tar.bz2",
420 ("git://git.invalid.infradead.org/mtd-utils.git;tag=1234567890123456789012345678901234567890", "git://.*/.*", "git://somewhere.org/somedir/BASENAME;protocol=http")
421 : "git://somewhere.org/somedir/mtd-utils.git;tag=1234567890123456789012345678901234567890;protocol=http",
422 ("git://git.invalid.infradead.org/foo/mtd-utils.git;tag=1234567890123456789012345678901234567890", "git://.*/.*", "git://somewhere.org/somedir/BASENAME;protocol=http")
423 : "git://somewhere.org/somedir/mtd-utils.git;tag=1234567890123456789012345678901234567890;protocol=http",
424 ("git://git.invalid.infradead.org/foo/mtd-utils.git;tag=1234567890123456789012345678901234567890", "git://.*/.*", "git://somewhere.org/somedir/MIRRORNAME;protocol=http")
425 : "git://somewhere.org/somedir/git.invalid.infradead.org.foo.mtd-utils.git;tag=1234567890123456789012345678901234567890;protocol=http",
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800426 ("http://somewhere.org/somedir1/somedir2/somefile_1.2.3.tar.gz", "http://.*/.*", "http://somewhere2.org")
427 : "http://somewhere2.org/somefile_1.2.3.tar.gz",
428 ("http://somewhere.org/somedir1/somedir2/somefile_1.2.3.tar.gz", "http://.*/.*", "http://somewhere2.org/")
429 : "http://somewhere2.org/somefile_1.2.3.tar.gz",
430 ("git://someserver.org/bitbake;tag=1234567890123456789012345678901234567890;branch=master", "git://someserver.org/bitbake;branch=master", "git://git.openembedded.org/bitbake;protocol=http")
431 : "git://git.openembedded.org/bitbake;tag=1234567890123456789012345678901234567890;branch=master;protocol=http",
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500432
433 #Renaming files doesn't work
434 #("http://somewhere.org/somedir1/somefile_1.2.3.tar.gz", "http://somewhere.org/somedir1/somefile_1.2.3.tar.gz", "http://somewhere2.org/somedir3/somefile_2.3.4.tar.gz") : "http://somewhere2.org/somedir3/somefile_2.3.4.tar.gz"
435 #("file://sstate-xyz.tgz", "file://.*/.*", "file:///somewhere/1234/sstate-cache") : "file:///somewhere/1234/sstate-cache/sstate-xyz.tgz",
436 }
437
438 mirrorvar = "http://.*/.* file:///somepath/downloads/ \n" \
439 "git://someserver.org/bitbake git://git.openembedded.org/bitbake \n" \
440 "https://.*/.* file:///someotherpath/downloads/ \n" \
441 "http://.*/.* file:///someotherpath/downloads/ \n"
442
443 def test_urireplace(self):
444 for k, v in self.replaceuris.items():
445 ud = bb.fetch.FetchData(k[0], self.d)
446 ud.setup_localpath(self.d)
447 mirrors = bb.fetch2.mirror_from_string("%s %s" % (k[1], k[2]))
448 newuris, uds = bb.fetch2.build_mirroruris(ud, mirrors, self.d)
449 self.assertEqual([v], newuris)
450
451 def test_urilist1(self):
452 fetcher = bb.fetch.FetchData("http://downloads.yoctoproject.org/releases/bitbake/bitbake-1.0.tar.gz", self.d)
453 mirrors = bb.fetch2.mirror_from_string(self.mirrorvar)
454 uris, uds = bb.fetch2.build_mirroruris(fetcher, mirrors, self.d)
455 self.assertEqual(uris, ['file:///somepath/downloads/bitbake-1.0.tar.gz', 'file:///someotherpath/downloads/bitbake-1.0.tar.gz'])
456
457 def test_urilist2(self):
458 # Catch https:// -> files:// bug
459 fetcher = bb.fetch.FetchData("https://downloads.yoctoproject.org/releases/bitbake/bitbake-1.0.tar.gz", self.d)
460 mirrors = bb.fetch2.mirror_from_string(self.mirrorvar)
461 uris, uds = bb.fetch2.build_mirroruris(fetcher, mirrors, self.d)
462 self.assertEqual(uris, ['file:///someotherpath/downloads/bitbake-1.0.tar.gz'])
463
464 def test_mirror_of_mirror(self):
465 # Test if mirror of a mirror works
466 mirrorvar = self.mirrorvar + " http://.*/.* http://otherdownloads.yoctoproject.org/downloads/ \n"
467 mirrorvar = mirrorvar + " http://otherdownloads.yoctoproject.org/.* http://downloads2.yoctoproject.org/downloads/ \n"
468 fetcher = bb.fetch.FetchData("http://downloads.yoctoproject.org/releases/bitbake/bitbake-1.0.tar.gz", self.d)
469 mirrors = bb.fetch2.mirror_from_string(mirrorvar)
470 uris, uds = bb.fetch2.build_mirroruris(fetcher, mirrors, self.d)
471 self.assertEqual(uris, ['file:///somepath/downloads/bitbake-1.0.tar.gz',
472 'file:///someotherpath/downloads/bitbake-1.0.tar.gz',
473 'http://otherdownloads.yoctoproject.org/downloads/bitbake-1.0.tar.gz',
474 'http://downloads2.yoctoproject.org/downloads/bitbake-1.0.tar.gz'])
475
Patrick Williamsd7e96312015-09-22 08:09:05 -0500476 recmirrorvar = "https://.*/[^/]* http://AAAA/A/A/A/ \n" \
477 "https://.*/[^/]* https://BBBB/B/B/B/ \n"
478
479 def test_recursive(self):
480 fetcher = bb.fetch.FetchData("https://downloads.yoctoproject.org/releases/bitbake/bitbake-1.0.tar.gz", self.d)
481 mirrors = bb.fetch2.mirror_from_string(self.recmirrorvar)
482 uris, uds = bb.fetch2.build_mirroruris(fetcher, mirrors, self.d)
483 self.assertEqual(uris, ['http://AAAA/A/A/A/bitbake/bitbake-1.0.tar.gz',
484 'https://BBBB/B/B/B/bitbake/bitbake-1.0.tar.gz',
485 'http://AAAA/A/A/A/B/B/bitbake/bitbake-1.0.tar.gz'])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500486
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800487
488class GitDownloadDirectoryNamingTest(FetcherTest):
489 def setUp(self):
490 super(GitDownloadDirectoryNamingTest, self).setUp()
491 self.recipe_url = "git://git.openembedded.org/bitbake"
492 self.recipe_dir = "git.openembedded.org.bitbake"
493 self.mirror_url = "git://github.com/openembedded/bitbake.git"
494 self.mirror_dir = "github.com.openembedded.bitbake.git"
495
496 self.d.setVar('SRCREV', '82ea737a0b42a8b53e11c9cde141e9e9c0bd8c40')
497
498 def setup_mirror_rewrite(self):
499 self.d.setVar("PREMIRRORS", self.recipe_url + " " + self.mirror_url + " \n")
500
501 @skipIfNoNetwork()
502 def test_that_directory_is_named_after_recipe_url_when_no_mirroring_is_used(self):
503 self.setup_mirror_rewrite()
504 fetcher = bb.fetch.Fetch([self.recipe_url], self.d)
505
506 fetcher.download()
507
508 dir = os.listdir(self.dldir + "/git2")
509 self.assertIn(self.recipe_dir, dir)
510
511 @skipIfNoNetwork()
512 def test_that_directory_exists_for_mirrored_url_and_recipe_url_when_mirroring_is_used(self):
513 self.setup_mirror_rewrite()
514 fetcher = bb.fetch.Fetch([self.recipe_url], self.d)
515
516 fetcher.download()
517
518 dir = os.listdir(self.dldir + "/git2")
519 self.assertIn(self.mirror_dir, dir)
520 self.assertIn(self.recipe_dir, dir)
521
522 @skipIfNoNetwork()
523 def test_that_recipe_directory_and_mirrored_directory_exists_when_mirroring_is_used_and_the_mirrored_directory_already_exists(self):
524 self.setup_mirror_rewrite()
525 fetcher = bb.fetch.Fetch([self.mirror_url], self.d)
526 fetcher.download()
527 fetcher = bb.fetch.Fetch([self.recipe_url], self.d)
528
529 fetcher.download()
530
531 dir = os.listdir(self.dldir + "/git2")
532 self.assertIn(self.mirror_dir, dir)
533 self.assertIn(self.recipe_dir, dir)
534
535
536class TarballNamingTest(FetcherTest):
537 def setUp(self):
538 super(TarballNamingTest, self).setUp()
539 self.recipe_url = "git://git.openembedded.org/bitbake"
540 self.recipe_tarball = "git2_git.openembedded.org.bitbake.tar.gz"
541 self.mirror_url = "git://github.com/openembedded/bitbake.git"
542 self.mirror_tarball = "git2_github.com.openembedded.bitbake.git.tar.gz"
543
544 self.d.setVar('BB_GENERATE_MIRROR_TARBALLS', '1')
545 self.d.setVar('SRCREV', '82ea737a0b42a8b53e11c9cde141e9e9c0bd8c40')
546
547 def setup_mirror_rewrite(self):
548 self.d.setVar("PREMIRRORS", self.recipe_url + " " + self.mirror_url + " \n")
549
550 @skipIfNoNetwork()
551 def test_that_the_recipe_tarball_is_created_when_no_mirroring_is_used(self):
552 fetcher = bb.fetch.Fetch([self.recipe_url], self.d)
553
554 fetcher.download()
555
556 dir = os.listdir(self.dldir)
557 self.assertIn(self.recipe_tarball, dir)
558
559 @skipIfNoNetwork()
560 def test_that_the_mirror_tarball_is_created_when_mirroring_is_used(self):
561 self.setup_mirror_rewrite()
562 fetcher = bb.fetch.Fetch([self.recipe_url], self.d)
563
564 fetcher.download()
565
566 dir = os.listdir(self.dldir)
567 self.assertIn(self.mirror_tarball, dir)
568
569
570class GitShallowTarballNamingTest(FetcherTest):
571 def setUp(self):
572 super(GitShallowTarballNamingTest, self).setUp()
573 self.recipe_url = "git://git.openembedded.org/bitbake"
574 self.recipe_tarball = "gitshallow_git.openembedded.org.bitbake_82ea737-1_master.tar.gz"
575 self.mirror_url = "git://github.com/openembedded/bitbake.git"
576 self.mirror_tarball = "gitshallow_github.com.openembedded.bitbake.git_82ea737-1_master.tar.gz"
577
578 self.d.setVar('BB_GIT_SHALLOW', '1')
579 self.d.setVar('BB_GENERATE_SHALLOW_TARBALLS', '1')
580 self.d.setVar('SRCREV', '82ea737a0b42a8b53e11c9cde141e9e9c0bd8c40')
581
582 def setup_mirror_rewrite(self):
583 self.d.setVar("PREMIRRORS", self.recipe_url + " " + self.mirror_url + " \n")
584
585 @skipIfNoNetwork()
586 def test_that_the_tarball_is_named_after_recipe_url_when_no_mirroring_is_used(self):
587 fetcher = bb.fetch.Fetch([self.recipe_url], self.d)
588
589 fetcher.download()
590
591 dir = os.listdir(self.dldir)
592 self.assertIn(self.recipe_tarball, dir)
593
594 @skipIfNoNetwork()
595 def test_that_the_mirror_tarball_is_created_when_mirroring_is_used(self):
596 self.setup_mirror_rewrite()
597 fetcher = bb.fetch.Fetch([self.recipe_url], self.d)
598
599 fetcher.download()
600
601 dir = os.listdir(self.dldir)
602 self.assertIn(self.mirror_tarball, dir)
603
604
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500605class FetcherLocalTest(FetcherTest):
606 def setUp(self):
607 def touch(fn):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600608 with open(fn, 'a'):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500609 os.utime(fn, None)
610
611 super(FetcherLocalTest, self).setUp()
612 self.localsrcdir = os.path.join(self.tempdir, 'localsrc')
613 os.makedirs(self.localsrcdir)
614 touch(os.path.join(self.localsrcdir, 'a'))
615 touch(os.path.join(self.localsrcdir, 'b'))
616 os.makedirs(os.path.join(self.localsrcdir, 'dir'))
617 touch(os.path.join(self.localsrcdir, 'dir', 'c'))
618 touch(os.path.join(self.localsrcdir, 'dir', 'd'))
619 os.makedirs(os.path.join(self.localsrcdir, 'dir', 'subdir'))
620 touch(os.path.join(self.localsrcdir, 'dir', 'subdir', 'e'))
Andrew Geisslerc3d88e42020-10-02 09:45:00 -0500621 touch(os.path.join(self.localsrcdir, r'backslash\x2dsystemd-unit.device'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500622 self.d.setVar("FILESPATH", self.localsrcdir)
623
624 def fetchUnpack(self, uris):
625 fetcher = bb.fetch.Fetch(uris, self.d)
626 fetcher.download()
627 fetcher.unpack(self.unpackdir)
628 flst = []
629 for root, dirs, files in os.walk(self.unpackdir):
630 for f in files:
631 flst.append(os.path.relpath(os.path.join(root, f), self.unpackdir))
632 flst.sort()
633 return flst
634
635 def test_local(self):
636 tree = self.fetchUnpack(['file://a', 'file://dir/c'])
637 self.assertEqual(tree, ['a', 'dir/c'])
638
Andrew Geisslerc3d88e42020-10-02 09:45:00 -0500639 def test_local_backslash(self):
640 tree = self.fetchUnpack([r'file://backslash\x2dsystemd-unit.device'])
641 self.assertEqual(tree, [r'backslash\x2dsystemd-unit.device'])
642
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500643 def test_local_wildcard(self):
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500644 with self.assertRaises(bb.fetch2.ParameterError):
645 tree = self.fetchUnpack(['file://a', 'file://dir/*'])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500646
647 def test_local_dir(self):
648 tree = self.fetchUnpack(['file://a', 'file://dir'])
649 self.assertEqual(tree, ['a', 'dir/c', 'dir/d', 'dir/subdir/e'])
650
651 def test_local_subdir(self):
652 tree = self.fetchUnpack(['file://dir/subdir'])
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500653 self.assertEqual(tree, ['dir/subdir/e'])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500654
655 def test_local_subdir_file(self):
656 tree = self.fetchUnpack(['file://dir/subdir/e'])
657 self.assertEqual(tree, ['dir/subdir/e'])
658
659 def test_local_subdirparam(self):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500660 tree = self.fetchUnpack(['file://a;subdir=bar', 'file://dir;subdir=foo/moo'])
661 self.assertEqual(tree, ['bar/a', 'foo/moo/dir/c', 'foo/moo/dir/d', 'foo/moo/dir/subdir/e'])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500662
663 def test_local_deepsubdirparam(self):
664 tree = self.fetchUnpack(['file://dir/subdir/e;subdir=bar'])
665 self.assertEqual(tree, ['bar/dir/subdir/e'])
666
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600667 def test_local_absolutedir(self):
668 # Unpacking to an absolute path that is a subdirectory of the root
669 # should work
670 tree = self.fetchUnpack(['file://a;subdir=%s' % os.path.join(self.unpackdir, 'bar')])
671
672 # Unpacking to an absolute path outside of the root should fail
673 with self.assertRaises(bb.fetch2.UnpackError):
674 self.fetchUnpack(['file://a;subdir=/bin/sh'])
675
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600676 def test_local_gitfetch_usehead(self):
677 # Create dummy local Git repo
678 src_dir = tempfile.mkdtemp(dir=self.tempdir,
679 prefix='gitfetch_localusehead_')
680 src_dir = os.path.abspath(src_dir)
681 bb.process.run("git init", cwd=src_dir)
682 bb.process.run("git commit --allow-empty -m'Dummy commit'",
683 cwd=src_dir)
684 # Use other branch than master
685 bb.process.run("git checkout -b my-devel", cwd=src_dir)
686 bb.process.run("git commit --allow-empty -m'Dummy commit 2'",
687 cwd=src_dir)
688 stdout = bb.process.run("git rev-parse HEAD", cwd=src_dir)
689 orig_rev = stdout[0].strip()
690
691 # Fetch and check revision
692 self.d.setVar("SRCREV", "AUTOINC")
693 url = "git://" + src_dir + ";protocol=file;usehead=1"
694 fetcher = bb.fetch.Fetch([url], self.d)
695 fetcher.download()
696 fetcher.unpack(self.unpackdir)
697 stdout = bb.process.run("git rev-parse HEAD",
698 cwd=os.path.join(self.unpackdir, 'git'))
699 unpack_rev = stdout[0].strip()
700 self.assertEqual(orig_rev, unpack_rev)
701
702 def test_local_gitfetch_usehead_withname(self):
703 # Create dummy local Git repo
704 src_dir = tempfile.mkdtemp(dir=self.tempdir,
705 prefix='gitfetch_localusehead_')
706 src_dir = os.path.abspath(src_dir)
707 bb.process.run("git init", cwd=src_dir)
708 bb.process.run("git commit --allow-empty -m'Dummy commit'",
709 cwd=src_dir)
710 # Use other branch than master
711 bb.process.run("git checkout -b my-devel", cwd=src_dir)
712 bb.process.run("git commit --allow-empty -m'Dummy commit 2'",
713 cwd=src_dir)
714 stdout = bb.process.run("git rev-parse HEAD", cwd=src_dir)
715 orig_rev = stdout[0].strip()
716
717 # Fetch and check revision
718 self.d.setVar("SRCREV", "AUTOINC")
719 url = "git://" + src_dir + ";protocol=file;usehead=1;name=newName"
720 fetcher = bb.fetch.Fetch([url], self.d)
721 fetcher.download()
722 fetcher.unpack(self.unpackdir)
723 stdout = bb.process.run("git rev-parse HEAD",
724 cwd=os.path.join(self.unpackdir, 'git'))
725 unpack_rev = stdout[0].strip()
726 self.assertEqual(orig_rev, unpack_rev)
727
Brad Bishop316dfdd2018-06-25 12:45:53 -0400728class FetcherNoNetworkTest(FetcherTest):
729 def setUp(self):
730 super().setUp()
731 # all test cases are based on not having network
732 self.d.setVar("BB_NO_NETWORK", "1")
733
734 def test_missing(self):
735 string = "this is a test file\n".encode("utf-8")
736 self.d.setVarFlag("SRC_URI", "md5sum", hashlib.md5(string).hexdigest())
737 self.d.setVarFlag("SRC_URI", "sha256sum", hashlib.sha256(string).hexdigest())
738
739 self.assertFalse(os.path.exists(os.path.join(self.dldir, "test-file.tar.gz")))
740 self.assertFalse(os.path.exists(os.path.join(self.dldir, "test-file.tar.gz.done")))
741 fetcher = bb.fetch.Fetch(["http://invalid.yoctoproject.org/test-file.tar.gz"], self.d)
742 with self.assertRaises(bb.fetch2.NetworkAccess):
743 fetcher.download()
744
745 def test_valid_missing_donestamp(self):
746 # create the file in the download directory with correct hash
747 string = "this is a test file\n".encode("utf-8")
748 with open(os.path.join(self.dldir, "test-file.tar.gz"), "wb") as f:
749 f.write(string)
750
751 self.d.setVarFlag("SRC_URI", "md5sum", hashlib.md5(string).hexdigest())
752 self.d.setVarFlag("SRC_URI", "sha256sum", hashlib.sha256(string).hexdigest())
753
754 self.assertTrue(os.path.exists(os.path.join(self.dldir, "test-file.tar.gz")))
755 self.assertFalse(os.path.exists(os.path.join(self.dldir, "test-file.tar.gz.done")))
756 fetcher = bb.fetch.Fetch(["http://invalid.yoctoproject.org/test-file.tar.gz"], self.d)
757 fetcher.download()
758 self.assertTrue(os.path.exists(os.path.join(self.dldir, "test-file.tar.gz.done")))
759
760 def test_invalid_missing_donestamp(self):
761 # create an invalid file in the download directory with incorrect hash
762 string = "this is a test file\n".encode("utf-8")
763 with open(os.path.join(self.dldir, "test-file.tar.gz"), "wb"):
764 pass
765
766 self.d.setVarFlag("SRC_URI", "md5sum", hashlib.md5(string).hexdigest())
767 self.d.setVarFlag("SRC_URI", "sha256sum", hashlib.sha256(string).hexdigest())
768
769 self.assertTrue(os.path.exists(os.path.join(self.dldir, "test-file.tar.gz")))
770 self.assertFalse(os.path.exists(os.path.join(self.dldir, "test-file.tar.gz.done")))
771 fetcher = bb.fetch.Fetch(["http://invalid.yoctoproject.org/test-file.tar.gz"], self.d)
772 with self.assertRaises(bb.fetch2.NetworkAccess):
773 fetcher.download()
774 # the existing file should not exist or should have be moved to "bad-checksum"
775 self.assertFalse(os.path.exists(os.path.join(self.dldir, "test-file.tar.gz")))
776
777 def test_nochecksums_missing(self):
778 self.assertFalse(os.path.exists(os.path.join(self.dldir, "test-file.tar.gz")))
779 self.assertFalse(os.path.exists(os.path.join(self.dldir, "test-file.tar.gz.done")))
780 # ssh fetch does not support checksums
781 fetcher = bb.fetch.Fetch(["ssh://invalid@invalid.yoctoproject.org/test-file.tar.gz"], self.d)
782 # attempts to download with missing donestamp
783 with self.assertRaises(bb.fetch2.NetworkAccess):
784 fetcher.download()
785
786 def test_nochecksums_missing_donestamp(self):
787 # create a file in the download directory
788 with open(os.path.join(self.dldir, "test-file.tar.gz"), "wb"):
789 pass
790
791 self.assertTrue(os.path.exists(os.path.join(self.dldir, "test-file.tar.gz")))
792 self.assertFalse(os.path.exists(os.path.join(self.dldir, "test-file.tar.gz.done")))
793 # ssh fetch does not support checksums
794 fetcher = bb.fetch.Fetch(["ssh://invalid@invalid.yoctoproject.org/test-file.tar.gz"], self.d)
795 # attempts to download with missing donestamp
796 with self.assertRaises(bb.fetch2.NetworkAccess):
797 fetcher.download()
798
799 def test_nochecksums_has_donestamp(self):
800 # create a file in the download directory with the donestamp
801 with open(os.path.join(self.dldir, "test-file.tar.gz"), "wb"):
802 pass
803 with open(os.path.join(self.dldir, "test-file.tar.gz.done"), "wb"):
804 pass
805
806 self.assertTrue(os.path.exists(os.path.join(self.dldir, "test-file.tar.gz")))
807 self.assertTrue(os.path.exists(os.path.join(self.dldir, "test-file.tar.gz.done")))
808 # ssh fetch does not support checksums
809 fetcher = bb.fetch.Fetch(["ssh://invalid@invalid.yoctoproject.org/test-file.tar.gz"], self.d)
810 # should not fetch
811 fetcher.download()
812 # both files should still exist
813 self.assertTrue(os.path.exists(os.path.join(self.dldir, "test-file.tar.gz")))
814 self.assertTrue(os.path.exists(os.path.join(self.dldir, "test-file.tar.gz.done")))
815
816 def test_nochecksums_missing_has_donestamp(self):
817 # create a file in the download directory with the donestamp
818 with open(os.path.join(self.dldir, "test-file.tar.gz.done"), "wb"):
819 pass
820
821 self.assertFalse(os.path.exists(os.path.join(self.dldir, "test-file.tar.gz")))
822 self.assertTrue(os.path.exists(os.path.join(self.dldir, "test-file.tar.gz.done")))
823 # ssh fetch does not support checksums
824 fetcher = bb.fetch.Fetch(["ssh://invalid@invalid.yoctoproject.org/test-file.tar.gz"], self.d)
825 with self.assertRaises(bb.fetch2.NetworkAccess):
826 fetcher.download()
827 # both files should still exist
828 self.assertFalse(os.path.exists(os.path.join(self.dldir, "test-file.tar.gz")))
829 self.assertFalse(os.path.exists(os.path.join(self.dldir, "test-file.tar.gz.done")))
830
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500831class FetcherNetworkTest(FetcherTest):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500832 @skipIfNoNetwork()
833 def test_fetch(self):
834 fetcher = bb.fetch.Fetch(["http://downloads.yoctoproject.org/releases/bitbake/bitbake-1.0.tar.gz", "http://downloads.yoctoproject.org/releases/bitbake/bitbake-1.1.tar.gz"], self.d)
835 fetcher.download()
836 self.assertEqual(os.path.getsize(self.dldir + "/bitbake-1.0.tar.gz"), 57749)
837 self.assertEqual(os.path.getsize(self.dldir + "/bitbake-1.1.tar.gz"), 57892)
838 self.d.setVar("BB_NO_NETWORK", "1")
839 fetcher = bb.fetch.Fetch(["http://downloads.yoctoproject.org/releases/bitbake/bitbake-1.0.tar.gz", "http://downloads.yoctoproject.org/releases/bitbake/bitbake-1.1.tar.gz"], self.d)
840 fetcher.download()
841 fetcher.unpack(self.unpackdir)
842 self.assertEqual(len(os.listdir(self.unpackdir + "/bitbake-1.0/")), 9)
843 self.assertEqual(len(os.listdir(self.unpackdir + "/bitbake-1.1/")), 9)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500844
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500845 @skipIfNoNetwork()
846 def test_fetch_mirror(self):
847 self.d.setVar("MIRRORS", "http://.*/.* http://downloads.yoctoproject.org/releases/bitbake")
848 fetcher = bb.fetch.Fetch(["http://invalid.yoctoproject.org/releases/bitbake/bitbake-1.0.tar.gz"], self.d)
849 fetcher.download()
850 self.assertEqual(os.path.getsize(self.dldir + "/bitbake-1.0.tar.gz"), 57749)
851
852 @skipIfNoNetwork()
853 def test_fetch_mirror_of_mirror(self):
854 self.d.setVar("MIRRORS", "http://.*/.* http://invalid2.yoctoproject.org/ \n http://invalid2.yoctoproject.org/.* http://downloads.yoctoproject.org/releases/bitbake")
855 fetcher = bb.fetch.Fetch(["http://invalid.yoctoproject.org/releases/bitbake/bitbake-1.0.tar.gz"], self.d)
856 fetcher.download()
857 self.assertEqual(os.path.getsize(self.dldir + "/bitbake-1.0.tar.gz"), 57749)
858
859 @skipIfNoNetwork()
860 def test_fetch_file_mirror_of_mirror(self):
861 self.d.setVar("MIRRORS", "http://.*/.* file:///some1where/ \n file:///some1where/.* file://some2where/ \n file://some2where/.* http://downloads.yoctoproject.org/releases/bitbake")
862 fetcher = bb.fetch.Fetch(["http://invalid.yoctoproject.org/releases/bitbake/bitbake-1.0.tar.gz"], self.d)
863 os.mkdir(self.dldir + "/some2where")
864 fetcher.download()
865 self.assertEqual(os.path.getsize(self.dldir + "/bitbake-1.0.tar.gz"), 57749)
866
867 @skipIfNoNetwork()
868 def test_fetch_premirror(self):
869 self.d.setVar("PREMIRRORS", "http://.*/.* http://downloads.yoctoproject.org/releases/bitbake")
870 fetcher = bb.fetch.Fetch(["http://invalid.yoctoproject.org/releases/bitbake/bitbake-1.0.tar.gz"], self.d)
871 fetcher.download()
872 self.assertEqual(os.path.getsize(self.dldir + "/bitbake-1.0.tar.gz"), 57749)
873
874 @skipIfNoNetwork()
875 def gitfetcher(self, url1, url2):
876 def checkrevision(self, fetcher):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500877 fetcher.unpack(self.unpackdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500878 revision = bb.process.run("git rev-parse HEAD", shell=True, cwd=self.unpackdir + "/git")[0].strip()
879 self.assertEqual(revision, "270a05b0b4ba0959fe0624d2a4885d7b70426da5")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500880
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500881 self.d.setVar("BB_GENERATE_MIRROR_TARBALLS", "1")
882 self.d.setVar("SRCREV", "270a05b0b4ba0959fe0624d2a4885d7b70426da5")
883 fetcher = bb.fetch.Fetch([url1], self.d)
884 fetcher.download()
885 checkrevision(self, fetcher)
886 # Wipe out the dldir clone and the unpacked source, turn off the network and check mirror tarball works
887 bb.utils.prunedir(self.dldir + "/git2/")
888 bb.utils.prunedir(self.unpackdir)
889 self.d.setVar("BB_NO_NETWORK", "1")
890 fetcher = bb.fetch.Fetch([url2], self.d)
891 fetcher.download()
892 checkrevision(self, fetcher)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500893
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500894 @skipIfNoNetwork()
895 def test_gitfetch(self):
896 url1 = url2 = "git://git.openembedded.org/bitbake"
897 self.gitfetcher(url1, url2)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500898
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500899 @skipIfNoNetwork()
900 def test_gitfetch_goodsrcrev(self):
901 # SRCREV is set but matches rev= parameter
902 url1 = url2 = "git://git.openembedded.org/bitbake;rev=270a05b0b4ba0959fe0624d2a4885d7b70426da5"
903 self.gitfetcher(url1, url2)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500904
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500905 @skipIfNoNetwork()
906 def test_gitfetch_badsrcrev(self):
907 # SRCREV is set but does not match rev= parameter
908 url1 = url2 = "git://git.openembedded.org/bitbake;rev=dead05b0b4ba0959fe0624d2a4885d7b70426da5"
909 self.assertRaises(bb.fetch.FetchError, self.gitfetcher, url1, url2)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500910
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500911 @skipIfNoNetwork()
912 def test_gitfetch_tagandrev(self):
913 # SRCREV is set but does not match rev= parameter
914 url1 = url2 = "git://git.openembedded.org/bitbake;rev=270a05b0b4ba0959fe0624d2a4885d7b70426da5;tag=270a05b0b4ba0959fe0624d2a4885d7b70426da5"
915 self.assertRaises(bb.fetch.FetchError, self.gitfetcher, url1, url2)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500916
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500917 @skipIfNoNetwork()
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600918 def test_gitfetch_usehead(self):
919 # Since self.gitfetcher() sets SRCREV we expect this to override
920 # `usehead=1' and instead fetch the specified SRCREV. See
921 # test_local_gitfetch_usehead() for a positive use of the usehead
922 # feature.
923 url = "git://git.openembedded.org/bitbake;usehead=1"
924 self.assertRaises(bb.fetch.ParameterError, self.gitfetcher, url, url)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500925
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500926 @skipIfNoNetwork()
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600927 def test_gitfetch_usehead_withname(self):
928 # Since self.gitfetcher() sets SRCREV we expect this to override
929 # `usehead=1' and instead fetch the specified SRCREV. See
930 # test_local_gitfetch_usehead() for a positive use of the usehead
931 # feature.
932 url = "git://git.openembedded.org/bitbake;usehead=1;name=newName"
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500933 self.assertRaises(bb.fetch.ParameterError, self.gitfetcher, url, url)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500934
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500935 @skipIfNoNetwork()
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800936 def test_gitfetch_finds_local_tarball_for_mirrored_url_when_previous_downloaded_by_the_recipe_url(self):
937 recipeurl = "git://git.openembedded.org/bitbake"
938 mirrorurl = "git://someserver.org/bitbake"
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500939 self.d.setVar("PREMIRRORS", "git://someserver.org/bitbake git://git.openembedded.org/bitbake \n")
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800940 self.gitfetcher(recipeurl, mirrorurl)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500941
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500942 @skipIfNoNetwork()
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800943 def test_gitfetch_finds_local_tarball_when_previous_downloaded_from_a_premirror(self):
944 recipeurl = "git://someserver.org/bitbake"
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500945 self.d.setVar("PREMIRRORS", "git://someserver.org/bitbake git://git.openembedded.org/bitbake \n")
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800946 self.gitfetcher(recipeurl, recipeurl)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500947
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500948 @skipIfNoNetwork()
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800949 def test_gitfetch_finds_local_repository_when_premirror_rewrites_the_recipe_url(self):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500950 realurl = "git://git.openembedded.org/bitbake"
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800951 recipeurl = "git://someserver.org/bitbake"
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500952 self.sourcedir = self.unpackdir.replace("unpacked", "sourcemirror.git")
953 os.chdir(self.tempdir)
954 bb.process.run("git clone %s %s 2> /dev/null" % (realurl, self.sourcedir), shell=True)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800955 self.d.setVar("PREMIRRORS", "%s git://%s;protocol=file \n" % (recipeurl, self.sourcedir))
956 self.gitfetcher(recipeurl, recipeurl)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600957
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500958 @skipIfNoNetwork()
959 def test_git_submodule(self):
Brad Bishopf8caae32019-03-25 13:13:56 -0400960 # URL with ssh submodules
961 url = "gitsm://git.yoctoproject.org/git-submodule-test;branch=ssh-gitsm-tests;rev=049da4a6cb198d7c0302e9e8b243a1443cb809a7"
962 # Original URL (comment this if you have ssh access to git.yoctoproject.org)
963 url = "gitsm://git.yoctoproject.org/git-submodule-test;branch=master;rev=a2885dd7d25380d23627e7544b7bbb55014b16ee"
964 fetcher = bb.fetch.Fetch([url], self.d)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500965 fetcher.download()
966 # Previous cwd has been deleted
967 os.chdir(os.path.dirname(self.unpackdir))
968 fetcher.unpack(self.unpackdir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500969
Brad Bishopf8caae32019-03-25 13:13:56 -0400970 repo_path = os.path.join(self.tempdir, 'unpacked', 'git')
971 self.assertTrue(os.path.exists(repo_path), msg='Unpacked repository missing')
972 self.assertTrue(os.path.exists(os.path.join(repo_path, 'bitbake')), msg='bitbake submodule missing')
973 self.assertFalse(os.path.exists(os.path.join(repo_path, 'na')), msg='uninitialized submodule present')
974
975 # Only when we're running the extended test with a submodule's submodule, can we check this.
976 if os.path.exists(os.path.join(repo_path, 'bitbake-gitsm-test1')):
977 self.assertTrue(os.path.exists(os.path.join(repo_path, 'bitbake-gitsm-test1', 'bitbake')), msg='submodule of submodule missing')
978
Brad Bishop96ff1982019-08-19 13:50:42 -0400979 @skipIfNoNetwork()
Brad Bishopf8caae32019-03-25 13:13:56 -0400980 def test_git_submodule_dbus_broker(self):
981 # The following external repositories have show failures in fetch and unpack operations
982 # We want to avoid regressions!
Andrew Geissler4c19ea12020-10-27 13:52:24 -0500983 url = "gitsm://github.com/bus1/dbus-broker;protocol=git;rev=fc874afa0992d0c75ec25acb43d344679f0ee7d2;branch=main"
Brad Bishopf8caae32019-03-25 13:13:56 -0400984 fetcher = bb.fetch.Fetch([url], self.d)
985 fetcher.download()
986 # Previous cwd has been deleted
987 os.chdir(os.path.dirname(self.unpackdir))
988 fetcher.unpack(self.unpackdir)
989
990 repo_path = os.path.join(self.tempdir, 'unpacked', 'git')
991 self.assertTrue(os.path.exists(os.path.join(repo_path, '.git/modules/subprojects/c-dvar/config')), msg='Missing submodule config "subprojects/c-dvar"')
992 self.assertTrue(os.path.exists(os.path.join(repo_path, '.git/modules/subprojects/c-list/config')), msg='Missing submodule config "subprojects/c-list"')
993 self.assertTrue(os.path.exists(os.path.join(repo_path, '.git/modules/subprojects/c-rbtree/config')), msg='Missing submodule config "subprojects/c-rbtree"')
994 self.assertTrue(os.path.exists(os.path.join(repo_path, '.git/modules/subprojects/c-sundry/config')), msg='Missing submodule config "subprojects/c-sundry"')
995 self.assertTrue(os.path.exists(os.path.join(repo_path, '.git/modules/subprojects/c-utf8/config')), msg='Missing submodule config "subprojects/c-utf8"')
996
Brad Bishop96ff1982019-08-19 13:50:42 -0400997 @skipIfNoNetwork()
Brad Bishopf8caae32019-03-25 13:13:56 -0400998 def test_git_submodule_CLI11(self):
999 url = "gitsm://github.com/CLIUtils/CLI11;protocol=git;rev=bd4dc911847d0cde7a6b41dfa626a85aab213baf"
1000 fetcher = bb.fetch.Fetch([url], self.d)
1001 fetcher.download()
1002 # Previous cwd has been deleted
1003 os.chdir(os.path.dirname(self.unpackdir))
1004 fetcher.unpack(self.unpackdir)
1005
1006 repo_path = os.path.join(self.tempdir, 'unpacked', 'git')
1007 self.assertTrue(os.path.exists(os.path.join(repo_path, '.git/modules/extern/googletest/config')), msg='Missing submodule config "extern/googletest"')
1008 self.assertTrue(os.path.exists(os.path.join(repo_path, '.git/modules/extern/json/config')), msg='Missing submodule config "extern/json"')
1009 self.assertTrue(os.path.exists(os.path.join(repo_path, '.git/modules/extern/sanitizers/config')), msg='Missing submodule config "extern/sanitizers"')
1010
Brad Bishop96ff1982019-08-19 13:50:42 -04001011 @skipIfNoNetwork()
Brad Bishop19323692019-04-05 15:28:33 -04001012 def test_git_submodule_update_CLI11(self):
1013 """ Prevent regression on update detection not finding missing submodule, or modules without needed commits """
1014 url = "gitsm://github.com/CLIUtils/CLI11;protocol=git;rev=cf6a99fa69aaefe477cc52e3ef4a7d2d7fa40714"
1015 fetcher = bb.fetch.Fetch([url], self.d)
1016 fetcher.download()
1017
1018 # CLI11 that pulls in a newer nlohmann-json
1019 url = "gitsm://github.com/CLIUtils/CLI11;protocol=git;rev=49ac989a9527ee9bb496de9ded7b4872c2e0e5ca"
1020 fetcher = bb.fetch.Fetch([url], self.d)
1021 fetcher.download()
1022 # Previous cwd has been deleted
1023 os.chdir(os.path.dirname(self.unpackdir))
1024 fetcher.unpack(self.unpackdir)
1025
1026 repo_path = os.path.join(self.tempdir, 'unpacked', 'git')
1027 self.assertTrue(os.path.exists(os.path.join(repo_path, '.git/modules/extern/googletest/config')), msg='Missing submodule config "extern/googletest"')
1028 self.assertTrue(os.path.exists(os.path.join(repo_path, '.git/modules/extern/json/config')), msg='Missing submodule config "extern/json"')
1029 self.assertTrue(os.path.exists(os.path.join(repo_path, '.git/modules/extern/sanitizers/config')), msg='Missing submodule config "extern/sanitizers"')
1030
Brad Bishop96ff1982019-08-19 13:50:42 -04001031 @skipIfNoNetwork()
Brad Bishopf8caae32019-03-25 13:13:56 -04001032 def test_git_submodule_aktualizr(self):
1033 url = "gitsm://github.com/advancedtelematic/aktualizr;branch=master;protocol=git;rev=d00d1a04cc2366d1a5f143b84b9f507f8bd32c44"
1034 fetcher = bb.fetch.Fetch([url], self.d)
1035 fetcher.download()
1036 # Previous cwd has been deleted
1037 os.chdir(os.path.dirname(self.unpackdir))
1038 fetcher.unpack(self.unpackdir)
1039
1040 repo_path = os.path.join(self.tempdir, 'unpacked', 'git')
1041 self.assertTrue(os.path.exists(os.path.join(repo_path, '.git/modules/partial/extern/isotp-c/config')), msg='Missing submodule config "partial/extern/isotp-c/config"')
1042 self.assertTrue(os.path.exists(os.path.join(repo_path, '.git/modules/partial/extern/isotp-c/modules/deps/bitfield-c/config')), msg='Missing submodule config "partial/extern/isotp-c/modules/deps/bitfield-c/config"')
1043 self.assertTrue(os.path.exists(os.path.join(repo_path, 'partial/extern/isotp-c/deps/bitfield-c/.git')), msg="Submodule of submodule isotp-c did not unpack properly")
1044 self.assertTrue(os.path.exists(os.path.join(repo_path, '.git/modules/tests/tuf-test-vectors/config')), msg='Missing submodule config "tests/tuf-test-vectors/config"')
1045 self.assertTrue(os.path.exists(os.path.join(repo_path, '.git/modules/third_party/googletest/config')), msg='Missing submodule config "third_party/googletest/config"')
1046 self.assertTrue(os.path.exists(os.path.join(repo_path, '.git/modules/third_party/HdrHistogram_c/config')), msg='Missing submodule config "third_party/HdrHistogram_c/config"')
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001047
Brad Bishop96ff1982019-08-19 13:50:42 -04001048 @skipIfNoNetwork()
Brad Bishop393846f2019-05-20 12:24:11 -04001049 def test_git_submodule_iotedge(self):
1050 """ Prevent regression on deeply nested submodules not being checked out properly, even though they were fetched. """
1051
1052 # This repository also has submodules where the module (name), path and url do not align
1053 url = "gitsm://github.com/azure/iotedge.git;protocol=git;rev=d76e0316c6f324345d77c48a83ce836d09392699"
1054 fetcher = bb.fetch.Fetch([url], self.d)
1055 fetcher.download()
1056 # Previous cwd has been deleted
1057 os.chdir(os.path.dirname(self.unpackdir))
1058 fetcher.unpack(self.unpackdir)
1059
1060 repo_path = os.path.join(self.tempdir, 'unpacked', 'git')
1061
1062 self.assertTrue(os.path.exists(os.path.join(repo_path, 'edgelet/hsm-sys/azure-iot-hsm-c/deps/c-shared/README.md')), msg='Missing submodule checkout')
1063 self.assertTrue(os.path.exists(os.path.join(repo_path, 'edgelet/hsm-sys/azure-iot-hsm-c/deps/c-shared/testtools/ctest/README.md')), msg='Missing submodule checkout')
1064 self.assertTrue(os.path.exists(os.path.join(repo_path, 'edgelet/hsm-sys/azure-iot-hsm-c/deps/c-shared/testtools/testrunner/readme.md')), msg='Missing submodule checkout')
1065 self.assertTrue(os.path.exists(os.path.join(repo_path, 'edgelet/hsm-sys/azure-iot-hsm-c/deps/c-shared/testtools/umock-c/readme.md')), msg='Missing submodule checkout')
1066 self.assertTrue(os.path.exists(os.path.join(repo_path, 'edgelet/hsm-sys/azure-iot-hsm-c/deps/c-shared/testtools/umock-c/deps/ctest/README.md')), msg='Missing submodule checkout')
1067 self.assertTrue(os.path.exists(os.path.join(repo_path, 'edgelet/hsm-sys/azure-iot-hsm-c/deps/c-shared/testtools/umock-c/deps/testrunner/readme.md')), msg='Missing submodule checkout')
1068 self.assertTrue(os.path.exists(os.path.join(repo_path, 'edgelet/hsm-sys/azure-iot-hsm-c/deps/utpm/README.md')), msg='Missing submodule checkout')
1069 self.assertTrue(os.path.exists(os.path.join(repo_path, 'edgelet/hsm-sys/azure-iot-hsm-c/deps/utpm/deps/c-utility/README.md')), msg='Missing submodule checkout')
1070 self.assertTrue(os.path.exists(os.path.join(repo_path, 'edgelet/hsm-sys/azure-iot-hsm-c/deps/utpm/deps/c-utility/testtools/ctest/README.md')), msg='Missing submodule checkout')
1071 self.assertTrue(os.path.exists(os.path.join(repo_path, 'edgelet/hsm-sys/azure-iot-hsm-c/deps/utpm/deps/c-utility/testtools/testrunner/readme.md')), msg='Missing submodule checkout')
1072 self.assertTrue(os.path.exists(os.path.join(repo_path, 'edgelet/hsm-sys/azure-iot-hsm-c/deps/utpm/deps/c-utility/testtools/umock-c/readme.md')), msg='Missing submodule checkout')
1073 self.assertTrue(os.path.exists(os.path.join(repo_path, 'edgelet/hsm-sys/azure-iot-hsm-c/deps/utpm/deps/c-utility/testtools/umock-c/deps/ctest/README.md')), msg='Missing submodule checkout')
1074 self.assertTrue(os.path.exists(os.path.join(repo_path, 'edgelet/hsm-sys/azure-iot-hsm-c/deps/utpm/deps/c-utility/testtools/umock-c/deps/testrunner/readme.md')), msg='Missing submodule checkout')
1075
Brad Bishop15ae2502019-06-18 21:44:24 -04001076class SVNTest(FetcherTest):
1077 def skipIfNoSvn():
1078 import shutil
1079 if not shutil.which("svn"):
1080 return unittest.skip("svn not installed, tests being skipped")
1081
1082 if not shutil.which("svnadmin"):
1083 return unittest.skip("svnadmin not installed, tests being skipped")
1084
1085 return lambda f: f
1086
1087 @skipIfNoSvn()
1088 def setUp(self):
1089 """ Create a local repository """
1090
1091 super(SVNTest, self).setUp()
1092
1093 # Create something we can fetch
1094 src_dir = tempfile.mkdtemp(dir=self.tempdir,
1095 prefix='svnfetch_srcdir_')
1096 src_dir = os.path.abspath(src_dir)
1097 bb.process.run("echo readme > README.md", cwd=src_dir)
1098
1099 # Store it in a local SVN repository
1100 repo_dir = tempfile.mkdtemp(dir=self.tempdir,
1101 prefix='svnfetch_localrepo_')
1102 repo_dir = os.path.abspath(repo_dir)
1103 bb.process.run("svnadmin create project", cwd=repo_dir)
1104
1105 self.repo_url = "file://%s/project" % repo_dir
1106 bb.process.run("svn import --non-interactive -m 'Initial import' %s %s/trunk" % (src_dir, self.repo_url),
1107 cwd=repo_dir)
1108
1109 bb.process.run("svn co %s svnfetch_co" % self.repo_url, cwd=self.tempdir)
1110 # Github will emulate SVN. Use this to check if we're downloding...
Andrew Geissler475cb722020-07-10 16:00:51 -05001111 bb.process.run("svn propset svn:externals 'bitbake svn://vcs.pcre.org/pcre2/code' .",
Brad Bishop15ae2502019-06-18 21:44:24 -04001112 cwd=os.path.join(self.tempdir, 'svnfetch_co', 'trunk'))
1113 bb.process.run("svn commit --non-interactive -m 'Add external'",
1114 cwd=os.path.join(self.tempdir, 'svnfetch_co', 'trunk'))
1115
1116 self.src_dir = src_dir
1117 self.repo_dir = repo_dir
1118
1119 @skipIfNoSvn()
1120 def tearDown(self):
1121 os.chdir(self.origdir)
1122 if os.environ.get("BB_TMPDIR_NOCLEAN") == "yes":
1123 print("Not cleaning up %s. Please remove manually." % self.tempdir)
1124 else:
1125 bb.utils.prunedir(self.tempdir)
1126
1127 @skipIfNoSvn()
1128 @skipIfNoNetwork()
1129 def test_noexternal_svn(self):
1130 # Always match the rev count from setUp (currently rev 2)
1131 url = "svn://%s;module=trunk;protocol=file;rev=2" % self.repo_url.replace('file://', '')
1132 fetcher = bb.fetch.Fetch([url], self.d)
1133 fetcher.download()
1134 os.chdir(os.path.dirname(self.unpackdir))
1135 fetcher.unpack(self.unpackdir)
1136
1137 self.assertTrue(os.path.exists(os.path.join(self.unpackdir, 'trunk')), msg="Missing trunk")
1138 self.assertTrue(os.path.exists(os.path.join(self.unpackdir, 'trunk', 'README.md')), msg="Missing contents")
1139 self.assertFalse(os.path.exists(os.path.join(self.unpackdir, 'trunk/bitbake/trunk')), msg="External dir should NOT exist")
1140 self.assertFalse(os.path.exists(os.path.join(self.unpackdir, 'trunk/bitbake/trunk', 'README')), msg="External README should NOT exit")
1141
1142 @skipIfNoSvn()
1143 def test_external_svn(self):
1144 # Always match the rev count from setUp (currently rev 2)
1145 url = "svn://%s;module=trunk;protocol=file;externals=allowed;rev=2" % self.repo_url.replace('file://', '')
1146 fetcher = bb.fetch.Fetch([url], self.d)
1147 fetcher.download()
1148 os.chdir(os.path.dirname(self.unpackdir))
1149 fetcher.unpack(self.unpackdir)
1150
1151 self.assertTrue(os.path.exists(os.path.join(self.unpackdir, 'trunk')), msg="Missing trunk")
1152 self.assertTrue(os.path.exists(os.path.join(self.unpackdir, 'trunk', 'README.md')), msg="Missing contents")
1153 self.assertTrue(os.path.exists(os.path.join(self.unpackdir, 'trunk/bitbake/trunk')), msg="External dir should exist")
1154 self.assertTrue(os.path.exists(os.path.join(self.unpackdir, 'trunk/bitbake/trunk', 'README')), msg="External README should exit")
1155
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001156class TrustedNetworksTest(FetcherTest):
1157 def test_trusted_network(self):
1158 # Ensure trusted_network returns False when the host IS in the list.
1159 url = "git://Someserver.org/foo;rev=1"
1160 self.d.setVar("BB_ALLOWED_NETWORKS", "server1.org someserver.org server2.org server3.org")
1161 self.assertTrue(bb.fetch.trusted_network(self.d, url))
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001162
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001163 def test_wild_trusted_network(self):
1164 # Ensure trusted_network returns true when the *.host IS in the list.
1165 url = "git://Someserver.org/foo;rev=1"
1166 self.d.setVar("BB_ALLOWED_NETWORKS", "server1.org *.someserver.org server2.org server3.org")
1167 self.assertTrue(bb.fetch.trusted_network(self.d, url))
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001168
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001169 def test_prefix_wild_trusted_network(self):
1170 # Ensure trusted_network returns true when the prefix matches *.host.
1171 url = "git://git.Someserver.org/foo;rev=1"
1172 self.d.setVar("BB_ALLOWED_NETWORKS", "server1.org *.someserver.org server2.org server3.org")
1173 self.assertTrue(bb.fetch.trusted_network(self.d, url))
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001174
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001175 def test_two_prefix_wild_trusted_network(self):
1176 # Ensure trusted_network returns true when the prefix matches *.host.
1177 url = "git://something.git.Someserver.org/foo;rev=1"
1178 self.d.setVar("BB_ALLOWED_NETWORKS", "server1.org *.someserver.org server2.org server3.org")
1179 self.assertTrue(bb.fetch.trusted_network(self.d, url))
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001180
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001181 def test_port_trusted_network(self):
1182 # Ensure trusted_network returns True, even if the url specifies a port.
1183 url = "git://someserver.org:8080/foo;rev=1"
1184 self.d.setVar("BB_ALLOWED_NETWORKS", "someserver.org")
1185 self.assertTrue(bb.fetch.trusted_network(self.d, url))
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001186
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001187 def test_untrusted_network(self):
1188 # Ensure trusted_network returns False when the host is NOT in the list.
1189 url = "git://someserver.org/foo;rev=1"
1190 self.d.setVar("BB_ALLOWED_NETWORKS", "server1.org server2.org server3.org")
1191 self.assertFalse(bb.fetch.trusted_network(self.d, url))
1192
1193 def test_wild_untrusted_network(self):
1194 # Ensure trusted_network returns False when the host is NOT in the list.
1195 url = "git://*.someserver.org/foo;rev=1"
1196 self.d.setVar("BB_ALLOWED_NETWORKS", "server1.org server2.org server3.org")
1197 self.assertFalse(bb.fetch.trusted_network(self.d, url))
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001198
1199class URLHandle(unittest.TestCase):
1200
1201 datatable = {
1202 "http://www.google.com/index.html" : ('http', 'www.google.com', '/index.html', '', '', {}),
1203 "cvs://anoncvs@cvs.handhelds.org/cvs;module=familiar/dist/ipkg" : ('cvs', 'cvs.handhelds.org', '/cvs', 'anoncvs', '', {'module': 'familiar/dist/ipkg'}),
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001204 "cvs://anoncvs:anonymous@cvs.handhelds.org/cvs;tag=V0-99-81;module=familiar/dist/ipkg" : ('cvs', 'cvs.handhelds.org', '/cvs', 'anoncvs', 'anonymous', collections.OrderedDict([('tag', 'V0-99-81'), ('module', 'familiar/dist/ipkg')])),
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001205 "git://git.openembedded.org/bitbake;branch=@foo" : ('git', 'git.openembedded.org', '/bitbake', '', '', {'branch': '@foo'}),
1206 "file://somelocation;someparam=1": ('file', '', 'somelocation', '', '', {'someparam': '1'}),
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001207 }
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001208 # we require a pathname to encodeurl but users can still pass such urls to
1209 # decodeurl and we need to handle them
1210 decodedata = datatable.copy()
1211 decodedata.update({
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001212 "http://somesite.net;someparam=1": ('http', 'somesite.net', '/', '', '', {'someparam': '1'}),
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001213 })
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001214
1215 def test_decodeurl(self):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001216 for k, v in self.decodedata.items():
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001217 result = bb.fetch.decodeurl(k)
1218 self.assertEqual(result, v)
1219
1220 def test_encodeurl(self):
1221 for k, v in self.datatable.items():
1222 result = bb.fetch.encodeurl(v)
1223 self.assertEqual(result, k)
1224
1225class FetchLatestVersionTest(FetcherTest):
1226
1227 test_git_uris = {
1228 # version pattern "X.Y.Z"
1229 ("mx-1.0", "git://github.com/clutter-project/mx.git;branch=mx-1.4", "9b1db6b8060bd00b121a692f942404a24ae2960f", "")
1230 : "1.99.4",
1231 # version pattern "vX.Y"
Andrew Geisslerd25ed322020-06-27 00:28:28 -05001232 # mirror of git.infradead.org since network issues interfered with testing
1233 ("mtd-utils", "git://git.yoctoproject.org/mtd-utils.git", "ca39eb1d98e736109c64ff9c1aa2a6ecca222d8f", "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001234 : "1.5.0",
1235 # version pattern "pkg_name-X.Y"
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001236 # mirror of git://anongit.freedesktop.org/git/xorg/proto/presentproto since network issues interfered with testing
1237 ("presentproto", "git://git.yoctoproject.org/bbfetchtests-presentproto", "24f3a56e541b0a9e6c6ee76081f441221a120ef9", "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001238 : "1.0",
1239 # version pattern "pkg_name-vX.Y.Z"
1240 ("dtc", "git://git.qemu.org/dtc.git", "65cc4d2748a2c2e6f27f1cf39e07a5dbabd80ebf", "")
1241 : "1.4.0",
1242 # combination version pattern
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001243 ("sysprof", "git://gitlab.gnome.org/GNOME/sysprof.git;protocol=https", "cd44ee6644c3641507fb53b8a2a69137f2971219", "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001244 : "1.2.0",
1245 ("u-boot-mkimage", "git://git.denx.de/u-boot.git;branch=master;protocol=git", "62c175fbb8a0f9a926c88294ea9f7e88eb898f6c", "")
1246 : "2014.01",
1247 # version pattern "yyyymmdd"
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001248 ("mobile-broadband-provider-info", "git://gitlab.gnome.org/GNOME/mobile-broadband-provider-info.git;protocol=https", "4ed19e11c2975105b71b956440acdb25d46a347d", "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001249 : "20120614",
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001250 # packages with a valid UPSTREAM_CHECK_GITTAGREGEX
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001251 # mirror of git://anongit.freedesktop.org/xorg/driver/xf86-video-omap since network issues interfered with testing
1252 ("xf86-video-omap", "git://git.yoctoproject.org/bbfetchtests-xf86-video-omap", "ae0394e687f1a77e966cf72f895da91840dffb8f", "(?P<pver>(\d+\.(\d\.?)*))")
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001253 : "0.4.3",
1254 ("build-appliance-image", "git://git.yoctoproject.org/poky", "b37dd451a52622d5b570183a81583cc34c2ff555", "(?P<pver>(([0-9][\.|_]?)+[0-9]))")
1255 : "11.0.0",
1256 ("chkconfig-alternatives-native", "git://github.com/kergoth/chkconfig;branch=sysroot", "cd437ecbd8986c894442f8fce1e0061e20f04dee", "chkconfig\-(?P<pver>((\d+[\.\-_]*)+))")
1257 : "1.3.59",
1258 ("remake", "git://github.com/rocky/remake.git", "f05508e521987c8494c92d9c2871aec46307d51d", "(?P<pver>(\d+\.(\d+\.)*\d*(\+dbg\d+(\.\d+)*)*))")
1259 : "3.82+dbg0.9",
1260 }
1261
1262 test_wget_uris = {
Andrew Geissler82c905d2020-04-13 13:39:40 -05001263 #
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001264 # packages with versions inside directory name
Andrew Geissler82c905d2020-04-13 13:39:40 -05001265 #
1266 # http://kernel.org/pub/linux/utils/util-linux/v2.23/util-linux-2.24.2.tar.bz2
1267 ("util-linux", "/pub/linux/utils/util-linux/v2.23/util-linux-2.24.2.tar.bz2", "", "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001268 : "2.24.2",
Andrew Geissler82c905d2020-04-13 13:39:40 -05001269 # http://www.abisource.com/downloads/enchant/1.6.0/enchant-1.6.0.tar.gz
1270 ("enchant", "/downloads/enchant/1.6.0/enchant-1.6.0.tar.gz", "", "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001271 : "1.6.0",
Andrew Geissler82c905d2020-04-13 13:39:40 -05001272 # http://www.cmake.org/files/v2.8/cmake-2.8.12.1.tar.gz
1273 ("cmake", "/files/v2.8/cmake-2.8.12.1.tar.gz", "", "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001274 : "2.8.12.1",
Andrew Geissler82c905d2020-04-13 13:39:40 -05001275 #
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001276 # packages with versions only in current directory
Andrew Geissler82c905d2020-04-13 13:39:40 -05001277 #
1278 # http://downloads.yoctoproject.org/releases/eglibc/eglibc-2.18-svnr23787.tar.bz2
1279 ("eglic", "/releases/eglibc/eglibc-2.18-svnr23787.tar.bz2", "", "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001280 : "2.19",
Andrew Geissler82c905d2020-04-13 13:39:40 -05001281 # http://downloads.yoctoproject.org/releases/gnu-config/gnu-config-20120814.tar.bz2
1282 ("gnu-config", "/releases/gnu-config/gnu-config-20120814.tar.bz2", "", "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001283 : "20120814",
Andrew Geissler82c905d2020-04-13 13:39:40 -05001284 #
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001285 # packages with "99" in the name of possible version
Andrew Geissler82c905d2020-04-13 13:39:40 -05001286 #
1287 # http://freedesktop.org/software/pulseaudio/releases/pulseaudio-4.0.tar.xz
1288 ("pulseaudio", "/software/pulseaudio/releases/pulseaudio-4.0.tar.xz", "", "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001289 : "5.0",
Andrew Geissler82c905d2020-04-13 13:39:40 -05001290 # http://xorg.freedesktop.org/releases/individual/xserver/xorg-server-1.15.1.tar.bz2
1291 ("xserver-xorg", "/releases/individual/xserver/xorg-server-1.15.1.tar.bz2", "", "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001292 : "1.15.1",
Andrew Geissler82c905d2020-04-13 13:39:40 -05001293 #
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001294 # packages with valid UPSTREAM_CHECK_URI and UPSTREAM_CHECK_REGEX
Andrew Geissler82c905d2020-04-13 13:39:40 -05001295 #
1296 # http://www.cups.org/software/1.7.2/cups-1.7.2-source.tar.bz2
1297 # https://github.com/apple/cups/releases
1298 ("cups", "/software/1.7.2/cups-1.7.2-source.tar.bz2", "/apple/cups/releases", "(?P<name>cups\-)(?P<pver>((\d+[\.\-_]*)+))\-source\.tar\.gz")
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001299 : "2.0.0",
Andrew Geissler82c905d2020-04-13 13:39:40 -05001300 # http://download.oracle.com/berkeley-db/db-5.3.21.tar.gz
1301 # http://ftp.debian.org/debian/pool/main/d/db5.3/
1302 ("db", "/berkeley-db/db-5.3.21.tar.gz", "/debian/pool/main/d/db5.3/", "(?P<name>db5\.3_)(?P<pver>\d+(\.\d+)+).+\.orig\.tar\.xz")
Brad Bishop79641f22019-09-10 07:20:22 -04001303 : "5.3.10",
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001304 }
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001305
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001306 @skipIfNoNetwork()
1307 def test_git_latest_versionstring(self):
1308 for k, v in self.test_git_uris.items():
1309 self.d.setVar("PN", k[0])
1310 self.d.setVar("SRCREV", k[2])
1311 self.d.setVar("UPSTREAM_CHECK_GITTAGREGEX", k[3])
1312 ud = bb.fetch2.FetchData(k[1], self.d)
1313 pupver= ud.method.latest_versionstring(ud, self.d)
1314 verstring = pupver[0]
Brad Bishop316dfdd2018-06-25 12:45:53 -04001315 self.assertTrue(verstring, msg="Could not find upstream version for %s" % k[0])
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001316 r = bb.utils.vercmp_string(v, verstring)
1317 self.assertTrue(r == -1 or r == 0, msg="Package %s, version: %s <= %s" % (k[0], v, verstring))
1318
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001319 def test_wget_latest_versionstring(self):
Andrew Geissler82c905d2020-04-13 13:39:40 -05001320 testdata = os.path.dirname(os.path.abspath(__file__)) + "/fetch-testdata"
1321 server = HTTPService(testdata)
1322 server.start()
1323 port = server.port
1324 try:
1325 for k, v in self.test_wget_uris.items():
1326 self.d.setVar("PN", k[0])
1327 checkuri = ""
1328 if k[2]:
1329 checkuri = "http://localhost:%s/" % port + k[2]
1330 self.d.setVar("UPSTREAM_CHECK_URI", checkuri)
1331 self.d.setVar("UPSTREAM_CHECK_REGEX", k[3])
1332 url = "http://localhost:%s/" % port + k[1]
1333 ud = bb.fetch2.FetchData(url, self.d)
1334 pupver = ud.method.latest_versionstring(ud, self.d)
1335 verstring = pupver[0]
1336 self.assertTrue(verstring, msg="Could not find upstream version for %s" % k[0])
1337 r = bb.utils.vercmp_string(v, verstring)
1338 self.assertTrue(r == -1 or r == 0, msg="Package %s, version: %s <= %s" % (k[0], v, verstring))
1339 finally:
1340 server.stop()
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001341
1342
1343class FetchCheckStatusTest(FetcherTest):
Andrew Geisslerc9f78652020-09-18 14:11:35 -05001344 test_wget_uris = ["http://downloads.yoctoproject.org/releases/sato/sato-engine-0.1.tar.gz",
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001345 "http://downloads.yoctoproject.org/releases/sato/sato-engine-0.2.tar.gz",
1346 "http://downloads.yoctoproject.org/releases/sato/sato-engine-0.3.tar.gz",
1347 "https://yoctoproject.org/",
Andrew Geissler95ac1b82021-03-31 14:34:31 -05001348 "https://docs.yoctoproject.org",
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001349 "http://downloads.yoctoproject.org/releases/opkg/opkg-0.1.7.tar.gz",
1350 "http://downloads.yoctoproject.org/releases/opkg/opkg-0.3.0.tar.gz",
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001351 "ftp://sourceware.org/pub/libffi/libffi-1.20.tar.gz",
1352 "http://ftp.gnu.org/gnu/autoconf/autoconf-2.60.tar.gz",
1353 "https://ftp.gnu.org/gnu/chess/gnuchess-5.08.tar.gz",
1354 "https://ftp.gnu.org/gnu/gmp/gmp-4.0.tar.gz",
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001355 # GitHub releases are hosted on Amazon S3, which doesn't support HEAD
1356 "https://github.com/kergoth/tslib/releases/download/1.1/tslib-1.1.tar.xz"
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001357 ]
1358
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001359 @skipIfNoNetwork()
1360 def test_wget_checkstatus(self):
1361 fetch = bb.fetch2.Fetch(self.test_wget_uris, self.d)
1362 for u in self.test_wget_uris:
1363 with self.subTest(url=u):
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001364 ud = fetch.ud[u]
1365 m = ud.method
1366 ret = m.checkstatus(fetch, ud, self.d)
1367 self.assertTrue(ret, msg="URI %s, can't check status" % (u))
1368
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001369 @skipIfNoNetwork()
1370 def test_wget_checkstatus_connection_cache(self):
1371 from bb.fetch2 import FetchConnectionCache
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001372
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001373 connection_cache = FetchConnectionCache()
1374 fetch = bb.fetch2.Fetch(self.test_wget_uris, self.d,
1375 connection_cache = connection_cache)
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001376
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001377 for u in self.test_wget_uris:
1378 with self.subTest(url=u):
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001379 ud = fetch.ud[u]
1380 m = ud.method
1381 ret = m.checkstatus(fetch, ud, self.d)
1382 self.assertTrue(ret, msg="URI %s, can't check status" % (u))
1383
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001384 connection_cache.close_connections()
1385
1386
1387class GitMakeShallowTest(FetcherTest):
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001388 def setUp(self):
1389 FetcherTest.setUp(self)
1390 self.gitdir = os.path.join(self.tempdir, 'gitshallow')
1391 bb.utils.mkdirhier(self.gitdir)
1392 bb.process.run('git init', cwd=self.gitdir)
1393
1394 def assertRefs(self, expected_refs):
1395 actual_refs = self.git(['for-each-ref', '--format=%(refname)']).splitlines()
1396 full_expected = self.git(['rev-parse', '--symbolic-full-name'] + expected_refs).splitlines()
1397 self.assertEqual(sorted(full_expected), sorted(actual_refs))
1398
1399 def assertRevCount(self, expected_count, args=None):
1400 if args is None:
1401 args = ['HEAD']
1402 revs = self.git(['rev-list'] + args)
1403 actual_count = len(revs.splitlines())
1404 self.assertEqual(expected_count, actual_count, msg='Object count `%d` is not the expected `%d`' % (actual_count, expected_count))
1405
1406 def git(self, cmd):
1407 if isinstance(cmd, str):
1408 cmd = 'git ' + cmd
1409 else:
1410 cmd = ['git'] + cmd
1411 return bb.process.run(cmd, cwd=self.gitdir)[0]
1412
1413 def make_shallow(self, args=None):
1414 if args is None:
1415 args = ['HEAD']
Brad Bishop316dfdd2018-06-25 12:45:53 -04001416 return bb.process.run([bb.fetch2.git.Git.make_shallow_path] + args, cwd=self.gitdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001417
1418 def add_empty_file(self, path, msg=None):
1419 if msg is None:
1420 msg = path
1421 open(os.path.join(self.gitdir, path), 'w').close()
1422 self.git(['add', path])
1423 self.git(['commit', '-m', msg, path])
1424
1425 def test_make_shallow_single_branch_no_merge(self):
1426 self.add_empty_file('a')
1427 self.add_empty_file('b')
1428 self.assertRevCount(2)
1429 self.make_shallow()
1430 self.assertRevCount(1)
1431
1432 def test_make_shallow_single_branch_one_merge(self):
1433 self.add_empty_file('a')
1434 self.add_empty_file('b')
1435 self.git('checkout -b a_branch')
1436 self.add_empty_file('c')
1437 self.git('checkout master')
1438 self.add_empty_file('d')
1439 self.git('merge --no-ff --no-edit a_branch')
1440 self.git('branch -d a_branch')
1441 self.add_empty_file('e')
1442 self.assertRevCount(6)
1443 self.make_shallow(['HEAD~2'])
1444 self.assertRevCount(5)
1445
1446 def test_make_shallow_at_merge(self):
1447 self.add_empty_file('a')
1448 self.git('checkout -b a_branch')
1449 self.add_empty_file('b')
1450 self.git('checkout master')
1451 self.git('merge --no-ff --no-edit a_branch')
1452 self.git('branch -d a_branch')
1453 self.assertRevCount(3)
1454 self.make_shallow()
1455 self.assertRevCount(1)
1456
1457 def test_make_shallow_annotated_tag(self):
1458 self.add_empty_file('a')
1459 self.add_empty_file('b')
1460 self.git('tag -a -m a_tag a_tag')
1461 self.assertRevCount(2)
1462 self.make_shallow(['a_tag'])
1463 self.assertRevCount(1)
1464
1465 def test_make_shallow_multi_ref(self):
1466 self.add_empty_file('a')
1467 self.add_empty_file('b')
1468 self.git('checkout -b a_branch')
1469 self.add_empty_file('c')
1470 self.git('checkout master')
1471 self.add_empty_file('d')
1472 self.git('checkout -b a_branch_2')
1473 self.add_empty_file('a_tag')
1474 self.git('tag a_tag')
1475 self.git('checkout master')
1476 self.git('branch -D a_branch_2')
1477 self.add_empty_file('e')
1478 self.assertRevCount(6, ['--all'])
1479 self.make_shallow()
1480 self.assertRevCount(5, ['--all'])
1481
1482 def test_make_shallow_multi_ref_trim(self):
1483 self.add_empty_file('a')
1484 self.git('checkout -b a_branch')
1485 self.add_empty_file('c')
1486 self.git('checkout master')
1487 self.assertRevCount(1)
1488 self.assertRevCount(2, ['--all'])
1489 self.assertRefs(['master', 'a_branch'])
1490 self.make_shallow(['-r', 'master', 'HEAD'])
1491 self.assertRevCount(1, ['--all'])
1492 self.assertRefs(['master'])
1493
1494 def test_make_shallow_noop(self):
1495 self.add_empty_file('a')
1496 self.assertRevCount(1)
1497 self.make_shallow()
1498 self.assertRevCount(1)
1499
1500 @skipIfNoNetwork()
1501 def test_make_shallow_bitbake(self):
1502 self.git('remote add origin https://github.com/openembedded/bitbake')
1503 self.git('fetch --tags origin')
1504 orig_revs = len(self.git('rev-list --all').splitlines())
1505 self.make_shallow(['refs/tags/1.10.0'])
1506 self.assertRevCount(orig_revs - 1746, ['--all'])
1507
1508class GitShallowTest(FetcherTest):
1509 def setUp(self):
1510 FetcherTest.setUp(self)
1511 self.gitdir = os.path.join(self.tempdir, 'git')
1512 self.srcdir = os.path.join(self.tempdir, 'gitsource')
1513
1514 bb.utils.mkdirhier(self.srcdir)
1515 self.git('init', cwd=self.srcdir)
1516 self.d.setVar('WORKDIR', self.tempdir)
1517 self.d.setVar('S', self.gitdir)
1518 self.d.delVar('PREMIRRORS')
1519 self.d.delVar('MIRRORS')
1520
1521 uri = 'git://%s;protocol=file;subdir=${S}' % self.srcdir
1522 self.d.setVar('SRC_URI', uri)
1523 self.d.setVar('SRCREV', '${AUTOREV}')
1524 self.d.setVar('AUTOREV', '${@bb.fetch2.get_autorev(d)}')
1525
1526 self.d.setVar('BB_GIT_SHALLOW', '1')
1527 self.d.setVar('BB_GENERATE_MIRROR_TARBALLS', '0')
1528 self.d.setVar('BB_GENERATE_SHALLOW_TARBALLS', '1')
1529
1530 def assertRefs(self, expected_refs, cwd=None):
1531 if cwd is None:
1532 cwd = self.gitdir
1533 actual_refs = self.git(['for-each-ref', '--format=%(refname)'], cwd=cwd).splitlines()
1534 full_expected = self.git(['rev-parse', '--symbolic-full-name'] + expected_refs, cwd=cwd).splitlines()
1535 self.assertEqual(sorted(set(full_expected)), sorted(set(actual_refs)))
1536
1537 def assertRevCount(self, expected_count, args=None, cwd=None):
1538 if args is None:
1539 args = ['HEAD']
1540 if cwd is None:
1541 cwd = self.gitdir
1542 revs = self.git(['rev-list'] + args, cwd=cwd)
1543 actual_count = len(revs.splitlines())
1544 self.assertEqual(expected_count, actual_count, msg='Object count `%d` is not the expected `%d`' % (actual_count, expected_count))
1545
1546 def git(self, cmd, cwd=None):
1547 if isinstance(cmd, str):
1548 cmd = 'git ' + cmd
1549 else:
1550 cmd = ['git'] + cmd
1551 if cwd is None:
1552 cwd = self.gitdir
1553 return bb.process.run(cmd, cwd=cwd)[0]
1554
1555 def add_empty_file(self, path, cwd=None, msg=None):
1556 if msg is None:
1557 msg = path
1558 if cwd is None:
1559 cwd = self.srcdir
1560 open(os.path.join(cwd, path), 'w').close()
1561 self.git(['add', path], cwd)
1562 self.git(['commit', '-m', msg, path], cwd)
1563
1564 def fetch(self, uri=None):
1565 if uri is None:
Brad Bishop19323692019-04-05 15:28:33 -04001566 uris = self.d.getVar('SRC_URI').split()
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001567 uri = uris[0]
1568 d = self.d
1569 else:
1570 d = self.d.createCopy()
1571 d.setVar('SRC_URI', uri)
1572 uri = d.expand(uri)
1573 uris = [uri]
1574
1575 fetcher = bb.fetch2.Fetch(uris, d)
1576 fetcher.download()
1577 ud = fetcher.ud[uri]
1578 return fetcher, ud
1579
1580 def fetch_and_unpack(self, uri=None):
1581 fetcher, ud = self.fetch(uri)
1582 fetcher.unpack(self.d.getVar('WORKDIR'))
1583 assert os.path.exists(self.d.getVar('S'))
1584 return fetcher, ud
1585
1586 def fetch_shallow(self, uri=None, disabled=False, keepclone=False):
1587 """Fetch a uri, generating a shallow tarball, then unpack using it"""
1588 fetcher, ud = self.fetch_and_unpack(uri)
1589 assert os.path.exists(ud.clonedir), 'Git clone in DLDIR (%s) does not exist for uri %s' % (ud.clonedir, uri)
1590
1591 # Confirm that the unpacked repo is unshallow
1592 if not disabled:
1593 assert os.path.exists(os.path.join(self.dldir, ud.mirrortarballs[0]))
1594
1595 # fetch and unpack, from the shallow tarball
1596 bb.utils.remove(self.gitdir, recurse=True)
1597 bb.utils.remove(ud.clonedir, recurse=True)
Brad Bishopf8caae32019-03-25 13:13:56 -04001598 bb.utils.remove(ud.clonedir.replace('gitsource', 'gitsubmodule'), recurse=True)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001599
1600 # confirm that the unpacked repo is used when no git clone or git
1601 # mirror tarball is available
1602 fetcher, ud = self.fetch_and_unpack(uri)
1603 if not disabled:
1604 assert os.path.exists(os.path.join(self.gitdir, '.git', 'shallow')), 'Unpacked git repository at %s is not shallow' % self.gitdir
1605 else:
1606 assert not os.path.exists(os.path.join(self.gitdir, '.git', 'shallow')), 'Unpacked git repository at %s is shallow' % self.gitdir
1607 return fetcher, ud
1608
1609 def test_shallow_disabled(self):
1610 self.add_empty_file('a')
1611 self.add_empty_file('b')
1612 self.assertRevCount(2, cwd=self.srcdir)
1613
1614 self.d.setVar('BB_GIT_SHALLOW', '0')
1615 self.fetch_shallow(disabled=True)
1616 self.assertRevCount(2)
1617
1618 def test_shallow_nobranch(self):
1619 self.add_empty_file('a')
1620 self.add_empty_file('b')
1621 self.assertRevCount(2, cwd=self.srcdir)
1622
1623 srcrev = self.git('rev-parse HEAD', cwd=self.srcdir).strip()
1624 self.d.setVar('SRCREV', srcrev)
Brad Bishop19323692019-04-05 15:28:33 -04001625 uri = self.d.getVar('SRC_URI').split()[0]
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001626 uri = '%s;nobranch=1;bare=1' % uri
1627
1628 self.fetch_shallow(uri)
1629 self.assertRevCount(1)
1630
1631 # shallow refs are used to ensure the srcrev sticks around when we
1632 # have no other branches referencing it
1633 self.assertRefs(['refs/shallow/default'])
1634
1635 def test_shallow_default_depth_1(self):
1636 # Create initial git repo
1637 self.add_empty_file('a')
1638 self.add_empty_file('b')
1639 self.assertRevCount(2, cwd=self.srcdir)
1640
1641 self.fetch_shallow()
1642 self.assertRevCount(1)
1643
1644 def test_shallow_depth_0_disables(self):
1645 self.add_empty_file('a')
1646 self.add_empty_file('b')
1647 self.assertRevCount(2, cwd=self.srcdir)
1648
1649 self.d.setVar('BB_GIT_SHALLOW_DEPTH', '0')
1650 self.fetch_shallow(disabled=True)
1651 self.assertRevCount(2)
1652
1653 def test_shallow_depth_default_override(self):
1654 self.add_empty_file('a')
1655 self.add_empty_file('b')
1656 self.assertRevCount(2, cwd=self.srcdir)
1657
1658 self.d.setVar('BB_GIT_SHALLOW_DEPTH', '2')
1659 self.d.setVar('BB_GIT_SHALLOW_DEPTH_default', '1')
1660 self.fetch_shallow()
1661 self.assertRevCount(1)
1662
1663 def test_shallow_depth_default_override_disable(self):
1664 self.add_empty_file('a')
1665 self.add_empty_file('b')
1666 self.add_empty_file('c')
1667 self.assertRevCount(3, cwd=self.srcdir)
1668
1669 self.d.setVar('BB_GIT_SHALLOW_DEPTH', '0')
1670 self.d.setVar('BB_GIT_SHALLOW_DEPTH_default', '2')
1671 self.fetch_shallow()
1672 self.assertRevCount(2)
1673
1674 def test_current_shallow_out_of_date_clone(self):
1675 # Create initial git repo
1676 self.add_empty_file('a')
1677 self.add_empty_file('b')
1678 self.add_empty_file('c')
1679 self.assertRevCount(3, cwd=self.srcdir)
1680
1681 # Clone and generate mirror tarball
1682 fetcher, ud = self.fetch()
1683
1684 # Ensure we have a current mirror tarball, but an out of date clone
1685 self.git('update-ref refs/heads/master refs/heads/master~1', cwd=ud.clonedir)
1686 self.assertRevCount(2, cwd=ud.clonedir)
1687
1688 # Fetch and unpack, from the current tarball, not the out of date clone
1689 bb.utils.remove(self.gitdir, recurse=True)
1690 fetcher, ud = self.fetch()
1691 fetcher.unpack(self.d.getVar('WORKDIR'))
1692 self.assertRevCount(1)
1693
1694 def test_shallow_single_branch_no_merge(self):
1695 self.add_empty_file('a')
1696 self.add_empty_file('b')
1697 self.assertRevCount(2, cwd=self.srcdir)
1698
1699 self.fetch_shallow()
1700 self.assertRevCount(1)
1701 assert os.path.exists(os.path.join(self.gitdir, 'a'))
1702 assert os.path.exists(os.path.join(self.gitdir, 'b'))
1703
1704 def test_shallow_no_dangling(self):
1705 self.add_empty_file('a')
1706 self.add_empty_file('b')
1707 self.assertRevCount(2, cwd=self.srcdir)
1708
1709 self.fetch_shallow()
1710 self.assertRevCount(1)
1711 assert not self.git('fsck --dangling')
1712
1713 def test_shallow_srcrev_branch_truncation(self):
1714 self.add_empty_file('a')
1715 self.add_empty_file('b')
1716 b_commit = self.git('rev-parse HEAD', cwd=self.srcdir).rstrip()
1717 self.add_empty_file('c')
1718 self.assertRevCount(3, cwd=self.srcdir)
1719
1720 self.d.setVar('SRCREV', b_commit)
1721 self.fetch_shallow()
1722
1723 # The 'c' commit was removed entirely, and 'a' was removed from history
1724 self.assertRevCount(1, ['--all'])
1725 self.assertEqual(self.git('rev-parse HEAD').strip(), b_commit)
1726 assert os.path.exists(os.path.join(self.gitdir, 'a'))
1727 assert os.path.exists(os.path.join(self.gitdir, 'b'))
1728 assert not os.path.exists(os.path.join(self.gitdir, 'c'))
1729
1730 def test_shallow_ref_pruning(self):
1731 self.add_empty_file('a')
1732 self.add_empty_file('b')
1733 self.git('branch a_branch', cwd=self.srcdir)
1734 self.assertRefs(['master', 'a_branch'], cwd=self.srcdir)
1735 self.assertRevCount(2, cwd=self.srcdir)
1736
1737 self.fetch_shallow()
1738
1739 self.assertRefs(['master', 'origin/master'])
1740 self.assertRevCount(1)
1741
1742 def test_shallow_submodules(self):
1743 self.add_empty_file('a')
1744 self.add_empty_file('b')
1745
1746 smdir = os.path.join(self.tempdir, 'gitsubmodule')
1747 bb.utils.mkdirhier(smdir)
1748 self.git('init', cwd=smdir)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001749 # Make this look like it was cloned from a remote...
1750 self.git('config --add remote.origin.url "%s"' % smdir, cwd=smdir)
1751 self.git('config --add remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"', cwd=smdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001752 self.add_empty_file('asub', cwd=smdir)
Brad Bishopf8caae32019-03-25 13:13:56 -04001753 self.add_empty_file('bsub', cwd=smdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001754
1755 self.git('submodule init', cwd=self.srcdir)
1756 self.git('submodule add file://%s' % smdir, cwd=self.srcdir)
1757 self.git('submodule update', cwd=self.srcdir)
1758 self.git('commit -m submodule -a', cwd=self.srcdir)
1759
1760 uri = 'gitsm://%s;protocol=file;subdir=${S}' % self.srcdir
1761 fetcher, ud = self.fetch_shallow(uri)
1762
Brad Bishopf8caae32019-03-25 13:13:56 -04001763 # Verify the main repository is shallow
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001764 self.assertRevCount(1)
Brad Bishopf8caae32019-03-25 13:13:56 -04001765
1766 # Verify the gitsubmodule directory is present
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001767 assert os.listdir(os.path.join(self.gitdir, 'gitsubmodule'))
1768
Brad Bishopf8caae32019-03-25 13:13:56 -04001769 # Verify the submodule is also shallow
1770 self.assertRevCount(1, cwd=os.path.join(self.gitdir, 'gitsubmodule'))
1771
Andrew Geissler82c905d2020-04-13 13:39:40 -05001772 def test_shallow_submodule_mirrors(self):
1773 self.add_empty_file('a')
1774 self.add_empty_file('b')
1775
1776 smdir = os.path.join(self.tempdir, 'gitsubmodule')
1777 bb.utils.mkdirhier(smdir)
1778 self.git('init', cwd=smdir)
1779 # Make this look like it was cloned from a remote...
1780 self.git('config --add remote.origin.url "%s"' % smdir, cwd=smdir)
1781 self.git('config --add remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"', cwd=smdir)
1782 self.add_empty_file('asub', cwd=smdir)
1783 self.add_empty_file('bsub', cwd=smdir)
1784
1785 self.git('submodule init', cwd=self.srcdir)
1786 self.git('submodule add file://%s' % smdir, cwd=self.srcdir)
1787 self.git('submodule update', cwd=self.srcdir)
1788 self.git('commit -m submodule -a', cwd=self.srcdir)
1789
1790 uri = 'gitsm://%s;protocol=file;subdir=${S}' % self.srcdir
1791
1792 # Fetch once to generate the shallow tarball
1793 fetcher, ud = self.fetch(uri)
1794
1795 # Set up the mirror
1796 mirrordir = os.path.join(self.tempdir, 'mirror')
1797 os.rename(self.dldir, mirrordir)
1798 self.d.setVar('PREMIRRORS', 'gitsm://.*/.* file://%s/\n' % mirrordir)
1799
1800 # Fetch from the mirror
1801 bb.utils.remove(self.dldir, recurse=True)
1802 bb.utils.remove(self.gitdir, recurse=True)
1803 self.fetch_and_unpack(uri)
1804
1805 # Verify the main repository is shallow
1806 self.assertRevCount(1)
1807
1808 # Verify the gitsubmodule directory is present
1809 assert os.listdir(os.path.join(self.gitdir, 'gitsubmodule'))
1810
1811 # Verify the submodule is also shallow
1812 self.assertRevCount(1, cwd=os.path.join(self.gitdir, 'gitsubmodule'))
Brad Bishopf8caae32019-03-25 13:13:56 -04001813
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001814 if any(os.path.exists(os.path.join(p, 'git-annex')) for p in os.environ.get('PATH').split(':')):
1815 def test_shallow_annex(self):
1816 self.add_empty_file('a')
1817 self.add_empty_file('b')
1818 self.git('annex init', cwd=self.srcdir)
1819 open(os.path.join(self.srcdir, 'c'), 'w').close()
1820 self.git('annex add c', cwd=self.srcdir)
1821 self.git('commit -m annex-c -a', cwd=self.srcdir)
1822 bb.process.run('chmod u+w -R %s' % os.path.join(self.srcdir, '.git', 'annex'))
1823
1824 uri = 'gitannex://%s;protocol=file;subdir=${S}' % self.srcdir
1825 fetcher, ud = self.fetch_shallow(uri)
1826
1827 self.assertRevCount(1)
1828 assert './.git/annex/' in bb.process.run('tar -tzf %s' % os.path.join(self.dldir, ud.mirrortarballs[0]))[0]
1829 assert os.path.exists(os.path.join(self.gitdir, 'c'))
1830
1831 def test_shallow_multi_one_uri(self):
1832 # Create initial git repo
1833 self.add_empty_file('a')
1834 self.add_empty_file('b')
1835 self.git('checkout -b a_branch', cwd=self.srcdir)
1836 self.add_empty_file('c')
1837 self.add_empty_file('d')
1838 self.git('checkout master', cwd=self.srcdir)
1839 self.git('tag v0.0 a_branch', cwd=self.srcdir)
1840 self.add_empty_file('e')
1841 self.git('merge --no-ff --no-edit a_branch', cwd=self.srcdir)
1842 self.add_empty_file('f')
1843 self.assertRevCount(7, cwd=self.srcdir)
1844
Brad Bishop19323692019-04-05 15:28:33 -04001845 uri = self.d.getVar('SRC_URI').split()[0]
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001846 uri = '%s;branch=master,a_branch;name=master,a_branch' % uri
1847
1848 self.d.setVar('BB_GIT_SHALLOW_DEPTH', '0')
1849 self.d.setVar('BB_GIT_SHALLOW_REVS', 'v0.0')
1850 self.d.setVar('SRCREV_master', '${AUTOREV}')
1851 self.d.setVar('SRCREV_a_branch', '${AUTOREV}')
1852
1853 self.fetch_shallow(uri)
1854
1855 self.assertRevCount(5)
1856 self.assertRefs(['master', 'origin/master', 'origin/a_branch'])
1857
1858 def test_shallow_multi_one_uri_depths(self):
1859 # Create initial git repo
1860 self.add_empty_file('a')
1861 self.add_empty_file('b')
1862 self.git('checkout -b a_branch', cwd=self.srcdir)
1863 self.add_empty_file('c')
1864 self.add_empty_file('d')
1865 self.git('checkout master', cwd=self.srcdir)
1866 self.add_empty_file('e')
1867 self.git('merge --no-ff --no-edit a_branch', cwd=self.srcdir)
1868 self.add_empty_file('f')
1869 self.assertRevCount(7, cwd=self.srcdir)
1870
Brad Bishop19323692019-04-05 15:28:33 -04001871 uri = self.d.getVar('SRC_URI').split()[0]
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001872 uri = '%s;branch=master,a_branch;name=master,a_branch' % uri
1873
1874 self.d.setVar('BB_GIT_SHALLOW_DEPTH', '0')
1875 self.d.setVar('BB_GIT_SHALLOW_DEPTH_master', '3')
1876 self.d.setVar('BB_GIT_SHALLOW_DEPTH_a_branch', '1')
1877 self.d.setVar('SRCREV_master', '${AUTOREV}')
1878 self.d.setVar('SRCREV_a_branch', '${AUTOREV}')
1879
1880 self.fetch_shallow(uri)
1881
1882 self.assertRevCount(4, ['--all'])
1883 self.assertRefs(['master', 'origin/master', 'origin/a_branch'])
1884
1885 def test_shallow_clone_preferred_over_shallow(self):
1886 self.add_empty_file('a')
1887 self.add_empty_file('b')
1888
1889 # Fetch once to generate the shallow tarball
1890 fetcher, ud = self.fetch()
1891 assert os.path.exists(os.path.join(self.dldir, ud.mirrortarballs[0]))
1892
1893 # Fetch and unpack with both the clonedir and shallow tarball available
1894 bb.utils.remove(self.gitdir, recurse=True)
1895 fetcher, ud = self.fetch_and_unpack()
1896
1897 # The unpacked tree should *not* be shallow
1898 self.assertRevCount(2)
1899 assert not os.path.exists(os.path.join(self.gitdir, '.git', 'shallow'))
1900
1901 def test_shallow_mirrors(self):
1902 self.add_empty_file('a')
1903 self.add_empty_file('b')
1904
1905 # Fetch once to generate the shallow tarball
1906 fetcher, ud = self.fetch()
1907 mirrortarball = ud.mirrortarballs[0]
1908 assert os.path.exists(os.path.join(self.dldir, mirrortarball))
1909
1910 # Set up the mirror
1911 mirrordir = os.path.join(self.tempdir, 'mirror')
1912 bb.utils.mkdirhier(mirrordir)
1913 self.d.setVar('PREMIRRORS', 'git://.*/.* file://%s/\n' % mirrordir)
1914
1915 os.rename(os.path.join(self.dldir, mirrortarball),
1916 os.path.join(mirrordir, mirrortarball))
1917
1918 # Fetch from the mirror
1919 bb.utils.remove(self.dldir, recurse=True)
1920 bb.utils.remove(self.gitdir, recurse=True)
1921 self.fetch_and_unpack()
1922 self.assertRevCount(1)
1923
1924 def test_shallow_invalid_depth(self):
1925 self.add_empty_file('a')
1926 self.add_empty_file('b')
1927
1928 self.d.setVar('BB_GIT_SHALLOW_DEPTH', '-12')
1929 with self.assertRaises(bb.fetch2.FetchError):
1930 self.fetch()
1931
1932 def test_shallow_invalid_depth_default(self):
1933 self.add_empty_file('a')
1934 self.add_empty_file('b')
1935
1936 self.d.setVar('BB_GIT_SHALLOW_DEPTH_default', '-12')
1937 with self.assertRaises(bb.fetch2.FetchError):
1938 self.fetch()
1939
1940 def test_shallow_extra_refs(self):
1941 self.add_empty_file('a')
1942 self.add_empty_file('b')
1943 self.git('branch a_branch', cwd=self.srcdir)
1944 self.assertRefs(['master', 'a_branch'], cwd=self.srcdir)
1945 self.assertRevCount(2, cwd=self.srcdir)
1946
1947 self.d.setVar('BB_GIT_SHALLOW_EXTRA_REFS', 'refs/heads/a_branch')
1948 self.fetch_shallow()
1949
1950 self.assertRefs(['master', 'origin/master', 'origin/a_branch'])
1951 self.assertRevCount(1)
1952
1953 def test_shallow_extra_refs_wildcard(self):
1954 self.add_empty_file('a')
1955 self.add_empty_file('b')
1956 self.git('branch a_branch', cwd=self.srcdir)
1957 self.git('tag v1.0', cwd=self.srcdir)
1958 self.assertRefs(['master', 'a_branch', 'v1.0'], cwd=self.srcdir)
1959 self.assertRevCount(2, cwd=self.srcdir)
1960
1961 self.d.setVar('BB_GIT_SHALLOW_EXTRA_REFS', 'refs/tags/*')
1962 self.fetch_shallow()
1963
1964 self.assertRefs(['master', 'origin/master', 'v1.0'])
1965 self.assertRevCount(1)
1966
1967 def test_shallow_missing_extra_refs(self):
1968 self.add_empty_file('a')
1969 self.add_empty_file('b')
1970
1971 self.d.setVar('BB_GIT_SHALLOW_EXTRA_REFS', 'refs/heads/foo')
1972 with self.assertRaises(bb.fetch2.FetchError):
1973 self.fetch()
1974
1975 def test_shallow_missing_extra_refs_wildcard(self):
1976 self.add_empty_file('a')
1977 self.add_empty_file('b')
1978
1979 self.d.setVar('BB_GIT_SHALLOW_EXTRA_REFS', 'refs/tags/*')
1980 self.fetch()
1981
1982 def test_shallow_remove_revs(self):
1983 # Create initial git repo
1984 self.add_empty_file('a')
1985 self.add_empty_file('b')
1986 self.git('checkout -b a_branch', cwd=self.srcdir)
1987 self.add_empty_file('c')
1988 self.add_empty_file('d')
1989 self.git('checkout master', cwd=self.srcdir)
1990 self.git('tag v0.0 a_branch', cwd=self.srcdir)
1991 self.add_empty_file('e')
1992 self.git('merge --no-ff --no-edit a_branch', cwd=self.srcdir)
1993 self.git('branch -d a_branch', cwd=self.srcdir)
1994 self.add_empty_file('f')
1995 self.assertRevCount(7, cwd=self.srcdir)
1996
1997 self.d.setVar('BB_GIT_SHALLOW_DEPTH', '0')
1998 self.d.setVar('BB_GIT_SHALLOW_REVS', 'v0.0')
1999
2000 self.fetch_shallow()
2001
2002 self.assertRevCount(5)
2003
2004 def test_shallow_invalid_revs(self):
2005 self.add_empty_file('a')
2006 self.add_empty_file('b')
2007
2008 self.d.setVar('BB_GIT_SHALLOW_DEPTH', '0')
2009 self.d.setVar('BB_GIT_SHALLOW_REVS', 'v0.0')
2010
2011 with self.assertRaises(bb.fetch2.FetchError):
2012 self.fetch()
2013
Brad Bishop64c979e2019-11-04 13:55:29 -05002014 def test_shallow_fetch_missing_revs(self):
2015 self.add_empty_file('a')
2016 self.add_empty_file('b')
2017 fetcher, ud = self.fetch(self.d.getVar('SRC_URI'))
2018 self.git('tag v0.0 master', cwd=self.srcdir)
2019 self.d.setVar('BB_GIT_SHALLOW_DEPTH', '0')
2020 self.d.setVar('BB_GIT_SHALLOW_REVS', 'v0.0')
2021 self.fetch_shallow()
2022
2023 def test_shallow_fetch_missing_revs_fails(self):
2024 self.add_empty_file('a')
2025 self.add_empty_file('b')
2026 fetcher, ud = self.fetch(self.d.getVar('SRC_URI'))
2027 self.d.setVar('BB_GIT_SHALLOW_DEPTH', '0')
2028 self.d.setVar('BB_GIT_SHALLOW_REVS', 'v0.0')
2029
2030 with self.assertRaises(bb.fetch2.FetchError), self.assertLogs("BitBake.Fetcher", level="ERROR") as cm:
2031 self.fetch_shallow()
2032 self.assertIn("Unable to find revision v0.0 even from upstream", cm.output[0])
2033
Brad Bishopd7bf8c12018-02-25 22:55:05 -05002034 @skipIfNoNetwork()
2035 def test_bitbake(self):
2036 self.git('remote add --mirror=fetch origin git://github.com/openembedded/bitbake', cwd=self.srcdir)
2037 self.git('config core.bare true', cwd=self.srcdir)
2038 self.git('fetch', cwd=self.srcdir)
2039
2040 self.d.setVar('BB_GIT_SHALLOW_DEPTH', '0')
2041 # Note that the 1.10.0 tag is annotated, so this also tests
2042 # reference of an annotated vs unannotated tag
2043 self.d.setVar('BB_GIT_SHALLOW_REVS', '1.10.0')
2044
2045 self.fetch_shallow()
2046
2047 # Confirm that the history of 1.10.0 was removed
2048 orig_revs = len(self.git('rev-list master', cwd=self.srcdir).splitlines())
2049 revs = len(self.git('rev-list master').splitlines())
2050 self.assertNotEqual(orig_revs, revs)
2051 self.assertRefs(['master', 'origin/master'])
2052 self.assertRevCount(orig_revs - 1758)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08002053
2054 def test_that_unpack_throws_an_error_when_the_git_clone_nor_shallow_tarball_exist(self):
2055 self.add_empty_file('a')
2056 fetcher, ud = self.fetch()
2057 bb.utils.remove(self.gitdir, recurse=True)
2058 bb.utils.remove(self.dldir, recurse=True)
2059
2060 with self.assertRaises(bb.fetch2.UnpackError) as context:
2061 fetcher.unpack(self.d.getVar('WORKDIR'))
2062
2063 self.assertIn("No up to date source found", context.exception.msg)
2064 self.assertIn("clone directory not available or not up to date", context.exception.msg)
2065
2066 @skipIfNoNetwork()
2067 def test_that_unpack_does_work_when_using_git_shallow_tarball_but_tarball_is_not_available(self):
2068 self.d.setVar('SRCREV', 'e5939ff608b95cdd4d0ab0e1935781ab9a276ac0')
2069 self.d.setVar('BB_GIT_SHALLOW', '1')
2070 self.d.setVar('BB_GENERATE_SHALLOW_TARBALLS', '1')
2071 fetcher = bb.fetch.Fetch(["git://git.yoctoproject.org/fstests"], self.d)
2072 fetcher.download()
2073
2074 bb.utils.remove(self.dldir + "/*.tar.gz")
2075 fetcher.unpack(self.unpackdir)
2076
2077 dir = os.listdir(self.unpackdir + "/git/")
2078 self.assertIn("fstests.doap", dir)
Brad Bishop00e122a2019-10-05 11:10:57 -04002079
2080class GitLfsTest(FetcherTest):
2081 def setUp(self):
2082 FetcherTest.setUp(self)
2083
2084 self.gitdir = os.path.join(self.tempdir, 'git')
2085 self.srcdir = os.path.join(self.tempdir, 'gitsource')
2086
2087 self.d.setVar('WORKDIR', self.tempdir)
2088 self.d.setVar('S', self.gitdir)
2089 self.d.delVar('PREMIRRORS')
2090 self.d.delVar('MIRRORS')
2091
2092 self.d.setVar('SRCREV', '${AUTOREV}')
2093 self.d.setVar('AUTOREV', '${@bb.fetch2.get_autorev(d)}')
2094
2095 bb.utils.mkdirhier(self.srcdir)
2096 self.git('init', cwd=self.srcdir)
2097 with open(os.path.join(self.srcdir, '.gitattributes'), 'wt') as attrs:
2098 attrs.write('*.mp3 filter=lfs -text')
2099 self.git(['add', '.gitattributes'], cwd=self.srcdir)
2100 self.git(['commit', '-m', "attributes", '.gitattributes'], cwd=self.srcdir)
2101
2102 def git(self, cmd, cwd=None):
2103 if isinstance(cmd, str):
2104 cmd = 'git ' + cmd
2105 else:
2106 cmd = ['git'] + cmd
2107 if cwd is None:
2108 cwd = self.gitdir
2109 return bb.process.run(cmd, cwd=cwd)[0]
2110
Andrew Geisslerd1e89492021-02-12 15:35:20 -06002111 def fetch(self, uri=None, download=True):
Brad Bishop00e122a2019-10-05 11:10:57 -04002112 uris = self.d.getVar('SRC_URI').split()
2113 uri = uris[0]
2114 d = self.d
2115
2116 fetcher = bb.fetch2.Fetch(uris, d)
Andrew Geisslerd1e89492021-02-12 15:35:20 -06002117 if download:
2118 fetcher.download()
Brad Bishop00e122a2019-10-05 11:10:57 -04002119 ud = fetcher.ud[uri]
2120 return fetcher, ud
2121
2122 def test_lfs_enabled(self):
2123 import shutil
2124
2125 uri = 'git://%s;protocol=file;subdir=${S};lfs=1' % self.srcdir
2126 self.d.setVar('SRC_URI', uri)
2127
Andrew Geisslerd1e89492021-02-12 15:35:20 -06002128 # Careful: suppress initial attempt at downloading until
2129 # we know whether git-lfs is installed.
2130 fetcher, ud = self.fetch(uri=None, download=False)
Brad Bishop00e122a2019-10-05 11:10:57 -04002131 self.assertIsNotNone(ud.method._find_git_lfs)
2132
Andrew Geisslerd1e89492021-02-12 15:35:20 -06002133 # If git-lfs can be found, the unpack should be successful. Only
2134 # attempt this with the real live copy of git-lfs installed.
2135 if ud.method._find_git_lfs(self.d):
2136 fetcher.download()
2137 shutil.rmtree(self.gitdir, ignore_errors=True)
2138 fetcher.unpack(self.d.getVar('WORKDIR'))
Brad Bishop00e122a2019-10-05 11:10:57 -04002139
2140 # If git-lfs cannot be found, the unpack should throw an error
2141 with self.assertRaises(bb.fetch2.FetchError):
Andrew Geisslerd1e89492021-02-12 15:35:20 -06002142 fetcher.download()
Brad Bishop00e122a2019-10-05 11:10:57 -04002143 ud.method._find_git_lfs = lambda d: False
2144 shutil.rmtree(self.gitdir, ignore_errors=True)
2145 fetcher.unpack(self.d.getVar('WORKDIR'))
2146
2147 def test_lfs_disabled(self):
2148 import shutil
2149
2150 uri = 'git://%s;protocol=file;subdir=${S};lfs=0' % self.srcdir
2151 self.d.setVar('SRC_URI', uri)
2152
Andrew Geisslerd1e89492021-02-12 15:35:20 -06002153 # In contrast to test_lfs_enabled(), allow the implicit download
2154 # done by self.fetch() to occur here. The point of this test case
2155 # is to verify that the fetcher can survive even if the source
2156 # repository has Git LFS usage configured.
Brad Bishop00e122a2019-10-05 11:10:57 -04002157 fetcher, ud = self.fetch()
2158 self.assertIsNotNone(ud.method._find_git_lfs)
2159
Andrew Geisslerd1e89492021-02-12 15:35:20 -06002160 # If git-lfs can be found, the unpack should be successful. A
2161 # live copy of git-lfs is not required for this case, so
2162 # unconditionally forge its presence.
Brad Bishop00e122a2019-10-05 11:10:57 -04002163 ud.method._find_git_lfs = lambda d: True
2164 shutil.rmtree(self.gitdir, ignore_errors=True)
2165 fetcher.unpack(self.d.getVar('WORKDIR'))
2166
2167 # If git-lfs cannot be found, the unpack should be successful
2168 ud.method._find_git_lfs = lambda d: False
2169 shutil.rmtree(self.gitdir, ignore_errors=True)
2170 fetcher.unpack(self.d.getVar('WORKDIR'))
Andrew Geissler82c905d2020-04-13 13:39:40 -05002171
Andrew Geisslerc3d88e42020-10-02 09:45:00 -05002172class GitURLWithSpacesTest(FetcherTest):
2173 test_git_urls = {
2174 "git://tfs-example.org:22/tfs/example%20path/example.git" : {
2175 'url': 'git://tfs-example.org:22/tfs/example%20path/example.git',
2176 'gitsrcname': 'tfs-example.org.22.tfs.example_path.example.git',
2177 'path': '/tfs/example path/example.git'
2178 },
2179 "git://tfs-example.org:22/tfs/example%20path/example%20repo.git" : {
2180 'url': 'git://tfs-example.org:22/tfs/example%20path/example%20repo.git',
2181 'gitsrcname': 'tfs-example.org.22.tfs.example_path.example_repo.git',
2182 'path': '/tfs/example path/example repo.git'
2183 }
2184 }
2185
2186 def test_urls(self):
2187
2188 # Set fake SRCREV to stop git fetcher from trying to contact non-existent git repo
2189 self.d.setVar('SRCREV', '82ea737a0b42a8b53e11c9cde141e9e9c0bd8c40')
2190
2191 for test_git_url, ref in self.test_git_urls.items():
2192
2193 fetcher = bb.fetch.Fetch([test_git_url], self.d)
2194 ud = fetcher.ud[fetcher.urls[0]]
2195
2196 self.assertEqual(ud.url, ref['url'])
2197 self.assertEqual(ud.path, ref['path'])
2198 self.assertEqual(ud.localfile, os.path.join(self.dldir, "git2", ref['gitsrcname']))
2199 self.assertEqual(ud.localpath, os.path.join(self.dldir, "git2", ref['gitsrcname']))
2200 self.assertEqual(ud.lockfile, os.path.join(self.dldir, "git2", ref['gitsrcname'] + '.lock'))
2201 self.assertEqual(ud.clonedir, os.path.join(self.dldir, "git2", ref['gitsrcname']))
2202 self.assertEqual(ud.fullmirror, os.path.join(self.dldir, "git2_" + ref['gitsrcname'] + '.tar.gz'))
2203
Andrew Geissler82c905d2020-04-13 13:39:40 -05002204class NPMTest(FetcherTest):
2205 def skipIfNoNpm():
2206 import shutil
2207 if not shutil.which('npm'):
2208 return unittest.skip('npm not installed, tests being skipped')
2209 return lambda f: f
2210
2211 @skipIfNoNpm()
2212 @skipIfNoNetwork()
2213 def test_npm(self):
2214 url = 'npm://registry.npmjs.org;package=@savoirfairelinux/node-server-example;version=1.0.0'
2215 fetcher = bb.fetch.Fetch([url], self.d)
2216 ud = fetcher.ud[fetcher.urls[0]]
2217 fetcher.download()
2218 self.assertTrue(os.path.exists(ud.localpath))
2219 self.assertTrue(os.path.exists(ud.localpath + '.done'))
2220 self.assertTrue(os.path.exists(ud.resolvefile))
2221 fetcher.unpack(self.unpackdir)
2222 unpackdir = os.path.join(self.unpackdir, 'npm')
2223 self.assertTrue(os.path.exists(os.path.join(unpackdir, 'package.json')))
2224
2225 @skipIfNoNpm()
2226 @skipIfNoNetwork()
2227 def test_npm_bad_checksum(self):
2228 url = 'npm://registry.npmjs.org;package=@savoirfairelinux/node-server-example;version=1.0.0'
2229 # Fetch once to get a tarball
2230 fetcher = bb.fetch.Fetch([url], self.d)
2231 ud = fetcher.ud[fetcher.urls[0]]
2232 fetcher.download()
2233 self.assertTrue(os.path.exists(ud.localpath))
2234 # Modify the tarball
2235 bad = b'bad checksum'
2236 with open(ud.localpath, 'wb') as f:
2237 f.write(bad)
2238 # Verify that the tarball is fetched again
2239 fetcher.download()
2240 badsum = hashlib.sha512(bad).hexdigest()
2241 self.assertTrue(os.path.exists(ud.localpath + '_bad-checksum_' + badsum))
2242 self.assertTrue(os.path.exists(ud.localpath))
2243
2244 @skipIfNoNpm()
2245 @skipIfNoNetwork()
2246 def test_npm_premirrors(self):
2247 url = 'npm://registry.npmjs.org;package=@savoirfairelinux/node-server-example;version=1.0.0'
2248 # Fetch once to get a tarball
2249 fetcher = bb.fetch.Fetch([url], self.d)
2250 ud = fetcher.ud[fetcher.urls[0]]
2251 fetcher.download()
2252 self.assertTrue(os.path.exists(ud.localpath))
2253 # Setup the mirror
2254 mirrordir = os.path.join(self.tempdir, 'mirror')
2255 bb.utils.mkdirhier(mirrordir)
2256 os.replace(ud.localpath, os.path.join(mirrordir, os.path.basename(ud.localpath)))
2257 self.d.setVar('PREMIRRORS', 'https?$://.*/.* file://%s/\n' % mirrordir)
2258 self.d.setVar('BB_FETCH_PREMIRRORONLY', '1')
2259 # Fetch again
2260 self.assertFalse(os.path.exists(ud.localpath))
2261 fetcher.download()
2262 self.assertTrue(os.path.exists(ud.localpath))
2263
2264 @skipIfNoNpm()
2265 @skipIfNoNetwork()
2266 def test_npm_mirrors(self):
2267 # Fetch once to get a tarball
2268 url = 'npm://registry.npmjs.org;package=@savoirfairelinux/node-server-example;version=1.0.0'
2269 fetcher = bb.fetch.Fetch([url], self.d)
2270 ud = fetcher.ud[fetcher.urls[0]]
2271 fetcher.download()
2272 self.assertTrue(os.path.exists(ud.localpath))
2273 # Setup the mirror
2274 mirrordir = os.path.join(self.tempdir, 'mirror')
2275 bb.utils.mkdirhier(mirrordir)
2276 os.replace(ud.localpath, os.path.join(mirrordir, os.path.basename(ud.localpath)))
2277 self.d.setVar('MIRRORS', 'https?$://.*/.* file://%s/\n' % mirrordir)
2278 # Update the resolved url to an invalid url
2279 with open(ud.resolvefile, 'r') as f:
2280 url = f.read()
2281 uri = URI(url)
2282 uri.path = '/invalid'
2283 with open(ud.resolvefile, 'w') as f:
2284 f.write(str(uri))
2285 # Fetch again
2286 self.assertFalse(os.path.exists(ud.localpath))
2287 fetcher.download()
2288 self.assertTrue(os.path.exists(ud.localpath))
2289
2290 @skipIfNoNpm()
2291 @skipIfNoNetwork()
2292 def test_npm_destsuffix_downloadfilename(self):
2293 url = 'npm://registry.npmjs.org;package=@savoirfairelinux/node-server-example;version=1.0.0;destsuffix=foo/bar;downloadfilename=foo-bar.tgz'
2294 fetcher = bb.fetch.Fetch([url], self.d)
2295 fetcher.download()
2296 self.assertTrue(os.path.exists(os.path.join(self.dldir, 'foo-bar.tgz')))
2297 fetcher.unpack(self.unpackdir)
2298 unpackdir = os.path.join(self.unpackdir, 'foo', 'bar')
2299 self.assertTrue(os.path.exists(os.path.join(unpackdir, 'package.json')))
2300
2301 def test_npm_no_network_no_tarball(self):
2302 url = 'npm://registry.npmjs.org;package=@savoirfairelinux/node-server-example;version=1.0.0'
2303 self.d.setVar('BB_NO_NETWORK', '1')
2304 fetcher = bb.fetch.Fetch([url], self.d)
2305 with self.assertRaises(bb.fetch2.NetworkAccess):
2306 fetcher.download()
2307
2308 @skipIfNoNpm()
2309 @skipIfNoNetwork()
2310 def test_npm_no_network_with_tarball(self):
2311 url = 'npm://registry.npmjs.org;package=@savoirfairelinux/node-server-example;version=1.0.0'
2312 # Fetch once to get a tarball
2313 fetcher = bb.fetch.Fetch([url], self.d)
2314 fetcher.download()
2315 # Disable network access
2316 self.d.setVar('BB_NO_NETWORK', '1')
2317 # Fetch again
2318 fetcher.download()
2319 fetcher.unpack(self.unpackdir)
2320 unpackdir = os.path.join(self.unpackdir, 'npm')
2321 self.assertTrue(os.path.exists(os.path.join(unpackdir, 'package.json')))
2322
2323 @skipIfNoNpm()
2324 @skipIfNoNetwork()
2325 def test_npm_registry_alternate(self):
2326 url = 'npm://registry.freajs.org;package=@savoirfairelinux/node-server-example;version=1.0.0'
2327 fetcher = bb.fetch.Fetch([url], self.d)
2328 fetcher.download()
2329 fetcher.unpack(self.unpackdir)
2330 unpackdir = os.path.join(self.unpackdir, 'npm')
2331 self.assertTrue(os.path.exists(os.path.join(unpackdir, 'package.json')))
2332
2333 @skipIfNoNpm()
2334 @skipIfNoNetwork()
2335 def test_npm_version_latest(self):
2336 url = 'npm://registry.npmjs.org;package=@savoirfairelinux/node-server-example;version=latest'
2337 fetcher = bb.fetch.Fetch([url], self.d)
2338 fetcher.download()
2339 fetcher.unpack(self.unpackdir)
2340 unpackdir = os.path.join(self.unpackdir, 'npm')
2341 self.assertTrue(os.path.exists(os.path.join(unpackdir, 'package.json')))
2342
2343 @skipIfNoNpm()
2344 @skipIfNoNetwork()
2345 def test_npm_registry_invalid(self):
2346 url = 'npm://registry.invalid.org;package=@savoirfairelinux/node-server-example;version=1.0.0'
2347 fetcher = bb.fetch.Fetch([url], self.d)
2348 with self.assertRaises(bb.fetch2.FetchError):
2349 fetcher.download()
2350
2351 @skipIfNoNpm()
2352 @skipIfNoNetwork()
2353 def test_npm_package_invalid(self):
2354 url = 'npm://registry.npmjs.org;package=@savoirfairelinux/invalid;version=1.0.0'
2355 fetcher = bb.fetch.Fetch([url], self.d)
2356 with self.assertRaises(bb.fetch2.FetchError):
2357 fetcher.download()
2358
2359 @skipIfNoNpm()
2360 @skipIfNoNetwork()
2361 def test_npm_version_invalid(self):
2362 url = 'npm://registry.npmjs.org;package=@savoirfairelinux/node-server-example;version=invalid'
2363 with self.assertRaises(bb.fetch2.ParameterError):
2364 fetcher = bb.fetch.Fetch([url], self.d)
2365
2366 @skipIfNoNpm()
2367 @skipIfNoNetwork()
2368 def test_npm_registry_none(self):
2369 url = 'npm://;package=@savoirfairelinux/node-server-example;version=1.0.0'
2370 with self.assertRaises(bb.fetch2.MalformedUrl):
2371 fetcher = bb.fetch.Fetch([url], self.d)
2372
2373 @skipIfNoNpm()
2374 @skipIfNoNetwork()
2375 def test_npm_package_none(self):
2376 url = 'npm://registry.npmjs.org;version=1.0.0'
2377 with self.assertRaises(bb.fetch2.MissingParameterError):
2378 fetcher = bb.fetch.Fetch([url], self.d)
2379
2380 @skipIfNoNpm()
2381 @skipIfNoNetwork()
2382 def test_npm_version_none(self):
2383 url = 'npm://registry.npmjs.org;package=@savoirfairelinux/node-server-example'
2384 with self.assertRaises(bb.fetch2.MissingParameterError):
2385 fetcher = bb.fetch.Fetch([url], self.d)
2386
2387 def create_shrinkwrap_file(self, data):
2388 import json
2389 datadir = os.path.join(self.tempdir, 'data')
2390 swfile = os.path.join(datadir, 'npm-shrinkwrap.json')
2391 bb.utils.mkdirhier(datadir)
2392 with open(swfile, 'w') as f:
2393 json.dump(data, f)
2394 # Also configure the S directory
2395 self.sdir = os.path.join(self.unpackdir, 'S')
2396 self.d.setVar('S', self.sdir)
2397 return swfile
2398
2399 @skipIfNoNpm()
2400 @skipIfNoNetwork()
2401 def test_npmsw(self):
2402 swfile = self.create_shrinkwrap_file({
2403 'dependencies': {
2404 'array-flatten': {
2405 'version': '1.1.1',
2406 'resolved': 'https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz',
2407 'integrity': 'sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=',
2408 'dependencies': {
2409 'content-type': {
2410 'version': 'https://registry.npmjs.org/content-type/-/content-type-1.0.4.tgz',
2411 'integrity': 'sha512-hIP3EEPs8tB9AT1L+NUqtwOAps4mk2Zob89MWXMHjHWg9milF/j4osnnQLXBCBFBk/tvIG/tUc9mOUJiPBhPXA==',
2412 'dependencies': {
2413 'cookie': {
2414 'version': 'git+https://github.com/jshttp/cookie.git#aec1177c7da67e3b3273df96cf476824dbc9ae09',
2415 'from': 'git+https://github.com/jshttp/cookie.git'
2416 }
2417 }
2418 }
2419 }
2420 }
2421 }
2422 })
2423 fetcher = bb.fetch.Fetch(['npmsw://' + swfile], self.d)
2424 fetcher.download()
2425 self.assertTrue(os.path.exists(os.path.join(self.dldir, 'npm2', 'array-flatten-1.1.1.tgz')))
2426 self.assertTrue(os.path.exists(os.path.join(self.dldir, 'npm2', 'content-type-1.0.4.tgz')))
2427 self.assertTrue(os.path.exists(os.path.join(self.dldir, 'git2', 'github.com.jshttp.cookie.git')))
2428 fetcher.unpack(self.unpackdir)
2429 self.assertTrue(os.path.exists(os.path.join(self.sdir, 'npm-shrinkwrap.json')))
2430 self.assertTrue(os.path.exists(os.path.join(self.sdir, 'node_modules', 'array-flatten', 'package.json')))
2431 self.assertTrue(os.path.exists(os.path.join(self.sdir, 'node_modules', 'array-flatten', 'node_modules', 'content-type', 'package.json')))
2432 self.assertTrue(os.path.exists(os.path.join(self.sdir, 'node_modules', 'array-flatten', 'node_modules', 'content-type', 'node_modules', 'cookie', 'package.json')))
2433
2434 @skipIfNoNpm()
2435 @skipIfNoNetwork()
2436 def test_npmsw_dev(self):
2437 swfile = self.create_shrinkwrap_file({
2438 'dependencies': {
2439 'array-flatten': {
2440 'version': '1.1.1',
2441 'resolved': 'https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz',
2442 'integrity': 'sha1-ml9pkFGx5wczKPKgCJaLZOopVdI='
2443 },
2444 'content-type': {
2445 'version': '1.0.4',
2446 'resolved': 'https://registry.npmjs.org/content-type/-/content-type-1.0.4.tgz',
2447 'integrity': 'sha512-hIP3EEPs8tB9AT1L+NUqtwOAps4mk2Zob89MWXMHjHWg9milF/j4osnnQLXBCBFBk/tvIG/tUc9mOUJiPBhPXA==',
2448 'dev': True
2449 }
2450 }
2451 })
2452 # Fetch with dev disabled
2453 fetcher = bb.fetch.Fetch(['npmsw://' + swfile], self.d)
2454 fetcher.download()
2455 self.assertTrue(os.path.exists(os.path.join(self.dldir, 'npm2', 'array-flatten-1.1.1.tgz')))
2456 self.assertFalse(os.path.exists(os.path.join(self.dldir, 'npm2', 'content-type-1.0.4.tgz')))
2457 # Fetch with dev enabled
2458 fetcher = bb.fetch.Fetch(['npmsw://' + swfile + ';dev=1'], self.d)
2459 fetcher.download()
2460 self.assertTrue(os.path.exists(os.path.join(self.dldir, 'npm2', 'array-flatten-1.1.1.tgz')))
2461 self.assertTrue(os.path.exists(os.path.join(self.dldir, 'npm2', 'content-type-1.0.4.tgz')))
2462
2463 @skipIfNoNpm()
2464 @skipIfNoNetwork()
2465 def test_npmsw_destsuffix(self):
2466 swfile = self.create_shrinkwrap_file({
2467 'dependencies': {
2468 'array-flatten': {
2469 'version': '1.1.1',
2470 'resolved': 'https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz',
2471 'integrity': 'sha1-ml9pkFGx5wczKPKgCJaLZOopVdI='
2472 }
2473 }
2474 })
2475 fetcher = bb.fetch.Fetch(['npmsw://' + swfile + ';destsuffix=foo/bar'], self.d)
2476 fetcher.download()
2477 fetcher.unpack(self.unpackdir)
2478 self.assertTrue(os.path.exists(os.path.join(self.unpackdir, 'foo', 'bar', 'node_modules', 'array-flatten', 'package.json')))
2479
2480 def test_npmsw_no_network_no_tarball(self):
2481 swfile = self.create_shrinkwrap_file({
2482 'dependencies': {
2483 'array-flatten': {
2484 'version': '1.1.1',
2485 'resolved': 'https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz',
2486 'integrity': 'sha1-ml9pkFGx5wczKPKgCJaLZOopVdI='
2487 }
2488 }
2489 })
2490 self.d.setVar('BB_NO_NETWORK', '1')
2491 fetcher = bb.fetch.Fetch(['npmsw://' + swfile], self.d)
2492 with self.assertRaises(bb.fetch2.NetworkAccess):
2493 fetcher.download()
2494
2495 @skipIfNoNpm()
2496 @skipIfNoNetwork()
2497 def test_npmsw_no_network_with_tarball(self):
2498 # Fetch once to get a tarball
2499 fetcher = bb.fetch.Fetch(['npm://registry.npmjs.org;package=array-flatten;version=1.1.1'], self.d)
2500 fetcher.download()
2501 # Disable network access
2502 self.d.setVar('BB_NO_NETWORK', '1')
2503 # Fetch again
2504 swfile = self.create_shrinkwrap_file({
2505 'dependencies': {
2506 'array-flatten': {
2507 'version': '1.1.1',
2508 'resolved': 'https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz',
2509 'integrity': 'sha1-ml9pkFGx5wczKPKgCJaLZOopVdI='
2510 }
2511 }
2512 })
2513 fetcher = bb.fetch.Fetch(['npmsw://' + swfile], self.d)
2514 fetcher.download()
2515 fetcher.unpack(self.unpackdir)
2516 self.assertTrue(os.path.exists(os.path.join(self.sdir, 'node_modules', 'array-flatten', 'package.json')))
2517
2518 @skipIfNoNpm()
2519 @skipIfNoNetwork()
2520 def test_npmsw_npm_reusability(self):
2521 # Fetch once with npmsw
2522 swfile = self.create_shrinkwrap_file({
2523 'dependencies': {
2524 'array-flatten': {
2525 'version': '1.1.1',
2526 'resolved': 'https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz',
2527 'integrity': 'sha1-ml9pkFGx5wczKPKgCJaLZOopVdI='
2528 }
2529 }
2530 })
2531 fetcher = bb.fetch.Fetch(['npmsw://' + swfile], self.d)
2532 fetcher.download()
2533 # Disable network access
2534 self.d.setVar('BB_NO_NETWORK', '1')
2535 # Fetch again with npm
2536 fetcher = bb.fetch.Fetch(['npm://registry.npmjs.org;package=array-flatten;version=1.1.1'], self.d)
2537 fetcher.download()
2538 fetcher.unpack(self.unpackdir)
2539 self.assertTrue(os.path.exists(os.path.join(self.unpackdir, 'npm', 'package.json')))
2540
2541 @skipIfNoNpm()
2542 @skipIfNoNetwork()
2543 def test_npmsw_bad_checksum(self):
2544 # Try to fetch with bad checksum
2545 swfile = self.create_shrinkwrap_file({
2546 'dependencies': {
2547 'array-flatten': {
2548 'version': '1.1.1',
2549 'resolved': 'https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz',
2550 'integrity': 'sha1-gfNEp2hqgLTFKT6P3AsBYMgsBqg='
2551 }
2552 }
2553 })
2554 fetcher = bb.fetch.Fetch(['npmsw://' + swfile], self.d)
2555 with self.assertRaises(bb.fetch2.FetchError):
2556 fetcher.download()
2557 # Fetch correctly to get a tarball
2558 swfile = self.create_shrinkwrap_file({
2559 'dependencies': {
2560 'array-flatten': {
2561 'version': '1.1.1',
2562 'resolved': 'https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz',
2563 'integrity': 'sha1-ml9pkFGx5wczKPKgCJaLZOopVdI='
2564 }
2565 }
2566 })
2567 fetcher = bb.fetch.Fetch(['npmsw://' + swfile], self.d)
2568 fetcher.download()
2569 localpath = os.path.join(self.dldir, 'npm2', 'array-flatten-1.1.1.tgz')
2570 self.assertTrue(os.path.exists(localpath))
2571 # Modify the tarball
2572 bad = b'bad checksum'
2573 with open(localpath, 'wb') as f:
2574 f.write(bad)
2575 # Verify that the tarball is fetched again
2576 fetcher.download()
2577 badsum = hashlib.sha1(bad).hexdigest()
2578 self.assertTrue(os.path.exists(localpath + '_bad-checksum_' + badsum))
2579 self.assertTrue(os.path.exists(localpath))
2580
2581 @skipIfNoNpm()
2582 @skipIfNoNetwork()
2583 def test_npmsw_premirrors(self):
2584 # Fetch once to get a tarball
2585 fetcher = bb.fetch.Fetch(['npm://registry.npmjs.org;package=array-flatten;version=1.1.1'], self.d)
2586 ud = fetcher.ud[fetcher.urls[0]]
2587 fetcher.download()
2588 self.assertTrue(os.path.exists(ud.localpath))
2589 # Setup the mirror
2590 mirrordir = os.path.join(self.tempdir, 'mirror')
2591 bb.utils.mkdirhier(mirrordir)
2592 os.replace(ud.localpath, os.path.join(mirrordir, os.path.basename(ud.localpath)))
2593 self.d.setVar('PREMIRRORS', 'https?$://.*/.* file://%s/\n' % mirrordir)
2594 self.d.setVar('BB_FETCH_PREMIRRORONLY', '1')
2595 # Fetch again
2596 self.assertFalse(os.path.exists(ud.localpath))
2597 swfile = self.create_shrinkwrap_file({
2598 'dependencies': {
2599 'array-flatten': {
2600 'version': '1.1.1',
2601 'resolved': 'https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz',
2602 'integrity': 'sha1-ml9pkFGx5wczKPKgCJaLZOopVdI='
2603 }
2604 }
2605 })
2606 fetcher = bb.fetch.Fetch(['npmsw://' + swfile], self.d)
2607 fetcher.download()
2608 self.assertTrue(os.path.exists(ud.localpath))
2609
2610 @skipIfNoNpm()
2611 @skipIfNoNetwork()
2612 def test_npmsw_mirrors(self):
2613 # Fetch once to get a tarball
2614 fetcher = bb.fetch.Fetch(['npm://registry.npmjs.org;package=array-flatten;version=1.1.1'], self.d)
2615 ud = fetcher.ud[fetcher.urls[0]]
2616 fetcher.download()
2617 self.assertTrue(os.path.exists(ud.localpath))
2618 # Setup the mirror
2619 mirrordir = os.path.join(self.tempdir, 'mirror')
2620 bb.utils.mkdirhier(mirrordir)
2621 os.replace(ud.localpath, os.path.join(mirrordir, os.path.basename(ud.localpath)))
2622 self.d.setVar('MIRRORS', 'https?$://.*/.* file://%s/\n' % mirrordir)
2623 # Fetch again with invalid url
2624 self.assertFalse(os.path.exists(ud.localpath))
2625 swfile = self.create_shrinkwrap_file({
2626 'dependencies': {
2627 'array-flatten': {
2628 'version': '1.1.1',
2629 'resolved': 'https://invalid',
2630 'integrity': 'sha1-ml9pkFGx5wczKPKgCJaLZOopVdI='
2631 }
2632 }
2633 })
2634 fetcher = bb.fetch.Fetch(['npmsw://' + swfile], self.d)
2635 fetcher.download()
2636 self.assertTrue(os.path.exists(ud.localpath))