From 26394ea4cfb1ea19313c414f08a98f49cd21093e Mon Sep 17 00:00:00 2001 From: Eldad Fux Date: Sat, 12 Dec 2020 07:42:29 +0200 Subject: [PATCH] Added timeput tests --- .../Functions/FunctionsCustomServerTest.php | 103 ++++++++++++++++-- tests/resources/functions/package-php.sh | 2 - tests/resources/functions/package-timeout.sh | 10 ++ tests/resources/functions/timeout.tar.gz | Bin 0 -> 168 bytes tests/resources/functions/timeout/index.php | 3 + 5 files changed, 107 insertions(+), 11 deletions(-) create mode 100644 tests/resources/functions/package-timeout.sh create mode 100644 tests/resources/functions/timeout.tar.gz create mode 100644 tests/resources/functions/timeout/index.php diff --git a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php index 9b11488364..dd8c0b59e7 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php @@ -480,19 +480,21 @@ class FunctionsCustomServerTest extends Scope * bash tests/resources/functions/package-*.sh */ $envs = [ - [ - 'language' => 'PHP', - 'version' => '8.0', - 'name' => 'php-8.0', - 'code' => $functions.'/php.tar.gz', - 'command' => 'php index.php', - ], [ 'language' => 'PHP', 'version' => '7.4', 'name' => 'php-7.4', 'code' => $functions.'/php.tar.gz', 'command' => 'php index.php', + 'timeout' => 15, + ], + [ + 'language' => 'PHP', + 'version' => '8.0', + 'name' => 'php-8.0', + 'code' => $functions.'/php.tar.gz', + 'command' => 'php index.php', + 'timeout' => 15, ], [ 'language' => 'Python', @@ -500,6 +502,7 @@ class FunctionsCustomServerTest extends Scope 'name' => 'python-3.8', 'code' => $functions.'/python.tar.gz', 'command' => 'python main.py', + 'timeout' => 15, ], [ 'language' => 'Node.js', @@ -507,6 +510,7 @@ class FunctionsCustomServerTest extends Scope 'name' => 'node-14', 'code' => $functions.'/node.tar.gz', 'command' => 'node index.js', + 'timeout' => 15, ], [ 'language' => 'Ruby', @@ -514,6 +518,7 @@ class FunctionsCustomServerTest extends Scope 'name' => 'ruby-2.7', 'code' => $functions.'/ruby.tar.gz', 'command' => 'ruby app.rb', + 'timeout' => 15, ], // [ // 'language' => 'Deno', @@ -521,6 +526,7 @@ class FunctionsCustomServerTest extends Scope // 'name' => 'deno-1.5', // 'code' => $functions.'/deno.tar.gz', // 'command' => 'deno run --allow-env index.ts', + // 'timeout' => 15, // ], ]; @@ -530,6 +536,7 @@ class FunctionsCustomServerTest extends Scope $name = $env['name'] ?? ''; $code = $env['code'] ?? ''; $command = $env['command'] ?? ''; + $timeout = $env['timeout'] ?? 15; $function = $this->client->call(Client::METHOD_POST, '/functions', array_merge([ 'content-type' => 'application/json', @@ -545,7 +552,7 @@ class FunctionsCustomServerTest extends Scope ], 'events' => [], 'schedule' => '', - 'timeout' => 15, + 'timeout' => $timeout, ]); // var_dump('http://'.gethostbyname(trim(`hostname`)).'/v1'); @@ -584,7 +591,7 @@ class FunctionsCustomServerTest extends Scope $executionId = $execution['body']['$id'] ?? ''; $this->assertEquals(201, $execution['headers']['status-code']); - sleep(25); + sleep(15); $executions = $this->client->call(Client::METHOD_GET, '/functions/'.$functionId.'/executions', array_merge([ 'content-type' => 'application/json', @@ -619,4 +626,82 @@ class FunctionsCustomServerTest extends Scope 'functionId' => $functionId, ]; } + /** + * @depends testENVS + */ + public function testTimeout() + { + $language = 'PHP'; + $version = '8.0'; + $name = 'php-8.0'; + $code = realpath(__DIR__ . '/../../../resources/functions').'/timeout.tar.gz'; + $command = 'php index.php'; + $timeout = 2; + + $function = $this->client->call(Client::METHOD_POST, '/functions', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'name' => 'Test '.$name, + 'env' => $name, + 'vars' => [], + 'events' => [], + 'schedule' => '', + 'timeout' => $timeout, + ]); + + $functionId = $function['body']['$id'] ?? ''; + + $this->assertEquals(201, $function['headers']['status-code']); + + $tag = $this->client->call(Client::METHOD_POST, '/functions/'.$functionId.'/tags', array_merge([ + 'content-type' => 'multipart/form-data', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'command' => $command, + 'code' => new CURLFile($code, 'application/x-gzip', basename($code)), + ]); + + $tagId = $tag['body']['$id'] ?? ''; + $this->assertEquals(201, $tag['headers']['status-code']); + + $tag = $this->client->call(Client::METHOD_PATCH, '/functions/'.$functionId.'/tag', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'tag' => $tagId, + ]); + + $this->assertEquals(200, $tag['headers']['status-code']); + + $execution = $this->client->call(Client::METHOD_POST, '/functions/'.$functionId.'/executions', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'async' => 1, + ]); + + $executionId = $execution['body']['$id'] ?? ''; + $this->assertEquals(201, $execution['headers']['status-code']); + + sleep(15); + + $executions = $this->client->call(Client::METHOD_GET, '/functions/'.$functionId.'/executions', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + + $this->assertEquals($executions['headers']['status-code'], 200); + $this->assertEquals($executions['body']['sum'], 1); + $this->assertIsArray($executions['body']['executions']); + $this->assertCount(1, $executions['body']['executions']); + $this->assertEquals($executions['body']['executions'][0]['$id'], $executionId); + $this->assertEquals($executions['body']['executions'][0]['trigger'], 'http'); + $this->assertEquals($executions['body']['executions'][0]['status'], 'failed'); + $this->assertEquals($executions['body']['executions'][0]['exitCode'], 1); + $this->assertGreaterThan(2, $executions['body']['executions'][0]['time']); + $this->assertLessThan(3, $executions['body']['executions'][0]['time']); + $this->assertEquals($executions['body']['executions'][0]['stdout'], ''); + $this->assertEquals($executions['body']['executions'][0]['stderr'], ''); + } } \ No newline at end of file diff --git a/tests/resources/functions/package-php.sh b/tests/resources/functions/package-php.sh index 17aa8602e1..86f0844d9f 100644 --- a/tests/resources/functions/package-php.sh +++ b/tests/resources/functions/package-php.sh @@ -3,8 +3,6 @@ echo 'PHP Packaging...' cp -r $(pwd)/tests/resources/functions/php $(pwd)/tests/resources/functions/packages/php -ls -ll $(pwd)/tests/resources/functions/packages/php - docker run --rm -v $(pwd)/tests/resources/functions/packages/php:/app -w /app composer:2.0 composer install --ignore-platform-reqs docker run --rm -v $(pwd)/tests/resources/functions/packages/php:/app -w /app appwrite/env-php-8.0:1.0.0 tar -zcvf code.tar.gz . diff --git a/tests/resources/functions/package-timeout.sh b/tests/resources/functions/package-timeout.sh new file mode 100644 index 0000000000..6ea5eeab64 --- /dev/null +++ b/tests/resources/functions/package-timeout.sh @@ -0,0 +1,10 @@ + +echo 'Timeout Packaging...' + +cp -r $(pwd)/tests/resources/functions/timeout $(pwd)/tests/resources/functions/packages/timeout + +docker run --rm -v $(pwd)/tests/resources/functions/packages/timeout:/app -w /app appwrite/env-php-8.0:1.0.0 tar -zcvf code.tar.gz . + +mv $(pwd)/tests/resources/functions/packages/timeout/code.tar.gz $(pwd)/tests/resources/functions/timeout.tar.gz + +rm -r $(pwd)/tests/resources/functions/packages/timeout \ No newline at end of file diff --git a/tests/resources/functions/timeout.tar.gz b/tests/resources/functions/timeout.tar.gz new file mode 100644 index 0000000000000000000000000000000000000000..16a2bec08bd936e2d6e0f9a864f409252763dcf5 GIT binary patch literal 168 zcmb2|=3oE;Cg!&neYp-92(&$1Q{AOgdO0m;S)@jV*ujRT8yCVp{5bYb6z!TagP ziE0xR<0EID+^Mso&vf_t$C0lttO&k%;Yl6GR^zbc@n>fQ+viwi`(EF6cysmoXZEYs zd^?_NGd**Y8g2#U!{}C^my}zT%@LKI7=6^Q-uUe_ci~LnQW+H4JUstUNb_|Tj P-_3B%bUrtO1_J{C`sPY2 literal 0 HcmV?d00001 diff --git a/tests/resources/functions/timeout/index.php b/tests/resources/functions/timeout/index.php new file mode 100644 index 0000000000..83cb8e1b56 --- /dev/null +++ b/tests/resources/functions/timeout/index.php @@ -0,0 +1,3 @@ +