appwrite/tests/e2e/Services/Functions/FunctionsBase.php

209 lines
8.1 KiB
PHP
Raw Normal View History

2020-05-05 20:37:59 +00:00
<?php
namespace Tests\E2E\Services\Functions;
2024-09-22 19:14:58 +00:00
use Appwrite\Tests\Async;
2024-09-19 11:35:52 +00:00
use CURLFile;
2020-05-05 20:37:59 +00:00
use Tests\E2E\Client;
2022-02-19 13:57:48 +00:00
use Utopia\CLI\Console;
2020-05-05 20:37:59 +00:00
trait FunctionsBase
{
2024-09-22 19:14:58 +00:00
use Async;
2024-10-08 07:54:40 +00:00
protected string $stdout = '';
protected string $stderr = '';
2022-02-19 13:57:48 +00:00
2024-09-19 11:35:52 +00:00
protected function setupFunction(mixed $params): string
2022-05-23 14:54:50 +00:00
{
2024-09-19 11:35:52 +00:00
$function = $this->client->call(Client::METHOD_POST, '/functions', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), $params);
2024-09-19 19:21:00 +00:00
$this->assertEquals($function['headers']['status-code'], 201, 'Setup function failed with status code: ' . $function['headers']['status-code'] . ' and response: ' . json_encode($function['body'], JSON_PRETTY_PRINT));
2024-09-19 11:35:52 +00:00
2024-09-19 13:17:38 +00:00
$functionId = $function['body']['$id'];
2024-09-19 11:35:52 +00:00
return $functionId;
2022-02-19 13:57:48 +00:00
}
2020-05-05 20:37:59 +00:00
2024-09-19 11:35:52 +00:00
protected function setupDeployment(string $functionId, mixed $params): string
{
2024-09-19 11:35:52 +00:00
$deployment = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/deployments', array_merge([
2024-09-19 13:17:38 +00:00
'content-type' => 'multipart/form-data',
2024-09-19 11:35:52 +00:00
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), $params);
2024-09-19 19:21:00 +00:00
$this->assertEquals($deployment['headers']['status-code'], 202, 'Setup deployment failed with status code: ' . $deployment['headers']['status-code'] . ' and response: ' . json_encode($deployment['body'], JSON_PRETTY_PRINT));
$deploymentId = $deployment['body']['$id'] ?? '';
2024-09-19 11:35:52 +00:00
$this->assertEventually(function () use ($functionId, $deploymentId) {
2024-09-19 19:21:00 +00:00
$deployment = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId . '/deployments/' . $deploymentId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
2024-09-19 19:21:00 +00:00
]));
2024-09-20 08:43:41 +00:00
$this->assertEquals('ready', $deployment['body']['status'], 'Deployment status is not ready, deployment: ' . json_encode($deployment['body'], JSON_PRETTY_PRINT));
2024-09-19 13:17:38 +00:00
}, 50000, 500);
2024-09-19 11:35:52 +00:00
return $deploymentId;
}
2024-09-19 11:35:52 +00:00
protected function cleanupFunction(string $functionId): void
{
$function = $this->client->call(Client::METHOD_DELETE, '/functions/' . $functionId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]));
$this->assertEquals($function['headers']['status-code'], 204);
}
2024-09-19 13:17:38 +00:00
protected function createFunction(mixed $params): mixed
2024-09-19 11:35:52 +00:00
{
$function = $this->client->call(Client::METHOD_POST, '/functions', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), $params);
return $function;
}
2024-09-19 13:17:38 +00:00
protected function createVariable(string $functionId, mixed $params): mixed
2024-09-19 11:35:52 +00:00
{
$variable = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/variables', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
2024-09-19 13:17:38 +00:00
], $this->getHeaders()), $params);
2024-09-19 11:35:52 +00:00
return $variable;
}
2024-09-19 13:17:38 +00:00
protected function getFunction(string $functionId): mixed
2024-09-19 11:35:52 +00:00
{
$function = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
return $function;
}
2024-09-19 13:17:38 +00:00
protected function getDeployment(string $functionId, string $deploymentId): mixed
2024-09-19 11:35:52 +00:00
{
$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()));
return $deployment;
}
2024-09-20 08:15:05 +00:00
protected function getExecution(string $functionId, $executionId): mixed
2024-09-19 11:35:52 +00:00
{
$execution = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId . '/executions/' . $executionId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
return $execution;
}
2024-09-20 08:15:05 +00:00
protected function listFunctions(mixed $params = []): mixed
2024-09-19 13:17:38 +00:00
{
$functions = $this->client->call(Client::METHOD_GET, '/functions', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), $params);
2024-09-19 13:17:38 +00:00
return $functions;
}
2024-09-20 08:15:05 +00:00
protected function listDeployments(string $functionId, $params = []): mixed
2024-09-19 11:35:52 +00:00
{
$deployments = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId . '/deployments', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
2024-09-19 13:17:38 +00:00
], $this->getHeaders()), $params);
2024-09-19 11:35:52 +00:00
return $deployments;
}
2024-09-20 08:15:05 +00:00
protected function listExecutions(string $functionId, mixed $params = []): mixed
2024-09-19 11:35:52 +00:00
{
$executions = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId . '/executions', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
2024-09-19 13:17:38 +00:00
], $this->getHeaders()), $params);
2024-09-19 11:35:52 +00:00
return $executions;
2022-02-19 13:57:48 +00:00
}
2020-05-05 20:37:59 +00:00
2024-09-25 11:32:09 +00:00
protected function packageFunction(string $function): CURLFile
{
2024-09-19 19:58:09 +00:00
$folderPath = realpath(__DIR__ . '/../../../resources/functions') . "/$function";
$tarPath = "$folderPath/code.tar.gz";
2024-10-08 07:54:40 +00:00
Console::execute("cd $folderPath && tar --exclude code.tar.gz -czf code.tar.gz .", '', $this->stdout, $this->stderr);
2024-09-19 19:58:09 +00:00
if (filesize($tarPath) > 1024 * 1024 * 5) {
2024-09-19 11:35:52 +00:00
throw new \Exception('Code package is too large. Use the chunked upload method instead.');
}
2024-09-19 11:35:52 +00:00
2024-09-19 19:58:09 +00:00
return new CURLFile($tarPath, 'application/x-gzip', \basename($tarPath));
2024-09-19 11:35:52 +00:00
}
2024-09-20 08:15:05 +00:00
protected function createDeployment(string $functionId, mixed $params = []): mixed
{
2024-09-19 13:17:38 +00:00
$deployment = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/deployments', array_merge([
2024-09-19 11:35:52 +00:00
'content-type' => 'multipart/form-data',
'x-appwrite-project' => $this->getProject()['$id'],
2024-09-19 13:17:38 +00:00
], $this->getHeaders()), $params);
2024-09-19 11:35:52 +00:00
return $deployment;
}
2024-09-19 13:17:38 +00:00
protected function getFunctionUsage(string $functionId, mixed $params): mixed
2024-09-19 11:35:52 +00:00
{
$usage = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId . '/usage', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
2024-09-19 13:17:38 +00:00
], $this->getHeaders()), $params);
2024-09-19 11:35:52 +00:00
return $usage;
}
protected function getTemplate(string $templateId)
{
$template = $this->client->call(Client::METHOD_GET, '/functions/templates/' . $templateId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
return $template;
}
2024-09-20 08:15:05 +00:00
protected function createExecution(string $functionId, mixed $params = []): mixed
2024-09-19 11:35:52 +00:00
{
$execution = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/executions', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), $params);
return $execution;
}
2024-09-20 08:15:05 +00:00
protected function deleteFunction(string $functionId): mixed
2024-09-19 11:35:52 +00:00
{
2024-09-25 11:32:09 +00:00
$function = $this->client->call(Client::METHOD_DELETE, '/functions/' . $functionId, array_merge([
2024-09-19 11:35:52 +00:00
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
2024-09-25 11:32:09 +00:00
], $this->getHeaders()));
2024-09-19 11:35:52 +00:00
return $function;
}
2022-05-23 14:54:50 +00:00
}