tests: Check the effect of '--without-tests' on implicit inputs.
[jackhill/guix/guix.git] / tests / store-deduplication.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2018, 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
19 (define-module (test-store-deduplication)
20 #:use-module (guix tests)
21 #:use-module (guix store deduplication)
22 #:use-module (gcrypt hash)
23 #:use-module ((guix utils) #:select (call-with-temporary-directory))
24 #:use-module (guix build utils)
25 #:use-module (rnrs bytevectors)
26 #:use-module (ice-9 binary-ports)
27 #:use-module (srfi srfi-1)
28 #:use-module (srfi srfi-26)
29 #:use-module (srfi srfi-64))
30
31 (test-begin "store-deduplication")
32
33 (test-equal "deduplicate"
34 (cons* #t #f ;inode comparisons
35 2 (make-list 5 6)) ;'nlink' values
36
37 (call-with-temporary-directory
38 (lambda (store)
39 (let ((data (string->utf8 "Hello, world!"))
40 (identical (map (lambda (n)
41 (string-append store "/" (number->string n)
42 "/a/b/c"))
43 (iota 5)))
44 (unique (string-append store "/unique")))
45 (for-each (lambda (file)
46 (mkdir-p (dirname file))
47 (call-with-output-file file
48 (lambda (port)
49 (put-bytevector port data))))
50 identical)
51 ;; Make the parent of IDENTICAL read-only. This should not prevent
52 ;; deduplication from inserting its hard link.
53 (chmod (dirname (second identical)) #o544)
54
55 (call-with-output-file unique
56 (lambda (port)
57 (put-bytevector port (string->utf8 "This is unique."))))
58
59 (deduplicate store (nar-sha256 store) #:store store)
60
61 ;; (system (string-append "ls -lRia " store))
62 (cons* (apply = (map (compose stat:ino stat) identical))
63 (= (stat:ino (stat unique))
64 (stat:ino (stat (car identical))))
65 (stat:nlink (stat unique))
66 (map (compose stat:nlink stat) identical))))))
67
68 (test-equal "deduplicate, ENOSPC"
69 (cons* #f ;inode comparison
70 (append (make-list 3 4)
71 (make-list 7 1))) ;'nlink' values
72
73 ;; In this scenario the first 3 files are properly deduplicated and then we
74 ;; simulate a full '.links' directory where link(2) gets ENOSPC, thereby
75 ;; preventing deduplication of the subsequent files.
76 (call-with-temporary-directory
77 (lambda (store)
78 (let ((true-link link)
79 (links 0)
80 (data1 (string->utf8 "Hello, world!"))
81 (data2 (string->utf8 "Hi, world!"))
82 (identical (map (lambda (n)
83 (string-append store "/" (number->string n)
84 "/a/b/c"))
85 (iota 10)))
86 (populate (lambda (data)
87 (lambda (file)
88 (mkdir-p (dirname file))
89 (call-with-output-file file
90 (lambda (port)
91 (put-bytevector port data)))))))
92 (for-each (populate data1) (take identical 5))
93 (for-each (populate data2) (drop identical 5))
94 (dynamic-wind
95 (lambda ()
96 (set! link (lambda (old new)
97 (set! links (+ links 1))
98 (if (<= links 4)
99 (true-link old new)
100 (throw 'system-error "link" "~A" '("Whaaat?!")
101 (list ENOSPC))))))
102 (lambda ()
103 (deduplicate store (nar-sha256 store) #:store store))
104 (lambda ()
105 (set! link true-link)))
106
107 (cons (apply = (map (compose stat:ino stat) identical))
108 (map (compose stat:nlink stat) identical))))))
109
110 (test-assert "copy-file/deduplicate"
111 (call-with-temporary-directory
112 (lambda (store)
113 (let ((source (search-path %load-path "gnu/packages/emacs-xyz.scm")))
114 (for-each (lambda (target)
115 (copy-file/deduplicate source
116 (string-append store target)
117 #:store store))
118 '("/a" "/b" "/c"))
119 (and (directory-exists? (string-append store "/.links"))
120 (file=? source (string-append store "/a"))
121 (apply = (map (compose stat:ino stat
122 (cut string-append store <>))
123 '("/a" "/b" "/c"))))))))
124
125 (test-end "store-deduplication")