tests.scm 16 KB

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