You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

287 lines
10 KiB

  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015, 2016, 2017 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 store)
  20. #:use-module (guix derivations)
  21. #:use-module (guix packages)
  22. #:use-module (guix base32)
  23. #:use-module (guix serialization)
  24. #:use-module (gcrypt hash)
  25. #:use-module (guix build-system gnu)
  26. #:use-module (gnu packages bootstrap)
  27. #:use-module (srfi srfi-34)
  28. #:use-module (rnrs bytevectors)
  29. #:use-module (ice-9 binary-ports)
  30. #:use-module (web uri)
  31. #:export (open-connection-for-tests
  32. random-text
  33. random-bytevector
  34. file=?
  35. network-reachable?
  36. shebang-too-long?
  37. mock
  38. %test-substitute-urls
  39. %substitute-directory
  40. with-derivation-narinfo
  41. with-derivation-substitute
  42. dummy-package
  43. dummy-origin))
  44. ;;; Commentary:
  45. ;;;
  46. ;;; This module provide shared infrastructure for the test suite. For
  47. ;;; internal use only.
  48. ;;;
  49. ;;; Code:
  50. (define %test-substitute-urls
  51. ;; URLs where to look for substitutes during tests.
  52. (make-parameter
  53. (or (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL") list)
  54. '())))
  55. (define* (open-connection-for-tests #:optional (uri (%daemon-socket-uri)))
  56. "Open a connection to the build daemon for tests purposes and return it."
  57. (guard (c ((nix-error? c)
  58. (format (current-error-port)
  59. "warning: build daemon error: ~s~%" c)
  60. #f))
  61. (let ((store (open-connection uri)))
  62. ;; Make sure we build everything by ourselves.
  63. (set-build-options store
  64. #:use-substitutes? #f
  65. #:substitute-urls (%test-substitute-urls))
  66. ;; Use the bootstrap Guile when running tests, so we don't end up
  67. ;; building everything in the temporary test store.
  68. (%guile-for-build (package-derivation store %bootstrap-guile))
  69. store)))
  70. (define (random-seed)
  71. (or (and=> (getenv "GUIX_TESTS_RANDOM_SEED")
  72. number->string)
  73. (logxor (getpid) (car (gettimeofday)))))
  74. (define %seed
  75. (let ((seed (random-seed)))
  76. (format (current-error-port) "random seed for tests: ~a~%"
  77. seed)
  78. (seed->random-state seed)))
  79. (define (random-text)
  80. "Return the hexadecimal representation of a random number."
  81. (number->string (random (expt 2 256) %seed) 16))
  82. (define (random-bytevector n)
  83. "Return a random bytevector of N bytes."
  84. (let ((bv (make-bytevector n)))
  85. (let loop ((i 0))
  86. (if (< i n)
  87. (begin
  88. (bytevector-u8-set! bv i (random 256 %seed))
  89. (loop (1+ i)))
  90. bv))))
  91. (define (file=? a b)
  92. "Return true if files A and B have the same type and same content."
  93. (and (eq? (stat:type (lstat a)) (stat:type (lstat b)))
  94. (case (stat:type (lstat a))
  95. ((regular)
  96. (equal?
  97. (call-with-input-file a get-bytevector-all)
  98. (call-with-input-file b get-bytevector-all)))
  99. ((symlink)
  100. (string=? (readlink a) (readlink b)))
  101. (else
  102. (error "what?" (lstat a))))))
  103. (define (network-reachable?)
  104. "Return true if we can reach the Internet."
  105. (false-if-exception (getaddrinfo "www.gnu.org" "80" AI_NUMERICSERV)))
  106. (define-syntax-rule (mock (module proc replacement) body ...)
  107. "Within BODY, replace the definition of PROC from MODULE with the definition
  108. given by REPLACEMENT."
  109. (let* ((m (resolve-module 'module))
  110. (original (module-ref m 'proc)))
  111. (dynamic-wind
  112. (lambda () (module-set! m 'proc replacement))
  113. (lambda () body ...)
  114. (lambda () (module-set! m 'proc original)))))
  115. ;;;
  116. ;;; Narinfo files, as used by the substituter.
  117. ;;;
  118. (define* (derivation-narinfo drv #:key (nar "example.nar")
  119. (sha256 (make-bytevector 32 0))
  120. (references '()))
  121. "Return the contents of the narinfo corresponding to DRV, with the specified
  122. REFERENCES (a list of store items); NAR should be the file name of the archive
  123. containing the substitute for DRV, and SHA256 is the expected hash."
  124. (format #f "StorePath: ~a
  125. URL: ~a
  126. Compression: none
  127. NarSize: 1234
  128. NarHash: sha256:~a
  129. References: ~a
  130. System: ~a
  131. Deriver: ~a~%"
  132. (derivation->output-path drv) ; StorePath
  133. nar ; URL
  134. (bytevector->nix-base32-string sha256) ; NarHash
  135. (string-join (map basename references)) ; References
  136. (derivation-system drv) ; System
  137. (basename
  138. (derivation-file-name drv)))) ; Deriver
  139. (define %substitute-directory
  140. (make-parameter
  141. (and=> (getenv "GUIX_BINARY_SUBSTITUTE_URL")
  142. (compose uri-path string->uri))))
  143. (define* (call-with-derivation-narinfo drv thunk
  144. #:key
  145. (sha256 (make-bytevector 32 0))
  146. (references '()))
  147. "Call THUNK in a context where fake substituter data, as read by 'guix
  148. substitute', has been installed for DRV. SHA256 is the hash of the
  149. expected output of DRV."
  150. (let* ((output (derivation->output-path drv))
  151. (dir (%substitute-directory))
  152. (info (string-append dir "/nix-cache-info"))
  153. (narinfo (string-append dir "/" (store-path-hash-part output)
  154. ".narinfo")))
  155. (dynamic-wind
  156. (lambda ()
  157. (call-with-output-file info
  158. (lambda (p)
  159. (format p "StoreDir: ~a\nWantMassQuery: 0\n"
  160. (%store-prefix))))
  161. (call-with-output-file narinfo
  162. (lambda (p)
  163. (display (derivation-narinfo drv #:sha256 sha256
  164. #:references references)
  165. p))))
  166. thunk
  167. (lambda ()
  168. (delete-file narinfo)
  169. (delete-file info)))))
  170. (define-syntax with-derivation-narinfo
  171. (syntax-rules (sha256 references =>)
  172. "Evaluate BODY in a context where DRV looks substitutable from the
  173. substituter's viewpoint."
  174. ((_ drv (sha256 => hash) (references => refs) body ...)
  175. (call-with-derivation-narinfo drv
  176. (lambda () body ...)
  177. #:sha256 hash
  178. #:references refs))
  179. ((_ drv (sha256 => hash) body ...)
  180. (with-derivation-narinfo drv
  181. (sha256 => hash) (references => '())
  182. body ...))
  183. ((_ drv body ...)
  184. (call-with-derivation-narinfo drv
  185. (lambda ()
  186. body ...)))))
  187. (define* (call-with-derivation-substitute drv contents thunk
  188. #:key
  189. sha256
  190. (references '()))
  191. "Call THUNK in a context where a substitute for DRV has been installed,
  192. using CONTENTS, a string, as its contents. If SHA256 is true, use it as the
  193. expected hash of the substitute; otherwise use the hash of the nar containing
  194. CONTENTS."
  195. (define dir (%substitute-directory))
  196. (dynamic-wind
  197. (lambda ()
  198. (call-with-output-file (string-append dir "/example.out")
  199. (lambda (port)
  200. (display contents port)))
  201. (call-with-output-file (string-append dir "/example.nar")
  202. (lambda (p)
  203. (write-file (string-append dir "/example.out") p))))
  204. (lambda ()
  205. (let ((hash (call-with-input-file (string-append dir "/example.nar")
  206. port-sha256)))
  207. ;; Create fake substituter data, to be read by 'guix substitute'.
  208. (call-with-derivation-narinfo drv
  209. thunk
  210. #:sha256 (or sha256 hash)
  211. #:references references)))
  212. (lambda ()
  213. (delete-file (string-append dir "/example.out"))
  214. (delete-file (string-append dir "/example.nar")))))
  215. (define (shebang-too-long?)
  216. "Return true if the typical shebang in the current store would exceed
  217. Linux's static limit---the BINPRM_BUF_SIZE constant, normally 128 characters
  218. all included."
  219. (define shebang
  220. (string-append "#!" (%store-prefix) "/"
  221. (make-string 32 #\a)
  222. "-bootstrap-binaries-0/bin/bash\0"))
  223. (> (string-length shebang) 128))
  224. (define-syntax with-derivation-substitute
  225. (syntax-rules (sha256 references =>)
  226. "Evaluate BODY in a context where DRV is substitutable with the given
  227. CONTENTS."
  228. ((_ drv contents (sha256 => hash) (references => refs) body ...)
  229. (call-with-derivation-substitute drv contents
  230. (lambda () body ...)
  231. #:sha256 hash
  232. #:references refs))
  233. ((_ drv contents (sha256 => hash) body ...)
  234. (with-derivation-substitute drv contents
  235. (sha256 => hash) (references => '())
  236. body ...))
  237. ((_ drv contents body ...)
  238. (call-with-derivation-substitute drv contents
  239. (lambda ()
  240. body ...)))))
  241. (define-syntax-rule (dummy-package name* extra-fields ...)
  242. "Return a \"dummy\" package called NAME*, with all its compulsory fields
  243. initialized with default values, and with EXTRA-FIELDS set as specified."
  244. (package extra-fields ...
  245. (name name*) (version "0") (source #f)
  246. (build-system gnu-build-system)
  247. (synopsis #f) (description #f)
  248. (home-page #f) (license #f)))
  249. (define-syntax-rule (dummy-origin extra-fields ...)
  250. "Return a \"dummy\" origin, with all its compulsory fields initialized with
  251. default values, and with EXTRA-FIELDS set as specified."
  252. (origin extra-fields ...
  253. (method #f) (uri "http://www.example.com")
  254. (sha256 (base32 (make-string 52 #\x)))))
  255. ;; Local Variables:
  256. ;; eval: (put 'call-with-derivation-narinfo 'scheme-indent-function 1)
  257. ;; eval: (put 'call-with-derivation-substitute 'scheme-indent-function 2)
  258. ;; End:
  259. ;;; tests.scm ends here