appwrite/tests/e2e/Services/GraphQL/FunctionsServerTest.php

535 lines
17 KiB
PHP
Raw Normal View History

2022-07-04 04:14:37 +00:00
<?php
namespace Tests\E2E\Services\GraphQL;
use Appwrite\Tests\Async;
use Appwrite\Tests\Async\Exceptions\Critical;
2022-07-04 04:14:37 +00:00
use Tests\E2E\Client;
use Tests\E2E\Scopes\ProjectCustom;
use Tests\E2E\Scopes\Scope;
use Tests\E2E\Scopes\SideServer;
2023-01-16 09:25:40 +00:00
use Utopia\Database\Helpers\ID;
use Utopia\Database\Helpers\Role;
2022-07-04 04:14:37 +00:00
2022-09-22 08:29:42 +00:00
class FunctionsServerTest extends Scope
2022-07-04 04:14:37 +00:00
{
use ProjectCustom;
use SideServer;
2022-09-22 08:29:42 +00:00
use Base;
use Async;
2022-07-04 04:14:37 +00:00
private static array $cachedFunction = [];
private static array $cachedDeployment = [];
private static array $cachedExecution = [];
protected function setupFunction(): array
2022-07-04 04:14:37 +00:00
{
$key = $this->getProject()['$id'];
2026-03-31 16:34:37 +00:00
if (!empty(self::$cachedFunction[$key])) {
return self::$cachedFunction[$key];
}
2022-07-04 04:14:37 +00:00
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::CREATE_FUNCTION);
2022-07-04 04:14:37 +00:00
$gqlPayload = [
'query' => $query,
'variables' => [
2022-09-22 01:53:41 +00:00
'functionId' => ID::unique(),
2022-07-04 04:14:37 +00:00
'name' => 'Test Function',
2025-07-09 14:58:55 +00:00
'entrypoint' => 'index.js',
'runtime' => 'node-22',
2022-09-22 02:16:13 +00:00
'execute' => [Role::any()->toString()],
2022-07-04 04:14:37 +00:00
]
];
$function = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
$this->assertIsArray($function['body']['data']);
$this->assertArrayNotHasKey('errors', $function['body']);
2022-09-22 02:16:13 +00:00
2022-07-04 04:14:37 +00:00
$function = $function['body']['data']['functionsCreate'];
2022-12-08 03:08:57 +00:00
$functionId = $function['_id'];
2022-09-22 02:16:13 +00:00
$query = '
mutation createVariables($functionId: String!) {
var1: functionsCreateVariable(functionId: $functionId, key: "name", value: "John Doe") {
2022-12-08 03:08:57 +00:00
_id
2022-09-22 02:16:13 +00:00
}
var2: functionsCreateVariable(functionId: $functionId, key: "age", value: "42") {
2022-12-08 03:08:57 +00:00
_id
2022-09-22 02:16:13 +00:00
}
}
';
$gqlPayload = [
'query' => $query,
'variables' => [
'functionId' => $functionId,
]
];
$variables = $this->client->call(Client::METHOD_POST, '/graphql', [
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $this->getProject()['apiKey'],
], $gqlPayload);
$this->assertIsArray($variables['body']['data']);
$this->assertArrayNotHasKey('errors', $variables['body']);
2022-07-04 04:14:37 +00:00
2026-03-31 16:34:37 +00:00
self::$cachedFunction[$key] = $function;
2022-07-04 04:14:37 +00:00
return $function;
}
protected function setupDeployment(): array
2022-07-04 04:14:37 +00:00
{
$key = $this->getProject()['$id'];
2026-03-31 16:34:37 +00:00
if (!empty(self::$cachedDeployment[$key])) {
return self::$cachedDeployment[$key];
}
$function = $this->setupFunction();
2022-07-04 04:14:37 +00:00
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::CREATE_DEPLOYMENT);
2023-10-26 14:50:35 +00:00
2022-07-04 04:14:37 +00:00
$gqlPayload = [
2022-07-07 05:50:49 +00:00
'operations' => \json_encode([
'query' => $query,
'variables' => [
2022-12-08 03:08:57 +00:00
'functionId' => $function['_id'],
2022-07-07 05:50:49 +00:00
'activate' => true,
'code' => null,
]
]),
'map' => \json_encode([
'code' => ["variables.code"]
]),
2025-07-09 14:58:55 +00:00
'code' => $this->packageFunction('basic'),
2022-07-04 04:14:37 +00:00
];
$deployment = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
2022-07-07 05:50:49 +00:00
'content-type' => 'multipart/form-data',
2022-07-04 04:14:37 +00:00
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
$this->assertIsArray($deployment['body']['data']);
$this->assertArrayNotHasKey('errors', $deployment['body']);
2023-08-29 01:11:57 +00:00
// Poll get deployment until an error, or status is either 'ready' or 'failed'
$deployment = $deployment['body']['data']['functionsCreateDeployment'];
$deploymentId = $deployment['_id'];
2022-07-07 05:50:49 +00:00
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::GET_DEPLOYMENT);
2023-08-29 01:11:57 +00:00
$gqlPayload = [
'query' => $query,
'variables' => [
'functionId' => $function['_id'],
'deploymentId' => $deploymentId,
]
];
$this->assertEventually(function () use ($projectId, $gqlPayload, &$deployment) {
2023-08-29 01:11:57 +00:00
$deployment = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
$this->assertIsArray($deployment['body']['data']);
$this->assertArrayNotHasKey('errors', $deployment['body']);
$deployment = $deployment['body']['data']['functionsGetDeployment'];
if ($deployment['status'] === 'failed') {
throw new Critical('Deployment build failed: ' . ($deployment['buildLogs'] ?? 'no logs'));
}
$this->assertEquals('ready', $deployment['status']);
2026-02-24 01:00:07 +00:00
}, 120000);
2026-03-31 16:34:37 +00:00
self::$cachedDeployment[$key] = $deployment;
2023-08-29 01:11:57 +00:00
return $deployment;
2022-07-04 04:14:37 +00:00
}
protected function setupExecution(): array
2022-07-04 04:14:37 +00:00
{
$key = $this->getProject()['$id'];
2026-03-31 16:34:37 +00:00
if (!empty(self::$cachedExecution[$key])) {
return self::$cachedExecution[$key];
}
$deployment = $this->setupDeployment();
2022-07-04 04:14:37 +00:00
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::CREATE_EXECUTION);
2022-07-04 04:14:37 +00:00
$gqlPayload = [
'query' => $query,
'variables' => [
2023-08-29 01:11:57 +00:00
'functionId' => $deployment['resourceId'],
2022-07-04 04:14:37 +00:00
]
];
$execution = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
$this->assertIsArray($execution['body']['data']);
$this->assertArrayNotHasKey('errors', $execution['body']);
2026-03-31 16:34:37 +00:00
self::$cachedExecution[$key] = $execution['body']['data']['functionsCreateExecution'];
return self::$cachedExecution[$key];
}
public function testCreateFunction(): void
{
$function = $this->setupFunction();
$this->assertIsArray($function);
2022-07-04 04:14:37 +00:00
}
/**
2023-08-29 01:11:57 +00:00
* @return void
2022-07-04 04:14:37 +00:00
* @throws \Exception
*/
public function testCreateDeployment(): void
2022-07-04 04:14:37 +00:00
{
$deployment = $this->setupDeployment();
$this->assertIsArray($deployment);
}
/**
* @return void
* @throws \Exception
*/
public function testCreateExecution(): void
{
$execution = $this->setupExecution();
$this->assertIsArray($execution);
}
/**
* @return void
* @throws \Exception
*/
public function testCreateRetryBuild(): void
{
$deployment = $this->setupDeployment();
2022-07-04 04:14:37 +00:00
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::RETRY_BUILD);
2022-07-04 04:14:37 +00:00
$gqlPayload = [
'query' => $query,
'variables' => [
2023-08-29 01:11:57 +00:00
'functionId' => $deployment['resourceId'],
2022-12-08 03:08:57 +00:00
'deploymentId' => $deployment['_id'],
2022-07-07 05:50:49 +00:00
'buildId' => $deployment['buildId'],
2022-07-04 04:14:37 +00:00
]
];
2023-08-29 01:11:57 +00:00
$response = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
2022-07-04 04:14:37 +00:00
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
2025-03-05 12:00:06 +00:00
$this->assertIsArray($response['body']['data']);
$this->assertEquals(200, $response['headers']['status-code']);
2022-07-04 04:14:37 +00:00
}
public function testGetFunctions(): array
{
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::GET_FUNCTIONS);
2022-07-04 04:14:37 +00:00
$gqlPayload = [
'query' => $query,
];
$functions = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
$this->assertIsArray($functions['body']['data']);
$this->assertArrayNotHasKey('errors', $functions['body']);
$functions = $functions['body']['data']['functionsList'];
$this->assertIsArray($functions);
return $functions;
}
/**
* @return array
* @throws \Exception
*/
public function testGetFunction(): array
2022-07-04 04:14:37 +00:00
{
$function = $this->setupFunction();
2022-07-04 04:14:37 +00:00
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::GET_FUNCTION);
2022-07-04 04:14:37 +00:00
$gqlPayload = [
'query' => $query,
'variables' => [
2022-12-08 03:08:57 +00:00
'functionId' => $function['_id'],
2022-07-04 04:14:37 +00:00
]
];
$function = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
$this->assertIsArray($function['body']['data']);
$this->assertArrayNotHasKey('errors', $function['body']);
$function = $function['body']['data']['functionsGet'];
$this->assertIsArray($function);
return $function;
}
public function testGetRuntimes(): array
{
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::GET_RUNTIMES);
2022-07-04 04:14:37 +00:00
$gqlPayload = [
'query' => $query,
];
$runtimes = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
2025-04-01 14:50:11 +00:00
]), $gqlPayload);
2022-07-04 04:14:37 +00:00
$this->assertIsArray($runtimes['body']['data']);
$this->assertArrayNotHasKey('errors', $runtimes['body']);
$runtimes = $runtimes['body']['data']['functionsListRuntimes'];
$this->assertIsArray($runtimes);
return $runtimes;
}
/**
* @return array
* @throws \Exception
*/
public function testGetDeployments()
2022-07-04 04:14:37 +00:00
{
$function = $this->setupFunction();
2022-07-04 04:14:37 +00:00
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::GET_DEPLOYMENTS);
2022-07-04 04:14:37 +00:00
$gqlPayload = [
'query' => $query,
'variables' => [
2022-12-08 03:08:57 +00:00
'functionId' => $function['_id'],
2022-07-04 04:14:37 +00:00
]
];
$deployments = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
$this->assertIsArray($deployments['body']['data']);
$this->assertArrayNotHasKey('errors', $deployments['body']);
$deployments = $deployments['body']['data']['functionsListDeployments'];
$this->assertIsArray($deployments);
return $deployments;
}
2022-07-07 05:50:49 +00:00
/**
* @return array
* @throws \Exception
*/
public function testGetDeployment()
2022-07-07 05:50:49 +00:00
{
$deployment = $this->setupDeployment();
2022-07-07 05:50:49 +00:00
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::GET_DEPLOYMENT);
2022-07-07 05:50:49 +00:00
$gqlPayload = [
'query' => $query,
'variables' => [
2023-08-29 01:11:57 +00:00
'functionId' => $deployment['resourceId'],
2022-12-08 03:08:57 +00:00
'deploymentId' => $deployment['_id'],
2022-07-07 05:50:49 +00:00
]
];
$deployment = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
$this->assertIsArray($deployment['body']['data']);
$this->assertArrayNotHasKey('errors', $deployment['body']);
$deployment = $deployment['body']['data']['functionsGetDeployment'];
$this->assertIsArray($deployment);
return $deployment;
}
2022-07-04 04:14:37 +00:00
/**
* @return array
* @throws \Exception
*/
public function testGetExecutions(): array
2022-07-04 04:14:37 +00:00
{
$function = $this->setupFunction();
2022-07-04 04:14:37 +00:00
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::GET_EXECUTIONS);
2022-07-04 04:14:37 +00:00
$gqlPayload = [
'query' => $query,
'variables' => [
2022-12-08 03:08:57 +00:00
'functionId' => $function['_id'],
2022-07-04 04:14:37 +00:00
]
];
$executions = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
$this->assertIsArray($executions['body']['data']);
$this->assertArrayNotHasKey('errors', $executions['body']);
$executions = $executions['body']['data']['functionsListExecutions'];
$this->assertIsArray($executions);
return $executions;
}
/**
* @return array
* @throws \Exception
*/
public function testGetExecution(): array
2022-07-04 04:14:37 +00:00
{
$execution = $this->setupExecution();
2022-07-04 04:14:37 +00:00
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::GET_EXECUTION);
2022-07-04 04:14:37 +00:00
$gqlPayload = [
'query' => $query,
'variables' => [
2023-08-29 01:11:57 +00:00
'functionId' => $execution['functionId'],
2022-12-08 03:08:57 +00:00
'executionId' => $execution['_id'],
2022-07-04 04:14:37 +00:00
]
];
$execution = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
$this->assertIsArray($execution['body']['data']);
$this->assertArrayNotHasKey('errors', $execution['body']);
$execution = $execution['body']['data']['functionsGetExecution'];
$this->assertIsArray($execution);
return $execution;
}
/**
* @return array
* @throws \Exception
*/
public function testUpdateFunction(): array
2022-07-04 04:14:37 +00:00
{
$function = $this->setupFunction();
2022-07-04 04:14:37 +00:00
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::UPDATE_FUNCTION);
2022-07-04 04:14:37 +00:00
$gqlPayload = [
'query' => $query,
'variables' => [
2022-12-08 03:08:57 +00:00
'functionId' => $function['_id'],
2022-07-04 04:14:37 +00:00
'name' => 'Test Function Updated',
2022-09-22 02:16:13 +00:00
'execute' => [Role::any()->toString()],
2023-08-20 17:40:44 +00:00
'entrypoint' => 'index.php',
'runtime' => 'php-8.0',
2022-07-04 04:14:37 +00:00
'vars' => [
'name' => 'John Doe',
'age' => 42,
],
]
];
$function = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
$this->assertIsArray($function['body']['data']);
$this->assertArrayNotHasKey('errors', $function['body']);
$function = $function['body']['data']['functionsUpdate'];
$this->assertIsArray($function);
return $function;
}
/**
* @throws \Exception
*/
public function testDeleteDeployment(): void
2022-07-04 04:14:37 +00:00
{
$deployment = $this->setupDeployment();
2022-07-04 04:14:37 +00:00
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::DELETE_DEPLOYMENT);
2022-07-04 04:14:37 +00:00
$gqlPayload = [
'query' => $query,
'variables' => [
2023-08-29 01:11:57 +00:00
'functionId' => $deployment['resourceId'],
2022-12-08 03:08:57 +00:00
'deploymentId' => $deployment['_id'],
2022-07-04 04:14:37 +00:00
]
];
$response = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
2022-07-18 09:38:13 +00:00
$this->assertIsNotArray($response['body']);
2022-07-13 11:21:02 +00:00
$this->assertEquals(204, $response['headers']['status-code']);
2023-08-29 01:11:57 +00:00
// Clear cache after deletion
$key = $this->getProject()['$id'];
2026-03-31 16:34:37 +00:00
self::$cachedDeployment[$key] = [];
self::$cachedExecution[$key] = [];
2022-07-04 04:14:37 +00:00
}
/**
* @throws \Exception
*/
public function testDeleteFunction(): void
2022-07-04 04:14:37 +00:00
{
// Ensure deployment is deleted first
$this->testDeleteDeployment();
$function = $this->setupFunction();
2022-07-04 04:14:37 +00:00
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::DELETE_FUNCTION);
2022-07-04 04:14:37 +00:00
$gqlPayload = [
'query' => $query,
'variables' => [
'functionId' => $function['_id'],
2022-07-04 04:14:37 +00:00
]
];
$response = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
2022-07-18 09:38:13 +00:00
$this->assertIsNotArray($response['body']);
2022-07-13 11:21:02 +00:00
$this->assertEquals(204, $response['headers']['status-code']);
// Clear cache after deletion
$key = $this->getProject()['$id'];
2026-03-31 16:34:37 +00:00
self::$cachedFunction[$key] = [];
2022-07-04 04:14:37 +00:00
}
2022-07-06 03:51:37 +00:00
}