gnu: Add JSON-XS.
[jackhill/guix/guix.git] / tests / guix-register.sh
1 # GNU Guix --- Functional package management for GNU
2 # Copyright © 2013, 2014, 2015 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 #
20 # Test the 'guix-register' command-line utility.
21 #
22
23 guix-register --version
24
25 new_store="t-register-$$"
26 closure="t-register-closure-$$"
27 rm -rf "$new_store"
28
29 exit_hook=":"
30 trap "chmod -R +w $new_store ; rm -rf $new_store $closure ; \$exit_hook" EXIT
31
32 #
33 # Registering items in the current store---i.e., without '--prefix'.
34 #
35
36 new_file="$NIX_STORE_DIR/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-guix-register-$$"
37 echo "Fake store file to test registration." > "$new_file"
38
39 # Register the file with zero references and no deriver.
40 guix-register <<EOF
41 $new_file
42
43 0
44 EOF
45
46 # Register an idendical file, and make sure it gets deduplicated.
47 new_file2="$new_file-duplicate"
48 cat "$new_file" > "$new_file2"
49 guix-register <<EOF
50 $new_file2
51
52 0
53 EOF
54
55 guile -c "
56 (exit (= (stat:ino (stat \"$new_file\"))
57 (stat:ino (stat \"$new_file2\"))))"
58
59 # Make sure both are valid, and delete them.
60 guile -c "
61 (use-modules (guix store))
62 (define s (open-connection))
63 (exit (and (valid-path? s \"$new_file\")
64 (valid-path? s \"$new_file2\")
65 (null? (references s \"$new_file\"))
66 (null? (references s \"$new_file2\"))
67 (pair? (delete-paths s (list \"$new_file\" \"$new_file2\")))))"
68
69
70 #
71 # Registering items in a new store, with '--prefix'.
72 #
73
74 mkdir -p "$new_store/$storedir"
75 new_store_dir="`cd "$new_store/$storedir" ; pwd -P`"
76 new_store="`cd "$new_store" ; pwd -P`"
77
78 to_copy="`guix build guile-bootstrap`"
79 cp -r "$to_copy" "$new_store_dir"
80 copied="$new_store_dir/`basename $to_copy`"
81
82 # Create a file representing a closure with zero references, and with an empty
83 # "deriver" field. Note that we give the file name as it appears in the
84 # original store, and 'guix-register' translates it to match the prefix.
85 cat >> "$closure" <<EOF
86 $to_copy
87
88 0
89 EOF
90
91 # Register it.
92 guix-register -p "$new_store" < "$closure"
93
94 # Doing it a second time shouldn't hurt.
95 guix-register --prefix "$new_store" "$closure"
96
97 # Same, but with the database stored in a different place.
98 guix-register -p "$new_store" \
99 --state-directory "$new_store/chbouib" "$closure"
100
101 # Now make sure this is recognized as valid.
102
103 ls -R "$new_store"
104 for state_dir in "$localstatedir/guix" "/chbouib"
105 do
106 NIX_STORE_DIR="$new_store_dir"
107 NIX_STATE_DIR="$new_store$state_dir"
108 NIX_LOG_DIR="$new_store$state_dir/log/guix"
109 NIX_DB_DIR="$new_store$state_dir/db"
110
111 export NIX_IGNORE_SYMLINK_STORE NIX_STORE_DIR NIX_STATE_DIR \
112 NIX_LOG_DIR NIX_DB_DIR
113
114 # Check whether we overflow the limitation on local socket name lengths.
115 if [ `echo "$NIX_STATE_DIR/daemon-socket/socket" | wc -c` -ge 108 ]
116 then
117 # Mark the test as skipped even though we already did some work so
118 # that the remainder is not silently skipped.
119 exit 77
120 fi
121
122 guix-daemon --disable-chroot &
123 subdaemon_pid=$!
124 exit_hook="kill $subdaemon_pid"
125
126 final_name="$storedir/`basename $to_copy`"
127
128 # At this point the copy in $new_store must be valid, and unreferenced.
129 # The database under $NIX_DB_DIR uses the $final_name, but we can't use
130 # that name in a 'valid-path?' query because 'assertStorePath' would kill
131 # us because of the wrong prefix. So we just list dead paths instead.
132 guile -c "
133 (use-modules (guix store))
134 (define s (open-connection))
135 (exit (equal? (list \"$copied\") (dead-paths s)))"
136
137 # Kill the daemon so we can access the database below (otherwise we may
138 # get "database is locked" errors.)
139 kill $subdaemon_pid
140 exit_hook=":"
141 while kill -0 $subdaemon_pid ; do sleep 0.5 ; done
142
143 # When 'sqlite3' is available, check the name in the database.
144 if type -P sqlite3
145 then
146 echo "select * from ValidPaths where path=\"$final_name\";" | \
147 sqlite3 "$NIX_DB_DIR/db.sqlite"
148 fi
149 done