tests.scm 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. (seed->random-state seed)))
  150. (define (random-text)
  151. "Return the hexadecimal representation of a random number."
  152. (number->string (random (expt 2 256) %seed) 16))
  153. (define (random-bytevector n)
  154. "Return a random bytevector of N bytes."
  155. (let ((bv (make-bytevector n)))
  156. (let loop ((i 0))
  157. (if (< i n)
  158. (begin
  159. (bytevector-u8-set! bv i (random 256 %seed))
  160. (loop (1+ i)))
  161. bv))))
  162. (define (file=? a b)
  163. "Return true if files A and B have the same type and same content."
  164. (and (eq? (stat:type (lstat a)) (stat:type (lstat b)))
  165. (case (stat:type (lstat a))
  166. ((regular)
  167. (equal?
  168. (call-with-input-file a get-bytevector-all)
  169. (call-with-input-file b get-bytevector-all)))
  170. ((symlink)
  171. (string=? (readlink a) (readlink b)))
  172. (else
  173. (error "what?" (lstat a))))))
  174. (define (canonical-file? file)
  175. "Return #t if FILE is in the store, is read-only, and its mtime is 1."
  176. (let ((st (lstat file)))
  177. (or (not (string-prefix? (%store-prefix) file))
  178. (eq? 'symlink (stat:type st))
  179. (and (= 1 (stat:mtime st))
  180. (zero? (logand #o222 (stat:mode st)))))))
  181. (define (network-reachable?)
  182. "Return true if we can reach the Internet."
  183. (false-if-exception (getaddrinfo "www.gnu.org" "80" AI_NUMERICSERV)))
  184. (define-syntax-rule (mock (module proc replacement) body ...)
  185. "Within BODY, replace the definition of PROC from MODULE with the definition
  186. given by REPLACEMENT."
  187. (let* ((m (resolve-module 'module))
  188. (original (module-ref m 'proc)))
  189. (dynamic-wind
  190. (lambda () (module-set! m 'proc replacement))
  191. (lambda () body ...)
  192. (lambda () (module-set! m 'proc original)))))
  193. (define-syntax-rule (test-assertm name exp)
  194. "Like 'test-assert', but EXP is a monadic value. A new connection to the
  195. store is opened."
  196. (test-assert name
  197. (let ((store (open-connection-for-tests)))
  198. (dynamic-wind
  199. (const #t)
  200. (lambda ()
  201. (run-with-store store exp
  202. #:guile-for-build (%guile-for-build)))
  203. (lambda ()
  204. (close-connection store))))))
  205. (define-syntax-rule (test-equalm name value exp)
  206. "Like 'test-equal', but EXP is a monadic value. A new connection to the
  207. store is opened."
  208. (test-equal name
  209. value
  210. (with-store store
  211. (run-with-store store exp
  212. #:guile-for-build (%guile-for-build)))))
  213. (define-syntax-rule (with-environment-variable variable value body ...)
  214. "Run BODY with VARIABLE set to VALUE."
  215. (let ((orig (getenv variable)))
  216. (dynamic-wind
  217. (lambda ()
  218. (setenv variable value))
  219. (lambda ()
  220. body ...)
  221. (lambda ()
  222. (if orig
  223. (setenv variable orig)
  224. (unsetenv variable))))))
  225. ;;;
  226. ;;; Narinfo files, as used by the substituter.
  227. ;;;
  228. (define* (derivation-narinfo drv #:key (nar "example.nar")
  229. (sha256 (make-bytevector 32 0))
  230. (references '()))
  231. "Return the contents of the narinfo corresponding to DRV, with the specified
  232. REFERENCES (a list of store items); NAR should be the file name of the archive
  233. containing the substitute for DRV, and SHA256 is the expected hash."
  234. (format #f "StorePath: ~a
  235. URL: ~a
  236. Compression: none
  237. NarSize: 1234
  238. NarHash: sha256:~a
  239. References: ~a
  240. System: ~a
  241. Deriver: ~a~%"
  242. (derivation->output-path drv) ; StorePath
  243. nar ; URL
  244. (bytevector->nix-base32-string sha256) ; NarHash
  245. (string-join (map basename references)) ; References
  246. (derivation-system drv) ; System
  247. (basename
  248. (derivation-file-name drv)))) ; Deriver
  249. (define %substitute-directory
  250. (make-parameter
  251. (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL")
  252. (compose uri-path string->uri))))
  253. (define* (call-with-derivation-narinfo drv thunk
  254. #:key
  255. (sha256 (make-bytevector 32 0))
  256. (references '()))
  257. "Call THUNK in a context where fake substituter data, as read by 'guix
  258. substitute', has been installed for DRV. SHA256 is the hash of the
  259. expected output of DRV."
  260. (let* ((output (derivation->output-path drv))
  261. (dir (%substitute-directory))
  262. (info (string-append dir "/nix-cache-info"))
  263. (narinfo (string-append dir "/" (store-path-hash-part output)
  264. ".narinfo")))
  265. (dynamic-wind
  266. (lambda ()
  267. (call-with-output-file info
  268. (lambda (p)
  269. (format p "StoreDir: ~a\nWantMassQuery: 0\n"
  270. (%store-prefix))))
  271. (call-with-output-file narinfo
  272. (lambda (p)
  273. (display (derivation-narinfo drv #:sha256 sha256
  274. #:references references)
  275. p))))
  276. thunk
  277. (lambda ()
  278. (delete-file narinfo)
  279. (delete-file info)))))
  280. (define-syntax with-derivation-narinfo
  281. (syntax-rules (sha256 references =>)
  282. "Evaluate BODY in a context where DRV looks substitutable from the
  283. substituter's viewpoint."
  284. ((_ drv (sha256 => hash) (references => refs) body ...)
  285. (call-with-derivation-narinfo drv
  286. (lambda () body ...)
  287. #:sha256 hash
  288. #:references refs))
  289. ((_ drv (sha256 => hash) body ...)
  290. (with-derivation-narinfo drv
  291. (sha256 => hash) (references => '())
  292. body ...))
  293. ((_ drv body ...)
  294. (call-with-derivation-narinfo drv
  295. (lambda ()
  296. body ...)))))
  297. (define* (call-with-derivation-substitute drv contents thunk
  298. #:key
  299. sha256
  300. (references '()))
  301. "Call THUNK in a context where a substitute for DRV has been installed,
  302. using CONTENTS, a string, as its contents. If SHA256 is true, use it as the
  303. expected hash of the substitute; otherwise use the hash of the nar containing
  304. CONTENTS."
  305. (define dir (%substitute-directory))
  306. (dynamic-wind
  307. (lambda ()
  308. (call-with-output-file (string-append dir "/example.out")
  309. (lambda (port)
  310. (display contents port)))
  311. (call-with-output-file (string-append dir "/example.nar")
  312. (lambda (p)
  313. (write-file (string-append dir "/example.out") p))))
  314. (lambda ()
  315. (let ((hash (call-with-input-file (string-append dir "/example.nar")
  316. port-sha256)))
  317. ;; Create fake substituter data, to be read by 'guix substitute'.
  318. (call-with-derivation-narinfo drv
  319. thunk
  320. #:sha256 (or sha256 hash)
  321. #:references references)))
  322. (lambda ()
  323. (delete-file (string-append dir "/example.out"))
  324. (delete-file (string-append dir "/example.nar")))))
  325. (define (shebang-too-long?)
  326. "Return true if the typical shebang in the current store would exceed
  327. Linux's static limit---the BINPRM_BUF_SIZE constant, normally 128 characters
  328. all included."
  329. (define shebang
  330. (string-append "#!" (%store-prefix) "/"
  331. (make-string 32 #\a)
  332. "-bootstrap-binaries-0/bin/bash\0"))
  333. (> (string-length shebang) 128))
  334. (define-syntax with-derivation-substitute
  335. (syntax-rules (sha256 references =>)
  336. "Evaluate BODY in a context where DRV is substitutable with the given
  337. CONTENTS."
  338. ((_ drv contents (sha256 => hash) (references => refs) body ...)
  339. (call-with-derivation-substitute drv contents
  340. (lambda () body ...)
  341. #:sha256 hash
  342. #:references refs))
  343. ((_ drv contents (sha256 => hash) body ...)
  344. (with-derivation-substitute drv contents
  345. (sha256 => hash) (references => '())
  346. body ...))
  347. ((_ drv contents body ...)
  348. (call-with-derivation-substitute drv contents
  349. (lambda ()
  350. body ...)))))
  351. (define-syntax-rule (dummy-package name* extra-fields ...)
  352. "Return a \"dummy\" package called NAME*, with all its compulsory fields
  353. initialized with default values, and with EXTRA-FIELDS set as specified."
  354. (let ((p (package
  355. (name name*) (version "0") (source #f)
  356. (build-system gnu-build-system)
  357. (synopsis #f) (description #f)
  358. (home-page #f) (license #f))))
  359. (package (inherit p) extra-fields ...)))
  360. (define-syntax-rule (dummy-origin extra-fields ...)
  361. "Return a \"dummy\" origin, with all its compulsory fields initialized with
  362. default values, and with EXTRA-FIELDS set as specified."
  363. (let ((o (origin (method #f) (uri "http://www.example.com")
  364. (sha256 (base32 (make-string 52 #\x))))))
  365. (origin (inherit o) extra-fields ...)))
  366. (define gnu-make-for-tests
  367. ;; This is a variant of 'gnu-make-boot0' that can be built with minimal
  368. ;; resources.
  369. (package-with-bootstrap-guile
  370. (package
  371. (inherit gnu-make)
  372. (name "make-test-boot0")
  373. (arguments
  374. `(#:guile ,%bootstrap-guile
  375. #:implicit-inputs? #f
  376. #:tests? #f ;cannot run "make check"
  377. ,@(substitute-keyword-arguments (package-arguments gnu-make)
  378. ((#:configure-flags flags ''())
  379. ;; As in 'gnu-make-boot0', work around a 'config.status' defect.
  380. `(cons "--disable-dependency-tracking" ,flags))
  381. ((#:phases phases)
  382. `(modify-phases ,phases
  383. (replace 'build
  384. (lambda _
  385. (invoke "./build.sh")
  386. #t))
  387. (replace 'install
  388. (lambda* (#:key outputs #:allow-other-keys)
  389. (let* ((out (assoc-ref outputs "out"))
  390. (bin (string-append out "/bin")))
  391. (install-file "make" bin)
  392. #t))))))))
  393. (native-inputs '()) ;no need for 'pkg-config'
  394. (inputs %bootstrap-inputs-for-tests))))
  395. ;; Local Variables:
  396. ;; eval: (put 'call-with-derivation-narinfo 'scheme-indent-function 1)
  397. ;; eval: (put 'call-with-derivation-substitute 'scheme-indent-function 2)
  398. ;; End:
  399. ;;; tests.scm ends here