guix-pack-relocatable.sh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # GNU Guix --- Functional package management for GNU
  2. # Copyright © 2018, 2019, 2020 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. # Test the 'guix pack --relocatable' using the external store, if any.
  20. #
  21. guix pack --version
  22. # 'guix pack --relocatable' requires a C compiler and libc.a, which our
  23. # bootstrap binaries don't provide. To make the test relatively inexpensive,
  24. # run it on the user's global store if possible, on the grounds that binaries
  25. # may already be there or can be built or downloaded inexpensively.
  26. storedir="`guile -c '(use-modules (guix config))(display %storedir)'`"
  27. localstatedir="`guile -c '(use-modules (guix config))(display %localstatedir)'`"
  28. NIX_STORE_DIR="$storedir"
  29. GUIX_DAEMON_SOCKET="$localstatedir/guix/daemon-socket/socket"
  30. export NIX_STORE_DIR GUIX_DAEMON_SOCKET
  31. if ! guile -c '(use-modules (guix)) (exit (false-if-exception (open-connection)))'
  32. then
  33. exit 77
  34. fi
  35. # Attempt to run the given command in a namespace where the store is
  36. # invisible. This makes sure the presence of the store does not hide
  37. # problems.
  38. run_without_store ()
  39. {
  40. if unshare -r true # Are user namespaces supported?
  41. then
  42. # Run that relocatable executable in a user namespace where we "erase"
  43. # the store by mounting an empty file system on top of it. That way,
  44. # we exercise the wrapper code that creates the user namespace and
  45. # bind-mounts the store.
  46. unshare -mrf sh -c 'mount -t tmpfs -o ro none "$NIX_STORE_DIR"; '"$*"
  47. else
  48. # Run the relocatable program in the current namespaces. This is a
  49. # weak test because we're going to access store items from the host
  50. # store.
  51. $*
  52. fi
  53. }
  54. test_directory="`mktemp -d`"
  55. export test_directory
  56. trap 'chmod -Rf +w "$test_directory"; rm -rf "$test_directory"' EXIT
  57. if unshare -r true
  58. then
  59. # Test the 'userns' execution engine.
  60. tarball="`guix pack -R -S /Bin=bin sed`"
  61. (cd "$test_directory"; tar xvf "$tarball")
  62. run_without_store "$test_directory/Bin/sed" --version > "$test_directory/output"
  63. grep 'GNU sed' "$test_directory/output"
  64. # Same with an explicit engine.
  65. run_without_store GUIX_EXECUTION_ENGINE="userns" \
  66. "$test_directory/Bin/sed" --version > "$test_directory/output"
  67. grep 'GNU sed' "$test_directory/output"
  68. # Check whether the exit code is preserved.
  69. ! run_without_store "$test_directory/Bin/sed" --does-not-exist
  70. chmod -Rf +w "$test_directory"; rm -rf "$test_directory"/*
  71. else
  72. echo "'userns' execution tests skipped" >&2
  73. fi
  74. case "`uname -m`" in
  75. x86_64|i?86)
  76. # Try '-RR' and PRoot.
  77. tarball="`guix pack -RR -S /Bin=bin sed`"
  78. tar tvf "$tarball" | grep /bin/proot
  79. (cd "$test_directory"; tar xf "$tarball")
  80. run_without_store GUIX_EXECUTION_ENGINE="proot" \
  81. "$test_directory/Bin/sed" --version > "$test_directory/output"
  82. grep 'GNU sed' "$test_directory/output"
  83. # Now with fakechroot.
  84. run_without_store GUIX_EXECUTION_ENGINE="fakechroot" \
  85. "$test_directory/Bin/sed" --version > "$test_directory/output"
  86. grep 'GNU sed' "$test_directory/output"
  87. chmod -Rf +w "$test_directory"; rm -rf "$test_directory"/*
  88. if unshare -r true
  89. then
  90. # Check whether the store contains everything it should. Check
  91. # once when erasing $STORE_PARENT ("/gnu") and once when erasing
  92. # $NIX_STORE_DIR ("/gnu/store").
  93. tarball="`guix pack -RR -S /bin=bin bash-minimal`"
  94. (cd "$test_directory"; tar xf "$tarball")
  95. STORE_PARENT="`dirname $NIX_STORE_DIR`"
  96. export STORE_PARENT
  97. for engine in userns proot fakechroot
  98. do
  99. for i in $(guix gc -R $(guix build bash-minimal | grep -v -e '-doc$'))
  100. do
  101. unshare -mrf sh -c "mount -t tmpfs none \"$NIX_STORE_DIR\"; GUIX_EXECUTION_ENGINE=$engine $test_directory/bin/sh -c 'echo $NIX_STORE_DIR/*'" | grep $(basename $i)
  102. unshare -mrf sh -c "mount -t tmpfs none \"$STORE_PARENT\"; GUIX_EXECUTION_ENGINE=$engine $test_directory/bin/sh -c 'echo $NIX_STORE_DIR/*'" | grep $(basename $i)
  103. done
  104. done
  105. chmod -Rf +w "$test_directory"; rm -rf "$test_directory"/*
  106. fi
  107. ;;
  108. *)
  109. echo "skipping PRoot and Fakechroot tests" >&2
  110. ;;
  111. esac
  112. # Ensure '-R' works with outputs other than "out".
  113. tarball="`guix pack -R -S /share=share groff:doc`"
  114. (cd "$test_directory"; tar xf "$tarball")
  115. test -d "$test_directory/share/doc/groff/html"
  116. # Ensure '-R' applies to propagated inputs. Failing to do that, it would fail
  117. # with a profile collision error in this case because 'python-scipy'
  118. # propagates 'python-numpy'. See <https://bugs.gnu.org/42510>.
  119. guix pack -RR python-numpy python-scipy --no-grafts -n