1
0
Fork 0
mirror of synced 2024-06-27 02:31:04 +12:00
appwrite/tests/e2e/Services/Functions/FunctionsCustomServerTest.php

136 lines
5.1 KiB
PHP
Raw Normal View History

2020-05-06 08:37:59 +12:00
<?php
namespace Tests\E2E\Services\Functions;
2020-07-15 15:55:37 +12:00
use CURLFile;
2020-05-06 08:37:59 +12:00
use Tests\E2E\Client;
use Tests\E2E\Scopes\ProjectCustom;
use Tests\E2E\Scopes\Scope;
use Tests\E2E\Scopes\SideServer;
2020-12-10 10:26:13 +13:00
use Utopia\CLI\Console;
2020-05-06 08:37:59 +12:00
2020-12-09 11:24:22 +13:00
class FunctionsCustomServerTest extends Scope
2020-05-06 08:37:59 +12:00
{
use FunctionsBase;
use ProjectCustom;
use SideServer;
2020-12-09 11:24:22 +13:00
public function testENVS():array
{
2020-12-10 21:45:32 +13:00
sleep(60);
2020-12-10 21:32:12 +13:00
2020-12-10 10:26:13 +13:00
$functions = realpath(__DIR__ . '/../../../resources/functions');
/**
* Command for rebuilding code packages:
* bash tests/resources/functions/package.sh
*/
2020-12-09 11:24:22 +13:00
$envs = [
2020-12-10 10:26:13 +13:00
//[
// 'name' => 'php-7.4',
// 'code' => $functions.'/php-fx.tar.gz',
// 'command' => 'php function.php',
//],
2020-12-09 11:24:22 +13:00
[
2020-12-10 10:26:13 +13:00
'language' => 'Python',
'version' => '3.8',
2020-12-09 11:24:22 +13:00
'name' => 'python-3.8',
2020-12-10 10:26:13 +13:00
'code' => $functions.'/python.tar.gz',
2020-12-09 11:24:22 +13:00
'command' => 'python main.py',
],
];
foreach ($envs as $key => $env) {
2020-12-10 10:26:13 +13:00
$language = $env['language'] ?? '';
$version = $env['version'] ?? '';
2020-12-09 11:24:22 +13:00
$name = $env['name'] ?? '';
$code = $env['code'] ?? '';
$command = $env['command'] ?? '';
$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' => [
'APPWRITE_ENDPOINT' => 'http://'.gethostbyname(trim(`hostname`)).'/v1',
'APPWRITE_PROJECT' => $this->getProject()['$id'],
'APPWRITE_SECRET' => $this->getProject()['apiKey'],
],
'events' => [],
'schedule' => '',
'timeout' => 10,
]);
$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()));
2020-12-10 20:38:20 +13:00
2020-12-10 23:03:13 +13:00
var_dump($executions['body']['executions'][0]);
2020-12-10 20:38:20 +13:00
var_dump($executions['body']['executions'][0]['stdout']);
var_dump($executions['body']['executions'][0]['stderr']);
2020-12-09 11:24:22 +13:00
$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'], 'completed');
$this->assertEquals($executions['body']['executions'][0]['exitCode'], 0);
2020-12-10 10:26:13 +13:00
$stdout = explode("\n", $executions['body']['executions'][0]['stdout']);
$this->assertEquals($stdout[0], $functionId);
$this->assertEquals($stdout[1], 'Test '.$name);
$this->assertEquals($stdout[2], $tagId);
$this->assertEquals($stdout[3], 'http');
$this->assertEquals($stdout[4], $language);
$this->assertEquals($stdout[5], $version);
2020-12-09 11:24:22 +13:00
}
return [
'functionId' => $functionId,
];
}
2020-05-06 08:37:59 +12:00
}