test-env.in 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #!/bin/sh
  2. # GNU Guix --- Functional package management for GNU
  3. # Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2021 Ludovic Courtès <ludo@gnu.org>
  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. # Usage: ./test-env COMMAND ARG...
  20. #
  21. # Run the daemon in the build directory, and run COMMAND within
  22. # `pre-inst-env'. This is used to run unit tests with the just-built
  23. # daemon, unless `--disable-daemon' was passed at configure time.
  24. # Make sure 'cd' behaves deterministically and doesn't write anything to
  25. # stdout.
  26. unset CDPATH
  27. case "$1" in
  28. --quiet-stderr)
  29. # Silence the daemon's output, which is often useless, as well as that
  30. # of Bash (such as "Terminated" messages when 'guix-daemon' is
  31. # killed.)
  32. exec 2> /dev/null
  33. shift
  34. ;;
  35. esac
  36. if [ -x "@abs_top_builddir@/guix-daemon" ]
  37. then
  38. NIX_STORE_DIR="@GUIX_TEST_ROOT@/store"
  39. # Do that because store.scm calls `canonicalize-path' on it.
  40. mkdir -p "$NIX_STORE_DIR"
  41. # Canonicalize the store directory name in an attempt to avoid symlinks in
  42. # it or its parent directories. See <http://bugs.gnu.org/17935>.
  43. NIX_STORE_DIR="`cd "@GUIX_TEST_ROOT@/store"; pwd -P`"
  44. GUIX_LOG_DIRECTORY="@GUIX_TEST_ROOT@/var/log/guix"
  45. GUIX_DATABASE_DIRECTORY="@GUIX_TEST_ROOT@/db"
  46. # Choose a PID-dependent name to allow for parallel builds. Note
  47. # that the directory name must be chosen so that the socket's file
  48. # name is less than 108-char long (the size of `sun_path' in glibc).
  49. # Currently, in Nix builds, we're at ~106 chars...
  50. GUIX_STATE_DIRECTORY="@GUIX_TEST_ROOT@/var/$$"
  51. # We can't exit when we reach the limit, because perhaps the test doesn't
  52. # actually rely on the daemon, but at least warn.
  53. if test "`echo -n "$GUIX_STATE_DIRECTORY/daemon-socket/socket" | wc -c`" -ge 108
  54. then
  55. echo "warning: exceeding socket file name limit; test may fail!" >&2
  56. fi
  57. # The configuration directory, for import/export signing keys.
  58. GUIX_CONFIGURATION_DIRECTORY="@GUIX_TEST_ROOT@/etc"
  59. if [ ! -d "$GUIX_CONFIGURATION_DIRECTORY" ]
  60. then
  61. # Copy the keys so that the secret key has the right permissions (the
  62. # daemon errors out when this is not the case.)
  63. mkdir -p "$GUIX_CONFIGURATION_DIRECTORY"
  64. cp "@abs_top_srcdir@/tests/keys/signing-key.sec" \
  65. "@abs_top_srcdir@/tests/keys/signing-key.pub" \
  66. "$GUIX_CONFIGURATION_DIRECTORY"
  67. chmod 400 "$GUIX_CONFIGURATION_DIRECTORY/signing-key.sec"
  68. fi
  69. # A place to store data of the substituter.
  70. GUIX_BINARY_SUBSTITUTE_URL="file://$GUIX_STATE_DIRECTORY/substituter-data"
  71. rm -rf "$GUIX_STATE_DIRECTORY/substituter-data"
  72. mkdir -p "$GUIX_STATE_DIRECTORY/substituter-data"
  73. # For a number of tests, we want to allow unsigned narinfos, for
  74. # simplicity.
  75. GUIX_ALLOW_UNAUTHENTICATED_SUBSTITUTES=yes
  76. # Place for the substituter's cache.
  77. XDG_CACHE_HOME="$GUIX_STATE_DIRECTORY/cache-$$"
  78. export NIX_IGNORE_SYMLINK_STORE NIX_STORE_DIR \
  79. GUIX_LOG_DIRECTORY GUIX_STATE_DIRECTORY GUIX_DATABASE_DIRECTORY \
  80. GUIX_BINARY_SUBSTITUTE_URL \
  81. GUIX_ALLOW_UNAUTHENTICATED_SUBSTITUTES \
  82. GUIX_CONFIGURATION_DIRECTORY XDG_CACHE_HOME
  83. # Launch the daemon without chroot support because is may be
  84. # unavailable, for instance if we're not running as root.
  85. "@abs_top_builddir@/pre-inst-env" \
  86. "@abs_top_builddir@/guix-daemon" --disable-chroot \
  87. --substitute-urls="$GUIX_BINARY_SUBSTITUTE_URL" &
  88. daemon_pid=$!
  89. trap "kill $daemon_pid ; rm -rf $GUIX_STATE_DIRECTORY" EXIT
  90. # The test suite expects the 'guile-bootstrap' package to be available.
  91. # Normally the Guile bootstrap tarball is downloaded by a fixed-output
  92. # derivation but when network access is missing we allow users to drop
  93. # the tarball in 'gnu/packages/bootstrap/SYSTEM' and "intern" it here.
  94. bootstrap_directory="@abs_top_builddir@/gnu/packages/bootstrap/@guix_system@"
  95. if [ -d "$bootstrap_directory" ]
  96. then
  97. # Make sure 'guix-daemon' is listening before invoking 'guix
  98. # download'.
  99. "@abs_top_builddir@/pre-inst-env" "@GUILE@" -c \
  100. '(use-modules (guix))
  101. (let loop ((i 10))
  102. (catch #t
  103. (lambda () (open-connection))
  104. (lambda (key . args)
  105. (if (zero? i)
  106. (apply throw key args)
  107. (begin (usleep 500000) (loop (- i 1)))))))'
  108. for file in "$bootstrap_directory"/guile-*
  109. do
  110. [ -f "$file" ] && \
  111. "@abs_top_builddir@/pre-inst-env" \
  112. guix download "file://$file" > /dev/null
  113. done
  114. fi
  115. fi
  116. # Avoid issues that could stem from l10n, such as language/encoding
  117. # mismatches.
  118. unset LANGUAGE
  119. LC_MESSAGES=C
  120. export LC_MESSAGES
  121. # Disable grafts by default because they can cause things to be built
  122. # regardless of '--dry-run'.
  123. GUIX_BUILD_OPTIONS="--no-grafts"
  124. export GUIX_BUILD_OPTIONS
  125. # Ignore user settings.
  126. unset GUIX_PACKAGE_PATH
  127. storedir="@storedir@"
  128. prefix="@prefix@"
  129. datarootdir="@datarootdir@"
  130. datadir="@datadir@"
  131. localstatedir="@localstatedir@"
  132. export storedir prefix datarootdir datadir localstatedir
  133. "@abs_top_builddir@/pre-inst-env" "$@"
  134. exit $?