1
0

run-local.sh 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. DEV_BRANCH="stable26"
  61. # "--timeout-multiplier N" option can be provided to set the timeout multiplier
  62. # to be used in ActorContext.
  63. TIMEOUT_MULTIPLIER=""
  64. if [ "$1" = "--timeout-multiplier" ]; then
  65. if [[ ! "$2" =~ ^[0-9]+$ ]]; then
  66. echo "--timeout-multiplier must be followed by a positive integer"
  67. exit 1
  68. fi
  69. TIMEOUT_MULTIPLIER=$2
  70. shift 2
  71. fi
  72. # "--nextcloud-server-domain XXX" option can be provided to set the domain used
  73. # by the Selenium server to access the Nextcloud server.
  74. DEFAULT_NEXTCLOUD_SERVER_DOMAIN="127.0.0.1"
  75. NEXTCLOUD_SERVER_DOMAIN="$DEFAULT_NEXTCLOUD_SERVER_DOMAIN"
  76. if [ "$1" = "--nextcloud-server-domain" ]; then
  77. NEXTCLOUD_SERVER_DOMAIN=$2
  78. shift 2
  79. fi
  80. # "--selenium-server XXX" option can be provided to set the domain and port used
  81. # by the acceptance tests to access the Selenium server.
  82. DEFAULT_SELENIUM_SERVER="127.0.0.1:4444"
  83. SELENIUM_SERVER="$DEFAULT_SELENIUM_SERVER"
  84. if [ "$1" = "--selenium-server" ]; then
  85. SELENIUM_SERVER=$2
  86. shift 2
  87. fi
  88. # Safety parameter to prevent executing this script by mistake and messing with
  89. # the Git repository.
  90. if [ "$1" != "allow-git-repository-modifications" ]; then
  91. echo "To run the acceptance tests use \"run.sh\" instead"
  92. exit 1
  93. fi
  94. SCENARIO_TO_RUN=$2
  95. if [ "$ACCEPTANCE_TESTS_DIR" != "tests/acceptance" ]; then
  96. if [ "$SCENARIO_TO_RUN" == "" ]; then
  97. echo "When an acceptance tests directory is given the scenario to run" \
  98. "should be provided too (paths are relative to the acceptance" \
  99. "tests directory; use the features directory to run all tests)"
  100. echo "No scenario was given, so \"features/\" was automatically used"
  101. SCENARIO_TO_RUN="features/"
  102. fi
  103. SCENARIO_TO_RUN=../../$ACCEPTANCE_TESTS_DIR/$SCENARIO_TO_RUN
  104. fi
  105. if [ "$TIMEOUT_MULTIPLIER" != "" ]; then
  106. # Although Behat documentation states that using the BEHAT_PARAMS
  107. # environment variable "You can set any value for any option that is
  108. # available in a behat.yml file" this is currently not true for the
  109. # constructor parameters of contexts (see
  110. # https://github.com/Behat/Behat/issues/983). Thus, the default "behat.yml"
  111. # configuration file has to be adjusted to provide the appropriate
  112. # parameters for ActorContext.
  113. ORIGINAL="\
  114. - ActorContext"
  115. REPLACEMENT="\
  116. - ActorContext:\n\
  117. actorTimeoutMultiplier: $TIMEOUT_MULTIPLIER"
  118. sed --in-place "s/$ORIGINAL/$REPLACEMENT/" $ACCEPTANCE_TESTS_CONFIG_DIR/behat.yml
  119. fi
  120. if [ "$NEXTCLOUD_SERVER_DOMAIN" != "$DEFAULT_NEXTCLOUD_SERVER_DOMAIN" ]; then
  121. # Although Behat documentation states that using the BEHAT_PARAMS
  122. # environment variable "You can set any value for any option that is
  123. # available in a behat.yml file" this is currently not true for the
  124. # constructor parameters of contexts (see
  125. # https://github.com/Behat/Behat/issues/983). Thus, the default "behat.yml"
  126. # configuration file has to be adjusted to provide the appropriate
  127. # parameters for NextcloudTestServerContext.
  128. #
  129. # Note that the substitution below is only valid if no parameters for
  130. # the helper are set in behat.yml, although it is valid if a specific
  131. # helper is.
  132. ORIGINAL="\
  133. - NextcloudTestServerContext:\?"
  134. REPLACEMENT="\
  135. - NextcloudTestServerContext:\n\
  136. nextcloudTestServerHelperParameters:\n\
  137. - $NEXTCLOUD_SERVER_DOMAIN"
  138. sed --in-place "s/$ORIGINAL/$REPLACEMENT/" $ACCEPTANCE_TESTS_CONFIG_DIR/behat.yml
  139. fi
  140. # Due to a bug in the Mink Extension for Behat it is not possible to use the
  141. # "paths.base" variable in the path to the custom Firefox profile. Thus, the
  142. # default "behat.yml" configuration file has to be adjusted to replace the
  143. # variable by its value before the configuration file is parsed by Behat.
  144. ORIGINAL="profile: %paths.base%"
  145. REPLACEMENT="profile: $ACCEPTANCE_TESTS_CONFIG_DIR"
  146. # As the substitution does not involve regular expressions or multilines it can
  147. # be done just with Bash. Moreover, this does not require escaping the regular
  148. # expression characters that may appear in the path, like "/".
  149. FILE_CONTENTS=$(<$ACCEPTANCE_TESTS_CONFIG_DIR/behat.yml)
  150. echo "${FILE_CONTENTS//$ORIGINAL/$REPLACEMENT}" > $ACCEPTANCE_TESTS_CONFIG_DIR/behat.yml
  151. # Set the Selenium server to be used by Mink. Although Mink sessions can be
  152. # extended through BEHAT_PARAMS this would require adding here too each new
  153. # session added to "behat.yml", including those added in the acceptance
  154. # tests of apps. Instead, the default "behat.yml" configuration file is
  155. # adjusted to replace the simulated "selenium.server" variable by its value
  156. # before the configuration file is parsed by Behat.
  157. ORIGINAL="wd_host: %selenium.server%"
  158. REPLACEMENT="wd_host: http://$SELENIUM_SERVER/wd/hub"
  159. # As the substitution does not involve regular expressions or multilines it
  160. # can be done just with Bash. Moreover, this does not require escaping the
  161. # regular expression characters that may appear in the URL, like "/".
  162. FILE_CONTENTS=$(<$ACCEPTANCE_TESTS_CONFIG_DIR/behat.yml)
  163. echo "${FILE_CONTENTS//$ORIGINAL/$REPLACEMENT}" > $ACCEPTANCE_TESTS_CONFIG_DIR/behat.yml
  164. composer install
  165. cd ../../
  166. # Link the default Apache directory to the root directory of the Nextcloud
  167. # server to make possible to run the Nextcloud server on Apache if needed.
  168. ln --symbolic $(pwd) /var/www/html
  169. # Add Notifications app to the "apps" directory (unless it is already there).
  170. if [ ! -e "apps/notifications" ]; then
  171. (cd apps && git clone --depth 1 --branch ${DEV_BRANCH} https://github.com/nextcloud/notifications)
  172. fi
  173. INSTALL_AND_CONFIGURE_SERVER_PARAMETERS=""
  174. if [ "$NEXTCLOUD_SERVER_DOMAIN" != "$DEFAULT_NEXTCLOUD_SERVER_DOMAIN" ]; then
  175. INSTALL_AND_CONFIGURE_SERVER_PARAMETERS+="--nextcloud-server-domain $NEXTCLOUD_SERVER_DOMAIN"
  176. fi
  177. echo "Installing and configuring Nextcloud server"
  178. # The server is installed and configured using the www-data user as it is the
  179. # user that Apache sub-processes will be run as; the PHP built-in web server is
  180. # run as the root user, and in that case the permissions of apps, config and
  181. # data dirs makes no difference, so this is valid for both cases.
  182. mkdir data
  183. chown -R www-data:www-data apps config data
  184. NEXTCLOUD_DIR=`pwd`
  185. su --shell /bin/bash --login www-data --command "cd $NEXTCLOUD_DIR && $ACCEPTANCE_TESTS_DIR/installAndConfigureServer.sh $INSTALL_AND_CONFIGURE_SERVER_PARAMETERS"
  186. echo "Saving the default state so acceptance tests can reset to it"
  187. find . -name ".gitignore" -exec rm --force {} \;
  188. # Create dummy files in empty directories to force Git to save the directories.
  189. find . -not -path "*.git*" -type d -empty -exec touch {}/.keep \;
  190. git add --all && echo 'Default state' | git -c user.name='John Doe' -c user.email='john@doe.org' commit --quiet --file=-
  191. cd tests/acceptance
  192. # Ensure that the Selenium server is ready before running the tests.
  193. echo "Waiting for Selenium"
  194. timeout 60s bash -c "while ! curl $SELENIUM_SERVER >/dev/null 2>&1; do sleep 1; done"
  195. vendor/bin/behat --colors --config=$ACCEPTANCE_TESTS_CONFIG_DIR/behat.yml $SCENARIO_TO_RUN