store.scm 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421
  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-assert "mapm/accumulate-builds"
  448. (let* ((d1 (run-with-store %store
  449. (gexp->derivation "foo" #~(mkdir #$output))))
  450. (d2 (run-with-store %store
  451. (gexp->derivation "bar" #~(mkdir #$output)))))
  452. (with-build-handler (lambda (continue store things mode)
  453. (equal? (map derivation-file-name (pk 'zz (list d1 d2)))
  454. (pk 'XX things)))
  455. (run-with-store %store
  456. (mapm/accumulate-builds built-derivations `((,d1) (,d2)))))))
  457. (test-equal "mapm/accumulate-builds, %current-target-system"
  458. (make-list 2 '("i586-pc-gnu" "i586-pc-gnu"))
  459. ;; Both the 'mapm' and 'mapm/accumulate-builds' procedures should see the
  460. ;; right #:target.
  461. (run-with-store %store
  462. (mlet %store-monad ((lst1 (mapm %store-monad
  463. (lambda _
  464. (current-target-system))
  465. '(a b)))
  466. (lst2 (mapm/accumulate-builds
  467. (lambda _
  468. (current-target-system))
  469. '(a b))))
  470. (return (list lst1 lst2)))
  471. #:system system
  472. #:target "i586-pc-gnu"))
  473. (test-assert "topologically-sorted, one item"
  474. (let* ((a (add-text-to-store %store "a" "a"))
  475. (b (add-text-to-store %store "b" "b" (list a)))
  476. (c (add-text-to-store %store "c" "c" (list b)))
  477. (d (add-text-to-store %store "d" "d" (list c)))
  478. (s (topologically-sorted %store (list d))))
  479. (equal? s (list a b c d))))
  480. (test-assert "topologically-sorted, several items"
  481. (let* ((a (add-text-to-store %store "a" "a"))
  482. (b (add-text-to-store %store "b" "b" (list a)))
  483. (c (add-text-to-store %store "c" "c" (list b)))
  484. (d (add-text-to-store %store "d" "d" (list c)))
  485. (s1 (topologically-sorted %store (list d a c b)))
  486. (s2 (topologically-sorted %store (list b d c a b d))))
  487. (equal? s1 s2 (list a b c d))))
  488. (test-assert "topologically-sorted, more difficult"
  489. (let* ((a (add-text-to-store %store "a" "a"))
  490. (b (add-text-to-store %store "b" "b" (list a)))
  491. (c (add-text-to-store %store "c" "c" (list b)))
  492. (d (add-text-to-store %store "d" "d" (list c)))
  493. (w (add-text-to-store %store "w" "w"))
  494. (x (add-text-to-store %store "x" "x" (list w)))
  495. (y (add-text-to-store %store "y" "y" (list x d)))
  496. (s1 (topologically-sorted %store (list y)))
  497. (s2 (topologically-sorted %store (list c y)))
  498. (s3 (topologically-sorted %store (cons y (references %store y)))))
  499. ;; The order in which 'references' returns the references of Y is
  500. ;; unspecified, so accommodate.
  501. (let* ((x-then-d? (equal? (references %store y) (list x d))))
  502. (and (equal? s1
  503. (if x-then-d?
  504. (list w x a b c d y)
  505. (list a b c d w x y)))
  506. (equal? s2
  507. (if x-then-d?
  508. (list a b c w x d y)
  509. (list a b c d w x y)))
  510. (lset= string=? s1 s3)))))
  511. (test-assert "current-build-output-port, UTF-8"
  512. ;; Are UTF-8 strings in the build log properly interpreted?
  513. (string-contains
  514. (with-fluids ((%default-port-encoding "UTF-8")) ;for the string port
  515. (call-with-output-string
  516. (lambda (port)
  517. (parameterize ((current-build-output-port port))
  518. (let* ((s "Here’s a Greek letter: λ.")
  519. (d (build-expression->derivation
  520. %store "foo" `(display ,s)
  521. #:guile-for-build
  522. (package-derivation %store %bootstrap-guile
  523. (%current-system)))))
  524. (guard (c ((store-protocol-error? c) #t))
  525. (build-derivations %store (list d))))))))
  526. "Here’s a Greek letter: λ."))
  527. (test-assert "current-build-output-port, UTF-8 + garbage"
  528. ;; What about a mixture of UTF-8 + garbage?
  529. (string-contains
  530. (with-fluids ((%default-port-encoding "UTF-8")) ;for the string port
  531. (call-with-output-string
  532. (lambda (port)
  533. (parameterize ((current-build-output-port port))
  534. (let ((d (build-expression->derivation
  535. %store "foo"
  536. `(begin
  537. (use-modules (rnrs io ports))
  538. (display "garbage: ")
  539. (put-bytevector (current-output-port) #vu8(128))
  540. (display "lambda: λ\n"))
  541. #:guile-for-build
  542. (package-derivation %store %bootstrap-guile))))
  543. (guard (c ((store-protocol-error? c) #t))
  544. (build-derivations %store (list d))))))))
  545. "garbage: �lambda: λ"))
  546. (test-assert "log-file, derivation"
  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. (and (build-derivations %store (list d))
  556. (file-exists? (pk (log-file %store (derivation-file-name d)))))))
  557. (test-assert "log-file, output file name"
  558. (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
  559. (s (add-to-store %store "bash" #t "sha256"
  560. (search-bootstrap-binary "bash"
  561. (%current-system))))
  562. (d (derivation %store "the-thing"
  563. s `("-e" ,b)
  564. #:env-vars `(("foo" . ,(random-text)))
  565. #:inputs `((,b) (,s))))
  566. (o (derivation->output-path d)))
  567. (and (build-derivations %store (list d))
  568. (file-exists? (pk (log-file %store o)))
  569. (string=? (log-file %store (derivation-file-name d))
  570. (log-file %store o)))))
  571. (test-assert "no substitutes"
  572. (with-store s
  573. (let* ((d1 (package-derivation s %bootstrap-guile (%current-system)))
  574. (d2 (package-derivation s %bootstrap-glibc (%current-system)))
  575. (o (map derivation->output-path (list d1 d2))))
  576. (set-build-options s #:use-substitutes? #f)
  577. (and (not (has-substitutes? s (derivation-file-name d1)))
  578. (not (has-substitutes? s (derivation-file-name d2)))
  579. (null? (substitutable-paths s o))
  580. (null? (substitutable-path-info s o))))))
  581. (test-assert "build-things with output path"
  582. (with-store s
  583. (let* ((c (random-text)) ;contents of the output
  584. (d (build-expression->derivation
  585. s "substitute-me"
  586. `(call-with-output-file %output
  587. (lambda (p)
  588. (display ,c p)))
  589. #:guile-for-build
  590. (package-derivation s %bootstrap-guile (%current-system))))
  591. (o (derivation->output-path d)))
  592. (set-build-options s #:use-substitutes? #f)
  593. ;; Pass 'build-things' the output file name, O. However, since there
  594. ;; are no substitutes for O, it will just do nothing.
  595. (build-things s (list o))
  596. (not (valid-path? s o)))))
  597. (test-skip (if (getenv "GUIX_BINARY_SUBSTITUTE_URL") 0 1))
  598. (test-assert "substitute query"
  599. (with-store s
  600. (let* ((d (package-derivation s %bootstrap-guile (%current-system)))
  601. (o (derivation->output-path d)))
  602. ;; Create fake substituter data, to be read by 'guix substitute'.
  603. (with-derivation-narinfo d
  604. ;; Remove entry from the local cache.
  605. (false-if-exception
  606. (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
  607. "/guix/substitute")))
  608. ;; Make sure 'guix substitute' correctly communicates the above
  609. ;; data.
  610. (set-build-options s #:use-substitutes? #t
  611. #:substitute-urls (%test-substitute-urls))
  612. (and (has-substitutes? s o)
  613. (equal? (list o) (substitutable-paths s (list o)))
  614. (match (pk 'spi (substitutable-path-info s (list o)))
  615. (((? substitutable? s))
  616. (and (string=? (substitutable-deriver s)
  617. (derivation-file-name d))
  618. (null? (substitutable-references s))
  619. (equal? (substitutable-nar-size s) 1234)))))))))
  620. (test-assert "substitute query, alternating URLs"
  621. (let* ((d (with-store s
  622. (package-derivation s %bootstrap-guile (%current-system))))
  623. (o (derivation->output-path d)))
  624. (with-derivation-narinfo d
  625. ;; Remove entry from the local cache.
  626. (false-if-exception
  627. (delete-file-recursively (string-append (getenv "XDG_CACHE_HOME")
  628. "/guix/substitute")))
  629. ;; Note: We reconnect to the daemon to force a new instance of 'guix
  630. ;; substitute' to be used; otherwise the #:substitute-urls of
  631. ;; 'set-build-options' would have no effect.
  632. (and (with-store s ;the right substitute URL
  633. (set-build-options s #:use-substitutes? #t
  634. #:substitute-urls (%test-substitute-urls))
  635. (has-substitutes? s o))
  636. (with-store s ;the wrong one
  637. (set-build-options s #:use-substitutes? #t
  638. #:substitute-urls (list
  639. "http://does-not-exist"))
  640. (not (has-substitutes? s o)))
  641. (with-store s ;the right one again
  642. (set-build-options s #:use-substitutes? #t
  643. #:substitute-urls (%test-substitute-urls))
  644. (has-substitutes? s o))
  645. (with-store s ;empty list of URLs
  646. (set-build-options s #:use-substitutes? #t
  647. #:substitute-urls '())
  648. (not (has-substitutes? s o)))))))
  649. (test-assert "substitute"
  650. (with-store s
  651. (let* ((c (random-text)) ; contents of the output
  652. (d (build-expression->derivation
  653. s "substitute-me"
  654. `(call-with-output-file %output
  655. (lambda (p)
  656. (exit 1) ; would actually fail
  657. (display ,c p)))
  658. #:guile-for-build
  659. (package-derivation s %bootstrap-guile (%current-system))))
  660. (o (derivation->output-path d)))
  661. (with-derivation-substitute d c
  662. (set-build-options s #:use-substitutes? #t
  663. #:substitute-urls (%test-substitute-urls))
  664. (and (has-substitutes? s o)
  665. (build-derivations s (list d))
  666. (canonical-file? o)
  667. (equal? c (call-with-input-file o get-string-all)))))))
  668. (test-assert "substitute, deduplication"
  669. (with-store s
  670. (let* ((c (random-text)) ; contents of the output
  671. (g (package-derivation s %bootstrap-guile))
  672. (d1 (build-expression->derivation s "substitute-me"
  673. `(begin ,c (exit 1))
  674. #:guile-for-build g))
  675. (d2 (build-expression->derivation s "build-me"
  676. `(call-with-output-file %output
  677. (lambda (p)
  678. (display ,c p)))
  679. #:guile-for-build g))
  680. (o1 (derivation->output-path d1))
  681. (o2 (derivation->output-path d2)))
  682. (with-derivation-substitute d1 c
  683. (set-build-options s #:use-substitutes? #t
  684. #:substitute-urls (%test-substitute-urls))
  685. (and (has-substitutes? s o1)
  686. (build-derivations s (list d2)) ;build
  687. (build-derivations s (list d1)) ;substitute
  688. (canonical-file? o1)
  689. (equal? c (call-with-input-file o1 get-string-all))
  690. (= (stat:ino (stat o1)) (stat:ino (stat o2))))))))
  691. (test-assert "substitute + build-things with output path"
  692. (with-store s
  693. (let* ((c (random-text)) ;contents of the output
  694. (d (build-expression->derivation
  695. s "substitute-me"
  696. `(call-with-output-file %output
  697. (lambda (p)
  698. (exit 1) ;would actually fail
  699. (display ,c p)))
  700. #:guile-for-build
  701. (package-derivation s %bootstrap-guile (%current-system))))
  702. (o (derivation->output-path d)))
  703. (with-derivation-substitute d c
  704. (set-build-options s #:use-substitutes? #t
  705. #:substitute-urls (%test-substitute-urls))
  706. (and (has-substitutes? s o)
  707. (build-things s (list o)) ;give the output path
  708. (valid-path? s o)
  709. (canonical-file? o)
  710. (equal? c (call-with-input-file o get-string-all)))))))
  711. (test-assert "substitute + build-things with specific output"
  712. (with-store s
  713. (let* ((c (random-text)) ;contents of the output
  714. (d (build-expression->derivation
  715. s "substitute-me" `(begin ,c (exit 1)) ;would fail
  716. #:outputs '("out" "one" "two")
  717. #:guile-for-build
  718. (package-derivation s %bootstrap-guile (%current-system))))
  719. (o (derivation->output-path d)))
  720. (with-derivation-substitute d c
  721. (set-build-options s #:use-substitutes? #t
  722. #:substitute-urls (%test-substitute-urls))
  723. (and (has-substitutes? s o)
  724. ;; Ask for nothing but the "out" output of D.
  725. (build-things s `((,(derivation-file-name d) . "out")))
  726. (valid-path? s o)
  727. (canonical-file? o)
  728. (equal? c (call-with-input-file o get-string-all)))))))
  729. (test-assert "substitute, corrupt output hash"
  730. ;; Tweak the substituter into installing a substitute whose hash doesn't
  731. ;; match the one announced in the narinfo. The daemon must notice this and
  732. ;; raise an error.
  733. (with-store s
  734. (let* ((c "hello, world") ; contents of the output
  735. (d (build-expression->derivation
  736. s "corrupt-substitute"
  737. `(mkdir %output)
  738. #:guile-for-build
  739. (package-derivation s %bootstrap-guile (%current-system))))
  740. (o (derivation->output-path d)))
  741. (with-derivation-substitute d c
  742. (sha256 => (make-bytevector 32 0)) ;select a hash that doesn't match C
  743. ;; Make sure we use 'guix substitute'.
  744. (set-build-options s
  745. #:use-substitutes? #t
  746. #:fallback? #f
  747. #:substitute-urls (%test-substitute-urls))
  748. (and (has-substitutes? s o)
  749. (guard (c ((store-protocol-error? c)
  750. ;; XXX: the daemon writes "hash mismatch in downloaded
  751. ;; path", but the actual error returned to the client
  752. ;; doesn't mention that.
  753. (pk 'corrupt c)
  754. (not (zero? (store-protocol-error-status c)))))
  755. (build-derivations s (list d))
  756. #f))))))
  757. (test-assert "substitute, corrupt output hash, build trace"
  758. ;; Likewise, and check the build trace.
  759. (with-store s
  760. (let* ((c "hello, world") ; contents of the output
  761. (d (build-expression->derivation
  762. s "corrupt-substitute"
  763. `(mkdir %output)
  764. #:guile-for-build
  765. (package-derivation s %bootstrap-guile (%current-system))))
  766. (o (derivation->output-path d)))
  767. ;; Make sure we use 'guix substitute'.
  768. (set-build-options s
  769. #:print-build-trace #t
  770. #:use-substitutes? #t
  771. #:fallback? #f
  772. #:substitute-urls (%test-substitute-urls))
  773. (with-derivation-substitute d c
  774. (sha256 => (make-bytevector 32 0)) ;select a hash that doesn't match C
  775. (define output
  776. (call-with-output-string
  777. (lambda (port)
  778. (parameterize ((current-build-output-port port))
  779. (guard (c ((store-protocol-error? c) #t))
  780. (build-derivations s (list d))
  781. #f)))))
  782. (define actual-hash
  783. (let-values (((port get-hash)
  784. (gcrypt:open-hash-port
  785. (gcrypt:hash-algorithm gcrypt:sha256))))
  786. (write-file-tree "foo" port
  787. #:file-type+size
  788. (lambda _
  789. (values 'regular (string-length c)))
  790. #:file-port
  791. (lambda _
  792. (open-input-string c)))
  793. (close-port port)
  794. (bytevector->nix-base32-string (get-hash))))
  795. (define expected-hash
  796. (bytevector->nix-base32-string (make-bytevector 32 0)))
  797. (define mismatch
  798. (string-append "@ hash-mismatch " o " sha256 "
  799. expected-hash " " actual-hash "\n"))
  800. (define failure
  801. (string-append "@ substituter-failed " o))
  802. (and (string-contains output mismatch)
  803. (string-contains output failure))))))
  804. (test-assert "substitute --fallback"
  805. (with-store s
  806. (let* ((t (random-text)) ; contents of the output
  807. (d (build-expression->derivation
  808. s "substitute-me-not"
  809. `(call-with-output-file %output
  810. (lambda (p)
  811. (display ,t p)))
  812. #:guile-for-build
  813. (package-derivation s %bootstrap-guile (%current-system))))
  814. (o (derivation->output-path d)))
  815. ;; Create fake substituter data, to be read by 'guix substitute'.
  816. (with-derivation-narinfo d
  817. ;; Make sure we use 'guix substitute'.
  818. (set-build-options s #:use-substitutes? #t
  819. #:substitute-urls (%test-substitute-urls))
  820. (and (has-substitutes? s o)
  821. (guard (c ((store-protocol-error? c)
  822. ;; The substituter failed as expected. Now make
  823. ;; sure that #:fallback? #t works correctly.
  824. (set-build-options s
  825. #:use-substitutes? #t
  826. #:substitute-urls
  827. (%test-substitute-urls)
  828. #:fallback? #t)
  829. (and (build-derivations s (list d))
  830. (equal? t (call-with-input-file o
  831. get-string-all)))))
  832. ;; Should fail.
  833. (build-derivations s (list d))
  834. #f))))))
  835. (test-assert "export/import several paths"
  836. (let* ((texts (unfold (cut >= <> 10)
  837. (lambda _ (random-text))
  838. 1+
  839. 0))
  840. (files (map (cut add-text-to-store %store "text" <>) texts))
  841. (dump (call-with-bytevector-output-port
  842. (cut export-paths %store files <>))))
  843. (delete-paths %store files)
  844. (and (every (negate file-exists?) files)
  845. (let* ((source (open-bytevector-input-port dump))
  846. (imported (import-paths %store source)))
  847. (and (equal? imported files)
  848. (every file-exists? files)
  849. (equal? texts
  850. (map (lambda (file)
  851. (call-with-input-file file
  852. get-string-all))
  853. files)))))))
  854. (test-assert "export/import paths, ensure topological order"
  855. (let* ((file0 (add-text-to-store %store "baz" (random-text)))
  856. (file1 (add-text-to-store %store "foo" (random-text)
  857. (list file0)))
  858. (file2 (add-text-to-store %store "bar" (random-text)
  859. (list file1)))
  860. (files (list file1 file2))
  861. (dump1 (call-with-bytevector-output-port
  862. (cute export-paths %store (list file1 file2) <>)))
  863. (dump2 (call-with-bytevector-output-port
  864. (cute export-paths %store (list file2 file1) <>))))
  865. (delete-paths %store files)
  866. (and (every (negate file-exists?) files)
  867. (bytevector=? dump1 dump2)
  868. (let* ((source (open-bytevector-input-port dump1))
  869. (imported (import-paths %store source)))
  870. ;; DUMP1 should contain exactly FILE1 and FILE2, not FILE0.
  871. (and (equal? imported (list file1 file2))
  872. (every file-exists? files)
  873. (equal? (list file0) (references %store file1))
  874. (equal? (list file1) (references %store file2)))))))
  875. (test-assert "export/import incomplete"
  876. (let* ((file0 (add-text-to-store %store "baz" (random-text)))
  877. (file1 (add-text-to-store %store "foo" (random-text)
  878. (list file0)))
  879. (file2 (add-text-to-store %store "bar" (random-text)
  880. (list file1)))
  881. (dump (call-with-bytevector-output-port
  882. (cute export-paths %store (list file2) <>))))
  883. (delete-paths %store (list file0 file1 file2))
  884. (guard (c ((store-protocol-error? c)
  885. (and (not (zero? (store-protocol-error-status c)))
  886. (string-contains (store-protocol-error-message c)
  887. "not valid"))))
  888. ;; Here we get an exception because DUMP does not include FILE0 and
  889. ;; FILE1, which are dependencies of FILE2.
  890. (import-paths %store (open-bytevector-input-port dump)))))
  891. (test-assert "export/import recursive"
  892. (let* ((file0 (add-text-to-store %store "baz" (random-text)))
  893. (file1 (add-text-to-store %store "foo" (random-text)
  894. (list file0)))
  895. (file2 (add-text-to-store %store "bar" (random-text)
  896. (list file1)))
  897. (dump (call-with-bytevector-output-port
  898. (cute export-paths %store (list file2) <>
  899. #:recursive? #t))))
  900. (delete-paths %store (list file0 file1 file2))
  901. (let ((imported (import-paths %store (open-bytevector-input-port dump))))
  902. (and (equal? imported (list file0 file1 file2))
  903. (every file-exists? (list file0 file1 file2))
  904. (equal? (list file0) (references %store file1))
  905. (equal? (list file1) (references %store file2))))))
  906. (test-assert "write-file & export-path yield the same result"
  907. ;; Here we compare 'write-file' and the daemon's own implementation.
  908. ;; 'write-file' is the reference because we know it sorts file
  909. ;; deterministically. Conversely, the daemon uses 'readdir' and the entries
  910. ;; currently happen to be sorted as a side-effect of some unrelated
  911. ;; operation (search for 'unhacked' in archive.cc.) Make sure we detect any
  912. ;; changes there.
  913. (run-with-store %store
  914. (mlet* %store-monad ((drv1 (package->derivation %bootstrap-guile))
  915. (out1 -> (derivation->output-path drv1))
  916. (data -> (unfold (cut >= <> 26)
  917. (lambda (i)
  918. (random-bytevector 128))
  919. 1+ 0))
  920. (build
  921. -> #~(begin
  922. (use-modules (rnrs io ports) (srfi srfi-1))
  923. (let ()
  924. (define letters
  925. (map (lambda (i)
  926. (string
  927. (integer->char
  928. (+ i (char->integer #\a)))))
  929. (iota 26)))
  930. (define (touch file data)
  931. (call-with-output-file file
  932. (lambda (port)
  933. (put-bytevector port data))))
  934. (mkdir #$output)
  935. (chdir #$output)
  936. ;; The files must be different so they have
  937. ;; different inode numbers, and the inode
  938. ;; order must differ from the lexicographic
  939. ;; order.
  940. (for-each touch
  941. (append (drop letters 10)
  942. (take letters 10))
  943. (list #$@data))
  944. #t)))
  945. (drv2 (gexp->derivation "bunch" build))
  946. (out2 -> (derivation->output-path drv2))
  947. (item-info -> (store-lift query-path-info)))
  948. (mbegin %store-monad
  949. (built-derivations (list drv1 drv2))
  950. (foldm %store-monad
  951. (lambda (item result)
  952. (define ref-hash
  953. (let-values (((port get) (gcrypt:open-sha256-port)))
  954. (write-file item port)
  955. (close-port port)
  956. (get)))
  957. ;; 'query-path-info' returns a hash produced by using the
  958. ;; daemon's C++ 'dump' function, which is the implementation
  959. ;; under test.
  960. (>>= (item-info item)
  961. (lambda (info)
  962. (return
  963. (and result
  964. (bytevector=? (path-info-hash info) ref-hash))))))
  965. #t
  966. (list out1 out2))))
  967. #:guile-for-build (%guile-for-build)))
  968. (test-assert "import not signed"
  969. (let* ((text (random-text))
  970. (file (add-file-tree-to-store %store
  971. `("tree" directory
  972. ("text" regular (data ,text))
  973. ("link" symlink "text"))))
  974. (dump (call-with-bytevector-output-port
  975. (lambda (port)
  976. (write-int 1 port) ;start
  977. (write-file file port) ;contents
  978. (write-int #x4558494e port) ;%export-magic
  979. (write-string file port) ;store item
  980. (write-string-list '() port) ;references
  981. (write-string "" port) ;deriver
  982. (write-int 0 port) ;not signed
  983. (write-int 0 port))))) ;done
  984. ;; Ensure 'import-paths' raises an exception.
  985. (guard (c ((store-protocol-error? c)
  986. (and (not (zero? (store-protocol-error-status c)))
  987. (string-contains (store-protocol-error-message c)
  988. "lacks a signature"))))
  989. (let* ((source (open-bytevector-input-port dump))
  990. (imported (import-paths %store source)))
  991. (pk 'unsigned-imported imported)
  992. #f))))
  993. (test-assert "import signed by unauthorized key"
  994. (let* ((text (random-text))
  995. (file (add-file-tree-to-store %store
  996. `("tree" directory
  997. ("text" regular (data ,text))
  998. ("link" symlink "text"))))
  999. (key (gcrypt:generate-key
  1000. (gcrypt:string->canonical-sexp
  1001. "(genkey (ecdsa (curve Ed25519) (flags rfc6979)))")))
  1002. (dump (call-with-bytevector-output-port
  1003. (lambda (port)
  1004. (write-int 1 port) ;start
  1005. (write-file file port) ;contents
  1006. (write-int #x4558494e port) ;%export-magic
  1007. (write-string file port) ;store item
  1008. (write-string-list '() port) ;references
  1009. (write-string "" port) ;deriver
  1010. (write-int 1 port) ;signed
  1011. (write-string (gcrypt:canonical-sexp->string
  1012. (signature-sexp
  1013. (gcrypt:bytevector->hash-data
  1014. (gcrypt:sha256 #vu8(0 1 2))
  1015. #:key-type 'ecc)
  1016. (gcrypt:find-sexp-token key 'private-key)
  1017. (gcrypt:find-sexp-token key 'public-key)))
  1018. port)
  1019. (write-int 0 port))))) ;done
  1020. ;; Ensure 'import-paths' raises an exception.
  1021. (guard (c ((store-protocol-error? c)
  1022. (and (not (zero? (store-protocol-error-status c)))
  1023. (string-contains (store-protocol-error-message c)
  1024. "unauthorized public key"))))
  1025. (let* ((source (open-bytevector-input-port dump))
  1026. (imported (import-paths %store source)))
  1027. (pk 'unauthorized-imported imported)
  1028. #f))))
  1029. (test-assert "import corrupt path"
  1030. (let* ((text (random-text))
  1031. (file (add-text-to-store %store "text" text))
  1032. (dump (call-with-bytevector-output-port
  1033. (cut export-paths %store (list file) <>))))
  1034. (delete-paths %store (list file))
  1035. ;; Flip a bit in the stream's payload. INDEX here falls in the middle of
  1036. ;; the file contents in DUMP, regardless of the store prefix.
  1037. (let* ((index #x70)
  1038. (byte (bytevector-u8-ref dump index)))
  1039. (bytevector-u8-set! dump index (logxor #xff byte)))
  1040. (and (not (file-exists? file))
  1041. (guard (c ((store-protocol-error? c)
  1042. (pk 'c c)
  1043. (and (not (zero? (store-protocol-error-status c)))
  1044. (string-contains (store-protocol-error-message c)
  1045. "corrupt"))))
  1046. (let* ((source (open-bytevector-input-port dump))
  1047. (imported (import-paths %store source)))
  1048. (pk 'corrupt-imported imported)
  1049. #f)))))
  1050. (test-assert "verify-store"
  1051. (let* ((text (random-text))
  1052. (file1 (add-text-to-store %store "foo" text))
  1053. (file2 (add-text-to-store %store "bar" (random-text)
  1054. (list file1))))
  1055. (and (pk 'verify1 (verify-store %store)) ;hopefully OK ;
  1056. (begin
  1057. (delete-file file1)
  1058. (not (pk 'verify2 (verify-store %store)))) ;bad! ;
  1059. (begin
  1060. ;; Using 'add-text-to-store' here wouldn't work: It would succeed ;
  1061. ;; without actually creating the file. ;
  1062. (call-with-output-file file1
  1063. (lambda (port)
  1064. (display text port)))
  1065. (pk 'verify3 (verify-store %store)))))) ;OK again
  1066. (test-assert "verify-store + check-contents"
  1067. ;; XXX: This test is I/O intensive.
  1068. (with-store s
  1069. (let* ((text (random-text))
  1070. (drv (build-expression->derivation
  1071. s "corrupt"
  1072. `(let ((out (assoc-ref %outputs "out")))
  1073. (call-with-output-file out
  1074. (lambda (port)
  1075. (display ,text port)))
  1076. #t)
  1077. #:guile-for-build
  1078. (package-derivation s %bootstrap-guile (%current-system))))
  1079. (file (derivation->output-path drv)))
  1080. (with-derivation-substitute drv text
  1081. (and (build-derivations s (list drv))
  1082. (verify-store s #:check-contents? #t) ;should be OK
  1083. (begin
  1084. (chmod file #o644)
  1085. (call-with-output-file file
  1086. (lambda (port)
  1087. (display "corrupt!" port)))
  1088. #t)
  1089. ;; Make sure the corruption is detected. We don't test repairing
  1090. ;; because only "trusted" users are allowed to do it, but we
  1091. ;; don't expose that notion of trusted users that nix-daemon
  1092. ;; supports because it seems dubious and redundant with what the
  1093. ;; OS provides (in Nix "trusted" users have additional
  1094. ;; privileges, such as overriding the set of substitute URLs, but
  1095. ;; we instead want to allow anyone to modify them, provided
  1096. ;; substitutes are signed by a root-approved key.)
  1097. (not (verify-store s #:check-contents? #t))
  1098. ;; Delete the corrupt item to leave the store in a clean state.
  1099. (delete-paths s (list file)))))))
  1100. (test-assert "build-things, check mode"
  1101. (with-store store
  1102. (call-with-temporary-output-file
  1103. (lambda (entropy entropy-port)
  1104. (write (random-text) entropy-port)
  1105. (force-output entropy-port)
  1106. (let* ((drv (build-expression->derivation
  1107. store "non-deterministic"
  1108. `(begin
  1109. (use-modules (rnrs io ports))
  1110. (let ((out (assoc-ref %outputs "out")))
  1111. (call-with-output-file out
  1112. (lambda (port)
  1113. ;; Rely on the fact that tests do not use the
  1114. ;; chroot, and thus ENTROPY is readable.
  1115. (display (call-with-input-file ,entropy
  1116. get-string-all)
  1117. port)))
  1118. #t))
  1119. #:guile-for-build
  1120. (package-derivation store %bootstrap-guile (%current-system))))
  1121. (file (derivation->output-path drv)))
  1122. (and (build-things store (list (derivation-file-name drv)))
  1123. (begin
  1124. (write (random-text) entropy-port)
  1125. (force-output entropy-port)
  1126. (guard (c ((store-protocol-error? c)
  1127. (pk 'determinism-exception c)
  1128. (and (not (zero? (store-protocol-error-status c)))
  1129. (string-contains (store-protocol-error-message c)
  1130. "deterministic"))))
  1131. ;; This one will produce a different result. Since we're in
  1132. ;; 'check' mode, this must fail.
  1133. (build-things store (list (derivation-file-name drv))
  1134. (build-mode check))
  1135. #f))))))))
  1136. (test-assert "build-succeeded trace in check mode"
  1137. (string-contains
  1138. (call-with-output-string
  1139. (lambda (port)
  1140. (let ((d (build-expression->derivation
  1141. %store "foo" '(mkdir (assoc-ref %outputs "out"))
  1142. #:guile-for-build
  1143. (package-derivation %store %bootstrap-guile))))
  1144. (build-derivations %store (list d))
  1145. (parameterize ((current-build-output-port port))
  1146. (build-derivations %store (list d) (build-mode check))))))
  1147. "@ build-succeeded"))
  1148. (test-assert "build multiple times"
  1149. (with-store store
  1150. ;; Ask to build twice.
  1151. (set-build-options store #:rounds 2 #:use-substitutes? #f)
  1152. (call-with-temporary-output-file
  1153. (lambda (entropy entropy-port)
  1154. (write (random-text) entropy-port)
  1155. (force-output entropy-port)
  1156. (let* ((drv (build-expression->derivation
  1157. store "non-deterministic"
  1158. `(begin
  1159. (use-modules (rnrs io ports))
  1160. (let ((out (assoc-ref %outputs "out")))
  1161. (call-with-output-file out
  1162. (lambda (port)
  1163. ;; Rely on the fact that tests do not use the
  1164. ;; chroot, and thus ENTROPY is accessible.
  1165. (display (call-with-input-file ,entropy
  1166. get-string-all)
  1167. port)
  1168. (call-with-output-file ,entropy
  1169. (lambda (port)
  1170. (write 'foobar port)))))
  1171. #t))
  1172. #:guile-for-build
  1173. (package-derivation store %bootstrap-guile (%current-system))))
  1174. (file (derivation->output-path drv)))
  1175. (guard (c ((store-protocol-error? c)
  1176. (pk 'multiple-build c)
  1177. (and (not (zero? (store-protocol-error-status c)))
  1178. (string-contains (store-protocol-error-message c)
  1179. "deterministic"))))
  1180. ;; This one will produce a different result on the second run.
  1181. (current-build-output-port (current-error-port))
  1182. (build-things store (list (derivation-file-name drv)))
  1183. #f))))))
  1184. (test-equal "store-lower"
  1185. "Lowered."
  1186. (let* ((add (store-lower text-file))
  1187. (file (add %store "foo" "Lowered.")))
  1188. (call-with-input-file file get-string-all)))
  1189. (test-equal "current-system"
  1190. "bar"
  1191. (parameterize ((%current-system "frob"))
  1192. (run-with-store %store
  1193. (mbegin %store-monad
  1194. (set-current-system "bar")
  1195. (current-system))
  1196. #:system "foo")))
  1197. (test-assert "query-path-info"
  1198. (let* ((ref (add-text-to-store %store "ref" "foo"))
  1199. (item (add-text-to-store %store "item" "bar" (list ref)))
  1200. (info (query-path-info %store item)))
  1201. (and (equal? (path-info-references info) (list ref))
  1202. (equal? (path-info-hash info)
  1203. (gcrypt:sha256
  1204. (string->utf8
  1205. (call-with-output-string (cut write-file item <>))))))))
  1206. (test-assert "path-info-deriver"
  1207. (let* ((b (add-text-to-store %store "build" "echo $foo > $out" '()))
  1208. (s (add-to-store %store "bash" #t "sha256"
  1209. (search-bootstrap-binary "bash"
  1210. (%current-system))))
  1211. (d (derivation %store "the-thing"
  1212. s `("-e" ,b)
  1213. #:env-vars `(("foo" . ,(random-text)))
  1214. #:inputs `((,b) (,s))))
  1215. (o (derivation->output-path d)))
  1216. (and (build-derivations %store (list d))
  1217. (not (path-info-deriver (query-path-info %store b)))
  1218. (string=? (derivation-file-name d)
  1219. (path-info-deriver (query-path-info %store o))))))
  1220. (test-equal "build-cores"
  1221. (list 0 42)
  1222. (with-store store
  1223. (let* ((build (add-text-to-store store "build.sh"
  1224. "echo $NIX_BUILD_CORES > $out"))
  1225. (bash (add-to-store store "bash" #t "sha256"
  1226. (search-bootstrap-binary "bash"
  1227. (%current-system))))
  1228. (drv1 (derivation store "the-thing" bash
  1229. `("-e" ,build)
  1230. #:inputs `((,bash) (,build))
  1231. #:env-vars `(("x" . ,(random-text)))))
  1232. (drv2 (derivation store "the-thing" bash
  1233. `("-e" ,build)
  1234. #:inputs `((,bash) (,build))
  1235. #:env-vars `(("x" . ,(random-text))))))
  1236. (and (build-derivations store (list drv1))
  1237. (begin
  1238. (set-build-options store #:build-cores 42)
  1239. (build-derivations store (list drv2)))
  1240. (list (call-with-input-file (derivation->output-path drv1)
  1241. read)
  1242. (call-with-input-file (derivation->output-path drv2)
  1243. read))))))
  1244. (test-equal "multiplexed-build-output"
  1245. '("Hello from first." "Hello from second.")
  1246. (with-store store
  1247. (let* ((build (add-text-to-store store "build.sh"
  1248. "echo Hello from $NAME.; echo > $out"))
  1249. (bash (add-to-store store "bash" #t "sha256"
  1250. (search-bootstrap-binary "bash"
  1251. (%current-system))))
  1252. (drv1 (derivation store "one" bash
  1253. `("-e" ,build)
  1254. #:inputs `((,bash) (,build))
  1255. #:env-vars `(("NAME" . "first")
  1256. ("x" . ,(random-text)))))
  1257. (drv2 (derivation store "two" bash
  1258. `("-e" ,build)
  1259. #:inputs `((,bash) (,build))
  1260. #:env-vars `(("NAME" . "second")
  1261. ("x" . ,(random-text))))))
  1262. (set-build-options store
  1263. #:print-build-trace #t
  1264. #:multiplexed-build-output? #t
  1265. #:max-build-jobs 10)
  1266. (let ((port (open-output-string)))
  1267. ;; Send the build log to PORT.
  1268. (parameterize ((current-build-output-port port))
  1269. (build-derivations store (list drv1 drv2)))
  1270. ;; Retrieve the build log; make sure it contains valid "@ build-log"
  1271. ;; traces that allow us to retrieve each builder's output (we assume
  1272. ;; there's exactly one "build-output" trace for each builder, which is
  1273. ;; reasonable.)
  1274. (let* ((log (get-output-string port))
  1275. (started (fold-matches
  1276. (make-regexp "@ build-started ([^ ]+) - ([^ ]+) ([^ ]+) ([0-9]+)")
  1277. log '() cons))
  1278. (done (fold-matches
  1279. (make-regexp "@ build-succeeded (.*) - (.*) (.*) (.*)")
  1280. log '() cons))
  1281. (output (fold-matches
  1282. (make-regexp "@ build-log ([[:digit:]]+) ([[:digit:]]+)\n([A-Za-z .*]+)\n")
  1283. log '() cons))
  1284. (drv-pid (lambda (name)
  1285. (lambda (m)
  1286. (let ((drv (match:substring m 1))
  1287. (pid (string->number
  1288. (match:substring m 4))))
  1289. (and (string-suffix? name drv) pid)))))
  1290. (pid-log (lambda (pid)
  1291. (lambda (m)
  1292. (let ((n (string->number
  1293. (match:substring m 1)))
  1294. (len (string->number
  1295. (match:substring m 2)))
  1296. (str (match:substring m 3)))
  1297. (and (= pid n)
  1298. (= (string-length str) (- len 1))
  1299. str)))))
  1300. (pid1 (any (drv-pid "one.drv") started))
  1301. (pid2 (any (drv-pid "two.drv") started)))
  1302. (list (any (pid-log pid1) output)
  1303. (any (pid-log pid2) output)))))))
  1304. (test-end "store")