pack.scm 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017, 2018, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2018 Ricardo Wurmus <rekado@elephly.net>
  4. ;;; Copyright © 2021 Maxim Cournoyer <maxim.cournoyer@gmail.com>
  5. ;;;
  6. ;;; This file is part of GNU Guix.
  7. ;;;
  8. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  9. ;;; under the terms of the GNU General Public License as published by
  10. ;;; the Free Software Foundation; either version 3 of the License, or (at
  11. ;;; your option) any later version.
  12. ;;;
  13. ;;; GNU Guix is distributed in the hope that it will be useful, but
  14. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;;; GNU General Public License for more details.
  17. ;;;
  18. ;;; You should have received a copy of the GNU General Public License
  19. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  20. (define-module (test-pack)
  21. #:use-module (guix scripts pack)
  22. #:use-module (guix store)
  23. #:use-module (guix derivations)
  24. #:use-module (guix profiles)
  25. #:use-module (guix packages)
  26. #:use-module (guix monads)
  27. #:use-module (guix grafts)
  28. #:use-module (guix tests)
  29. #:use-module (guix gexp)
  30. #:use-module (guix modules)
  31. #:use-module (gnu packages)
  32. #:use-module ((gnu packages base) #:select (glibc-utf8-locales))
  33. #:use-module (gnu packages bootstrap)
  34. #:use-module ((gnu packages compression) #:select (squashfs-tools))
  35. #:use-module ((gnu packages debian) #:select (dpkg))
  36. #:use-module ((gnu packages guile) #:select (guile-sqlite3))
  37. #:use-module ((gnu packages gnupg) #:select (guile-gcrypt))
  38. #:use-module (srfi srfi-64))
  39. (define %store
  40. (open-connection-for-tests))
  41. ;; Globally disable grafts because they can trigger early builds.
  42. (%graft? #f)
  43. (define-syntax-rule (test-assertm name store exp)
  44. (test-assert name
  45. (let ((guile (package-derivation store %bootstrap-guile)))
  46. (run-with-store store exp
  47. #:guile-for-build guile))))
  48. (define %gzip-compressor
  49. ;; Compressor that uses the bootstrap 'gzip'.
  50. ((@ (guix scripts pack) compressor) "gzip"
  51. ".gz"
  52. #~(list #+(file-append %bootstrap-coreutils&co "/bin/gzip") "-6n")))
  53. (define %tar-bootstrap %bootstrap-coreutils&co)
  54. (define %ar-bootstrap %bootstrap-binutils)
  55. (test-begin "pack")
  56. (unless (network-reachable?) (test-skip 1))
  57. (test-assertm "self-contained-tarball" %store
  58. (mlet* %store-monad
  59. ((profile -> (profile
  60. (content (packages->manifest (list %bootstrap-guile)))
  61. (hooks '())
  62. (locales? #f)))
  63. (tarball (self-contained-tarball "pack" profile
  64. #:symlinks '(("/bin/Guile"
  65. -> "bin/guile"))
  66. #:compressor %gzip-compressor
  67. #:archiver %tar-bootstrap))
  68. (check (gexp->derivation
  69. "check-tarball"
  70. (with-imported-modules '((guix build utils))
  71. #~(begin
  72. (use-modules (guix build utils)
  73. (srfi srfi-1))
  74. (define store
  75. ;; The unpacked store.
  76. (string-append "." (%store-directory) "/"))
  77. (define (canonical? file)
  78. ;; Return #t if FILE is read-only and its mtime is 1.
  79. (let ((st (lstat file)))
  80. (or (not (string-prefix? store file))
  81. (eq? 'symlink (stat:type st))
  82. (and (= 1 (stat:mtime st))
  83. (zero? (logand #o222
  84. (stat:mode st)))))))
  85. (define bin
  86. (string-append "." #$profile "/bin"))
  87. (setenv "PATH"
  88. (string-append #$%tar-bootstrap "/bin"))
  89. (system* "tar" "xvf" #$tarball)
  90. (mkdir #$output)
  91. (exit
  92. (and (file-exists? (string-append bin "/guile"))
  93. (file-exists? store)
  94. (every canonical?
  95. (find-files "." (const #t)
  96. #:directories? #t))
  97. (string=? (string-append #$%bootstrap-guile "/bin")
  98. (readlink bin))
  99. (string=? (string-append ".." #$profile
  100. "/bin/guile")
  101. (readlink "bin/Guile")))))))))
  102. (built-derivations (list check))))
  103. ;; The following test needs guile-sqlite3, libgcrypt, etc. as a consequence of
  104. ;; commit c45477d2a1a651485feede20fe0f3d15aec48b39 and related changes. Thus,
  105. ;; run it on the user's store, if it's available, on the grounds that these
  106. ;; dependencies may be already there, or we can get substitutes or build them
  107. ;; quite inexpensively; see <https://bugs.gnu.org/32184>.
  108. (with-external-store store
  109. (unless store (test-skip 1))
  110. (test-assertm "self-contained-tarball + localstatedir" store
  111. (mlet* %store-monad
  112. ((guile (set-guile-for-build (default-guile)))
  113. (profile (profile-derivation (packages->manifest
  114. (list %bootstrap-guile))
  115. #:hooks '()
  116. #:locales? #f))
  117. (tarball (self-contained-tarball "tar-pack" profile
  118. #:localstatedir? #t))
  119. (check (gexp->derivation
  120. "check-tarball"
  121. #~(let ((bin (string-append "." #$profile "/bin")))
  122. (setenv "PATH"
  123. (string-append #$%tar-bootstrap "/bin"))
  124. (system* "tar" "xvf" #$tarball)
  125. (mkdir #$output)
  126. (exit
  127. (and (file-exists? "var/guix/db/db.sqlite")
  128. (string=? (string-append #$%bootstrap-guile "/bin")
  129. (readlink bin))))))))
  130. (built-derivations (list check))))
  131. (unless store (test-skip 1))
  132. (test-assertm "self-contained-tarball + localstatedir, UTF-8 file names" store
  133. (mlet* %store-monad
  134. ((guile (set-guile-for-build (default-guile)))
  135. (tree (interned-file-tree
  136. `("directory-with-utf8-file-names" directory
  137. ("α" regular (data "alpha"))
  138. ("λ" regular (data "lambda")))))
  139. (tarball (self-contained-tarball "tar-pack" tree
  140. #:localstatedir? #t))
  141. (check (gexp->derivation
  142. "check-tarball"
  143. (with-extensions (list guile-sqlite3 guile-gcrypt)
  144. (with-imported-modules (source-module-closure
  145. '((guix store database)))
  146. #~(begin
  147. (use-modules (guix store database)
  148. (rnrs io ports)
  149. (srfi srfi-1))
  150. (define (valid-file? basename data)
  151. (define file
  152. (string-append "./" #$tree "/" basename))
  153. (string=? (call-with-input-file (pk 'file file)
  154. get-string-all)
  155. data))
  156. (setenv "PATH"
  157. (string-append #$%tar-bootstrap "/bin"))
  158. (system* "tar" "xvf" #$tarball)
  159. (sql-schema
  160. #$(local-file (search-path %load-path
  161. "guix/store/schema.sql")))
  162. (with-database "var/guix/db/db.sqlite" db
  163. ;; Make sure non-ASCII file names are properly
  164. ;; handled.
  165. (setenv "GUIX_LOCPATH"
  166. #+(file-append glibc-utf8-locales
  167. "/lib/locale"))
  168. (setlocale LC_ALL "en_US.utf8")
  169. (mkdir #$output)
  170. (exit
  171. (and (every valid-file?
  172. '("α" "λ")
  173. '("alpha" "lambda"))
  174. (integer? (path-id db #$tree)))))))))))
  175. (built-derivations (list check))))
  176. (unless store (test-skip 1))
  177. (test-assertm "docker-image + localstatedir" store
  178. (mlet* %store-monad
  179. ((guile (set-guile-for-build (default-guile)))
  180. (profile (profile-derivation (packages->manifest
  181. (list %bootstrap-guile))
  182. #:hooks '()
  183. #:locales? #f))
  184. (tarball (docker-image "docker-pack" profile
  185. #:symlinks '(("/bin/Guile" -> "bin/guile"))
  186. #:localstatedir? #t))
  187. (check (gexp->derivation
  188. "check-tarball"
  189. (with-imported-modules '((guix build utils))
  190. #~(begin
  191. (use-modules (guix build utils)
  192. (ice-9 match))
  193. (define bin
  194. (string-append "." #$profile "/bin"))
  195. (setenv "PATH" (string-append #$%tar-bootstrap "/bin"))
  196. (mkdir "base")
  197. (with-directory-excursion "base"
  198. (invoke "tar" "xvf" #$tarball))
  199. (match (find-files "base" "layer.tar")
  200. ((layer)
  201. (invoke "tar" "xvf" layer)))
  202. (when
  203. (and (file-exists? (string-append bin "/guile"))
  204. (file-exists? "var/guix/db/db.sqlite")
  205. (file-is-directory? "tmp")
  206. (string=? (string-append #$%bootstrap-guile "/bin")
  207. (pk 'binlink (readlink bin)))
  208. (string=? (string-append #$profile "/bin/guile")
  209. (pk 'guilelink (readlink "bin/Guile"))))
  210. (mkdir #$output)))))))
  211. (built-derivations (list check))))
  212. (unless store (test-skip 1))
  213. (test-assertm "squashfs-image + localstatedir" store
  214. (mlet* %store-monad
  215. ((guile (set-guile-for-build (default-guile)))
  216. (profile (profile-derivation (packages->manifest
  217. (list %bootstrap-guile))
  218. #:hooks '()
  219. #:locales? #f))
  220. (image (squashfs-image "squashfs-pack" profile
  221. #:symlinks '(("/bin" -> "bin"))
  222. #:localstatedir? #t))
  223. (check (gexp->derivation
  224. "check-tarball"
  225. (with-imported-modules '((guix build utils))
  226. #~(begin
  227. (use-modules (guix build utils)
  228. (ice-9 match))
  229. (define bin
  230. (string-append "." #$profile "/bin"))
  231. (setenv "PATH"
  232. (string-append #$squashfs-tools "/bin"))
  233. (invoke "unsquashfs" #$image)
  234. (with-directory-excursion "squashfs-root"
  235. (when (and (file-exists? (string-append bin
  236. "/guile"))
  237. (file-exists? "var/guix/db/db.sqlite")
  238. (string=? (string-append #$%bootstrap-guile "/bin")
  239. (pk 'binlink (readlink bin)))
  240. ;; This is a relative symlink target.
  241. (string=? (string-drop
  242. (string-append #$profile "/bin")
  243. 1)
  244. (pk 'guilelink (readlink "bin"))))
  245. (mkdir #$output))))))))
  246. (built-derivations (list check))))
  247. (unless store (test-skip 1))
  248. (test-assertm "deb archive with symlinks and control files" store
  249. (mlet* %store-monad
  250. ((guile (set-guile-for-build (default-guile)))
  251. (profile (profile-derivation (packages->manifest
  252. (list %bootstrap-guile))
  253. #:hooks '()
  254. #:locales? #f))
  255. (deb (debian-archive
  256. "deb-pack" profile
  257. #:compressor %gzip-compressor
  258. #:symlinks '(("/opt/gnu/bin" -> "bin"))
  259. #:archiver %tar-bootstrap
  260. #:extra-options
  261. (list #:triggers-file
  262. (plain-file "triggers"
  263. "activate-noawait /usr/share/icons/hicolor\n")
  264. #:postinst-file
  265. (plain-file "postinst"
  266. "echo running configure script\n"))))
  267. (check
  268. (gexp->derivation "check-deb-pack"
  269. (with-imported-modules '((guix build utils))
  270. #~(begin
  271. (use-modules (guix build utils)
  272. (ice-9 match)
  273. (ice-9 popen)
  274. (ice-9 rdelim)
  275. (ice-9 textual-ports)
  276. (rnrs base))
  277. (setenv "PATH" (string-join
  278. (list (string-append #+%tar-bootstrap "/bin")
  279. (string-append #+dpkg "/bin")
  280. (string-append #+%ar-bootstrap "/bin"))
  281. ":"))
  282. ;; Validate the output of 'dpkg --info'.
  283. (let* ((port (open-pipe* OPEN_READ "dpkg" "--info" #$deb))
  284. (info (get-string-all port))
  285. (exit-val (status:exit-val (close-pipe port))))
  286. (assert (zero? exit-val))
  287. (assert (string-contains
  288. info
  289. (string-append "Package: "
  290. #+(package-name %bootstrap-guile))))
  291. (assert (string-contains
  292. info
  293. (string-append "Version: "
  294. #+(package-version %bootstrap-guile)))))
  295. ;; Sanity check .deb contents.
  296. (invoke "ar" "-xv" #$deb)
  297. (assert (file-exists? "debian-binary"))
  298. (assert (file-exists? "data.tar.gz"))
  299. (assert (file-exists? "control.tar.gz"))
  300. ;; Verify there are no hard links in data.tar.gz, as hard
  301. ;; links would cause dpkg to fail unpacking the archive.
  302. (define hard-links
  303. (let ((port (open-pipe* OPEN_READ "tar" "-tvf" "data.tar.gz")))
  304. (let loop ((hard-links '()))
  305. (match (read-line port)
  306. ((? eof-object?)
  307. (assert (zero? (status:exit-val (close-pipe port))))
  308. hard-links)
  309. (line
  310. (if (string-prefix? "u" line)
  311. (loop (cons line hard-links))
  312. (loop hard-links)))))))
  313. (unless (null? hard-links)
  314. (error "hard links found in data.tar.gz" hard-links))
  315. ;; Verify the presence of the control files.
  316. (invoke "tar" "-xf" "control.tar.gz")
  317. (assert (file-exists? "control"))
  318. (assert (and (file-exists? "postinst")
  319. (= #o111 ;script is executable
  320. (logand #o111 (stat:perms
  321. (stat "postinst"))))))
  322. (assert (file-exists? "triggers"))
  323. (mkdir #$output))))))
  324. (built-derivations (list check)))))
  325. (test-end)
  326. ;; Local Variables:
  327. ;; eval: (put 'test-assertm 'scheme-indent-function 2)
  328. ;; End: