projects/guix·blob rust-team · refs

142 lines · 5.3 KiB

1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2017-2018, 2020-2021, 2024 Ludovic Courtès <[email protected]>
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-database)
20 #:use-module (guix tests)
21 #:use-module (guix store)
22 #:use-module (guix store database)
23 #:use-module (guix build store-copy)
24 #:use-module ((guix build utils)
25 #:select (mkdir-p delete-file-recursively
26 call-with-temporary-output-file))
27 #:use-module (srfi srfi-26)
28 #:use-module (srfi srfi-64))
29
30;; Test the (guix store database) module.
31
32(define %store
33 (open-connection-for-tests))
34
35
36(test-begin "store-database")
37
38(test-assert "register-items"
39 (let ((file (string-append (%store-prefix) "/" (make-string 32 #\f)
40 "-fake")))
41 (when (valid-path? %store file)
42 (delete-paths %store (list file)))
43 (false-if-exception (delete-file file))
44
45 (let ((ref (add-text-to-store %store "ref-of-fake" (random-text)))
46 (drv (string-append file ".drv")))
47 (call-with-output-file file
48 (cut display "This is a fake store item.\n" <>))
49 (reset-timestamps file)
50 (with-database (store-database-file) db
51 (register-items db (list (store-info file drv (list ref)))))
52
53 (and (valid-path? %store file)
54 (equal? (references %store file) (list ref))
55 (null? (valid-derivers %store file))
56 (null? (referrers %store file))
57 (list (stat:mtime (lstat file))
58 (stat:mtime (lstat ref)))))))
59
60(test-equal "register-items, directory"
61 '(1 1 1)
62 (let ((file (string-append (%store-prefix) "/" (make-string 32 #\f)
63 "-fake-directory")))
64 (when (valid-path? %store file)
65 (delete-paths %store (list file)))
66 (false-if-exception (delete-file-recursively file))
67
68 (let ((drv (string-append file ".drv")))
69 (mkdir-p (string-append file "/a"))
70 (call-with-output-file (string-append file "/a/b")
71 (const #t))
72 (reset-timestamps file)
73 (with-database (store-database-file) db
74 (register-items db (list (store-info file drv '()))))
75
76 (and (valid-path? %store file)
77 (null? (references %store file))
78 (null? (valid-derivers %store file))
79 (null? (referrers %store file))
80 (list (stat:mtime (lstat file))
81 (stat:mtime (lstat (string-append file "/a")))
82 (stat:mtime (lstat (string-append file "/a/b"))))))))
83
84(test-equal "new database"
85 (list 1 2)
86 (call-with-temporary-output-file
87 (lambda (db-file port)
88 (delete-file db-file)
89 (with-database db-file db
90 (register-valid-path db
91 #:path "/gnu/foo"
92 #:references '()
93 #:deriver "/gnu/foo.drv"
94 #:hash (string-append "sha256:" (make-string 64 #\e))
95 #:nar-size 1234)
96 (register-valid-path db
97 #:path "/gnu/bar"
98 #:references '("/gnu/foo")
99 #:deriver "/gnu/bar.drv"
100 #:hash (string-append "sha256:" (make-string 64 #\a))
101 #:nar-size 4321)
102 (list (valid-path-id db "/gnu/foo")
103 (valid-path-id db "/gnu/bar"))))))
104
105(test-assert "register-valid-path with unregistered references"
106 ;; Make sure we get a "NOT NULL constraint failed: Refs.reference" error
107 ;; when we try to add references that are not registered yet. Better safe
108 ;; than sorry.
109 (call-with-temporary-output-file
110 (lambda (db-file port)
111 (delete-file db-file)
112 (catch 'sqlite-error
113 (lambda ()
114 (with-database db-file db
115 (register-valid-path db #:path "/gnu/foo"
116 #:references '("/gnu/bar")
117 #:deriver "/gnu/foo.drv"
118 #:hash (string-append "sha256:" (make-string 64 #\e))
119 #:nar-size 1234))
120 #f)
121 (lambda args
122 (pk 'welcome-exception! args)
123 #t)))))
124
125(test-equal "register-valid-path with incorrect size"
126 'out-of-range
127 (call-with-temporary-output-file
128 (lambda (db-file port)
129 (delete-file db-file)
130 (catch #t
131 (lambda ()
132 (with-database db-file db
133 (register-valid-path db #:path "/gnu/foo"
134 #:references '("/gnu/bar")
135 #:deriver "/gnu/foo.drv"
136 #:hash (string-append "sha256:" (make-string 64 #\e))
137 #:nar-size -1234))
138 #f)
139 (lambda (key . _)
140 key)))))
141
142(test-end "store-database")