run-local.sh 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #!/usr/bin/env bash
  2. # @copyright Copyright (c) 2017, Daniel Calviño Sánchez (danxuliu@gmail.com)
  3. #
  4. # @license GNU AGPL version 3 or any later version
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as
  8. # published by the Free Software Foundation, either version 3 of the
  9. # License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. # Helper script to run the acceptance tests, which test a running Nextcloud
  19. # instance from the point of view of a real user, configured to start the
  20. # Nextcloud server themselves and from their grandparent directory.
  21. #
  22. # The acceptance tests are written in Behat so, besides running the tests, this
  23. # script installs Behat, its dependencies, and some related packages in the
  24. # "vendor" subdirectory of the acceptance tests. The acceptance tests expect
  25. # that the last commit in the Git repository provides the default state of the
  26. # Nextcloud server, so the script installs the Nextcloud server and saves a
  27. # snapshot of the whole grandparent directory (no .gitignore file is used) in
  28. # the Git repository. Finally, the acceptance tests also use the Selenium server
  29. # to control a web browser, so this script waits for the Selenium server
  30. # (which should have been started before executing this script) to be ready
  31. # before running the tests.
  32. #
  33. # By default the acceptance tests run are those for the Nextcloud server;
  34. # acceptance tests for apps can be run by providing the
  35. # "--acceptance-tests-dir XXX" option. When this option is used the Behat
  36. # configuration and the Nextcloud installation script used by the acceptance
  37. # tests for the Nextcloud server are ignored; they must be provided in the given
  38. # acceptance tests directory. Note, however, that the context classes for the
  39. # Nextcloud server and the core acceptance test framework classes are
  40. # automatically loaded; there is no need to explicitly set them in the Behat
  41. # configuration. Also, even when that option is used, the packages installed by
  42. # this script end in the "vendor" subdirectory of the acceptance tests for the
  43. # Nextcloud server, not in the one given in the option.
  44. # Exit immediately on errors.
  45. set -o errexit
  46. # Ensure working directory is script directory, as some actions (like installing
  47. # Behat through Composer or running Behat) expect that.
  48. cd "$(dirname $0)"
  49. # "--acceptance-tests-dir XXX" option can be provided to set the directory
  50. # (relative to the root directory of the Nextcloud server) used to look for the
  51. # Behat configuration and the Nextcloud installation script.
  52. # By default it is "tests/acceptance", that is, the acceptance tests for the
  53. # Nextcloud server itself.
  54. ACCEPTANCE_TESTS_DIR="tests/acceptance"
  55. if [ "$1" = "--acceptance-tests-dir" ]; then
  56. ACCEPTANCE_TESTS_DIR=$2
  57. shift 2
  58. fi
  59. ACCEPTANCE_TESTS_CONFIG_DIR="../../$ACCEPTANCE_TESTS_DIR/config"
  60. # "--timeout-multiplier N" option can be provided to set the timeout multiplier
  61. # to be used in ActorContext.
  62. TIMEOUT_MULTIPLIER=""
  63. if [ "$1" = "--timeout-multiplier" ]; then
  64. if [[ ! "$2" =~ ^[0-9]+$ ]]; then
  65. echo "--timeout-multiplier must be followed by a positive integer"
  66. exit 1
  67. fi
  68. TIMEOUT_MULTIPLIER=$2
  69. shift 2
  70. fi
  71. # "--nextcloud-server-domain XXX" option can be provided to set the domain used
  72. # by the Selenium server to access the Nextcloud server.
  73. DEFAULT_NEXTCLOUD_SERVER_DOMAIN="127.0.0.1"
  74. NEXTCLOUD_SERVER_DOMAIN="$DEFAULT_NEXTCLOUD_SERVER_DOMAIN"
  75. if [ "$1" = "--nextcloud-server-domain" ]; then
  76. NEXTCLOUD_SERVER_DOMAIN=$2
  77. shift 2
  78. fi
  79. # "--selenium-server XXX" option can be provided to set the domain and port used
  80. # by the acceptance tests to access the Selenium server.
  81. DEFAULT_SELENIUM_SERVER="127.0.0.1:4444"
  82. SELENIUM_SERVER="$DEFAULT_SELENIUM_SERVER"
  83. if [ "$1" = "--selenium-server" ]; then
  84. SELENIUM_SERVER=$2
  85. shift 2
  86. fi
  87. # Safety parameter to prevent executing this script by mistake and messing with
  88. # the Git repository.
  89. if [ "$1" != "allow-git-repository-modifications" ]; then
  90. echo "To run the acceptance tests use \"run.sh\" instead"
  91. exit 1
  92. fi
  93. SCENARIO_TO_RUN=$2
  94. if [ "$ACCEPTANCE_TESTS_DIR" != "tests/acceptance" ]; then
  95. if [ "$SCENARIO_TO_RUN" == "" ]; then
  96. echo "When an acceptance tests directory is given the scenario to run" \
  97. "should be provided too (paths are relative to the acceptance" \
  98. "tests directory; use the features directory to run all tests)"
  99. echo "No scenario was given, so \"features/\" was automatically used"
  100. SCENARIO_TO_RUN="features/"
  101. fi
  102. SCENARIO_TO_RUN=../../$ACCEPTANCE_TESTS_DIR/$SCENARIO_TO_RUN
  103. fi
  104. if [ "$TIMEOUT_MULTIPLIER" != "" ]; then
  105. # Although Behat documentation states that using the BEHAT_PARAMS
  106. # environment variable "You can set any value for any option that is
  107. # available in a behat.yml file" this is currently not true for the
  108. # constructor parameters of contexts (see
  109. # https://github.com/Behat/Behat/issues/983). Thus, the default "behat.yml"
  110. # configuration file has to be adjusted to provide the appropriate
  111. # parameters for ActorContext.
  112. ORIGINAL="\
  113. - ActorContext"
  114. REPLACEMENT="\
  115. - ActorContext:\n\
  116. actorTimeoutMultiplier: $TIMEOUT_MULTIPLIER"
  117. sed --in-place "s/$ORIGINAL/$REPLACEMENT/" $ACCEPTANCE_TESTS_CONFIG_DIR/behat.yml
  118. fi
  119. if [ "$NEXTCLOUD_SERVER_DOMAIN" != "$DEFAULT_NEXTCLOUD_SERVER_DOMAIN" ]; then
  120. # Although Behat documentation states that using the BEHAT_PARAMS
  121. # environment variable "You can set any value for any option that is
  122. # available in a behat.yml file" this is currently not true for the
  123. # constructor parameters of contexts (see
  124. # https://github.com/Behat/Behat/issues/983). Thus, the default "behat.yml"
  125. # configuration file has to be adjusted to provide the appropriate
  126. # parameters for NextcloudTestServerContext.
  127. #
  128. # Note that the substitution below is only valid if no parameters for
  129. # the helper are set in behat.yml, although it is valid if a specific
  130. # helper is.
  131. ORIGINAL="\
  132. - NextcloudTestServerContext:\?"
  133. REPLACEMENT="\
  134. - NextcloudTestServerContext:\n\
  135. nextcloudTestServerHelperParameters:\n\
  136. - $NEXTCLOUD_SERVER_DOMAIN"
  137. sed --in-place "s/$ORIGINAL/$REPLACEMENT/" $ACCEPTANCE_TESTS_CONFIG_DIR/behat.yml
  138. fi
  139. # Due to a bug in the Mink Extension for Behat it is not possible to use the
  140. # "paths.base" variable in the path to the custom Firefox profile. Thus, the
  141. # default "behat.yml" configuration file has to be adjusted to replace the
  142. # variable by its value before the configuration file is parsed by Behat.
  143. ORIGINAL="profile: %paths.base%"
  144. REPLACEMENT="profile: $ACCEPTANCE_TESTS_CONFIG_DIR"
  145. # As the substitution does not involve regular expressions or multilines it can
  146. # be done just with Bash. Moreover, this does not require escaping the regular
  147. # expression characters that may appear in the path, like "/".
  148. FILE_CONTENTS=$(<$ACCEPTANCE_TESTS_CONFIG_DIR/behat.yml)
  149. echo "${FILE_CONTENTS//$ORIGINAL/$REPLACEMENT}" > $ACCEPTANCE_TESTS_CONFIG_DIR/behat.yml
  150. # Set the Selenium server to be used by Mink. Although Mink sessions can be
  151. # extended through BEHAT_PARAMS this would require adding here too each new
  152. # session added to "behat.yml", including those added in the acceptance
  153. # tests of apps. Instead, the default "behat.yml" configuration file is
  154. # adjusted to replace the simulated "selenium.server" variable by its value
  155. # before the configuration file is parsed by Behat.
  156. ORIGINAL="wd_host: %selenium.server%"
  157. REPLACEMENT="wd_host: http://$SELENIUM_SERVER/wd/hub"
  158. # As the substitution does not involve regular expressions or multilines it
  159. # can be done just with Bash. Moreover, this does not require escaping the
  160. # regular expression characters that may appear in the URL, like "/".
  161. FILE_CONTENTS=$(<$ACCEPTANCE_TESTS_CONFIG_DIR/behat.yml)
  162. echo "${FILE_CONTENTS//$ORIGINAL/$REPLACEMENT}" > $ACCEPTANCE_TESTS_CONFIG_DIR/behat.yml
  163. composer install
  164. cd ../../
  165. # Link the default Apache directory to the root directory of the Nextcloud
  166. # server to make possible to run the Nextcloud server on Apache if needed.
  167. ln --symbolic $(pwd) /var/www/html
  168. INSTALL_AND_CONFIGURE_SERVER_PARAMETERS=""
  169. if [ "$NEXTCLOUD_SERVER_DOMAIN" != "$DEFAULT_NEXTCLOUD_SERVER_DOMAIN" ]; then
  170. INSTALL_AND_CONFIGURE_SERVER_PARAMETERS+="--nextcloud-server-domain $NEXTCLOUD_SERVER_DOMAIN"
  171. fi
  172. echo "Installing and configuring Nextcloud server"
  173. # The server is installed and configured using the www-data user as it is the
  174. # user that Apache sub-processes will be run as; the PHP built-in web server is
  175. # run as the root user, and in that case the permissions of apps, config and
  176. # data dirs makes no difference, so this is valid for both cases.
  177. mkdir data
  178. chown -R www-data:www-data apps config data
  179. NEXTCLOUD_DIR=`pwd`
  180. su --shell /bin/bash --login www-data --command "cd $NEXTCLOUD_DIR && $ACCEPTANCE_TESTS_DIR/installAndConfigureServer.sh $INSTALL_AND_CONFIGURE_SERVER_PARAMETERS"
  181. echo "Saving the default state so acceptance tests can reset to it"
  182. find . -name ".gitignore" -exec rm --force {} \;
  183. # Create dummy files in empty directories to force Git to save the directories.
  184. find . -not -path "*.git*" -type d -empty -exec touch {}/.keep \;
  185. git add --all && echo 'Default state' | git -c user.name='John Doe' -c user.email='john@doe.org' commit --quiet --file=-
  186. cd tests/acceptance
  187. # Ensure that the Selenium server is ready before running the tests.
  188. echo "Waiting for Selenium"
  189. timeout 60s bash -c "while ! curl $SELENIUM_SERVER >/dev/null 2>&1; do sleep 1; done"
  190. vendor/bin/behat --config=$ACCEPTANCE_TESTS_CONFIG_DIR/behat.yml $SCENARIO_TO_RUN