store.scm 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (test-store)
  19. #:use-module (guix tests)
  20. #:use-module (guix config)
  21. #:use-module (guix store)
  22. #:use-module (guix utils)
  23. #:use-module (guix monads)
  24. #:use-module ((gcrypt hash) #:prefix gcrypt:)
  25. #:use-module ((gcrypt pk-crypto) #:prefix gcrypt:)
  26. #:use-module (guix pki)
  27. #:use-module (guix base32)
  28. #:use-module (guix packages)
  29. #:use-module (guix derivations)
  30. #:use-module (guix serialization)
  31. #:use-module (guix build utils)
  32. #:use-module (guix gexp)
  33. #:use-module (gnu packages)
  34. #:use-module (gnu packages bootstrap)
  35. #:use-module (ice-9 match)
  36. #:use-module (ice-9 regex)
  37. #:use-module (rnrs bytevectors)
  38. #:use-module (rnrs io ports)
  39. #:use-module (web uri)
  40. #:use-module (srfi srfi-1)
  41. #:use-module (srfi srfi-11)
  42. #:use-module (srfi srfi-26)
  43. #:use-module (srfi srfi-34)
  44. #:use-module (srfi srfi-64))
  45. ;; Test the (guix store) module.
  46. (define %store
  47. (open-connection-for-tests))
  48. (define %shell
  49. (or (getenv "SHELL") (getenv "CONFIG_SHELL") "/bin/sh"))
  50. (test-begin "store")
  51. (test-assert "open-connection with file:// URI"
  52. (let ((store (open-connection (string-append "file://"
  53. (%daemon-socket-uri)))))
  54. (and (add-text-to-store store "foo" "bar")
  55. (begin
  56. (close-connection store)
  57. #t))))
  58. (test-equal "connection handshake error"
  59. EPROTO
  60. (let ((port (%make-void-port "rw")))
  61. (guard (c ((store-connection-error? c)
  62. (and (eq? port (store-connection-error-file c))
  63. (store-connection-error-code c))))
  64. (open-connection #f #:port port)
  65. 'broken)))
  66. (test-equal "store-path-hash-part"
  67. "283gqy39v3g9dxjy26rynl0zls82fmcg"
  68. (store-path-hash-part
  69. (string-append (%store-prefix)
  70. "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
  71. (test-equal "store-path-hash-part #f"
  72. #f
  73. (store-path-hash-part
  74. (string-append (%store-prefix)
  75. "/foo/bar/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
  76. (test-equal "store-path-package-name"
  77. "guile-2.0.7"
  78. (store-path-package-name
  79. (string-append (%store-prefix)
  80. "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7")))
  81. (test-equal "store-path-package-name #f"
  82. #f
  83. (store-path-package-name
  84. "/foo/bar/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7"))
  85. (test-assert "direct-store-path?"
  86. (and (direct-store-path?
  87. (string-append (%store-prefix)
  88. "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7"))
  89. (not (direct-store-path?
  90. (string-append
  91. (%store-prefix)
  92. "/283gqy39v3g9dxjy26rynl0zls82fmcg-guile-2.0.7/bin/guile")))
  93. (not (direct-store-path? (%store-prefix)))))
  94. (test-skip (if %store 0 15))
  95. (test-equal "profiles/per-user exists and is not writable"
  96. #o755
  97. (stat:perms (stat (string-append %state-directory "/profiles/per-user"))))
  98. (test-equal "profiles/per-user/$USER exists"
  99. (list (getuid) #o755)
  100. (let ((s (stat (string-append %state-directory "/profiles/per-user/"
  101. (passwd:name (getpwuid (getuid)))))))
  102. (list (stat:uid s) (stat:perms s))))
  103. (test-equal "add-to-store"
  104. '("sha1" "sha256" "sha512" "sha3-256" "sha3-512" "blake2s-256")
  105. (let* ((file (search-path %load-path "guix.scm"))
  106. (content (call-with-input-file file get-bytevector-all)))
  107. (map (lambda (hash-algo)
  108. (let ((file (add-to-store %store "guix.scm" #f hash-algo file)))
  109. (and (direct-store-path? file)
  110. (bytevector=? (call-with-input-file file get-bytevector-all)
  111. content)
  112. hash-algo)))
  113. '("sha1" "sha256" "sha512" "sha3-256" "sha3-512" "blake2s-256"))))
  114. (test-equal "add-data-to-store"
  115. #vu8(1 2 3 4 5)
  116. (call-with-input-file (add-data-to-store %store "data" #vu8(1 2 3 4 5))
  117. get-bytevector-all))
  118. (test-assert "valid-path? live"
  119. (let ((p (add-text-to-store %store "hello" "hello, world")))
  120. (valid-path? %store p)))
  121. (test-assert "valid-path? false"
  122. (not (valid-path? %store
  123. (string-append (%store-prefix) "/"
  124. (make-string 32 #\e) "-foobar"))))
  125. (test-equal "with-store, multiple values" ;<https://bugs.gnu.org/42912>
  126. '(1 2 3)
  127. (call-with-values
  128. (lambda ()
  129. (with-store s
  130. (add-text-to-store s "foo" "bar")
  131. (values 1 2 3)))
  132. list))
  133. (test-assert "valid-path? error"
  134. (with-store s
  135. (guard (c ((store-protocol-error? c) #t))
  136. (valid-path? s "foo")
  137. #f)))
  138. (test-assert "valid-path? recovery"
  139. ;; Prior to Nix commit 51800e0 (18 Mar. 2014), the daemon would immediately
  140. ;; close the connection after receiving a 'valid-path?' RPC with a non-store
  141. ;; file name. See
  142. ;; <http://article.gmane.org/gmane.linux.distributions.nixos/12411> for
  143. ;; details.
  144. (with-store s
  145. (let-syntax ((true-if-error (syntax-rules ()
  146. ((_ exp)
  147. (guard (c ((store-protocol-error? c) #t))
  148. exp #f)))))
  149. (and (true-if-error (valid-path? s "foo"))
  150. (true-if-error (valid-path? s "bar"))
  151. (true-if-error (valid-path? s "baz"))
  152. (true-if-error (valid-path? s "chbouib"))
  153. (valid-path? s (add-text-to-store s "valid" "yeah"))))))
  154. (test-assert "hash-part->path"
  155. (let ((p (add-text-to-store %store "hello" "hello, world")))
  156. (equal? (hash-part->path %store (store-path-hash-part p))
  157. p)))
  158. (test-assert "dead-paths"
  159. (let ((p (add-text-to-store %store "random-text" (random-text))))
  160. (->bool (member p (dead-paths %store)))))
  161. ;; FIXME: Find a test for `live-paths'.
  162. ;;
  163. ;; (test-assert "temporary root is in live-paths"
  164. ;; (let* ((p1 (add-text-to-store %store "random-text"
  165. ;; (random-text) '()))
  166. ;; (b (add-text-to-store %store "link-builder"
  167. ;; (format #f "echo ~a > $out" p1)
  168. ;; '()))
  169. ;; (d1 (derivation %store "link"
  170. ;; "/bin/sh" `("-e" ,b)
  171. ;; #:inputs `((,b) (,p1))))
  172. ;; (p2 (derivation->output-path d1)))
  173. ;; (and (add-temp-root %store p2)
  174. ;; (build-derivations %store (list d1))
  175. ;; (valid-path? %store p1)
  176. ;; (member (pk p2) (live-paths %store)))))
  177. (test-assert "add-indirect-root and find-roots"
  178. (call-with-temporary-directory
  179. (lambda (directory)
  180. (let* ((item (add-text-to-store %store "something" (random-text)))
  181. (root (string-append directory "/gc-root")))
  182. (symlink item root)
  183. (add-indirect-root %store root)
  184. (let ((result (member (cons root item) (find-roots %store))))
  185. (delete-file root)
  186. result)))))
  187. (test-assert "permanent root"
  188. (let* ((p (with-store store
  189. (let ((p (add-text-to-store store "random-text"
  190. (random-text))))
  191. (add-permanent-root p)
  192. (add-permanent-root p) ; should not throw
  193. p))))
  194. (and (member p (live-paths %store))
  195. (begin
  196. (remove-permanent-root p)
  197. (->bool (member p (dead-paths %store)))))))
  198. (test-assert "dead path can be explicitly collected"
  199. (let ((p (add-text-to-store %store "random-text"
  200. (random-text) '())))
  201. (let-values (((paths freed) (delete-paths %store (list p))))
  202. (and (equal? paths (list p))
  203. ;; XXX: On some file systems (notably Btrfs), freed
  204. ;; may return 0. See <https://bugs.gnu.org/29363>.
  205. ;;(> freed 0)
  206. (not (file-exists? p))))))
  207. (test-assert "add-text-to-store/add-to-store vs. delete-paths"
  208. ;; Before, 'add-text-to-store' and 'add-to-store' would return the same
  209. ;; store item without noticing that it is no longer valid.
  210. (with-store store
  211. (let* ((text (random-text))
  212. (file (search-path %load-path "guix.scm"))
  213. (path1 (add-text-to-store store "delete-me" text))
  214. (path2 (add-to-store store "delete-me" #t "sha256" file))
  215. (deleted (delete-paths store (list path1 path2))))
  216. (and (string=? path1 (add-text-to-store store "delete-me" text))
  217. (string=? path2 (add-to-store store "delete-me" #t "sha256" file))
  218. (lset= string=? deleted (list path1 path2))
  219. (valid-path? store path1)
  220. (valid-path? store path2)
  221. (file-exists? path1)
  222. (file-exists? path2)))))
  223. (test-equal "add-file-tree-to-store"
  224. `(42
  225. ("." directory #t)
  226. ("./bar" directory #t)
  227. ("./foo" directory #t)
  228. ("./foo/a" regular "file a")
  229. ("./foo/b" symlink "a")
  230. ("./foo/c" directory #t)
  231. ("./foo/c/p" regular "file p")
  232. ("./foo/c/q" directory #t)
  233. ("./foo/c/q/x" regular
  234. ,(string-append "#!" %shell "\nexit 42"))
  235. ("./foo/c/q/y" symlink "..")
  236. ("./foo/c/q/z" directory #t))
  237. (let* ((tree `("file-tree" directory
  238. ("foo" directory
  239. ("a" regular (data "file a"))
  240. ("b" symlink "a")
  241. ("c" directory
  242. ("p" regular (data ,(string->utf8 "file p")))
  243. ("q" directory
  244. ("x" executable
  245. (data ,(string-append "#!" %shell "\nexit 42")))
  246. ("y" symlink "..")
  247. ("z" directory))))
  248. ("bar" directory)))
  249. (result (add-file-tree-to-store %store tree)))
  250. (cons (status:exit-val (system* (string-append result "/foo/c/q/x")))
  251. (with-directory-excursion result
  252. (map (lambda (file)
  253. (let ((type (stat:type (lstat file))))
  254. `(,file ,type
  255. ,(match type
  256. ((or 'regular 'executable)
  257. (call-with-input-file file
  258. get-string-all))
  259. ('symlink (readlink file))
  260. ('directory #t)))))
  261. (find-files "." #:directories? #t))))))
  262. (test-equal "add-file-tree-to-store, flat"
  263. "Hello, world!"
  264. (let* ((tree `("flat-file" regular (data "Hello, world!")))
  265. (result (add-file-tree-to-store %store tree)))
  266. (and (file-exists? result)
  267. (call-with-input-file result get-string-all))))
  268. (test-assert "references"
  269. (let* ((t1 (add-text-to-store %store "random1"
  270. (random-text)))
  271. (t2 (add-text-to-store %store "random2"
  272. (random-text) (list t1))))
  273. (and (equal? (list t1) (references %store t2))
  274. (equal? (list t2) (referrers %store t1))
  275. (null? (references %store t1))
  276. (null? (referrers %store t2)))))
  277. (test-equal "substitutable-path-info when substitutes are turned off"
  278. '()
  279. (with-store s
  280. (set-build-options s #:use-substitutes? #f)
  281. (let* ((b (add-to-store s "bash" #t "sha256"
  282. (search-bootstrap-binary "bash"
  283. (%current-system))))
  284. (d (derivation s "the-thing" b '("--version")
  285. #:inputs `((,b))))
  286. (o (derivation->output-path d)))
  287. (with-derivation-narinfo d
  288. (substitutable-path-info s (list o))))))
  289. (test-equal "substitutable-paths when substitutes are turned off"
  290. '()
  291. (with-store s
  292. (set-build-options s #:use-substitutes? #f)
  293. (let* ((b (add-to-store s "bash" #t "sha256"
  294. (search-bootstrap-binary "bash"
  295. (%current-system))))
  296. (d (derivation s "the-thing" b '("--version")
  297. #:inputs `((,b))))
  298. (o (derivation->output-path d)))
  299. (with-derivation-narinfo d
  300. (substitutable-paths s (list o))))))
  301. (test-assert "requisites"
  302. (let* ((t1 (add-text-to-store %store "random1"
  303. (random-text) '()))
  304. (t2 (add-text-to-store %store "random2"
  305. (random-text) (list t1)))
  306. (t3 (add-text-to-store %store "random3"
  307. (random-text) (list t2)))
  308. (t4 (add-text-to-store %store "random4"
  309. (random-text) (list t1 t3))))
  310. (define (same? x y)
  311. (and (= (length x) (length y))
  312. (lset= equal? x y)))
  313. (and (same? (requisites %store (list t1)) (list t1))
  314. (same? (requisites %store (list t2)) (list t1 t2))
  315. (same? (requisites %store (list t3)) (list t1 t2 t3))
  316. (same? (requisites %store (list t4)) (list t1 t2 t3 t4))
  317. (same? (requisites %store (list t1 t2 t3 t4))
  318. (list t1 t2 t3 t4)))))
  319. (test-assert "derivers"
  320. (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
  321. (s (add-to-store %store "bash" #t "sha256"
  322. (search-bootstrap-binary "bash"
  323. (%current-system))))
  324. (d (derivation %store "the-thing"
  325. s `("-e" ,b)
  326. #:env-vars `(("foo" . ,(random-text)))
  327. #:inputs `((,b) (,s))))
  328. (o (derivation->output-path d)))
  329. (and (build-derivations %store (list d))
  330. (equal? (query-derivation-outputs %store (derivation-file-name d))
  331. (list o))
  332. (equal? (valid-derivers %store o)
  333. (list (derivation-file-name d))))))
  334. (test-equal "with-build-handler"
  335. 'success
  336. (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
  337. (s (add-to-store %store "bash" #t "sha256"
  338. (search-bootstrap-binary "bash"
  339. (%current-system))))
  340. (d1 (derivation %store "the-thing"
  341. s `("-e" ,b)
  342. #:env-vars `(("foo" . ,(random-text)))
  343. #:sources (list b s)))
  344. (d2 (derivation %store "the-thing"
  345. s `("-e" ,b)
  346. #:env-vars `(("foo" . ,(random-text))
  347. ("bar" . "baz"))
  348. #:sources (list b s)))
  349. (o1 (derivation->output-path d1))
  350. (o2 (derivation->output-path d2)))
  351. (with-build-handler
  352. (let ((counter 0))
  353. (lambda (continue store things mode)
  354. (match things
  355. ((drv)
  356. (set! counter (+ 1 counter))
  357. (if (string=? drv (derivation-file-name d1))
  358. (continue #t)
  359. (and (string=? drv (derivation-file-name d2))
  360. (= counter 2)
  361. 'success))))))
  362. (build-derivations %store (list d1))
  363. (build-derivations %store (list d2))
  364. 'fail)))
  365. (test-equal "with-build-handler + with-store"
  366. 'success
  367. ;; Check that STORE remains valid when the build handler invokes CONTINUE,
  368. ;; even though 'with-build-handler' is outside the dynamic extent of
  369. ;; 'with-store'.
  370. (with-build-handler (lambda (continue store things mode)
  371. (match things
  372. ((drv)
  373. (and (string-suffix? "thingie.drv" drv)
  374. (not (port-closed?
  375. (store-connection-socket store)))
  376. (continue #t)))))
  377. (with-store store
  378. (let* ((b (add-text-to-store store "build" "echo $foo > $out" '()))
  379. (s (add-to-store store "bash" #t "sha256"
  380. (search-bootstrap-binary "bash"
  381. (%current-system))))
  382. (d (derivation store "thingie"
  383. s `("-e" ,b)
  384. #:env-vars `(("foo" . ,(random-text)))
  385. #:sources (list b s))))
  386. (build-derivations store (list d))
  387. ;; Here STORE's socket should still be open.
  388. (and (valid-path? store (derivation->output-path d))
  389. 'success)))))
  390. (test-assert "map/accumulate-builds"
  391. (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
  392. (s (add-to-store %store "bash" #t "sha256"
  393. (search-bootstrap-binary "bash"
  394. (%current-system))))
  395. (d1 (derivation %store "the-thing"
  396. s `("-e" ,b)
  397. #:env-vars `(("foo" . ,(random-text)))
  398. #:sources (list b s)))
  399. (d2 (derivation %store "the-thing"
  400. s `("-e" ,b)
  401. #:env-vars `(("foo" . ,(random-text))
  402. ("bar" . "baz"))
  403. #:sources (list b s))))
  404. (with-build-handler (lambda (continue store things mode)
  405. (equal? (map derivation-file-name (list d1 d2))
  406. things))
  407. (map/accumulate-builds %store
  408. (lambda (drv)
  409. (build-derivations %store (list drv))
  410. (add-to-store %store "content-addressed"
  411. #t "sha256"
  412. (derivation->output-path drv)))
  413. (list d1 d2)))))
  414. (test-equal "map/accumulate-builds cutoff" ;https://issues.guix.gnu.org/50264
  415. (iota 20)
  416. ;; Make sure that, when the cutoff is reached, 'map/accumulate-builds' still
  417. ;; returns the right result and calls the build handler by batches.
  418. (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
  419. (s (add-to-store %store "bash" #t "sha256"
  420. (search-bootstrap-binary "bash"
  421. (%current-system))))
  422. (d (map (lambda (i)
  423. (derivation %store (string-append "the-thing-"
  424. (number->string i))
  425. s `("-e" ,b)
  426. #:env-vars `(("foo" . ,(random-text)))
  427. #:sources (list b s)
  428. #:properties `((n . ,i))))
  429. (iota 20)))
  430. (calls '()))
  431. (define lst
  432. (with-build-handler (lambda (continue store things mode)
  433. (set! calls (cons things calls))
  434. (continue #f))
  435. (map/accumulate-builds %store
  436. (lambda (d)
  437. (build-derivations %store (list d))
  438. (assq-ref (derivation-properties d) 'n))
  439. d
  440. #:cutoff 7)))
  441. (match (reverse calls)
  442. (((batch1 ...) (batch2 ...) (batch3 ...))
  443. (and (equal? (map derivation-file-name (take d 8)) batch1)
  444. (equal? (map derivation-file-name (take (drop d 8) 8)) batch2)
  445. (equal? (map derivation-file-name (drop d 16)) batch3)
  446. lst)))))
  447. (test-equal "map/accumulate-builds and different store"
  448. '(d2) ;see <https://issues.guix.gnu.org/46756>
  449. (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
  450. (s (add-to-store %store "bash" #t "sha256"
  451. (search-bootstrap-binary "bash"
  452. (%current-system))))
  453. (d1 (derivation %store "first"
  454. s `("-e" ,b)
  455. #:env-vars `(("foo" . ,(random-text)))
  456. #:sources (list b s)))
  457. (d2 (derivation %store "second"
  458. s `("-e" ,b)
  459. #:env-vars `(("foo" . ,(random-text))
  460. ("bar" . "baz"))
  461. #:sources (list b s))))
  462. (with-store alternate-store
  463. (with-build-handler (lambda (continue store things mode)
  464. ;; If this handler is called, it means that
  465. ;; 'map/accumulate-builds' triggered a build,
  466. ;; which it shouldn't since the inner
  467. ;; 'build-derivations' call is for another store.
  468. 'failed)
  469. (map/accumulate-builds %store
  470. (lambda (drv)
  471. (build-derivations alternate-store (list d2))
  472. 'd2)
  473. (list d1))))))
  474. (test-assert "mapm/accumulate-builds"
  475. (let* ((d1 (run-with-store %store
  476. (gexp->derivation "foo" #~(mkdir #$output))))
  477. (d2 (run-with-store %store
  478. (gexp->derivation "bar" #~(mkdir #$output)))))
  479. (with-build-handler (lambda (continue store things mode)
  480. (equal? (map derivation-file-name (pk 'zz (list d1 d2)))
  481. (pk 'XX things)))
  482. (run-with-store %store
  483. (mapm/accumulate-builds built-derivations `((,d1) (,d2)))))))
  484. (test-equal "mapm/accumulate-builds, %current-target-system"
  485. (make-list 2 '("i586-pc-gnu" "i586-pc-gnu"))
  486. ;; Both the 'mapm' and 'mapm/accumulate-builds' procedures should see the
  487. ;; right #:target.
  488. (run-with-store %store
  489. (mlet %store-monad ((lst1 (mapm %store-monad
  490. (lambda _
  491. (current-target-system))
  492. '(a b)))
  493. (lst2 (mapm/accumulate-builds
  494. (lambda _
  495. (current-target-system))
  496. '(a b))))
  497. (return (list lst1 lst2)))
  498. #:system system
  499. #:target "i586-pc-gnu"))
  500. (test-assert "topologically-sorted, one item"
  501. (let* ((a (add-text-to-store %store "a" "a"))
  502. (b (add-text-to-store %store "b" "b" (list a)))
  503. (c (add-text-to-store %store "c" "c" (list b)))
  504. (d (add-text-to-store %store "d" "d" (list c)))
  505. (s (topologically-sorted %store (list d))))
  506. (equal? s (list a b c d))))
  507. (test-assert "topologically-sorted, several items"
  508. (let* ((a (add-text-to-store %store "a" "a"))
  509. (b (add-text-to-store %store "b" "b" (list a)))
  510. (c (add-text-to-store %store "c" "c" (list b)))
  511. (d (add-text-to-store %store "d" "d" (list c)))
  512. (s1 (topologically-sorted %store (list d a c b)))
  513. (s2 (topologically-sorted %store (list b d c a b d))))
  514. (equal? s1 s2 (list a b c d))))
  515. (test-assert "topologically-sorted, more difficult"
  516. (let* ((a (add-text-to-store %store "a" "a"))
  517. (b (add-text-to-store %store "b" "b" (list a)))
  518. (c (add-text-to-store %store "c" "c" (list b)))
  519. (d (add-text-to-store %store "d" "d" (list c)))
  520. (w (add-text-to-store %store "w" "w"))
  521. (x (add-text-to-store %store "x" "x" (list w)))
  522. (y (add-text-to-store %store "y" "y" (list x d)))
  523. (s1 (topologically-sorted %store (list y)))
  524. (s2 (topologically-sorted %store (list c y)))
  525. (s3 (topologically-sorted %store (cons y (references %store y)))))
  526. ;; The order in which 'references' returns the references of Y is
  527. ;; unspecified, so accommodate.
  528. (let* ((x-then-d? (equal? (references %store y) (list x d))))
  529. (and (equal? s1
  530. (if x-then-d?
  531. (list w x a b c d y)
  532. (list a b c d w x y)))
  533. (equal? s2
  534. (if x-then-d?
  535. (list a b c w x d y)
  536. (list a b c d w x y)))
  537. (lset= string=? s1 s3)))))
  538. (test-assert "current-build-output-port, UTF-8"
  539. ;; Are UTF-8 strings in the build log properly interpreted?
  540. (string-contains
  541. (with-fluids ((%default-port-encoding "UTF-8")) ;for the string port
  542. (call-with-output-string
  543. (lambda (port)
  544. (parameterize ((current-build-output-port port))
  545. (let* ((s "Here’s a Greek letter: λ.")
  546. (d (build-expression->derivation
  547. %store "foo" `(display ,s)
  548. #:guile-for-build
  549. (package-derivation %store %bootstrap-guile
  550. (%current-system)))))
  551. (guard (c ((store-protocol-error? c) #t))
  552. (build-derivations %store (list d))))))))
  553. "Here’s a Greek letter: λ."))
  554. (test-assert "current-build-output-port, UTF-8 + garbage"
  555. ;; What about a mixture of UTF-8 + garbage?
  556. (string-contains
  557. (with-fluids ((%default-port-encoding "UTF-8")) ;for the string port
  558. (call-with-output-string
  559. (lambda (port)
  560. (parameterize ((current-build-output-port port))
  561. (let ((d (build-expression->derivation
  562. %store "foo"
  563. `(begin
  564. (use-modules (rnrs io ports))
  565. (display "garbage: ")
  566. (put-bytevector (current-output-port) #vu8(128))
  567. (display "lambda: λ\n"))
  568. #:guile-for-build
  569. (package-derivation %store %bootstrap-guile))))
  570. (guard (c ((store-protocol-error? c) #t))
  571. (build-derivations %store (list d))))))))
  572. "garbage: �lambda: λ"))
  573. (test-assert "log-file, derivation"
  574. (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
  575. (s (add-to-store %store "bash" #t "sha256"
  576. (search-bootstrap-binary "bash"
  577. (%current-system))))
  578. (d (derivation %store "the-thing"
  579. s `("-e" ,b)
  580. #:env-vars `(("foo" . ,(random-text)))
  581. #:inputs `((,b) (,s)))))
  582. (and (build-derivations %store (list d))
  583. (file-exists? (pk (log-file %store (derivation-file-name d)))))))
  584. (test-assert "log-file, output file name"
  585. (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
  586. (s (add-to-store %store "bash" #t "sha256"
  587. (search-bootstrap-binary "bash"
  588. (%current-system))))
  589. (d (derivation %store "the-thing"
  590. s `("-e" ,b)
  591. #:env-vars `(("foo" . ,(random-text)))
  592. #:inputs `((,b) (,s))))
  593. (o (derivation->output-path d)))
  594. (and (build-derivations %store (list d))
  595. (file-exists? (pk (log-file %store o)))
  596. (string=? (log-file %store (derivation-file-name d))
  597. (log-file %store o)))))
  598. (test-assert "no substitutes"
  599. (with-store s
  600. (let* ((d1 (package-derivation s %bootstrap-guile (%current-system)))
  601. (d2 (package-derivation s %bootstrap-glibc (%current-system)))
  602. (o (map derivation->output-path (list d1 d2))))
  603. (set-build-options s #:use-substitutes? #f)
  604. (and (not (has-substitutes? s (derivation-file-name d1)))
  605. (not (has-substitutes? s (derivation-file-name d2)))
  606. (null? (substitutable-paths s o))
  607. (null? (substitutable-path-info s o))))))
  608. (test-assert "build-things with output path"
  609. (with-store s
  610. (let* ((c (random-text)) ;contents of the output
  611. (d (build-expression->derivation
  612. s "substitute-me"
  613. `(call-with-output-file %output
  614. (lambda (p)
  615. (display ,c p)))
  616. #:guile-for-build
  617. (package-derivation s %bootstrap-guile (%current-system))))
  618. (o (derivation->output-path d)))
  619. (set-build-options s #:use-substitutes? #f)
  620. ;; Pass 'build-things' the output file name, O. However, since there
  621. ;; are no substitutes for O, it will just do nothing.
  622. (build-things s (list o))
  623. (not (valid-path? s o)))))
  624. (test-skip (if (getenv "GUIX_BINARY_SUBSTITUTE_URL") 0 1))
  625. (test-assert "substitute query"
  626. (with-store s
  627. (let* ((d (package-derivation s %bootstrap-guile (%current-system)))
  628. (o (derivation->output-path d)))
  629. ;; Create fake substituter data, to be read by 'guix substitute'.
  630. (with-derivation-narinfo d
  631. ;; Remove entry from the local cache.
  632. (false-if-exception
  633. (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
  634. "/guix/substitute")))
  635. ;; Make sure 'guix substitute' correctly communicates the above
  636. ;; data.
  637. (set-build-options s #:use-substitutes? #t
  638. #:substitute-urls (%test-substitute-urls))
  639. (and (has-substitutes? s o)
  640. (equal? (list o) (substitutable-paths s (list o)))
  641. (match (pk 'spi (substitutable-path-info s (list o)))
  642. (((? substitutable? s))
  643. (and (string=? (substitutable-deriver s)
  644. (derivation-file-name d))
  645. (null? (substitutable-references s))
  646. (equal? (substitutable-nar-size s) 1234)))))))))
  647. (test-assert "substitute query, alternating URLs"
  648. (let* ((d (with-store s
  649. (package-derivation s %bootstrap-guile (%current-system))))
  650. (o (derivation->output-path d)))
  651. (with-derivation-narinfo d
  652. ;; Remove entry from the local cache.
  653. (false-if-exception
  654. (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
  655. "/guix/substitute")))
  656. ;; Note: We reconnect to the daemon to force a new instance of 'guix
  657. ;; substitute' to be used; otherwise the #:substitute-urls of
  658. ;; 'set-build-options' would have no effect.
  659. (and (with-store s ;the right substitute URL
  660. (set-build-options s #:use-substitutes? #t
  661. #:substitute-urls (%test-substitute-urls))
  662. (has-substitutes? s o))
  663. (with-store s ;the wrong one
  664. (set-build-options s #:use-substitutes? #t
  665. #:substitute-urls (list
  666. "http://does-not-exist"))
  667. (not (has-substitutes? s o)))
  668. (with-store s ;the right one again
  669. (set-build-options s #:use-substitutes? #t
  670. #:substitute-urls (%test-substitute-urls))
  671. (has-substitutes? s o))
  672. (with-store s ;empty list of URLs
  673. (set-build-options s #:use-substitutes? #t
  674. #:substitute-urls '())
  675. (not (has-substitutes? s o)))))))
  676. (test-assert "substitute"
  677. (with-store s
  678. (let* ((c (random-text)) ; contents of the output
  679. (d (build-expression->derivation
  680. s "substitute-me"
  681. `(call-with-output-file %output
  682. (lambda (p)
  683. (exit 1) ; would actually fail
  684. (display ,c p)))
  685. #:guile-for-build
  686. (package-derivation s %bootstrap-guile (%current-system))))
  687. (o (derivation->output-path d)))
  688. (with-derivation-substitute d c
  689. (set-build-options s #:use-substitutes? #t
  690. #:substitute-urls (%test-substitute-urls))
  691. (and (has-substitutes? s o)
  692. (build-derivations s (list d))
  693. (canonical-file? o)
  694. (equal? c (call-with-input-file o get-string-all)))))))
  695. (test-assert "substitute, deduplication"
  696. (with-store s
  697. ;; Note: C must be longer than %DEDUPLICATION-MINIMUM-SIZE.
  698. (let* ((c (string-concatenate
  699. (make-list 200 (random-text)))) ; contents of the output
  700. (g (package-derivation s %bootstrap-guile))
  701. (d1 (build-expression->derivation s "substitute-me"
  702. `(begin ,c (exit 1))
  703. #:guile-for-build g))
  704. (d2 (build-expression->derivation s "build-me"
  705. `(call-with-output-file %output
  706. (lambda (p)
  707. (display ,c p)))
  708. #:guile-for-build g))
  709. (o1 (derivation->output-path d1))
  710. (o2 (derivation->output-path d2)))
  711. (with-derivation-substitute d1 c
  712. (set-build-options s #:use-substitutes? #t
  713. #:substitute-urls (%test-substitute-urls))
  714. (and (has-substitutes? s o1)
  715. (build-derivations s (list d2)) ;build
  716. (build-derivations s (list d1)) ;substitute
  717. (canonical-file? o1)
  718. (equal? c (call-with-input-file o1 get-string-all))
  719. (= (stat:ino (stat o1)) (stat:ino (stat o2))))))))
  720. (test-assert "substitute + build-things with output path"
  721. (with-store s
  722. (let* ((c (random-text)) ;contents of the output
  723. (d (build-expression->derivation
  724. s "substitute-me"
  725. `(call-with-output-file %output
  726. (lambda (p)
  727. (exit 1) ;would actually fail
  728. (display ,c p)))
  729. #:guile-for-build
  730. (package-derivation s %bootstrap-guile (%current-system))))
  731. (o (derivation->output-path d)))
  732. (with-derivation-substitute d c
  733. (set-build-options s #:use-substitutes? #t
  734. #:substitute-urls (%test-substitute-urls))
  735. (and (has-substitutes? s o)
  736. (build-things s (list o)) ;give the output path
  737. (valid-path? s o)
  738. (canonical-file? o)
  739. (equal? c (call-with-input-file o get-string-all)))))))
  740. (test-assert "substitute + build-things with specific output"
  741. (with-store s
  742. (let* ((c (random-text)) ;contents of the output
  743. (d (build-expression->derivation
  744. s "substitute-me" `(begin ,c (exit 1)) ;would fail
  745. #:outputs '("out" "one" "two")
  746. #:guile-for-build
  747. (package-derivation s %bootstrap-guile (%current-system))))
  748. (o (derivation->output-path d)))
  749. (with-derivation-substitute d c
  750. (set-build-options s #:use-substitutes? #t
  751. #:substitute-urls (%test-substitute-urls))
  752. (and (has-substitutes? s o)
  753. ;; Ask for nothing but the "out" output of D.
  754. (build-things s `((,(derivation-file-name d) . "out")))
  755. (valid-path? s o)
  756. (canonical-file? o)
  757. (equal? c (call-with-input-file o get-string-all)))))))
  758. (test-assert "substitute, corrupt output hash"
  759. ;; Tweak the substituter into installing a substitute whose hash doesn't
  760. ;; match the one announced in the narinfo. The daemon must notice this and
  761. ;; raise an error.
  762. (with-store s
  763. (let* ((c "hello, world") ; contents of the output
  764. (d (build-expression->derivation
  765. s "corrupt-substitute"
  766. `(mkdir %output)
  767. #:guile-for-build
  768. (package-derivation s %bootstrap-guile (%current-system))))
  769. (o (derivation->output-path d)))
  770. (with-derivation-substitute d c
  771. (sha256 => (make-bytevector 32 0)) ;select a hash that doesn't match C
  772. ;; Make sure we use 'guix substitute'.
  773. (set-build-options s
  774. #:use-substitutes? #t
  775. #:fallback? #f
  776. #:substitute-urls (%test-substitute-urls))
  777. (and (has-substitutes? s o)
  778. (guard (c ((store-protocol-error? c)
  779. ;; XXX: the daemon writes "hash mismatch in downloaded
  780. ;; path", but the actual error returned to the client
  781. ;; doesn't mention that.
  782. (pk 'corrupt c)
  783. (not (zero? (store-protocol-error-status c)))))
  784. (build-derivations s (list d))
  785. #f))))))
  786. (test-assert "substitute, corrupt output hash, build trace"
  787. ;; Likewise, and check the build trace.
  788. (with-store s
  789. (let* ((c "hello, world") ; contents of the output
  790. (d (build-expression->derivation
  791. s "corrupt-substitute"
  792. `(mkdir %output)
  793. #:guile-for-build
  794. (package-derivation s %bootstrap-guile (%current-system))))
  795. (o (derivation->output-path d)))
  796. ;; Make sure we use 'guix substitute'.
  797. (set-build-options s
  798. #:print-build-trace #t
  799. #:use-substitutes? #t
  800. #:fallback? #f
  801. #:substitute-urls (%test-substitute-urls))
  802. (with-derivation-substitute d c
  803. (sha256 => (make-bytevector 32 0)) ;select a hash that doesn't match C
  804. (define output
  805. (call-with-output-string
  806. (lambda (port)
  807. (parameterize ((current-build-output-port port))
  808. (guard (c ((store-protocol-error? c) #t))
  809. (build-derivations s (list d))
  810. #f)))))
  811. (define actual-hash
  812. (let-values (((port get-hash)
  813. (gcrypt:open-hash-port
  814. (gcrypt:hash-algorithm gcrypt:sha256))))
  815. (write-file-tree "foo" port
  816. #:file-type+size
  817. (lambda _
  818. (values 'regular (string-length c)))
  819. #:file-port
  820. (lambda _
  821. (open-input-string c)))
  822. (close-port port)
  823. (bytevector->nix-base32-string (get-hash))))
  824. (define expected-hash
  825. (bytevector->nix-base32-string (make-bytevector 32 0)))
  826. (define mismatch
  827. (string-append "@ hash-mismatch " o " sha256 "
  828. expected-hash " " actual-hash "\n"))
  829. (define failure
  830. (string-append "@ substituter-failed " o))
  831. (and (string-contains output mismatch)
  832. (string-contains output failure))))))
  833. (test-assert "substitute --fallback"
  834. (with-store s
  835. (let* ((t (random-text)) ; contents of the output
  836. (d (build-expression->derivation
  837. s "substitute-me-not"
  838. `(call-with-output-file %output
  839. (lambda (p)
  840. (display ,t p)))
  841. #:guile-for-build
  842. (package-derivation s %bootstrap-guile (%current-system))))
  843. (o (derivation->output-path d)))
  844. ;; Create fake substituter data, to be read by 'guix substitute'.
  845. (with-derivation-narinfo d
  846. ;; Make sure we use 'guix substitute'.
  847. (set-build-options s #:use-substitutes? #t
  848. #:substitute-urls (%test-substitute-urls))
  849. (and (has-substitutes? s o)
  850. (guard (c ((store-protocol-error? c)
  851. ;; The substituter failed as expected. Now make
  852. ;; sure that #:fallback? #t works correctly.
  853. (set-build-options s
  854. #:use-substitutes? #t
  855. #:substitute-urls
  856. (%test-substitute-urls)
  857. #:fallback? #t)
  858. (and (build-derivations s (list d))
  859. (equal? t (call-with-input-file o
  860. get-string-all)))))
  861. ;; Should fail.
  862. (build-derivations s (list d))
  863. #f))))))
  864. (test-equal "substitute query and large size"
  865. (+ 100 (expt 2 63)) ;<https://issues.guix.gnu.org/51983>
  866. (with-store s
  867. (let* ((size (+ 100 (expt 2 63))) ;does not fit in signed 'long long'
  868. (item (string-append (%store-prefix)
  869. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-bad-size")))
  870. ;; Create fake substituter data, to be read by 'guix substitute'.
  871. (call-with-output-file (string-append (%substitute-directory)
  872. "/" (store-path-hash-part item)
  873. ".narinfo")
  874. (lambda (port)
  875. (format port "StorePath: ~a
  876. URL: http://example.org
  877. Compression: none
  878. NarSize: ~a
  879. NarHash: sha256:0fj9vhblff2997pi7qjj7lhmy7wzhnjwmkm2hmq6gr4fzmg10s0w
  880. References:
  881. System: x86_64-linux~%"
  882. item size)))
  883. ;; Remove entry from the local cache.
  884. (false-if-exception
  885. (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
  886. "/guix/substitute")))
  887. ;; Make sure 'guix substitute' correctly communicates the above
  888. ;; data.
  889. (set-build-options s #:use-substitutes? #t
  890. #:substitute-urls (%test-substitute-urls))
  891. (match (pk 'spi (substitutable-path-info s (list item)))
  892. (((? substitutable? s))
  893. (and (equal? (substitutable-path s) item)
  894. (substitutable-nar-size s)))))))
  895. (test-equal "substitute and large size"
  896. (+ 100 (expt 2 31)) ;<https://issues.guix.gnu.org/46212>
  897. (with-store s
  898. (let* ((size (+ 100 (expt 2 31))) ;does not fit in signed 'int'
  899. (item (string-append (%store-prefix)
  900. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-bad-size-"
  901. (random-text)))
  902. (nar (string-append (%substitute-directory) "/nar")))
  903. ;; Create a dummy nar to allow for substitution.
  904. (call-with-output-file nar
  905. (lambda (port)
  906. (write-file-tree (store-path-package-name item) port
  907. #:file-type+size (lambda _
  908. (values 'regular 12))
  909. #:file-port (lambda _
  910. (open-input-string "Hello world.")))))
  911. ;; Create fake substituter data, to be read by 'guix substitute'.
  912. (call-with-output-file (string-append (%substitute-directory)
  913. "/" (store-path-hash-part item)
  914. ".narinfo")
  915. (lambda (port)
  916. (format port "StorePath: ~a
  917. URL: file://~a
  918. Compression: none
  919. NarSize: ~a
  920. NarHash: sha256:~a
  921. References:
  922. System: x86_64-linux~%"
  923. item nar size
  924. (bytevector->nix-base32-string (gcrypt:file-sha256 nar)))))
  925. ;; Remove entry from the local cache.
  926. (false-if-exception
  927. (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
  928. "/guix/substitute")))
  929. ;; Make sure 'guix substitute' correctly communicates the above
  930. ;; data.
  931. (set-build-options s #:use-substitutes? #t
  932. #:substitute-urls (%test-substitute-urls))
  933. (ensure-path s item)
  934. (path-info-nar-size (query-path-info s item)))))
  935. (test-assert "export/import several paths"
  936. (let* ((texts (unfold (cut >= <> 10)
  937. (lambda _ (random-text))
  938. 1+
  939. 0))
  940. (files (map (cut add-text-to-store %store "text" <>) texts))
  941. (dump (call-with-bytevector-output-port
  942. (cut export-paths %store files <>))))
  943. (delete-paths %store files)
  944. (and (every (negate file-exists?) files)
  945. (let* ((source (open-bytevector-input-port dump))
  946. (imported (import-paths %store source)))
  947. (and (equal? imported files)
  948. (every file-exists? files)
  949. (equal? texts
  950. (map (lambda (file)
  951. (call-with-input-file file
  952. get-string-all))
  953. files)))))))
  954. (test-assert "export/import paths, ensure topological order"
  955. (let* ((file0 (add-text-to-store %store "baz" (random-text)))
  956. (file1 (add-text-to-store %store "foo" (random-text)
  957. (list file0)))
  958. (file2 (add-text-to-store %store "bar" (random-text)
  959. (list file1)))
  960. (files (list file1 file2))
  961. (dump1 (call-with-bytevector-output-port
  962. (cute export-paths %store (list file1 file2) <>)))
  963. (dump2 (call-with-bytevector-output-port
  964. (cute export-paths %store (list file2 file1) <>))))
  965. (delete-paths %store files)
  966. (and (every (negate file-exists?) files)
  967. (bytevector=? dump1 dump2)
  968. (let* ((source (open-bytevector-input-port dump1))
  969. (imported (import-paths %store source)))
  970. ;; DUMP1 should contain exactly FILE1 and FILE2, not FILE0.
  971. (and (equal? imported (list file1 file2))
  972. (every file-exists? files)
  973. (equal? (list file0) (references %store file1))
  974. (equal? (list file1) (references %store file2)))))))
  975. (test-assert "export/import incomplete"
  976. (let* ((file0 (add-text-to-store %store "baz" (random-text)))
  977. (file1 (add-text-to-store %store "foo" (random-text)
  978. (list file0)))
  979. (file2 (add-text-to-store %store "bar" (random-text)
  980. (list file1)))
  981. (dump (call-with-bytevector-output-port
  982. (cute export-paths %store (list file2) <>))))
  983. (delete-paths %store (list file0 file1 file2))
  984. (guard (c ((store-protocol-error? c)
  985. (and (not (zero? (store-protocol-error-status c)))
  986. (string-contains (store-protocol-error-message c)
  987. "not valid"))))
  988. ;; Here we get an exception because DUMP does not include FILE0 and
  989. ;; FILE1, which are dependencies of FILE2.
  990. (import-paths %store (open-bytevector-input-port dump)))))
  991. (test-assert "export/import recursive"
  992. (let* ((file0 (add-text-to-store %store "baz" (random-text)))
  993. (file1 (add-text-to-store %store "foo" (random-text)
  994. (list file0)))
  995. (file2 (add-text-to-store %store "bar" (random-text)
  996. (list file1)))
  997. (dump (call-with-bytevector-output-port
  998. (cute export-paths %store (list file2) <>
  999. #:recursive? #t))))
  1000. (delete-paths %store (list file0 file1 file2))
  1001. (let ((imported (import-paths %store (open-bytevector-input-port dump))))
  1002. (and (equal? imported (list file0 file1 file2))
  1003. (every file-exists? (list file0 file1 file2))
  1004. (equal? (list file0) (references %store file1))
  1005. (equal? (list file1) (references %store file2))))))
  1006. (test-assert "write-file & export-path yield the same result"
  1007. ;; Here we compare 'write-file' and the daemon's own implementation.
  1008. ;; 'write-file' is the reference because we know it sorts file
  1009. ;; deterministically. Conversely, the daemon uses 'readdir' and the entries
  1010. ;; currently happen to be sorted as a side-effect of some unrelated
  1011. ;; operation (search for 'unhacked' in archive.cc.) Make sure we detect any
  1012. ;; changes there.
  1013. (run-with-store %store
  1014. (mlet* %store-monad ((drv1 (package->derivation %bootstrap-guile))
  1015. (out1 -> (derivation->output-path drv1))
  1016. (data -> (unfold (cut >= <> 26)
  1017. (lambda (i)
  1018. (random-bytevector 128))
  1019. 1+ 0))
  1020. (build
  1021. -> #~(begin
  1022. (use-modules (rnrs io ports) (srfi srfi-1))
  1023. (let ()
  1024. (define letters
  1025. (map (lambda (i)
  1026. (string
  1027. (integer->char
  1028. (+ i (char->integer #\a)))))
  1029. (iota 26)))
  1030. (define (touch file data)
  1031. (call-with-output-file file
  1032. (lambda (port)
  1033. (put-bytevector port data))))
  1034. (mkdir #$output)
  1035. (chdir #$output)
  1036. ;; The files must be different so they have
  1037. ;; different inode numbers, and the inode
  1038. ;; order must differ from the lexicographic
  1039. ;; order.
  1040. (for-each touch
  1041. (append (drop letters 10)
  1042. (take letters 10))
  1043. (list #$@data))
  1044. #t)))
  1045. (drv2 (gexp->derivation "bunch" build))
  1046. (out2 -> (derivation->output-path drv2))
  1047. (item-info -> (store-lift query-path-info)))
  1048. (mbegin %store-monad
  1049. (built-derivations (list drv1 drv2))
  1050. (foldm %store-monad
  1051. (lambda (item result)
  1052. (define ref-hash
  1053. (let-values (((port get) (gcrypt:open-sha256-port)))
  1054. (write-file item port)
  1055. (close-port port)
  1056. (get)))
  1057. ;; 'query-path-info' returns a hash produced by using the
  1058. ;; daemon's C++ 'dump' function, which is the implementation
  1059. ;; under test.
  1060. (>>= (item-info item)
  1061. (lambda (info)
  1062. (return
  1063. (and result
  1064. (bytevector=? (path-info-hash info) ref-hash))))))
  1065. #t
  1066. (list out1 out2))))
  1067. #:guile-for-build (%guile-for-build)))
  1068. (test-assert "import not signed"
  1069. (let* ((text (random-text))
  1070. (file (add-file-tree-to-store %store
  1071. `("tree" directory
  1072. ("text" regular (data ,text))
  1073. ("link" symlink "text"))))
  1074. (dump (call-with-bytevector-output-port
  1075. (lambda (port)
  1076. (write-int 1 port) ;start
  1077. (write-file file port) ;contents
  1078. (write-int #x4558494e port) ;%export-magic
  1079. (write-string file port) ;store item
  1080. (write-string-list '() port) ;references
  1081. (write-string "" port) ;deriver
  1082. (write-int 0 port) ;not signed
  1083. (write-int 0 port))))) ;done
  1084. ;; Ensure 'import-paths' raises an exception.
  1085. (guard (c ((store-protocol-error? c)
  1086. (and (not (zero? (store-protocol-error-status c)))
  1087. (string-contains (store-protocol-error-message c)
  1088. "lacks a signature"))))
  1089. (let* ((source (open-bytevector-input-port dump))
  1090. (imported (import-paths %store source)))
  1091. (pk 'unsigned-imported imported)
  1092. #f))))
  1093. (test-assert "import signed by unauthorized key"
  1094. (let* ((text (random-text))
  1095. (file (add-file-tree-to-store %store
  1096. `("tree" directory
  1097. ("text" regular (data ,text))
  1098. ("link" symlink "text"))))
  1099. (key (gcrypt:generate-key
  1100. (gcrypt:string->canonical-sexp
  1101. "(genkey (ecdsa (curve Ed25519) (flags rfc6979)))")))
  1102. (dump (call-with-bytevector-output-port
  1103. (lambda (port)
  1104. (write-int 1 port) ;start
  1105. (write-file file port) ;contents
  1106. (write-int #x4558494e port) ;%export-magic
  1107. (write-string file port) ;store item
  1108. (write-string-list '() port) ;references
  1109. (write-string "" port) ;deriver
  1110. (write-int 1 port) ;signed
  1111. (write-string (gcrypt:canonical-sexp->string
  1112. (signature-sexp
  1113. (gcrypt:bytevector->hash-data
  1114. (gcrypt:sha256 #vu8(0 1 2))
  1115. #:key-type 'ecc)
  1116. (gcrypt:find-sexp-token key 'private-key)
  1117. (gcrypt:find-sexp-token key 'public-key)))
  1118. port)
  1119. (write-int 0 port))))) ;done
  1120. ;; Ensure 'import-paths' raises an exception.
  1121. (guard (c ((store-protocol-error? c)
  1122. (and (not (zero? (store-protocol-error-status c)))
  1123. (string-contains (store-protocol-error-message c)
  1124. "unauthorized public key"))))
  1125. (let* ((source (open-bytevector-input-port dump))
  1126. (imported (import-paths %store source)))
  1127. (pk 'unauthorized-imported imported)
  1128. #f))))
  1129. (test-assert "import corrupt path"
  1130. (let* ((text (random-text))
  1131. (file (add-text-to-store %store "text" text))
  1132. (dump (call-with-bytevector-output-port
  1133. (cut export-paths %store (list file) <>))))
  1134. (delete-paths %store (list file))
  1135. ;; Flip a bit in the stream's payload. INDEX here falls in the middle of
  1136. ;; the file contents in DUMP, regardless of the store prefix.
  1137. (let* ((index #x70)
  1138. (byte (bytevector-u8-ref dump index)))
  1139. (bytevector-u8-set! dump index (logxor #xff byte)))
  1140. (and (not (file-exists? file))
  1141. (guard (c ((store-protocol-error? c)
  1142. (pk 'c c)
  1143. (and (not (zero? (store-protocol-error-status c)))
  1144. (string-contains (store-protocol-error-message c)
  1145. "corrupt"))))
  1146. (let* ((source (open-bytevector-input-port dump))
  1147. (imported (import-paths %store source)))
  1148. (pk 'corrupt-imported imported)
  1149. #f)))))
  1150. (test-assert "verify-store"
  1151. (let* ((text (random-text))
  1152. (file1 (add-text-to-store %store "foo" text))
  1153. (file2 (add-text-to-store %store "bar" (random-text)
  1154. (list file1))))
  1155. (and (pk 'verify1 (verify-store %store)) ;hopefully OK ;
  1156. (begin
  1157. (delete-file file1)
  1158. (not (pk 'verify2 (verify-store %store)))) ;bad! ;
  1159. (begin
  1160. ;; Using 'add-text-to-store' here wouldn't work: It would succeed ;
  1161. ;; without actually creating the file. ;
  1162. (call-with-output-file file1
  1163. (lambda (port)
  1164. (display text port)))
  1165. (pk 'verify3 (verify-store %store)))))) ;OK again
  1166. (test-assert "verify-store + check-contents"
  1167. ;; XXX: This test is I/O intensive.
  1168. (with-store s
  1169. (let* ((text (random-text))
  1170. (drv (build-expression->derivation
  1171. s "corrupt"
  1172. `(let ((out (assoc-ref %outputs "out")))
  1173. (call-with-output-file out
  1174. (lambda (port)
  1175. (display ,text port)))
  1176. #t)
  1177. #:guile-for-build
  1178. (package-derivation s %bootstrap-guile (%current-system))))
  1179. (file (derivation->output-path drv)))
  1180. (with-derivation-substitute drv text
  1181. (and (build-derivations s (list drv))
  1182. (verify-store s #:check-contents? #t) ;should be OK
  1183. (begin
  1184. (chmod file #o644)
  1185. (call-with-output-file file
  1186. (lambda (port)
  1187. (display "corrupt!" port)))
  1188. #t)
  1189. ;; Make sure the corruption is detected. We don't test repairing
  1190. ;; because only "trusted" users are allowed to do it, but we
  1191. ;; don't expose that notion of trusted users that nix-daemon
  1192. ;; supports because it seems dubious and redundant with what the
  1193. ;; OS provides (in Nix "trusted" users have additional
  1194. ;; privileges, such as overriding the set of substitute URLs, but
  1195. ;; we instead want to allow anyone to modify them, provided
  1196. ;; substitutes are signed by a root-approved key.)
  1197. (not (verify-store s #:check-contents? #t))
  1198. ;; Delete the corrupt item to leave the store in a clean state.
  1199. (delete-paths s (list file)))))))
  1200. (test-assert "build-things, check mode"
  1201. (with-store store
  1202. (call-with-temporary-output-file
  1203. (lambda (entropy entropy-port)
  1204. (write (random-text) entropy-port)
  1205. (force-output entropy-port)
  1206. (let* ((drv (build-expression->derivation
  1207. store "non-deterministic"
  1208. `(begin
  1209. (use-modules (rnrs io ports))
  1210. (let ((out (assoc-ref %outputs "out")))
  1211. (call-with-output-file out
  1212. (lambda (port)
  1213. ;; Rely on the fact that tests do not use the
  1214. ;; chroot, and thus ENTROPY is readable.
  1215. (display (call-with-input-file ,entropy
  1216. get-string-all)
  1217. port)))
  1218. #t))
  1219. #:guile-for-build
  1220. (package-derivation store %bootstrap-guile (%current-system))))
  1221. (file (derivation->output-path drv)))
  1222. (and (build-things store (list (derivation-file-name drv)))
  1223. (begin
  1224. (write (random-text) entropy-port)
  1225. (force-output entropy-port)
  1226. (guard (c ((store-protocol-error? c)
  1227. (pk 'determinism-exception c)
  1228. (and (not (zero? (store-protocol-error-status c)))
  1229. (string-contains (store-protocol-error-message c)
  1230. "deterministic"))))
  1231. ;; This one will produce a different result. Since we're in
  1232. ;; 'check' mode, this must fail.
  1233. (build-things store (list (derivation-file-name drv))
  1234. (build-mode check))
  1235. #f))))))))
  1236. (test-assert "build-succeeded trace in check mode"
  1237. (string-contains
  1238. (call-with-output-string
  1239. (lambda (port)
  1240. (let ((d (build-expression->derivation
  1241. %store "foo" '(mkdir (assoc-ref %outputs "out"))
  1242. #:guile-for-build
  1243. (package-derivation %store %bootstrap-guile))))
  1244. (build-derivations %store (list d))
  1245. (parameterize ((current-build-output-port port))
  1246. (build-derivations %store (list d) (build-mode check))))))
  1247. "@ build-succeeded"))
  1248. (test-assert "build multiple times"
  1249. (with-store store
  1250. ;; Ask to build twice.
  1251. (set-build-options store #:rounds 2 #:use-substitutes? #f)
  1252. (call-with-temporary-output-file
  1253. (lambda (entropy entropy-port)
  1254. (write (random-text) entropy-port)
  1255. (force-output entropy-port)
  1256. (let* ((drv (build-expression->derivation
  1257. store "non-deterministic"
  1258. `(begin
  1259. (use-modules (rnrs io ports))
  1260. (let ((out (assoc-ref %outputs "out")))
  1261. (call-with-output-file out
  1262. (lambda (port)
  1263. ;; Rely on the fact that tests do not use the
  1264. ;; chroot, and thus ENTROPY is accessible.
  1265. (display (call-with-input-file ,entropy
  1266. get-string-all)
  1267. port)
  1268. (call-with-output-file ,entropy
  1269. (lambda (port)
  1270. (write 'foobar port)))))
  1271. #t))
  1272. #:guile-for-build
  1273. (package-derivation store %bootstrap-guile (%current-system))))
  1274. (file (derivation->output-path drv)))
  1275. (guard (c ((store-protocol-error? c)
  1276. (pk 'multiple-build c)
  1277. (and (not (zero? (store-protocol-error-status c)))
  1278. (string-contains (store-protocol-error-message c)
  1279. "deterministic"))))
  1280. ;; This one will produce a different result on the second run.
  1281. (current-build-output-port (current-error-port))
  1282. (build-things store (list (derivation-file-name drv)))
  1283. #f))))))
  1284. (test-equal "store-lower"
  1285. "Lowered."
  1286. (let* ((add (store-lower text-file))
  1287. (file (add %store "foo" "Lowered.")))
  1288. (call-with-input-file file get-string-all)))
  1289. (test-equal "current-system"
  1290. "bar"
  1291. (parameterize ((%current-system "frob"))
  1292. (run-with-store %store
  1293. (mbegin %store-monad
  1294. (set-current-system "bar")
  1295. (current-system))
  1296. #:system "foo")))
  1297. (test-assert "query-path-info"
  1298. (let* ((ref (add-text-to-store %store "ref" "foo"))
  1299. (item (add-text-to-store %store "item" "bar" (list ref)))
  1300. (info (query-path-info %store item)))
  1301. (and (equal? (path-info-references info) (list ref))
  1302. (equal? (path-info-hash info)
  1303. (gcrypt:sha256
  1304. (string->utf8
  1305. (call-with-output-string (cut write-file item <>))))))))
  1306. (test-assert "path-info-deriver"
  1307. (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
  1308. (s (add-to-store %store "bash" #t "sha256"
  1309. (search-bootstrap-binary "bash"
  1310. (%current-system))))
  1311. (d (derivation %store "the-thing"
  1312. s `("-e" ,b)
  1313. #:env-vars `(("foo" . ,(random-text)))
  1314. #:inputs `((,b) (,s))))
  1315. (o (derivation->output-path d)))
  1316. (and (build-derivations %store (list d))
  1317. (not (path-info-deriver (query-path-info %store b)))
  1318. (string=? (derivation-file-name d)
  1319. (path-info-deriver (query-path-info %store o))))))
  1320. (test-equal "build-cores"
  1321. (list 0 42)
  1322. (with-store store
  1323. (let* ((build (add-text-to-store store "build.sh"
  1324. "echo $NIX_BUILD_CORES > $out"))
  1325. (bash (add-to-store store "bash" #t "sha256"
  1326. (search-bootstrap-binary "bash"
  1327. (%current-system))))
  1328. (drv1 (derivation store "the-thing" bash
  1329. `("-e" ,build)
  1330. #:inputs `((,bash) (,build))
  1331. #:env-vars `(("x" . ,(random-text)))))
  1332. (drv2 (derivation store "the-thing" bash
  1333. `("-e" ,build)
  1334. #:inputs `((,bash) (,build))
  1335. #:env-vars `(("x" . ,(random-text))))))
  1336. (and (build-derivations store (list drv1))
  1337. (begin
  1338. (set-build-options store #:build-cores 42)
  1339. (build-derivations store (list drv2)))
  1340. (list (call-with-input-file (derivation->output-path drv1)
  1341. read)
  1342. (call-with-input-file (derivation->output-path drv2)
  1343. read))))))
  1344. (test-equal "multiplexed-build-output"
  1345. '("Hello from first." "Hello from second.")
  1346. (with-store store
  1347. (let* ((build (add-text-to-store store "build.sh"
  1348. "echo Hello from $NAME.; echo > $out"))
  1349. (bash (add-to-store store "bash" #t "sha256"
  1350. (search-bootstrap-binary "bash"
  1351. (%current-system))))
  1352. (drv1 (derivation store "one" bash
  1353. `("-e" ,build)
  1354. #:inputs `((,bash) (,build))
  1355. #:env-vars `(("NAME" . "first")
  1356. ("x" . ,(random-text)))))
  1357. (drv2 (derivation store "two" bash
  1358. `("-e" ,build)
  1359. #:inputs `((,bash) (,build))
  1360. #:env-vars `(("NAME" . "second")
  1361. ("x" . ,(random-text))))))
  1362. (set-build-options store
  1363. #:print-build-trace #t
  1364. #:multiplexed-build-output? #t
  1365. #:max-build-jobs 10)
  1366. (let ((port (open-output-string)))
  1367. ;; Send the build log to PORT.
  1368. (parameterize ((current-build-output-port port))
  1369. (build-derivations store (list drv1 drv2)))
  1370. ;; Retrieve the build log; make sure it contains valid "@ build-log"
  1371. ;; traces that allow us to retrieve each builder's output (we assume
  1372. ;; there's exactly one "build-output" trace for each builder, which is
  1373. ;; reasonable.)
  1374. (let* ((log (get-output-string port))
  1375. (started (fold-matches
  1376. (make-regexp "@ build-started ([^ ]+) - ([^ ]+) ([^ ]+) ([0-9]+)")
  1377. log '() cons))
  1378. (done (fold-matches
  1379. (make-regexp "@ build-succeeded (.*) - (.*) (.*) (.*)")
  1380. log '() cons))
  1381. (output (fold-matches
  1382. (make-regexp "@ build-log ([[:digit:]]+) ([[:digit:]]+)\n([A-Za-z .*]+)\n")
  1383. log '() cons))
  1384. (drv-pid (lambda (name)
  1385. (lambda (m)
  1386. (let ((drv (match:substring m 1))
  1387. (pid (string->number
  1388. (match:substring m 4))))
  1389. (and (string-suffix? name drv) pid)))))
  1390. (pid-log (lambda (pid)
  1391. (lambda (m)
  1392. (let ((n (string->number
  1393. (match:substring m 1)))
  1394. (len (string->number
  1395. (match:substring m 2)))
  1396. (str (match:substring m 3)))
  1397. (and (= pid n)
  1398. (= (string-length str) (- len 1))
  1399. str)))))
  1400. (pid1 (any (drv-pid "one.drv") started))
  1401. (pid2 (any (drv-pid "two.drv") started)))
  1402. (list (any (pid-log pid1) output)
  1403. (any (pid-log pid2) output)))))))
  1404. (test-end "store")