tests.scm 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013-2022 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 (guix tests)
  19. #:use-module ((guix config) #:select (%storedir %localstatedir))
  20. #:use-module (guix store)
  21. #:use-module (guix derivations)
  22. #:use-module (guix gexp)
  23. #:use-module (guix packages)
  24. #:use-module (guix base32)
  25. #:use-module (guix serialization)
  26. #:use-module (guix monads)
  27. #:use-module ((guix utils) #:select (substitute-keyword-arguments))
  28. #:use-module ((guix build utils) #:select (mkdir-p compressor))
  29. #:use-module ((gcrypt hash) #:hide (sha256))
  30. #:use-module (guix build-system gnu)
  31. #:use-module (gnu packages base)
  32. #:use-module (gnu packages bootstrap)
  33. #:use-module (srfi srfi-26)
  34. #:use-module (srfi srfi-34)
  35. #:use-module (srfi srfi-64)
  36. #:use-module (rnrs bytevectors)
  37. #:use-module (ice-9 match)
  38. #:use-module (ice-9 binary-ports)
  39. #:use-module (web uri)
  40. #:export (open-connection-for-tests
  41. with-external-store
  42. %seed
  43. random-text
  44. random-bytevector
  45. file=?
  46. canonical-file?
  47. network-reachable?
  48. shebang-too-long?
  49. with-environment-variable
  50. search-bootstrap-binary
  51. mock
  52. %test-substitute-urls
  53. test-assertm
  54. test-equalm
  55. %substitute-directory
  56. with-derivation-narinfo
  57. with-derivation-substitute
  58. dummy-package
  59. dummy-origin
  60. gnu-make-for-tests
  61. test-file))
  62. ;;; Commentary:
  63. ;;;
  64. ;;; This module provide shared infrastructure for the test suite. For
  65. ;;; internal use only.
  66. ;;;
  67. ;;; Code:
  68. (define %test-substitute-urls
  69. ;; URLs where to look for substitutes during tests.
  70. (make-parameter
  71. (or (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL") list)
  72. '())))
  73. (define* (open-connection-for-tests #:optional (uri (%daemon-socket-uri)))
  74. "Open a connection to the build daemon for tests purposes and return it."
  75. (guard (c ((store-error? c)
  76. (format (current-error-port)
  77. "warning: build daemon error: ~s~%" c)
  78. #f))
  79. (let ((store (open-connection uri)))
  80. ;; Make sure we build everything by ourselves. When we build something,
  81. ;; it should take at most 5 minutes.
  82. (set-build-options store
  83. #:use-substitutes? #f
  84. #:substitute-urls (%test-substitute-urls)
  85. #:timeout (* 5 60))
  86. ;; Use the bootstrap Guile when running tests, so we don't end up
  87. ;; building everything in the temporary test store.
  88. (%guile-for-build (package-derivation store %bootstrap-guile))
  89. store)))
  90. (define (bootstrap-binary-file program system)
  91. "Return the absolute file name where bootstrap binary PROGRAM for SYSTEM is
  92. stored."
  93. (string-append (dirname (search-path %load-path
  94. "gnu/packages/bootstrap.scm"))
  95. "/bootstrap/" system "/" program))
  96. (define (search-bootstrap-binary file-name system)
  97. "Search the bootstrap binary FILE-NAME for SYSTEM. Raise an error if not
  98. found."
  99. ;; Note: Keep bootstrap binaries on the local file system so that the 'guix'
  100. ;; package can provide them as inputs and copy them to the right place.
  101. (let* ((system (match system
  102. ("x86_64-linux" "i686-linux")
  103. (_ system)))
  104. (file (bootstrap-binary-file file-name system)))
  105. (if (file-exists? file)
  106. file
  107. (with-store store
  108. (run-with-store store
  109. (mlet %store-monad ((drv (origin->derivation
  110. (bootstrap-executable file-name system))))
  111. (mbegin %store-monad
  112. (built-derivations (list drv))
  113. (begin
  114. (mkdir-p (dirname file))
  115. (copy-file (derivation->output-path drv) file)
  116. (return file)))))))))
  117. (define (call-with-external-store proc)
  118. "Call PROC with an open connection to the external store or #f it there is
  119. no external store to talk to."
  120. (parameterize ((%daemon-socket-uri
  121. (string-append %localstatedir
  122. "/guix/daemon-socket/socket"))
  123. (%store-prefix %storedir))
  124. (define store
  125. (catch #t
  126. (lambda ()
  127. (open-connection))
  128. (const #f)))
  129. (let ((store-variable (getenv "NIX_STORE_DIR")))
  130. (dynamic-wind
  131. (lambda ()
  132. ;; This environment variable is set by 'pre-inst-env' but it
  133. ;; influences '%store-directory' in (guix build utils), which is
  134. ;; itself used in (guix packages). Thus, unset it before going any
  135. ;; further.
  136. (unsetenv "NIX_STORE_DIR"))
  137. (lambda ()
  138. (when store
  139. ;; Make sure we don't end up rebuilding the world for those tests.
  140. (set-build-options store #:timeout (* 10 60)))
  141. (proc store))
  142. (lambda ()
  143. (when store-variable
  144. (setenv "NIX_STORE_DIR" store-variable))
  145. (when store
  146. (close-connection store)))))))
  147. (define-syntax-rule (with-external-store store exp ...)
  148. "Evaluate EXP with STORE bound to the external store rather than the
  149. temporary test store, or #f if there is no external store to talk to.
  150. This is meant to be used for tests that need to build packages that would be
  151. too expensive to build entirely in the test store."
  152. (call-with-external-store (lambda (store) exp ...)))
  153. (define (random-seed)
  154. (or (and=> (getenv "GUIX_TESTS_RANDOM_SEED")
  155. number->string)
  156. (logxor (getpid) (car (gettimeofday)))))
  157. (define (%seed)
  158. (let ((seed (random-seed)))
  159. (format (current-error-port) "random seed for tests: ~a~%"
  160. seed)
  161. (let ((result (seed->random-state seed)))
  162. (set! %seed (lambda () result))
  163. result)))
  164. (define (random-text)
  165. "Return the hexadecimal representation of a random number."
  166. (number->string (random (expt 2 256) (%seed)) 16))
  167. (define (random-bytevector n)
  168. "Return a random bytevector of N bytes."
  169. (let ((bv (make-bytevector n)))
  170. (let loop ((i 0))
  171. (if (< i n)
  172. (begin
  173. (bytevector-u8-set! bv i (random 256 (%seed)))
  174. (loop (1+ i)))
  175. bv))))
  176. (define* (file=? a b #:optional (stat lstat))
  177. "Return true if files A and B have the same type and same content. Call
  178. STAT to obtain file metadata."
  179. (let ((sta (stat a)) (stb (stat b)))
  180. (and (eq? (stat:type sta) (stat:type stb))
  181. (case (stat:type sta)
  182. ((regular)
  183. (or (and (= (stat:ino sta) (stat:ino stb))
  184. (= (stat:dev sta) (stat:dev stb)))
  185. (equal?
  186. (call-with-input-file a get-bytevector-all)
  187. (call-with-input-file b get-bytevector-all))))
  188. ((symlink)
  189. (string=? (readlink a) (readlink b)))
  190. (else
  191. (error "what?" (stat a)))))))
  192. (define (canonical-file? file)
  193. "Return #t if FILE is in the store, is read-only, and its mtime is 1."
  194. (let ((st (lstat file)))
  195. (or (not (string-prefix? (%store-prefix) file))
  196. (eq? 'symlink (stat:type st))
  197. (and (= 1 (stat:mtime st))
  198. (zero? (logand #o222 (stat:mode st)))))))
  199. (define (network-reachable?)
  200. "Return true if we can reach the Internet."
  201. (false-if-exception (getaddrinfo "www.gnu.org" "80" AI_NUMERICSERV)))
  202. (define-syntax-rule (mock (module proc replacement) body ...)
  203. "Within BODY, replace the definition of PROC from MODULE with the definition
  204. given by REPLACEMENT."
  205. (let* ((m (resolve-module 'module))
  206. (original (module-ref m 'proc)))
  207. (dynamic-wind
  208. (lambda () (module-set! m 'proc replacement))
  209. (lambda () body ...)
  210. (lambda () (module-set! m 'proc original)))))
  211. (define-syntax-rule (test-assertm name exp)
  212. "Like 'test-assert', but EXP is a monadic value. A new connection to the
  213. store is opened."
  214. (test-assert name
  215. (let ((store (open-connection-for-tests)))
  216. (dynamic-wind
  217. (const #t)
  218. (lambda ()
  219. (run-with-store store exp
  220. #:guile-for-build (%guile-for-build)))
  221. (lambda ()
  222. (close-connection store))))))
  223. (define-syntax-rule (test-equalm name value exp)
  224. "Like 'test-equal', but EXP is a monadic value. A new connection to the
  225. store is opened."
  226. (test-equal name
  227. value
  228. (with-store store
  229. (run-with-store store exp
  230. #:guile-for-build (%guile-for-build)))))
  231. (define-syntax-rule (with-environment-variable variable value body ...)
  232. "Run BODY with VARIABLE set to VALUE."
  233. (let ((orig (getenv variable)))
  234. (dynamic-wind
  235. (lambda ()
  236. (setenv variable value))
  237. (lambda ()
  238. body ...)
  239. (lambda ()
  240. (if orig
  241. (setenv variable orig)
  242. (unsetenv variable))))))
  243. ;;;
  244. ;;; Narinfo files, as used by the substituter.
  245. ;;;
  246. (define* (derivation-narinfo drv #:key (nar "example.nar")
  247. (sha256 (make-bytevector 32 0))
  248. (references '()))
  249. "Return the contents of the narinfo corresponding to DRV, with the specified
  250. REFERENCES (a list of store items); NAR should be the file name of the archive
  251. containing the substitute for DRV, and SHA256 is the expected hash."
  252. (format #f "StorePath: ~a
  253. URL: ~a
  254. Compression: none
  255. NarSize: 1234
  256. NarHash: sha256:~a
  257. References: ~a
  258. System: ~a
  259. Deriver: ~a~%"
  260. (derivation->output-path drv) ; StorePath
  261. nar ; URL
  262. (bytevector->nix-base32-string sha256) ; NarHash
  263. (string-join (map basename references)) ; References
  264. (derivation-system drv) ; System
  265. (basename
  266. (derivation-file-name drv)))) ; Deriver
  267. (define %substitute-directory
  268. (make-parameter
  269. (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL")
  270. (compose uri-path string->uri))))
  271. (define* (call-with-derivation-narinfo drv thunk
  272. #:key
  273. (sha256 (make-bytevector 32 0))
  274. (references '()))
  275. "Call THUNK in a context where fake substituter data, as read by 'guix
  276. substitute', has been installed for DRV. SHA256 is the hash of the
  277. expected output of DRV."
  278. (let* ((output (derivation->output-path drv))
  279. (dir (%substitute-directory))
  280. (info (string-append dir "/nix-cache-info"))
  281. (narinfo (string-append dir "/" (store-path-hash-part output)
  282. ".narinfo")))
  283. (dynamic-wind
  284. (lambda ()
  285. (call-with-output-file info
  286. (lambda (p)
  287. (format p "StoreDir: ~a\nWantMassQuery: 0\n"
  288. (%store-prefix))))
  289. (call-with-output-file narinfo
  290. (lambda (p)
  291. (display (derivation-narinfo drv #:sha256 sha256
  292. #:references references)
  293. p))))
  294. thunk
  295. (lambda ()
  296. (delete-file narinfo)
  297. (delete-file info)))))
  298. (define-syntax with-derivation-narinfo
  299. (syntax-rules (sha256 references =>)
  300. "Evaluate BODY in a context where DRV looks substitutable from the
  301. substituter's viewpoint."
  302. ((_ drv (sha256 => hash) (references => refs) body ...)
  303. (call-with-derivation-narinfo drv
  304. (lambda () body ...)
  305. #:sha256 hash
  306. #:references refs))
  307. ((_ drv (sha256 => hash) body ...)
  308. (with-derivation-narinfo drv
  309. (sha256 => hash) (references => '())
  310. body ...))
  311. ((_ drv body ...)
  312. (call-with-derivation-narinfo drv
  313. (lambda ()
  314. body ...)))))
  315. (define* (call-with-derivation-substitute drv contents thunk
  316. #:key
  317. sha256
  318. (references '()))
  319. "Call THUNK in a context where a substitute for DRV has been installed,
  320. using CONTENTS, a string, as its contents. If SHA256 is true, use it as the
  321. expected hash of the substitute; otherwise use the hash of the nar containing
  322. CONTENTS."
  323. (define dir (%substitute-directory))
  324. (dynamic-wind
  325. (lambda ()
  326. (call-with-output-file (string-append dir "/example.out")
  327. (lambda (port)
  328. (display contents port)))
  329. (call-with-output-file (string-append dir "/example.nar")
  330. (lambda (p)
  331. (write-file (string-append dir "/example.out") p))))
  332. (lambda ()
  333. (let ((hash (call-with-input-file (string-append dir "/example.nar")
  334. port-sha256)))
  335. ;; Create fake substituter data, to be read by 'guix substitute'.
  336. (call-with-derivation-narinfo drv
  337. thunk
  338. #:sha256 (or sha256 hash)
  339. #:references references)))
  340. (lambda ()
  341. (delete-file (string-append dir "/example.out"))
  342. (delete-file (string-append dir "/example.nar")))))
  343. (define (shebang-too-long?)
  344. "Return true if the typical shebang in the current store would exceed
  345. Linux's static limit---the BINPRM_BUF_SIZE constant, normally 128 characters
  346. all included."
  347. (define shebang
  348. (string-append "#!" (%store-prefix) "/"
  349. (make-string 32 #\a)
  350. "-bootstrap-binaries-0/bin/bash\0"))
  351. (> (string-length shebang) 128))
  352. (define-syntax with-derivation-substitute
  353. (syntax-rules (sha256 references =>)
  354. "Evaluate BODY in a context where DRV is substitutable with the given
  355. CONTENTS."
  356. ((_ drv contents (sha256 => hash) (references => refs) body ...)
  357. (call-with-derivation-substitute drv contents
  358. (lambda () body ...)
  359. #:sha256 hash
  360. #:references refs))
  361. ((_ drv contents (sha256 => hash) body ...)
  362. (with-derivation-substitute drv contents
  363. (sha256 => hash) (references => '())
  364. body ...))
  365. ((_ drv contents body ...)
  366. (call-with-derivation-substitute drv contents
  367. (lambda ()
  368. body ...)))))
  369. (define-syntax-rule (dummy-package name* extra-fields ...)
  370. "Return a \"dummy\" package called NAME*, with all its compulsory fields
  371. initialized with default values, and with EXTRA-FIELDS set as specified."
  372. (let ((p (package
  373. (name name*) (version "0") (source #f)
  374. (build-system gnu-build-system)
  375. (synopsis #f) (description #f)
  376. (home-page #f) (license #f))))
  377. (package (inherit p) extra-fields ...)))
  378. (define-syntax-rule (dummy-origin extra-fields ...)
  379. "Return a \"dummy\" origin, with all its compulsory fields initialized with
  380. default values, and with EXTRA-FIELDS set as specified."
  381. (let ((o (origin (method #f) (uri "http://www.example.com")
  382. (sha256 (base32 (make-string 52 #\x))))))
  383. (origin (inherit o) extra-fields ...)))
  384. (define gnu-make-for-tests
  385. ;; This is a variant of 'gnu-make-boot0' that can be built with minimal
  386. ;; resources.
  387. (package-with-bootstrap-guile
  388. (package
  389. (inherit gnu-make)
  390. (name "make-test-boot0")
  391. (arguments
  392. `(#:guile ,%bootstrap-guile
  393. #:implicit-inputs? #f
  394. #:tests? #f ;cannot run "make check"
  395. ,@(substitute-keyword-arguments (package-arguments gnu-make)
  396. ((#:configure-flags flags ''())
  397. ;; As in 'gnu-make-boot0', work around a 'config.status' defect.
  398. `(cons "--disable-dependency-tracking" ,flags))
  399. ((#:phases phases)
  400. `(modify-phases ,phases
  401. (replace 'build
  402. (lambda _
  403. (invoke "./build.sh")
  404. #t))
  405. (replace 'install
  406. (lambda* (#:key outputs #:allow-other-keys)
  407. (let* ((out (assoc-ref outputs "out"))
  408. (bin (string-append out "/bin")))
  409. (install-file "make" bin)
  410. #t))))))))
  411. (native-inputs '()) ;no need for 'pkg-config'
  412. (inputs %bootstrap-inputs-for-tests))))
  413. ;;;
  414. ;;; Test utility procedures.
  415. (define (test-file store name content)
  416. "Create a simple file in STORE with CONTENT (a string), compressed according
  417. to its file name extension. Return both its file name and its hash."
  418. (let* ((ext (string-index-right name #\.))
  419. (name-sans-ext (if ext
  420. (string-take name (string-index-right name #\.))
  421. name))
  422. (comp (compressor name))
  423. (command #~(if #+comp
  424. (string-append #+%bootstrap-coreutils&co
  425. "/bin/" #+comp)
  426. #f))
  427. (f (with-imported-modules '((guix build utils))
  428. (computed-file name
  429. #~(begin
  430. (use-modules (guix build utils)
  431. (rnrs io simple))
  432. (with-output-to-file #+name-sans-ext
  433. (lambda _
  434. (format #t #+content)))
  435. (when #+command
  436. (invoke #+command #+name-sans-ext))
  437. (copy-file #+name #$output))
  438. #:guile %bootstrap-guile)))
  439. (file-drv (run-with-store store (lower-object f)))
  440. (file (derivation->output-path file-drv))
  441. (file-drv-outputs (derivation-outputs file-drv))
  442. (_ (build-derivations store (list file-drv)))
  443. (file-hash (derivation-output-hash
  444. (assoc-ref file-drv-outputs "out"))))
  445. (values file file-hash)))
  446. ;;;
  447. ;; Local Variables:
  448. ;; eval: (put 'call-with-derivation-narinfo 'scheme-indent-function 1)
  449. ;; eval: (put 'call-with-derivation-substitute 'scheme-indent-function 2)
  450. ;; End:
  451. ;;; tests.scm ends here