pack.scm 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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, 2023 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 tests)
  28. #:use-module (guix gexp)
  29. #:use-module (guix modules)
  30. #:use-module (guix utils)
  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 package-management) #:select (rpm))
  35. #:use-module ((gnu packages compression) #:select (squashfs-tools))
  36. #:use-module ((gnu packages debian) #:select (dpkg))
  37. #:use-module ((gnu packages guile) #:select (guile-sqlite3))
  38. #:use-module ((gnu packages gnupg) #:select (guile-gcrypt))
  39. #:use-module ((gnu packages linux) #:select (fakeroot))
  40. #:use-module (srfi srfi-64))
  41. (define %store
  42. (open-connection-for-tests))
  43. ;; Globally disable grafts because they can trigger early builds.
  44. (%graft? #f)
  45. (define-syntax-rule (test-assertm name store exp)
  46. (test-assert name
  47. (let ((guile (package-derivation store %bootstrap-guile)))
  48. (run-with-store store exp
  49. #:guile-for-build guile))))
  50. (define %gzip-compressor
  51. ;; Compressor that uses the bootstrap 'gzip'.
  52. ((@ (guix scripts pack) compressor) "gzip"
  53. ".gz"
  54. #~(list #+(file-append %bootstrap-coreutils&co "/bin/gzip") "-6n")))
  55. (define %tar-bootstrap %bootstrap-coreutils&co)
  56. (define %ar-bootstrap %bootstrap-binutils)
  57. ;;; This is a variant of the RPM package configured so that its database can
  58. ;;; be created on a writable location readily available inside the build
  59. ;;; container ("/tmp").
  60. (define rpm-for-tests
  61. (package
  62. (inherit rpm)
  63. (arguments (substitute-keyword-arguments (package-arguments rpm)
  64. ((#:configure-flags flags '())
  65. #~(cons "--localstatedir=/tmp"
  66. (delete "--localstatedir=/var" #$flags)))))))
  67. (test-begin "pack")
  68. ;; The following test needs guile-sqlite3, libgcrypt, etc. as a consequence of
  69. ;; commit c45477d2a1a651485feede20fe0f3d15aec48b39 and related changes. Thus,
  70. ;; run it on the user's store, if it's available, on the grounds that these
  71. ;; dependencies may be already there, or we can get substitutes or build them
  72. ;; quite inexpensively; see <https://bugs.gnu.org/32184>.
  73. (with-external-store store
  74. (unless store (test-skip 1))
  75. (test-assertm "self-contained-tarball" store
  76. (mlet* %store-monad
  77. ((guile (set-guile-for-build (default-guile)))
  78. (profile -> (profile
  79. (content (packages->manifest (list %bootstrap-guile)))
  80. (hooks '())
  81. (locales? #f)))
  82. (tarball (self-contained-tarball "pack" profile
  83. #:symlinks '(("/bin/Guile"
  84. -> "bin/guile"))
  85. #:compressor %gzip-compressor
  86. #:archiver %tar-bootstrap))
  87. (check (gexp->derivation
  88. "check-tarball"
  89. (with-imported-modules '((guix build utils))
  90. #~(begin
  91. (use-modules (guix build utils)
  92. (srfi srfi-1))
  93. (define store
  94. ;; The unpacked store.
  95. (string-append "." (%store-directory) "/"))
  96. (define (canonical? file)
  97. ;; Return #t if FILE is read-only and its mtime is 1.
  98. (let ((st (lstat file)))
  99. (or (not (string-prefix? store file))
  100. (eq? 'symlink (stat:type st))
  101. (and (= 1 (stat:mtime st))
  102. (zero? (logand #o222
  103. (stat:mode st)))))))
  104. (define bin
  105. (string-append "." #$profile "/bin"))
  106. (setenv "PATH"
  107. (string-append #$%tar-bootstrap "/bin"))
  108. (system* "tar" "xvf" #$tarball)
  109. (mkdir #$output)
  110. (exit
  111. (and (file-exists? (string-append bin "/guile"))
  112. (file-exists? store)
  113. (every canonical?
  114. (find-files "." (const #t)
  115. #:directories? #t))
  116. (string=? (string-append #$%bootstrap-guile "/bin")
  117. (readlink bin))
  118. (string=? (string-append ".." #$profile
  119. "/bin/guile")
  120. (readlink "bin/Guile")))))))))
  121. (built-derivations (list check))))
  122. (unless store (test-skip 1))
  123. (test-assertm "self-contained-tarball + localstatedir" store
  124. (mlet* %store-monad
  125. ((guile (set-guile-for-build (default-guile)))
  126. (profile -> (profile
  127. (content (packages->manifest (list %bootstrap-guile)))
  128. (hooks '())
  129. (locales? #f)))
  130. (tarball (self-contained-tarball "tar-pack" profile
  131. #:localstatedir? #t))
  132. (check (gexp->derivation
  133. "check-tarball"
  134. #~(let ((bin (string-append "." #$profile "/bin")))
  135. (setenv "PATH"
  136. (string-append #$%tar-bootstrap "/bin"))
  137. (system* "tar" "xvf" #$tarball)
  138. (mkdir #$output)
  139. (exit
  140. (and (file-exists? "var/guix/db/db.sqlite")
  141. (string=? (string-append #$%bootstrap-guile "/bin")
  142. (readlink bin))))))))
  143. (built-derivations (list check))))
  144. (unless store (test-skip 1))
  145. (test-assertm "self-contained-tarball + localstatedir, UTF-8 file names" store
  146. (mlet* %store-monad
  147. ((guile (set-guile-for-build (default-guile)))
  148. (tree (interned-file-tree
  149. `("directory-with-utf8-file-names" directory
  150. ("α" regular (data "alpha"))
  151. ("λ" regular (data "lambda")))))
  152. (tarball (self-contained-tarball "tar-pack" tree
  153. #:localstatedir? #t))
  154. (check (gexp->derivation
  155. "check-tarball"
  156. (with-extensions (list guile-sqlite3 guile-gcrypt)
  157. (with-imported-modules (source-module-closure
  158. '((guix store database)))
  159. #~(begin
  160. (use-modules (guix store database)
  161. (rnrs io ports)
  162. (srfi srfi-1))
  163. (define (valid-file? basename data)
  164. (define file
  165. (string-append "./" #$tree "/" basename))
  166. (string=? (call-with-input-file (pk 'file file)
  167. get-string-all)
  168. data))
  169. (setenv "PATH"
  170. (string-append #$%tar-bootstrap "/bin"))
  171. (system* "tar" "xvf" #$tarball)
  172. (sql-schema
  173. #$(local-file (search-path %load-path
  174. "guix/store/schema.sql")))
  175. (with-database "var/guix/db/db.sqlite" db
  176. ;; Make sure non-ASCII file names are properly
  177. ;; handled.
  178. (setenv "GUIX_LOCPATH"
  179. #+(file-append glibc-utf8-locales
  180. "/lib/locale"))
  181. (setlocale LC_ALL "en_US.utf8")
  182. (mkdir #$output)
  183. (exit
  184. (and (every valid-file?
  185. '("α" "λ")
  186. '("alpha" "lambda"))
  187. (integer? (path-id db #$tree)))))))))))
  188. (built-derivations (list check))))
  189. (unless store (test-skip 1))
  190. (test-assertm "docker-image + localstatedir" store
  191. (mlet* %store-monad
  192. ((guile (set-guile-for-build (default-guile)))
  193. (profile -> (profile
  194. (content (packages->manifest (list %bootstrap-guile)))
  195. (hooks '())
  196. (locales? #f)))
  197. (tarball (docker-image "docker-pack" profile
  198. #:symlinks '(("/bin/Guile" -> "bin/guile"))
  199. #:localstatedir? #t))
  200. (check (gexp->derivation
  201. "check-tarball"
  202. (with-imported-modules '((guix build utils))
  203. #~(begin
  204. (use-modules (guix build utils)
  205. (ice-9 match))
  206. (define bin
  207. (string-append "." #$profile "/bin"))
  208. (setenv "PATH" (string-append #$%tar-bootstrap "/bin"))
  209. (mkdir "base")
  210. (with-directory-excursion "base"
  211. (invoke "tar" "xvf" #$tarball))
  212. (match (find-files "base" "layer.tar")
  213. ((layer)
  214. (invoke "tar" "xvf" layer)))
  215. (when (and (file-exists? (string-append bin "/guile"))
  216. (file-exists? "var/guix/db/db.sqlite")
  217. (file-is-directory? "tmp")
  218. (string=? (string-append #$%bootstrap-guile "/bin")
  219. (pk 'binlink (readlink bin)))
  220. (string=? (string-append #$profile "/bin/guile")
  221. (pk 'guilelink (readlink "bin/Guile"))))
  222. (mkdir #$output)))))))
  223. (built-derivations (list check))))
  224. (unless store (test-skip 1))
  225. (test-assertm "squashfs-image + localstatedir" store
  226. (mlet* %store-monad
  227. ((guile (set-guile-for-build (default-guile)))
  228. (profile -> (profile
  229. (content (packages->manifest (list %bootstrap-guile)))
  230. (hooks '())
  231. (locales? #f)))
  232. (image (squashfs-image "squashfs-pack" profile
  233. #:symlinks '(("/bin" -> "bin"))
  234. #:localstatedir? #t))
  235. (check (gexp->derivation
  236. "check-tarball"
  237. (with-imported-modules '((guix build utils))
  238. #~(begin
  239. (use-modules (guix build utils)
  240. (ice-9 match))
  241. (define bin
  242. (string-append "." #$profile "/bin"))
  243. (setenv "PATH"
  244. (string-append #$squashfs-tools "/bin"))
  245. (invoke "unsquashfs" #$image)
  246. (with-directory-excursion "squashfs-root"
  247. (when (and (file-exists? (string-append bin
  248. "/guile"))
  249. (file-exists? "var/guix/db/db.sqlite")
  250. (string=? (string-append #$%bootstrap-guile "/bin")
  251. (pk 'binlink (readlink bin)))
  252. ;; This is a relative symlink target.
  253. (string=? (string-drop
  254. (string-append #$profile "/bin")
  255. 1)
  256. (pk 'guilelink (readlink "bin"))))
  257. (mkdir #$output))))))))
  258. (built-derivations (list check))))
  259. (unless store (test-skip 1))
  260. (test-assertm "deb archive with symlinks and control files" store
  261. (mlet* %store-monad
  262. ((guile (set-guile-for-build (default-guile)))
  263. (profile -> (profile
  264. (content (packages->manifest (list %bootstrap-guile)))
  265. (hooks '())
  266. (locales? #f)))
  267. (deb (debian-archive
  268. "deb-pack" profile
  269. #:compressor %gzip-compressor
  270. #:symlinks '(("/opt/gnu/bin" -> "bin"))
  271. #:archiver %tar-bootstrap
  272. #:extra-options
  273. (list #:triggers-file
  274. (plain-file "triggers"
  275. "activate-noawait /usr/share/icons/hicolor\n")
  276. #:postinst-file
  277. (plain-file "postinst"
  278. "echo running configure script\n"))))
  279. (check
  280. (gexp->derivation
  281. "check-deb-pack"
  282. (with-imported-modules '((guix build utils))
  283. #~(begin
  284. (use-modules (guix build utils)
  285. (ice-9 match)
  286. (ice-9 popen)
  287. (ice-9 rdelim)
  288. (ice-9 textual-ports)
  289. (rnrs base))
  290. (setenv "PATH" (string-join
  291. (list (string-append #+%tar-bootstrap "/bin")
  292. (string-append #+dpkg "/bin")
  293. (string-append #+%ar-bootstrap "/bin"))
  294. ":"))
  295. ;; Validate the output of 'dpkg --info'.
  296. (let* ((port (open-pipe* OPEN_READ "dpkg" "--info" #$deb))
  297. (info (get-string-all port))
  298. (exit-val (status:exit-val (close-pipe port))))
  299. (assert (zero? exit-val))
  300. (assert (string-contains
  301. info
  302. (string-append "Package: "
  303. #+(package-name %bootstrap-guile))))
  304. (assert (string-contains
  305. info
  306. (string-append "Version: "
  307. #+(package-version %bootstrap-guile)))))
  308. ;; Sanity check .deb contents.
  309. (invoke "ar" "-xv" #$deb)
  310. (assert (file-exists? "debian-binary"))
  311. (assert (file-exists? "data.tar.gz"))
  312. (assert (file-exists? "control.tar.gz"))
  313. ;; Verify there are no hard links in data.tar.gz, as hard
  314. ;; links would cause dpkg to fail unpacking the archive.
  315. (define hard-links
  316. (let ((port (open-pipe* OPEN_READ "tar" "-tvf" "data.tar.gz")))
  317. (let loop ((hard-links '()))
  318. (match (read-line port)
  319. ((? eof-object?)
  320. (assert (zero? (status:exit-val (close-pipe port))))
  321. hard-links)
  322. (line
  323. (if (string-prefix? "u" line)
  324. (loop (cons line hard-links))
  325. (loop hard-links)))))))
  326. (unless (null? hard-links)
  327. (error "hard links found in data.tar.gz" hard-links))
  328. ;; Verify the presence of the control files.
  329. (invoke "tar" "-xf" "control.tar.gz")
  330. (assert (file-exists? "control"))
  331. (assert (and (file-exists? "postinst")
  332. (= #o111 ;script is executable
  333. (logand #o111 (stat:perms
  334. (stat "postinst"))))))
  335. (assert (file-exists? "triggers"))
  336. (mkdir #$output))))))
  337. (built-derivations (list check))))
  338. (unless store (test-skip 1))
  339. (test-assertm "rpm archive can be installed/uninstalled" store
  340. (mlet* %store-monad
  341. ((guile (set-guile-for-build (default-guile)))
  342. (profile -> (profile
  343. (content (packages->manifest (list %bootstrap-guile)))
  344. (hooks '())
  345. (locales? #f)))
  346. (rpm-pack (rpm-archive "rpm-pack" profile
  347. #:compressor %gzip-compressor
  348. #:symlinks '(("/bin/guile" -> "bin/guile"))
  349. #:extra-options '(#:relocatable? #t)))
  350. (check
  351. (gexp->derivation
  352. "check-rpm-pack"
  353. (with-imported-modules (source-module-closure
  354. '((guix build utils)))
  355. #~(begin
  356. (use-modules (guix build utils))
  357. (define fakeroot #+(file-append fakeroot "/bin/fakeroot"))
  358. (define rpm #+(file-append rpm-for-tests "/bin/rpm"))
  359. (mkdir-p "/tmp/lib/rpm")
  360. ;; Install the RPM package. This causes RPM to validate the
  361. ;; signatures, header as well as the file digests, which
  362. ;; makes it a rather thorough test.
  363. (mkdir "test-prefix")
  364. (invoke fakeroot rpm "--install"
  365. (string-append "--prefix=" (getcwd) "/test-prefix")
  366. #$rpm-pack)
  367. ;; Invoke the installed Guile command.
  368. (invoke "./test-prefix/bin/guile" "--version")
  369. ;; Uninstall the RPM package.
  370. (invoke fakeroot rpm "--erase" "guile-bootstrap")
  371. ;; Required so the above is run.
  372. (mkdir #$output))))))
  373. (built-derivations (list check)))))
  374. (test-end)
  375. ;; Local Variables:
  376. ;; eval: (put 'test-assertm 'scheme-indent-function 2)
  377. ;; End: