NextcloudTestServerLocalApacheHelper.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. *
  4. * @copyright Copyright (c) 2018, Daniel Calviño Sánchez (danxuliu@gmail.com)
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * Helper to manage a Nextcloud test server started directly by the acceptance
  24. * tests themselves using the Apache web server.
  25. *
  26. * The Nextcloud test server is executed using the Apache web server; the
  27. * default Apache directory is expected to have been set to the root directory
  28. * of the Nextcloud server (for example, by linking "var/www/html" to it); in
  29. * any case, note that the acceptance tests must be run from the acceptance
  30. * tests directory. The "setUp" method resets the Nextcloud server to its
  31. * initial state and starts it, while the "cleanUp" method stops it. To be able
  32. * to reset the Nextcloud server to its initial state a Git repository must be
  33. * provided in the root directory of the Nextcloud server; the last commit in
  34. * that Git repository must provide the initial state for the Nextcloud server
  35. * expected by the acceptance tests. When the Nextcloud server is reset the
  36. * owner of "apps", "config" and "data" must be set to the user that Apache
  37. * server is run as; it is assumed that Apache is run as "www-data".
  38. *
  39. * The Nextcloud server is available at "$nextcloudServerDomain", which can be
  40. * optionally specified when the NextcloudTestServerLocalApacheHelper is
  41. * created; if no value is given "127.0.0.1" is used by default. In any case,
  42. * the value of "$nextcloudServerDomain" must be seen as a trusted domain by the
  43. * Nextcloud server (which would be the case for "127.0.0.1" if it was installed
  44. * by running "occ maintenance:install"). The base URL to access the Nextcloud
  45. * server can be got from "getBaseUrl".
  46. */
  47. class NextcloudTestServerLocalApacheHelper implements NextcloudTestServerHelper {
  48. /**
  49. * @var string
  50. */
  51. private $nextcloudServerDomain;
  52. /**
  53. * Creates a new NextcloudTestServerLocalApacheHelper.
  54. */
  55. public function __construct($nextcloudServerDomain = "127.0.0.1") {
  56. $this->nextcloudServerDomain = $nextcloudServerDomain;
  57. }
  58. /**
  59. * Sets up the Nextcloud test server.
  60. *
  61. * It resets the Nextcloud test server restoring its last saved Git state
  62. * and then waits for the Nextcloud test server to start again; if the
  63. * server can not be reset or if it does not start again after some time an
  64. * exception is thrown (as it is just a warning for the test runner and
  65. * nothing to be explicitly catched a plain base Exception is used).
  66. *
  67. * @throws \Exception if the Nextcloud test server can not be reset or
  68. * started again.
  69. */
  70. public function setUp(): void {
  71. // Ensure that previous Apache server is not running (as cleanUp may not
  72. // have been called).
  73. $this->stopApacheServer();
  74. $this->execOrException("cd ../../ && git reset --hard HEAD");
  75. $this->execOrException("cd ../../ && git clean -d --force");
  76. $this->execOrException("cd ../../ && chown -R www-data:www-data apps config data");
  77. $this->execOrException("service apache2 start");
  78. $timeout = 60;
  79. if (!Utils::waitForServer($this->getBaseUrl(), $timeout)) {
  80. throw new Exception("Nextcloud test server could not be started");
  81. }
  82. }
  83. /**
  84. * Cleans up the Nextcloud test server.
  85. *
  86. * It stops the running Nextcloud test server, if any.
  87. */
  88. public function cleanUp() {
  89. $this->stopApacheServer();
  90. }
  91. /**
  92. * Returns the base URL of the Nextcloud test server.
  93. *
  94. * @return string the base URL of the Nextcloud test server.
  95. */
  96. public function getBaseUrl() {
  97. return "http://" . $this->nextcloudServerDomain . "/index.php";
  98. }
  99. /**
  100. * Executes the given command, throwing an Exception if it fails.
  101. *
  102. * @param string $command the command to execute.
  103. * @throws \Exception if the command fails to execute.
  104. */
  105. private function execOrException($command) {
  106. exec($command . " 2>&1", $output, $returnValue);
  107. if ($returnValue != 0) {
  108. throw new Exception("'$command' could not be executed: " . implode("\n", $output));
  109. }
  110. }
  111. /**
  112. * Stops the Apache server started in setUp, if any.
  113. */
  114. private function stopApacheServer() {
  115. $this->execOrException("service apache2 stop");
  116. }
  117. }