run-local.sh 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. # "--timeout-multiplier N" option can be provided to set the timeout multiplier
  60. # to be used in ActorContext.
  61. TIMEOUT_MULTIPLIER=""
  62. if [ "$1" = "--timeout-multiplier" ]; then
  63. if [[ ! "$2" =~ ^[0-9]+$ ]]; then
  64. echo "--timeout-multiplier must be followed by a positive integer"
  65. exit 1
  66. fi
  67. TIMEOUT_MULTIPLIER=$2
  68. shift 2
  69. fi
  70. # "--nextcloud-server-domain XXX" option can be provided to set the domain used
  71. # by the Selenium server to access the Nextcloud server.
  72. DEFAULT_NEXTCLOUD_SERVER_DOMAIN="127.0.0.1"
  73. NEXTCLOUD_SERVER_DOMAIN="$DEFAULT_NEXTCLOUD_SERVER_DOMAIN"
  74. if [ "$1" = "--nextcloud-server-domain" ]; then
  75. NEXTCLOUD_SERVER_DOMAIN=$2
  76. shift 2
  77. fi
  78. # "--selenium-server XXX" option can be provided to set the domain and port used
  79. # by the acceptance tests to access the Selenium server.
  80. DEFAULT_SELENIUM_SERVER="127.0.0.1:4444"
  81. SELENIUM_SERVER="$DEFAULT_SELENIUM_SERVER"
  82. if [ "$1" = "--selenium-server" ]; then
  83. SELENIUM_SERVER=$2
  84. shift 2
  85. fi
  86. # Safety parameter to prevent executing this script by mistake and messing with
  87. # the Git repository.
  88. if [ "$1" != "allow-git-repository-modifications" ]; then
  89. echo "To run the acceptance tests use \"run.sh\" instead"
  90. exit 1
  91. fi
  92. SCENARIO_TO_RUN=$2
  93. if [ "$ACCEPTANCE_TESTS_DIR" != "tests/acceptance" ]; then
  94. if [ "$SCENARIO_TO_RUN" == "" ]; then
  95. echo "When an acceptance tests directory is given the scenario to run" \
  96. "should be provided too (paths are relative to the acceptance" \
  97. "tests directory; use the features directory to run all tests)"
  98. echo "No scenario was given, so \"features/\" was automatically used"
  99. SCENARIO_TO_RUN="features/"
  100. fi
  101. SCENARIO_TO_RUN=../../$ACCEPTANCE_TESTS_DIR/$SCENARIO_TO_RUN
  102. fi
  103. if [ "$TIMEOUT_MULTIPLIER" != "" ]; then
  104. # Although Behat documentation states that using the BEHAT_PARAMS
  105. # environment variable "You can set any value for any option that is
  106. # available in a behat.yml file" this is currently not true for the
  107. # constructor parameters of contexts (see
  108. # https://github.com/Behat/Behat/issues/983). Thus, the default "behat.yml"
  109. # configuration file has to be adjusted to provide the appropriate
  110. # parameters for ActorContext.
  111. ORIGINAL="\
  112. - ActorContext"
  113. REPLACEMENT="\
  114. - ActorContext:\n\
  115. actorTimeoutMultiplier: $TIMEOUT_MULTIPLIER"
  116. sed --in-place "s/$ORIGINAL/$REPLACEMENT/" ../../$ACCEPTANCE_TESTS_DIR/config/behat.yml
  117. fi
  118. if [ "$NEXTCLOUD_SERVER_DOMAIN" != "$DEFAULT_NEXTCLOUD_SERVER_DOMAIN" ]; then
  119. # Although Behat documentation states that using the BEHAT_PARAMS
  120. # environment variable "You can set any value for any option that is
  121. # available in a behat.yml file" this is currently not true for the
  122. # constructor parameters of contexts (see
  123. # https://github.com/Behat/Behat/issues/983). Thus, the default "behat.yml"
  124. # configuration file has to be adjusted to provide the appropriate
  125. # parameters for NextcloudTestServerContext.
  126. #
  127. # Note that the substitution below is only valid if no parameters for
  128. # the helper are set in behat.yml, although it is valid if a specific
  129. # helper is.
  130. ORIGINAL="\
  131. - NextcloudTestServerContext:\?"
  132. REPLACEMENT="\
  133. - NextcloudTestServerContext:\n\
  134. nextcloudTestServerHelperParameters:\n\
  135. - $NEXTCLOUD_SERVER_DOMAIN"
  136. sed --in-place "s/$ORIGINAL/$REPLACEMENT/" ../../$ACCEPTANCE_TESTS_DIR/config/behat.yml
  137. fi
  138. if [ "$SELENIUM_SERVER" != "$DEFAULT_SELENIUM_SERVER" ]; then
  139. # Set the Selenium server to be used by Mink; this extends the default
  140. # configuration from "config/behat.yml".
  141. export BEHAT_PARAMS='
  142. {
  143. "extensions": {
  144. "Behat\\MinkExtension": {
  145. "sessions": {
  146. "default": {
  147. "selenium2": {
  148. "wd_host": "http://'"$SELENIUM_SERVER"'/wd/hub"
  149. }
  150. },
  151. "John": {
  152. "selenium2": {
  153. "wd_host": "http://'"$SELENIUM_SERVER"'/wd/hub"
  154. }
  155. },
  156. "Jane": {
  157. "selenium2": {
  158. "wd_host": "http://'"$SELENIUM_SERVER"'/wd/hub"
  159. }
  160. }
  161. }
  162. }
  163. }
  164. }'
  165. fi
  166. composer install
  167. cd ../../
  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. git add --all && echo 'Default state' | git -c user.name='John Doe' -c user.email='john@doe.org' commit --quiet --file=-
  184. cd tests/acceptance
  185. # Ensure that the Selenium server is ready before running the tests.
  186. echo "Waiting for Selenium"
  187. timeout 60s bash -c "while ! curl $SELENIUM_SERVER >/dev/null 2>&1; do sleep 1; done"
  188. vendor/bin/behat --config=../../$ACCEPTANCE_TESTS_DIR/config/behat.yml $SCENARIO_TO_RUN