tests.scm 18 KB

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