fetch.py 113 KB

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