1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2014 Alex Kost <alezost@gmail.com>
5 ;;; This file is part of GNU Guix.
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.
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.
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/>.
20 (define-module (test-profiles)
21 #:use-module (guix tests)
22 #:use-module (guix profiles)
23 #:use-module (guix gexp)
24 #:use-module (guix store)
25 #:use-module (guix monads)
26 #:use-module (guix grafts)
27 #:use-module (guix packages)
28 #:use-module (guix derivations)
29 #:use-module (guix build-system trivial)
30 #:use-module (gnu packages bootstrap)
31 #:use-module ((gnu packages base) #:prefix packages:)
32 #:use-module ((gnu packages guile) #:prefix packages:)
33 #:use-module (ice-9 match)
34 #:use-module (ice-9 regex)
35 #:use-module (ice-9 popen)
36 #:use-module (rnrs io ports)
37 #:use-module (srfi srfi-1)
38 #:use-module (srfi srfi-11)
39 #:use-module (srfi srfi-34)
40 #:use-module (srfi srfi-64))
42 ;; Test the (guix profiles) module.
45 (open-connection-for-tests))
47 ;; Globally disable grafts because they can trigger early builds.
50 ;; Example manifest entries.
56 (item "/gnu/store/...")
63 (item "/gnu/store/...")
66 (define guile-2.0.9:debug
67 (manifest-entry (inherit guile-2.0.9)
74 (item "/gnu/store/...")
78 (test-begin "profiles")
80 (test-assert "manifest-installed?"
81 (let ((m (manifest (list guile-2.0.9 guile-2.0.9:debug))))
82 (and (manifest-installed? m (manifest-pattern (name "guile")))
83 (manifest-installed? m (manifest-pattern
84 (name "guile") (output "debug")))
85 (manifest-installed? m (manifest-pattern
86 (name "guile") (output "out")
88 (not (manifest-installed?
89 m (manifest-pattern (name "guile") (version "1.8.8"))))
90 (not (manifest-installed?
91 m (manifest-pattern (name "guile") (output "foobar")))))))
93 (test-assert "manifest-matching-entries"
94 (let* ((e (list guile-2.0.9 guile-2.0.9:debug))
97 (manifest-matching-entries m
98 (list (manifest-pattern
101 (equal? (list guile-2.0.9)
102 (manifest-matching-entries m
103 (list (manifest-pattern
105 (version "2.0.9"))))))))
107 (test-assert "manifest-matching-entries, no match"
108 (let ((m (manifest (list guile-2.0.9)))
109 (p (manifest-pattern (name "python"))))
110 (guard (c ((unmatched-pattern-error? c)
111 (and (eq? p (unmatched-pattern-error-pattern c))
112 (eq? m (unmatched-pattern-error-manifest c)))))
113 (manifest-matching-entries m (list p))
116 (test-assert "manifest-remove"
117 (let* ((m0 (manifest (list guile-2.0.9 guile-2.0.9:debug)))
118 (m1 (manifest-remove m0
119 (list (manifest-pattern (name "guile")))))
120 (m2 (manifest-remove m1
121 (list (manifest-pattern (name "guile"))))) ; same
122 (m3 (manifest-remove m2
123 (list (manifest-pattern
124 (name "guile") (output "debug")))))
125 (m4 (manifest-remove m3
126 (list (manifest-pattern (name "guile"))))))
127 (match (manifest-entries m2)
128 ((($ <manifest-entry> "guile" "2.0.9" "debug"))
130 (null? (manifest-entries m3))
131 (null? (manifest-entries m4)))))))
133 (test-assert "manifest-add"
134 (let* ((m0 (manifest '()))
135 (m1 (manifest-add m0 (list guile-1.8.8)))
136 (m2 (manifest-add m1 (list guile-2.0.9)))
137 (m3 (manifest-add m2 (list guile-2.0.9:debug)))
138 (m4 (manifest-add m3 (list guile-2.0.9:debug))))
139 (and (match (manifest-entries m1)
140 ((($ <manifest-entry> "guile" "1.8.8" "out")) #t)
142 (match (manifest-entries m2)
143 ((($ <manifest-entry> "guile" "2.0.9" "out")) #t)
147 (test-equal "manifest-add removes duplicates" ;<https://bugs.gnu.org/30569>
149 (manifest-entries (manifest-add (manifest '())
150 (list guile-2.0.9 guile-2.0.9))))
152 (test-assert "manifest-perform-transaction"
153 (let* ((m0 (manifest (list guile-2.0.9 guile-2.0.9:debug)))
154 (t1 (manifest-transaction
155 (install (list guile-1.8.8))
156 (remove (list (manifest-pattern (name "guile")
157 (output "debug"))))))
158 (t2 (manifest-transaction
159 (remove (list (manifest-pattern (name "guile")
162 (m1 (manifest-perform-transaction m0 t1))
163 (m2 (manifest-perform-transaction m1 t2))
164 (m3 (manifest-perform-transaction m0 t2)))
165 (and (match (manifest-entries m1)
166 ((($ <manifest-entry> "guile" "1.8.8" "out")) #t)
169 (null? (manifest-entries m3)))))
171 (test-assert "manifest-transaction-effects"
172 (let* ((m0 (manifest (list guile-1.8.8)))
173 (t (manifest-transaction
174 (install (list guile-2.0.9 glibc)))))
175 (let-values (((remove install upgrade downgrade)
176 (manifest-transaction-effects m0 t)))
177 (and (null? remove) (null? downgrade)
178 (equal? (list glibc) install)
179 (equal? (list (cons guile-1.8.8 guile-2.0.9)) upgrade)))))
181 (test-assert "manifest-transaction-effects and downgrades"
182 (let* ((m0 (manifest (list guile-2.0.9)))
183 (t (manifest-transaction (install (list guile-1.8.8)))))
184 (let-values (((remove install upgrade downgrade)
185 (manifest-transaction-effects m0 t)))
186 (and (null? remove) (null? install) (null? upgrade)
187 (equal? (list (cons guile-2.0.9 guile-1.8.8)) downgrade)))))
189 (test-assert "manifest-transaction-effects and pseudo-upgrades"
190 (let* ((m0 (manifest (list guile-2.0.9)))
191 (t (manifest-transaction (install (list guile-2.0.9)))))
192 (let-values (((remove install upgrade downgrade)
193 (manifest-transaction-effects m0 t)))
194 (and (null? remove) (null? install) (null? downgrade)
195 (equal? (list (cons guile-2.0.9 guile-2.0.9)) upgrade)))))
197 (test-assert "manifest-transaction-null?"
198 (manifest-transaction-null? (manifest-transaction)))
200 (test-assert "manifest-transaction-removal-candidate?"
201 (let ((m (manifest (list guile-2.0.9)))
202 (t (manifest-transaction
203 (remove (list (manifest-pattern (name "guile")))))))
204 (and (manifest-transaction-removal-candidate? guile-2.0.9 t)
205 (not (manifest-transaction-removal-candidate? glibc t)))))
207 (test-assertm "profile-derivation"
209 ((entry -> (package->manifest-entry %bootstrap-guile))
210 (guile (package->derivation %bootstrap-guile))
211 (drv (profile-derivation (manifest (list entry))
214 (profile -> (derivation->output-path drv))
215 (bindir -> (string-append profile "/bin"))
216 (_ (built-derivations (list drv))))
217 (return (and (file-exists? (string-append bindir "/guile"))
218 (string=? (dirname (readlink bindir))
219 (derivation->output-path guile))))))
221 (test-assertm "profile-derivation relative symlinks, one entry"
223 ((entry -> (package->manifest-entry %bootstrap-guile))
224 (guile (package->derivation %bootstrap-guile))
225 (drv (profile-derivation (manifest (list entry))
226 #:relative-symlinks? #t
229 (profile -> (derivation->output-path drv))
230 (bindir -> (string-append profile "/bin"))
231 (_ (built-derivations (list drv))))
232 (return (and (file-exists? (string-append bindir "/guile"))
233 (string=? (readlink bindir)
236 (derivation->output-path guile))
239 (unless (network-reachable?) (test-skip 1))
240 (test-assertm "profile-derivation relative symlinks, two entries"
242 ((gnu-make-boot0 -> (@@ (gnu packages commencement) gnu-make-boot0))
243 (manifest -> (packages->manifest
244 (list %bootstrap-guile gnu-make-boot0)))
245 (guile (package->derivation %bootstrap-guile))
246 (make (package->derivation gnu-make-boot0))
247 (drv (profile-derivation manifest
248 #:relative-symlinks? #t
251 (profile -> (derivation->output-path drv))
252 (bindir -> (string-append profile "/bin"))
253 (_ (built-derivations (list drv))))
254 (return (and (file-exists? (string-append bindir "/guile"))
255 (file-exists? (string-append bindir "/make"))
256 (string=? (readlink (string-append bindir "/guile"))
257 (string-append "../../"
259 (derivation->output-path guile))
261 (string=? (readlink (string-append bindir "/make"))
262 (string-append "../../"
264 (derivation->output-path make))
267 (test-assertm "profile-derivation, inputs"
269 ((entry -> (package->manifest-entry packages:glibc "debug"))
270 (drv (profile-derivation (manifest (list entry))
273 (return (derivation-inputs drv))))
275 (test-assertm "profile-derivation, cross-compilation"
277 ((manifest -> (packages->manifest (list packages:sed packages:grep)))
278 (target -> "arm-linux-gnueabihf")
279 (grep (package->cross-derivation packages:grep target))
280 (sed (package->cross-derivation packages:sed target))
281 (locales (package->derivation packages:glibc-utf8-locales))
282 (drv (profile-derivation manifest
286 (define (find-input package)
287 (let ((name (string-append (package-full-name package "-") ".drv")))
289 (let ((input (derivation-input-path input)))
290 (and (string-suffix? name input) input)))
291 (derivation-inputs drv))))
293 ;; The inputs for grep and sed should be cross-build derivations, but that
294 ;; for the glibc-utf8-locales should be a native build.
295 (return (and (string=? (derivation-system drv) (%current-system))
296 (string=? (find-input packages:grep)
297 (derivation-file-name grep))
298 (string=? (find-input packages:sed)
299 (derivation-file-name sed))
300 (string=? (find-input packages:glibc-utf8-locales)
301 (derivation-file-name locales))))))
303 (test-assert "package->manifest-entry defaults to \"out\""
304 (let ((outputs (package-outputs packages:glibc)))
305 (equal? (manifest-entry-output
306 (package->manifest-entry (package
307 (inherit packages:glibc)
308 (outputs (reverse outputs)))))
309 (manifest-entry-output
310 (package->manifest-entry packages:glibc))
313 (test-assertm "profile-manifest, search-paths"
316 (inherit %bootstrap-guile)
318 (package-native-search-paths packages:guile-2.0))))
319 (entry -> (package->manifest-entry guile))
320 (drv (profile-derivation (manifest (list entry))
323 (profile -> (derivation->output-path drv)))
325 (built-derivations (list drv))
327 ;; Read the manifest back and make sure search paths are preserved.
328 (let ((manifest (profile-manifest profile)))
329 (match (manifest-entries manifest)
331 (return (equal? (manifest-entry-search-paths result)
332 (manifest-entry-search-paths entry)
333 (package-native-search-paths
334 packages:guile-2.0)))))))))
336 (test-assert "package->manifest-entry, search paths"
337 ;; See <http://bugs.gnu.org/22073>.
338 (let ((mpl (@ (gnu packages python-xyz) python2-matplotlib)))
340 (package-transitive-native-search-paths mpl)
341 (manifest-entry-search-paths
342 (package->manifest-entry mpl)))))
344 (test-equal "packages->manifest, propagated inputs"
347 (list (package-name package) (package-version package)
349 (package-propagated-inputs packages:guile-2.2))
351 (list (manifest-entry-name entry)
352 (manifest-entry-version entry)
353 (manifest-entry-item entry)))
354 (manifest-entry-dependencies
355 (package->manifest-entry packages:guile-2.2))))
357 (test-assert "manifest-entry-parent"
358 (let ((entry (package->manifest-entry packages:guile-2.2)))
359 (match (manifest-entry-dependencies entry)
361 (and (every (lambda (parent)
362 (eq? entry (force parent)))
363 (map manifest-entry-parent dependencies))
364 (not (force (manifest-entry-parent entry))))))))
366 (test-assertm "read-manifest"
367 (mlet* %store-monad ((manifest -> (packages->manifest
369 (inherit %bootstrap-guile)
371 (package-native-search-paths
372 packages:guile-2.0))))))
373 (drv (profile-derivation manifest
376 (out -> (derivation->output-path drv)))
377 (define (entry->sexp entry)
378 (list (manifest-entry-name entry)
379 (manifest-entry-version entry)
380 (manifest-entry-search-paths entry)
381 (manifest-entry-dependencies entry)
382 (force (manifest-entry-parent entry))))
385 (built-derivations (list drv))
386 (let ((manifest2 (profile-manifest out)))
387 (return (equal? (map entry->sexp (manifest-entries manifest))
388 (map entry->sexp (manifest-entries manifest2))))))))
390 (test-equal "collision"
391 '(("guile-bootstrap" "2.0") ("guile-bootstrap" "42"))
392 (guard (c ((profile-collision-error? c)
393 (let ((entry1 (profile-collision-error-entry c))
394 (entry2 (profile-collision-error-conflict c)))
395 (list (list (manifest-entry-name entry1)
396 (manifest-entry-version entry1))
397 (list (manifest-entry-name entry2)
398 (manifest-entry-version entry2))))))
399 (run-with-store %store
400 (mlet* %store-monad ((p0 -> (package
401 (inherit %bootstrap-guile)
403 (p1 -> (dummy-package "p1"
404 (propagated-inputs `(("p0" ,p0)))))
405 (manifest -> (packages->manifest
406 (list %bootstrap-guile p1)))
407 (drv (profile-derivation manifest
412 (test-equal "collision of propagated inputs"
413 '(("guile-bootstrap" "2.0") ("guile-bootstrap" "42"))
414 (guard (c ((profile-collision-error? c)
415 (let ((entry1 (profile-collision-error-entry c))
416 (entry2 (profile-collision-error-conflict c)))
417 (list (list (manifest-entry-name entry1)
418 (manifest-entry-version entry1))
419 (list (manifest-entry-name entry2)
420 (manifest-entry-version entry2))))))
421 (run-with-store %store
422 (mlet* %store-monad ((p0 -> (package
423 (inherit %bootstrap-guile)
425 (p1 -> (dummy-package "p1"
427 `(("guile" ,%bootstrap-guile)))))
428 (p2 -> (dummy-package "p2"
431 (manifest -> (packages->manifest (list p1 p2)))
432 (drv (profile-derivation manifest
437 (test-assertm "no collision"
438 ;; Here we have an entry that is "lowered" (its 'item' field is a store file
439 ;; name) and another entry (its 'item' field is a package) that is
441 (mlet* %store-monad ((p -> (dummy-package "p"
443 `(("guile" ,%bootstrap-guile)))))
444 (guile (package->derivation %bootstrap-guile))
445 (entry -> (manifest-entry
446 (inherit (package->manifest-entry
448 (item (derivation->output-path guile))))
449 (manifest -> (manifest
451 (package->manifest-entry p))))
452 (drv (profile-derivation manifest)))
453 (return (->bool drv))))
455 (test-assertm "etc/profile"
456 ;; Make sure we get an 'etc/profile' file that at least defines $PATH.
459 (inherit %bootstrap-guile)
461 (package-native-search-paths packages:guile-2.0))))
462 (entry -> (package->manifest-entry guile))
463 (drv (profile-derivation (manifest (list entry))
466 (profile -> (derivation->output-path drv)))
468 (built-derivations (list drv))
469 (let* ((pipe (open-input-pipe
470 (string-append "unset GUIX_PROFILE; "
471 ;; 'source' is a Bashism; use '.' (dot).
472 ". " profile "/etc/profile; "
473 ;; Don't try to parse set(1) output because
474 ;; it differs among shells; just use echo.
476 (path (get-string-all pipe)))
478 (and (zero? (close-pipe pipe))
479 (string-contains path (string-append profile "/bin"))))))))
481 (test-assertm "etc/profile when etc/ already exists"
482 ;; Here 'union-build' makes the profile's etc/ a symlink to the package's
483 ;; etc/ directory, which makes it read-only. Make sure the profile build
486 ((thing -> (dummy-package "dummy"
487 (build-system trivial-build-system)
489 `(#:guile ,%bootstrap-guile
491 (let ((out (assoc-ref %outputs "out")))
493 (mkdir (string-append out "/etc"))
494 (call-with-output-file (string-append out "/etc/foo")
496 (display "foo!" port)))
498 (entry -> (package->manifest-entry thing))
499 (drv (profile-derivation (manifest (list entry))
502 (profile -> (derivation->output-path drv)))
504 (built-derivations (list drv))
505 (return (and (file-exists? (string-append profile "/etc/profile"))
506 (string=? (call-with-input-file
507 (string-append profile "/etc/foo")
511 (test-assertm "etc/profile when etc/ is a symlink"
512 ;; When etc/ is a symlink, the unsymlink code in 0.8.2 would fail
513 ;; gracelessly because 'scandir' would return #f.
515 ((thing -> (dummy-package "dummy"
516 (build-system trivial-build-system)
518 `(#:guile ,%bootstrap-guile
520 (let ((out (assoc-ref %outputs "out")))
522 (mkdir (string-append out "/foo"))
523 (symlink "foo" (string-append out "/etc"))
524 (call-with-output-file (string-append out "/etc/bar")
526 (display "foo!" port)))
528 (entry -> (package->manifest-entry thing))
529 (drv (profile-derivation (manifest (list entry))
532 (profile -> (derivation->output-path drv)))
534 (built-derivations (list drv))
535 (return (and (file-exists? (string-append profile "/etc/profile"))
536 (string=? (call-with-input-file
537 (string-append profile "/etc/bar")
541 (test-assertm "profile-derivation when etc/ is a relative symlink"
542 ;; See <https://bugs.gnu.org/32686>.
544 ((etc (gexp->derivation
548 (call-with-output-file (string-append #$output "/foo")
550 (display "Heya!" port))))))
551 (thing -> (dummy-package "dummy"
552 (build-system trivial-build-system)
556 `(#:guile ,%bootstrap-guile
558 (let ((out (assoc-ref %outputs "out"))
559 (etc (assoc-ref %build-inputs "etc")))
561 (symlink etc (string-append out "/etc"))
563 (entry -> (package->manifest-entry thing))
564 (drv (profile-derivation (manifest (list entry))
565 #:relative-symlinks? #t
568 (profile -> (derivation->output-path drv)))
570 (built-derivations (list drv))
571 (return (string=? (call-with-input-file
572 (string-append profile "/etc/foo")
576 (test-equalm "union vs. dangling symlink" ;<https://bugs.gnu.org/26949>
579 ((thing1 -> (dummy-package "dummy"
580 (build-system trivial-build-system)
582 `(#:guile ,%bootstrap-guile
584 (let ((out (assoc-ref %outputs "out")))
586 (symlink "does-not-exist"
587 (string-append out "/dangling"))
589 (thing2 -> (package (inherit thing1) (name "dummy2")))
590 (drv (profile-derivation (packages->manifest
591 (list thing1 thing2))
594 (profile -> (derivation->output-path drv)))
596 (built-derivations (list drv))
597 (return (readlink (readlink (string-append profile "/dangling")))))))
599 (test-equalm "profile in profile"
602 ;; Make sure we can build a profile that has another profile has one of its
603 ;; entries. The new profile's /manifest and /etc/profile must override the
606 ((prof0 (profile-derivation
608 (list (package->manifest-entry %bootstrap-guile)))
611 (prof1 (profile-derivation
612 (manifest (list (manifest-entry
619 (built-derivations (list prof1))
620 (let ((out (derivation->output-path prof1)))
621 (return (and (file-exists?
622 (string-append out "/bin/guile"))
623 (let ((manifest (profile-manifest out)))
624 (match (manifest-entries manifest)
626 (list (manifest-entry-name entry)
627 (manifest-entry-version entry)))))))))))
629 (test-end "profiles")
632 ;;; eval: (put 'dummy-package 'scheme-indent-function 1)