system: 'init' does not recompute the hash of each store item.
[jackhill/guix/guix.git] / tests / store-database.scm
CommitLineData
7f9d184d 1;;; GNU Guix --- Functional package management for GNU
ac435c14 2;;; Copyright © 2017, 2018, 2020 Ludovic Courtès <ludo@gnu.org>
7f9d184d
CR
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)
df2f6400 21 #:use-module (guix store)
7f9d184d 22 #:use-module (guix store database)
3931c761 23 #:use-module ((guix utils) #:select (call-with-temporary-output-file))
ac435c14
LC
24 #:use-module ((guix build utils)
25 #:select (mkdir-p delete-file-recursively))
7f9d184d
CR
26 #:use-module (srfi srfi-26)
27 #:use-module (srfi srfi-64))
28
29;; Test the (guix store database) module.
30
31(define %store
32 (open-connection-for-tests))
33
34\f
35(test-begin "store-database")
36
0682cc59 37(test-assert "register-path"
7f9d184d
CR
38 (let ((file (string-append (%store-prefix) "/" (make-string 32 #\f)
39 "-fake")))
40 (when (valid-path? %store file)
41 (delete-paths %store (list file)))
42 (false-if-exception (delete-file file))
43
44 (let ((ref (add-text-to-store %store "ref-of-fake" (random-text)))
45 (drv (string-append file ".drv")))
46 (call-with-output-file file
47 (cut display "This is a fake store item.\n" <>))
0682cc59 48 (reset-timestamps file)
7f9d184d
CR
49 (register-path file
50 #:references (list ref)
51 #:deriver drv)
52
53 (and (valid-path? %store file)
54 (equal? (references %store file) (list ref))
55 (null? (valid-derivers %store file))
e4752118
LC
56 (null? (referrers %store file))
57 (list (stat:mtime (lstat file))
58 (stat:mtime (lstat ref)))))))
7f9d184d 59
ac435c14
LC
60(test-equal "register-path, 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))
0682cc59 72 (reset-timestamps file)
ac435c14
LC
73 (register-path file #:deriver drv)
74
75 (and (valid-path? %store file)
76 (null? (references %store file))
77 (null? (valid-derivers %store file))
78 (null? (referrers %store file))
79 (list (stat:mtime (lstat file))
80 (stat:mtime (lstat (string-append file "/a")))
81 (stat:mtime (lstat (string-append file "/a/b"))))))))
82
3931c761
LC
83(test-equal "new database"
84 (list 1 2)
85 (call-with-temporary-output-file
86 (lambda (db-file port)
87 (delete-file db-file)
ef1297e8
LC
88 (with-database db-file db
89 (sqlite-register db
90 #:path "/gnu/foo"
91 #:references '()
92 #:deriver "/gnu/foo.drv"
93 #:hash (string-append "sha256:" (make-string 64 #\e))
94 #:nar-size 1234)
95 (sqlite-register db
96 #:path "/gnu/bar"
97 #:references '("/gnu/foo")
98 #:deriver "/gnu/bar.drv"
99 #:hash (string-append "sha256:" (make-string 64 #\a))
100 #:nar-size 4321)
101 (let ((path-id (@@ (guix store database) path-id)))
3931c761
LC
102 (list (path-id db "/gnu/foo")
103 (path-id db "/gnu/bar")))))))
104
f8f9f7ca
LC
105(test-assert "register-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 ()
ef1297e8
LC
114 (with-database db-file db
115 (sqlite-register 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))
f8f9f7ca
LC
120 #f)
121 (lambda args
122 (pk 'welcome-exception! args)
123 #t)))))
124
7f9d184d 125(test-end "store-database")