fetch.py 111 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515
  1. #
  2. # BitBake Tests for the Fetcher (fetch2/)
  3. #
  4. # Copyright (C) 2012 Richard Purdie
  5. #
  6. # SPDX-License-Identifier: GPL-2.0-only
  7. #
  8. import unittest
  9. import hashlib
  10. import tempfile
  11. import collections
  12. import os
  13. from bb.fetch2 import URI
  14. from bb.fetch2 import FetchMethod
  15. import bb
  16. from bb.tests.support.httpserver import HTTPService
  17. def skipIfNoNetwork():
  18. if os.environ.get("BB_SKIP_NETTESTS") == "yes":
  19. return unittest.skip("Network tests being skipped")
  20. return lambda f: f
  21. class URITest(unittest.TestCase):
  22. test_uris = {
  23. "http://www.google.com/index.html" : {
  24. 'uri': 'http://www.google.com/index.html',
  25. 'scheme': 'http',
  26. 'hostname': 'www.google.com',
  27. 'port': None,
  28. 'hostport': 'www.google.com',
  29. 'path': '/index.html',
  30. 'userinfo': '',
  31. 'username': '',
  32. 'password': '',
  33. 'params': {},
  34. 'query': {},
  35. 'relative': False
  36. },
  37. "http://www.google.com/index.html;param1=value1" : {
  38. 'uri': 'http://www.google.com/index.html;param1=value1',
  39. 'scheme': 'http',
  40. 'hostname': 'www.google.com',
  41. 'port': None,
  42. 'hostport': 'www.google.com',
  43. 'path': '/index.html',
  44. 'userinfo': '',
  45. 'username': '',
  46. 'password': '',
  47. 'params': {
  48. 'param1': 'value1'
  49. },
  50. 'query': {},
  51. 'relative': False
  52. },
  53. "http://www.example.org/index.html?param1=value1" : {
  54. 'uri': 'http://www.example.org/index.html?param1=value1',
  55. 'scheme': 'http',
  56. 'hostname': 'www.example.org',
  57. 'port': None,
  58. 'hostport': 'www.example.org',
  59. 'path': '/index.html',
  60. 'userinfo': '',
  61. 'username': '',
  62. 'password': '',
  63. 'params': {},
  64. 'query': {
  65. 'param1': 'value1'
  66. },
  67. 'relative': False
  68. },
  69. "http://www.example.org/index.html?qparam1=qvalue1;param2=value2" : {
  70. 'uri': 'http://www.example.org/index.html?qparam1=qvalue1;param2=value2',
  71. 'scheme': 'http',
  72. 'hostname': 'www.example.org',
  73. 'port': None,
  74. 'hostport': 'www.example.org',
  75. 'path': '/index.html',
  76. 'userinfo': '',
  77. 'username': '',
  78. 'password': '',
  79. 'params': {
  80. 'param2': 'value2'
  81. },
  82. 'query': {
  83. 'qparam1': 'qvalue1'
  84. },
  85. 'relative': False
  86. },
  87. "http://www.example.com:8080/index.html" : {
  88. 'uri': 'http://www.example.com:8080/index.html',
  89. 'scheme': 'http',
  90. 'hostname': 'www.example.com',
  91. 'port': 8080,
  92. 'hostport': 'www.example.com:8080',
  93. 'path': '/index.html',
  94. 'userinfo': '',
  95. 'username': '',
  96. 'password': '',
  97. 'params': {},
  98. 'query': {},
  99. 'relative': False
  100. },
  101. "cvs://anoncvs@cvs.handhelds.org/cvs;module=familiar/dist/ipkg" : {
  102. 'uri': 'cvs://anoncvs@cvs.handhelds.org/cvs;module=familiar/dist/ipkg',
  103. 'scheme': 'cvs',
  104. 'hostname': 'cvs.handhelds.org',
  105. 'port': None,
  106. 'hostport': 'cvs.handhelds.org',
  107. 'path': '/cvs',
  108. 'userinfo': 'anoncvs',
  109. 'username': 'anoncvs',
  110. 'password': '',
  111. 'params': {
  112. 'module': 'familiar/dist/ipkg'
  113. },
  114. 'query': {},
  115. 'relative': False
  116. },
  117. "cvs://anoncvs:anonymous@cvs.handhelds.org/cvs;tag=V0-99-81;module=familiar/dist/ipkg": {
  118. 'uri': 'cvs://anoncvs:anonymous@cvs.handhelds.org/cvs;tag=V0-99-81;module=familiar/dist/ipkg',
  119. 'scheme': 'cvs',
  120. 'hostname': 'cvs.handhelds.org',
  121. 'port': None,
  122. 'hostport': 'cvs.handhelds.org',
  123. 'path': '/cvs',
  124. 'userinfo': 'anoncvs:anonymous',
  125. 'username': 'anoncvs',
  126. 'password': 'anonymous',
  127. 'params': collections.OrderedDict([
  128. ('tag', 'V0-99-81'),
  129. ('module', 'familiar/dist/ipkg')
  130. ]),
  131. 'query': {},
  132. 'relative': False
  133. },
  134. "file://example.diff": { # NOTE: Not RFC compliant!
  135. 'uri': 'file:example.diff',
  136. 'scheme': 'file',
  137. 'hostname': '',
  138. 'port': None,
  139. 'hostport': '',
  140. 'path': 'example.diff',
  141. 'userinfo': '',
  142. 'username': '',
  143. 'password': '',
  144. 'params': {},
  145. 'query': {},
  146. 'relative': True
  147. },
  148. "file:example.diff": { # NOTE: RFC compliant version of the former
  149. 'uri': 'file:example.diff',
  150. 'scheme': 'file',
  151. 'hostname': '',
  152. 'port': None,
  153. 'hostport': '',
  154. 'path': 'example.diff',
  155. 'userinfo': '',
  156. 'userinfo': '',
  157. 'username': '',
  158. 'password': '',
  159. 'params': {},
  160. 'query': {},
  161. 'relative': True
  162. },
  163. "file:///tmp/example.diff": {
  164. 'uri': 'file:///tmp/example.diff',
  165. 'scheme': 'file',
  166. 'hostname': '',
  167. 'port': None,
  168. 'hostport': '',
  169. 'path': '/tmp/example.diff',
  170. 'userinfo': '',
  171. 'userinfo': '',
  172. 'username': '',
  173. 'password': '',
  174. 'params': {},
  175. 'query': {},
  176. 'relative': False
  177. },
  178. "git:///path/example.git": {
  179. 'uri': 'git:///path/example.git',
  180. 'scheme': 'git',
  181. 'hostname': '',
  182. 'port': None,
  183. 'hostport': '',
  184. 'path': '/path/example.git',
  185. 'userinfo': '',
  186. 'userinfo': '',
  187. 'username': '',
  188. 'password': '',
  189. 'params': {},
  190. 'query': {},
  191. 'relative': False
  192. },
  193. "git:path/example.git": {
  194. 'uri': 'git:path/example.git',
  195. 'scheme': 'git',
  196. 'hostname': '',
  197. 'port': None,
  198. 'hostport': '',
  199. 'path': 'path/example.git',
  200. 'userinfo': '',
  201. 'userinfo': '',
  202. 'username': '',
  203. 'password': '',
  204. 'params': {},
  205. 'query': {},
  206. 'relative': True
  207. },
  208. "git://example.net/path/example.git": {
  209. 'uri': 'git://example.net/path/example.git',
  210. 'scheme': 'git',
  211. 'hostname': 'example.net',
  212. 'port': None,
  213. 'hostport': 'example.net',
  214. 'path': '/path/example.git',
  215. 'userinfo': '',
  216. 'userinfo': '',
  217. 'username': '',
  218. 'password': '',
  219. 'params': {},
  220. 'query': {},
  221. 'relative': False
  222. },
  223. "http://somesite.net;someparam=1": {
  224. 'uri': 'http://somesite.net;someparam=1',
  225. 'scheme': 'http',
  226. 'hostname': 'somesite.net',
  227. 'port': None,
  228. 'hostport': 'somesite.net',
  229. 'path': '',
  230. 'userinfo': '',
  231. 'userinfo': '',
  232. 'username': '',
  233. 'password': '',
  234. 'params': {"someparam" : "1"},
  235. 'query': {},
  236. 'relative': False
  237. },
  238. "file://somelocation;someparam=1": {
  239. 'uri': 'file:somelocation;someparam=1',
  240. 'scheme': 'file',
  241. 'hostname': '',
  242. 'port': None,
  243. 'hostport': '',
  244. 'path': 'somelocation',
  245. 'userinfo': '',
  246. 'userinfo': '',
  247. 'username': '',
  248. 'password': '',
  249. 'params': {"someparam" : "1"},
  250. 'query': {},
  251. 'relative': True
  252. }
  253. }
  254. def test_uri(self):
  255. for test_uri, ref in self.test_uris.items():
  256. uri = URI(test_uri)
  257. self.assertEqual(str(uri), ref['uri'])
  258. # expected attributes
  259. self.assertEqual(uri.scheme, ref['scheme'])
  260. self.assertEqual(uri.userinfo, ref['userinfo'])
  261. self.assertEqual(uri.username, ref['username'])
  262. self.assertEqual(uri.password, ref['password'])
  263. self.assertEqual(uri.hostname, ref['hostname'])
  264. self.assertEqual(uri.port, ref['port'])
  265. self.assertEqual(uri.hostport, ref['hostport'])
  266. self.assertEqual(uri.path, ref['path'])
  267. self.assertEqual(uri.params, ref['params'])
  268. self.assertEqual(uri.relative, ref['relative'])
  269. def test_dict(self):
  270. for test in self.test_uris.values():
  271. uri = URI()
  272. self.assertEqual(uri.scheme, '')
  273. self.assertEqual(uri.userinfo, '')
  274. self.assertEqual(uri.username, '')
  275. self.assertEqual(uri.password, '')
  276. self.assertEqual(uri.hostname, '')
  277. self.assertEqual(uri.port, None)
  278. self.assertEqual(uri.path, '')
  279. self.assertEqual(uri.params, {})
  280. uri.scheme = test['scheme']
  281. self.assertEqual(uri.scheme, test['scheme'])
  282. uri.userinfo = test['userinfo']
  283. self.assertEqual(uri.userinfo, test['userinfo'])
  284. self.assertEqual(uri.username, test['username'])
  285. self.assertEqual(uri.password, test['password'])
  286. # make sure changing the values doesn't do anything unexpected
  287. uri.username = 'changeme'
  288. self.assertEqual(uri.username, 'changeme')
  289. self.assertEqual(uri.password, test['password'])
  290. uri.password = 'insecure'
  291. self.assertEqual(uri.username, 'changeme')
  292. self.assertEqual(uri.password, 'insecure')
  293. # reset back after our trickery
  294. uri.userinfo = test['userinfo']
  295. self.assertEqual(uri.userinfo, test['userinfo'])
  296. self.assertEqual(uri.username, test['username'])
  297. self.assertEqual(uri.password, test['password'])
  298. uri.hostname = test['hostname']
  299. self.assertEqual(uri.hostname, test['hostname'])
  300. self.assertEqual(uri.hostport, test['hostname'])
  301. uri.port = test['port']
  302. self.assertEqual(uri.port, test['port'])
  303. self.assertEqual(uri.hostport, test['hostport'])
  304. uri.path = test['path']
  305. self.assertEqual(uri.path, test['path'])
  306. uri.params = test['params']
  307. self.assertEqual(uri.params, test['params'])
  308. uri.query = test['query']
  309. self.assertEqual(uri.query, test['query'])
  310. self.assertEqual(str(uri), test['uri'])
  311. uri.params = {}
  312. self.assertEqual(uri.params, {})
  313. self.assertEqual(str(uri), (str(uri).split(";"))[0])
  314. class FetcherTest(unittest.TestCase):
  315. def setUp(self):
  316. self.origdir = os.getcwd()
  317. self.d = bb.data.init()
  318. self.tempdir = tempfile.mkdtemp()
  319. self.dldir = os.path.join(self.tempdir, "download")
  320. os.mkdir(self.dldir)
  321. self.d.setVar("DL_DIR", self.dldir)
  322. self.unpackdir = os.path.join(self.tempdir, "unpacked")
  323. os.mkdir(self.unpackdir)
  324. persistdir = os.path.join(self.tempdir, "persistdata")
  325. self.d.setVar("PERSISTENT_DIR", persistdir)
  326. def tearDown(self):
  327. os.chdir(self.origdir)
  328. if os.environ.get("BB_TMPDIR_NOCLEAN") == "yes":
  329. print("Not cleaning up %s. Please remove manually." % self.tempdir)
  330. else:
  331. bb.utils.prunedir(self.tempdir)
  332. class MirrorUriTest(FetcherTest):
  333. replaceuris = {
  334. ("git://git.invalid.infradead.org/mtd-utils.git;tag=1234567890123456789012345678901234567890", "git://.*/.*", "http://somewhere.org/somedir/")
  335. : "http://somewhere.org/somedir/git2_git.invalid.infradead.org.mtd-utils.git.tar.gz",
  336. ("git://git.invalid.infradead.org/mtd-utils.git;tag=1234567890123456789012345678901234567890", "git://.*/([^/]+/)*([^/]*)", "git://somewhere.org/somedir/\\2;protocol=http")
  337. : "git://somewhere.org/somedir/mtd-utils.git;tag=1234567890123456789012345678901234567890;protocol=http",
  338. ("git://git.invalid.infradead.org/foo/mtd-utils.git;tag=1234567890123456789012345678901234567890", "git://.*/([^/]+/)*([^/]*)", "git://somewhere.org/somedir/\\2;protocol=http")
  339. : "git://somewhere.org/somedir/mtd-utils.git;tag=1234567890123456789012345678901234567890;protocol=http",
  340. ("git://git.invalid.infradead.org/foo/mtd-utils.git;tag=1234567890123456789012345678901234567890", "git://.*/([^/]+/)*([^/]*)", "git://somewhere.org/\\2;protocol=http")
  341. : "git://somewhere.org/mtd-utils.git;tag=1234567890123456789012345678901234567890;protocol=http",
  342. ("git://someserver.org/bitbake;tag=1234567890123456789012345678901234567890", "git://someserver.org/bitbake", "git://git.openembedded.org/bitbake")
  343. : "git://git.openembedded.org/bitbake;tag=1234567890123456789012345678901234567890",
  344. ("file://sstate-xyz.tgz", "file://.*", "file:///somewhere/1234/sstate-cache")
  345. : "file:///somewhere/1234/sstate-cache/sstate-xyz.tgz",
  346. ("file://sstate-xyz.tgz", "file://.*", "file:///somewhere/1234/sstate-cache/")
  347. : "file:///somewhere/1234/sstate-cache/sstate-xyz.tgz",
  348. ("http://somewhere.org/somedir1/somedir2/somefile_1.2.3.tar.gz", "http://.*/.*", "http://somewhere2.org/somedir3")
  349. : "http://somewhere2.org/somedir3/somefile_1.2.3.tar.gz",
  350. ("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")
  351. : "http://somewhere2.org/somedir3/somefile_1.2.3.tar.gz",
  352. ("http://www.apache.org/dist/subversion/subversion-1.7.1.tar.bz2", "http://www.apache.org/dist", "http://archive.apache.org/dist")
  353. : "http://archive.apache.org/dist/subversion/subversion-1.7.1.tar.bz2",
  354. ("http://www.apache.org/dist/subversion/subversion-1.7.1.tar.bz2", "http://.*/.*", "file:///somepath/downloads/")
  355. : "file:///somepath/downloads/subversion-1.7.1.tar.bz2",
  356. ("git://git.invalid.infradead.org/mtd-utils.git;tag=1234567890123456789012345678901234567890", "git://.*/.*", "git://somewhere.org/somedir/BASENAME;protocol=http")
  357. : "git://somewhere.org/somedir/mtd-utils.git;tag=1234567890123456789012345678901234567890;protocol=http",
  358. ("git://git.invalid.infradead.org/foo/mtd-utils.git;tag=1234567890123456789012345678901234567890", "git://.*/.*", "git://somewhere.org/somedir/BASENAME;protocol=http")
  359. : "git://somewhere.org/somedir/mtd-utils.git;tag=1234567890123456789012345678901234567890;protocol=http",
  360. ("git://git.invalid.infradead.org/foo/mtd-utils.git;tag=1234567890123456789012345678901234567890", "git://.*/.*", "git://somewhere.org/somedir/MIRRORNAME;protocol=http")
  361. : "git://somewhere.org/somedir/git.invalid.infradead.org.foo.mtd-utils.git;tag=1234567890123456789012345678901234567890;protocol=http",
  362. ("http://somewhere.org/somedir1/somedir2/somefile_1.2.3.tar.gz", "http://.*/.*", "http://somewhere2.org")
  363. : "http://somewhere2.org/somefile_1.2.3.tar.gz",
  364. ("http://somewhere.org/somedir1/somedir2/somefile_1.2.3.tar.gz", "http://.*/.*", "http://somewhere2.org/")
  365. : "http://somewhere2.org/somefile_1.2.3.tar.gz",
  366. ("git://someserver.org/bitbake;tag=1234567890123456789012345678901234567890;branch=master", "git://someserver.org/bitbake;branch=master", "git://git.openembedded.org/bitbake;protocol=http")
  367. : "git://git.openembedded.org/bitbake;tag=1234567890123456789012345678901234567890;branch=master;protocol=http",
  368. #Renaming files doesn't work
  369. #("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"
  370. #("file://sstate-xyz.tgz", "file://.*/.*", "file:///somewhere/1234/sstate-cache") : "file:///somewhere/1234/sstate-cache/sstate-xyz.tgz",
  371. }
  372. mirrorvar = "http://.*/.* file:///somepath/downloads/ \n" \
  373. "git://someserver.org/bitbake git://git.openembedded.org/bitbake \n" \
  374. "https://.*/.* file:///someotherpath/downloads/ \n" \
  375. "http://.*/.* file:///someotherpath/downloads/ \n"
  376. def test_urireplace(self):
  377. for k, v in self.replaceuris.items():
  378. ud = bb.fetch.FetchData(k[0], self.d)
  379. ud.setup_localpath(self.d)
  380. mirrors = bb.fetch2.mirror_from_string("%s %s" % (k[1], k[2]))
  381. newuris, uds = bb.fetch2.build_mirroruris(ud, mirrors, self.d)
  382. self.assertEqual([v], newuris)
  383. def test_urilist1(self):
  384. fetcher = bb.fetch.FetchData("http://downloads.yoctoproject.org/releases/bitbake/bitbake-1.0.tar.gz", self.d)
  385. mirrors = bb.fetch2.mirror_from_string(self.mirrorvar)
  386. uris, uds = bb.fetch2.build_mirroruris(fetcher, mirrors, self.d)
  387. self.assertEqual(uris, ['file:///somepath/downloads/bitbake-1.0.tar.gz', 'file:///someotherpath/downloads/bitbake-1.0.tar.gz'])
  388. def test_urilist2(self):
  389. # Catch https:// -> files:// bug
  390. fetcher = bb.fetch.FetchData("https://downloads.yoctoproject.org/releases/bitbake/bitbake-1.0.tar.gz", self.d)
  391. mirrors = bb.fetch2.mirror_from_string(self.mirrorvar)
  392. uris, uds = bb.fetch2.build_mirroruris(fetcher, mirrors, self.d)
  393. self.assertEqual(uris, ['file:///someotherpath/downloads/bitbake-1.0.tar.gz'])
  394. def test_mirror_of_mirror(self):
  395. # Test if mirror of a mirror works
  396. mirrorvar = self.mirrorvar + " http://.*/.* http://otherdownloads.yoctoproject.org/downloads/ \n"
  397. mirrorvar = mirrorvar + " http://otherdownloads.yoctoproject.org/.* http://downloads2.yoctoproject.org/downloads/ \n"
  398. fetcher = bb.fetch.FetchData("http://downloads.yoctoproject.org/releases/bitbake/bitbake-1.0.tar.gz", self.d)
  399. mirrors = bb.fetch2.mirror_from_string(mirrorvar)
  400. uris, uds = bb.fetch2.build_mirroruris(fetcher, mirrors, self.d)
  401. self.assertEqual(uris, ['file:///somepath/downloads/bitbake-1.0.tar.gz',
  402. 'file:///someotherpath/downloads/bitbake-1.0.tar.gz',
  403. 'http://otherdownloads.yoctoproject.org/downloads/bitbake-1.0.tar.gz',
  404. 'http://downloads2.yoctoproject.org/downloads/bitbake-1.0.tar.gz'])
  405. recmirrorvar = "https://.*/[^/]* http://AAAA/A/A/A/ \n" \
  406. "https://.*/[^/]* https://BBBB/B/B/B/ \n"
  407. def test_recursive(self):
  408. fetcher = bb.fetch.FetchData("https://downloads.yoctoproject.org/releases/bitbake/bitbake-1.0.tar.gz", self.d)
  409. mirrors = bb.fetch2.mirror_from_string(self.recmirrorvar)
  410. uris, uds = bb.fetch2.build_mirroruris(fetcher, mirrors, self.d)
  411. self.assertEqual(uris, ['http://AAAA/A/A/A/bitbake/bitbake-1.0.tar.gz',
  412. 'https://BBBB/B/B/B/bitbake/bitbake-1.0.tar.gz',
  413. 'http://AAAA/A/A/A/B/B/bitbake/bitbake-1.0.tar.gz'])
  414. class GitDownloadDirectoryNamingTest(FetcherTest):
  415. def setUp(self):
  416. super(GitDownloadDirectoryNamingTest, self).setUp()
  417. self.recipe_url = "git://git.openembedded.org/bitbake"
  418. self.recipe_dir = "git.openembedded.org.bitbake"
  419. self.mirror_url = "git://github.com/openembedded/bitbake.git"
  420. self.mirror_dir = "github.com.openembedded.bitbake.git"
  421. self.d.setVar('SRCREV', '82ea737a0b42a8b53e11c9cde141e9e9c0bd8c40')
  422. def setup_mirror_rewrite(self):
  423. self.d.setVar("PREMIRRORS", self.recipe_url + " " + self.mirror_url + " \n")
  424. @skipIfNoNetwork()
  425. def test_that_directory_is_named_after_recipe_url_when_no_mirroring_is_used(self):
  426. self.setup_mirror_rewrite()
  427. fetcher = bb.fetch.Fetch([self.recipe_url], self.d)
  428. fetcher.download()
  429. dir = os.listdir(self.dldir + "/git2")
  430. self.assertIn(self.recipe_dir, dir)
  431. @skipIfNoNetwork()
  432. def test_that_directory_exists_for_mirrored_url_and_recipe_url_when_mirroring_is_used(self):
  433. self.setup_mirror_rewrite()
  434. fetcher = bb.fetch.Fetch([self.recipe_url], self.d)
  435. fetcher.download()
  436. dir = os.listdir(self.dldir + "/git2")
  437. self.assertIn(self.mirror_dir, dir)
  438. self.assertIn(self.recipe_dir, dir)
  439. @skipIfNoNetwork()
  440. def test_that_recipe_directory_and_mirrored_directory_exists_when_mirroring_is_used_and_the_mirrored_directory_already_exists(self):
  441. self.setup_mirror_rewrite()
  442. fetcher = bb.fetch.Fetch([self.mirror_url], self.d)
  443. fetcher.download()
  444. fetcher = bb.fetch.Fetch([self.recipe_url], self.d)
  445. fetcher.download()
  446. dir = os.listdir(self.dldir + "/git2")
  447. self.assertIn(self.mirror_dir, dir)
  448. self.assertIn(self.recipe_dir, dir)
  449. class TarballNamingTest(FetcherTest):
  450. def setUp(self):
  451. super(TarballNamingTest, self).setUp()
  452. self.recipe_url = "git://git.openembedded.org/bitbake"
  453. self.recipe_tarball = "git2_git.openembedded.org.bitbake.tar.gz"
  454. self.mirror_url = "git://github.com/openembedded/bitbake.git"
  455. self.mirror_tarball = "git2_github.com.openembedded.bitbake.git.tar.gz"
  456. self.d.setVar('BB_GENERATE_MIRROR_TARBALLS', '1')
  457. self.d.setVar('SRCREV', '82ea737a0b42a8b53e11c9cde141e9e9c0bd8c40')
  458. def setup_mirror_rewrite(self):
  459. self.d.setVar("PREMIRRORS", self.recipe_url + " " + self.mirror_url + " \n")
  460. @skipIfNoNetwork()
  461. def test_that_the_recipe_tarball_is_created_when_no_mirroring_is_used(self):
  462. fetcher = bb.fetch.Fetch([self.recipe_url], self.d)
  463. fetcher.download()
  464. dir = os.listdir(self.dldir)
  465. self.assertIn(self.recipe_tarball, dir)
  466. @skipIfNoNetwork()
  467. def test_that_the_mirror_tarball_is_created_when_mirroring_is_used(self):
  468. self.setup_mirror_rewrite()
  469. fetcher = bb.fetch.Fetch([self.recipe_url], self.d)
  470. fetcher.download()
  471. dir = os.listdir(self.dldir)
  472. self.assertIn(self.mirror_tarball, dir)
  473. class GitShallowTarballNamingTest(FetcherTest):
  474. def setUp(self):
  475. super(GitShallowTarballNamingTest, self).setUp()
  476. self.recipe_url = "git://git.openembedded.org/bitbake"
  477. self.recipe_tarball = "gitshallow_git.openembedded.org.bitbake_82ea737-1_master.tar.gz"
  478. self.mirror_url = "git://github.com/openembedded/bitbake.git"
  479. self.mirror_tarball = "gitshallow_github.com.openembedded.bitbake.git_82ea737-1_master.tar.gz"
  480. self.d.setVar('BB_GIT_SHALLOW', '1')
  481. self.d.setVar('BB_GENERATE_SHALLOW_TARBALLS', '1')
  482. self.d.setVar('SRCREV', '82ea737a0b42a8b53e11c9cde141e9e9c0bd8c40')
  483. def setup_mirror_rewrite(self):
  484. self.d.setVar("PREMIRRORS", self.recipe_url + " " + self.mirror_url + " \n")
  485. @skipIfNoNetwork()
  486. def test_that_the_tarball_is_named_after_recipe_url_when_no_mirroring_is_used(self):
  487. fetcher = bb.fetch.Fetch([self.recipe_url], self.d)
  488. fetcher.download()
  489. dir = os.listdir(self.dldir)
  490. self.assertIn(self.recipe_tarball, dir)
  491. @skipIfNoNetwork()
  492. def test_that_the_mirror_tarball_is_created_when_mirroring_is_used(self):
  493. self.setup_mirror_rewrite()
  494. fetcher = bb.fetch.Fetch([self.recipe_url], self.d)
  495. fetcher.download()
  496. dir = os.listdir(self.dldir)
  497. self.assertIn(self.mirror_tarball, dir)
  498. class FetcherLocalTest(FetcherTest):
  499. def setUp(self):
  500. def touch(fn):
  501. with open(fn, 'a'):
  502. os.utime(fn, None)
  503. super(FetcherLocalTest, self).setUp()
  504. self.localsrcdir = os.path.join(self.tempdir, 'localsrc')
  505. os.makedirs(self.localsrcdir)
  506. touch(os.path.join(self.localsrcdir, 'a'))
  507. touch(os.path.join(self.localsrcdir, 'b'))
  508. os.makedirs(os.path.join(self.localsrcdir, 'dir'))
  509. touch(os.path.join(self.localsrcdir, 'dir', 'c'))
  510. touch(os.path.join(self.localsrcdir, 'dir', 'd'))
  511. os.makedirs(os.path.join(self.localsrcdir, 'dir', 'subdir'))
  512. touch(os.path.join(self.localsrcdir, 'dir', 'subdir', 'e'))
  513. self.d.setVar("FILESPATH", self.localsrcdir)
  514. def fetchUnpack(self, uris):
  515. fetcher = bb.fetch.Fetch(uris, self.d)
  516. fetcher.download()
  517. fetcher.unpack(self.unpackdir)
  518. flst = []
  519. for root, dirs, files in os.walk(self.unpackdir):
  520. for f in files:
  521. flst.append(os.path.relpath(os.path.join(root, f), self.unpackdir))
  522. flst.sort()
  523. return flst
  524. def test_local(self):
  525. tree = self.fetchUnpack(['file://a', 'file://dir/c'])
  526. self.assertEqual(tree, ['a', 'dir/c'])
  527. def test_local_wildcard(self):
  528. tree = self.fetchUnpack(['file://a', 'file://dir/*'])
  529. self.assertEqual(tree, ['a', 'dir/c', 'dir/d', 'dir/subdir/e'])
  530. def test_local_dir(self):
  531. tree = self.fetchUnpack(['file://a', 'file://dir'])
  532. self.assertEqual(tree, ['a', 'dir/c', 'dir/d', 'dir/subdir/e'])
  533. def test_local_subdir(self):
  534. tree = self.fetchUnpack(['file://dir/subdir'])
  535. self.assertEqual(tree, ['dir/subdir/e'])
  536. def test_local_subdir_file(self):
  537. tree = self.fetchUnpack(['file://dir/subdir/e'])
  538. self.assertEqual(tree, ['dir/subdir/e'])
  539. def test_local_subdirparam(self):
  540. tree = self.fetchUnpack(['file://a;subdir=bar', 'file://dir;subdir=foo/moo'])
  541. self.assertEqual(tree, ['bar/a', 'foo/moo/dir/c', 'foo/moo/dir/d', 'foo/moo/dir/subdir/e'])
  542. def test_local_deepsubdirparam(self):
  543. tree = self.fetchUnpack(['file://dir/subdir/e;subdir=bar'])
  544. self.assertEqual(tree, ['bar/dir/subdir/e'])
  545. def test_local_absolutedir(self):
  546. # Unpacking to an absolute path that is a subdirectory of the root
  547. # should work
  548. tree = self.fetchUnpack(['file://a;subdir=%s' % os.path.join(self.unpackdir, 'bar')])
  549. # Unpacking to an absolute path outside of the root should fail
  550. with self.assertRaises(bb.fetch2.UnpackError):
  551. self.fetchUnpack(['file://a;subdir=/bin/sh'])
  552. class FetcherNoNetworkTest(FetcherTest):
  553. def setUp(self):
  554. super().setUp()
  555. # all test cases are based on not having network
  556. self.d.setVar("BB_NO_NETWORK", "1")
  557. def test_missing(self):
  558. string = "this is a test file\n".encode("utf-8")
  559. self.d.setVarFlag("SRC_URI", "md5sum", hashlib.md5(string).hexdigest())
  560. self.d.setVarFlag("SRC_URI", "sha256sum", hashlib.sha256(string).hexdigest())
  561. self.assertFalse(os.path.exists(os.path.join(self.dldir, "test-file.tar.gz")))
  562. self.assertFalse(os.path.exists(os.path.join(self.dldir, "test-file.tar.gz.done")))
  563. fetcher = bb.fetch.Fetch(["http://invalid.yoctoproject.org/test-file.tar.gz"], self.d)
  564. with self.assertRaises(bb.fetch2.NetworkAccess):
  565. fetcher.download()
  566. def test_valid_missing_donestamp(self):
  567. # create the file in the download directory with correct hash
  568. string = "this is a test file\n".encode("utf-8")
  569. with open(os.path.join(self.dldir, "test-file.tar.gz"), "wb") as f:
  570. f.write(string)
  571. self.d.setVarFlag("SRC_URI", "md5sum", hashlib.md5(string).hexdigest())
  572. self.d.setVarFlag("SRC_URI", "sha256sum", hashlib.sha256(string).hexdigest())
  573. self.assertTrue(os.path.exists(os.path.join(self.dldir, "test-file.tar.gz")))
  574. self.assertFalse(os.path.exists(os.path.join(self.dldir, "test-file.tar.gz.done")))
  575. fetcher = bb.fetch.Fetch(["http://invalid.yoctoproject.org/test-file.tar.gz"], self.d)
  576. fetcher.download()
  577. self.assertTrue(os.path.exists(os.path.join(self.dldir, "test-file.tar.gz.done")))
  578. def test_invalid_missing_donestamp(self):
  579. # create an invalid file in the download directory with incorrect hash
  580. string = "this is a test file\n".encode("utf-8")
  581. with open(os.path.join(self.dldir, "test-file.tar.gz"), "wb"):
  582. pass
  583. self.d.setVarFlag("SRC_URI", "md5sum", hashlib.md5(string).hexdigest())
  584. self.d.setVarFlag("SRC_URI", "sha256sum", hashlib.sha256(string).hexdigest())
  585. self.assertTrue(os.path.exists(os.path.join(self.dldir, "test-file.tar.gz")))
  586. self.assertFalse(os.path.exists(os.path.join(self.dldir, "test-file.tar.gz.done")))
  587. fetcher = bb.fetch.Fetch(["http://invalid.yoctoproject.org/test-file.tar.gz"], self.d)
  588. with self.assertRaises(bb.fetch2.NetworkAccess):
  589. fetcher.download()
  590. # the existing file should not exist or should have be moved to "bad-checksum"
  591. self.assertFalse(os.path.exists(os.path.join(self.dldir, "test-file.tar.gz")))
  592. def test_nochecksums_missing(self):
  593. self.assertFalse(os.path.exists(os.path.join(self.dldir, "test-file.tar.gz")))
  594. self.assertFalse(os.path.exists(os.path.join(self.dldir, "test-file.tar.gz.done")))
  595. # ssh fetch does not support checksums
  596. fetcher = bb.fetch.Fetch(["ssh://invalid@invalid.yoctoproject.org/test-file.tar.gz"], self.d)
  597. # attempts to download with missing donestamp
  598. with self.assertRaises(bb.fetch2.NetworkAccess):
  599. fetcher.download()
  600. def test_nochecksums_missing_donestamp(self):
  601. # create a file in the download directory
  602. with open(os.path.join(self.dldir, "test-file.tar.gz"), "wb"):
  603. pass
  604. self.assertTrue(os.path.exists(os.path.join(self.dldir, "test-file.tar.gz")))
  605. self.assertFalse(os.path.exists(os.path.join(self.dldir, "test-file.tar.gz.done")))
  606. # ssh fetch does not support checksums
  607. fetcher = bb.fetch.Fetch(["ssh://invalid@invalid.yoctoproject.org/test-file.tar.gz"], self.d)
  608. # attempts to download with missing donestamp
  609. with self.assertRaises(bb.fetch2.NetworkAccess):
  610. fetcher.download()
  611. def test_nochecksums_has_donestamp(self):
  612. # create a file in the download directory with the donestamp
  613. with open(os.path.join(self.dldir, "test-file.tar.gz"), "wb"):
  614. pass
  615. with open(os.path.join(self.dldir, "test-file.tar.gz.done"), "wb"):
  616. pass
  617. self.assertTrue(os.path.exists(os.path.join(self.dldir, "test-file.tar.gz")))
  618. self.assertTrue(os.path.exists(os.path.join(self.dldir, "test-file.tar.gz.done")))
  619. # ssh fetch does not support checksums
  620. fetcher = bb.fetch.Fetch(["ssh://invalid@invalid.yoctoproject.org/test-file.tar.gz"], self.d)
  621. # should not fetch
  622. fetcher.download()
  623. # both files should still exist
  624. self.assertTrue(os.path.exists(os.path.join(self.dldir, "test-file.tar.gz")))
  625. self.assertTrue(os.path.exists(os.path.join(self.dldir, "test-file.tar.gz.done")))
  626. def test_nochecksums_missing_has_donestamp(self):
  627. # create a file in the download directory with the donestamp
  628. with open(os.path.join(self.dldir, "test-file.tar.gz.done"), "wb"):
  629. pass
  630. self.assertFalse(os.path.exists(os.path.join(self.dldir, "test-file.tar.gz")))
  631. self.assertTrue(os.path.exists(os.path.join(self.dldir, "test-file.tar.gz.done")))
  632. # ssh fetch does not support checksums
  633. fetcher = bb.fetch.Fetch(["ssh://invalid@invalid.yoctoproject.org/test-file.tar.gz"], self.d)
  634. with self.assertRaises(bb.fetch2.NetworkAccess):
  635. fetcher.download()
  636. # both files should still exist
  637. self.assertFalse(os.path.exists(os.path.join(self.dldir, "test-file.tar.gz")))
  638. self.assertFalse(os.path.exists(os.path.join(self.dldir, "test-file.tar.gz.done")))
  639. class FetcherNetworkTest(FetcherTest):
  640. @skipIfNoNetwork()
  641. def test_fetch(self):
  642. 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)
  643. fetcher.download()
  644. self.assertEqual(os.path.getsize(self.dldir + "/bitbake-1.0.tar.gz"), 57749)
  645. self.assertEqual(os.path.getsize(self.dldir + "/bitbake-1.1.tar.gz"), 57892)
  646. self.d.setVar("BB_NO_NETWORK", "1")
  647. 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)
  648. fetcher.download()
  649. fetcher.unpack(self.unpackdir)
  650. self.assertEqual(len(os.listdir(self.unpackdir + "/bitbake-1.0/")), 9)
  651. self.assertEqual(len(os.listdir(self.unpackdir + "/bitbake-1.1/")), 9)
  652. @skipIfNoNetwork()
  653. def test_fetch_mirror(self):
  654. self.d.setVar("MIRRORS", "http://.*/.* http://downloads.yoctoproject.org/releases/bitbake")
  655. fetcher = bb.fetch.Fetch(["http://invalid.yoctoproject.org/releases/bitbake/bitbake-1.0.tar.gz"], self.d)
  656. fetcher.download()
  657. self.assertEqual(os.path.getsize(self.dldir + "/bitbake-1.0.tar.gz"), 57749)
  658. @skipIfNoNetwork()
  659. def test_fetch_mirror_of_mirror(self):
  660. self.d.setVar("MIRRORS", "http://.*/.* http://invalid2.yoctoproject.org/ \n http://invalid2.yoctoproject.org/.* http://downloads.yoctoproject.org/releases/bitbake")
  661. fetcher = bb.fetch.Fetch(["http://invalid.yoctoproject.org/releases/bitbake/bitbake-1.0.tar.gz"], self.d)
  662. fetcher.download()
  663. self.assertEqual(os.path.getsize(self.dldir + "/bitbake-1.0.tar.gz"), 57749)
  664. @skipIfNoNetwork()
  665. def test_fetch_file_mirror_of_mirror(self):
  666. self.d.setVar("MIRRORS", "http://.*/.* file:///some1where/ \n file:///some1where/.* file://some2where/ \n file://some2where/.* http://downloads.yoctoproject.org/releases/bitbake")
  667. fetcher = bb.fetch.Fetch(["http://invalid.yoctoproject.org/releases/bitbake/bitbake-1.0.tar.gz"], self.d)
  668. os.mkdir(self.dldir + "/some2where")
  669. fetcher.download()
  670. self.assertEqual(os.path.getsize(self.dldir + "/bitbake-1.0.tar.gz"), 57749)
  671. @skipIfNoNetwork()
  672. def test_fetch_premirror(self):
  673. self.d.setVar("PREMIRRORS", "http://.*/.* http://downloads.yoctoproject.org/releases/bitbake")
  674. fetcher = bb.fetch.Fetch(["http://invalid.yoctoproject.org/releases/bitbake/bitbake-1.0.tar.gz"], self.d)
  675. fetcher.download()
  676. self.assertEqual(os.path.getsize(self.dldir + "/bitbake-1.0.tar.gz"), 57749)
  677. @skipIfNoNetwork()
  678. def gitfetcher(self, url1, url2):
  679. def checkrevision(self, fetcher):
  680. fetcher.unpack(self.unpackdir)
  681. revision = bb.process.run("git rev-parse HEAD", shell=True, cwd=self.unpackdir + "/git")[0].strip()
  682. self.assertEqual(revision, "270a05b0b4ba0959fe0624d2a4885d7b70426da5")
  683. self.d.setVar("BB_GENERATE_MIRROR_TARBALLS", "1")
  684. self.d.setVar("SRCREV", "270a05b0b4ba0959fe0624d2a4885d7b70426da5")
  685. fetcher = bb.fetch.Fetch([url1], self.d)
  686. fetcher.download()
  687. checkrevision(self, fetcher)
  688. # Wipe out the dldir clone and the unpacked source, turn off the network and check mirror tarball works
  689. bb.utils.prunedir(self.dldir + "/git2/")
  690. bb.utils.prunedir(self.unpackdir)
  691. self.d.setVar("BB_NO_NETWORK", "1")
  692. fetcher = bb.fetch.Fetch([url2], self.d)
  693. fetcher.download()
  694. checkrevision(self, fetcher)
  695. @skipIfNoNetwork()
  696. def test_gitfetch(self):
  697. url1 = url2 = "git://git.openembedded.org/bitbake"
  698. self.gitfetcher(url1, url2)
  699. @skipIfNoNetwork()
  700. def test_gitfetch_goodsrcrev(self):
  701. # SRCREV is set but matches rev= parameter
  702. url1 = url2 = "git://git.openembedded.org/bitbake;rev=270a05b0b4ba0959fe0624d2a4885d7b70426da5"
  703. self.gitfetcher(url1, url2)
  704. @skipIfNoNetwork()
  705. def test_gitfetch_badsrcrev(self):
  706. # SRCREV is set but does not match rev= parameter
  707. url1 = url2 = "git://git.openembedded.org/bitbake;rev=dead05b0b4ba0959fe0624d2a4885d7b70426da5"
  708. self.assertRaises(bb.fetch.FetchError, self.gitfetcher, url1, url2)
  709. @skipIfNoNetwork()
  710. def test_gitfetch_tagandrev(self):
  711. # SRCREV is set but does not match rev= parameter
  712. url1 = url2 = "git://git.openembedded.org/bitbake;rev=270a05b0b4ba0959fe0624d2a4885d7b70426da5;tag=270a05b0b4ba0959fe0624d2a4885d7b70426da5"
  713. self.assertRaises(bb.fetch.FetchError, self.gitfetcher, url1, url2)
  714. @skipIfNoNetwork()
  715. def test_gitfetch_localusehead(self):
  716. # Create dummy local Git repo
  717. src_dir = tempfile.mkdtemp(dir=self.tempdir,
  718. prefix='gitfetch_localusehead_')
  719. src_dir = os.path.abspath(src_dir)
  720. bb.process.run("git init", cwd=src_dir)
  721. bb.process.run("git commit --allow-empty -m'Dummy commit'",
  722. cwd=src_dir)
  723. # Use other branch than master
  724. bb.process.run("git checkout -b my-devel", cwd=src_dir)
  725. bb.process.run("git commit --allow-empty -m'Dummy commit 2'",
  726. cwd=src_dir)
  727. stdout = bb.process.run("git rev-parse HEAD", cwd=src_dir)
  728. orig_rev = stdout[0].strip()
  729. # Fetch and check revision
  730. self.d.setVar("SRCREV", "AUTOINC")
  731. url = "git://" + src_dir + ";protocol=file;usehead=1"
  732. fetcher = bb.fetch.Fetch([url], self.d)
  733. fetcher.download()
  734. fetcher.unpack(self.unpackdir)
  735. stdout = bb.process.run("git rev-parse HEAD",
  736. cwd=os.path.join(self.unpackdir, 'git'))
  737. unpack_rev = stdout[0].strip()
  738. self.assertEqual(orig_rev, unpack_rev)
  739. @skipIfNoNetwork()
  740. def test_gitfetch_remoteusehead(self):
  741. url = "git://git.openembedded.org/bitbake;usehead=1"
  742. self.assertRaises(bb.fetch.ParameterError, self.gitfetcher, url, url)
  743. @skipIfNoNetwork()
  744. def test_gitfetch_finds_local_tarball_for_mirrored_url_when_previous_downloaded_by_the_recipe_url(self):
  745. recipeurl = "git://git.openembedded.org/bitbake"
  746. mirrorurl = "git://someserver.org/bitbake"
  747. self.d.setVar("PREMIRRORS", "git://someserver.org/bitbake git://git.openembedded.org/bitbake \n")
  748. self.gitfetcher(recipeurl, mirrorurl)
  749. @skipIfNoNetwork()
  750. def test_gitfetch_finds_local_tarball_when_previous_downloaded_from_a_premirror(self):
  751. recipeurl = "git://someserver.org/bitbake"
  752. self.d.setVar("PREMIRRORS", "git://someserver.org/bitbake git://git.openembedded.org/bitbake \n")
  753. self.gitfetcher(recipeurl, recipeurl)
  754. @skipIfNoNetwork()
  755. def test_gitfetch_finds_local_repository_when_premirror_rewrites_the_recipe_url(self):
  756. realurl = "git://git.openembedded.org/bitbake"
  757. recipeurl = "git://someserver.org/bitbake"
  758. self.sourcedir = self.unpackdir.replace("unpacked", "sourcemirror.git")
  759. os.chdir(self.tempdir)
  760. bb.process.run("git clone %s %s 2> /dev/null" % (realurl, self.sourcedir), shell=True)
  761. self.d.setVar("PREMIRRORS", "%s git://%s;protocol=file \n" % (recipeurl, self.sourcedir))
  762. self.gitfetcher(recipeurl, recipeurl)
  763. @skipIfNoNetwork()
  764. def test_git_submodule(self):
  765. # URL with ssh submodules
  766. url = "gitsm://git.yoctoproject.org/git-submodule-test;branch=ssh-gitsm-tests;rev=049da4a6cb198d7c0302e9e8b243a1443cb809a7"
  767. # Original URL (comment this if you have ssh access to git.yoctoproject.org)
  768. url = "gitsm://git.yoctoproject.org/git-submodule-test;branch=master;rev=a2885dd7d25380d23627e7544b7bbb55014b16ee"
  769. fetcher = bb.fetch.Fetch([url], self.d)
  770. fetcher.download()
  771. # Previous cwd has been deleted
  772. os.chdir(os.path.dirname(self.unpackdir))
  773. fetcher.unpack(self.unpackdir)
  774. repo_path = os.path.join(self.tempdir, 'unpacked', 'git')
  775. self.assertTrue(os.path.exists(repo_path), msg='Unpacked repository missing')
  776. self.assertTrue(os.path.exists(os.path.join(repo_path, 'bitbake')), msg='bitbake submodule missing')
  777. self.assertFalse(os.path.exists(os.path.join(repo_path, 'na')), msg='uninitialized submodule present')
  778. # Only when we're running the extended test with a submodule's submodule, can we check this.
  779. if os.path.exists(os.path.join(repo_path, 'bitbake-gitsm-test1')):
  780. self.assertTrue(os.path.exists(os.path.join(repo_path, 'bitbake-gitsm-test1', 'bitbake')), msg='submodule of submodule missing')
  781. @skipIfNoNetwork()
  782. def test_git_submodule_dbus_broker(self):
  783. # The following external repositories have show failures in fetch and unpack operations
  784. # We want to avoid regressions!
  785. url = "gitsm://github.com/bus1/dbus-broker;protocol=git;rev=fc874afa0992d0c75ec25acb43d344679f0ee7d2"
  786. fetcher = bb.fetch.Fetch([url], self.d)
  787. fetcher.download()
  788. # Previous cwd has been deleted
  789. os.chdir(os.path.dirname(self.unpackdir))
  790. fetcher.unpack(self.unpackdir)
  791. repo_path = os.path.join(self.tempdir, 'unpacked', 'git')
  792. self.assertTrue(os.path.exists(os.path.join(repo_path, '.git/modules/subprojects/c-dvar/config')), msg='Missing submodule config "subprojects/c-dvar"')
  793. self.assertTrue(os.path.exists(os.path.join(repo_path, '.git/modules/subprojects/c-list/config')), msg='Missing submodule config "subprojects/c-list"')
  794. self.assertTrue(os.path.exists(os.path.join(repo_path, '.git/modules/subprojects/c-rbtree/config')), msg='Missing submodule config "subprojects/c-rbtree"')
  795. self.assertTrue(os.path.exists(os.path.join(repo_path, '.git/modules/subprojects/c-sundry/config')), msg='Missing submodule config "subprojects/c-sundry"')
  796. self.assertTrue(os.path.exists(os.path.join(repo_path, '.git/modules/subprojects/c-utf8/config')), msg='Missing submodule config "subprojects/c-utf8"')
  797. @skipIfNoNetwork()
  798. def test_git_submodule_CLI11(self):
  799. url = "gitsm://github.com/CLIUtils/CLI11;protocol=git;rev=bd4dc911847d0cde7a6b41dfa626a85aab213baf"
  800. fetcher = bb.fetch.Fetch([url], self.d)
  801. fetcher.download()
  802. # Previous cwd has been deleted
  803. os.chdir(os.path.dirname(self.unpackdir))
  804. fetcher.unpack(self.unpackdir)
  805. repo_path = os.path.join(self.tempdir, 'unpacked', 'git')
  806. self.assertTrue(os.path.exists(os.path.join(repo_path, '.git/modules/extern/googletest/config')), msg='Missing submodule config "extern/googletest"')
  807. self.assertTrue(os.path.exists(os.path.join(repo_path, '.git/modules/extern/json/config')), msg='Missing submodule config "extern/json"')
  808. self.assertTrue(os.path.exists(os.path.join(repo_path, '.git/modules/extern/sanitizers/config')), msg='Missing submodule config "extern/sanitizers"')
  809. @skipIfNoNetwork()
  810. def test_git_submodule_update_CLI11(self):
  811. """ Prevent regression on update detection not finding missing submodule, or modules without needed commits """
  812. url = "gitsm://github.com/CLIUtils/CLI11;protocol=git;rev=cf6a99fa69aaefe477cc52e3ef4a7d2d7fa40714"
  813. fetcher = bb.fetch.Fetch([url], self.d)
  814. fetcher.download()
  815. # CLI11 that pulls in a newer nlohmann-json
  816. url = "gitsm://github.com/CLIUtils/CLI11;protocol=git;rev=49ac989a9527ee9bb496de9ded7b4872c2e0e5ca"
  817. fetcher = bb.fetch.Fetch([url], self.d)
  818. fetcher.download()
  819. # Previous cwd has been deleted
  820. os.chdir(os.path.dirname(self.unpackdir))
  821. fetcher.unpack(self.unpackdir)
  822. repo_path = os.path.join(self.tempdir, 'unpacked', 'git')
  823. self.assertTrue(os.path.exists(os.path.join(repo_path, '.git/modules/extern/googletest/config')), msg='Missing submodule config "extern/googletest"')
  824. self.assertTrue(os.path.exists(os.path.join(repo_path, '.git/modules/extern/json/config')), msg='Missing submodule config "extern/json"')
  825. self.assertTrue(os.path.exists(os.path.join(repo_path, '.git/modules/extern/sanitizers/config')), msg='Missing submodule config "extern/sanitizers"')
  826. @skipIfNoNetwork()
  827. def test_git_submodule_aktualizr(self):
  828. url = "gitsm://github.com/advancedtelematic/aktualizr;branch=master;protocol=git;rev=d00d1a04cc2366d1a5f143b84b9f507f8bd32c44"
  829. fetcher = bb.fetch.Fetch([url], self.d)
  830. fetcher.download()
  831. # Previous cwd has been deleted
  832. os.chdir(os.path.dirname(self.unpackdir))
  833. fetcher.unpack(self.unpackdir)
  834. repo_path = os.path.join(self.tempdir, 'unpacked', 'git')
  835. 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"')
  836. 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"')
  837. 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")
  838. 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"')
  839. self.assertTrue(os.path.exists(os.path.join(repo_path, '.git/modules/third_party/googletest/config')), msg='Missing submodule config "third_party/googletest/config"')
  840. 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"')
  841. @skipIfNoNetwork()
  842. def test_git_submodule_iotedge(self):
  843. """ Prevent regression on deeply nested submodules not being checked out properly, even though they were fetched. """
  844. # This repository also has submodules where the module (name), path and url do not align
  845. url = "gitsm://github.com/azure/iotedge.git;protocol=git;rev=d76e0316c6f324345d77c48a83ce836d09392699"
  846. fetcher = bb.fetch.Fetch([url], self.d)
  847. fetcher.download()
  848. # Previous cwd has been deleted
  849. os.chdir(os.path.dirname(self.unpackdir))
  850. fetcher.unpack(self.unpackdir)
  851. repo_path = os.path.join(self.tempdir, 'unpacked', 'git')
  852. 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')
  853. 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')
  854. 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')
  855. 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')
  856. 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')
  857. 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')
  858. 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')
  859. 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')
  860. 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')
  861. 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')
  862. 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')
  863. 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')
  864. 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')
  865. class SVNTest(FetcherTest):
  866. def skipIfNoSvn():
  867. import shutil
  868. if not shutil.which("svn"):
  869. return unittest.skip("svn not installed, tests being skipped")
  870. if not shutil.which("svnadmin"):
  871. return unittest.skip("svnadmin not installed, tests being skipped")
  872. return lambda f: f
  873. @skipIfNoSvn()
  874. def setUp(self):
  875. """ Create a local repository """
  876. super(SVNTest, self).setUp()
  877. # Create something we can fetch
  878. src_dir = tempfile.mkdtemp(dir=self.tempdir,
  879. prefix='svnfetch_srcdir_')
  880. src_dir = os.path.abspath(src_dir)
  881. bb.process.run("echo readme > README.md", cwd=src_dir)
  882. # Store it in a local SVN repository
  883. repo_dir = tempfile.mkdtemp(dir=self.tempdir,
  884. prefix='svnfetch_localrepo_')
  885. repo_dir = os.path.abspath(repo_dir)
  886. bb.process.run("svnadmin create project", cwd=repo_dir)
  887. self.repo_url = "file://%s/project" % repo_dir
  888. bb.process.run("svn import --non-interactive -m 'Initial import' %s %s/trunk" % (src_dir, self.repo_url),
  889. cwd=repo_dir)
  890. bb.process.run("svn co %s svnfetch_co" % self.repo_url, cwd=self.tempdir)
  891. # Github will emulate SVN. Use this to check if we're downloding...
  892. bb.process.run("svn propset svn:externals 'bitbake svn://vcs.pcre.org/pcre2/code' .",
  893. cwd=os.path.join(self.tempdir, 'svnfetch_co', 'trunk'))
  894. bb.process.run("svn commit --non-interactive -m 'Add external'",
  895. cwd=os.path.join(self.tempdir, 'svnfetch_co', 'trunk'))
  896. self.src_dir = src_dir
  897. self.repo_dir = repo_dir
  898. @skipIfNoSvn()
  899. def tearDown(self):
  900. os.chdir(self.origdir)
  901. if os.environ.get("BB_TMPDIR_NOCLEAN") == "yes":
  902. print("Not cleaning up %s. Please remove manually." % self.tempdir)
  903. else:
  904. bb.utils.prunedir(self.tempdir)
  905. @skipIfNoSvn()
  906. @skipIfNoNetwork()
  907. def test_noexternal_svn(self):
  908. # Always match the rev count from setUp (currently rev 2)
  909. url = "svn://%s;module=trunk;protocol=file;rev=2" % self.repo_url.replace('file://', '')
  910. fetcher = bb.fetch.Fetch([url], self.d)
  911. fetcher.download()
  912. os.chdir(os.path.dirname(self.unpackdir))
  913. fetcher.unpack(self.unpackdir)
  914. self.assertTrue(os.path.exists(os.path.join(self.unpackdir, 'trunk')), msg="Missing trunk")
  915. self.assertTrue(os.path.exists(os.path.join(self.unpackdir, 'trunk', 'README.md')), msg="Missing contents")
  916. self.assertFalse(os.path.exists(os.path.join(self.unpackdir, 'trunk/bitbake/trunk')), msg="External dir should NOT exist")
  917. self.assertFalse(os.path.exists(os.path.join(self.unpackdir, 'trunk/bitbake/trunk', 'README')), msg="External README should NOT exit")
  918. @skipIfNoSvn()
  919. def test_external_svn(self):
  920. # Always match the rev count from setUp (currently rev 2)
  921. url = "svn://%s;module=trunk;protocol=file;externals=allowed;rev=2" % self.repo_url.replace('file://', '')
  922. fetcher = bb.fetch.Fetch([url], self.d)
  923. fetcher.download()
  924. os.chdir(os.path.dirname(self.unpackdir))
  925. fetcher.unpack(self.unpackdir)
  926. self.assertTrue(os.path.exists(os.path.join(self.unpackdir, 'trunk')), msg="Missing trunk")
  927. self.assertTrue(os.path.exists(os.path.join(self.unpackdir, 'trunk', 'README.md')), msg="Missing contents")
  928. self.assertTrue(os.path.exists(os.path.join(self.unpackdir, 'trunk/bitbake/trunk')), msg="External dir should exist")
  929. self.assertTrue(os.path.exists(os.path.join(self.unpackdir, 'trunk/bitbake/trunk', 'README')), msg="External README should exit")
  930. class TrustedNetworksTest(FetcherTest):
  931. def test_trusted_network(self):
  932. # Ensure trusted_network returns False when the host IS in the list.
  933. url = "git://Someserver.org/foo;rev=1"
  934. self.d.setVar("BB_ALLOWED_NETWORKS", "server1.org someserver.org server2.org server3.org")
  935. self.assertTrue(bb.fetch.trusted_network(self.d, url))
  936. def test_wild_trusted_network(self):
  937. # Ensure trusted_network returns true when the *.host IS in the list.
  938. url = "git://Someserver.org/foo;rev=1"
  939. self.d.setVar("BB_ALLOWED_NETWORKS", "server1.org *.someserver.org server2.org server3.org")
  940. self.assertTrue(bb.fetch.trusted_network(self.d, url))
  941. def test_prefix_wild_trusted_network(self):
  942. # Ensure trusted_network returns true when the prefix matches *.host.
  943. url = "git://git.Someserver.org/foo;rev=1"
  944. self.d.setVar("BB_ALLOWED_NETWORKS", "server1.org *.someserver.org server2.org server3.org")
  945. self.assertTrue(bb.fetch.trusted_network(self.d, url))
  946. def test_two_prefix_wild_trusted_network(self):
  947. # Ensure trusted_network returns true when the prefix matches *.host.
  948. url = "git://something.git.Someserver.org/foo;rev=1"
  949. self.d.setVar("BB_ALLOWED_NETWORKS", "server1.org *.someserver.org server2.org server3.org")
  950. self.assertTrue(bb.fetch.trusted_network(self.d, url))
  951. def test_port_trusted_network(self):
  952. # Ensure trusted_network returns True, even if the url specifies a port.
  953. url = "git://someserver.org:8080/foo;rev=1"
  954. self.d.setVar("BB_ALLOWED_NETWORKS", "someserver.org")
  955. self.assertTrue(bb.fetch.trusted_network(self.d, url))
  956. def test_untrusted_network(self):
  957. # Ensure trusted_network returns False when the host is NOT in the list.
  958. url = "git://someserver.org/foo;rev=1"
  959. self.d.setVar("BB_ALLOWED_NETWORKS", "server1.org server2.org server3.org")
  960. self.assertFalse(bb.fetch.trusted_network(self.d, url))
  961. def test_wild_untrusted_network(self):
  962. # Ensure trusted_network returns False when the host is NOT in the list.
  963. url = "git://*.someserver.org/foo;rev=1"
  964. self.d.setVar("BB_ALLOWED_NETWORKS", "server1.org server2.org server3.org")
  965. self.assertFalse(bb.fetch.trusted_network(self.d, url))
  966. class URLHandle(unittest.TestCase):
  967. datatable = {
  968. "http://www.google.com/index.html" : ('http', 'www.google.com', '/index.html', '', '', {}),
  969. "cvs://anoncvs@cvs.handhelds.org/cvs;module=familiar/dist/ipkg" : ('cvs', 'cvs.handhelds.org', '/cvs', 'anoncvs', '', {'module': 'familiar/dist/ipkg'}),
  970. "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')])),
  971. "git://git.openembedded.org/bitbake;branch=@foo" : ('git', 'git.openembedded.org', '/bitbake', '', '', {'branch': '@foo'}),
  972. "file://somelocation;someparam=1": ('file', '', 'somelocation', '', '', {'someparam': '1'}),
  973. }
  974. # we require a pathname to encodeurl but users can still pass such urls to
  975. # decodeurl and we need to handle them
  976. decodedata = datatable.copy()
  977. decodedata.update({
  978. "http://somesite.net;someparam=1": ('http', 'somesite.net', '/', '', '', {'someparam': '1'}),
  979. })
  980. def test_decodeurl(self):
  981. for k, v in self.decodedata.items():
  982. result = bb.fetch.decodeurl(k)
  983. self.assertEqual(result, v)
  984. def test_encodeurl(self):
  985. for k, v in self.datatable.items():
  986. result = bb.fetch.encodeurl(v)
  987. self.assertEqual(result, k)
  988. class FetchLatestVersionTest(FetcherTest):
  989. test_git_uris = {
  990. # version pattern "X.Y.Z"
  991. ("mx-1.0", "git://github.com/clutter-project/mx.git;branch=mx-1.4", "9b1db6b8060bd00b121a692f942404a24ae2960f", "")
  992. : "1.99.4",
  993. # version pattern "vX.Y"
  994. # mirror of git.infradead.org since network issues interfered with testing
  995. ("mtd-utils", "git://git.yoctoproject.org/mtd-utils.git", "ca39eb1d98e736109c64ff9c1aa2a6ecca222d8f", "")
  996. : "1.5.0",
  997. # version pattern "pkg_name-X.Y"
  998. # mirror of git://anongit.freedesktop.org/git/xorg/proto/presentproto since network issues interfered with testing
  999. ("presentproto", "git://git.yoctoproject.org/bbfetchtests-presentproto", "24f3a56e541b0a9e6c6ee76081f441221a120ef9", "")
  1000. : "1.0",
  1001. # version pattern "pkg_name-vX.Y.Z"
  1002. ("dtc", "git://git.qemu.org/dtc.git", "65cc4d2748a2c2e6f27f1cf39e07a5dbabd80ebf", "")
  1003. : "1.4.0",
  1004. # combination version pattern
  1005. ("sysprof", "git://gitlab.gnome.org/GNOME/sysprof.git;protocol=https", "cd44ee6644c3641507fb53b8a2a69137f2971219", "")
  1006. : "1.2.0",
  1007. ("u-boot-mkimage", "git://git.denx.de/u-boot.git;branch=master;protocol=git", "62c175fbb8a0f9a926c88294ea9f7e88eb898f6c", "")
  1008. : "2014.01",
  1009. # version pattern "yyyymmdd"
  1010. ("mobile-broadband-provider-info", "git://gitlab.gnome.org/GNOME/mobile-broadband-provider-info.git;protocol=https", "4ed19e11c2975105b71b956440acdb25d46a347d", "")
  1011. : "20120614",
  1012. # packages with a valid UPSTREAM_CHECK_GITTAGREGEX
  1013. # mirror of git://anongit.freedesktop.org/xorg/driver/xf86-video-omap since network issues interfered with testing
  1014. ("xf86-video-omap", "git://git.yoctoproject.org/bbfetchtests-xf86-video-omap", "ae0394e687f1a77e966cf72f895da91840dffb8f", "(?P<pver>(\d+\.(\d\.?)*))")
  1015. : "0.4.3",
  1016. ("build-appliance-image", "git://git.yoctoproject.org/poky", "b37dd451a52622d5b570183a81583cc34c2ff555", "(?P<pver>(([0-9][\.|_]?)+[0-9]))")
  1017. : "11.0.0",
  1018. ("chkconfig-alternatives-native", "git://github.com/kergoth/chkconfig;branch=sysroot", "cd437ecbd8986c894442f8fce1e0061e20f04dee", "chkconfig\-(?P<pver>((\d+[\.\-_]*)+))")
  1019. : "1.3.59",
  1020. ("remake", "git://github.com/rocky/remake.git", "f05508e521987c8494c92d9c2871aec46307d51d", "(?P<pver>(\d+\.(\d+\.)*\d*(\+dbg\d+(\.\d+)*)*))")
  1021. : "3.82+dbg0.9",
  1022. }
  1023. test_wget_uris = {
  1024. #
  1025. # packages with versions inside directory name
  1026. #
  1027. # http://kernel.org/pub/linux/utils/util-linux/v2.23/util-linux-2.24.2.tar.bz2
  1028. ("util-linux", "/pub/linux/utils/util-linux/v2.23/util-linux-2.24.2.tar.bz2", "", "")
  1029. : "2.24.2",
  1030. # http://www.abisource.com/downloads/enchant/1.6.0/enchant-1.6.0.tar.gz
  1031. ("enchant", "/downloads/enchant/1.6.0/enchant-1.6.0.tar.gz", "", "")
  1032. : "1.6.0",
  1033. # http://www.cmake.org/files/v2.8/cmake-2.8.12.1.tar.gz
  1034. ("cmake", "/files/v2.8/cmake-2.8.12.1.tar.gz", "", "")
  1035. : "2.8.12.1",
  1036. #
  1037. # packages with versions only in current directory
  1038. #
  1039. # http://downloads.yoctoproject.org/releases/eglibc/eglibc-2.18-svnr23787.tar.bz2
  1040. ("eglic", "/releases/eglibc/eglibc-2.18-svnr23787.tar.bz2", "", "")
  1041. : "2.19",
  1042. # http://downloads.yoctoproject.org/releases/gnu-config/gnu-config-20120814.tar.bz2
  1043. ("gnu-config", "/releases/gnu-config/gnu-config-20120814.tar.bz2", "", "")
  1044. : "20120814",
  1045. #
  1046. # packages with "99" in the name of possible version
  1047. #
  1048. # http://freedesktop.org/software/pulseaudio/releases/pulseaudio-4.0.tar.xz
  1049. ("pulseaudio", "/software/pulseaudio/releases/pulseaudio-4.0.tar.xz", "", "")
  1050. : "5.0",
  1051. # http://xorg.freedesktop.org/releases/individual/xserver/xorg-server-1.15.1.tar.bz2
  1052. ("xserver-xorg", "/releases/individual/xserver/xorg-server-1.15.1.tar.bz2", "", "")
  1053. : "1.15.1",
  1054. #
  1055. # packages with valid UPSTREAM_CHECK_URI and UPSTREAM_CHECK_REGEX
  1056. #
  1057. # http://www.cups.org/software/1.7.2/cups-1.7.2-source.tar.bz2
  1058. # https://github.com/apple/cups/releases
  1059. ("cups", "/software/1.7.2/cups-1.7.2-source.tar.bz2", "/apple/cups/releases", "(?P<name>cups\-)(?P<pver>((\d+[\.\-_]*)+))\-source\.tar\.gz")
  1060. : "2.0.0",
  1061. # http://download.oracle.com/berkeley-db/db-5.3.21.tar.gz
  1062. # http://ftp.debian.org/debian/pool/main/d/db5.3/
  1063. ("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")
  1064. : "5.3.10",
  1065. }
  1066. @skipIfNoNetwork()
  1067. def test_git_latest_versionstring(self):
  1068. for k, v in self.test_git_uris.items():
  1069. self.d.setVar("PN", k[0])
  1070. self.d.setVar("SRCREV", k[2])
  1071. self.d.setVar("UPSTREAM_CHECK_GITTAGREGEX", k[3])
  1072. ud = bb.fetch2.FetchData(k[1], self.d)
  1073. pupver= ud.method.latest_versionstring(ud, self.d)
  1074. verstring = pupver[0]
  1075. self.assertTrue(verstring, msg="Could not find upstream version for %s" % k[0])
  1076. r = bb.utils.vercmp_string(v, verstring)
  1077. self.assertTrue(r == -1 or r == 0, msg="Package %s, version: %s <= %s" % (k[0], v, verstring))
  1078. def test_wget_latest_versionstring(self):
  1079. testdata = os.path.dirname(os.path.abspath(__file__)) + "/fetch-testdata"
  1080. server = HTTPService(testdata)
  1081. server.start()
  1082. port = server.port
  1083. try:
  1084. for k, v in self.test_wget_uris.items():
  1085. self.d.setVar("PN", k[0])
  1086. checkuri = ""
  1087. if k[2]:
  1088. checkuri = "http://localhost:%s/" % port + k[2]
  1089. self.d.setVar("UPSTREAM_CHECK_URI", checkuri)
  1090. self.d.setVar("UPSTREAM_CHECK_REGEX", k[3])
  1091. url = "http://localhost:%s/" % port + k[1]
  1092. ud = bb.fetch2.FetchData(url, self.d)
  1093. pupver = ud.method.latest_versionstring(ud, self.d)
  1094. verstring = pupver[0]
  1095. self.assertTrue(verstring, msg="Could not find upstream version for %s" % k[0])
  1096. r = bb.utils.vercmp_string(v, verstring)
  1097. self.assertTrue(r == -1 or r == 0, msg="Package %s, version: %s <= %s" % (k[0], v, verstring))
  1098. finally:
  1099. server.stop()
  1100. class FetchCheckStatusTest(FetcherTest):
  1101. test_wget_uris = ["http://downloads.yoctoproject.org/releases/sato/sato-engine-0.1.tar.gz",
  1102. "http://downloads.yoctoproject.org/releases/sato/sato-engine-0.2.tar.gz",
  1103. "http://downloads.yoctoproject.org/releases/sato/sato-engine-0.3.tar.gz",
  1104. "https://yoctoproject.org/",
  1105. "https://yoctoproject.org/documentation",
  1106. "http://downloads.yoctoproject.org/releases/opkg/opkg-0.1.7.tar.gz",
  1107. "http://downloads.yoctoproject.org/releases/opkg/opkg-0.3.0.tar.gz",
  1108. "ftp://sourceware.org/pub/libffi/libffi-1.20.tar.gz",
  1109. "http://ftp.gnu.org/gnu/autoconf/autoconf-2.60.tar.gz",
  1110. "https://ftp.gnu.org/gnu/chess/gnuchess-5.08.tar.gz",
  1111. "https://ftp.gnu.org/gnu/gmp/gmp-4.0.tar.gz",
  1112. # GitHub releases are hosted on Amazon S3, which doesn't support HEAD
  1113. "https://github.com/kergoth/tslib/releases/download/1.1/tslib-1.1.tar.xz"
  1114. ]
  1115. @skipIfNoNetwork()
  1116. def test_wget_checkstatus(self):
  1117. fetch = bb.fetch2.Fetch(self.test_wget_uris, self.d)
  1118. for u in self.test_wget_uris:
  1119. with self.subTest(url=u):
  1120. ud = fetch.ud[u]
  1121. m = ud.method
  1122. ret = m.checkstatus(fetch, ud, self.d)
  1123. self.assertTrue(ret, msg="URI %s, can't check status" % (u))
  1124. @skipIfNoNetwork()
  1125. def test_wget_checkstatus_connection_cache(self):
  1126. from bb.fetch2 import FetchConnectionCache
  1127. connection_cache = FetchConnectionCache()
  1128. fetch = bb.fetch2.Fetch(self.test_wget_uris, self.d,
  1129. connection_cache = connection_cache)
  1130. for u in self.test_wget_uris:
  1131. with self.subTest(url=u):
  1132. ud = fetch.ud[u]
  1133. m = ud.method
  1134. ret = m.checkstatus(fetch, ud, self.d)
  1135. self.assertTrue(ret, msg="URI %s, can't check status" % (u))
  1136. connection_cache.close_connections()
  1137. class GitMakeShallowTest(FetcherTest):
  1138. def setUp(self):
  1139. FetcherTest.setUp(self)
  1140. self.gitdir = os.path.join(self.tempdir, 'gitshallow')
  1141. bb.utils.mkdirhier(self.gitdir)
  1142. bb.process.run('git init', cwd=self.gitdir)
  1143. def assertRefs(self, expected_refs):
  1144. actual_refs = self.git(['for-each-ref', '--format=%(refname)']).splitlines()
  1145. full_expected = self.git(['rev-parse', '--symbolic-full-name'] + expected_refs).splitlines()
  1146. self.assertEqual(sorted(full_expected), sorted(actual_refs))
  1147. def assertRevCount(self, expected_count, args=None):
  1148. if args is None:
  1149. args = ['HEAD']
  1150. revs = self.git(['rev-list'] + args)
  1151. actual_count = len(revs.splitlines())
  1152. self.assertEqual(expected_count, actual_count, msg='Object count `%d` is not the expected `%d`' % (actual_count, expected_count))
  1153. def git(self, cmd):
  1154. if isinstance(cmd, str):
  1155. cmd = 'git ' + cmd
  1156. else:
  1157. cmd = ['git'] + cmd
  1158. return bb.process.run(cmd, cwd=self.gitdir)[0]
  1159. def make_shallow(self, args=None):
  1160. if args is None:
  1161. args = ['HEAD']
  1162. return bb.process.run([bb.fetch2.git.Git.make_shallow_path] + args, cwd=self.gitdir)
  1163. def add_empty_file(self, path, msg=None):
  1164. if msg is None:
  1165. msg = path
  1166. open(os.path.join(self.gitdir, path), 'w').close()
  1167. self.git(['add', path])
  1168. self.git(['commit', '-m', msg, path])
  1169. def test_make_shallow_single_branch_no_merge(self):
  1170. self.add_empty_file('a')
  1171. self.add_empty_file('b')
  1172. self.assertRevCount(2)
  1173. self.make_shallow()
  1174. self.assertRevCount(1)
  1175. def test_make_shallow_single_branch_one_merge(self):
  1176. self.add_empty_file('a')
  1177. self.add_empty_file('b')
  1178. self.git('checkout -b a_branch')
  1179. self.add_empty_file('c')
  1180. self.git('checkout master')
  1181. self.add_empty_file('d')
  1182. self.git('merge --no-ff --no-edit a_branch')
  1183. self.git('branch -d a_branch')
  1184. self.add_empty_file('e')
  1185. self.assertRevCount(6)
  1186. self.make_shallow(['HEAD~2'])
  1187. self.assertRevCount(5)
  1188. def test_make_shallow_at_merge(self):
  1189. self.add_empty_file('a')
  1190. self.git('checkout -b a_branch')
  1191. self.add_empty_file('b')
  1192. self.git('checkout master')
  1193. self.git('merge --no-ff --no-edit a_branch')
  1194. self.git('branch -d a_branch')
  1195. self.assertRevCount(3)
  1196. self.make_shallow()
  1197. self.assertRevCount(1)
  1198. def test_make_shallow_annotated_tag(self):
  1199. self.add_empty_file('a')
  1200. self.add_empty_file('b')
  1201. self.git('tag -a -m a_tag a_tag')
  1202. self.assertRevCount(2)
  1203. self.make_shallow(['a_tag'])
  1204. self.assertRevCount(1)
  1205. def test_make_shallow_multi_ref(self):
  1206. self.add_empty_file('a')
  1207. self.add_empty_file('b')
  1208. self.git('checkout -b a_branch')
  1209. self.add_empty_file('c')
  1210. self.git('checkout master')
  1211. self.add_empty_file('d')
  1212. self.git('checkout -b a_branch_2')
  1213. self.add_empty_file('a_tag')
  1214. self.git('tag a_tag')
  1215. self.git('checkout master')
  1216. self.git('branch -D a_branch_2')
  1217. self.add_empty_file('e')
  1218. self.assertRevCount(6, ['--all'])
  1219. self.make_shallow()
  1220. self.assertRevCount(5, ['--all'])
  1221. def test_make_shallow_multi_ref_trim(self):
  1222. self.add_empty_file('a')
  1223. self.git('checkout -b a_branch')
  1224. self.add_empty_file('c')
  1225. self.git('checkout master')
  1226. self.assertRevCount(1)
  1227. self.assertRevCount(2, ['--all'])
  1228. self.assertRefs(['master', 'a_branch'])
  1229. self.make_shallow(['-r', 'master', 'HEAD'])
  1230. self.assertRevCount(1, ['--all'])
  1231. self.assertRefs(['master'])
  1232. def test_make_shallow_noop(self):
  1233. self.add_empty_file('a')
  1234. self.assertRevCount(1)
  1235. self.make_shallow()
  1236. self.assertRevCount(1)
  1237. @skipIfNoNetwork()
  1238. def test_make_shallow_bitbake(self):
  1239. self.git('remote add origin https://github.com/openembedded/bitbake')
  1240. self.git('fetch --tags origin')
  1241. orig_revs = len(self.git('rev-list --all').splitlines())
  1242. self.make_shallow(['refs/tags/1.10.0'])
  1243. self.assertRevCount(orig_revs - 1746, ['--all'])
  1244. class GitShallowTest(FetcherTest):
  1245. def setUp(self):
  1246. FetcherTest.setUp(self)
  1247. self.gitdir = os.path.join(self.tempdir, 'git')
  1248. self.srcdir = os.path.join(self.tempdir, 'gitsource')
  1249. bb.utils.mkdirhier(self.srcdir)
  1250. self.git('init', cwd=self.srcdir)
  1251. self.d.setVar('WORKDIR', self.tempdir)
  1252. self.d.setVar('S', self.gitdir)
  1253. self.d.delVar('PREMIRRORS')
  1254. self.d.delVar('MIRRORS')
  1255. uri = 'git://%s;protocol=file;subdir=${S}' % self.srcdir
  1256. self.d.setVar('SRC_URI', uri)
  1257. self.d.setVar('SRCREV', '${AUTOREV}')
  1258. self.d.setVar('AUTOREV', '${@bb.fetch2.get_autorev(d)}')
  1259. self.d.setVar('BB_GIT_SHALLOW', '1')
  1260. self.d.setVar('BB_GENERATE_MIRROR_TARBALLS', '0')
  1261. self.d.setVar('BB_GENERATE_SHALLOW_TARBALLS', '1')
  1262. def assertRefs(self, expected_refs, cwd=None):
  1263. if cwd is None:
  1264. cwd = self.gitdir
  1265. actual_refs = self.git(['for-each-ref', '--format=%(refname)'], cwd=cwd).splitlines()
  1266. full_expected = self.git(['rev-parse', '--symbolic-full-name'] + expected_refs, cwd=cwd).splitlines()
  1267. self.assertEqual(sorted(set(full_expected)), sorted(set(actual_refs)))
  1268. def assertRevCount(self, expected_count, args=None, cwd=None):
  1269. if args is None:
  1270. args = ['HEAD']
  1271. if cwd is None:
  1272. cwd = self.gitdir
  1273. revs = self.git(['rev-list'] + args, cwd=cwd)
  1274. actual_count = len(revs.splitlines())
  1275. self.assertEqual(expected_count, actual_count, msg='Object count `%d` is not the expected `%d`' % (actual_count, expected_count))
  1276. def git(self, cmd, cwd=None):
  1277. if isinstance(cmd, str):
  1278. cmd = 'git ' + cmd
  1279. else:
  1280. cmd = ['git'] + cmd
  1281. if cwd is None:
  1282. cwd = self.gitdir
  1283. return bb.process.run(cmd, cwd=cwd)[0]
  1284. def add_empty_file(self, path, cwd=None, msg=None):
  1285. if msg is None:
  1286. msg = path
  1287. if cwd is None:
  1288. cwd = self.srcdir
  1289. open(os.path.join(cwd, path), 'w').close()
  1290. self.git(['add', path], cwd)
  1291. self.git(['commit', '-m', msg, path], cwd)
  1292. def fetch(self, uri=None):
  1293. if uri is None:
  1294. uris = self.d.getVar('SRC_URI').split()
  1295. uri = uris[0]
  1296. d = self.d
  1297. else:
  1298. d = self.d.createCopy()
  1299. d.setVar('SRC_URI', uri)
  1300. uri = d.expand(uri)
  1301. uris = [uri]
  1302. fetcher = bb.fetch2.Fetch(uris, d)
  1303. fetcher.download()
  1304. ud = fetcher.ud[uri]
  1305. return fetcher, ud
  1306. def fetch_and_unpack(self, uri=None):
  1307. fetcher, ud = self.fetch(uri)
  1308. fetcher.unpack(self.d.getVar('WORKDIR'))
  1309. assert os.path.exists(self.d.getVar('S'))
  1310. return fetcher, ud
  1311. def fetch_shallow(self, uri=None, disabled=False, keepclone=False):
  1312. """Fetch a uri, generating a shallow tarball, then unpack using it"""
  1313. fetcher, ud = self.fetch_and_unpack(uri)
  1314. assert os.path.exists(ud.clonedir), 'Git clone in DLDIR (%s) does not exist for uri %s' % (ud.clonedir, uri)
  1315. # Confirm that the unpacked repo is unshallow
  1316. if not disabled:
  1317. assert os.path.exists(os.path.join(self.dldir, ud.mirrortarballs[0]))
  1318. # fetch and unpack, from the shallow tarball
  1319. bb.utils.remove(self.gitdir, recurse=True)
  1320. bb.utils.remove(ud.clonedir, recurse=True)
  1321. bb.utils.remove(ud.clonedir.replace('gitsource', 'gitsubmodule'), recurse=True)
  1322. # confirm that the unpacked repo is used when no git clone or git
  1323. # mirror tarball is available
  1324. fetcher, ud = self.fetch_and_unpack(uri)
  1325. if not disabled:
  1326. assert os.path.exists(os.path.join(self.gitdir, '.git', 'shallow')), 'Unpacked git repository at %s is not shallow' % self.gitdir
  1327. else:
  1328. assert not os.path.exists(os.path.join(self.gitdir, '.git', 'shallow')), 'Unpacked git repository at %s is shallow' % self.gitdir
  1329. return fetcher, ud
  1330. def test_shallow_disabled(self):
  1331. self.add_empty_file('a')
  1332. self.add_empty_file('b')
  1333. self.assertRevCount(2, cwd=self.srcdir)
  1334. self.d.setVar('BB_GIT_SHALLOW', '0')
  1335. self.fetch_shallow(disabled=True)
  1336. self.assertRevCount(2)
  1337. def test_shallow_nobranch(self):
  1338. self.add_empty_file('a')
  1339. self.add_empty_file('b')
  1340. self.assertRevCount(2, cwd=self.srcdir)
  1341. srcrev = self.git('rev-parse HEAD', cwd=self.srcdir).strip()
  1342. self.d.setVar('SRCREV', srcrev)
  1343. uri = self.d.getVar('SRC_URI').split()[0]
  1344. uri = '%s;nobranch=1;bare=1' % uri
  1345. self.fetch_shallow(uri)
  1346. self.assertRevCount(1)
  1347. # shallow refs are used to ensure the srcrev sticks around when we
  1348. # have no other branches referencing it
  1349. self.assertRefs(['refs/shallow/default'])
  1350. def test_shallow_default_depth_1(self):
  1351. # Create initial git repo
  1352. self.add_empty_file('a')
  1353. self.add_empty_file('b')
  1354. self.assertRevCount(2, cwd=self.srcdir)
  1355. self.fetch_shallow()
  1356. self.assertRevCount(1)
  1357. def test_shallow_depth_0_disables(self):
  1358. self.add_empty_file('a')
  1359. self.add_empty_file('b')
  1360. self.assertRevCount(2, cwd=self.srcdir)
  1361. self.d.setVar('BB_GIT_SHALLOW_DEPTH', '0')
  1362. self.fetch_shallow(disabled=True)
  1363. self.assertRevCount(2)
  1364. def test_shallow_depth_default_override(self):
  1365. self.add_empty_file('a')
  1366. self.add_empty_file('b')
  1367. self.assertRevCount(2, cwd=self.srcdir)
  1368. self.d.setVar('BB_GIT_SHALLOW_DEPTH', '2')
  1369. self.d.setVar('BB_GIT_SHALLOW_DEPTH_default', '1')
  1370. self.fetch_shallow()
  1371. self.assertRevCount(1)
  1372. def test_shallow_depth_default_override_disable(self):
  1373. self.add_empty_file('a')
  1374. self.add_empty_file('b')
  1375. self.add_empty_file('c')
  1376. self.assertRevCount(3, cwd=self.srcdir)
  1377. self.d.setVar('BB_GIT_SHALLOW_DEPTH', '0')
  1378. self.d.setVar('BB_GIT_SHALLOW_DEPTH_default', '2')
  1379. self.fetch_shallow()
  1380. self.assertRevCount(2)
  1381. def test_current_shallow_out_of_date_clone(self):
  1382. # Create initial git repo
  1383. self.add_empty_file('a')
  1384. self.add_empty_file('b')
  1385. self.add_empty_file('c')
  1386. self.assertRevCount(3, cwd=self.srcdir)
  1387. # Clone and generate mirror tarball
  1388. fetcher, ud = self.fetch()
  1389. # Ensure we have a current mirror tarball, but an out of date clone
  1390. self.git('update-ref refs/heads/master refs/heads/master~1', cwd=ud.clonedir)
  1391. self.assertRevCount(2, cwd=ud.clonedir)
  1392. # Fetch and unpack, from the current tarball, not the out of date clone
  1393. bb.utils.remove(self.gitdir, recurse=True)
  1394. fetcher, ud = self.fetch()
  1395. fetcher.unpack(self.d.getVar('WORKDIR'))
  1396. self.assertRevCount(1)
  1397. def test_shallow_single_branch_no_merge(self):
  1398. self.add_empty_file('a')
  1399. self.add_empty_file('b')
  1400. self.assertRevCount(2, cwd=self.srcdir)
  1401. self.fetch_shallow()
  1402. self.assertRevCount(1)
  1403. assert os.path.exists(os.path.join(self.gitdir, 'a'))
  1404. assert os.path.exists(os.path.join(self.gitdir, 'b'))
  1405. def test_shallow_no_dangling(self):
  1406. self.add_empty_file('a')
  1407. self.add_empty_file('b')
  1408. self.assertRevCount(2, cwd=self.srcdir)
  1409. self.fetch_shallow()
  1410. self.assertRevCount(1)
  1411. assert not self.git('fsck --dangling')
  1412. def test_shallow_srcrev_branch_truncation(self):
  1413. self.add_empty_file('a')
  1414. self.add_empty_file('b')
  1415. b_commit = self.git('rev-parse HEAD', cwd=self.srcdir).rstrip()
  1416. self.add_empty_file('c')
  1417. self.assertRevCount(3, cwd=self.srcdir)
  1418. self.d.setVar('SRCREV', b_commit)
  1419. self.fetch_shallow()
  1420. # The 'c' commit was removed entirely, and 'a' was removed from history
  1421. self.assertRevCount(1, ['--all'])
  1422. self.assertEqual(self.git('rev-parse HEAD').strip(), b_commit)
  1423. assert os.path.exists(os.path.join(self.gitdir, 'a'))
  1424. assert os.path.exists(os.path.join(self.gitdir, 'b'))
  1425. assert not os.path.exists(os.path.join(self.gitdir, 'c'))
  1426. def test_shallow_ref_pruning(self):
  1427. self.add_empty_file('a')
  1428. self.add_empty_file('b')
  1429. self.git('branch a_branch', cwd=self.srcdir)
  1430. self.assertRefs(['master', 'a_branch'], cwd=self.srcdir)
  1431. self.assertRevCount(2, cwd=self.srcdir)
  1432. self.fetch_shallow()
  1433. self.assertRefs(['master', 'origin/master'])
  1434. self.assertRevCount(1)
  1435. def test_shallow_submodules(self):
  1436. self.add_empty_file('a')
  1437. self.add_empty_file('b')
  1438. smdir = os.path.join(self.tempdir, 'gitsubmodule')
  1439. bb.utils.mkdirhier(smdir)
  1440. self.git('init', cwd=smdir)
  1441. # Make this look like it was cloned from a remote...
  1442. self.git('config --add remote.origin.url "%s"' % smdir, cwd=smdir)
  1443. self.git('config --add remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"', cwd=smdir)
  1444. self.add_empty_file('asub', cwd=smdir)
  1445. self.add_empty_file('bsub', cwd=smdir)
  1446. self.git('submodule init', cwd=self.srcdir)
  1447. self.git('submodule add file://%s' % smdir, cwd=self.srcdir)
  1448. self.git('submodule update', cwd=self.srcdir)
  1449. self.git('commit -m submodule -a', cwd=self.srcdir)
  1450. uri = 'gitsm://%s;protocol=file;subdir=${S}' % self.srcdir
  1451. fetcher, ud = self.fetch_shallow(uri)
  1452. # Verify the main repository is shallow
  1453. self.assertRevCount(1)
  1454. # Verify the gitsubmodule directory is present
  1455. assert os.listdir(os.path.join(self.gitdir, 'gitsubmodule'))
  1456. # Verify the submodule is also shallow
  1457. self.assertRevCount(1, cwd=os.path.join(self.gitdir, 'gitsubmodule'))
  1458. def test_shallow_submodule_mirrors(self):
  1459. self.add_empty_file('a')
  1460. self.add_empty_file('b')
  1461. smdir = os.path.join(self.tempdir, 'gitsubmodule')
  1462. bb.utils.mkdirhier(smdir)
  1463. self.git('init', cwd=smdir)
  1464. # Make this look like it was cloned from a remote...
  1465. self.git('config --add remote.origin.url "%s"' % smdir, cwd=smdir)
  1466. self.git('config --add remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"', cwd=smdir)
  1467. self.add_empty_file('asub', cwd=smdir)
  1468. self.add_empty_file('bsub', cwd=smdir)
  1469. self.git('submodule init', cwd=self.srcdir)
  1470. self.git('submodule add file://%s' % smdir, cwd=self.srcdir)
  1471. self.git('submodule update', cwd=self.srcdir)
  1472. self.git('commit -m submodule -a', cwd=self.srcdir)
  1473. uri = 'gitsm://%s;protocol=file;subdir=${S}' % self.srcdir
  1474. # Fetch once to generate the shallow tarball
  1475. fetcher, ud = self.fetch(uri)
  1476. # Set up the mirror
  1477. mirrordir = os.path.join(self.tempdir, 'mirror')
  1478. os.rename(self.dldir, mirrordir)
  1479. self.d.setVar('PREMIRRORS', 'gitsm://.*/.* file://%s/\n' % mirrordir)
  1480. # Fetch from the mirror
  1481. bb.utils.remove(self.dldir, recurse=True)
  1482. bb.utils.remove(self.gitdir, recurse=True)
  1483. self.fetch_and_unpack(uri)
  1484. # Verify the main repository is shallow
  1485. self.assertRevCount(1)
  1486. # Verify the gitsubmodule directory is present
  1487. assert os.listdir(os.path.join(self.gitdir, 'gitsubmodule'))
  1488. # Verify the submodule is also shallow
  1489. self.assertRevCount(1, cwd=os.path.join(self.gitdir, 'gitsubmodule'))
  1490. if any(os.path.exists(os.path.join(p, 'git-annex')) for p in os.environ.get('PATH').split(':')):
  1491. def test_shallow_annex(self):
  1492. self.add_empty_file('a')
  1493. self.add_empty_file('b')
  1494. self.git('annex init', cwd=self.srcdir)
  1495. open(os.path.join(self.srcdir, 'c'), 'w').close()
  1496. self.git('annex add c', cwd=self.srcdir)
  1497. self.git('commit -m annex-c -a', cwd=self.srcdir)
  1498. bb.process.run('chmod u+w -R %s' % os.path.join(self.srcdir, '.git', 'annex'))
  1499. uri = 'gitannex://%s;protocol=file;subdir=${S}' % self.srcdir
  1500. fetcher, ud = self.fetch_shallow(uri)
  1501. self.assertRevCount(1)
  1502. assert './.git/annex/' in bb.process.run('tar -tzf %s' % os.path.join(self.dldir, ud.mirrortarballs[0]))[0]
  1503. assert os.path.exists(os.path.join(self.gitdir, 'c'))
  1504. def test_shallow_multi_one_uri(self):
  1505. # Create initial git repo
  1506. self.add_empty_file('a')
  1507. self.add_empty_file('b')
  1508. self.git('checkout -b a_branch', cwd=self.srcdir)
  1509. self.add_empty_file('c')
  1510. self.add_empty_file('d')
  1511. self.git('checkout master', cwd=self.srcdir)
  1512. self.git('tag v0.0 a_branch', cwd=self.srcdir)
  1513. self.add_empty_file('e')
  1514. self.git('merge --no-ff --no-edit a_branch', cwd=self.srcdir)
  1515. self.add_empty_file('f')
  1516. self.assertRevCount(7, cwd=self.srcdir)
  1517. uri = self.d.getVar('SRC_URI').split()[0]
  1518. uri = '%s;branch=master,a_branch;name=master,a_branch' % uri
  1519. self.d.setVar('BB_GIT_SHALLOW_DEPTH', '0')
  1520. self.d.setVar('BB_GIT_SHALLOW_REVS', 'v0.0')
  1521. self.d.setVar('SRCREV_master', '${AUTOREV}')
  1522. self.d.setVar('SRCREV_a_branch', '${AUTOREV}')
  1523. self.fetch_shallow(uri)
  1524. self.assertRevCount(5)
  1525. self.assertRefs(['master', 'origin/master', 'origin/a_branch'])
  1526. def test_shallow_multi_one_uri_depths(self):
  1527. # Create initial git repo
  1528. self.add_empty_file('a')
  1529. self.add_empty_file('b')
  1530. self.git('checkout -b a_branch', cwd=self.srcdir)
  1531. self.add_empty_file('c')
  1532. self.add_empty_file('d')
  1533. self.git('checkout master', cwd=self.srcdir)
  1534. self.add_empty_file('e')
  1535. self.git('merge --no-ff --no-edit a_branch', cwd=self.srcdir)
  1536. self.add_empty_file('f')
  1537. self.assertRevCount(7, cwd=self.srcdir)
  1538. uri = self.d.getVar('SRC_URI').split()[0]
  1539. uri = '%s;branch=master,a_branch;name=master,a_branch' % uri
  1540. self.d.setVar('BB_GIT_SHALLOW_DEPTH', '0')
  1541. self.d.setVar('BB_GIT_SHALLOW_DEPTH_master', '3')
  1542. self.d.setVar('BB_GIT_SHALLOW_DEPTH_a_branch', '1')
  1543. self.d.setVar('SRCREV_master', '${AUTOREV}')
  1544. self.d.setVar('SRCREV_a_branch', '${AUTOREV}')
  1545. self.fetch_shallow(uri)
  1546. self.assertRevCount(4, ['--all'])
  1547. self.assertRefs(['master', 'origin/master', 'origin/a_branch'])
  1548. def test_shallow_clone_preferred_over_shallow(self):
  1549. self.add_empty_file('a')
  1550. self.add_empty_file('b')
  1551. # Fetch once to generate the shallow tarball
  1552. fetcher, ud = self.fetch()
  1553. assert os.path.exists(os.path.join(self.dldir, ud.mirrortarballs[0]))
  1554. # Fetch and unpack with both the clonedir and shallow tarball available
  1555. bb.utils.remove(self.gitdir, recurse=True)
  1556. fetcher, ud = self.fetch_and_unpack()
  1557. # The unpacked tree should *not* be shallow
  1558. self.assertRevCount(2)
  1559. assert not os.path.exists(os.path.join(self.gitdir, '.git', 'shallow'))
  1560. def test_shallow_mirrors(self):
  1561. self.add_empty_file('a')
  1562. self.add_empty_file('b')
  1563. # Fetch once to generate the shallow tarball
  1564. fetcher, ud = self.fetch()
  1565. mirrortarball = ud.mirrortarballs[0]
  1566. assert os.path.exists(os.path.join(self.dldir, mirrortarball))
  1567. # Set up the mirror
  1568. mirrordir = os.path.join(self.tempdir, 'mirror')
  1569. bb.utils.mkdirhier(mirrordir)
  1570. self.d.setVar('PREMIRRORS', 'git://.*/.* file://%s/\n' % mirrordir)
  1571. os.rename(os.path.join(self.dldir, mirrortarball),
  1572. os.path.join(mirrordir, mirrortarball))
  1573. # Fetch from the mirror
  1574. bb.utils.remove(self.dldir, recurse=True)
  1575. bb.utils.remove(self.gitdir, recurse=True)
  1576. self.fetch_and_unpack()
  1577. self.assertRevCount(1)
  1578. def test_shallow_invalid_depth(self):
  1579. self.add_empty_file('a')
  1580. self.add_empty_file('b')
  1581. self.d.setVar('BB_GIT_SHALLOW_DEPTH', '-12')
  1582. with self.assertRaises(bb.fetch2.FetchError):
  1583. self.fetch()
  1584. def test_shallow_invalid_depth_default(self):
  1585. self.add_empty_file('a')
  1586. self.add_empty_file('b')
  1587. self.d.setVar('BB_GIT_SHALLOW_DEPTH_default', '-12')
  1588. with self.assertRaises(bb.fetch2.FetchError):
  1589. self.fetch()
  1590. def test_shallow_extra_refs(self):
  1591. self.add_empty_file('a')
  1592. self.add_empty_file('b')
  1593. self.git('branch a_branch', cwd=self.srcdir)
  1594. self.assertRefs(['master', 'a_branch'], cwd=self.srcdir)
  1595. self.assertRevCount(2, cwd=self.srcdir)
  1596. self.d.setVar('BB_GIT_SHALLOW_EXTRA_REFS', 'refs/heads/a_branch')
  1597. self.fetch_shallow()
  1598. self.assertRefs(['master', 'origin/master', 'origin/a_branch'])
  1599. self.assertRevCount(1)
  1600. def test_shallow_extra_refs_wildcard(self):
  1601. self.add_empty_file('a')
  1602. self.add_empty_file('b')
  1603. self.git('branch a_branch', cwd=self.srcdir)
  1604. self.git('tag v1.0', cwd=self.srcdir)
  1605. self.assertRefs(['master', 'a_branch', 'v1.0'], cwd=self.srcdir)
  1606. self.assertRevCount(2, cwd=self.srcdir)
  1607. self.d.setVar('BB_GIT_SHALLOW_EXTRA_REFS', 'refs/tags/*')
  1608. self.fetch_shallow()
  1609. self.assertRefs(['master', 'origin/master', 'v1.0'])
  1610. self.assertRevCount(1)
  1611. def test_shallow_missing_extra_refs(self):
  1612. self.add_empty_file('a')
  1613. self.add_empty_file('b')
  1614. self.d.setVar('BB_GIT_SHALLOW_EXTRA_REFS', 'refs/heads/foo')
  1615. with self.assertRaises(bb.fetch2.FetchError):
  1616. self.fetch()
  1617. def test_shallow_missing_extra_refs_wildcard(self):
  1618. self.add_empty_file('a')
  1619. self.add_empty_file('b')
  1620. self.d.setVar('BB_GIT_SHALLOW_EXTRA_REFS', 'refs/tags/*')
  1621. self.fetch()
  1622. def test_shallow_remove_revs(self):
  1623. # Create initial git repo
  1624. self.add_empty_file('a')
  1625. self.add_empty_file('b')
  1626. self.git('checkout -b a_branch', cwd=self.srcdir)
  1627. self.add_empty_file('c')
  1628. self.add_empty_file('d')
  1629. self.git('checkout master', cwd=self.srcdir)
  1630. self.git('tag v0.0 a_branch', cwd=self.srcdir)
  1631. self.add_empty_file('e')
  1632. self.git('merge --no-ff --no-edit a_branch', cwd=self.srcdir)
  1633. self.git('branch -d a_branch', cwd=self.srcdir)
  1634. self.add_empty_file('f')
  1635. self.assertRevCount(7, cwd=self.srcdir)
  1636. self.d.setVar('BB_GIT_SHALLOW_DEPTH', '0')
  1637. self.d.setVar('BB_GIT_SHALLOW_REVS', 'v0.0')
  1638. self.fetch_shallow()
  1639. self.assertRevCount(5)
  1640. def test_shallow_invalid_revs(self):
  1641. self.add_empty_file('a')
  1642. self.add_empty_file('b')
  1643. self.d.setVar('BB_GIT_SHALLOW_DEPTH', '0')
  1644. self.d.setVar('BB_GIT_SHALLOW_REVS', 'v0.0')
  1645. with self.assertRaises(bb.fetch2.FetchError):
  1646. self.fetch()
  1647. def test_shallow_fetch_missing_revs(self):
  1648. self.add_empty_file('a')
  1649. self.add_empty_file('b')
  1650. fetcher, ud = self.fetch(self.d.getVar('SRC_URI'))
  1651. self.git('tag v0.0 master', cwd=self.srcdir)
  1652. self.d.setVar('BB_GIT_SHALLOW_DEPTH', '0')
  1653. self.d.setVar('BB_GIT_SHALLOW_REVS', 'v0.0')
  1654. self.fetch_shallow()
  1655. def test_shallow_fetch_missing_revs_fails(self):
  1656. self.add_empty_file('a')
  1657. self.add_empty_file('b')
  1658. fetcher, ud = self.fetch(self.d.getVar('SRC_URI'))
  1659. self.d.setVar('BB_GIT_SHALLOW_DEPTH', '0')
  1660. self.d.setVar('BB_GIT_SHALLOW_REVS', 'v0.0')
  1661. with self.assertRaises(bb.fetch2.FetchError), self.assertLogs("BitBake.Fetcher", level="ERROR") as cm:
  1662. self.fetch_shallow()
  1663. self.assertIn("Unable to find revision v0.0 even from upstream", cm.output[0])
  1664. @skipIfNoNetwork()
  1665. def test_bitbake(self):
  1666. self.git('remote add --mirror=fetch origin git://github.com/openembedded/bitbake', cwd=self.srcdir)
  1667. self.git('config core.bare true', cwd=self.srcdir)
  1668. self.git('fetch', cwd=self.srcdir)
  1669. self.d.setVar('BB_GIT_SHALLOW_DEPTH', '0')
  1670. # Note that the 1.10.0 tag is annotated, so this also tests
  1671. # reference of an annotated vs unannotated tag
  1672. self.d.setVar('BB_GIT_SHALLOW_REVS', '1.10.0')
  1673. self.fetch_shallow()
  1674. # Confirm that the history of 1.10.0 was removed
  1675. orig_revs = len(self.git('rev-list master', cwd=self.srcdir).splitlines())
  1676. revs = len(self.git('rev-list master').splitlines())
  1677. self.assertNotEqual(orig_revs, revs)
  1678. self.assertRefs(['master', 'origin/master'])
  1679. self.assertRevCount(orig_revs - 1758)
  1680. def test_that_unpack_throws_an_error_when_the_git_clone_nor_shallow_tarball_exist(self):
  1681. self.add_empty_file('a')
  1682. fetcher, ud = self.fetch()
  1683. bb.utils.remove(self.gitdir, recurse=True)
  1684. bb.utils.remove(self.dldir, recurse=True)
  1685. with self.assertRaises(bb.fetch2.UnpackError) as context:
  1686. fetcher.unpack(self.d.getVar('WORKDIR'))
  1687. self.assertIn("No up to date source found", context.exception.msg)
  1688. self.assertIn("clone directory not available or not up to date", context.exception.msg)
  1689. @skipIfNoNetwork()
  1690. def test_that_unpack_does_work_when_using_git_shallow_tarball_but_tarball_is_not_available(self):
  1691. self.d.setVar('SRCREV', 'e5939ff608b95cdd4d0ab0e1935781ab9a276ac0')
  1692. self.d.setVar('BB_GIT_SHALLOW', '1')
  1693. self.d.setVar('BB_GENERATE_SHALLOW_TARBALLS', '1')
  1694. fetcher = bb.fetch.Fetch(["git://git.yoctoproject.org/fstests"], self.d)
  1695. fetcher.download()
  1696. bb.utils.remove(self.dldir + "/*.tar.gz")
  1697. fetcher.unpack(self.unpackdir)
  1698. dir = os.listdir(self.unpackdir + "/git/")
  1699. self.assertIn("fstests.doap", dir)
  1700. class GitLfsTest(FetcherTest):
  1701. def setUp(self):
  1702. FetcherTest.setUp(self)
  1703. self.gitdir = os.path.join(self.tempdir, 'git')
  1704. self.srcdir = os.path.join(self.tempdir, 'gitsource')
  1705. self.d.setVar('WORKDIR', self.tempdir)
  1706. self.d.setVar('S', self.gitdir)
  1707. self.d.delVar('PREMIRRORS')
  1708. self.d.delVar('MIRRORS')
  1709. self.d.setVar('SRCREV', '${AUTOREV}')
  1710. self.d.setVar('AUTOREV', '${@bb.fetch2.get_autorev(d)}')
  1711. bb.utils.mkdirhier(self.srcdir)
  1712. self.git('init', cwd=self.srcdir)
  1713. with open(os.path.join(self.srcdir, '.gitattributes'), 'wt') as attrs:
  1714. attrs.write('*.mp3 filter=lfs -text')
  1715. self.git(['add', '.gitattributes'], cwd=self.srcdir)
  1716. self.git(['commit', '-m', "attributes", '.gitattributes'], cwd=self.srcdir)
  1717. def git(self, cmd, cwd=None):
  1718. if isinstance(cmd, str):
  1719. cmd = 'git ' + cmd
  1720. else:
  1721. cmd = ['git'] + cmd
  1722. if cwd is None:
  1723. cwd = self.gitdir
  1724. return bb.process.run(cmd, cwd=cwd)[0]
  1725. def fetch(self, uri=None):
  1726. uris = self.d.getVar('SRC_URI').split()
  1727. uri = uris[0]
  1728. d = self.d
  1729. fetcher = bb.fetch2.Fetch(uris, d)
  1730. fetcher.download()
  1731. ud = fetcher.ud[uri]
  1732. return fetcher, ud
  1733. def test_lfs_enabled(self):
  1734. import shutil
  1735. uri = 'git://%s;protocol=file;subdir=${S};lfs=1' % self.srcdir
  1736. self.d.setVar('SRC_URI', uri)
  1737. fetcher, ud = self.fetch()
  1738. self.assertIsNotNone(ud.method._find_git_lfs)
  1739. # If git-lfs can be found, the unpack should be successful
  1740. ud.method._find_git_lfs = lambda d: True
  1741. shutil.rmtree(self.gitdir, ignore_errors=True)
  1742. fetcher.unpack(self.d.getVar('WORKDIR'))
  1743. # If git-lfs cannot be found, the unpack should throw an error
  1744. with self.assertRaises(bb.fetch2.FetchError):
  1745. ud.method._find_git_lfs = lambda d: False
  1746. shutil.rmtree(self.gitdir, ignore_errors=True)
  1747. fetcher.unpack(self.d.getVar('WORKDIR'))
  1748. def test_lfs_disabled(self):
  1749. import shutil
  1750. uri = 'git://%s;protocol=file;subdir=${S};lfs=0' % self.srcdir
  1751. self.d.setVar('SRC_URI', uri)
  1752. fetcher, ud = self.fetch()
  1753. self.assertIsNotNone(ud.method._find_git_lfs)
  1754. # If git-lfs can be found, the unpack should be successful
  1755. ud.method._find_git_lfs = lambda d: True
  1756. shutil.rmtree(self.gitdir, ignore_errors=True)
  1757. fetcher.unpack(self.d.getVar('WORKDIR'))
  1758. # If git-lfs cannot be found, the unpack should be successful
  1759. ud.method._find_git_lfs = lambda d: False
  1760. shutil.rmtree(self.gitdir, ignore_errors=True)
  1761. fetcher.unpack(self.d.getVar('WORKDIR'))
  1762. class NPMTest(FetcherTest):
  1763. def skipIfNoNpm():
  1764. import shutil
  1765. if not shutil.which('npm'):
  1766. return unittest.skip('npm not installed, tests being skipped')
  1767. return lambda f: f
  1768. @skipIfNoNpm()
  1769. @skipIfNoNetwork()
  1770. def test_npm(self):
  1771. url = 'npm://registry.npmjs.org;package=@savoirfairelinux/node-server-example;version=1.0.0'
  1772. fetcher = bb.fetch.Fetch([url], self.d)
  1773. ud = fetcher.ud[fetcher.urls[0]]
  1774. fetcher.download()
  1775. self.assertTrue(os.path.exists(ud.localpath))
  1776. self.assertTrue(os.path.exists(ud.localpath + '.done'))
  1777. self.assertTrue(os.path.exists(ud.resolvefile))
  1778. fetcher.unpack(self.unpackdir)
  1779. unpackdir = os.path.join(self.unpackdir, 'npm')
  1780. self.assertTrue(os.path.exists(os.path.join(unpackdir, 'package.json')))
  1781. @skipIfNoNpm()
  1782. @skipIfNoNetwork()
  1783. def test_npm_bad_checksum(self):
  1784. url = 'npm://registry.npmjs.org;package=@savoirfairelinux/node-server-example;version=1.0.0'
  1785. # Fetch once to get a tarball
  1786. fetcher = bb.fetch.Fetch([url], self.d)
  1787. ud = fetcher.ud[fetcher.urls[0]]
  1788. fetcher.download()
  1789. self.assertTrue(os.path.exists(ud.localpath))
  1790. # Modify the tarball
  1791. bad = b'bad checksum'
  1792. with open(ud.localpath, 'wb') as f:
  1793. f.write(bad)
  1794. # Verify that the tarball is fetched again
  1795. fetcher.download()
  1796. badsum = hashlib.sha512(bad).hexdigest()
  1797. self.assertTrue(os.path.exists(ud.localpath + '_bad-checksum_' + badsum))
  1798. self.assertTrue(os.path.exists(ud.localpath))
  1799. @skipIfNoNpm()
  1800. @skipIfNoNetwork()
  1801. def test_npm_premirrors(self):
  1802. url = 'npm://registry.npmjs.org;package=@savoirfairelinux/node-server-example;version=1.0.0'
  1803. # Fetch once to get a tarball
  1804. fetcher = bb.fetch.Fetch([url], self.d)
  1805. ud = fetcher.ud[fetcher.urls[0]]
  1806. fetcher.download()
  1807. self.assertTrue(os.path.exists(ud.localpath))
  1808. # Setup the mirror
  1809. mirrordir = os.path.join(self.tempdir, 'mirror')
  1810. bb.utils.mkdirhier(mirrordir)
  1811. os.replace(ud.localpath, os.path.join(mirrordir, os.path.basename(ud.localpath)))
  1812. self.d.setVar('PREMIRRORS', 'https?$://.*/.* file://%s/\n' % mirrordir)
  1813. self.d.setVar('BB_FETCH_PREMIRRORONLY', '1')
  1814. # Fetch again
  1815. self.assertFalse(os.path.exists(ud.localpath))
  1816. fetcher.download()
  1817. self.assertTrue(os.path.exists(ud.localpath))
  1818. @skipIfNoNpm()
  1819. @skipIfNoNetwork()
  1820. def test_npm_mirrors(self):
  1821. # Fetch once to get a tarball
  1822. url = 'npm://registry.npmjs.org;package=@savoirfairelinux/node-server-example;version=1.0.0'
  1823. fetcher = bb.fetch.Fetch([url], self.d)
  1824. ud = fetcher.ud[fetcher.urls[0]]
  1825. fetcher.download()
  1826. self.assertTrue(os.path.exists(ud.localpath))
  1827. # Setup the mirror
  1828. mirrordir = os.path.join(self.tempdir, 'mirror')
  1829. bb.utils.mkdirhier(mirrordir)
  1830. os.replace(ud.localpath, os.path.join(mirrordir, os.path.basename(ud.localpath)))
  1831. self.d.setVar('MIRRORS', 'https?$://.*/.* file://%s/\n' % mirrordir)
  1832. # Update the resolved url to an invalid url
  1833. with open(ud.resolvefile, 'r') as f:
  1834. url = f.read()
  1835. uri = URI(url)
  1836. uri.path = '/invalid'
  1837. with open(ud.resolvefile, 'w') as f:
  1838. f.write(str(uri))
  1839. # Fetch again
  1840. self.assertFalse(os.path.exists(ud.localpath))
  1841. fetcher.download()
  1842. self.assertTrue(os.path.exists(ud.localpath))
  1843. @skipIfNoNpm()
  1844. @skipIfNoNetwork()
  1845. def test_npm_destsuffix_downloadfilename(self):
  1846. url = 'npm://registry.npmjs.org;package=@savoirfairelinux/node-server-example;version=1.0.0;destsuffix=foo/bar;downloadfilename=foo-bar.tgz'
  1847. fetcher = bb.fetch.Fetch([url], self.d)
  1848. fetcher.download()
  1849. self.assertTrue(os.path.exists(os.path.join(self.dldir, 'foo-bar.tgz')))
  1850. fetcher.unpack(self.unpackdir)
  1851. unpackdir = os.path.join(self.unpackdir, 'foo', 'bar')
  1852. self.assertTrue(os.path.exists(os.path.join(unpackdir, 'package.json')))
  1853. def test_npm_no_network_no_tarball(self):
  1854. url = 'npm://registry.npmjs.org;package=@savoirfairelinux/node-server-example;version=1.0.0'
  1855. self.d.setVar('BB_NO_NETWORK', '1')
  1856. fetcher = bb.fetch.Fetch([url], self.d)
  1857. with self.assertRaises(bb.fetch2.NetworkAccess):
  1858. fetcher.download()
  1859. @skipIfNoNpm()
  1860. @skipIfNoNetwork()
  1861. def test_npm_no_network_with_tarball(self):
  1862. url = 'npm://registry.npmjs.org;package=@savoirfairelinux/node-server-example;version=1.0.0'
  1863. # Fetch once to get a tarball
  1864. fetcher = bb.fetch.Fetch([url], self.d)
  1865. fetcher.download()
  1866. # Disable network access
  1867. self.d.setVar('BB_NO_NETWORK', '1')
  1868. # Fetch again
  1869. fetcher.download()
  1870. fetcher.unpack(self.unpackdir)
  1871. unpackdir = os.path.join(self.unpackdir, 'npm')
  1872. self.assertTrue(os.path.exists(os.path.join(unpackdir, 'package.json')))
  1873. @skipIfNoNpm()
  1874. @skipIfNoNetwork()
  1875. def test_npm_registry_alternate(self):
  1876. url = 'npm://registry.freajs.org;package=@savoirfairelinux/node-server-example;version=1.0.0'
  1877. fetcher = bb.fetch.Fetch([url], self.d)
  1878. fetcher.download()
  1879. fetcher.unpack(self.unpackdir)
  1880. unpackdir = os.path.join(self.unpackdir, 'npm')
  1881. self.assertTrue(os.path.exists(os.path.join(unpackdir, 'package.json')))
  1882. @skipIfNoNpm()
  1883. @skipIfNoNetwork()
  1884. def test_npm_version_latest(self):
  1885. url = 'npm://registry.npmjs.org;package=@savoirfairelinux/node-server-example;version=latest'
  1886. fetcher = bb.fetch.Fetch([url], self.d)
  1887. fetcher.download()
  1888. fetcher.unpack(self.unpackdir)
  1889. unpackdir = os.path.join(self.unpackdir, 'npm')
  1890. self.assertTrue(os.path.exists(os.path.join(unpackdir, 'package.json')))
  1891. @skipIfNoNpm()
  1892. @skipIfNoNetwork()
  1893. def test_npm_registry_invalid(self):
  1894. url = 'npm://registry.invalid.org;package=@savoirfairelinux/node-server-example;version=1.0.0'
  1895. fetcher = bb.fetch.Fetch([url], self.d)
  1896. with self.assertRaises(bb.fetch2.FetchError):
  1897. fetcher.download()
  1898. @skipIfNoNpm()
  1899. @skipIfNoNetwork()
  1900. def test_npm_package_invalid(self):
  1901. url = 'npm://registry.npmjs.org;package=@savoirfairelinux/invalid;version=1.0.0'
  1902. fetcher = bb.fetch.Fetch([url], self.d)
  1903. with self.assertRaises(bb.fetch2.FetchError):
  1904. fetcher.download()
  1905. @skipIfNoNpm()
  1906. @skipIfNoNetwork()
  1907. def test_npm_version_invalid(self):
  1908. url = 'npm://registry.npmjs.org;package=@savoirfairelinux/node-server-example;version=invalid'
  1909. with self.assertRaises(bb.fetch2.ParameterError):
  1910. fetcher = bb.fetch.Fetch([url], self.d)
  1911. @skipIfNoNpm()
  1912. @skipIfNoNetwork()
  1913. def test_npm_registry_none(self):
  1914. url = 'npm://;package=@savoirfairelinux/node-server-example;version=1.0.0'
  1915. with self.assertRaises(bb.fetch2.MalformedUrl):
  1916. fetcher = bb.fetch.Fetch([url], self.d)
  1917. @skipIfNoNpm()
  1918. @skipIfNoNetwork()
  1919. def test_npm_package_none(self):
  1920. url = 'npm://registry.npmjs.org;version=1.0.0'
  1921. with self.assertRaises(bb.fetch2.MissingParameterError):
  1922. fetcher = bb.fetch.Fetch([url], self.d)
  1923. @skipIfNoNpm()
  1924. @skipIfNoNetwork()
  1925. def test_npm_version_none(self):
  1926. url = 'npm://registry.npmjs.org;package=@savoirfairelinux/node-server-example'
  1927. with self.assertRaises(bb.fetch2.MissingParameterError):
  1928. fetcher = bb.fetch.Fetch([url], self.d)
  1929. def create_shrinkwrap_file(self, data):
  1930. import json
  1931. datadir = os.path.join(self.tempdir, 'data')
  1932. swfile = os.path.join(datadir, 'npm-shrinkwrap.json')
  1933. bb.utils.mkdirhier(datadir)
  1934. with open(swfile, 'w') as f:
  1935. json.dump(data, f)
  1936. # Also configure the S directory
  1937. self.sdir = os.path.join(self.unpackdir, 'S')
  1938. self.d.setVar('S', self.sdir)
  1939. return swfile
  1940. @skipIfNoNpm()
  1941. @skipIfNoNetwork()
  1942. def test_npmsw(self):
  1943. swfile = self.create_shrinkwrap_file({
  1944. 'dependencies': {
  1945. 'array-flatten': {
  1946. 'version': '1.1.1',
  1947. 'resolved': 'https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz',
  1948. 'integrity': 'sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=',
  1949. 'dependencies': {
  1950. 'content-type': {
  1951. 'version': 'https://registry.npmjs.org/content-type/-/content-type-1.0.4.tgz',
  1952. 'integrity': 'sha512-hIP3EEPs8tB9AT1L+NUqtwOAps4mk2Zob89MWXMHjHWg9milF/j4osnnQLXBCBFBk/tvIG/tUc9mOUJiPBhPXA==',
  1953. 'dependencies': {
  1954. 'cookie': {
  1955. 'version': 'git+https://github.com/jshttp/cookie.git#aec1177c7da67e3b3273df96cf476824dbc9ae09',
  1956. 'from': 'git+https://github.com/jshttp/cookie.git'
  1957. }
  1958. }
  1959. }
  1960. }
  1961. }
  1962. }
  1963. })
  1964. fetcher = bb.fetch.Fetch(['npmsw://' + swfile], self.d)
  1965. fetcher.download()
  1966. self.assertTrue(os.path.exists(os.path.join(self.dldir, 'npm2', 'array-flatten-1.1.1.tgz')))
  1967. self.assertTrue(os.path.exists(os.path.join(self.dldir, 'npm2', 'content-type-1.0.4.tgz')))
  1968. self.assertTrue(os.path.exists(os.path.join(self.dldir, 'git2', 'github.com.jshttp.cookie.git')))
  1969. fetcher.unpack(self.unpackdir)
  1970. self.assertTrue(os.path.exists(os.path.join(self.sdir, 'npm-shrinkwrap.json')))
  1971. self.assertTrue(os.path.exists(os.path.join(self.sdir, 'node_modules', 'array-flatten', 'package.json')))
  1972. self.assertTrue(os.path.exists(os.path.join(self.sdir, 'node_modules', 'array-flatten', 'node_modules', 'content-type', 'package.json')))
  1973. self.assertTrue(os.path.exists(os.path.join(self.sdir, 'node_modules', 'array-flatten', 'node_modules', 'content-type', 'node_modules', 'cookie', 'package.json')))
  1974. @skipIfNoNpm()
  1975. @skipIfNoNetwork()
  1976. def test_npmsw_dev(self):
  1977. swfile = self.create_shrinkwrap_file({
  1978. 'dependencies': {
  1979. 'array-flatten': {
  1980. 'version': '1.1.1',
  1981. 'resolved': 'https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz',
  1982. 'integrity': 'sha1-ml9pkFGx5wczKPKgCJaLZOopVdI='
  1983. },
  1984. 'content-type': {
  1985. 'version': '1.0.4',
  1986. 'resolved': 'https://registry.npmjs.org/content-type/-/content-type-1.0.4.tgz',
  1987. 'integrity': 'sha512-hIP3EEPs8tB9AT1L+NUqtwOAps4mk2Zob89MWXMHjHWg9milF/j4osnnQLXBCBFBk/tvIG/tUc9mOUJiPBhPXA==',
  1988. 'dev': True
  1989. }
  1990. }
  1991. })
  1992. # Fetch with dev disabled
  1993. fetcher = bb.fetch.Fetch(['npmsw://' + swfile], self.d)
  1994. fetcher.download()
  1995. self.assertTrue(os.path.exists(os.path.join(self.dldir, 'npm2', 'array-flatten-1.1.1.tgz')))
  1996. self.assertFalse(os.path.exists(os.path.join(self.dldir, 'npm2', 'content-type-1.0.4.tgz')))
  1997. # Fetch with dev enabled
  1998. fetcher = bb.fetch.Fetch(['npmsw://' + swfile + ';dev=1'], self.d)
  1999. fetcher.download()
  2000. self.assertTrue(os.path.exists(os.path.join(self.dldir, 'npm2', 'array-flatten-1.1.1.tgz')))
  2001. self.assertTrue(os.path.exists(os.path.join(self.dldir, 'npm2', 'content-type-1.0.4.tgz')))
  2002. @skipIfNoNpm()
  2003. @skipIfNoNetwork()
  2004. def test_npmsw_destsuffix(self):
  2005. swfile = self.create_shrinkwrap_file({
  2006. 'dependencies': {
  2007. 'array-flatten': {
  2008. 'version': '1.1.1',
  2009. 'resolved': 'https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz',
  2010. 'integrity': 'sha1-ml9pkFGx5wczKPKgCJaLZOopVdI='
  2011. }
  2012. }
  2013. })
  2014. fetcher = bb.fetch.Fetch(['npmsw://' + swfile + ';destsuffix=foo/bar'], self.d)
  2015. fetcher.download()
  2016. fetcher.unpack(self.unpackdir)
  2017. self.assertTrue(os.path.exists(os.path.join(self.unpackdir, 'foo', 'bar', 'node_modules', 'array-flatten', 'package.json')))
  2018. def test_npmsw_no_network_no_tarball(self):
  2019. swfile = self.create_shrinkwrap_file({
  2020. 'dependencies': {
  2021. 'array-flatten': {
  2022. 'version': '1.1.1',
  2023. 'resolved': 'https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz',
  2024. 'integrity': 'sha1-ml9pkFGx5wczKPKgCJaLZOopVdI='
  2025. }
  2026. }
  2027. })
  2028. self.d.setVar('BB_NO_NETWORK', '1')
  2029. fetcher = bb.fetch.Fetch(['npmsw://' + swfile], self.d)
  2030. with self.assertRaises(bb.fetch2.NetworkAccess):
  2031. fetcher.download()
  2032. @skipIfNoNpm()
  2033. @skipIfNoNetwork()
  2034. def test_npmsw_no_network_with_tarball(self):
  2035. # Fetch once to get a tarball
  2036. fetcher = bb.fetch.Fetch(['npm://registry.npmjs.org;package=array-flatten;version=1.1.1'], self.d)
  2037. fetcher.download()
  2038. # Disable network access
  2039. self.d.setVar('BB_NO_NETWORK', '1')
  2040. # Fetch again
  2041. swfile = self.create_shrinkwrap_file({
  2042. 'dependencies': {
  2043. 'array-flatten': {
  2044. 'version': '1.1.1',
  2045. 'resolved': 'https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz',
  2046. 'integrity': 'sha1-ml9pkFGx5wczKPKgCJaLZOopVdI='
  2047. }
  2048. }
  2049. })
  2050. fetcher = bb.fetch.Fetch(['npmsw://' + swfile], self.d)
  2051. fetcher.download()
  2052. fetcher.unpack(self.unpackdir)
  2053. self.assertTrue(os.path.exists(os.path.join(self.sdir, 'node_modules', 'array-flatten', 'package.json')))
  2054. @skipIfNoNpm()
  2055. @skipIfNoNetwork()
  2056. def test_npmsw_npm_reusability(self):
  2057. # Fetch once with npmsw
  2058. swfile = self.create_shrinkwrap_file({
  2059. 'dependencies': {
  2060. 'array-flatten': {
  2061. 'version': '1.1.1',
  2062. 'resolved': 'https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz',
  2063. 'integrity': 'sha1-ml9pkFGx5wczKPKgCJaLZOopVdI='
  2064. }
  2065. }
  2066. })
  2067. fetcher = bb.fetch.Fetch(['npmsw://' + swfile], self.d)
  2068. fetcher.download()
  2069. # Disable network access
  2070. self.d.setVar('BB_NO_NETWORK', '1')
  2071. # Fetch again with npm
  2072. fetcher = bb.fetch.Fetch(['npm://registry.npmjs.org;package=array-flatten;version=1.1.1'], self.d)
  2073. fetcher.download()
  2074. fetcher.unpack(self.unpackdir)
  2075. self.assertTrue(os.path.exists(os.path.join(self.unpackdir, 'npm', 'package.json')))
  2076. @skipIfNoNpm()
  2077. @skipIfNoNetwork()
  2078. def test_npmsw_bad_checksum(self):
  2079. # Try to fetch with bad checksum
  2080. swfile = self.create_shrinkwrap_file({
  2081. 'dependencies': {
  2082. 'array-flatten': {
  2083. 'version': '1.1.1',
  2084. 'resolved': 'https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz',
  2085. 'integrity': 'sha1-gfNEp2hqgLTFKT6P3AsBYMgsBqg='
  2086. }
  2087. }
  2088. })
  2089. fetcher = bb.fetch.Fetch(['npmsw://' + swfile], self.d)
  2090. with self.assertRaises(bb.fetch2.FetchError):
  2091. fetcher.download()
  2092. # Fetch correctly to get a tarball
  2093. swfile = self.create_shrinkwrap_file({
  2094. 'dependencies': {
  2095. 'array-flatten': {
  2096. 'version': '1.1.1',
  2097. 'resolved': 'https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz',
  2098. 'integrity': 'sha1-ml9pkFGx5wczKPKgCJaLZOopVdI='
  2099. }
  2100. }
  2101. })
  2102. fetcher = bb.fetch.Fetch(['npmsw://' + swfile], self.d)
  2103. fetcher.download()
  2104. localpath = os.path.join(self.dldir, 'npm2', 'array-flatten-1.1.1.tgz')
  2105. self.assertTrue(os.path.exists(localpath))
  2106. # Modify the tarball
  2107. bad = b'bad checksum'
  2108. with open(localpath, 'wb') as f:
  2109. f.write(bad)
  2110. # Verify that the tarball is fetched again
  2111. fetcher.download()
  2112. badsum = hashlib.sha1(bad).hexdigest()
  2113. self.assertTrue(os.path.exists(localpath + '_bad-checksum_' + badsum))
  2114. self.assertTrue(os.path.exists(localpath))
  2115. @skipIfNoNpm()
  2116. @skipIfNoNetwork()
  2117. def test_npmsw_premirrors(self):
  2118. # Fetch once to get a tarball
  2119. fetcher = bb.fetch.Fetch(['npm://registry.npmjs.org;package=array-flatten;version=1.1.1'], self.d)
  2120. ud = fetcher.ud[fetcher.urls[0]]
  2121. fetcher.download()
  2122. self.assertTrue(os.path.exists(ud.localpath))
  2123. # Setup the mirror
  2124. mirrordir = os.path.join(self.tempdir, 'mirror')
  2125. bb.utils.mkdirhier(mirrordir)
  2126. os.replace(ud.localpath, os.path.join(mirrordir, os.path.basename(ud.localpath)))
  2127. self.d.setVar('PREMIRRORS', 'https?$://.*/.* file://%s/\n' % mirrordir)
  2128. self.d.setVar('BB_FETCH_PREMIRRORONLY', '1')
  2129. # Fetch again
  2130. self.assertFalse(os.path.exists(ud.localpath))
  2131. swfile = self.create_shrinkwrap_file({
  2132. 'dependencies': {
  2133. 'array-flatten': {
  2134. 'version': '1.1.1',
  2135. 'resolved': 'https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz',
  2136. 'integrity': 'sha1-ml9pkFGx5wczKPKgCJaLZOopVdI='
  2137. }
  2138. }
  2139. })
  2140. fetcher = bb.fetch.Fetch(['npmsw://' + swfile], self.d)
  2141. fetcher.download()
  2142. self.assertTrue(os.path.exists(ud.localpath))
  2143. @skipIfNoNpm()
  2144. @skipIfNoNetwork()
  2145. def test_npmsw_mirrors(self):
  2146. # Fetch once to get a tarball
  2147. fetcher = bb.fetch.Fetch(['npm://registry.npmjs.org;package=array-flatten;version=1.1.1'], self.d)
  2148. ud = fetcher.ud[fetcher.urls[0]]
  2149. fetcher.download()
  2150. self.assertTrue(os.path.exists(ud.localpath))
  2151. # Setup the mirror
  2152. mirrordir = os.path.join(self.tempdir, 'mirror')
  2153. bb.utils.mkdirhier(mirrordir)
  2154. os.replace(ud.localpath, os.path.join(mirrordir, os.path.basename(ud.localpath)))
  2155. self.d.setVar('MIRRORS', 'https?$://.*/.* file://%s/\n' % mirrordir)
  2156. # Fetch again with invalid url
  2157. self.assertFalse(os.path.exists(ud.localpath))
  2158. swfile = self.create_shrinkwrap_file({
  2159. 'dependencies': {
  2160. 'array-flatten': {
  2161. 'version': '1.1.1',
  2162. 'resolved': 'https://invalid',
  2163. 'integrity': 'sha1-ml9pkFGx5wczKPKgCJaLZOopVdI='
  2164. }
  2165. }
  2166. })
  2167. fetcher = bb.fetch.Fetch(['npmsw://' + swfile], self.d)
  2168. fetcher.download()
  2169. self.assertTrue(os.path.exists(ud.localpath))