tests.scm 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (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. (dynamic-wind
  128. (const #t)
  129. (lambda ()
  130. ;; Since we're using a different store we must clear the
  131. ;; package-derivation cache.
  132. (hash-clear! (@@ (guix packages) %derivation-cache))
  133. (proc store))
  134. (lambda ()
  135. (when store
  136. (close-connection store))))))
  137. (define-syntax-rule (with-external-store store exp ...)
  138. "Evaluate EXP with STORE bound to the external store rather than the
  139. temporary test store, or #f if there is no external store to talk to.
  140. This is meant to be used for tests that need to build packages that would be
  141. too expensive to build entirely in the test store."
  142. (call-with-external-store (lambda (store) exp ...)))
  143. (define (random-seed)
  144. (or (and=> (getenv "GUIX_TESTS_RANDOM_SEED")
  145. number->string)
  146. (logxor (getpid) (car (gettimeofday)))))
  147. (define (%seed)
  148. (let ((seed (random-seed)))
  149. (format (current-error-port) "random seed for tests: ~a~%"
  150. seed)
  151. (let ((result (seed->random-state seed)))
  152. (set! %seed (lambda () result))
  153. result)))
  154. (define (random-text)
  155. "Return the hexadecimal representation of a random number."
  156. (number->string (random (expt 2 256) (%seed)) 16))
  157. (define (random-bytevector n)
  158. "Return a random bytevector of N bytes."
  159. (let ((bv (make-bytevector n)))
  160. (let loop ((i 0))
  161. (if (< i n)
  162. (begin
  163. (bytevector-u8-set! bv i (random 256 (%seed)))
  164. (loop (1+ i)))
  165. bv))))
  166. (define (file=? a b)
  167. "Return true if files A and B have the same type and same content."
  168. (and (eq? (stat:type (lstat a)) (stat:type (lstat b)))
  169. (case (stat:type (lstat a))
  170. ((regular)
  171. (equal?
  172. (call-with-input-file a get-bytevector-all)
  173. (call-with-input-file b get-bytevector-all)))
  174. ((symlink)
  175. (string=? (readlink a) (readlink b)))
  176. (else
  177. (error "what?" (lstat a))))))
  178. (define (canonical-file? file)
  179. "Return #t if FILE is in the store, is read-only, and its mtime is 1."
  180. (let ((st (lstat file)))
  181. (or (not (string-prefix? (%store-prefix) file))
  182. (eq? 'symlink (stat:type st))
  183. (and (= 1 (stat:mtime st))
  184. (zero? (logand #o222 (stat:mode st)))))))
  185. (define (network-reachable?)
  186. "Return true if we can reach the Internet."
  187. (false-if-exception (getaddrinfo "www.gnu.org" "80" AI_NUMERICSERV)))
  188. (define-syntax-rule (mock (module proc replacement) body ...)
  189. "Within BODY, replace the definition of PROC from MODULE with the definition
  190. given by REPLACEMENT."
  191. (let* ((m (resolve-module 'module))
  192. (original (module-ref m 'proc)))
  193. (dynamic-wind
  194. (lambda () (module-set! m 'proc replacement))
  195. (lambda () body ...)
  196. (lambda () (module-set! m 'proc original)))))
  197. (define-syntax-rule (test-assertm name exp)
  198. "Like 'test-assert', but EXP is a monadic value. A new connection to the
  199. store is opened."
  200. (test-assert name
  201. (let ((store (open-connection-for-tests)))
  202. (dynamic-wind
  203. (const #t)
  204. (lambda ()
  205. (run-with-store store exp
  206. #:guile-for-build (%guile-for-build)))
  207. (lambda ()
  208. (close-connection store))))))
  209. (define-syntax-rule (test-equalm name value exp)
  210. "Like 'test-equal', but EXP is a monadic value. A new connection to the
  211. store is opened."
  212. (test-equal name
  213. value
  214. (with-store store
  215. (run-with-store store exp
  216. #:guile-for-build (%guile-for-build)))))
  217. (define-syntax-rule (with-environment-variable variable value body ...)
  218. "Run BODY with VARIABLE set to VALUE."
  219. (let ((orig (getenv variable)))
  220. (dynamic-wind
  221. (lambda ()
  222. (setenv variable value))
  223. (lambda ()
  224. body ...)
  225. (lambda ()
  226. (if orig
  227. (setenv variable orig)
  228. (unsetenv variable))))))
  229. ;;;
  230. ;;; Narinfo files, as used by the substituter.
  231. ;;;
  232. (define* (derivation-narinfo drv #:key (nar "example.nar")
  233. (sha256 (make-bytevector 32 0))
  234. (references '()))
  235. "Return the contents of the narinfo corresponding to DRV, with the specified
  236. REFERENCES (a list of store items); NAR should be the file name of the archive
  237. containing the substitute for DRV, and SHA256 is the expected hash."
  238. (format #f "StorePath: ~a
  239. URL: ~a
  240. Compression: none
  241. NarSize: 1234
  242. NarHash: sha256:~a
  243. References: ~a
  244. System: ~a
  245. Deriver: ~a~%"
  246. (derivation->output-path drv) ; StorePath
  247. nar ; URL
  248. (bytevector->nix-base32-string sha256) ; NarHash
  249. (string-join (map basename references)) ; References
  250. (derivation-system drv) ; System
  251. (basename
  252. (derivation-file-name drv)))) ; Deriver
  253. (define %substitute-directory
  254. (make-parameter
  255. (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL")
  256. (compose uri-path string->uri))))
  257. (define* (call-with-derivation-narinfo drv thunk
  258. #:key
  259. (sha256 (make-bytevector 32 0))
  260. (references '()))
  261. "Call THUNK in a context where fake substituter data, as read by 'guix
  262. substitute', has been installed for DRV. SHA256 is the hash of the
  263. expected output of DRV."
  264. (let* ((output (derivation->output-path drv))
  265. (dir (%substitute-directory))
  266. (info (string-append dir "/nix-cache-info"))
  267. (narinfo (string-append dir "/" (store-path-hash-part output)
  268. ".narinfo")))
  269. (dynamic-wind
  270. (lambda ()
  271. (call-with-output-file info
  272. (lambda (p)
  273. (format p "StoreDir: ~a\nWantMassQuery: 0\n"
  274. (%store-prefix))))
  275. (call-with-output-file narinfo
  276. (lambda (p)
  277. (display (derivation-narinfo drv #:sha256 sha256
  278. #:references references)
  279. p))))
  280. thunk
  281. (lambda ()
  282. (delete-file narinfo)
  283. (delete-file info)))))
  284. (define-syntax with-derivation-narinfo
  285. (syntax-rules (sha256 references =>)
  286. "Evaluate BODY in a context where DRV looks substitutable from the
  287. substituter's viewpoint."
  288. ((_ drv (sha256 => hash) (references => refs) body ...)
  289. (call-with-derivation-narinfo drv
  290. (lambda () body ...)
  291. #:sha256 hash
  292. #:references refs))
  293. ((_ drv (sha256 => hash) body ...)
  294. (with-derivation-narinfo drv
  295. (sha256 => hash) (references => '())
  296. body ...))
  297. ((_ drv body ...)
  298. (call-with-derivation-narinfo drv
  299. (lambda ()
  300. body ...)))))
  301. (define* (call-with-derivation-substitute drv contents thunk
  302. #:key
  303. sha256
  304. (references '()))
  305. "Call THUNK in a context where a substitute for DRV has been installed,
  306. using CONTENTS, a string, as its contents. If SHA256 is true, use it as the
  307. expected hash of the substitute; otherwise use the hash of the nar containing
  308. CONTENTS."
  309. (define dir (%substitute-directory))
  310. (dynamic-wind
  311. (lambda ()
  312. (call-with-output-file (string-append dir "/example.out")
  313. (lambda (port)
  314. (display contents port)))
  315. (call-with-output-file (string-append dir "/example.nar")
  316. (lambda (p)
  317. (write-file (string-append dir "/example.out") p))))
  318. (lambda ()
  319. (let ((hash (call-with-input-file (string-append dir "/example.nar")
  320. port-sha256)))
  321. ;; Create fake substituter data, to be read by 'guix substitute'.
  322. (call-with-derivation-narinfo drv
  323. thunk
  324. #:sha256 (or sha256 hash)
  325. #:references references)))
  326. (lambda ()
  327. (delete-file (string-append dir "/example.out"))
  328. (delete-file (string-append dir "/example.nar")))))
  329. (define (shebang-too-long?)
  330. "Return true if the typical shebang in the current store would exceed
  331. Linux's static limit---the BINPRM_BUF_SIZE constant, normally 128 characters
  332. all included."
  333. (define shebang
  334. (string-append "#!" (%store-prefix) "/"
  335. (make-string 32 #\a)
  336. "-bootstrap-binaries-0/bin/bash\0"))
  337. (> (string-length shebang) 128))
  338. (define-syntax with-derivation-substitute
  339. (syntax-rules (sha256 references =>)
  340. "Evaluate BODY in a context where DRV is substitutable with the given
  341. CONTENTS."
  342. ((_ drv contents (sha256 => hash) (references => refs) body ...)
  343. (call-with-derivation-substitute drv contents
  344. (lambda () body ...)
  345. #:sha256 hash
  346. #:references refs))
  347. ((_ drv contents (sha256 => hash) body ...)
  348. (with-derivation-substitute drv contents
  349. (sha256 => hash) (references => '())
  350. body ...))
  351. ((_ drv contents body ...)
  352. (call-with-derivation-substitute drv contents
  353. (lambda ()
  354. body ...)))))
  355. (define-syntax-rule (dummy-package name* extra-fields ...)
  356. "Return a \"dummy\" package called NAME*, with all its compulsory fields
  357. initialized with default values, and with EXTRA-FIELDS set as specified."
  358. (let ((p (package
  359. (name name*) (version "0") (source #f)
  360. (build-system gnu-build-system)
  361. (synopsis #f) (description #f)
  362. (home-page #f) (license #f))))
  363. (package (inherit p) extra-fields ...)))
  364. (define-syntax-rule (dummy-origin extra-fields ...)
  365. "Return a \"dummy\" origin, with all its compulsory fields initialized with
  366. default values, and with EXTRA-FIELDS set as specified."
  367. (let ((o (origin (method #f) (uri "http://www.example.com")
  368. (sha256 (base32 (make-string 52 #\x))))))
  369. (origin (inherit o) extra-fields ...)))
  370. (define gnu-make-for-tests
  371. ;; This is a variant of 'gnu-make-boot0' that can be built with minimal
  372. ;; resources.
  373. (package-with-bootstrap-guile
  374. (package
  375. (inherit gnu-make)
  376. (name "make-test-boot0")
  377. (arguments
  378. `(#:guile ,%bootstrap-guile
  379. #:implicit-inputs? #f
  380. #:tests? #f ;cannot run "make check"
  381. ,@(substitute-keyword-arguments (package-arguments gnu-make)
  382. ((#:configure-flags flags ''())
  383. ;; As in 'gnu-make-boot0', work around a 'config.status' defect.
  384. `(cons "--disable-dependency-tracking" ,flags))
  385. ((#:phases phases)
  386. `(modify-phases ,phases
  387. (replace 'build
  388. (lambda _
  389. (invoke "./build.sh")
  390. #t))
  391. (replace 'install
  392. (lambda* (#:key outputs #:allow-other-keys)
  393. (let* ((out (assoc-ref outputs "out"))
  394. (bin (string-append out "/bin")))
  395. (install-file "make" bin)
  396. #t))))))))
  397. (native-inputs '()) ;no need for 'pkg-config'
  398. (inputs %bootstrap-inputs-for-tests))))
  399. ;;;
  400. ;;; Test utility procedures.
  401. (define (test-file store name content)
  402. "Create a simple file in STORE with CONTENT (a string), compressed according
  403. to its file name extension. Return both its file name and its hash."
  404. (let* ((ext (string-index-right name #\.))
  405. (name-sans-ext (if ext
  406. (string-take name (string-index-right name #\.))
  407. name))
  408. (comp (compressor name))
  409. (command #~(if #+comp
  410. (string-append #+%bootstrap-coreutils&co
  411. "/bin/" #+comp)
  412. #f))
  413. (f (with-imported-modules '((guix build utils))
  414. (computed-file name
  415. #~(begin
  416. (use-modules (guix build utils)
  417. (rnrs io simple))
  418. (with-output-to-file #+name-sans-ext
  419. (lambda _
  420. (format #t #+content)))
  421. (when #+command
  422. (invoke #+command #+name-sans-ext))
  423. (copy-file #+name #$output)))))
  424. (file-drv (run-with-store store (lower-object f)))
  425. (file (derivation->output-path file-drv))
  426. (file-drv-outputs (derivation-outputs file-drv))
  427. (_ (build-derivations store (list file-drv)))
  428. (file-hash (derivation-output-hash
  429. (assoc-ref file-drv-outputs "out"))))
  430. (values file file-hash)))
  431. ;;;
  432. ;; Local Variables:
  433. ;; eval: (put 'call-with-derivation-narinfo 'scheme-indent-function 1)
  434. ;; eval: (put 'call-with-derivation-substitute 'scheme-indent-function 2)
  435. ;; End:
  436. ;;; tests.scm ends here