From 3ce4e928c606f2b3a1c393819ca4a1268e3b058b Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Sat, 20 Aug 2016 15:19:15 +0200 Subject: tests: Use one webserver per testcase This allows the tests to run somewhat parallel (still limited by the database) and simplifies running single testcases without the ./run-tests.sh wrapper. Signed-off-by: Florian Pritz --- application/controllers/tools.php | 4 +-- application/test/Test.php | 49 +++++++++++++++++++++++++++++++--- application/test/tests/test_api_v1.php | 5 ++-- application/test/tests/test_api_v2.php | 7 ++--- run-tests.sh | 13 +-------- 5 files changed, 55 insertions(+), 23 deletions(-) diff --git a/application/controllers/tools.php b/application/controllers/tools.php index 7808b11cf..c3209e8f7 100644 --- a/application/controllers/tools.php +++ b/application/controllers/tools.php @@ -70,15 +70,13 @@ class Tools extends MY_Controller { function test() { global $argv; - $url = $argv[3]; - $testcase = $argv[4]; + $testcase = $argv[3]; $testcase = str_replace("application/", "", $testcase); $testcase = str_replace("/", "\\", $testcase); $testcase = str_replace(".php", "", $testcase); $test = new $testcase(); - $test->setServer($url); $exitcode = 0; diff --git a/application/test/Test.php b/application/test/Test.php index a0ebf880e..33278436b 100644 --- a/application/test/Test.php +++ b/application/test/Test.php @@ -25,8 +25,9 @@ class TestMore extends \TestMore { abstract class Test { protected $t; - protected $server = ""; + protected $server_url = ""; private $testid = ""; + private $server_proc = null; public function __construct() { @@ -34,9 +35,51 @@ abstract class Test { $this->t->plan("no_plan"); } - public function setServer($server) + public function __destruct() { - $this->server = $server; + if ($this->server_proc) { + proc_terminate($this->server_proc); + } + } + + public function startServer($port) + { + $url = "http://127.0.0.1:$port/index.php"; + + $pipes = []; + $descriptorspec = [ + 0 => ['file', '/dev/null', 'r'], + 1 => STDOUT, + 2 => STDOUT, + ]; + + $this->server_proc = proc_open("php -S 127.0.0.1:$port", $descriptorspec, $pipes); + + $this->wait_for_server($url); + $this->server_url = $url; + } + + private function wait_for_server($url) + { + while (!$this->url_is_reachable($url)) { + echo "Waiting for server at $url to start...\n"; + usleep(10000); + } + } + + private function url_is_reachable($url) + { + $handle = curl_init($url); + curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE); + curl_exec($handle); + $status = curl_getinfo($handle, CURLINFO_HTTP_CODE); + curl_close($handle); + + if ($status == 200) { + return true; + } + + return false; } public function setTestID($testid) diff --git a/application/test/tests/test_api_v1.php b/application/test/tests/test_api_v1.php index 0fb709da8..4d50d4796 100644 --- a/application/test/tests/test_api_v1.php +++ b/application/test/tests/test_api_v1.php @@ -19,11 +19,12 @@ class test_api_v1 extends \test\Test { $CI->load->model("muser"); $CI->load->model("mfile"); + $this->startServer(23115); } private function uploadFile($apikey, $file) { - $ret = $this->CallAPI("POST", "$this->server/api/v1.1.0/file/upload", array( + $ret = $this->CallAPI("POST", "$this->server_url/api/v1.1.0/file/upload", array( "apikey" => $apikey, "file[1]" => curl_file_create($file), )); @@ -54,7 +55,7 @@ class test_api_v1 extends \test\Test { private function callEndpoint($verb, $endpoint, $data) { - return $this->CallAPI($verb, "$this->server/api/v1.0.0/$endpoint", $data); + return $this->CallAPI($verb, "$this->server_url/api/v1.0.0/$endpoint", $data); } public function test_callPrivateEndpointsWithoutApikey() diff --git a/application/test/tests/test_api_v2.php b/application/test/tests/test_api_v2.php index c18cd5b8f..b50af4bcf 100644 --- a/application/test/tests/test_api_v2.php +++ b/application/test/tests/test_api_v2.php @@ -19,11 +19,12 @@ class test_api_v2 extends \test\Test { $CI->load->model("muser"); $CI->load->model("mfile"); + $this->startServer(23116); } private function uploadFile($apikey, $file) { - $ret = $this->CallAPI("POST", "$this->server/api/v2.0.0/file/upload", array( + $ret = $this->CallAPI("POST", "$this->server_url/api/v2.0.0/file/upload", array( "apikey" => $apikey, "file[1]" => curl_file_create($file), )); @@ -54,7 +55,7 @@ class test_api_v2 extends \test\Test { private function callEndpoint($verb, $endpoint, $data) { - return $this->CallAPI($verb, "$this->server/api/v2.0.0/$endpoint", $data); + return $this->CallAPI($verb, "$this->server_url/api/v2.0.0/$endpoint", $data); } public function test_callPrivateEndpointsWithoutApikey() @@ -75,7 +76,7 @@ class test_api_v2 extends \test\Test { $this->t->is_deeply(array( 'status' => 'error', 'error_id' => 'api/not-authenticated', - 'message' => 'Not authenticated. FileBin requires you to have an account, please go to the homepage at http://127.0.0.1:23115/ for more information.', + 'message' => 'Not authenticated. FileBin requires you to have an account, please go to the homepage at http://127.0.0.1:23116/ for more information.', ), $ret, "expected error"); } } diff --git a/run-tests.sh b/run-tests.sh index 35a23cbdc..c429acc88 100755 --- a/run-tests.sh +++ b/run-tests.sh @@ -6,10 +6,6 @@ export ENVIRONMENT="testsuite" startdir="$(dirname "$0")" -url="" -port=23115 -ip='127.0.0.1' -url="http://$ip:$port/index.php" die() { echo "$@" >&2 @@ -33,19 +29,12 @@ cleanup() { php index.php tools drop_all_tables } -php -S "$ip:$port" -t public_html 2>/dev/null 1>&2 & - mkdir -p test-coverage-data -while ! curl -s "$url" >/dev/null; do - sleep 0.1; -done - # run tests phpdbg -qrr index.php tools drop_all_tables || exit 1 phpdbg -qrr index.php tools update_database || exit 1 -prove --ext .php --state=hot,slow,all,save --timer -o -e "phpdbg -qrr index.php tools test $url" -r "$@" application/test/tests/ || exit 1 - +prove --ext .php --state=hot,slow,all,save --timer -o -e "phpdbg -qrr index.php tools test" -r "$@" application/test/tests/ || exit 1 php index.php tools generate_coverage_report rm -rf test-coverage-data -- cgit v1.2.3-24-g4f1b