run-local.sh 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. # Exit immediately on errors.
  33. set -o errexit
  34. # Ensure working directory is script directory, as some actions (like installing
  35. # Behat through Composer or running Behat) expect that.
  36. cd "$(dirname $0)"
  37. # "--timeout-multiplier N" option can be provided before any other parameter to
  38. # set the timeout multiplier to be used in ActorContext.
  39. TIMEOUT_MULTIPLIER=""
  40. if [ "$1" = "--timeout-multiplier" ]; then
  41. if [[ ! "$2" =~ ^[0-9]+$ ]]; then
  42. echo "--timeout-multiplier must be followed by a positive integer"
  43. exit 1
  44. fi
  45. TIMEOUT_MULTIPLIER=$2
  46. shift 2
  47. fi
  48. # "--nextcloud-server-domain XXX" option can be provided to set the domain used
  49. # by the Selenium server to access the Nextcloud server.
  50. DEFAULT_NEXTCLOUD_SERVER_DOMAIN="127.0.0.1"
  51. NEXTCLOUD_SERVER_DOMAIN="$DEFAULT_NEXTCLOUD_SERVER_DOMAIN"
  52. if [ "$1" = "--nextcloud-server-domain" ]; then
  53. NEXTCLOUD_SERVER_DOMAIN=$2
  54. shift 2
  55. fi
  56. # "--selenium-server XXX" option can be provided to set the domain and port used
  57. # by the acceptance tests to access the Selenium server.
  58. DEFAULT_SELENIUM_SERVER="127.0.0.1:4444"
  59. SELENIUM_SERVER="$DEFAULT_SELENIUM_SERVER"
  60. if [ "$1" = "--selenium-server" ]; then
  61. SELENIUM_SERVER=$2
  62. shift 2
  63. fi
  64. # Safety parameter to prevent executing this script by mistake and messing with
  65. # the Git repository.
  66. if [ "$1" != "allow-git-repository-modifications" ]; then
  67. echo "To run the acceptance tests use \"run.sh\" instead"
  68. exit 1
  69. fi
  70. SCENARIO_TO_RUN=$2
  71. if [ "$TIMEOUT_MULTIPLIER" != "" ]; then
  72. # Although Behat documentation states that using the BEHAT_PARAMS
  73. # environment variable "You can set any value for any option that is
  74. # available in a behat.yml file" this is currently not true for the
  75. # constructor parameters of contexts (see
  76. # https://github.com/Behat/Behat/issues/983). Thus, the default "behat.yml"
  77. # configuration file has to be adjusted to provide the appropriate
  78. # parameters for ActorContext.
  79. ORIGINAL="\
  80. - ActorContext"
  81. REPLACEMENT="\
  82. - ActorContext:\n\
  83. actorTimeoutMultiplier: $TIMEOUT_MULTIPLIER"
  84. sed --in-place "s/$ORIGINAL/$REPLACEMENT/" config/behat.yml
  85. fi
  86. if [ "$NEXTCLOUD_SERVER_DOMAIN" != "$DEFAULT_NEXTCLOUD_SERVER_DOMAIN" ]; then
  87. # Although Behat documentation states that using the BEHAT_PARAMS
  88. # environment variable "You can set any value for any option that is
  89. # available in a behat.yml file" this is currently not true for the
  90. # constructor parameters of contexts (see
  91. # https://github.com/Behat/Behat/issues/983). Thus, the default "behat.yml"
  92. # configuration file has to be adjusted to provide the appropriate
  93. # parameters for NextcloudTestServerContext.
  94. ORIGINAL="\
  95. - NextcloudTestServerContext"
  96. REPLACEMENT="\
  97. - NextcloudTestServerContext:\n\
  98. nextcloudTestServerHelperParameters:\n\
  99. - $NEXTCLOUD_SERVER_DOMAIN"
  100. sed --in-place "s/$ORIGINAL/$REPLACEMENT/" config/behat.yml
  101. fi
  102. if [ "$SELENIUM_SERVER" != "$DEFAULT_SELENIUM_SERVER" ]; then
  103. # Set the Selenium server to be used by Mink; this extends the default
  104. # configuration from "config/behat.yml".
  105. export BEHAT_PARAMS='
  106. {
  107. "extensions": {
  108. "Behat\\MinkExtension": {
  109. "sessions": {
  110. "default": {
  111. "selenium2": {
  112. "wd_host": "http://'"$SELENIUM_SERVER"'/wd/hub"
  113. }
  114. },
  115. "John": {
  116. "selenium2": {
  117. "wd_host": "http://'"$SELENIUM_SERVER"'/wd/hub"
  118. }
  119. },
  120. "Jane": {
  121. "selenium2": {
  122. "wd_host": "http://'"$SELENIUM_SERVER"'/wd/hub"
  123. }
  124. }
  125. }
  126. }
  127. }
  128. }'
  129. fi
  130. composer install
  131. cd ../../
  132. INSTALL_AND_CONFIGURE_SERVER_PARAMETERS=""
  133. if [ "$NEXTCLOUD_SERVER_domain" != "$DEFAULT_NEXTCLOUD_SERVER_DOMAIN" ]; then
  134. INSTALL_AND_CONFIGURE_SERVER_PARAMETERS+="--nextcloud-server-domain $NEXTCLOUD_SERVER_DOMAIN"
  135. fi
  136. echo "Installing and configuring Nextcloud server"
  137. tests/acceptance/installAndConfigureServer.sh $INSTALL_AND_CONFIGURE_SERVER_PARAMETERS
  138. echo "Saving the default state so acceptance tests can reset to it"
  139. find . -name ".gitignore" -exec rm --force {} \;
  140. git add --all && echo 'Default state' | git -c user.name='John Doe' -c user.email='john@doe.org' commit --quiet --file=-
  141. cd tests/acceptance
  142. # Ensure that the Selenium server is ready before running the tests.
  143. echo "Waiting for Selenium"
  144. timeout 60s bash -c "while ! curl $SELENIUM_SERVER >/dev/null 2>&1; do sleep 1; done"
  145. vendor/bin/behat $SCENARIO_TO_RUN