From 5b184236c1eb737db85bc8556e3f58a4f43b2bea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Thu, 8 Aug 2024 09:38:37 +0200 Subject: [PATCH 01/13] Dynamic keys in builds --- src/Appwrite/Platform/Workers/Builds.php | 14 ++++++++++++++ .../Functions/FunctionsCustomServerTest.php | 11 +++++++++++ tests/resources/functions/php-scopes/setup.sh | 6 ++++++ 3 files changed, 31 insertions(+) create mode 100644 tests/resources/functions/php-scopes/setup.sh diff --git a/src/Appwrite/Platform/Workers/Builds.php b/src/Appwrite/Platform/Workers/Builds.php index d0fb597258..818dbf89d1 100644 --- a/src/Appwrite/Platform/Workers/Builds.php +++ b/src/Appwrite/Platform/Workers/Builds.php @@ -2,6 +2,7 @@ namespace Appwrite\Platform\Workers; +use Ahc\Jwt\JWT; use Appwrite\Event\Event; use Appwrite\Event\Func; use Appwrite\Event\Usage; @@ -393,8 +394,21 @@ class Builds extends Action $vars[$var->getAttribute('key')] = $var->getAttribute('value', ''); } + $jwtExpiry = (int) System::getEnv('_APP_FUNCTIONS_BUILD_TIMEOUT', 900); + $jwtObj = new JWT(System::getEnv('_APP_OPENSSL_KEY_V1'), 'HS256', $jwtExpiry, 0); + $apiKey = $jwtObj->encode([ + 'projectId' => $project->getId(), + 'scopes' => $function->getAttribute('scopes', []) + ]); + + $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS') == 'disabled' ? 'http' : 'https'; + $hostname = System::getEnv('_APP_DOMAIN'); + $endpoint = $protocol . '://' . $hostname . "/v1"; + // Appwrite vars $vars = \array_merge($vars, [ + 'APPWRITE_FUNCTION_API_ENDPOINT' => $endpoint, + 'APPWRITE_FUNCTION_API_KEY' => API_KEY_DYNAMIC . '_' . $apiKey, 'APPWRITE_FUNCTION_ID' => $function->getId(), 'APPWRITE_FUNCTION_NAME' => $function->getAttribute('name'), 'APPWRITE_FUNCTION_DEPLOYMENT' => $deployment->getId(), diff --git a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php index e3148752c8..9510276a80 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php @@ -1544,6 +1544,7 @@ class FunctionsCustomServerTest extends Scope 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ 'entrypoint' => 'index.php', + 'commands' => 'sh setup.sh', 'code' => new CURLFile($code, 'application/x-gzip', basename($code)), 'activate' => true ]); @@ -1553,6 +1554,16 @@ class FunctionsCustomServerTest extends Scope $this->awaitDeploymentIsBuilt($function['body']['$id'], $deploymentId, checkForSuccess: false); + $deployment = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId . '/deployments/' . $deploymentId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), []); + + $this->assertEquals(200, $deployment['headers']['status-code']); + $this->assertStringContainsStringIgnoringCase("200 OK", $deployment['body']['buildLogs']); + $this->assertStringContainsStringIgnoringCase('"total":', $deployment['body']['buildLogs']); + $this->assertStringContainsStringIgnoringCase('"users":', $deployment['body']['buildLogs']); + $deployment = $this->client->call(Client::METHOD_PATCH, '/functions/' . $functionId . '/deployments/' . $deploymentId, array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], diff --git a/tests/resources/functions/php-scopes/setup.sh b/tests/resources/functions/php-scopes/setup.sh new file mode 100644 index 0000000000..a2f78a4f3d --- /dev/null +++ b/tests/resources/functions/php-scopes/setup.sh @@ -0,0 +1,6 @@ + +ENDPOINT="$APPWRITE_FUNCTION_API_ENDPOINT/users" +PROJECT_ID="$APPWRITE_FUNCTION_PROJECT_ID" +API_KEY="$APPWRITE_FUNCTION_API_KEY" + +curl -v -X GET $ENDPOINT -H "x-appwrite-project: $PROJECT_ID" -H "x-appwrite-key: $API_KEY" \ No newline at end of file From 25f5c83594a3d0ff6713d579f6ac6c100a691a0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Thu, 8 Aug 2024 08:38:15 +0000 Subject: [PATCH 02/13] Fix 4XX execution status --- app/controllers/api/functions.php | 2 +- app/controllers/general.php | 2 +- src/Appwrite/Platform/Workers/Functions.php | 2 +- .../Functions/FunctionsCustomServerTest.php | 67 +++++++++++++++++++ tests/resources/functions/php-4xx/index.php | 5 ++ 5 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 tests/resources/functions/php-4xx/index.php diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index f155d7427c..e08c69b8fb 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -1878,7 +1878,7 @@ App::post('/v1/functions/:functionId/executions') } /** Update execution status */ - $status = $executionResponse['statusCode'] >= 400 ? 'failed' : 'completed'; + $status = $executionResponse['statusCode'] >= 500 ? 'failed' : 'completed'; $execution->setAttribute('status', $status); $execution->setAttribute('responseStatusCode', $executionResponse['statusCode']); $execution->setAttribute('responseHeaders', $headersFiltered); diff --git a/app/controllers/general.php b/app/controllers/general.php index d532110df3..95e6dc18df 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -302,7 +302,7 @@ function router(App $utopia, Database $dbForConsole, callable $getProjectDB, Swo } /** Update execution status */ - $status = $executionResponse['statusCode'] >= 400 ? 'failed' : 'completed'; + $status = $executionResponse['statusCode'] >= 500 ? 'failed' : 'completed'; $execution->setAttribute('status', $status); $execution->setAttribute('responseStatusCode', $executionResponse['statusCode']); $execution->setAttribute('responseHeaders', $headersFiltered); diff --git a/src/Appwrite/Platform/Workers/Functions.php b/src/Appwrite/Platform/Workers/Functions.php index 78671bfeb0..8ade08dc2d 100644 --- a/src/Appwrite/Platform/Workers/Functions.php +++ b/src/Appwrite/Platform/Workers/Functions.php @@ -494,7 +494,7 @@ class Functions extends Action logging: $function->getAttribute('logging', true), ); - $status = $executionResponse['statusCode'] >= 400 ? 'failed' : 'completed'; + $status = $executionResponse['statusCode'] >= 500 ? 'failed' : 'completed'; $headersFiltered = []; foreach ($executionResponse['headers'] as $key => $value) { diff --git a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php index e3148752c8..30f7c54df7 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php @@ -1659,6 +1659,73 @@ class FunctionsCustomServerTest extends Scope $this->assertEquals(204, $response['headers']['status-code']); } + public function text4xxExecution() + { + $timeout = 15; + $code = realpath(__DIR__ . '/../../../resources/functions') . "/php-4xx/code.tar.gz"; + $this->packageCode('php-4xx'); + + $function = $this->client->call(Client::METHOD_POST, '/functions', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'functionId' => ID::unique(), + 'name' => 'Test PHP 4XX executions', + 'runtime' => 'php-8.0', + 'entrypoint' => 'index.php', + 'timeout' => $timeout, + ]); + + $functionId = $function['body']['$id'] ?? ''; + + $this->assertEquals(201, $function['headers']['status-code']); + + $deployment = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/deployments', array_merge([ + 'content-type' => 'multipart/form-data', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'entrypoint' => 'index.php', + 'code' => new CURLFile($code, 'application/x-gzip', basename($code)), + 'activate' => true + ]); + + $deploymentId = $deployment['body']['$id'] ?? ''; + $this->assertEquals(202, $deployment['headers']['status-code']); + + $this->awaitDeploymentIsBuilt($function['body']['$id'], $deploymentId, checkForSuccess: false); + + $deployment = $this->client->call(Client::METHOD_PATCH, '/functions/' . $functionId . '/deployments/' . $deploymentId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), []); + + $this->assertEquals(200, $deployment['headers']['status-code']); + + // Wait a little for activation to finish + sleep(5); + + $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' => false + ]); + + $this->assertEquals(201, $execution['headers']['status-code']); + $this->assertEquals('completed', $execution['body']['status']); + $this->assertEquals(400, $execution['body']['responseStatusCode']); + $this->assertEquals('Invalid input.', $execution['body']['responseBody']); + + // Cleanup : Delete function + $response = $this->client->call(Client::METHOD_DELETE, '/functions/' . $functionId, [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ], []); + + $this->assertEquals(204, $response['headers']['status-code']); + } + public function testFunctionsDomain() { $timeout = 15; diff --git a/tests/resources/functions/php-4xx/index.php b/tests/resources/functions/php-4xx/index.php new file mode 100644 index 0000000000..92f0bf28fe --- /dev/null +++ b/tests/resources/functions/php-4xx/index.php @@ -0,0 +1,5 @@ +res->text('Invalid input.', 400); +}; From f8fb98d377663aad2333f115de7b894c2763a9a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Thu, 8 Aug 2024 08:42:38 +0000 Subject: [PATCH 03/13] Update response filter --- src/Appwrite/Utopia/Response/Filters/V18.php | 4 ++ .../unit/Utopia/Response/Filters/V18Test.php | 40 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/Appwrite/Utopia/Response/Filters/V18.php b/src/Appwrite/Utopia/Response/Filters/V18.php index d0aa680e3b..62a19e3985 100644 --- a/src/Appwrite/Utopia/Response/Filters/V18.php +++ b/src/Appwrite/Utopia/Response/Filters/V18.php @@ -24,6 +24,10 @@ class V18 extends Filter protected function parseExecution(array $content) { + if($content['status'] === 'completed' && $content['statusCode'] >= 400 && $content['statusCode'] < 500) { + $content['status'] === 'failed'; + } + unset($content['scheduledAt']); return $content; } diff --git a/tests/unit/Utopia/Response/Filters/V18Test.php b/tests/unit/Utopia/Response/Filters/V18Test.php index c4011c08a1..2110e518a9 100644 --- a/tests/unit/Utopia/Response/Filters/V18Test.php +++ b/tests/unit/Utopia/Response/Filters/V18Test.php @@ -34,6 +34,46 @@ class V18Test extends TestCase ], [ ] + ], + 'update 404 status' => [ + [ + 'statusCode' => '404', + 'status' => 'completed' + ], + [ + 'statusCode' => '404', + 'status' => 'failed' + ] + ], + 'update 400 status' => [ + [ + 'statusCode' => '400', + 'status' => 'completed' + ], + [ + 'statusCode' => '400', + 'status' => 'failed' + ] + ], + 'dont update 200 status' => [ + [ + 'statusCode' => '200', + 'status' => 'completed' + ], + [ + 'statusCode' => '200', + 'status' => 'completed' + ] + ], + 'dont update 500 status' => [ + [ + 'statusCode' => '500', + 'status' => 'failed' + ], + [ + 'statusCode' => '500', + 'status' => 'failed' + ] ] ]; } From f561311d761040a4ec5cdd5cf779d9f62f19120b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Thu, 8 Aug 2024 08:50:01 +0000 Subject: [PATCH 04/13] Fix failing test --- tests/e2e/Services/Functions/FunctionsCustomServerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php index 9510276a80..f58f307871 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php @@ -1544,7 +1544,7 @@ class FunctionsCustomServerTest extends Scope 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ 'entrypoint' => 'index.php', - 'commands' => 'sh setup.sh', + 'commands' => 'sh setup.sh && composer install', 'code' => new CURLFile($code, 'application/x-gzip', basename($code)), 'activate' => true ]); From bc4a2508138424f3e981ab9ceaf7dd615c872643 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Thu, 8 Aug 2024 08:51:38 +0000 Subject: [PATCH 05/13] Fix bug in response filter --- src/Appwrite/Utopia/Response/Filters/V18.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Appwrite/Utopia/Response/Filters/V18.php b/src/Appwrite/Utopia/Response/Filters/V18.php index 62a19e3985..2b78d60b37 100644 --- a/src/Appwrite/Utopia/Response/Filters/V18.php +++ b/src/Appwrite/Utopia/Response/Filters/V18.php @@ -25,7 +25,7 @@ class V18 extends Filter protected function parseExecution(array $content) { if($content['status'] === 'completed' && $content['statusCode'] >= 400 && $content['statusCode'] < 500) { - $content['status'] === 'failed'; + $content['status'] = 'failed'; } unset($content['scheduledAt']); From 90b9d176ba9cb29d1292855aa4bd07d6e88c28fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Thu, 8 Aug 2024 09:02:49 +0000 Subject: [PATCH 06/13] Fix unit test placement --- .../unit/Utopia/Response/Filters/V18Test.php | 52 +++++++++---------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/tests/unit/Utopia/Response/Filters/V18Test.php b/tests/unit/Utopia/Response/Filters/V18Test.php index 2110e518a9..5396779b77 100644 --- a/tests/unit/Utopia/Response/Filters/V18Test.php +++ b/tests/unit/Utopia/Response/Filters/V18Test.php @@ -35,6 +35,32 @@ class V18Test extends TestCase [ ] ], + ]; + } + + /** + * @dataProvider functionProvider + */ + public function testFunction(array $content, array $expected): void + { + $model = Response::MODEL_FUNCTION; + + $result = $this->filter->parse($content, $model); + + $this->assertEquals($expected, $result); + } + + + public function executionProvider(): array + { + return [ + 'remove scheduledAt' => [ + [ + 'scheduledAt' => '2024-07-13T09:00:00.000Z', + ], + [ + ] + ], 'update 404 status' => [ [ 'statusCode' => '404', @@ -78,32 +104,6 @@ class V18Test extends TestCase ]; } - /** - * @dataProvider functionProvider - */ - public function testFunction(array $content, array $expected): void - { - $model = Response::MODEL_FUNCTION; - - $result = $this->filter->parse($content, $model); - - $this->assertEquals($expected, $result); - } - - - public function executionProvider(): array - { - return [ - 'remove scheduledAt' => [ - [ - 'scheduledAt' => '2024-07-13T09:00:00.000Z', - ], - [ - ] - ] - ]; - } - /** * @dataProvider executionProvider */ From 47316a250b375b04ffee29243d906820e8fef177 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Thu, 8 Aug 2024 09:15:53 +0000 Subject: [PATCH 07/13] Edge case for test case --- src/Appwrite/Utopia/Response/Filters/V18.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Appwrite/Utopia/Response/Filters/V18.php b/src/Appwrite/Utopia/Response/Filters/V18.php index 2b78d60b37..6485b6f0ba 100644 --- a/src/Appwrite/Utopia/Response/Filters/V18.php +++ b/src/Appwrite/Utopia/Response/Filters/V18.php @@ -24,8 +24,10 @@ class V18 extends Filter protected function parseExecution(array $content) { - if($content['status'] === 'completed' && $content['statusCode'] >= 400 && $content['statusCode'] < 500) { - $content['status'] = 'failed'; + if(!empty($content['status']) && !empty($content['statusCode'])) { + if($content['status'] === 'completed' && $content['statusCode'] >= 400 && $content['statusCode'] < 500) { + $content['status'] = 'failed'; + } } unset($content['scheduledAt']); From f6dec62c507333a79904ebf18a730081b60d91c2 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 9 Aug 2024 12:56:55 +0100 Subject: [PATCH 08/13] feat: add base/key runtime --- app/controllers/api/functions.php | 4 ++-- src/Appwrite/Utopia/Response/Model/Runtime.php | 6 ++++++ tests/e2e/Services/Functions/FunctionsCustomServerTest.php | 1 + 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index 443260ea90..f70895ad60 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -436,8 +436,8 @@ App::get('/v1/functions/runtimes') continue; } - $runtimes[$key]['$id'] = $key; - $allowed[] = $runtimes[$key]; + $runtime['$id'] = $key; + $allowed[] = $runtime; } $response->dynamic(new Document([ diff --git a/src/Appwrite/Utopia/Response/Model/Runtime.php b/src/Appwrite/Utopia/Response/Model/Runtime.php index 8bc42cb418..3c328c4d40 100644 --- a/src/Appwrite/Utopia/Response/Model/Runtime.php +++ b/src/Appwrite/Utopia/Response/Model/Runtime.php @@ -16,6 +16,12 @@ class Runtime extends Model 'default' => '', 'example' => 'python-3.8', ]) + ->addRule('key', [ + 'type' => self::TYPE_STRING, + 'description' => 'Parent runtime key.', + 'default' => '', + 'example' => 'python', + ]) ->addRule('name', [ 'type' => self::TYPE_STRING, 'description' => 'Runtime Name.', diff --git a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php index e3148752c8..3e6a00a4cb 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php @@ -1412,6 +1412,7 @@ class FunctionsCustomServerTest extends Scope $this->assertArrayHasKey('$id', $runtime); $this->assertArrayHasKey('name', $runtime); + $this->assertArrayHasKey('key', $runtime); $this->assertArrayHasKey('version', $runtime); $this->assertArrayHasKey('logo', $runtime); $this->assertArrayHasKey('image', $runtime); From e1c8cfc4a13334f225af572957866994f3f6e800 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 9 Aug 2024 12:59:43 +0100 Subject: [PATCH 09/13] chore: refactor $key/$id names --- app/controllers/api/functions.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index f70895ad60..3232f6bfe1 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -431,12 +431,12 @@ App::get('/v1/functions/runtimes') $allowList = \array_filter(\explode(',', System::getEnv('_APP_FUNCTIONS_RUNTIMES', ''))); $allowed = []; - foreach ($runtimes as $key => $runtime) { - if (!empty($allowList) && !\in_array($key, $allowList)) { + foreach ($runtimes as $id => $runtime) { + if (!empty($allowList) && !\in_array($id, $allowList)) { continue; } - $runtime['$id'] = $key; + $runtime['$id'] = $id; $allowed[] = $runtime; } From 738b08aab54d4615738d4c067d8c1d9af1239362 Mon Sep 17 00:00:00 2001 From: Binyamin Yawitz <316103+byawitz@users.noreply.github.com> Date: Fri, 9 Aug 2024 12:43:05 -0400 Subject: [PATCH 10/13] fix: Updating benchmark comment instead of a new one --- .github/workflows/tests.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 3e5cb35721..c4e911b340 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -8,7 +8,7 @@ env: IMAGE: appwrite-dev CACHE_KEY: appwrite-dev-${{ github.event.pull_request.head.sha }} -on: [pull_request] +on: [ pull_request ] jobs: setup: @@ -216,3 +216,4 @@ jobs: uses: thollander/actions-comment-pull-request@v2 with: filePath: benchmark.txt + comment_tag: benchmark From bd30c9c1532e9bb560a46fa9818f50069f542994 Mon Sep 17 00:00:00 2001 From: Binyamin Yawitz <316103+byawitz@users.noreply.github.com> Date: Fri, 9 Aug 2024 14:19:30 -0400 Subject: [PATCH 11/13] fix: checking other package --- .github/workflows/tests.yml | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index c4e911b340..d6b8c3e0c1 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -212,8 +212,17 @@ jobs: name: benchmark.json path: benchmark.json retention-days: 7 - - name: Comment on PR - uses: thollander/actions-comment-pull-request@v2 + - name: Find Comment + uses: peter-evans/find-comment@v3 + id: fc with: - filePath: benchmark.txt - comment_tag: benchmark + issue-number: ${{ github.event.pull_request.number }} + comment-author: 'github-actions[bot]' + body-includes: Benchmark results + - name: Comment on PR + uses: peter-evans/create-or-update-comment@v4 + with: + comment-id: ${{ steps.fc.outputs.comment-id }} + issue-number: ${{ github.event.pull_request.number }} + body-path: benchmark.txt + edit-mode: replace From 6f1e3722bb7e37e33f6f2d451b88d87a2eb4e057 Mon Sep 17 00:00:00 2001 From: Binyamin Yawitz <316103+byawitz@users.noreply.github.com> Date: Fri, 9 Aug 2024 14:44:51 -0400 Subject: [PATCH 12/13] fix: checking another package --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index d6b8c3e0c1..633bd46ea4 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -8,7 +8,7 @@ env: IMAGE: appwrite-dev CACHE_KEY: appwrite-dev-${{ github.event.pull_request.head.sha }} -on: [ pull_request ] +on: [pull_request] jobs: setup: From 1334cf116bd3d50fe75e27e551c474a152889a15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 12 Aug 2024 07:26:20 +0000 Subject: [PATCH 13/13] Speed up new test --- .../Functions/FunctionsCustomServerTest.php | 84 ++++--------------- tests/resources/functions/php-4xx/index.php | 5 -- tests/resources/functions/php/index.php | 4 +- 3 files changed, 20 insertions(+), 73 deletions(-) delete mode 100644 tests/resources/functions/php-4xx/index.php diff --git a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php index 30f7c54df7..0b6bf985c1 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php @@ -802,6 +802,23 @@ class FunctionsCustomServerTest extends Scope $this->assertEquals('', $execution['body']['logs']); $this->assertLessThan(10, $execution['body']['duration']); + $execution = $this->client->call(Client::METHOD_POST, '/functions/' . $data['functionId'] . '/executions', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'async' => false, + 'path' => '/?code=400' + ]); + $this->assertEquals(201, $execution['headers']['status-code']); + $this->assertEquals('completed', $execution['body']['status']); + $this->assertEquals(400, $execution['body']['responseStatusCode']); + + $execution = $this->client->call(Client::METHOD_DELETE, '/functions/' . $data['functionId'] . '/executions/' . $execution['body']['$id'], array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), []); + $this->assertEquals(204, $execution['headers']['status-code']); + return array_merge($data, ['executionId' => $executionId]); } @@ -1659,73 +1676,6 @@ class FunctionsCustomServerTest extends Scope $this->assertEquals(204, $response['headers']['status-code']); } - public function text4xxExecution() - { - $timeout = 15; - $code = realpath(__DIR__ . '/../../../resources/functions') . "/php-4xx/code.tar.gz"; - $this->packageCode('php-4xx'); - - $function = $this->client->call(Client::METHOD_POST, '/functions', array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()), [ - 'functionId' => ID::unique(), - 'name' => 'Test PHP 4XX executions', - 'runtime' => 'php-8.0', - 'entrypoint' => 'index.php', - 'timeout' => $timeout, - ]); - - $functionId = $function['body']['$id'] ?? ''; - - $this->assertEquals(201, $function['headers']['status-code']); - - $deployment = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/deployments', array_merge([ - 'content-type' => 'multipart/form-data', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()), [ - 'entrypoint' => 'index.php', - 'code' => new CURLFile($code, 'application/x-gzip', basename($code)), - 'activate' => true - ]); - - $deploymentId = $deployment['body']['$id'] ?? ''; - $this->assertEquals(202, $deployment['headers']['status-code']); - - $this->awaitDeploymentIsBuilt($function['body']['$id'], $deploymentId, checkForSuccess: false); - - $deployment = $this->client->call(Client::METHOD_PATCH, '/functions/' . $functionId . '/deployments/' . $deploymentId, array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()), []); - - $this->assertEquals(200, $deployment['headers']['status-code']); - - // Wait a little for activation to finish - sleep(5); - - $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' => false - ]); - - $this->assertEquals(201, $execution['headers']['status-code']); - $this->assertEquals('completed', $execution['body']['status']); - $this->assertEquals(400, $execution['body']['responseStatusCode']); - $this->assertEquals('Invalid input.', $execution['body']['responseBody']); - - // Cleanup : Delete function - $response = $this->client->call(Client::METHOD_DELETE, '/functions/' . $functionId, [ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - 'x-appwrite-key' => $this->getProject()['apiKey'], - ], []); - - $this->assertEquals(204, $response['headers']['status-code']); - } - public function testFunctionsDomain() { $timeout = 15; diff --git a/tests/resources/functions/php-4xx/index.php b/tests/resources/functions/php-4xx/index.php deleted file mode 100644 index 92f0bf28fe..0000000000 --- a/tests/resources/functions/php-4xx/index.php +++ /dev/null @@ -1,5 +0,0 @@ -res->text('Invalid input.', 400); -}; diff --git a/tests/resources/functions/php/index.php b/tests/resources/functions/php/index.php index d5328c40e1..e8e5eb67d5 100644 --- a/tests/resources/functions/php/index.php +++ b/tests/resources/functions/php/index.php @@ -1,6 +1,8 @@ req->query['code'] ?? '200'; + return $context->res->json([ 'APPWRITE_FUNCTION_ID' => \getenv('APPWRITE_FUNCTION_ID') ?: '', 'APPWRITE_FUNCTION_NAME' => \getenv('APPWRITE_FUNCTION_NAME') ?: '', @@ -11,5 +13,5 @@ return function ($context) { 'APPWRITE_REGION' => \getenv('APPWRITE_REGION') ?: '', 'UNICODE_TEST' => "êä", 'GLOBAL_VARIABLE' => \getenv('GLOBAL_VARIABLE') ?: '' - ]); + ], \intval($statusCode)); };