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