profiles.scm 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2014 Alex Kost <alezost@gmail.com>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (test-profiles)
  20. #:use-module (guix tests)
  21. #:use-module (guix profiles)
  22. #:use-module (guix store)
  23. #:use-module (guix monads)
  24. #:use-module (guix grafts)
  25. #:use-module (guix packages)
  26. #:use-module (guix derivations)
  27. #:use-module (guix build-system trivial)
  28. #:use-module (gnu packages bootstrap)
  29. #:use-module ((gnu packages base) #:prefix packages:)
  30. #:use-module ((gnu packages guile) #:prefix packages:)
  31. #:use-module (ice-9 match)
  32. #:use-module (ice-9 regex)
  33. #:use-module (ice-9 popen)
  34. #:use-module (rnrs io ports)
  35. #:use-module (srfi srfi-1)
  36. #:use-module (srfi srfi-11)
  37. #:use-module (srfi srfi-34)
  38. #:use-module (srfi srfi-64))
  39. ;; Test the (guix profiles) module.
  40. (define %store
  41. (open-connection-for-tests))
  42. ;; Globally disable grafts because they can trigger early builds.
  43. (%graft? #f)
  44. (define-syntax-rule (test-assertm name exp)
  45. (test-assert name
  46. (run-with-store %store exp
  47. #:guile-for-build (%guile-for-build))))
  48. (define-syntax-rule (test-equalm name value exp)
  49. (test-equal name
  50. value
  51. (run-with-store %store exp
  52. #:guile-for-build (%guile-for-build))))
  53. ;; Example manifest entries.
  54. (define guile-1.8.8
  55. (manifest-entry
  56. (name "guile")
  57. (version "1.8.8")
  58. (item "/gnu/store/...")
  59. (output "out")))
  60. (define guile-2.0.9
  61. (manifest-entry
  62. (name "guile")
  63. (version "2.0.9")
  64. (item "/gnu/store/...")
  65. (output "out")))
  66. (define guile-2.0.9:debug
  67. (manifest-entry (inherit guile-2.0.9)
  68. (output "debug")))
  69. (define glibc
  70. (manifest-entry
  71. (name "glibc")
  72. (version "2.19")
  73. (item "/gnu/store/...")
  74. (output "out")))
  75. (test-begin "profiles")
  76. (test-assert "manifest-installed?"
  77. (let ((m (manifest (list guile-2.0.9 guile-2.0.9:debug))))
  78. (and (manifest-installed? m (manifest-pattern (name "guile")))
  79. (manifest-installed? m (manifest-pattern
  80. (name "guile") (output "debug")))
  81. (manifest-installed? m (manifest-pattern
  82. (name "guile") (output "out")
  83. (version "2.0.9")))
  84. (not (manifest-installed?
  85. m (manifest-pattern (name "guile") (version "1.8.8"))))
  86. (not (manifest-installed?
  87. m (manifest-pattern (name "guile") (output "foobar")))))))
  88. (test-assert "manifest-matching-entries"
  89. (let* ((e (list guile-2.0.9 guile-2.0.9:debug))
  90. (m (manifest e)))
  91. (and (null? (manifest-matching-entries m
  92. (list (manifest-pattern
  93. (name "python")))))
  94. (equal? e
  95. (manifest-matching-entries m
  96. (list (manifest-pattern
  97. (name "guile")
  98. (output #f)))))
  99. (equal? (list guile-2.0.9)
  100. (manifest-matching-entries m
  101. (list (manifest-pattern
  102. (name "guile")
  103. (version "2.0.9"))))))))
  104. (test-assert "manifest-remove"
  105. (let* ((m0 (manifest (list guile-2.0.9 guile-2.0.9:debug)))
  106. (m1 (manifest-remove m0
  107. (list (manifest-pattern (name "guile")))))
  108. (m2 (manifest-remove m1
  109. (list (manifest-pattern (name "guile"))))) ; same
  110. (m3 (manifest-remove m2
  111. (list (manifest-pattern
  112. (name "guile") (output "debug")))))
  113. (m4 (manifest-remove m3
  114. (list (manifest-pattern (name "guile"))))))
  115. (match (manifest-entries m2)
  116. ((($ <manifest-entry> "guile" "2.0.9" "debug"))
  117. (and (equal? m1 m2)
  118. (null? (manifest-entries m3))
  119. (null? (manifest-entries m4)))))))
  120. (test-assert "manifest-add"
  121. (let* ((m0 (manifest '()))
  122. (m1 (manifest-add m0 (list guile-1.8.8)))
  123. (m2 (manifest-add m1 (list guile-2.0.9)))
  124. (m3 (manifest-add m2 (list guile-2.0.9:debug)))
  125. (m4 (manifest-add m3 (list guile-2.0.9:debug))))
  126. (and (match (manifest-entries m1)
  127. ((($ <manifest-entry> "guile" "1.8.8" "out")) #t)
  128. (_ #f))
  129. (match (manifest-entries m2)
  130. ((($ <manifest-entry> "guile" "2.0.9" "out")) #t)
  131. (_ #f))
  132. (equal? m3 m4))))
  133. (test-assert "manifest-perform-transaction"
  134. (let* ((m0 (manifest (list guile-2.0.9 guile-2.0.9:debug)))
  135. (t1 (manifest-transaction
  136. (install (list guile-1.8.8))
  137. (remove (list (manifest-pattern (name "guile")
  138. (output "debug"))))))
  139. (t2 (manifest-transaction
  140. (remove (list (manifest-pattern (name "guile")
  141. (version "2.0.9")
  142. (output #f))))))
  143. (m1 (manifest-perform-transaction m0 t1))
  144. (m2 (manifest-perform-transaction m1 t2))
  145. (m3 (manifest-perform-transaction m0 t2)))
  146. (and (match (manifest-entries m1)
  147. ((($ <manifest-entry> "guile" "1.8.8" "out")) #t)
  148. (_ #f))
  149. (equal? m1 m2)
  150. (null? (manifest-entries m3)))))
  151. (test-assert "manifest-transaction-effects"
  152. (let* ((m0 (manifest (list guile-1.8.8)))
  153. (t (manifest-transaction
  154. (install (list guile-2.0.9 glibc))
  155. (remove (list (manifest-pattern (name "coreutils")))))))
  156. (let-values (((remove install upgrade downgrade)
  157. (manifest-transaction-effects m0 t)))
  158. (and (null? remove) (null? downgrade)
  159. (equal? (list glibc) install)
  160. (equal? (list (cons guile-1.8.8 guile-2.0.9)) upgrade)))))
  161. (test-assert "manifest-transaction-effects and downgrades"
  162. (let* ((m0 (manifest (list guile-2.0.9)))
  163. (t (manifest-transaction (install (list guile-1.8.8)))))
  164. (let-values (((remove install upgrade downgrade)
  165. (manifest-transaction-effects m0 t)))
  166. (and (null? remove) (null? install) (null? upgrade)
  167. (equal? (list (cons guile-2.0.9 guile-1.8.8)) downgrade)))))
  168. (test-assert "manifest-transaction-effects and pseudo-upgrades"
  169. (let* ((m0 (manifest (list guile-2.0.9)))
  170. (t (manifest-transaction (install (list guile-2.0.9)))))
  171. (let-values (((remove install upgrade downgrade)
  172. (manifest-transaction-effects m0 t)))
  173. (and (null? remove) (null? install) (null? downgrade)
  174. (equal? (list (cons guile-2.0.9 guile-2.0.9)) upgrade)))))
  175. (test-assert "manifest-transaction-null?"
  176. (manifest-transaction-null? (manifest-transaction)))
  177. (test-assert "manifest-transaction-removal-candidate?"
  178. (let ((m (manifest (list guile-2.0.9)))
  179. (t (manifest-transaction
  180. (remove (list (manifest-pattern (name "guile")))))))
  181. (and (manifest-transaction-removal-candidate? guile-2.0.9 t)
  182. (not (manifest-transaction-removal-candidate? glibc t)))))
  183. (test-assertm "profile-derivation"
  184. (mlet* %store-monad
  185. ((entry -> (package->manifest-entry %bootstrap-guile))
  186. (guile (package->derivation %bootstrap-guile))
  187. (drv (profile-derivation (manifest (list entry))
  188. #:hooks '()
  189. #:locales? #f))
  190. (profile -> (derivation->output-path drv))
  191. (bindir -> (string-append profile "/bin"))
  192. (_ (built-derivations (list drv))))
  193. (return (and (file-exists? (string-append bindir "/guile"))
  194. (string=? (dirname (readlink bindir))
  195. (derivation->output-path guile))))))
  196. (test-assertm "profile-derivation, inputs"
  197. (mlet* %store-monad
  198. ((entry -> (package->manifest-entry packages:glibc "debug"))
  199. (drv (profile-derivation (manifest (list entry))
  200. #:hooks '()
  201. #:locales? #f)))
  202. (return (derivation-inputs drv))))
  203. (test-assertm "profile-derivation, cross-compilation"
  204. (mlet* %store-monad
  205. ((manifest -> (packages->manifest (list packages:sed packages:grep)))
  206. (target -> "arm-linux-gnueabihf")
  207. (grep (package->cross-derivation packages:grep target))
  208. (sed (package->cross-derivation packages:sed target))
  209. (locales (package->derivation packages:glibc-utf8-locales))
  210. (drv (profile-derivation manifest
  211. #:hooks '()
  212. #:locales? #t
  213. #:target target)))
  214. (define (find-input name)
  215. (let ((name (string-append name ".drv")))
  216. (any (lambda (input)
  217. (let ((input (derivation-input-path input)))
  218. (and (string-suffix? name input) input)))
  219. (derivation-inputs drv))))
  220. ;; The inputs for grep and sed should be cross-build derivations, but that
  221. ;; for the glibc-utf8-locales should be a native build.
  222. (return (and (string=? (derivation-system drv) (%current-system))
  223. (string=? (find-input (package-full-name packages:grep))
  224. (derivation-file-name grep))
  225. (string=? (find-input (package-full-name packages:sed))
  226. (derivation-file-name sed))
  227. (string=? (find-input
  228. (package-full-name packages:glibc-utf8-locales))
  229. (derivation-file-name locales))))))
  230. (test-assert "package->manifest-entry defaults to \"out\""
  231. (let ((outputs (package-outputs packages:glibc)))
  232. (equal? (manifest-entry-output
  233. (package->manifest-entry (package
  234. (inherit packages:glibc)
  235. (outputs (reverse outputs)))))
  236. (manifest-entry-output
  237. (package->manifest-entry packages:glibc))
  238. "out")))
  239. (test-assertm "profile-manifest, search-paths"
  240. (mlet* %store-monad
  241. ((guile -> (package
  242. (inherit %bootstrap-guile)
  243. (native-search-paths
  244. (package-native-search-paths packages:guile-2.0))))
  245. (entry -> (package->manifest-entry guile))
  246. (drv (profile-derivation (manifest (list entry))
  247. #:hooks '()
  248. #:locales? #f))
  249. (profile -> (derivation->output-path drv)))
  250. (mbegin %store-monad
  251. (built-derivations (list drv))
  252. ;; Read the manifest back and make sure search paths are preserved.
  253. (let ((manifest (profile-manifest profile)))
  254. (match (manifest-entries manifest)
  255. ((result)
  256. (return (equal? (manifest-entry-search-paths result)
  257. (manifest-entry-search-paths entry)
  258. (package-native-search-paths
  259. packages:guile-2.0)))))))))
  260. (test-assert "package->manifest-entry, search paths"
  261. ;; See <http://bugs.gnu.org/22073>.
  262. (let ((mpl (@ (gnu packages python) python2-matplotlib)))
  263. (lset= eq?
  264. (package-transitive-native-search-paths mpl)
  265. (manifest-entry-search-paths
  266. (package->manifest-entry mpl)))))
  267. (test-equal "packages->manifest, propagated inputs"
  268. (map (match-lambda
  269. ((label package)
  270. (list (package-name package) (package-version package)
  271. package)))
  272. (package-propagated-inputs packages:guile-2.2))
  273. (map (lambda (entry)
  274. (list (manifest-entry-name entry)
  275. (manifest-entry-version entry)
  276. (manifest-entry-item entry)))
  277. (manifest-entry-dependencies
  278. (package->manifest-entry packages:guile-2.2))))
  279. (test-assert "manifest-entry-parent"
  280. (let ((entry (package->manifest-entry packages:guile-2.2)))
  281. (match (manifest-entry-dependencies entry)
  282. ((dependencies ..1)
  283. (and (every (lambda (parent)
  284. (eq? entry (force parent)))
  285. (map manifest-entry-parent dependencies))
  286. (not (force (manifest-entry-parent entry))))))))
  287. (test-assertm "read-manifest"
  288. (mlet* %store-monad ((manifest -> (packages->manifest
  289. (list (package
  290. (inherit %bootstrap-guile)
  291. (native-search-paths
  292. (package-native-search-paths
  293. packages:guile-2.0))))))
  294. (drv (profile-derivation manifest
  295. #:hooks '()
  296. #:locales? #f))
  297. (out -> (derivation->output-path drv)))
  298. (define (entry->sexp entry)
  299. (list (manifest-entry-name entry)
  300. (manifest-entry-version entry)
  301. (manifest-entry-search-paths entry)
  302. (manifest-entry-dependencies entry)
  303. (force (manifest-entry-parent entry))))
  304. (mbegin %store-monad
  305. (built-derivations (list drv))
  306. (let ((manifest2 (profile-manifest out)))
  307. (return (equal? (map entry->sexp (manifest-entries manifest))
  308. (map entry->sexp (manifest-entries manifest2))))))))
  309. (test-equal "collision"
  310. '(("guile-bootstrap" "2.0") ("guile-bootstrap" "42"))
  311. (guard (c ((profile-collision-error? c)
  312. (let ((entry1 (profile-collision-error-entry c))
  313. (entry2 (profile-collision-error-conflict c)))
  314. (list (list (manifest-entry-name entry1)
  315. (manifest-entry-version entry1))
  316. (list (manifest-entry-name entry2)
  317. (manifest-entry-version entry2))))))
  318. (run-with-store %store
  319. (mlet* %store-monad ((p0 -> (package
  320. (inherit %bootstrap-guile)
  321. (version "42")))
  322. (p1 -> (dummy-package "p1"
  323. (propagated-inputs `(("p0" ,p0)))))
  324. (manifest -> (packages->manifest
  325. (list %bootstrap-guile p1)))
  326. (drv (profile-derivation manifest
  327. #:hooks '()
  328. #:locales? #f)))
  329. (return #f)))))
  330. (test-equal "collision of propagated inputs"
  331. '(("guile-bootstrap" "2.0") ("guile-bootstrap" "42"))
  332. (guard (c ((profile-collision-error? c)
  333. (let ((entry1 (profile-collision-error-entry c))
  334. (entry2 (profile-collision-error-conflict c)))
  335. (list (list (manifest-entry-name entry1)
  336. (manifest-entry-version entry1))
  337. (list (manifest-entry-name entry2)
  338. (manifest-entry-version entry2))))))
  339. (run-with-store %store
  340. (mlet* %store-monad ((p0 -> (package
  341. (inherit %bootstrap-guile)
  342. (version "42")))
  343. (p1 -> (dummy-package "p1"
  344. (propagated-inputs
  345. `(("guile" ,%bootstrap-guile)))))
  346. (p2 -> (dummy-package "p2"
  347. (propagated-inputs
  348. `(("guile" ,p0)))))
  349. (manifest -> (packages->manifest (list p1 p2)))
  350. (drv (profile-derivation manifest
  351. #:hooks '()
  352. #:locales? #f)))
  353. (return #f)))))
  354. (test-assertm "no collision"
  355. ;; Here we have an entry that is "lowered" (its 'item' field is a store file
  356. ;; name) and another entry (its 'item' field is a package) that is
  357. ;; equivalent.
  358. (mlet* %store-monad ((p -> (dummy-package "p"
  359. (propagated-inputs
  360. `(("guile" ,%bootstrap-guile)))))
  361. (guile (package->derivation %bootstrap-guile))
  362. (entry -> (manifest-entry
  363. (inherit (package->manifest-entry
  364. %bootstrap-guile))
  365. (item (derivation->output-path guile))))
  366. (manifest -> (manifest
  367. (list entry
  368. (package->manifest-entry p))))
  369. (drv (profile-derivation manifest)))
  370. (return (->bool drv))))
  371. (test-assertm "etc/profile"
  372. ;; Make sure we get an 'etc/profile' file that at least defines $PATH.
  373. (mlet* %store-monad
  374. ((guile -> (package
  375. (inherit %bootstrap-guile)
  376. (native-search-paths
  377. (package-native-search-paths packages:guile-2.0))))
  378. (entry -> (package->manifest-entry guile))
  379. (drv (profile-derivation (manifest (list entry))
  380. #:hooks '()
  381. #:locales? #f))
  382. (profile -> (derivation->output-path drv)))
  383. (mbegin %store-monad
  384. (built-derivations (list drv))
  385. (let* ((pipe (open-input-pipe
  386. (string-append "unset GUIX_PROFILE; "
  387. ;; 'source' is a Bashism; use '.' (dot).
  388. ". " profile "/etc/profile; "
  389. ;; Don't try to parse set(1) output because
  390. ;; it differs among shells; just use echo.
  391. "echo $PATH")))
  392. (path (get-string-all pipe)))
  393. (return
  394. (and (zero? (close-pipe pipe))
  395. (string-contains path (string-append profile "/bin"))))))))
  396. (test-assertm "etc/profile when etc/ already exists"
  397. ;; Here 'union-build' makes the profile's etc/ a symlink to the package's
  398. ;; etc/ directory, which makes it read-only. Make sure the profile build
  399. ;; handles that.
  400. (mlet* %store-monad
  401. ((thing -> (dummy-package "dummy"
  402. (build-system trivial-build-system)
  403. (arguments
  404. `(#:guile ,%bootstrap-guile
  405. #:builder
  406. (let ((out (assoc-ref %outputs "out")))
  407. (mkdir out)
  408. (mkdir (string-append out "/etc"))
  409. (call-with-output-file (string-append out "/etc/foo")
  410. (lambda (port)
  411. (display "foo!" port))))))))
  412. (entry -> (package->manifest-entry thing))
  413. (drv (profile-derivation (manifest (list entry))
  414. #:hooks '()
  415. #:locales? #f))
  416. (profile -> (derivation->output-path drv)))
  417. (mbegin %store-monad
  418. (built-derivations (list drv))
  419. (return (and (file-exists? (string-append profile "/etc/profile"))
  420. (string=? (call-with-input-file
  421. (string-append profile "/etc/foo")
  422. get-string-all)
  423. "foo!"))))))
  424. (test-assertm "etc/profile when etc/ is a symlink"
  425. ;; When etc/ is a symlink, the unsymlink code in 0.8.2 would fail
  426. ;; gracelessly because 'scandir' would return #f.
  427. (mlet* %store-monad
  428. ((thing -> (dummy-package "dummy"
  429. (build-system trivial-build-system)
  430. (arguments
  431. `(#:guile ,%bootstrap-guile
  432. #:builder
  433. (let ((out (assoc-ref %outputs "out")))
  434. (mkdir out)
  435. (mkdir (string-append out "/foo"))
  436. (symlink "foo" (string-append out "/etc"))
  437. (call-with-output-file (string-append out "/etc/bar")
  438. (lambda (port)
  439. (display "foo!" port))))))))
  440. (entry -> (package->manifest-entry thing))
  441. (drv (profile-derivation (manifest (list entry))
  442. #:hooks '()
  443. #:locales? #f))
  444. (profile -> (derivation->output-path drv)))
  445. (mbegin %store-monad
  446. (built-derivations (list drv))
  447. (return (and (file-exists? (string-append profile "/etc/profile"))
  448. (string=? (call-with-input-file
  449. (string-append profile "/etc/bar")
  450. get-string-all)
  451. "foo!"))))))
  452. (test-equalm "union vs. dangling symlink" ;<https://bugs.gnu.org/26949>
  453. "does-not-exist"
  454. (mlet* %store-monad
  455. ((thing1 -> (dummy-package "dummy"
  456. (build-system trivial-build-system)
  457. (arguments
  458. `(#:guile ,%bootstrap-guile
  459. #:builder
  460. (let ((out (assoc-ref %outputs "out")))
  461. (mkdir out)
  462. (symlink "does-not-exist"
  463. (string-append out "/dangling"))
  464. #t)))))
  465. (thing2 -> (package (inherit thing1) (name "dummy2")))
  466. (drv (profile-derivation (packages->manifest
  467. (list thing1 thing2))
  468. #:hooks '()
  469. #:locales? #f))
  470. (profile -> (derivation->output-path drv)))
  471. (mbegin %store-monad
  472. (built-derivations (list drv))
  473. (return (readlink (readlink (string-append profile "/dangling")))))))
  474. (test-end "profiles")
  475. ;;; Local Variables:
  476. ;;; eval: (put 'dummy-package 'scheme-indent-function 1)
  477. ;;; End: