1
0
Fork 0
mirror of synced 2024-06-02 10:54:44 +12:00
appwrite/tests/e2e/Services/Functions/FunctionsCustomServerTest.php

546 lines
20 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-09 11:24:22 +13:00
class FunctionsCustomServerTest extends Scope
2020-05-06 08:37:59 +12:00
{
use FunctionsBase;
use ProjectCustom;
use SideServer;
public function testCreate():array
{
/**
* Test for SUCCESS
*/
$response1 = $this->client->call(Client::METHOD_POST, '/functions', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'name' => 'Test',
2020-08-01 16:29:18 +12:00
'env' => 'php-7.4',
2020-05-06 08:37:59 +12:00
'vars' => [
2020-08-10 03:03:52 +12:00
'funcKey1' => 'funcValue1',
'funcKey2' => 'funcValue2',
'funcKey3' => 'funcValue3',
2020-05-06 08:37:59 +12:00
],
'events' => [
'account.create',
'account.delete',
],
'schedule' => '* * * * *',
'timeout' => 10,
]);
2020-10-15 10:11:12 +13:00
$functionId = $response1['body']['$id'] ?? '';
2020-05-06 17:54:46 +12:00
2020-05-06 08:37:59 +12:00
$this->assertEquals(201, $response1['headers']['status-code']);
$this->assertNotEmpty($response1['body']['$id']);
$this->assertEquals('Test', $response1['body']['name']);
2020-08-01 16:29:18 +12:00
$this->assertEquals('php-7.4', $response1['body']['env']);
2020-05-06 08:37:59 +12:00
$this->assertIsInt($response1['body']['dateCreated']);
$this->assertIsInt($response1['body']['dateUpdated']);
2020-05-11 16:39:00 +12:00
$this->assertEquals('', $response1['body']['tag']);
2020-05-13 10:07:00 +12:00
$this->assertEquals([
2020-08-10 03:03:52 +12:00
'funcKey1' => 'funcValue1',
'funcKey2' => 'funcValue2',
'funcKey3' => 'funcValue3',
2020-05-13 10:07:00 +12:00
], $response1['body']['vars']);
2020-05-06 08:37:59 +12:00
$this->assertEquals([
'account.create',
'account.delete',
], $response1['body']['events']);
$this->assertEquals('* * * * *', $response1['body']['schedule']);
$this->assertEquals(10, $response1['body']['timeout']);
/**
* Test for FAILURE
*/
2020-05-06 17:54:46 +12:00
return [
'functionId' => $functionId,
];
}
/**
* @depends testCreate
*/
public function testList(array $data):array
{
/**
* Test for SUCCESS
*/
$function = $this->client->call(Client::METHOD_GET, '/functions', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
$this->assertEquals($function['headers']['status-code'], 200);
$this->assertEquals($function['body']['sum'], 1);
$this->assertIsArray($function['body']['functions']);
$this->assertCount(1, $function['body']['functions']);
$this->assertEquals($function['body']['functions'][0]['name'], 'Test');
return $data;
}
/**
2020-05-06 18:14:34 +12:00
* @depends testList
2020-05-06 17:54:46 +12:00
*/
public function testGet(array $data):array
{
/**
* Test for SUCCESS
*/
$function = $this->client->call(Client::METHOD_GET, '/functions/' . $data['functionId'], array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
$this->assertEquals($function['headers']['status-code'], 200);
$this->assertEquals($function['body']['name'], 'Test');
/**
* Test for FAILURE
*/
$function = $this->client->call(Client::METHOD_GET, '/functions/x', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
$this->assertEquals($function['headers']['status-code'], 404);
return $data;
2020-05-06 08:37:59 +12:00
}
2020-05-06 18:14:34 +12:00
/**
* @depends testGet
*/
public function testUpdate($data):array
{
/**
* Test for SUCCESS
*/
$response1 = $this->client->call(Client::METHOD_PUT, '/functions/'.$data['functionId'], array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'name' => 'Test1',
'vars' => [
'key4' => 'value4',
'key5' => 'value5',
'key6' => 'value6',
],
'events' => [
'account.update.name',
'account.update.email',
],
'schedule' => '* * * * 1',
'timeout' => 5,
]);
$this->assertEquals(200, $response1['headers']['status-code']);
$this->assertNotEmpty($response1['body']['$id']);
$this->assertEquals('Test1', $response1['body']['name']);
$this->assertIsInt($response1['body']['dateCreated']);
$this->assertIsInt($response1['body']['dateUpdated']);
2020-05-11 16:39:00 +12:00
$this->assertEquals('', $response1['body']['tag']);
2020-05-13 10:07:00 +12:00
$this->assertEquals([
'key4' => 'value4',
'key5' => 'value5',
'key6' => 'value6',
], $response1['body']['vars']);
2020-05-06 18:14:34 +12:00
$this->assertEquals([
'account.update.name',
'account.update.email',
], $response1['body']['events']);
$this->assertEquals('* * * * 1', $response1['body']['schedule']);
$this->assertEquals(5, $response1['body']['timeout']);
/**
* Test for FAILURE
*/
return $data;
}
2020-05-07 00:12:52 +12:00
/**
* @depends testUpdate
*/
2020-05-07 05:35:56 +12:00
public function testCreateTag($data):array
{
/**
* Test for SUCCESS
*/
$tag = $this->client->call(Client::METHOD_POST, '/functions/'.$data['functionId'].'/tags', array_merge([
2020-07-15 16:18:39 +12:00
'content-type' => 'multipart/form-data',
2020-05-07 05:35:56 +12:00
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
2020-07-18 20:13:38 +12:00
'command' => 'php function.php',
2020-07-18 19:55:19 +12:00
'code' => new CURLFile(realpath(__DIR__ . '/../../../resources/functions/php-fx.tar.gz'), 'application/x-gzip', 'php-fx.tar.gz'),
2020-05-07 05:35:56 +12:00
]);
2020-10-15 10:11:12 +13:00
$tagId = $tag['body']['$id'] ?? '';
2020-05-07 05:35:56 +12:00
$this->assertEquals(201, $tag['headers']['status-code']);
$this->assertNotEmpty($tag['body']['$id']);
$this->assertIsInt($tag['body']['dateCreated']);
2020-07-18 20:13:15 +12:00
$this->assertEquals('php function.php', $tag['body']['command']);
2020-08-08 22:41:17 +12:00
$this->assertEquals(751, $tag['body']['size']);
2020-05-07 05:35:56 +12:00
/**
* Test for FAILURE
*/
return array_merge($data, ['tagId' => $tagId]);
}
/**
* @depends testCreateTag
*/
2020-05-11 16:39:00 +12:00
public function testUpdateTag($data):array
2020-05-07 05:35:56 +12:00
{
/**
* Test for SUCCESS
*/
2020-05-11 16:39:00 +12:00
$response = $this->client->call(Client::METHOD_PATCH, '/functions/'.$data['functionId'].'/tag', array_merge([
2020-05-07 05:35:56 +12:00
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
2020-05-11 16:39:00 +12:00
'tag' => $data['tagId'],
2020-05-07 05:35:56 +12:00
]);
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertNotEmpty($response['body']['$id']);
$this->assertIsInt($response['body']['dateCreated']);
$this->assertIsInt($response['body']['dateUpdated']);
2020-05-11 16:39:00 +12:00
$this->assertEquals($data['tagId'], $response['body']['tag']);
2020-05-07 05:35:56 +12:00
/**
* Test for FAILURE
*/
return $data;
}
/**
* @depends testCreateTag
*/
public function testListTags(array $data):array
{
/**
* Test for SUCCESS
*/
$function = $this->client->call(Client::METHOD_GET, '/functions/'.$data['functionId'].'/tags', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
$this->assertEquals($function['headers']['status-code'], 200);
$this->assertEquals($function['body']['sum'], 1);
$this->assertIsArray($function['body']['tags']);
$this->assertCount(1, $function['body']['tags']);
return $data;
}
/**
* @depends testCreateTag
*/
public function testGetTag(array $data):array
{
/**
* Test for SUCCESS
*/
$function = $this->client->call(Client::METHOD_GET, '/functions/'.$data['functionId'].'/tags/' . $data['tagId'], array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
2020-07-15 16:18:39 +12:00
$this->assertEquals(200, $function['headers']['status-code']);
2020-08-08 22:41:17 +12:00
$this->assertEquals(751, $function['body']['size']);
2020-05-07 05:35:56 +12:00
/**
* Test for FAILURE
*/
$function = $this->client->call(Client::METHOD_GET, '/functions/'.$data['functionId'].'/tags/x', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
$this->assertEquals($function['headers']['status-code'], 404);
return $data;
}
/**
2020-05-11 16:39:00 +12:00
* @depends testUpdateTag
2020-05-07 05:35:56 +12:00
*/
public function testCreateExecution($data):array
{
/**
* Test for SUCCESS
*/
$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' => 1,
]);
2020-10-15 10:11:12 +13:00
$executionId = $execution['body']['$id'] ?? '';
2020-05-07 05:35:56 +12:00
$this->assertEquals(201, $execution['headers']['status-code']);
$this->assertNotEmpty($execution['body']['$id']);
$this->assertNotEmpty($execution['body']['functionId']);
$this->assertIsInt($execution['body']['dateCreated']);
$this->assertEquals($data['functionId'], $execution['body']['functionId']);
$this->assertEquals('waiting', $execution['body']['status']);
$this->assertEquals(0, $execution['body']['exitCode']);
$this->assertEquals('', $execution['body']['stdout']);
$this->assertEquals('', $execution['body']['stderr']);
$this->assertEquals(0, $execution['body']['time']);
2020-07-23 19:36:59 +12:00
2020-08-06 18:59:33 +12:00
// sleep(75);
// $execution = $this->client->call(Client::METHOD_GET, '/functions/'.$data['functionId'].'/executions/'.$executionId, array_merge([
// 'content-type' => 'application/json',
// 'x-appwrite-project' => $this->getProject()['$id'],
// ], $this->getHeaders()));
// $this->assertNotEmpty($execution['body']['$id']);
// $this->assertNotEmpty($execution['body']['functionId']);
// $this->assertIsInt($execution['body']['dateCreated']);
// $this->assertEquals($data['functionId'], $execution['body']['functionId']);
// $this->assertEquals('completed', $execution['body']['status']);
// $this->assertEquals(0, $execution['body']['exitCode']);
// $this->assertStringContainsString('APPWRITE_FUNCTION_ID', $execution['body']['stdout']);
// $this->assertStringContainsString('APPWRITE_FUNCTION_NAME', $execution['body']['stdout']);
// $this->assertStringContainsString('APPWRITE_FUNCTION_TAG', $execution['body']['stdout']);
// $this->assertStringContainsString('APPWRITE_FUNCTION_TRIGGER', $execution['body']['stdout']);
// $this->assertStringContainsString('APPWRITE_FUNCTION_ENV_NAME', $execution['body']['stdout']);
// $this->assertStringContainsString('APPWRITE_FUNCTION_ENV_VERSION', $execution['body']['stdout']);
// $this->assertStringContainsString('Hello World', $execution['body']['stdout']);
// $this->assertStringContainsString($execution['body']['functionId'], $execution['body']['stdout']);
// $this->assertEquals('', $execution['body']['stderr']);
// $this->assertGreaterThan(0.100, $execution['body']['time']);
// $this->assertLessThan(0.500, $execution['body']['time']);
2020-07-23 19:36:59 +12:00
2020-05-07 05:35:56 +12:00
/**
* Test for FAILURE
*/
return array_merge($data, ['executionId' => $executionId]);
}
/**
* @depends testCreateExecution
*/
public function testListExecutions(array $data):array
{
/**
* Test for SUCCESS
*/
$function = $this->client->call(Client::METHOD_GET, '/functions/'.$data['functionId'].'/executions', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
$this->assertEquals($function['headers']['status-code'], 200);
$this->assertEquals($function['body']['sum'], 1);
$this->assertIsArray($function['body']['executions']);
$this->assertCount(1, $function['body']['executions']);
$this->assertEquals($function['body']['executions'][0]['$id'], $data['executionId']);
return $data;
}
/**
* @depends testListExecutions
*/
public function testGetExecution(array $data):array
{
/**
* Test for SUCCESS
*/
$function = $this->client->call(Client::METHOD_GET, '/functions/'.$data['functionId'].'/executions/' . $data['executionId'], array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
$this->assertEquals($function['headers']['status-code'], 200);
$this->assertEquals($function['body']['$id'], $data['executionId']);
/**
* Test for FAILURE
*/
$function = $this->client->call(Client::METHOD_GET, '/functions/'.$data['functionId'].'/executions/x', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
$this->assertEquals($function['headers']['status-code'], 404);
return $data;
}
/**
* @depends testGetExecution
*/
public function testDeleteTag($data):array
{
/**
* Test for SUCCESS
*/
$function = $this->client->call(Client::METHOD_DELETE, '/functions/'.$data['functionId'].'/tags/' . $data['tagId'], array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
$this->assertEquals(204, $function['headers']['status-code']);
$this->assertEmpty($function['body']);
$function = $this->client->call(Client::METHOD_GET, '/functions/'.$data['functionId'].'/tags/' . $data['tagId'], array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
$this->assertEquals(404, $function['headers']['status-code']);
/**
* Test for FAILURE
*/
return $data;
}
/**
* @depends testCreateTag
*/
2020-05-07 00:12:52 +12:00
public function testDelete($data):array
{
/**
* Test for SUCCESS
*/
$function = $this->client->call(Client::METHOD_DELETE, '/functions/'.$data['functionId'], array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
$this->assertEquals(204, $function['headers']['status-code']);
$this->assertEmpty($function['body']);
$function = $this->client->call(Client::METHOD_GET, '/functions/' . $data['functionId'], array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
$this->assertEquals(404, $function['headers']['status-code']);
/**
* Test for FAILURE
*/
return $data;
}
2020-12-09 11:24:22 +13:00
public function testENVS():array
{
$envs = [
[
'name' => 'php-7.4',
'code' => realpath(__DIR__ . '/../../../resources/functions/php-fx.tar.gz'),
'command' => 'php function.php',
],
[
'name' => 'python-3.8',
'code' => realpath(__DIR__ . '/../../../resources/functions/python/code.tar.gz'),
'command' => 'python main.py',
],
];
foreach ($envs as $key => $env) {
$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()));
var_dump($executions['body']['executions'][0]);
$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);
}
return [
'functionId' => $functionId,
];
}
2020-05-06 08:37:59 +12:00
}