pack.scm 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. (unless (network-reachable?) (test-skip 1))
  69. (test-assertm "self-contained-tarball" %store
  70. (mlet* %store-monad
  71. ((profile -> (profile
  72. (content (packages->manifest (list %bootstrap-guile)))
  73. (hooks '())
  74. (locales? #f)))
  75. (tarball (self-contained-tarball "pack" profile
  76. #:symlinks '(("/bin/Guile"
  77. -> "bin/guile"))
  78. #:compressor %gzip-compressor
  79. #:archiver %tar-bootstrap))
  80. (check (gexp->derivation
  81. "check-tarball"
  82. (with-imported-modules '((guix build utils))
  83. #~(begin
  84. (use-modules (guix build utils)
  85. (srfi srfi-1))
  86. (define store
  87. ;; The unpacked store.
  88. (string-append "." (%store-directory) "/"))
  89. (define (canonical? file)
  90. ;; Return #t if FILE is read-only and its mtime is 1.
  91. (let ((st (lstat file)))
  92. (or (not (string-prefix? store file))
  93. (eq? 'symlink (stat:type st))
  94. (and (= 1 (stat:mtime st))
  95. (zero? (logand #o222
  96. (stat:mode st)))))))
  97. (define bin
  98. (string-append "." #$profile "/bin"))
  99. (setenv "PATH"
  100. (string-append #$%tar-bootstrap "/bin"))
  101. (system* "tar" "xvf" #$tarball)
  102. (mkdir #$output)
  103. (exit
  104. (and (file-exists? (string-append bin "/guile"))
  105. (file-exists? store)
  106. (every canonical?
  107. (find-files "." (const #t)
  108. #:directories? #t))
  109. (string=? (string-append #$%bootstrap-guile "/bin")
  110. (readlink bin))
  111. (string=? (string-append ".." #$profile
  112. "/bin/guile")
  113. (readlink "bin/Guile")))))))))
  114. (built-derivations (list check))))
  115. ;; The following test needs guile-sqlite3, libgcrypt, etc. as a consequence of
  116. ;; commit c45477d2a1a651485feede20fe0f3d15aec48b39 and related changes. Thus,
  117. ;; run it on the user's store, if it's available, on the grounds that these
  118. ;; dependencies may be already there, or we can get substitutes or build them
  119. ;; quite inexpensively; see <https://bugs.gnu.org/32184>.
  120. (with-external-store store
  121. (unless store (test-skip 1))
  122. (test-assertm "self-contained-tarball + localstatedir" store
  123. (mlet* %store-monad
  124. ((guile (set-guile-for-build (default-guile)))
  125. (profile -> (profile
  126. (content (packages->manifest (list %bootstrap-guile)))
  127. (hooks '())
  128. (locales? #f)))
  129. (tarball (self-contained-tarball "tar-pack" profile
  130. #:localstatedir? #t))
  131. (check (gexp->derivation
  132. "check-tarball"
  133. #~(let ((bin (string-append "." #$profile "/bin")))
  134. (setenv "PATH"
  135. (string-append #$%tar-bootstrap "/bin"))
  136. (system* "tar" "xvf" #$tarball)
  137. (mkdir #$output)
  138. (exit
  139. (and (file-exists? "var/guix/db/db.sqlite")
  140. (string=? (string-append #$%bootstrap-guile "/bin")
  141. (readlink bin))))))))
  142. (built-derivations (list check))))
  143. (unless store (test-skip 1))
  144. (test-assertm "self-contained-tarball + localstatedir, UTF-8 file names" store
  145. (mlet* %store-monad
  146. ((guile (set-guile-for-build (default-guile)))
  147. (tree (interned-file-tree
  148. `("directory-with-utf8-file-names" directory
  149. ("α" regular (data "alpha"))
  150. ("λ" regular (data "lambda")))))
  151. (tarball (self-contained-tarball "tar-pack" tree
  152. #:localstatedir? #t))
  153. (check (gexp->derivation
  154. "check-tarball"
  155. (with-extensions (list guile-sqlite3 guile-gcrypt)
  156. (with-imported-modules (source-module-closure
  157. '((guix store database)))
  158. #~(begin
  159. (use-modules (guix store database)
  160. (rnrs io ports)
  161. (srfi srfi-1))
  162. (define (valid-file? basename data)
  163. (define file
  164. (string-append "./" #$tree "/" basename))
  165. (string=? (call-with-input-file (pk 'file file)
  166. get-string-all)
  167. data))
  168. (setenv "PATH"
  169. (string-append #$%tar-bootstrap "/bin"))
  170. (system* "tar" "xvf" #$tarball)
  171. (sql-schema
  172. #$(local-file (search-path %load-path
  173. "guix/store/schema.sql")))
  174. (with-database "var/guix/db/db.sqlite" db
  175. ;; Make sure non-ASCII file names are properly
  176. ;; handled.
  177. (setenv "GUIX_LOCPATH"
  178. #+(file-append glibc-utf8-locales
  179. "/lib/locale"))
  180. (setlocale LC_ALL "en_US.utf8")
  181. (mkdir #$output)
  182. (exit
  183. (and (every valid-file?
  184. '("α" "λ")
  185. '("alpha" "lambda"))
  186. (integer? (path-id db #$tree)))))))))))
  187. (built-derivations (list check))))
  188. (unless store (test-skip 1))
  189. (test-assertm "docker-image + localstatedir" store
  190. (mlet* %store-monad
  191. ((guile (set-guile-for-build (default-guile)))
  192. (profile -> (profile
  193. (content (packages->manifest (list %bootstrap-guile)))
  194. (hooks '())
  195. (locales? #f)))
  196. (tarball (docker-image "docker-pack" profile
  197. #:symlinks '(("/bin/Guile" -> "bin/guile"))
  198. #:localstatedir? #t))
  199. (check (gexp->derivation
  200. "check-tarball"
  201. (with-imported-modules '((guix build utils))
  202. #~(begin
  203. (use-modules (guix build utils)
  204. (ice-9 match))
  205. (define bin
  206. (string-append "." #$profile "/bin"))
  207. (setenv "PATH" (string-append #$%tar-bootstrap "/bin"))
  208. (mkdir "base")
  209. (with-directory-excursion "base"
  210. (invoke "tar" "xvf" #$tarball))
  211. (match (find-files "base" "layer.tar")
  212. ((layer)
  213. (invoke "tar" "xvf" layer)))
  214. (when
  215. (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 "check-deb-pack"
  281. (with-imported-modules '((guix build utils))
  282. #~(begin
  283. (use-modules (guix build utils)
  284. (ice-9 match)
  285. (ice-9 popen)
  286. (ice-9 rdelim)
  287. (ice-9 textual-ports)
  288. (rnrs base))
  289. (setenv "PATH" (string-join
  290. (list (string-append #+%tar-bootstrap "/bin")
  291. (string-append #+dpkg "/bin")
  292. (string-append #+%ar-bootstrap "/bin"))
  293. ":"))
  294. ;; Validate the output of 'dpkg --info'.
  295. (let* ((port (open-pipe* OPEN_READ "dpkg" "--info" #$deb))
  296. (info (get-string-all port))
  297. (exit-val (status:exit-val (close-pipe port))))
  298. (assert (zero? exit-val))
  299. (assert (string-contains
  300. info
  301. (string-append "Package: "
  302. #+(package-name %bootstrap-guile))))
  303. (assert (string-contains
  304. info
  305. (string-append "Version: "
  306. #+(package-version %bootstrap-guile)))))
  307. ;; Sanity check .deb contents.
  308. (invoke "ar" "-xv" #$deb)
  309. (assert (file-exists? "debian-binary"))
  310. (assert (file-exists? "data.tar.gz"))
  311. (assert (file-exists? "control.tar.gz"))
  312. ;; Verify there are no hard links in data.tar.gz, as hard
  313. ;; links would cause dpkg to fail unpacking the archive.
  314. (define hard-links
  315. (let ((port (open-pipe* OPEN_READ "tar" "-tvf" "data.tar.gz")))
  316. (let loop ((hard-links '()))
  317. (match (read-line port)
  318. ((? eof-object?)
  319. (assert (zero? (status:exit-val (close-pipe port))))
  320. hard-links)
  321. (line
  322. (if (string-prefix? "u" line)
  323. (loop (cons line hard-links))
  324. (loop hard-links)))))))
  325. (unless (null? hard-links)
  326. (error "hard links found in data.tar.gz" hard-links))
  327. ;; Verify the presence of the control files.
  328. (invoke "tar" "-xf" "control.tar.gz")
  329. (assert (file-exists? "control"))
  330. (assert (and (file-exists? "postinst")
  331. (= #o111 ;script is executable
  332. (logand #o111 (stat:perms
  333. (stat "postinst"))))))
  334. (assert (file-exists? "triggers"))
  335. (mkdir #$output))))))
  336. (built-derivations (list check))))
  337. (unless store (test-skip 1))
  338. (test-assertm "rpm archive can be installed/uninstalled" store
  339. (mlet* %store-monad
  340. ((guile (set-guile-for-build (default-guile)))
  341. (profile -> (profile
  342. (content (packages->manifest (list %bootstrap-guile)))
  343. (hooks '())
  344. (locales? #f)))
  345. (rpm-pack (rpm-archive "rpm-pack" profile
  346. #:compressor %gzip-compressor
  347. #:symlinks '(("/bin/guile" -> "bin/guile"))
  348. #:extra-options '(#:relocatable? #t)))
  349. (check
  350. (gexp->derivation "check-rpm-pack"
  351. (with-imported-modules (source-module-closure
  352. '((guix build utils)))
  353. #~(begin
  354. (use-modules (guix build utils))
  355. (define fakeroot #+(file-append fakeroot "/bin/fakeroot"))
  356. (define rpm #+(file-append rpm-for-tests "/bin/rpm"))
  357. (mkdir-p "/tmp/lib/rpm")
  358. ;; Install the RPM package. This causes RPM to validate the
  359. ;; signatures, header as well as the file digests, which
  360. ;; makes it a rather thorough test.
  361. (mkdir "test-prefix")
  362. (invoke fakeroot rpm "--install"
  363. (string-append "--prefix=" (getcwd) "/test-prefix")
  364. #$rpm-pack)
  365. ;; Invoke the installed Guile command.
  366. (invoke "./test-prefix/bin/guile" "--version")
  367. ;; Uninstall the RPM package.
  368. (invoke fakeroot rpm "--erase" "guile-bootstrap")
  369. ;; Required so the above is run.
  370. (mkdir #$output))))))
  371. (built-derivations (list check)))))
  372. (test-end)
  373. ;; Local Variables:
  374. ;; eval: (put 'test-assertm 'scheme-indent-function 2)
  375. ;; End: