store.scm 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 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")))
  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 "permanent root"
  178. (let* ((p (with-store store
  179. (let ((p (add-text-to-store store "random-text"
  180. (random-text))))
  181. (add-permanent-root p)
  182. (add-permanent-root p) ; should not throw
  183. p))))
  184. (and (member p (live-paths %store))
  185. (begin
  186. (remove-permanent-root p)
  187. (->bool (member p (dead-paths %store)))))))
  188. (test-assert "dead path can be explicitly collected"
  189. (let ((p (add-text-to-store %store "random-text"
  190. (random-text) '())))
  191. (let-values (((paths freed) (delete-paths %store (list p))))
  192. (and (equal? paths (list p))
  193. ;; XXX: On some file systems (notably Btrfs), freed
  194. ;; may return 0. See <https://bugs.gnu.org/29363>.
  195. ;;(> freed 0)
  196. (not (file-exists? p))))))
  197. (test-assert "add-text-to-store/add-to-store vs. delete-paths"
  198. ;; Before, 'add-text-to-store' and 'add-to-store' would return the same
  199. ;; store item without noticing that it is no longer valid.
  200. (with-store store
  201. (let* ((text (random-text))
  202. (file (search-path %load-path "guix.scm"))
  203. (path1 (add-text-to-store store "delete-me" text))
  204. (path2 (add-to-store store "delete-me" #t "sha256" file))
  205. (deleted (delete-paths store (list path1 path2))))
  206. (and (string=? path1 (add-text-to-store store "delete-me" text))
  207. (string=? path2 (add-to-store store "delete-me" #t "sha256" file))
  208. (lset= string=? deleted (list path1 path2))
  209. (valid-path? store path1)
  210. (valid-path? store path2)
  211. (file-exists? path1)
  212. (file-exists? path2)))))
  213. (test-equal "add-file-tree-to-store"
  214. `(42
  215. ("." directory #t)
  216. ("./bar" directory #t)
  217. ("./foo" directory #t)
  218. ("./foo/a" regular "file a")
  219. ("./foo/b" symlink "a")
  220. ("./foo/c" directory #t)
  221. ("./foo/c/p" regular "file p")
  222. ("./foo/c/q" directory #t)
  223. ("./foo/c/q/x" regular
  224. ,(string-append "#!" %shell "\nexit 42"))
  225. ("./foo/c/q/y" symlink "..")
  226. ("./foo/c/q/z" directory #t))
  227. (let* ((tree `("file-tree" directory
  228. ("foo" directory
  229. ("a" regular (data "file a"))
  230. ("b" symlink "a")
  231. ("c" directory
  232. ("p" regular (data ,(string->utf8 "file p")))
  233. ("q" directory
  234. ("x" executable
  235. (data ,(string-append "#!" %shell "\nexit 42")))
  236. ("y" symlink "..")
  237. ("z" directory))))
  238. ("bar" directory)))
  239. (result (add-file-tree-to-store %store tree)))
  240. (cons (status:exit-val (system* (string-append result "/foo/c/q/x")))
  241. (with-directory-excursion result
  242. (map (lambda (file)
  243. (let ((type (stat:type (lstat file))))
  244. `(,file ,type
  245. ,(match type
  246. ((or 'regular 'executable)
  247. (call-with-input-file file
  248. get-string-all))
  249. ('symlink (readlink file))
  250. ('directory #t)))))
  251. (find-files "." #:directories? #t))))))
  252. (test-equal "add-file-tree-to-store, flat"
  253. "Hello, world!"
  254. (let* ((tree `("flat-file" regular (data "Hello, world!")))
  255. (result (add-file-tree-to-store %store tree)))
  256. (and (file-exists? result)
  257. (call-with-input-file result get-string-all))))
  258. (test-assert "references"
  259. (let* ((t1 (add-text-to-store %store "random1"
  260. (random-text)))
  261. (t2 (add-text-to-store %store "random2"
  262. (random-text) (list t1))))
  263. (and (equal? (list t1) (references %store t2))
  264. (equal? (list t2) (referrers %store t1))
  265. (null? (references %store t1))
  266. (null? (referrers %store t2)))))
  267. (test-assert "references/substitutes missing reference info"
  268. (with-store s
  269. (set-build-options s #:use-substitutes? #f)
  270. (guard (c ((store-protocol-error? c) #t))
  271. (let* ((b (add-to-store s "bash" #t "sha256"
  272. (search-bootstrap-binary "bash"
  273. (%current-system))))
  274. (d (derivation s "the-thing" b '("--help")
  275. #:inputs `((,b)))))
  276. (references/substitutes s (list (derivation->output-path d) b))
  277. #f))))
  278. (test-assert "references/substitutes with substitute info"
  279. (with-store s
  280. (set-build-options s #:use-substitutes? #t)
  281. (let* ((t1 (add-text-to-store s "random1" (random-text)))
  282. (t2 (add-text-to-store s "random2" (random-text)
  283. (list t1)))
  284. (t3 (add-text-to-store s "build" "echo -n $t2 > $out"))
  285. (b (add-to-store s "bash" #t "sha256"
  286. (search-bootstrap-binary "bash"
  287. (%current-system))))
  288. (d (derivation s "the-thing" b `("-e" ,t3)
  289. #:inputs `((,b) (,t3) (,t2))
  290. #:env-vars `(("t2" . ,t2))))
  291. (o (derivation->output-path d)))
  292. (with-derivation-narinfo d
  293. (sha256 => (gcrypt:sha256 (string->utf8 t2)))
  294. (references => (list t2))
  295. (equal? (references/substitutes s (list o t3 t2 t1))
  296. `((,t2) ;refs of O
  297. () ;refs of T3
  298. (,t1) ;refs of T2
  299. ())))))) ;refs of T1
  300. (test-equal "substitutable-path-info when substitutes are turned off"
  301. '()
  302. (with-store s
  303. (set-build-options s #:use-substitutes? #f)
  304. (let* ((b (add-to-store s "bash" #t "sha256"
  305. (search-bootstrap-binary "bash"
  306. (%current-system))))
  307. (d (derivation s "the-thing" b '("--version")
  308. #:inputs `((,b))))
  309. (o (derivation->output-path d)))
  310. (with-derivation-narinfo d
  311. (substitutable-path-info s (list o))))))
  312. (test-equal "substitutable-paths when substitutes are turned off"
  313. '()
  314. (with-store s
  315. (set-build-options s #:use-substitutes? #f)
  316. (let* ((b (add-to-store s "bash" #t "sha256"
  317. (search-bootstrap-binary "bash"
  318. (%current-system))))
  319. (d (derivation s "the-thing" b '("--version")
  320. #:inputs `((,b))))
  321. (o (derivation->output-path d)))
  322. (with-derivation-narinfo d
  323. (substitutable-paths s (list o))))))
  324. (test-assert "requisites"
  325. (let* ((t1 (add-text-to-store %store "random1"
  326. (random-text) '()))
  327. (t2 (add-text-to-store %store "random2"
  328. (random-text) (list t1)))
  329. (t3 (add-text-to-store %store "random3"
  330. (random-text) (list t2)))
  331. (t4 (add-text-to-store %store "random4"
  332. (random-text) (list t1 t3))))
  333. (define (same? x y)
  334. (and (= (length x) (length y))
  335. (lset= equal? x y)))
  336. (and (same? (requisites %store (list t1)) (list t1))
  337. (same? (requisites %store (list t2)) (list t1 t2))
  338. (same? (requisites %store (list t3)) (list t1 t2 t3))
  339. (same? (requisites %store (list t4)) (list t1 t2 t3 t4))
  340. (same? (requisites %store (list t1 t2 t3 t4))
  341. (list t1 t2 t3 t4)))))
  342. (test-assert "derivers"
  343. (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
  344. (s (add-to-store %store "bash" #t "sha256"
  345. (search-bootstrap-binary "bash"
  346. (%current-system))))
  347. (d (derivation %store "the-thing"
  348. s `("-e" ,b)
  349. #:env-vars `(("foo" . ,(random-text)))
  350. #:inputs `((,b) (,s))))
  351. (o (derivation->output-path d)))
  352. (and (build-derivations %store (list d))
  353. (equal? (query-derivation-outputs %store (derivation-file-name d))
  354. (list o))
  355. (equal? (valid-derivers %store o)
  356. (list (derivation-file-name d))))))
  357. (test-equal "with-build-handler"
  358. 'success
  359. (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
  360. (s (add-to-store %store "bash" #t "sha256"
  361. (search-bootstrap-binary "bash"
  362. (%current-system))))
  363. (d1 (derivation %store "the-thing"
  364. s `("-e" ,b)
  365. #:env-vars `(("foo" . ,(random-text)))
  366. #:sources (list b s)))
  367. (d2 (derivation %store "the-thing"
  368. s `("-e" ,b)
  369. #:env-vars `(("foo" . ,(random-text))
  370. ("bar" . "baz"))
  371. #:sources (list b s)))
  372. (o1 (derivation->output-path d1))
  373. (o2 (derivation->output-path d2)))
  374. (with-build-handler
  375. (let ((counter 0))
  376. (lambda (continue store things mode)
  377. (match things
  378. ((drv)
  379. (set! counter (+ 1 counter))
  380. (if (string=? drv (derivation-file-name d1))
  381. (continue #t)
  382. (and (string=? drv (derivation-file-name d2))
  383. (= counter 2)
  384. 'success))))))
  385. (build-derivations %store (list d1))
  386. (build-derivations %store (list d2))
  387. 'fail)))
  388. (test-equal "with-build-handler + with-store"
  389. 'success
  390. ;; Check that STORE remains valid when the build handler invokes CONTINUE,
  391. ;; even though 'with-build-handler' is outside the dynamic extent of
  392. ;; 'with-store'.
  393. (with-build-handler (lambda (continue store things mode)
  394. (match things
  395. ((drv)
  396. (and (string-suffix? "thingie.drv" drv)
  397. (not (port-closed?
  398. (store-connection-socket store)))
  399. (continue #t)))))
  400. (with-store store
  401. (let* ((b (add-text-to-store store "build" "echo $foo > $out" '()))
  402. (s (add-to-store store "bash" #t "sha256"
  403. (search-bootstrap-binary "bash"
  404. (%current-system))))
  405. (d (derivation store "thingie"
  406. s `("-e" ,b)
  407. #:env-vars `(("foo" . ,(random-text)))
  408. #:sources (list b s))))
  409. (build-derivations store (list d))
  410. ;; Here STORE's socket should still be open.
  411. (and (valid-path? store (derivation->output-path d))
  412. 'success)))))
  413. (test-assert "map/accumulate-builds"
  414. (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
  415. (s (add-to-store %store "bash" #t "sha256"
  416. (search-bootstrap-binary "bash"
  417. (%current-system))))
  418. (d1 (derivation %store "the-thing"
  419. s `("-e" ,b)
  420. #:env-vars `(("foo" . ,(random-text)))
  421. #:sources (list b s)))
  422. (d2 (derivation %store "the-thing"
  423. s `("-e" ,b)
  424. #:env-vars `(("foo" . ,(random-text))
  425. ("bar" . "baz"))
  426. #:sources (list b s))))
  427. (with-build-handler (lambda (continue store things mode)
  428. (equal? (map derivation-file-name (list d1 d2))
  429. things))
  430. (map/accumulate-builds %store
  431. (lambda (drv)
  432. (build-derivations %store (list drv))
  433. (add-to-store %store "content-addressed"
  434. #t "sha256"
  435. (derivation->output-path drv)))
  436. (list d1 d2)))))
  437. (test-assert "mapm/accumulate-builds"
  438. (let* ((d1 (run-with-store %store
  439. (gexp->derivation "foo" #~(mkdir #$output))))
  440. (d2 (run-with-store %store
  441. (gexp->derivation "bar" #~(mkdir #$output)))))
  442. (with-build-handler (lambda (continue store things mode)
  443. (equal? (map derivation-file-name (pk 'zz (list d1 d2)))
  444. (pk 'XX things)))
  445. (run-with-store %store
  446. (mapm/accumulate-builds built-derivations `((,d1) (,d2)))))))
  447. (test-equal "mapm/accumulate-builds, %current-target-system"
  448. (make-list 2 '("i586-pc-gnu" "i586-pc-gnu"))
  449. ;; Both the 'mapm' and 'mapm/accumulate-builds' procedures should see the
  450. ;; right #:target.
  451. (run-with-store %store
  452. (mlet %store-monad ((lst1 (mapm %store-monad
  453. (lambda _
  454. (current-target-system))
  455. '(a b)))
  456. (lst2 (mapm/accumulate-builds
  457. (lambda _
  458. (current-target-system))
  459. '(a b))))
  460. (return (list lst1 lst2)))
  461. #:system system
  462. #:target "i586-pc-gnu"))
  463. (test-assert "topologically-sorted, one item"
  464. (let* ((a (add-text-to-store %store "a" "a"))
  465. (b (add-text-to-store %store "b" "b" (list a)))
  466. (c (add-text-to-store %store "c" "c" (list b)))
  467. (d (add-text-to-store %store "d" "d" (list c)))
  468. (s (topologically-sorted %store (list d))))
  469. (equal? s (list a b c d))))
  470. (test-assert "topologically-sorted, several items"
  471. (let* ((a (add-text-to-store %store "a" "a"))
  472. (b (add-text-to-store %store "b" "b" (list a)))
  473. (c (add-text-to-store %store "c" "c" (list b)))
  474. (d (add-text-to-store %store "d" "d" (list c)))
  475. (s1 (topologically-sorted %store (list d a c b)))
  476. (s2 (topologically-sorted %store (list b d c a b d))))
  477. (equal? s1 s2 (list a b c d))))
  478. (test-assert "topologically-sorted, more difficult"
  479. (let* ((a (add-text-to-store %store "a" "a"))
  480. (b (add-text-to-store %store "b" "b" (list a)))
  481. (c (add-text-to-store %store "c" "c" (list b)))
  482. (d (add-text-to-store %store "d" "d" (list c)))
  483. (w (add-text-to-store %store "w" "w"))
  484. (x (add-text-to-store %store "x" "x" (list w)))
  485. (y (add-text-to-store %store "y" "y" (list x d)))
  486. (s1 (topologically-sorted %store (list y)))
  487. (s2 (topologically-sorted %store (list c y)))
  488. (s3 (topologically-sorted %store (cons y (references %store y)))))
  489. ;; The order in which 'references' returns the references of Y is
  490. ;; unspecified, so accommodate.
  491. (let* ((x-then-d? (equal? (references %store y) (list x d))))
  492. (and (equal? s1
  493. (if x-then-d?
  494. (list w x a b c d y)
  495. (list a b c d w x y)))
  496. (equal? s2
  497. (if x-then-d?
  498. (list a b c w x d y)
  499. (list a b c d w x y)))
  500. (lset= string=? s1 s3)))))
  501. (test-assert "current-build-output-port, UTF-8"
  502. ;; Are UTF-8 strings in the build log properly interpreted?
  503. (string-contains
  504. (with-fluids ((%default-port-encoding "UTF-8")) ;for the string port
  505. (call-with-output-string
  506. (lambda (port)
  507. (parameterize ((current-build-output-port port))
  508. (let* ((s "Here’s a Greek letter: λ.")
  509. (d (build-expression->derivation
  510. %store "foo" `(display ,s)
  511. #:guile-for-build
  512. (package-derivation s %bootstrap-guile (%current-system)))))
  513. (guard (c ((store-protocol-error? c) #t))
  514. (build-derivations %store (list d))))))))
  515. "Here’s a Greek letter: λ."))
  516. (test-assert "current-build-output-port, UTF-8 + garbage"
  517. ;; What about a mixture of UTF-8 + garbage?
  518. (string-contains
  519. (with-fluids ((%default-port-encoding "UTF-8")) ;for the string port
  520. (call-with-output-string
  521. (lambda (port)
  522. (parameterize ((current-build-output-port port))
  523. (let ((d (build-expression->derivation
  524. %store "foo"
  525. `(begin
  526. (use-modules (rnrs io ports))
  527. (display "garbage: ")
  528. (put-bytevector (current-output-port) #vu8(128))
  529. (display "lambda: λ\n"))
  530. #:guile-for-build
  531. (package-derivation %store %bootstrap-guile))))
  532. (guard (c ((store-protocol-error? c) #t))
  533. (build-derivations %store (list d))))))))
  534. "garbage: �lambda: λ"))
  535. (test-assert "log-file, derivation"
  536. (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
  537. (s (add-to-store %store "bash" #t "sha256"
  538. (search-bootstrap-binary "bash"
  539. (%current-system))))
  540. (d (derivation %store "the-thing"
  541. s `("-e" ,b)
  542. #:env-vars `(("foo" . ,(random-text)))
  543. #:inputs `((,b) (,s)))))
  544. (and (build-derivations %store (list d))
  545. (file-exists? (pk (log-file %store (derivation-file-name d)))))))
  546. (test-assert "log-file, output file name"
  547. (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
  548. (s (add-to-store %store "bash" #t "sha256"
  549. (search-bootstrap-binary "bash"
  550. (%current-system))))
  551. (d (derivation %store "the-thing"
  552. s `("-e" ,b)
  553. #:env-vars `(("foo" . ,(random-text)))
  554. #:inputs `((,b) (,s))))
  555. (o (derivation->output-path d)))
  556. (and (build-derivations %store (list d))
  557. (file-exists? (pk (log-file %store o)))
  558. (string=? (log-file %store (derivation-file-name d))
  559. (log-file %store o)))))
  560. (test-assert "no substitutes"
  561. (with-store s
  562. (let* ((d1 (package-derivation s %bootstrap-guile (%current-system)))
  563. (d2 (package-derivation s %bootstrap-glibc (%current-system)))
  564. (o (map derivation->output-path (list d1 d2))))
  565. (set-build-options s #:use-substitutes? #f)
  566. (and (not (has-substitutes? s (derivation-file-name d1)))
  567. (not (has-substitutes? s (derivation-file-name d2)))
  568. (null? (substitutable-paths s o))
  569. (null? (substitutable-path-info s o))))))
  570. (test-assert "build-things with output path"
  571. (with-store s
  572. (let* ((c (random-text)) ;contents of the output
  573. (d (build-expression->derivation
  574. s "substitute-me"
  575. `(call-with-output-file %output
  576. (lambda (p)
  577. (display ,c p)))
  578. #:guile-for-build
  579. (package-derivation s %bootstrap-guile (%current-system))))
  580. (o (derivation->output-path d)))
  581. (set-build-options s #:use-substitutes? #f)
  582. ;; Pass 'build-things' the output file name, O. However, since there
  583. ;; are no substitutes for O, it will just do nothing.
  584. (build-things s (list o))
  585. (not (valid-path? s o)))))
  586. (test-skip (if (getenv "GUIX_BINARY_SUBSTITUTE_URL") 0 1))
  587. (test-assert "substitute query"
  588. (with-store s
  589. (let* ((d (package-derivation s %bootstrap-guile (%current-system)))
  590. (o (derivation->output-path d)))
  591. ;; Create fake substituter data, to be read by 'guix substitute'.
  592. (with-derivation-narinfo d
  593. ;; Remove entry from the local cache.
  594. (false-if-exception
  595. (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
  596. "/guix/substitute")))
  597. ;; Make sure 'guix substitute' correctly communicates the above
  598. ;; data.
  599. (set-build-options s #:use-substitutes? #t
  600. #:substitute-urls (%test-substitute-urls))
  601. (and (has-substitutes? s o)
  602. (equal? (list o) (substitutable-paths s (list o)))
  603. (match (pk 'spi (substitutable-path-info s (list o)))
  604. (((? substitutable? s))
  605. (and (string=? (substitutable-deriver s)
  606. (derivation-file-name d))
  607. (null? (substitutable-references s))
  608. (equal? (substitutable-nar-size s) 1234)))))))))
  609. (test-assert "substitute query, alternating URLs"
  610. (let* ((d (with-store s
  611. (package-derivation s %bootstrap-guile (%current-system))))
  612. (o (derivation->output-path d)))
  613. (with-derivation-narinfo d
  614. ;; Remove entry from the local cache.
  615. (false-if-exception
  616. (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
  617. "/guix/substitute")))
  618. ;; Note: We reconnect to the daemon to force a new instance of 'guix
  619. ;; substitute' to be used; otherwise the #:substitute-urls of
  620. ;; 'set-build-options' would have no effect.
  621. (and (with-store s ;the right substitute URL
  622. (set-build-options s #:use-substitutes? #t
  623. #:substitute-urls (%test-substitute-urls))
  624. (has-substitutes? s o))
  625. (with-store s ;the wrong one
  626. (set-build-options s #:use-substitutes? #t
  627. #:substitute-urls (list
  628. "http://does-not-exist"))
  629. (not (has-substitutes? s o)))
  630. (with-store s ;the right one again
  631. (set-build-options s #:use-substitutes? #t
  632. #:substitute-urls (%test-substitute-urls))
  633. (has-substitutes? s o))
  634. (with-store s ;empty list of URLs
  635. (set-build-options s #:use-substitutes? #t
  636. #:substitute-urls '())
  637. (not (has-substitutes? s o)))))))
  638. (test-assert "substitute"
  639. (with-store s
  640. (let* ((c (random-text)) ; contents of the output
  641. (d (build-expression->derivation
  642. s "substitute-me"
  643. `(call-with-output-file %output
  644. (lambda (p)
  645. (exit 1) ; would actually fail
  646. (display ,c p)))
  647. #:guile-for-build
  648. (package-derivation s %bootstrap-guile (%current-system))))
  649. (o (derivation->output-path d)))
  650. (with-derivation-substitute d c
  651. (set-build-options s #:use-substitutes? #t
  652. #:substitute-urls (%test-substitute-urls))
  653. (and (has-substitutes? s o)
  654. (build-derivations s (list d))
  655. (equal? c (call-with-input-file o get-string-all)))))))
  656. (test-assert "substitute + build-things with output path"
  657. (with-store s
  658. (let* ((c (random-text)) ;contents of the output
  659. (d (build-expression->derivation
  660. s "substitute-me"
  661. `(call-with-output-file %output
  662. (lambda (p)
  663. (exit 1) ;would actually fail
  664. (display ,c p)))
  665. #:guile-for-build
  666. (package-derivation s %bootstrap-guile (%current-system))))
  667. (o (derivation->output-path d)))
  668. (with-derivation-substitute d c
  669. (set-build-options s #:use-substitutes? #t
  670. #:substitute-urls (%test-substitute-urls))
  671. (and (has-substitutes? s o)
  672. (build-things s (list o)) ;give the output path
  673. (valid-path? s o)
  674. (equal? c (call-with-input-file o get-string-all)))))))
  675. (test-assert "substitute + build-things with specific output"
  676. (with-store s
  677. (let* ((c (random-text)) ;contents of the output
  678. (d (build-expression->derivation
  679. s "substitute-me" `(begin ,c (exit 1)) ;would fail
  680. #:outputs '("out" "one" "two")
  681. #:guile-for-build
  682. (package-derivation s %bootstrap-guile (%current-system))))
  683. (o (derivation->output-path d)))
  684. (with-derivation-substitute d c
  685. (set-build-options s #:use-substitutes? #t
  686. #:substitute-urls (%test-substitute-urls))
  687. (and (has-substitutes? s o)
  688. ;; Ask for nothing but the "out" output of D.
  689. (build-things s `((,(derivation-file-name d) . "out")))
  690. (valid-path? s o)
  691. (equal? c (call-with-input-file o get-string-all)))))))
  692. (test-assert "substitute, corrupt output hash"
  693. ;; Tweak the substituter into installing a substitute whose hash doesn't
  694. ;; match the one announced in the narinfo. The daemon must notice this and
  695. ;; raise an error.
  696. (with-store s
  697. (let* ((c "hello, world") ; contents of the output
  698. (d (build-expression->derivation
  699. s "corrupt-substitute"
  700. `(mkdir %output)
  701. #:guile-for-build
  702. (package-derivation s %bootstrap-guile (%current-system))))
  703. (o (derivation->output-path d)))
  704. (with-derivation-substitute d c
  705. (sha256 => (make-bytevector 32 0)) ;select a hash that doesn't match C
  706. ;; Make sure we use 'guix substitute'.
  707. (set-build-options s
  708. #:use-substitutes? #t
  709. #:fallback? #f
  710. #:substitute-urls (%test-substitute-urls))
  711. (and (has-substitutes? s o)
  712. (guard (c ((store-protocol-error? c)
  713. ;; XXX: the daemon writes "hash mismatch in downloaded
  714. ;; path", but the actual error returned to the client
  715. ;; doesn't mention that.
  716. (pk 'corrupt c)
  717. (not (zero? (store-protocol-error-status c)))))
  718. (build-derivations s (list d))
  719. #f))))))
  720. (test-assert "substitute --fallback"
  721. (with-store s
  722. (let* ((t (random-text)) ; contents of the output
  723. (d (build-expression->derivation
  724. s "substitute-me-not"
  725. `(call-with-output-file %output
  726. (lambda (p)
  727. (display ,t p)))
  728. #:guile-for-build
  729. (package-derivation s %bootstrap-guile (%current-system))))
  730. (o (derivation->output-path d)))
  731. ;; Create fake substituter data, to be read by 'guix substitute'.
  732. (with-derivation-narinfo d
  733. ;; Make sure we use 'guix substitute'.
  734. (set-build-options s #:use-substitutes? #t
  735. #:substitute-urls (%test-substitute-urls))
  736. (and (has-substitutes? s o)
  737. (guard (c ((store-protocol-error? c)
  738. ;; The substituter failed as expected. Now make
  739. ;; sure that #:fallback? #t works correctly.
  740. (set-build-options s
  741. #:use-substitutes? #t
  742. #:substitute-urls
  743. (%test-substitute-urls)
  744. #:fallback? #t)
  745. (and (build-derivations s (list d))
  746. (equal? t (call-with-input-file o
  747. get-string-all)))))
  748. ;; Should fail.
  749. (build-derivations s (list d))
  750. #f))))))
  751. (test-assert "export/import several paths"
  752. (let* ((texts (unfold (cut >= <> 10)
  753. (lambda _ (random-text))
  754. 1+
  755. 0))
  756. (files (map (cut add-text-to-store %store "text" <>) texts))
  757. (dump (call-with-bytevector-output-port
  758. (cut export-paths %store files <>))))
  759. (delete-paths %store files)
  760. (and (every (negate file-exists?) files)
  761. (let* ((source (open-bytevector-input-port dump))
  762. (imported (import-paths %store source)))
  763. (and (equal? imported files)
  764. (every file-exists? files)
  765. (equal? texts
  766. (map (lambda (file)
  767. (call-with-input-file file
  768. get-string-all))
  769. files)))))))
  770. (test-assert "export/import paths, ensure topological order"
  771. (let* ((file0 (add-text-to-store %store "baz" (random-text)))
  772. (file1 (add-text-to-store %store "foo" (random-text)
  773. (list file0)))
  774. (file2 (add-text-to-store %store "bar" (random-text)
  775. (list file1)))
  776. (files (list file1 file2))
  777. (dump1 (call-with-bytevector-output-port
  778. (cute export-paths %store (list file1 file2) <>)))
  779. (dump2 (call-with-bytevector-output-port
  780. (cute export-paths %store (list file2 file1) <>))))
  781. (delete-paths %store files)
  782. (and (every (negate file-exists?) files)
  783. (bytevector=? dump1 dump2)
  784. (let* ((source (open-bytevector-input-port dump1))
  785. (imported (import-paths %store source)))
  786. ;; DUMP1 should contain exactly FILE1 and FILE2, not FILE0.
  787. (and (equal? imported (list file1 file2))
  788. (every file-exists? files)
  789. (equal? (list file0) (references %store file1))
  790. (equal? (list file1) (references %store file2)))))))
  791. (test-assert "export/import incomplete"
  792. (let* ((file0 (add-text-to-store %store "baz" (random-text)))
  793. (file1 (add-text-to-store %store "foo" (random-text)
  794. (list file0)))
  795. (file2 (add-text-to-store %store "bar" (random-text)
  796. (list file1)))
  797. (dump (call-with-bytevector-output-port
  798. (cute export-paths %store (list file2) <>))))
  799. (delete-paths %store (list file0 file1 file2))
  800. (guard (c ((store-protocol-error? c)
  801. (and (not (zero? (store-protocol-error-status c)))
  802. (string-contains (store-protocol-error-message c)
  803. "not valid"))))
  804. ;; Here we get an exception because DUMP does not include FILE0 and
  805. ;; FILE1, which are dependencies of FILE2.
  806. (import-paths %store (open-bytevector-input-port dump)))))
  807. (test-assert "export/import recursive"
  808. (let* ((file0 (add-text-to-store %store "baz" (random-text)))
  809. (file1 (add-text-to-store %store "foo" (random-text)
  810. (list file0)))
  811. (file2 (add-text-to-store %store "bar" (random-text)
  812. (list file1)))
  813. (dump (call-with-bytevector-output-port
  814. (cute export-paths %store (list file2) <>
  815. #:recursive? #t))))
  816. (delete-paths %store (list file0 file1 file2))
  817. (let ((imported (import-paths %store (open-bytevector-input-port dump))))
  818. (and (equal? imported (list file0 file1 file2))
  819. (every file-exists? (list file0 file1 file2))
  820. (equal? (list file0) (references %store file1))
  821. (equal? (list file1) (references %store file2))))))
  822. (test-assert "write-file & export-path yield the same result"
  823. ;; Here we compare 'write-file' and the daemon's own implementation.
  824. ;; 'write-file' is the reference because we know it sorts file
  825. ;; deterministically. Conversely, the daemon uses 'readdir' and the entries
  826. ;; currently happen to be sorted as a side-effect of some unrelated
  827. ;; operation (search for 'unhacked' in archive.cc.) Make sure we detect any
  828. ;; changes there.
  829. (run-with-store %store
  830. (mlet* %store-monad ((drv1 (package->derivation %bootstrap-guile))
  831. (out1 -> (derivation->output-path drv1))
  832. (data -> (unfold (cut >= <> 26)
  833. (lambda (i)
  834. (random-bytevector 128))
  835. 1+ 0))
  836. (build
  837. -> #~(begin
  838. (use-modules (rnrs io ports) (srfi srfi-1))
  839. (let ()
  840. (define letters
  841. (map (lambda (i)
  842. (string
  843. (integer->char
  844. (+ i (char->integer #\a)))))
  845. (iota 26)))
  846. (define (touch file data)
  847. (call-with-output-file file
  848. (lambda (port)
  849. (put-bytevector port data))))
  850. (mkdir #$output)
  851. (chdir #$output)
  852. ;; The files must be different so they have
  853. ;; different inode numbers, and the inode
  854. ;; order must differ from the lexicographic
  855. ;; order.
  856. (for-each touch
  857. (append (drop letters 10)
  858. (take letters 10))
  859. (list #$@data))
  860. #t)))
  861. (drv2 (gexp->derivation "bunch" build))
  862. (out2 -> (derivation->output-path drv2))
  863. (item-info -> (store-lift query-path-info)))
  864. (mbegin %store-monad
  865. (built-derivations (list drv1 drv2))
  866. (foldm %store-monad
  867. (lambda (item result)
  868. (define ref-hash
  869. (let-values (((port get) (gcrypt:open-sha256-port)))
  870. (write-file item port)
  871. (close-port port)
  872. (get)))
  873. ;; 'query-path-info' returns a hash produced by using the
  874. ;; daemon's C++ 'dump' function, which is the implementation
  875. ;; under test.
  876. (>>= (item-info item)
  877. (lambda (info)
  878. (return
  879. (and result
  880. (bytevector=? (path-info-hash info) ref-hash))))))
  881. #t
  882. (list out1 out2))))
  883. #:guile-for-build (%guile-for-build)))
  884. (test-assert "import not signed"
  885. (let* ((text (random-text))
  886. (file (add-file-tree-to-store %store
  887. `("tree" directory
  888. ("text" regular (data ,text))
  889. ("link" symlink "text"))))
  890. (dump (call-with-bytevector-output-port
  891. (lambda (port)
  892. (write-int 1 port) ;start
  893. (write-file file port) ;contents
  894. (write-int #x4558494e port) ;%export-magic
  895. (write-string file port) ;store item
  896. (write-string-list '() port) ;references
  897. (write-string "" port) ;deriver
  898. (write-int 0 port) ;not signed
  899. (write-int 0 port))))) ;done
  900. ;; Ensure 'import-paths' raises an exception.
  901. (guard (c ((store-protocol-error? c)
  902. (and (not (zero? (store-protocol-error-status c)))
  903. (string-contains (store-protocol-error-message c)
  904. "lacks a signature"))))
  905. (let* ((source (open-bytevector-input-port dump))
  906. (imported (import-paths %store source)))
  907. (pk 'unsigned-imported imported)
  908. #f))))
  909. (test-assert "import signed by unauthorized key"
  910. (let* ((text (random-text))
  911. (file (add-file-tree-to-store %store
  912. `("tree" directory
  913. ("text" regular (data ,text))
  914. ("link" symlink "text"))))
  915. (key (gcrypt:generate-key
  916. (gcrypt:string->canonical-sexp
  917. "(genkey (ecdsa (curve Ed25519) (flags rfc6979)))")))
  918. (dump (call-with-bytevector-output-port
  919. (lambda (port)
  920. (write-int 1 port) ;start
  921. (write-file file port) ;contents
  922. (write-int #x4558494e port) ;%export-magic
  923. (write-string file port) ;store item
  924. (write-string-list '() port) ;references
  925. (write-string "" port) ;deriver
  926. (write-int 1 port) ;signed
  927. (write-string (gcrypt:canonical-sexp->string
  928. (signature-sexp
  929. (gcrypt:bytevector->hash-data
  930. (gcrypt:sha256 #vu8(0 1 2))
  931. #:key-type 'ecc)
  932. (gcrypt:find-sexp-token key 'private-key)
  933. (gcrypt:find-sexp-token key 'public-key)))
  934. port)
  935. (write-int 0 port))))) ;done
  936. ;; Ensure 'import-paths' raises an exception.
  937. (guard (c ((store-protocol-error? c)
  938. (and (not (zero? (store-protocol-error-status c)))
  939. (string-contains (store-protocol-error-message c)
  940. "unauthorized public key"))))
  941. (let* ((source (open-bytevector-input-port dump))
  942. (imported (import-paths %store source)))
  943. (pk 'unauthorized-imported imported)
  944. #f))))
  945. (test-assert "import corrupt path"
  946. (let* ((text (random-text))
  947. (file (add-text-to-store %store "text" text))
  948. (dump (call-with-bytevector-output-port
  949. (cut export-paths %store (list file) <>))))
  950. (delete-paths %store (list file))
  951. ;; Flip a bit in the stream's payload. INDEX here falls in the middle of
  952. ;; the file contents in DUMP, regardless of the store prefix.
  953. (let* ((index #x70)
  954. (byte (bytevector-u8-ref dump index)))
  955. (bytevector-u8-set! dump index (logxor #xff byte)))
  956. (and (not (file-exists? file))
  957. (guard (c ((store-protocol-error? c)
  958. (pk 'c c)
  959. (and (not (zero? (store-protocol-error-status c)))
  960. (string-contains (store-protocol-error-message c)
  961. "corrupt"))))
  962. (let* ((source (open-bytevector-input-port dump))
  963. (imported (import-paths %store source)))
  964. (pk 'corrupt-imported imported)
  965. #f)))))
  966. (test-assert "verify-store"
  967. (let* ((text (random-text))
  968. (file1 (add-text-to-store %store "foo" text))
  969. (file2 (add-text-to-store %store "bar" (random-text)
  970. (list file1))))
  971. (and (pk 'verify1 (verify-store %store)) ;hopefully OK ;
  972. (begin
  973. (delete-file file1)
  974. (not (pk 'verify2 (verify-store %store)))) ;bad! ;
  975. (begin
  976. ;; Using 'add-text-to-store' here wouldn't work: It would succeed ;
  977. ;; without actually creating the file. ;
  978. (call-with-output-file file1
  979. (lambda (port)
  980. (display text port)))
  981. (pk 'verify3 (verify-store %store)))))) ;OK again
  982. (test-assert "verify-store + check-contents"
  983. ;; XXX: This test is I/O intensive.
  984. (with-store s
  985. (let* ((text (random-text))
  986. (drv (build-expression->derivation
  987. s "corrupt"
  988. `(let ((out (assoc-ref %outputs "out")))
  989. (call-with-output-file out
  990. (lambda (port)
  991. (display ,text port)))
  992. #t)
  993. #:guile-for-build
  994. (package-derivation s %bootstrap-guile (%current-system))))
  995. (file (derivation->output-path drv)))
  996. (with-derivation-substitute drv text
  997. (and (build-derivations s (list drv))
  998. (verify-store s #:check-contents? #t) ;should be OK
  999. (begin
  1000. (chmod file #o644)
  1001. (call-with-output-file file
  1002. (lambda (port)
  1003. (display "corrupt!" port)))
  1004. #t)
  1005. ;; Make sure the corruption is detected. We don't test repairing
  1006. ;; because only "trusted" users are allowed to do it, but we
  1007. ;; don't expose that notion of trusted users that nix-daemon
  1008. ;; supports because it seems dubious and redundant with what the
  1009. ;; OS provides (in Nix "trusted" users have additional
  1010. ;; privileges, such as overriding the set of substitute URLs, but
  1011. ;; we instead want to allow anyone to modify them, provided
  1012. ;; substitutes are signed by a root-approved key.)
  1013. (not (verify-store s #:check-contents? #t))
  1014. ;; Delete the corrupt item to leave the store in a clean state.
  1015. (delete-paths s (list file)))))))
  1016. (test-assert "build-things, check mode"
  1017. (with-store store
  1018. (call-with-temporary-output-file
  1019. (lambda (entropy entropy-port)
  1020. (write (random-text) entropy-port)
  1021. (force-output entropy-port)
  1022. (let* ((drv (build-expression->derivation
  1023. store "non-deterministic"
  1024. `(begin
  1025. (use-modules (rnrs io ports))
  1026. (let ((out (assoc-ref %outputs "out")))
  1027. (call-with-output-file out
  1028. (lambda (port)
  1029. ;; Rely on the fact that tests do not use the
  1030. ;; chroot, and thus ENTROPY is readable.
  1031. (display (call-with-input-file ,entropy
  1032. get-string-all)
  1033. port)))
  1034. #t))
  1035. #:guile-for-build
  1036. (package-derivation store %bootstrap-guile (%current-system))))
  1037. (file (derivation->output-path drv)))
  1038. (and (build-things store (list (derivation-file-name drv)))
  1039. (begin
  1040. (write (random-text) entropy-port)
  1041. (force-output entropy-port)
  1042. (guard (c ((store-protocol-error? c)
  1043. (pk 'determinism-exception c)
  1044. (and (not (zero? (store-protocol-error-status c)))
  1045. (string-contains (store-protocol-error-message c)
  1046. "deterministic"))))
  1047. ;; This one will produce a different result. Since we're in
  1048. ;; 'check' mode, this must fail.
  1049. (build-things store (list (derivation-file-name drv))
  1050. (build-mode check))
  1051. #f))))))))
  1052. (test-assert "build-succeeded trace in check mode"
  1053. (string-contains
  1054. (call-with-output-string
  1055. (lambda (port)
  1056. (let ((d (build-expression->derivation
  1057. %store "foo" '(mkdir (assoc-ref %outputs "out"))
  1058. #:guile-for-build
  1059. (package-derivation %store %bootstrap-guile))))
  1060. (build-derivations %store (list d))
  1061. (parameterize ((current-build-output-port port))
  1062. (build-derivations %store (list d) (build-mode check))))))
  1063. "@ build-succeeded"))
  1064. (test-assert "build multiple times"
  1065. (with-store store
  1066. ;; Ask to build twice.
  1067. (set-build-options store #:rounds 2 #:use-substitutes? #f)
  1068. (call-with-temporary-output-file
  1069. (lambda (entropy entropy-port)
  1070. (write (random-text) entropy-port)
  1071. (force-output entropy-port)
  1072. (let* ((drv (build-expression->derivation
  1073. store "non-deterministic"
  1074. `(begin
  1075. (use-modules (rnrs io ports))
  1076. (let ((out (assoc-ref %outputs "out")))
  1077. (call-with-output-file out
  1078. (lambda (port)
  1079. ;; Rely on the fact that tests do not use the
  1080. ;; chroot, and thus ENTROPY is accessible.
  1081. (display (call-with-input-file ,entropy
  1082. get-string-all)
  1083. port)
  1084. (call-with-output-file ,entropy
  1085. (lambda (port)
  1086. (write 'foobar port)))))
  1087. #t))
  1088. #:guile-for-build
  1089. (package-derivation store %bootstrap-guile (%current-system))))
  1090. (file (derivation->output-path drv)))
  1091. (guard (c ((store-protocol-error? c)
  1092. (pk 'multiple-build c)
  1093. (and (not (zero? (store-protocol-error-status c)))
  1094. (string-contains (store-protocol-error-message c)
  1095. "deterministic"))))
  1096. ;; This one will produce a different result on the second run.
  1097. (current-build-output-port (current-error-port))
  1098. (build-things store (list (derivation-file-name drv)))
  1099. #f))))))
  1100. (test-equal "store-lower"
  1101. "Lowered."
  1102. (let* ((add (store-lower text-file))
  1103. (file (add %store "foo" "Lowered.")))
  1104. (call-with-input-file file get-string-all)))
  1105. (test-equal "current-system"
  1106. "bar"
  1107. (parameterize ((%current-system "frob"))
  1108. (run-with-store %store
  1109. (mbegin %store-monad
  1110. (set-current-system "bar")
  1111. (current-system))
  1112. #:system "foo")))
  1113. (test-assert "query-path-info"
  1114. (let* ((ref (add-text-to-store %store "ref" "foo"))
  1115. (item (add-text-to-store %store "item" "bar" (list ref)))
  1116. (info (query-path-info %store item)))
  1117. (and (equal? (path-info-references info) (list ref))
  1118. (equal? (path-info-hash info)
  1119. (gcrypt:sha256
  1120. (string->utf8
  1121. (call-with-output-string (cut write-file item <>))))))))
  1122. (test-assert "path-info-deriver"
  1123. (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
  1124. (s (add-to-store %store "bash" #t "sha256"
  1125. (search-bootstrap-binary "bash"
  1126. (%current-system))))
  1127. (d (derivation %store "the-thing"
  1128. s `("-e" ,b)
  1129. #:env-vars `(("foo" . ,(random-text)))
  1130. #:inputs `((,b) (,s))))
  1131. (o (derivation->output-path d)))
  1132. (and (build-derivations %store (list d))
  1133. (not (path-info-deriver (query-path-info %store b)))
  1134. (string=? (derivation-file-name d)
  1135. (path-info-deriver (query-path-info %store o))))))
  1136. (test-equal "build-cores"
  1137. (list 0 42)
  1138. (with-store store
  1139. (let* ((build (add-text-to-store store "build.sh"
  1140. "echo $NIX_BUILD_CORES > $out"))
  1141. (bash (add-to-store store "bash" #t "sha256"
  1142. (search-bootstrap-binary "bash"
  1143. (%current-system))))
  1144. (drv1 (derivation store "the-thing" bash
  1145. `("-e" ,build)
  1146. #:inputs `((,bash) (,build))
  1147. #:env-vars `(("x" . ,(random-text)))))
  1148. (drv2 (derivation store "the-thing" bash
  1149. `("-e" ,build)
  1150. #:inputs `((,bash) (,build))
  1151. #:env-vars `(("x" . ,(random-text))))))
  1152. (and (build-derivations store (list drv1))
  1153. (begin
  1154. (set-build-options store #:build-cores 42)
  1155. (build-derivations store (list drv2)))
  1156. (list (call-with-input-file (derivation->output-path drv1)
  1157. read)
  1158. (call-with-input-file (derivation->output-path drv2)
  1159. read))))))
  1160. (test-equal "multiplexed-build-output"
  1161. '("Hello from first." "Hello from second.")
  1162. (with-store store
  1163. (let* ((build (add-text-to-store store "build.sh"
  1164. "echo Hello from $NAME.; echo > $out"))
  1165. (bash (add-to-store store "bash" #t "sha256"
  1166. (search-bootstrap-binary "bash"
  1167. (%current-system))))
  1168. (drv1 (derivation store "one" bash
  1169. `("-e" ,build)
  1170. #:inputs `((,bash) (,build))
  1171. #:env-vars `(("NAME" . "first")
  1172. ("x" . ,(random-text)))))
  1173. (drv2 (derivation store "two" bash
  1174. `("-e" ,build)
  1175. #:inputs `((,bash) (,build))
  1176. #:env-vars `(("NAME" . "second")
  1177. ("x" . ,(random-text))))))
  1178. (set-build-options store
  1179. #:print-build-trace #t
  1180. #:multiplexed-build-output? #t
  1181. #:max-build-jobs 10)
  1182. (let ((port (open-output-string)))
  1183. ;; Send the build log to PORT.
  1184. (parameterize ((current-build-output-port port))
  1185. (build-derivations store (list drv1 drv2)))
  1186. ;; Retrieve the build log; make sure it contains valid "@ build-log"
  1187. ;; traces that allow us to retrieve each builder's output (we assume
  1188. ;; there's exactly one "build-output" trace for each builder, which is
  1189. ;; reasonable.)
  1190. (let* ((log (get-output-string port))
  1191. (started (fold-matches
  1192. (make-regexp "@ build-started ([^ ]+) - ([^ ]+) ([^ ]+) ([0-9]+)")
  1193. log '() cons))
  1194. (done (fold-matches
  1195. (make-regexp "@ build-succeeded (.*) - (.*) (.*) (.*)")
  1196. log '() cons))
  1197. (output (fold-matches
  1198. (make-regexp "@ build-log ([[:digit:]]+) ([[:digit:]]+)\n([A-Za-z .*]+)\n")
  1199. log '() cons))
  1200. (drv-pid (lambda (name)
  1201. (lambda (m)
  1202. (let ((drv (match:substring m 1))
  1203. (pid (string->number
  1204. (match:substring m 4))))
  1205. (and (string-suffix? name drv) pid)))))
  1206. (pid-log (lambda (pid)
  1207. (lambda (m)
  1208. (let ((n (string->number
  1209. (match:substring m 1)))
  1210. (len (string->number
  1211. (match:substring m 2)))
  1212. (str (match:substring m 3)))
  1213. (and (= pid n)
  1214. (= (string-length str) (- len 1))
  1215. str)))))
  1216. (pid1 (any (drv-pid "one.drv") started))
  1217. (pid2 (any (drv-pid "two.drv") started)))
  1218. (list (any (pid-log pid1) output)
  1219. (any (pid-log pid2) output)))))))
  1220. (test-end "store")