gnu: Add bsnes.
[jackhill/guix/guix.git] / tests / pack.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2018 Ricardo Wurmus <rekado@elephly.net>
4 ;;;
5 ;;; This file is part of GNU Guix.
6 ;;;
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
11 ;;;
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
16 ;;;
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19
20 (define-module (test-pack)
21 #:use-module (guix scripts pack)
22 #:use-module (guix store)
23 #:use-module (guix derivations)
24 #:use-module (guix profiles)
25 #:use-module (guix packages)
26 #:use-module (guix monads)
27 #:use-module (guix grafts)
28 #:use-module (guix tests)
29 #:use-module (guix gexp)
30 #:use-module (gnu packages bootstrap)
31 #:use-module ((gnu packages compression) #:select (squashfs-tools))
32 #:use-module (srfi srfi-64))
33
34 (define %store
35 (open-connection-for-tests))
36
37 ;; Globally disable grafts because they can trigger early builds.
38 (%graft? #f)
39
40 (define-syntax-rule (test-assertm name store exp)
41 (test-assert name
42 (let ((guile (package-derivation store %bootstrap-guile)))
43 (run-with-store store exp
44 #:guile-for-build guile))))
45
46 (define %gzip-compressor
47 ;; Compressor that uses the bootstrap 'gzip'.
48 ((@ (guix scripts pack) compressor) "gzip"
49 "gz"
50 #~(#+(file-append %bootstrap-coreutils&co "/bin/gzip") "-6n")))
51
52 (define %tar-bootstrap %bootstrap-coreutils&co)
53
54 \f
55 (test-begin "pack")
56
57 (unless (network-reachable?) (test-skip 1))
58 (test-assertm "self-contained-tarball" %store
59 (mlet* %store-monad
60 ((profile (profile-derivation (packages->manifest
61 (list %bootstrap-guile))
62 #:hooks '()
63 #:locales? #f))
64 (tarball (self-contained-tarball "pack" profile
65 #:symlinks '(("/bin/Guile"
66 -> "bin/guile"))
67 #:compressor %gzip-compressor
68 #:archiver %tar-bootstrap))
69 (check (gexp->derivation
70 "check-tarball"
71 (with-imported-modules '((guix build utils))
72 #~(begin
73 (use-modules (guix build utils)
74 (srfi srfi-1))
75
76 (define store
77 ;; The unpacked store.
78 (string-append "." (%store-directory) "/"))
79
80 (define (canonical? file)
81 ;; Return #t if FILE is read-only and its mtime is 1.
82 (let ((st (lstat file)))
83 (or (not (string-prefix? store file))
84 (eq? 'symlink (stat:type st))
85 (and (= 1 (stat:mtime st))
86 (zero? (logand #o222
87 (stat:mode st)))))))
88
89 (define bin
90 (string-append "." #$profile "/bin"))
91
92 (setenv "PATH"
93 (string-append #$%tar-bootstrap "/bin"))
94 (system* "tar" "xvf" #$tarball)
95 (mkdir #$output)
96 (exit
97 (and (file-exists? (string-append bin "/guile"))
98 (file-exists? store)
99 (every canonical?
100 (find-files "." (const #t)
101 #:directories? #t))
102 (string=? (string-append #$%bootstrap-guile "/bin")
103 (readlink bin))
104 (string=? (string-append ".." #$profile
105 "/bin/guile")
106 (readlink "bin/Guile")))))))))
107 (built-derivations (list check))))
108
109 ;; The following test needs guile-sqlite3, libgcrypt, etc. as a consequence of
110 ;; commit c45477d2a1a651485feede20fe0f3d15aec48b39 and related changes. Thus,
111 ;; run it on the user's store, if it's available, on the grounds that these
112 ;; dependencies may be already there, or we can get substitutes or build them
113 ;; quite inexpensively; see <https://bugs.gnu.org/32184>.
114
115 (with-external-store store
116 (unless store (test-skip 1))
117 (test-assertm "self-contained-tarball + localstatedir" store
118 (mlet* %store-monad
119 ((guile (set-guile-for-build (default-guile)))
120 (profile (profile-derivation (packages->manifest
121 (list %bootstrap-guile))
122 #:hooks '()
123 #:locales? #f))
124 (tarball (self-contained-tarball "tar-pack" profile
125 #:localstatedir? #t))
126 (check (gexp->derivation
127 "check-tarball"
128 #~(let ((bin (string-append "." #$profile "/bin")))
129 (setenv "PATH"
130 (string-append #$%tar-bootstrap "/bin"))
131 (system* "tar" "xvf" #$tarball)
132 (mkdir #$output)
133 (exit
134 (and (file-exists? "var/guix/db/db.sqlite")
135 (string=? (string-append #$%bootstrap-guile "/bin")
136 (readlink bin))))))))
137 (built-derivations (list check))))
138
139 (unless store (test-skip 1))
140 (test-assertm "docker-image + localstatedir" store
141 (mlet* %store-monad
142 ((guile (set-guile-for-build (default-guile)))
143 (profile (profile-derivation (packages->manifest
144 (list %bootstrap-guile))
145 #:hooks '()
146 #:locales? #f))
147 (tarball (docker-image "docker-pack" profile
148 #:symlinks '(("/bin/Guile" -> "bin/guile"))
149 #:localstatedir? #t))
150 (check (gexp->derivation
151 "check-tarball"
152 (with-imported-modules '((guix build utils))
153 #~(begin
154 (use-modules (guix build utils)
155 (ice-9 match))
156
157 (define bin
158 (string-append "." #$profile "/bin"))
159
160 (setenv "PATH" (string-append #$%tar-bootstrap "/bin"))
161 (mkdir "base")
162 (with-directory-excursion "base"
163 (invoke "tar" "xvf" #$tarball))
164
165 (match (find-files "base" "layer.tar")
166 ((layer)
167 (invoke "tar" "xvf" layer)))
168
169 (when
170 (and (file-exists? (string-append bin "/guile"))
171 (file-exists? "var/guix/db/db.sqlite")
172 (file-is-directory? "tmp")
173 (string=? (string-append #$%bootstrap-guile "/bin")
174 (pk 'binlink (readlink bin)))
175 (string=? (string-append #$profile "/bin/guile")
176 (pk 'guilelink (readlink "bin/Guile"))))
177 (mkdir #$output)))))))
178 (built-derivations (list check))))
179
180 (unless store (test-skip 1))
181 (test-assertm "squashfs-image + localstatedir" store
182 (mlet* %store-monad
183 ((guile (set-guile-for-build (default-guile)))
184 (profile (profile-derivation (packages->manifest
185 (list %bootstrap-guile))
186 #:hooks '()
187 #:locales? #f))
188 (image (squashfs-image "squashfs-pack" profile
189 #:symlinks '(("/bin" -> "bin"))
190 #:localstatedir? #t))
191 (check (gexp->derivation
192 "check-tarball"
193 (with-imported-modules '((guix build utils))
194 #~(begin
195 (use-modules (guix build utils)
196 (ice-9 match))
197
198 (define bin
199 (string-append "." #$profile "/bin"))
200
201 (setenv "PATH"
202 (string-append #$squashfs-tools "/bin"))
203 (invoke "unsquashfs" #$image)
204 (with-directory-excursion "squashfs-root"
205 (when (and (file-exists? (string-append bin
206 "/guile"))
207 (file-exists? "var/guix/db/db.sqlite")
208 (string=? (string-append #$%bootstrap-guile "/bin")
209 (pk 'binlink (readlink bin)))
210
211 ;; This is a relative symlink target.
212 (string=? (string-drop
213 (string-append #$profile "/bin")
214 1)
215 (pk 'guilelink (readlink "bin"))))
216 (mkdir #$output))))))))
217 (built-derivations (list check)))))
218
219 (test-end)
220
221 ;; Local Variables:
222 ;; eval: (put 'test-assertm 'scheme-indent-function 2)
223 ;; End: