fetch.py 130 KB

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