tests: Move some of the narinfo test tools to (guix tests).
[jackhill/guix/guix.git] / guix / tests.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014 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
19 (define-module (guix tests)
20 #:use-module (guix store)
21 #:use-module (guix derivations)
22 #:use-module (guix packages)
23 #:use-module (gnu packages bootstrap)
24 #:use-module (srfi srfi-34)
25 #:use-module (rnrs bytevectors)
26 #:use-module (web uri)
27 #:export (open-connection-for-tests
28 random-text
29 random-bytevector
30 with-derivation-narinfo))
31
32 ;;; Commentary:
33 ;;;
34 ;;; This module provide shared infrastructure for the test suite. For
35 ;;; internal use only.
36 ;;;
37 ;;; Code:
38
39 (define (open-connection-for-tests)
40 "Open a connection to the build daemon for tests purposes and return it."
41 (guard (c ((nix-error? c)
42 (format (current-error-port)
43 "warning: build daemon error: ~s~%" c)
44 #f))
45 (let ((store (open-connection)))
46 ;; Make sure we build everything by ourselves.
47 (set-build-options store #:use-substitutes? #f)
48
49 ;; Use the bootstrap Guile when running tests, so we don't end up
50 ;; building everything in the temporary test store.
51 (%guile-for-build (package-derivation store %bootstrap-guile))
52
53 store)))
54
55 (define %seed
56 (seed->random-state (logxor (getpid) (car (gettimeofday)))))
57
58 (define (random-text)
59 "Return the hexadecimal representation of a random number."
60 (number->string (random (expt 2 256) %seed) 16))
61
62 (define (random-bytevector n)
63 "Return a random bytevector of N bytes."
64 (let ((bv (make-bytevector n)))
65 (let loop ((i 0))
66 (if (< i n)
67 (begin
68 (bytevector-u8-set! bv i (random 256 %seed))
69 (loop (1+ i)))
70 bv))))
71
72 \f
73 ;;;
74 ;;; Narinfo files, as used by the substituter.
75 ;;;
76
77 (define* (derivation-narinfo drv #:optional (nar "example.nar"))
78 "Return the contents of the narinfo corresponding to DRV; NAR should be the
79 file name of the archive containing the substitute for DRV."
80 (format #f "StorePath: ~a
81 URL: ~a
82 Compression: none
83 NarSize: 1234
84 References:
85 System: ~a
86 Deriver: ~a~%"
87 (derivation->output-path drv) ; StorePath
88 nar ; URL
89 (derivation-system drv) ; System
90 (basename
91 (derivation-file-name drv)))) ; Deriver
92
93 (define (call-with-derivation-narinfo drv thunk)
94 "Call THUNK in a context where fake substituter data, as read by 'guix
95 substitute-binary', has been installed for DRV."
96 (let* ((output (derivation->output-path drv))
97 (dir (uri-path
98 (string->uri (getenv "GUIX_BINARY_SUBSTITUTE_URL"))))
99 (info (string-append dir "/nix-cache-info"))
100 (narinfo (string-append dir "/" (store-path-hash-part output)
101 ".narinfo")))
102 (dynamic-wind
103 (lambda ()
104 (call-with-output-file info
105 (lambda (p)
106 (format p "StoreDir: ~a\nWantMassQuery: 0\n"
107 (%store-prefix))))
108 (call-with-output-file narinfo
109 (lambda (p)
110 (display (derivation-narinfo drv) p))))
111 thunk
112 (lambda ()
113 (delete-file narinfo)
114 (delete-file info)))))
115
116 (define-syntax-rule (with-derivation-narinfo drv body ...)
117 "Evaluate BODY in a context where DRV looks substitutable from the
118 substituter's viewpoint."
119 (call-with-derivation-narinfo drv
120 (lambda ()
121 body ...)))
122
123 ;; Local Variables:
124 ;; eval: (put 'call-with-derivation-narinfo 'scheme-indent-function 1)
125 ;; End:
126
127 ;;; tests.scm ends here