2020-05-05 20:37:59 +00:00
< ? php
namespace Tests\E2E\Services\Functions ;
2022-08-30 11:10:15 +00:00
use Appwrite\Tests\Retry ;
2020-07-15 03:55:37 +00:00
use CURLFile ;
2020-05-05 20:37:59 +00:00
use Tests\E2E\Client ;
use Tests\E2E\Scopes\ProjectCustom ;
use Tests\E2E\Scopes\Scope ;
use Tests\E2E\Scopes\SideServer ;
2024-08-05 03:21:10 +00:00
use Utopia\App ;
2023-12-20 10:55:09 +00:00
use Utopia\Database\Document ;
2022-12-14 15:42:25 +00:00
use Utopia\Database\Helpers\ID ;
2023-12-20 10:55:09 +00:00
use Utopia\Database\Query ;
2023-03-01 12:00:54 +00:00
use Utopia\Database\Validator\Datetime as DatetimeValidator ;
2020-05-05 20:37:59 +00:00
2020-12-08 22:24:22 +00:00
class FunctionsCustomServerTest extends Scope
2020-05-05 20:37:59 +00:00
{
use FunctionsBase ;
use ProjectCustom ;
use SideServer ;
2022-04-13 12:39:31 +00:00
public function testCreate () : array
2020-12-10 17:47:32 +00:00
{
/**
* 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 ()), [
2022-08-14 10:33:36 +00:00
'functionId' => ID :: unique (),
2020-12-10 17:47:32 +00:00
'name' => 'Test' ,
2021-06-21 14:42:39 +00:00
'runtime' => 'php-8.0' ,
2023-08-11 13:34:57 +00:00
'entrypoint' => 'index.php' ,
2020-12-10 17:47:32 +00:00
'events' => [
2023-09-25 07:55:55 +00:00
'buckets.*.create' ,
'buckets.*.delete' ,
2020-12-10 17:47:32 +00:00
],
2021-04-23 15:05:17 +00:00
'schedule' => '0 0 1 1 *' ,
2020-12-10 17:47:32 +00:00
'timeout' => 10 ,
]);
$functionId = $response1 [ 'body' ][ '$id' ] ? ? '' ;
$this -> assertEquals ( 201 , $response1 [ 'headers' ][ 'status-code' ]);
$this -> assertNotEmpty ( $response1 [ 'body' ][ '$id' ]);
$this -> assertEquals ( 'Test' , $response1 [ 'body' ][ 'name' ]);
2021-06-22 15:56:05 +00:00
$this -> assertEquals ( 'php-8.0' , $response1 [ 'body' ][ 'runtime' ]);
2022-12-19 11:21:09 +00:00
$dateValidator = new DatetimeValidator ();
$this -> assertEquals ( true , $dateValidator -> isValid ( $response1 [ 'body' ][ '$createdAt' ]));
$this -> assertEquals ( true , $dateValidator -> isValid ( $response1 [ 'body' ][ '$updatedAt' ]));
2022-01-24 23:46:13 +00:00
$this -> assertEquals ( '' , $response1 [ 'body' ][ 'deployment' ]);
2020-12-10 17:47:32 +00:00
$this -> assertEquals ([
2023-09-25 07:55:55 +00:00
'buckets.*.create' ,
'buckets.*.delete' ,
2020-12-10 17:47:32 +00:00
], $response1 [ 'body' ][ 'events' ]);
2021-04-23 15:05:17 +00:00
$this -> assertEquals ( '0 0 1 1 *' , $response1 [ 'body' ][ 'schedule' ]);
2020-12-10 17:47:32 +00:00
$this -> assertEquals ( 10 , $response1 [ 'body' ][ 'timeout' ]);
2022-04-13 12:39:31 +00:00
2022-08-03 13:32:50 +00:00
/** Create Variables */
2022-08-09 15:29:24 +00:00
$variable = $this -> client -> call ( Client :: METHOD_POST , '/functions/' . $functionId . '/variables' , array_merge ([
2022-08-03 13:32:50 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'key' => 'funcKey1' ,
'value' => 'funcValue1' ,
]);
2022-08-09 15:29:24 +00:00
$variable2 = $this -> client -> call ( Client :: METHOD_POST , '/functions/' . $functionId . '/variables' , array_merge ([
2022-08-03 13:32:50 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'key' => 'funcKey2' ,
'value' => 'funcValue2' ,
]);
2022-08-09 15:29:24 +00:00
$variable3 = $this -> client -> call ( Client :: METHOD_POST , '/functions/' . $functionId . '/variables' , array_merge ([
2022-08-03 13:32:50 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'key' => 'funcKey3' ,
'value' => 'funcValue3' ,
]);
2022-08-03 13:35:11 +00:00
2022-08-03 13:32:50 +00:00
$this -> assertEquals ( 201 , $variable [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 201 , $variable2 [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 201 , $variable3 [ 'headers' ][ 'status-code' ]);
2020-12-10 17:47:32 +00:00
/**
* Test for FAILURE
*/
return [
'functionId' => $functionId ,
];
}
/**
* @ depends testCreate
*/
2022-04-13 12:39:31 +00:00
public function testList ( array $data ) : array
2020-12-10 17:47:32 +00:00
{
/**
* Test for SUCCESS
*/
2021-08-09 12:33:16 +00:00
2021-09-23 07:01:10 +00:00
/**
* Test search queries
*/
2021-09-21 08:22:13 +00:00
$response = $this -> client -> call ( Client :: METHOD_GET , '/functions' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'search' => $data [ 'functionId' ]
]);
$this -> assertEquals ( $response [ 'headers' ][ 'status-code' ], 200 );
$this -> assertCount ( 1 , $response [ 'body' ][ 'functions' ]);
$this -> assertEquals ( $response [ 'body' ][ 'functions' ][ 0 ][ 'name' ], 'Test' );
2022-08-24 11:55:43 +00:00
$response = $this -> client -> call ( Client :: METHOD_GET , '/functions' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-12-20 10:55:09 +00:00
'queries' => [
Query :: limit ( 1 ) -> toString (),
],
2022-08-24 11:55:43 +00:00
]);
$this -> assertEquals ( $response [ 'headers' ][ 'status-code' ], 200 );
2023-05-03 11:53:40 +00:00
$this -> assertCount ( 1 , $response [ 'body' ][ 'functions' ]);
2022-08-24 11:55:43 +00:00
$response = $this -> client -> call ( Client :: METHOD_GET , '/functions' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-12-20 10:55:09 +00:00
'queries' => [
Query :: offset ( 1 ) -> toString (),
],
2022-08-24 11:55:43 +00:00
]);
$this -> assertEquals ( $response [ 'headers' ][ 'status-code' ], 200 );
$this -> assertCount ( 0 , $response [ 'body' ][ 'functions' ]);
$response = $this -> client -> call ( Client :: METHOD_GET , '/functions' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-12-20 10:55:09 +00:00
'queries' => [
Query :: equal ( 'enabled' , [ true ]) -> toString (),
],
2022-08-24 11:55:43 +00:00
]);
$this -> assertEquals ( $response [ 'headers' ][ 'status-code' ], 200 );
$this -> assertCount ( 1 , $response [ 'body' ][ 'functions' ]);
$response = $this -> client -> call ( Client :: METHOD_GET , '/functions' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-12-20 10:55:09 +00:00
'queries' => [
Query :: equal ( 'enabled' , [ false ]) -> toString (),
],
2022-08-24 11:55:43 +00:00
]);
$this -> assertEquals ( $response [ 'headers' ][ 'status-code' ], 200 );
$this -> assertCount ( 0 , $response [ 'body' ][ 'functions' ]);
2021-09-21 08:22:13 +00:00
$response = $this -> client -> call ( Client :: METHOD_GET , '/functions' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'search' => 'Test'
]);
$this -> assertEquals ( $response [ 'headers' ][ 'status-code' ], 200 );
$this -> assertCount ( 1 , $response [ 'body' ][ 'functions' ]);
$this -> assertEquals ( $response [ 'body' ][ 'functions' ][ 0 ][ '$id' ], $data [ 'functionId' ]);
$response = $this -> client -> call ( Client :: METHOD_GET , '/functions' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'search' => 'php-8.0'
]);
$this -> assertEquals ( $response [ 'headers' ][ 'status-code' ], 200 );
$this -> assertCount ( 1 , $response [ 'body' ][ 'functions' ]);
$this -> assertEquals ( $response [ 'body' ][ 'functions' ][ 0 ][ '$id' ], $data [ 'functionId' ]);
2021-09-23 07:01:10 +00:00
/**
* Test pagination
*/
2021-08-09 12:33:16 +00:00
$response = $this -> client -> call ( Client :: METHOD_POST , '/functions' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2022-08-14 10:33:36 +00:00
'functionId' => ID :: unique (),
2021-08-09 12:33:16 +00:00
'name' => 'Test 2' ,
'runtime' => 'php-8.0' ,
2023-08-11 13:34:57 +00:00
'entrypoint' => 'index.php' ,
2021-08-09 12:33:16 +00:00
'events' => [
2023-09-25 07:55:55 +00:00
'buckets.*.create' ,
'buckets.*.delete' ,
2021-08-09 12:33:16 +00:00
],
'schedule' => '0 0 1 1 *' ,
'timeout' => 10 ,
]);
$this -> assertNotEmpty ( $response [ 'body' ][ '$id' ]);
2022-08-03 13:32:50 +00:00
/** Create Variables */
2022-08-09 15:29:24 +00:00
$variable = $this -> client -> call ( Client :: METHOD_POST , '/functions/' . $response [ 'body' ][ '$id' ] . '/variables' , array_merge ([
2022-08-03 13:32:50 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'key' => 'funcKey1' ,
'value' => 'funcValue1' ,
]);
2022-08-03 13:35:11 +00:00
2022-08-09 15:29:24 +00:00
$variable2 = $this -> client -> call ( Client :: METHOD_POST , '/functions/' . $response [ 'body' ][ '$id' ] . '/variables' , array_merge ([
2022-08-03 13:32:50 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'key' => 'funcKey2' ,
'value' => 'funcValue2' ,
]);
2022-08-03 13:35:11 +00:00
2022-08-09 15:29:24 +00:00
$variable3 = $this -> client -> call ( Client :: METHOD_POST , '/functions/' . $response [ 'body' ][ '$id' ] . '/variables' , array_merge ([
2022-08-03 13:32:50 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'key' => 'funcKey3' ,
'value' => 'funcValue3' ,
]);
2022-08-03 13:35:11 +00:00
2022-08-03 13:32:50 +00:00
$this -> assertEquals ( 201 , $variable [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 201 , $variable2 [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 201 , $variable3 [ 'headers' ][ 'status-code' ]);
2021-08-09 12:33:16 +00:00
$functions = $this -> client -> call ( Client :: METHOD_GET , '/functions' , array_merge ([
2020-12-10 17:47:32 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
2021-08-09 12:33:16 +00:00
$this -> assertEquals ( $functions [ 'headers' ][ 'status-code' ], 200 );
2022-02-27 09:57:09 +00:00
$this -> assertEquals ( $functions [ 'body' ][ 'total' ], 2 );
2021-08-09 12:33:16 +00:00
$this -> assertIsArray ( $functions [ 'body' ][ 'functions' ]);
$this -> assertCount ( 2 , $functions [ 'body' ][ 'functions' ]);
$this -> assertEquals ( $functions [ 'body' ][ 'functions' ][ 0 ][ 'name' ], 'Test' );
$this -> assertEquals ( $functions [ 'body' ][ 'functions' ][ 1 ][ 'name' ], 'Test 2' );
$response = $this -> client -> call ( Client :: METHOD_GET , '/functions' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-12-20 10:55:09 +00:00
'queries' => [
Query :: cursorAfter ( new Document ([ '$id' => $functions [ 'body' ][ 'functions' ][ 0 ][ '$id' ]])) -> toString (),
],
2021-08-09 12:33:16 +00:00
]);
$this -> assertEquals ( $response [ 'headers' ][ 'status-code' ], 200 );
$this -> assertCount ( 1 , $response [ 'body' ][ 'functions' ]);
$this -> assertEquals ( $response [ 'body' ][ 'functions' ][ 0 ][ 'name' ], 'Test 2' );
2021-10-05 10:30:33 +00:00
$response = $this -> client -> call ( Client :: METHOD_GET , '/functions' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-12-20 10:55:09 +00:00
'queries' => [
Query :: cursorBefore ( new Document ([ '$id' => $functions [ 'body' ][ 'functions' ][ 1 ][ '$id' ]])) -> toString (),
],
2021-10-05 10:30:33 +00:00
]);
$this -> assertEquals ( $response [ 'headers' ][ 'status-code' ], 200 );
$this -> assertCount ( 1 , $response [ 'body' ][ 'functions' ]);
$this -> assertEquals ( $response [ 'body' ][ 'functions' ][ 0 ][ 'name' ], 'Test' );
/**
* Test for FAILURE
*/
$response = $this -> client -> call ( Client :: METHOD_GET , '/functions' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-12-20 10:55:09 +00:00
'queries' => [
Query :: cursorAfter ( new Document ([ '$id' => 'unknown' ])) -> toString (),
],
2021-10-05 10:30:33 +00:00
]);
$this -> assertEquals ( $response [ 'headers' ][ 'status-code' ], 400 );
2020-12-10 17:47:32 +00:00
return $data ;
}
/**
* @ depends testList
*/
2022-04-13 12:39:31 +00:00
public function testGet ( array $data ) : array
2020-12-10 17:47:32 +00:00
{
/**
* 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' );
2022-02-28 15:17:44 +00:00
2020-12-10 17:47:32 +00:00
/**
* 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 ;
}
/**
* @ depends testGet
*/
2022-04-13 12:39:31 +00:00
public function testUpdate ( $data ) : array
2020-12-10 17:47:32 +00:00
{
/**
* Test for SUCCESS
*/
2022-04-13 12:39:31 +00:00
$response1 = $this -> client -> call ( Client :: METHOD_PUT , '/functions/' . $data [ 'functionId' ], array_merge ([
2020-12-10 17:47:32 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'name' => 'Test1' ,
'events' => [
2022-04-13 12:39:31 +00:00
'users.*.update.name' ,
'users.*.update.email' ,
2020-12-10 17:47:32 +00:00
],
2021-04-23 15:08:18 +00:00
'schedule' => '0 0 1 1 *' ,
2023-08-16 06:19:42 +00:00
'timeout' => 15 ,
'runtime' => 'php-8.0' ,
'entrypoint' => 'index.php' ,
2020-12-10 17:47:32 +00:00
]);
$this -> assertEquals ( 200 , $response1 [ 'headers' ][ 'status-code' ]);
$this -> assertNotEmpty ( $response1 [ 'body' ][ '$id' ]);
$this -> assertEquals ( 'Test1' , $response1 [ 'body' ][ 'name' ]);
2022-12-19 11:21:09 +00:00
$dateValidator = new DatetimeValidator ();
$this -> assertEquals ( true , $dateValidator -> isValid ( $response1 [ 'body' ][ '$createdAt' ]));
$this -> assertEquals ( true , $dateValidator -> isValid ( $response1 [ 'body' ][ '$updatedAt' ]));
2022-01-24 23:46:13 +00:00
$this -> assertEquals ( '' , $response1 [ 'body' ][ 'deployment' ]);
2020-12-10 17:47:32 +00:00
$this -> assertEquals ([
2022-04-13 12:39:31 +00:00
'users.*.update.name' ,
'users.*.update.email' ,
2020-12-10 17:47:32 +00:00
], $response1 [ 'body' ][ 'events' ]);
2021-04-23 15:08:18 +00:00
$this -> assertEquals ( '0 0 1 1 *' , $response1 [ 'body' ][ 'schedule' ]);
2023-08-16 06:19:42 +00:00
$this -> assertEquals ( 15 , $response1 [ 'body' ][ 'timeout' ]);
2022-04-13 12:39:31 +00:00
2023-09-05 11:55:02 +00:00
/**
* Create global variable to test in execution later
*/
$headers = [
'content-type' => 'application/json' ,
'origin' => 'http://localhost' ,
'cookie' => 'a_session_console=' . $this -> getRoot ()[ 'session' ],
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-mode' => 'admin' ,
];
2024-03-06 17:34:21 +00:00
$variable = $this -> client -> call ( Client :: METHOD_POST , '/project/variables' , $headers , [
2023-09-05 11:55:02 +00:00
'key' => 'GLOBAL_VARIABLE' ,
'value' => 'Global Variable Value' ,
2024-03-06 17:34:21 +00:00
]);
2023-09-05 11:55:02 +00:00
$this -> assertEquals ( 201 , $variable [ 'headers' ][ 'status-code' ]);
2020-12-10 17:47:32 +00:00
/**
* Test for FAILURE
*/
return $data ;
}
/**
* @ depends testUpdate
*/
2022-04-13 12:39:31 +00:00
public function testCreateDeployment ( $data ) : array
2020-12-10 17:47:32 +00:00
{
/**
* Test for SUCCESS
*/
2022-02-19 13:57:48 +00:00
$folder = 'php' ;
2022-04-13 12:39:31 +00:00
$code = realpath ( __DIR__ . '/../../../resources/functions' ) . " / $folder /code.tar.gz " ;
2022-02-19 13:57:48 +00:00
$this -> packageCode ( $folder );
2022-02-28 15:17:44 +00:00
2022-04-13 12:39:31 +00:00
$deployment = $this -> client -> call ( Client :: METHOD_POST , '/functions/' . $data [ 'functionId' ] . '/deployments' , array_merge ([
2020-12-10 17:47:32 +00:00
'content-type' => 'multipart/form-data' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2022-02-19 13:57:48 +00:00
'code' => new CURLFile ( $code , 'application/x-gzip' , \basename ( $code )),
2023-03-01 12:00:54 +00:00
'activate' => true
2020-12-10 17:47:32 +00:00
]);
2022-01-24 23:46:13 +00:00
$deploymentId = $deployment [ 'body' ][ '$id' ] ? ? '' ;
2020-12-10 17:47:32 +00:00
2022-07-20 12:01:18 +00:00
$this -> assertEquals ( 202 , $deployment [ 'headers' ][ 'status-code' ]);
2022-01-24 23:46:13 +00:00
$this -> assertNotEmpty ( $deployment [ 'body' ][ '$id' ]);
2023-02-05 20:39:41 +00:00
$this -> assertEquals ( true , ( new DatetimeValidator ()) -> isValid ( $deployment [ 'body' ][ '$createdAt' ]));
2022-01-24 23:46:13 +00:00
$this -> assertEquals ( 'index.php' , $deployment [ 'body' ][ 'entrypoint' ]);
2021-12-10 10:43:56 +00:00
2023-08-23 03:12:04 +00:00
// Poll until deployment is built
while ( true ) {
$deployment = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $data [ 'functionId' ] . '/deployments/' . $deploymentId , [
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ],
]);
if (
$deployment [ 'headers' ][ 'status-code' ] >= 400
|| \in_array ( $deployment [ 'body' ][ 'status' ], [ 'ready' , 'failed' ])
) {
break ;
}
\sleep ( 1 );
}
$this -> assertEquals ( 200 , $deployment [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'ready' , $deployment [ 'body' ][ 'status' ]);
2021-09-28 07:51:09 +00:00
2022-02-23 06:07:15 +00:00
return array_merge ( $data , [ 'deploymentId' => $deploymentId ]);
}
/**
* @ depends testUpdate
*/
2022-04-13 12:39:31 +00:00
public function testCreateDeploymentLarge ( $data ) : array
{
2021-09-28 07:51:09 +00:00
/**
* Test for Large Code File SUCCESS
*/
2022-02-23 06:07:15 +00:00
$folder = 'php-large' ;
2022-04-13 12:39:31 +00:00
$code = realpath ( __DIR__ . '/../../../resources/functions' ) . " / $folder /code.tar.gz " ;
2022-02-23 06:07:15 +00:00
$this -> packageCode ( $folder );
2022-04-13 12:39:31 +00:00
$chunkSize = 5 * 1024 * 1024 ;
2022-02-23 06:07:15 +00:00
$handle = @ fopen ( $code , " rb " );
2021-09-28 07:51:09 +00:00
$mimeType = 'application/x-gzip' ;
$counter = 0 ;
2022-02-23 06:07:15 +00:00
$size = filesize ( $code );
2021-09-28 07:51:09 +00:00
$headers = [
'content-type' => 'multipart/form-data' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ]
];
$id = '' ;
while ( ! feof ( $handle )) {
$curlFile = new \CURLFile ( 'data://' . $mimeType . ';base64,' . base64_encode ( @ fread ( $handle , $chunkSize )), $mimeType , 'php-large-fx.tar.gz' );
2023-04-19 05:38:54 +00:00
$headers [ 'content-range' ] = 'bytes ' . ( $counter * $chunkSize ) . '-' . min (((( $counter * $chunkSize ) + $chunkSize ) - 1 ), $size - 1 ) . '/' . $size ;
2022-04-13 12:39:31 +00:00
if ( ! empty ( $id )) {
2021-09-28 07:51:09 +00:00
$headers [ 'x-appwrite-id' ] = $id ;
}
2022-04-13 12:39:31 +00:00
$largeTag = $this -> client -> call ( Client :: METHOD_POST , '/functions/' . $data [ 'functionId' ] . '/deployments' , array_merge ( $headers , $this -> getHeaders ()), [
2022-02-18 11:16:10 +00:00
'entrypoint' => 'index.php' ,
2021-09-28 07:51:09 +00:00
'code' => $curlFile ,
2023-03-01 12:00:54 +00:00
'activate' => true
2021-09-28 07:51:09 +00:00
]);
$counter ++ ;
$id = $largeTag [ 'body' ][ '$id' ];
}
@ fclose ( $handle );
2022-07-20 12:01:18 +00:00
$this -> assertEquals ( 202 , $largeTag [ 'headers' ][ 'status-code' ]);
2021-09-28 07:51:09 +00:00
$this -> assertNotEmpty ( $largeTag [ 'body' ][ '$id' ]);
2023-02-05 20:39:41 +00:00
$this -> assertEquals ( true , ( new DatetimeValidator ()) -> isValid ( $largeTag [ 'body' ][ '$createdAt' ]));
2022-02-18 11:16:10 +00:00
$this -> assertEquals ( 'index.php' , $largeTag [ 'body' ][ 'entrypoint' ]);
2021-09-28 07:51:09 +00:00
$this -> assertGreaterThan ( 10000 , $largeTag [ 'body' ][ 'size' ]);
2022-04-13 12:39:31 +00:00
2022-02-23 06:07:15 +00:00
return $data ;
2020-12-10 17:47:32 +00:00
}
/**
2022-01-24 23:46:13 +00:00
* @ depends testCreateDeployment
2020-12-10 17:47:32 +00:00
*/
2022-04-13 12:39:31 +00:00
public function testUpdateDeployment ( $data ) : array
2020-12-10 17:47:32 +00:00
{
/**
* Test for SUCCESS
*/
2022-04-13 12:39:31 +00:00
$response = $this -> client -> call ( Client :: METHOD_PATCH , '/functions/' . $data [ 'functionId' ] . '/deployments/' . $data [ 'deploymentId' ], array_merge ([
2020-12-10 17:47:32 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2022-02-15 09:16:32 +00:00
], $this -> getHeaders ()), []);
2020-12-10 17:47:32 +00:00
$this -> assertEquals ( 200 , $response [ 'headers' ][ 'status-code' ]);
$this -> assertNotEmpty ( $response [ 'body' ][ '$id' ]);
2022-12-19 11:21:09 +00:00
$dateValidator = new DatetimeValidator ();
$this -> assertEquals ( true , $dateValidator -> isValid ( $response [ 'body' ][ '$createdAt' ]));
$this -> assertEquals ( true , $dateValidator -> isValid ( $response [ 'body' ][ '$updatedAt' ]));
2022-01-24 23:46:13 +00:00
$this -> assertEquals ( $data [ 'deploymentId' ], $response [ 'body' ][ 'deployment' ]);
2022-04-13 12:39:31 +00:00
2020-12-10 17:47:32 +00:00
/**
* Test for FAILURE
*/
return $data ;
}
/**
2022-01-24 23:46:13 +00:00
* @ depends testCreateDeployment
2020-12-10 17:47:32 +00:00
*/
2022-04-13 12:39:31 +00:00
public function testListDeployments ( array $data ) : array
2020-12-10 17:47:32 +00:00
{
/**
* Test for SUCCESS
*/
2022-04-13 12:39:31 +00:00
$function = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $data [ 'functionId' ] . '/deployments' , array_merge ([
2020-12-10 17:47:32 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
$this -> assertEquals ( $function [ 'headers' ][ 'status-code' ], 200 );
2022-02-27 09:57:09 +00:00
$this -> assertEquals ( $function [ 'body' ][ 'total' ], 2 );
2022-01-24 23:46:13 +00:00
$this -> assertIsArray ( $function [ 'body' ][ 'deployments' ]);
2022-02-18 11:16:10 +00:00
$this -> assertCount ( 2 , $function [ 'body' ][ 'deployments' ]);
2020-12-10 17:47:32 +00:00
2021-09-23 07:01:10 +00:00
/**
* Test search queries
*/
2022-04-13 12:39:31 +00:00
$function = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $data [ 'functionId' ] . '/deployments' , array_merge ([
2021-09-21 08:22:13 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders (), [
'search' => $data [ 'functionId' ]
]));
$this -> assertEquals ( $function [ 'headers' ][ 'status-code' ], 200 );
2022-02-27 09:57:09 +00:00
$this -> assertEquals ( 2 , $function [ 'body' ][ 'total' ]);
2022-01-24 23:46:13 +00:00
$this -> assertIsArray ( $function [ 'body' ][ 'deployments' ]);
2022-02-18 11:16:10 +00:00
$this -> assertCount ( 2 , $function [ 'body' ][ 'deployments' ]);
2022-01-24 23:46:13 +00:00
$this -> assertEquals ( $function [ 'body' ][ 'deployments' ][ 0 ][ '$id' ], $data [ 'deploymentId' ]);
2021-09-21 08:22:13 +00:00
2022-08-24 11:55:43 +00:00
$function = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $data [ 'functionId' ] . '/deployments' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-12-20 10:55:09 +00:00
'queries' => [
Query :: limit ( 1 ) -> toString (),
],
2022-08-24 11:55:43 +00:00
]);
$this -> assertEquals ( $function [ 'headers' ][ 'status-code' ], 200 );
$this -> assertCount ( 1 , $function [ 'body' ][ 'deployments' ]);
$function = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $data [ 'functionId' ] . '/deployments' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-12-20 10:55:09 +00:00
'queries' => [
Query :: offset ( 1 ) -> toString (),
],
2022-08-24 11:55:43 +00:00
]);
$this -> assertEquals ( $function [ 'headers' ][ 'status-code' ], 200 );
$this -> assertCount ( 1 , $function [ 'body' ][ 'deployments' ]);
$function = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $data [ 'functionId' ] . '/deployments' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-12-20 10:55:09 +00:00
'queries' => [
Query :: equal ( 'entrypoint' , [ 'index.php' ]) -> toString (),
],
2022-08-24 11:55:43 +00:00
]);
$this -> assertEquals ( $function [ 'headers' ][ 'status-code' ], 200 );
$this -> assertCount ( 2 , $function [ 'body' ][ 'deployments' ]);
$function = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $data [ 'functionId' ] . '/deployments' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-12-20 10:55:09 +00:00
'queries' => [
Query :: equal ( 'entrypoint' , [ 'index.js' ]) -> toString (),
],
2022-08-24 11:55:43 +00:00
]);
$this -> assertEquals ( $function [ 'headers' ][ 'status-code' ], 200 );
$this -> assertCount ( 0 , $function [ 'body' ][ 'deployments' ]);
2022-04-13 12:39:31 +00:00
$function = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $data [ 'functionId' ] . '/deployments' , array_merge ([
2021-09-21 08:22:13 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders (), [
'search' => 'Test'
]));
$this -> assertEquals ( $function [ 'headers' ][ 'status-code' ], 200 );
2022-02-27 09:57:09 +00:00
$this -> assertEquals ( 2 , $function [ 'body' ][ 'total' ]);
2022-01-24 23:46:13 +00:00
$this -> assertIsArray ( $function [ 'body' ][ 'deployments' ]);
2022-02-18 11:16:10 +00:00
$this -> assertCount ( 2 , $function [ 'body' ][ 'deployments' ]);
2022-01-24 23:46:13 +00:00
$this -> assertEquals ( $function [ 'body' ][ 'deployments' ][ 0 ][ '$id' ], $data [ 'deploymentId' ]);
2021-09-21 08:22:13 +00:00
2022-04-13 12:39:31 +00:00
$function = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $data [ 'functionId' ] . '/deployments' , array_merge ([
2021-09-21 08:22:13 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders (), [
'search' => 'php-8.0'
]));
$this -> assertEquals ( $function [ 'headers' ][ 'status-code' ], 200 );
2022-02-27 09:57:09 +00:00
$this -> assertEquals ( 2 , $function [ 'body' ][ 'total' ]);
2022-01-24 23:46:13 +00:00
$this -> assertIsArray ( $function [ 'body' ][ 'deployments' ]);
2022-02-18 11:16:10 +00:00
$this -> assertCount ( 2 , $function [ 'body' ][ 'deployments' ]);
2022-01-24 23:46:13 +00:00
$this -> assertEquals ( $function [ 'body' ][ 'deployments' ][ 0 ][ '$id' ], $data [ 'deploymentId' ]);
2021-09-21 08:22:13 +00:00
2020-12-10 17:47:32 +00:00
return $data ;
}
/**
2022-01-24 23:46:13 +00:00
* @ depends testCreateDeployment
2020-12-10 17:47:32 +00:00
*/
2022-04-13 12:39:31 +00:00
public function testGetDeployment ( array $data ) : array
2020-12-10 17:47:32 +00:00
{
/**
* Test for SUCCESS
*/
2022-04-13 12:39:31 +00:00
$function = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $data [ 'functionId' ] . '/deployments/' . $data [ 'deploymentId' ], array_merge ([
2020-12-10 17:47:32 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
$this -> assertEquals ( 200 , $function [ 'headers' ][ 'status-code' ]);
2023-08-16 06:19:42 +00:00
$this -> assertGreaterThan ( 0 , $function [ 'body' ][ 'buildTime' ]);
2022-10-31 05:17:13 +00:00
$this -> assertNotEmpty ( $function [ 'body' ][ 'status' ]);
2023-08-16 06:19:42 +00:00
$this -> assertNotEmpty ( $function [ 'body' ][ 'buildLogs' ]);
2020-12-10 17:47:32 +00:00
/**
* Test for FAILURE
*/
2022-04-13 12:39:31 +00:00
$function = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $data [ 'functionId' ] . '/deployments/x' , array_merge ([
2020-12-10 17:47:32 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
$this -> assertEquals ( $function [ 'headers' ][ 'status-code' ], 404 );
return $data ;
}
/**
2022-01-24 23:46:13 +00:00
* @ depends testUpdateDeployment
2020-12-10 17:47:32 +00:00
*/
2022-04-13 12:39:31 +00:00
public function testCreateExecution ( $data ) : array
2020-12-10 17:47:32 +00:00
{
/**
* Test for SUCCESS
*/
2022-04-13 12:39:31 +00:00
$execution = $this -> client -> call ( Client :: METHOD_POST , '/functions/' . $data [ 'functionId' ] . '/executions' , array_merge ([
2020-12-10 17:47:32 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-08-16 06:19:42 +00:00
'async' => false ,
2020-12-10 17:47:32 +00:00
]);
$executionId = $execution [ 'body' ][ '$id' ] ? ? '' ;
2023-08-16 06:19:42 +00:00
$this -> assertEquals ( 201 , $execution [ 'headers' ][ 'status-code' ]);
2021-04-23 15:05:17 +00:00
$this -> assertNotEmpty ( $execution [ 'body' ][ '$id' ]);
$this -> assertNotEmpty ( $execution [ 'body' ][ 'functionId' ]);
2023-02-05 20:39:41 +00:00
$this -> assertEquals ( true , ( new DatetimeValidator ()) -> isValid ( $execution [ 'body' ][ '$createdAt' ]));
2021-04-23 15:05:17 +00:00
$this -> assertEquals ( $data [ 'functionId' ], $execution [ 'body' ][ 'functionId' ]);
$this -> assertEquals ( 'completed' , $execution [ 'body' ][ 'status' ]);
2023-08-16 06:19:42 +00:00
$this -> assertEquals ( 200 , $execution [ 'body' ][ 'responseStatusCode' ]);
$this -> assertStringContainsString ( $execution [ 'body' ][ 'functionId' ], $execution [ 'body' ][ 'responseBody' ]);
$this -> assertStringContainsString ( $data [ 'deploymentId' ], $execution [ 'body' ][ 'responseBody' ]);
$this -> assertStringContainsString ( 'Test1' , $execution [ 'body' ][ 'responseBody' ]);
$this -> assertStringContainsString ( 'http' , $execution [ 'body' ][ 'responseBody' ]);
$this -> assertStringContainsString ( 'PHP' , $execution [ 'body' ][ 'responseBody' ]);
$this -> assertStringContainsString ( '8.0' , $execution [ 'body' ][ 'responseBody' ]);
2023-09-05 11:55:02 +00:00
$this -> assertStringContainsString ( 'Global Variable Value' , $execution [ 'body' ][ 'responseBody' ]);
2023-08-16 06:19:42 +00:00
// $this->assertStringContainsString('êä', $execution['body']['responseBody']); // tests unknown utf-8 chars
2023-02-14 11:01:38 +00:00
$this -> assertEquals ( '' , $execution [ 'body' ][ 'errors' ]);
$this -> assertEquals ( '' , $execution [ 'body' ][ 'logs' ]);
2023-08-16 06:19:42 +00:00
$this -> assertLessThan ( 10 , $execution [ 'body' ][ 'duration' ]);
2020-12-10 17:47:32 +00:00
return array_merge ( $data , [ 'executionId' => $executionId ]);
}
/**
* @ depends testCreateExecution
*/
2022-04-13 12:39:31 +00:00
public function testListExecutions ( array $data ) : array
2020-12-10 17:47:32 +00:00
{
/**
* Test for SUCCESS
*/
2022-04-13 12:39:31 +00:00
$function = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $data [ 'functionId' ] . '/executions' , array_merge ([
2020-12-10 17:47:32 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
$this -> assertEquals ( $function [ 'headers' ][ 'status-code' ], 200 );
2022-02-27 09:57:09 +00:00
$this -> assertEquals ( $function [ 'body' ][ 'total' ], 1 );
2020-12-10 17:47:32 +00:00
$this -> assertIsArray ( $function [ 'body' ][ 'executions' ]);
$this -> assertCount ( 1 , $function [ 'body' ][ 'executions' ]);
$this -> assertEquals ( $function [ 'body' ][ 'executions' ][ 0 ][ '$id' ], $data [ 'executionId' ]);
2022-08-24 11:55:43 +00:00
$response = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $data [ 'functionId' ] . '/executions' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-12-20 10:55:09 +00:00
'queries' => [
Query :: limit ( 1 ) -> toString (),
],
2022-08-24 11:55:43 +00:00
]);
$this -> assertEquals ( 200 , $response [ 'headers' ][ 'status-code' ]);
2023-05-03 11:53:40 +00:00
$this -> assertCount ( 1 , $response [ 'body' ][ 'executions' ]);
2022-08-24 11:55:43 +00:00
$response = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $data [ 'functionId' ] . '/executions' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-12-20 10:55:09 +00:00
'queries' => [
Query :: offset ( 1 ) -> toString (),
],
2022-08-24 11:55:43 +00:00
]);
$this -> assertEquals ( 200 , $response [ 'headers' ][ 'status-code' ]);
$this -> assertCount ( 0 , $response [ 'body' ][ 'executions' ]);
$response = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $data [ 'functionId' ] . '/executions' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-12-20 10:55:09 +00:00
'queries' => [
Query :: equal ( 'trigger' , [ 'http' ]) -> toString (),
],
2022-08-24 11:55:43 +00:00
]);
$this -> assertEquals ( 200 , $response [ 'headers' ][ 'status-code' ]);
$this -> assertCount ( 1 , $response [ 'body' ][ 'executions' ]);
2021-09-27 10:12:42 +00:00
/**
* Test search queries
*/
2022-04-13 12:39:31 +00:00
$response = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $data [ 'functionId' ] . '/executions' , array_merge ([
2021-09-27 10:12:42 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'search' => $data [ 'executionId' ],
]);
$this -> assertEquals ( 200 , $response [ 'headers' ][ 'status-code' ]);
2022-02-27 09:57:09 +00:00
$this -> assertEquals ( 1 , $response [ 'body' ][ 'total' ]);
$this -> assertIsInt ( $response [ 'body' ][ 'total' ]);
2021-09-27 10:12:42 +00:00
$this -> assertCount ( 1 , $response [ 'body' ][ 'executions' ]);
$this -> assertEquals ( $data [ 'functionId' ], $response [ 'body' ][ 'executions' ][ 0 ][ 'functionId' ]);
2022-04-13 12:39:31 +00:00
$response = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $data [ 'functionId' ] . '/executions' , array_merge ([
2021-09-27 10:12:42 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'search' => $data [ 'functionId' ],
]);
$this -> assertEquals ( 200 , $response [ 'headers' ][ 'status-code' ]);
2022-02-27 09:57:09 +00:00
$this -> assertEquals ( 1 , $response [ 'body' ][ 'total' ]);
$this -> assertIsInt ( $response [ 'body' ][ 'total' ]);
2021-09-27 10:12:42 +00:00
$this -> assertCount ( 1 , $response [ 'body' ][ 'executions' ]);
$this -> assertEquals ( $data [ 'executionId' ], $response [ 'body' ][ 'executions' ][ 0 ][ '$id' ]);
2020-12-10 17:47:32 +00:00
return $data ;
}
2021-11-24 10:01:43 +00:00
/**
2022-01-24 23:46:13 +00:00
* @ depends testUpdateDeployment
2021-11-24 10:01:43 +00:00
*/
2022-09-06 04:42:46 +00:00
#[Retry(count: 2)]
2022-04-13 12:39:31 +00:00
public function testSyncCreateExecution ( $data ) : array
2021-11-24 10:01:43 +00:00
{
/**
* Test for SUCCESS
*/
2022-04-13 12:39:31 +00:00
$execution = $this -> client -> call ( Client :: METHOD_POST , '/functions/' . $data [ 'functionId' ] . '/executions' , array_merge ([
2021-11-24 10:01:43 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2022-09-07 08:24:49 +00:00
// Testing default value, should be 'async' => false
2021-11-24 10:01:43 +00:00
]);
2023-08-17 21:38:05 +00:00
2022-02-27 21:09:13 +00:00
$this -> assertEquals ( 201 , $execution [ 'headers' ][ 'status-code' ]);
2021-11-24 10:01:43 +00:00
$this -> assertEquals ( 'completed' , $execution [ 'body' ][ 'status' ]);
2023-08-16 06:19:42 +00:00
$this -> assertEquals ( 200 , $execution [ 'body' ][ 'responseStatusCode' ]);
$this -> assertStringContainsString ( 'Test1' , $execution [ 'body' ][ 'responseBody' ]);
$this -> assertStringContainsString ( 'http' , $execution [ 'body' ][ 'responseBody' ]);
$this -> assertStringContainsString ( 'PHP' , $execution [ 'body' ][ 'responseBody' ]);
$this -> assertStringContainsString ( '8.0' , $execution [ 'body' ][ 'responseBody' ]);
// $this->assertStringContainsString('êä', $execution['body']['response']); // tests unknown utf-8 chars
2022-11-10 12:04:49 +00:00
$this -> assertLessThan ( 1.500 , $execution [ 'body' ][ 'duration' ]);
2021-11-24 10:01:43 +00:00
return $data ;
}
2020-12-10 17:47:32 +00:00
/**
* @ depends testListExecutions
*/
2022-04-13 12:39:31 +00:00
public function testGetExecution ( array $data ) : array
2020-12-10 17:47:32 +00:00
{
/**
* Test for SUCCESS
*/
2022-04-13 12:39:31 +00:00
$function = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $data [ 'functionId' ] . '/executions/' . $data [ 'executionId' ], array_merge ([
2020-12-10 17:47:32 +00:00
'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
*/
2022-04-13 12:39:31 +00:00
$function = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $data [ 'functionId' ] . '/executions/x' , array_merge ([
2020-12-10 17:47:32 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
$this -> assertEquals ( $function [ 'headers' ][ 'status-code' ], 404 );
return $data ;
}
2024-07-22 09:12:43 +00:00
/**
* @ depends testGetExecution
*/
#[Retry(count: 2)]
public function testUpdateSpecs ( $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' ,
'events' => [
'users.*.update.name' ,
'users.*.update.email' ,
],
'timeout' => 15 ,
'runtime' => 'php-8.0' ,
'entrypoint' => 'index.php' ,
2024-07-31 11:27:32 +00:00
'specification' => 's-1vcpu-1gb' ,
2024-07-22 09:12:43 +00:00
]);
$this -> assertEquals ( 200 , $response1 [ 'headers' ][ 'status-code' ]);
$this -> assertNotEmpty ( $response1 [ 'body' ][ '$id' ]);
2024-07-31 11:27:32 +00:00
$this -> assertEquals ( 's-1vcpu-1gb' , $response1 [ 'body' ][ 'specification' ]);
2024-07-22 09:12:43 +00:00
// Test Execution
$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 ()));
2024-07-22 09:17:38 +00:00
2024-07-22 09:12:43 +00:00
$output = json_decode ( $execution [ 'body' ][ 'responseBody' ], true );
2024-07-22 09:17:38 +00:00
2024-07-22 09:12:43 +00:00
$this -> assertEquals ( 1 , $output [ 'APPWRITE_FUNCTION_CPUS' ]);
$this -> assertEquals ( 1024 , $output [ 'APPWRITE_FUNCTION_MEMORY' ]);
$response2 = $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' ,
'events' => [
'users.*.update.name' ,
'users.*.update.email' ,
],
'timeout' => 15 ,
'runtime' => 'php-8.0' ,
'entrypoint' => 'index.php' ,
2024-07-31 11:27:32 +00:00
'specification' => 's-1vcpu-512mb' ,
2024-07-22 09:12:43 +00:00
]);
$this -> assertEquals ( 200 , $response2 [ 'headers' ][ 'status-code' ]);
$this -> assertNotEmpty ( $response2 [ 'body' ][ '$id' ]);
2024-07-31 11:27:32 +00:00
$this -> assertEquals ( 's-1vcpu-512mb' , $response2 [ 'body' ][ 'specification' ]);
2024-07-22 09:12:43 +00:00
// Test Execution
$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 ()));
2024-07-22 09:17:38 +00:00
2024-07-22 09:12:43 +00:00
$output = json_decode ( $execution [ 'body' ][ 'responseBody' ], true );
2024-07-22 09:17:38 +00:00
2024-07-22 09:12:43 +00:00
$this -> assertEquals ( 1 , $output [ 'APPWRITE_FUNCTION_CPUS' ]);
$this -> assertEquals ( 512 , $output [ 'APPWRITE_FUNCTION_MEMORY' ]);
/**
* Test for FAILURE
*/
$response3 = $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' ,
'events' => [
'users.*.update.name' ,
'users.*.update.email' ,
],
'timeout' => 15 ,
'runtime' => 'php-8.0' ,
'entrypoint' => 'index.php' ,
2024-07-31 11:27:32 +00:00
'specification' => 's-2vcpu-512mb' , // Invalid specification
2024-07-22 09:12:43 +00:00
]);
$this -> assertEquals ( 400 , $response3 [ 'headers' ][ 'status-code' ]);
2024-07-31 11:27:32 +00:00
$this -> assertEquals ( 'Invalid `specification` param: String must be a valid specification value of s-1vcpu-512mb, s-1vcpu-1gb' , $response3 [ 'body' ][ 'message' ]);
2024-07-22 09:12:43 +00:00
return $data ;
}
2020-12-10 17:47:32 +00:00
/**
* @ depends testGetExecution
*/
2022-04-13 12:39:31 +00:00
public function testDeleteDeployment ( $data ) : array
2020-12-10 17:47:32 +00:00
{
/**
* Test for SUCCESS
*/
2022-04-13 12:39:31 +00:00
$function = $this -> client -> call ( Client :: METHOD_DELETE , '/functions/' . $data [ 'functionId' ] . '/deployments/' . $data [ 'deploymentId' ], array_merge ([
2020-12-10 17:47:32 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
$this -> assertEquals ( 204 , $function [ 'headers' ][ 'status-code' ]);
$this -> assertEmpty ( $function [ 'body' ]);
2022-04-13 12:39:31 +00:00
$function = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $data [ 'functionId' ] . '/deployments/' . $data [ 'deploymentId' ], array_merge ([
2020-12-10 17:47:32 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
2022-04-13 12:39:31 +00:00
2020-12-10 17:47:32 +00:00
$this -> assertEquals ( 404 , $function [ 'headers' ][ 'status-code' ]);
/**
* Test for FAILURE
*/
return $data ;
}
/**
2022-01-24 23:46:13 +00:00
* @ depends testCreateDeployment
2020-12-10 17:47:32 +00:00
*/
2022-04-13 12:39:31 +00:00
public function testDelete ( $data ) : array
2020-12-10 17:47:32 +00:00
{
/**
* Test for SUCCESS
*/
2022-04-13 12:39:31 +00:00
$function = $this -> client -> call ( Client :: METHOD_DELETE , '/functions/' . $data [ 'functionId' ], array_merge ([
2020-12-10 17:47:32 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
$this -> assertEquals ( 204 , $function [ 'headers' ][ 'status-code' ]);
$this -> assertEmpty ( $function [ 'body' ]);
2022-01-29 00:54:47 +00:00
$function = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $data [ 'functionId' ], array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
2022-04-13 12:39:31 +00:00
2022-01-29 00:54:47 +00:00
$this -> assertEquals ( 404 , $function [ 'headers' ][ 'status-code' ]);
2020-12-10 17:47:32 +00:00
/**
* Test for FAILURE
*/
return $data ;
}
2020-12-12 05:42:29 +00:00
public function testTimeout ()
{
$name = 'php-8.0' ;
2021-09-06 00:37:20 +00:00
$entrypoint = 'index.php' ;
2023-08-16 06:19:42 +00:00
$timeout = 15 ;
2022-02-19 13:57:48 +00:00
$folder = 'timeout' ;
2022-04-13 12:39:31 +00:00
$code = realpath ( __DIR__ . '/../../../resources/functions' ) . " / $folder /code.tar.gz " ;
2022-02-19 13:57:48 +00:00
$this -> packageCode ( $folder );
2022-04-13 12:39:31 +00:00
2020-12-12 05:42:29 +00:00
$function = $this -> client -> call ( Client :: METHOD_POST , '/functions' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2022-08-14 10:33:36 +00:00
'functionId' => ID :: unique (),
2022-04-13 12:39:31 +00:00
'name' => 'Test ' . $name ,
2021-06-21 14:42:39 +00:00
'runtime' => $name ,
2023-08-11 13:34:57 +00:00
'entrypoint' => $entrypoint ,
2020-12-12 05:42:29 +00:00
'events' => [],
2024-05-20 12:14:48 +00:00
'schedule' => '* * * * *' , // execute every minute
2020-12-12 05:42:29 +00:00
'timeout' => $timeout ,
]);
$functionId = $function [ 'body' ][ '$id' ] ? ? '' ;
$this -> assertEquals ( 201 , $function [ 'headers' ][ 'status-code' ]);
2024-05-20 12:14:48 +00:00
$this -> assertEquals ( '* * * * *' , $function [ 'body' ][ 'schedule' ]);
2020-12-12 05:42:29 +00:00
2022-04-13 12:39:31 +00:00
$deployment = $this -> client -> call ( Client :: METHOD_POST , '/functions/' . $functionId . '/deployments' , array_merge ([
2020-12-12 05:42:29 +00:00
'content-type' => 'multipart/form-data' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2021-09-01 09:48:56 +00:00
'entrypoint' => $entrypoint ,
2021-01-31 19:11:03 +00:00
'code' => new CURLFile ( $code , 'application/x-gzip' , basename ( $code )),
2022-02-19 16:14:31 +00:00
'activate' => true ,
2020-12-12 05:42:29 +00:00
]);
2023-08-23 03:39:38 +00:00
$deploymentId = $deployment [ 'body' ][ '$id' ] ? ? '' ;
2022-07-20 12:01:18 +00:00
$this -> assertEquals ( 202 , $deployment [ 'headers' ][ 'status-code' ]);
2020-12-12 05:42:29 +00:00
2023-08-23 03:12:04 +00:00
// Poll until deployment is built
while ( true ) {
$deployment = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $functionId . '/deployments/' . $deploymentId , [
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ],
]);
if (
$deployment [ 'headers' ][ 'status-code' ] >= 400
|| \in_array ( $deployment [ 'body' ][ 'status' ], [ 'ready' , 'failed' ])
) {
break ;
}
\sleep ( 1 );
}
$this -> assertEquals ( 'ready' , $deployment [ 'body' ][ 'status' ]);
2022-04-13 12:39:31 +00:00
$execution = $this -> client -> call ( Client :: METHOD_POST , '/functions/' . $functionId . '/executions' , array_merge ([
2020-12-12 05:42:29 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2021-12-06 15:04:00 +00:00
'async' => true ,
2020-12-12 05:42:29 +00:00
]);
$executionId = $execution [ 'body' ][ '$id' ] ? ? '' ;
2022-04-13 12:39:31 +00:00
2022-07-20 12:01:18 +00:00
$this -> assertEquals ( 202 , $execution [ 'headers' ][ 'status-code' ]);
2020-12-12 05:42:29 +00:00
2022-09-14 17:17:03 +00:00
sleep ( 20 );
2020-12-12 05:42:29 +00:00
2022-04-13 12:39:31 +00:00
$executions = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $functionId . '/executions' , array_merge ([
2020-12-12 05:42:29 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
$this -> assertEquals ( $executions [ 'headers' ][ 'status-code' ], 200 );
2022-02-27 09:57:09 +00:00
$this -> assertEquals ( $executions [ 'body' ][ 'total' ], 1 );
2020-12-12 05:42:29 +00:00
$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' ], 'failed' );
2023-08-16 06:19:42 +00:00
$this -> assertEquals ( $executions [ 'body' ][ 'executions' ][ 0 ][ 'responseStatusCode' ], 500 );
2022-09-09 12:02:04 +00:00
$this -> assertGreaterThan ( 2 , $executions [ 'body' ][ 'executions' ][ 0 ][ 'duration' ]);
2023-08-16 06:19:42 +00:00
$this -> assertLessThan ( 20 , $executions [ 'body' ][ 'executions' ][ 0 ][ 'duration' ]);
$this -> assertEquals ( $executions [ 'body' ][ 'executions' ][ 0 ][ 'responseBody' ], '' );
2023-02-14 11:01:38 +00:00
$this -> assertEquals ( $executions [ 'body' ][ 'executions' ][ 0 ][ 'logs' ], '' );
2023-08-16 06:19:42 +00:00
$this -> assertStringContainsString ( 'timed out' , $executions [ 'body' ][ 'executions' ][ 0 ][ 'errors' ]);
2022-01-29 00:00:25 +00:00
2024-06-06 08:52:22 +00:00
sleep ( 75 ); // Wait for scheduled execution to be created and time out
2024-05-20 12:14:48 +00:00
$executions = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $functionId . '/executions' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
$this -> assertEquals ( 200 , $executions [ 'headers' ][ 'status-code' ]);
$this -> assertCount ( 2 , $executions [ 'body' ][ 'executions' ]);
$this -> assertIsArray ( $executions [ 'body' ][ 'executions' ]);
$this -> assertEquals ( $executions [ 'body' ][ 'executions' ][ 1 ][ 'trigger' ], 'schedule' );
2022-05-16 13:05:58 +00:00
// Cleanup : Delete function
2022-04-13 12:39:31 +00:00
$response = $this -> client -> call ( Client :: METHOD_DELETE , '/functions/' . $functionId , [
2022-01-29 00:00:25 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ],
], []);
$this -> assertEquals ( 204 , $response [ 'headers' ][ 'status-code' ]);
2020-12-12 05:42:29 +00:00
}
2021-03-10 19:43:15 +00:00
/**
2023-08-19 08:05:49 +00:00
*
* @ return array < mixed >
2021-03-10 19:43:15 +00:00
*/
2023-08-19 08:05:49 +00:00
public function provideCustomExecutions () : array
2021-03-10 19:43:15 +00:00
{
2023-08-19 08:05:49 +00:00
return [
[ 'folder' => 'php-fn' , 'name' => 'php-8.0' , 'entrypoint' => 'index.php' , 'runtimeName' => 'PHP' , 'runtimeVersion' => '8.0' ],
[ 'folder' => 'node' , 'name' => 'node-18.0' , 'entrypoint' => 'index.js' , 'runtimeName' => 'Node.js' , 'runtimeVersion' => '18.0' ],
[ 'folder' => 'python' , 'name' => 'python-3.9' , 'entrypoint' => 'main.py' , 'runtimeName' => 'Python' , 'runtimeVersion' => '3.9' ],
[ 'folder' => 'ruby' , 'name' => 'ruby-3.1' , 'entrypoint' => 'main.rb' , 'runtimeName' => 'Ruby' , 'runtimeVersion' => '3.1' ],
2023-08-19 11:37:45 +00:00
// Swift and Dart disabled as it's very slow.
// [ 'folder' => 'dart', 'name' => 'dart-2.15', 'entrypoint' => 'main.dart', 'runtimeName' => 'Dart', 'runtimeVersion' => '2.15' ],
// [ 'folder' => 'swift', 'name' => 'swift-5.5', 'entrypoint' => 'index.swift', 'runtimeName' => 'Swift', 'runtimeVersion' => '5.5' ],
2023-08-19 08:05:49 +00:00
];
2021-03-10 19:43:15 +00:00
}
2021-12-09 13:02:12 +00:00
2023-08-19 08:05:49 +00:00
/**
* @ param string $folder
* @ param string $name
* @ param string $entrypoint
2023-08-19 11:37:45 +00:00
*
2023-08-19 08:05:49 +00:00
* @ dataProvider provideCustomExecutions
* @ depends testTimeout
*/
public function testCreateCustomExecution ( string $folder , string $name , string $entrypoint , string $runtimeName , string $runtimeVersion )
2021-12-09 13:02:12 +00:00
{
2023-09-05 13:47:59 +00:00
$timeout = 5 ;
2022-04-13 12:39:31 +00:00
$code = realpath ( __DIR__ . '/../../../resources/functions' ) . " / $folder /code.tar.gz " ;
2022-02-23 14:39:09 +00:00
$this -> packageCode ( $folder );
2022-02-28 15:43:00 +00:00
2022-02-23 14:39:09 +00:00
$function = $this -> client -> call ( Client :: METHOD_POST , '/functions' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2022-08-14 10:33:36 +00:00
'functionId' => ID :: unique (),
2022-04-13 12:39:31 +00:00
'name' => 'Test ' . $name ,
2022-02-23 14:39:09 +00:00
'runtime' => $name ,
2023-08-11 13:34:57 +00:00
'entrypoint' => $entrypoint ,
2022-02-23 14:39:09 +00:00
'events' => [],
'timeout' => $timeout ,
]);
$functionId = $function [ 'body' ][ '$id' ] ? ? '' ;
$this -> assertEquals ( 201 , $function [ 'headers' ][ 'status-code' ]);
2022-08-09 15:29:24 +00:00
$variable = $this -> client -> call ( Client :: METHOD_POST , '/functions/' . $functionId . '/variables' , array_merge ([
2022-08-03 13:32:50 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'key' => 'CUSTOM_VARIABLE' ,
'value' => 'variable' ,
]);
$this -> assertEquals ( 201 , $variable [ 'headers' ][ 'status-code' ]);
2022-04-13 12:39:31 +00:00
$deployment = $this -> client -> call ( Client :: METHOD_POST , '/functions/' . $functionId . '/deployments' , array_merge ([
2022-02-23 14:39:09 +00:00
'content-type' => 'multipart/form-data' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'entrypoint' => $entrypoint ,
'code' => new CURLFile ( $code , 'application/x-gzip' , basename ( $code )),
2023-08-19 08:05:49 +00:00
'activate' => true
2022-02-23 14:39:09 +00:00
]);
$deploymentId = $deployment [ 'body' ][ '$id' ] ? ? '' ;
2022-07-20 12:01:18 +00:00
$this -> assertEquals ( 202 , $deployment [ 'headers' ][ 'status-code' ]);
2022-02-23 14:39:09 +00:00
2023-08-23 03:12:04 +00:00
// Poll until deployment is built
while ( true ) {
$deployment = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $function [ 'body' ][ '$id' ] . '/deployments/' . $deploymentId , [
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ],
]);
if (
$deployment [ 'headers' ][ 'status-code' ] >= 400
|| \in_array ( $deployment [ 'body' ][ 'status' ], [ 'ready' , 'failed' ])
) {
break ;
}
\sleep ( 1 );
}
2022-02-23 14:39:09 +00:00
2023-08-19 08:05:49 +00:00
$deployment = $this -> client -> call ( Client :: METHOD_PATCH , '/functions/' . $functionId . '/deployments/' . $deploymentId , array_merge ([
2022-08-03 13:32:50 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-08-19 08:05:49 +00:00
], $this -> getHeaders ()), []);
2022-02-23 16:22:58 +00:00
2023-08-19 08:05:49 +00:00
$this -> assertEquals ( 200 , $deployment [ 'headers' ][ 'status-code' ]);
2022-02-23 16:22:58 +00:00
2022-04-13 12:39:31 +00:00
$execution = $this -> client -> call ( Client :: METHOD_POST , '/functions/' . $functionId . '/executions' , array_merge ([
2022-02-23 16:22:58 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-08-19 08:05:49 +00:00
'body' => 'foobar' ,
'async' => false
2022-02-23 16:22:58 +00:00
]);
$executionId = $execution [ 'body' ][ '$id' ] ? ? '' ;
2023-08-19 08:05:49 +00:00
$output = json_decode ( $execution [ 'body' ][ 'responseBody' ], true );
2022-04-13 12:39:31 +00:00
2023-08-19 08:05:49 +00:00
$this -> assertEquals ( 201 , $execution [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'completed' , $execution [ 'body' ][ 'status' ]);
$this -> assertEquals ( 200 , $execution [ 'body' ][ 'responseStatusCode' ]);
2022-02-23 16:22:58 +00:00
$this -> assertEquals ( $functionId , $output [ 'APPWRITE_FUNCTION_ID' ]);
2022-04-13 12:39:31 +00:00
$this -> assertEquals ( 'Test ' . $name , $output [ 'APPWRITE_FUNCTION_NAME' ]);
2022-02-23 16:22:58 +00:00
$this -> assertEquals ( $deploymentId , $output [ 'APPWRITE_FUNCTION_DEPLOYMENT' ]);
$this -> assertEquals ( 'http' , $output [ 'APPWRITE_FUNCTION_TRIGGER' ]);
2023-08-19 08:05:49 +00:00
$this -> assertEquals ( $runtimeName , $output [ 'APPWRITE_FUNCTION_RUNTIME_NAME' ]);
$this -> assertEquals ( $runtimeVersion , $output [ 'APPWRITE_FUNCTION_RUNTIME_VERSION' ]);
2022-02-23 16:22:58 +00:00
$this -> assertEquals ( '' , $output [ 'APPWRITE_FUNCTION_EVENT' ]);
$this -> assertEquals ( 'foobar' , $output [ 'APPWRITE_FUNCTION_DATA' ]);
2023-08-19 08:05:49 +00:00
$this -> assertEquals ( 'variable' , $output [ 'CUSTOM_VARIABLE' ]);
$this -> assertEmpty ( $output [ 'APPWRITE_FUNCTION_USER_ID' ]);
2022-02-23 16:22:58 +00:00
$this -> assertEmpty ( $output [ 'APPWRITE_FUNCTION_JWT' ]);
$this -> assertEquals ( $this -> getProject ()[ '$id' ], $output [ 'APPWRITE_FUNCTION_PROJECT_ID' ]);
2023-08-19 08:05:49 +00:00
$this -> assertStringContainsString ( 'Amazing Function Log' , $execution [ 'body' ][ 'logs' ]);
$this -> assertEmpty ( $execution [ 'body' ][ 'errors' ]);
2022-02-23 16:22:58 +00:00
2022-04-13 12:39:31 +00:00
$executions = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $functionId . '/executions' , array_merge ([
2022-02-24 05:57:25 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
2022-04-13 12:39:31 +00:00
2022-02-24 05:57:25 +00:00
$this -> assertEquals ( $executions [ 'headers' ][ 'status-code' ], 200 );
2022-02-27 09:57:09 +00:00
$this -> assertEquals ( $executions [ 'body' ][ 'total' ], 1 );
2022-02-24 05:57:25 +00:00
$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' );
2023-08-19 08:05:49 +00:00
$this -> assertStringContainsString ( 'Amazing Function Log' , $executions [ 'body' ][ 'executions' ][ 0 ][ 'logs' ]);
2022-02-24 05:57:25 +00:00
2022-05-23 14:54:50 +00:00
// Cleanup : Delete function
2022-04-13 12:39:31 +00:00
$response = $this -> client -> call ( Client :: METHOD_DELETE , '/functions/' . $functionId , [
2022-02-24 05:57:25 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-09-05 13:47:59 +00:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ],
], []);
$this -> assertEquals ( 204 , $response [ 'headers' ][ 'status-code' ]);
}
public function testv2Function ()
{
$timeout = 5 ;
$code = realpath ( __DIR__ . '/../../../resources/functions' ) . " /php-v2/code.tar.gz " ;
$this -> packageCode ( 'php-v2' );
$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 V2' ,
'runtime' => 'php-8.0' ,
'entrypoint' => 'index.php' ,
'events' => [],
'timeout' => $timeout ,
]);
$functionId = $function [ 'body' ][ '$id' ] ? ? '' ;
$this -> assertEquals ( 201 , $function [ 'headers' ][ 'status-code' ]);
$headers = [
'content-type' => 'application/json' ,
'origin' => 'http://localhost' ,
'cookie' => 'a_session_console=' . $this -> getRoot ()[ 'session' ],
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-mode' => 'admin' ,
];
$variable = $this -> client -> call ( Client :: METHOD_PATCH , '/mock/functions-v2' , $headers , [
'functionId' => $functionId
]);
$this -> assertEquals ( 204 , $variable [ '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' ]);
// Poll until deployment is built
while ( true ) {
$deployment = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $function [ 'body' ][ '$id' ] . '/deployments/' . $deploymentId , [
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ],
]);
if (
$deployment [ 'headers' ][ 'status-code' ] >= 400
|| \in_array ( $deployment [ 'body' ][ 'status' ], [ 'ready' , 'failed' ])
) {
break ;
}
\sleep ( 1 );
}
$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' ]);
$execution = $this -> client -> call ( Client :: METHOD_POST , '/functions/' . $functionId . '/executions' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'body' => 'foobar' ,
'async' => false
]);
$output = json_decode ( $execution [ 'body' ][ 'responseBody' ], true );
$this -> assertEquals ( 201 , $execution [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'completed' , $execution [ 'body' ][ 'status' ]);
$this -> assertEquals ( 200 , $execution [ 'body' ][ 'responseStatusCode' ]);
$this -> assertEquals ( true , $output [ 'v2Woks' ]);
// Cleanup : Delete function
$response = $this -> client -> call ( Client :: METHOD_DELETE , '/functions/' . $functionId , [
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2022-02-24 05:57:25 +00:00
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ],
], []);
$this -> assertEquals ( 204 , $response [ 'headers' ][ 'status-code' ]);
}
2021-12-09 13:02:12 +00:00
public function testGetRuntimes ()
{
$runtimes = $this -> client -> call ( Client :: METHOD_GET , '/functions/runtimes' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
$this -> assertEquals ( 200 , $runtimes [ 'headers' ][ 'status-code' ]);
2022-02-27 09:57:09 +00:00
$this -> assertGreaterThan ( 0 , $runtimes [ 'body' ][ 'total' ]);
2021-12-09 13:02:12 +00:00
$runtime = $runtimes [ 'body' ][ 'runtimes' ][ 0 ];
$this -> assertArrayHasKey ( '$id' , $runtime );
$this -> assertArrayHasKey ( 'name' , $runtime );
$this -> assertArrayHasKey ( 'version' , $runtime );
$this -> assertArrayHasKey ( 'logo' , $runtime );
$this -> assertArrayHasKey ( 'image' , $runtime );
$this -> assertArrayHasKey ( 'base' , $runtime );
$this -> assertArrayHasKey ( 'supports' , $runtime );
}
2023-09-24 11:19:31 +00:00
public function testEventTrigger ()
{
$timeout = 5 ;
$code = realpath ( __DIR__ . '/../../../resources/functions' ) . " /php-event/code.tar.gz " ;
$this -> packageCode ( 'php-event' );
$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 Event executions' ,
'runtime' => 'php-8.0' ,
'entrypoint' => 'index.php' ,
'events' => [
'users.*.create' ,
],
'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' ]);
// Poll until deployment is built
while ( true ) {
$deployment = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $function [ 'body' ][ '$id' ] . '/deployments/' . $deploymentId , [
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ],
]);
if (
$deployment [ 'headers' ][ 'status-code' ] >= 400
|| \in_array ( $deployment [ 'body' ][ 'status' ], [ 'ready' , 'failed' ])
) {
break ;
}
\sleep ( 1 );
}
$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' ]);
2023-09-24 12:38:33 +00:00
// Wait a little for activation to finish
sleep ( 5 );
2023-09-24 11:19:31 +00:00
// Create user to trigger event
$user = $this -> client -> call ( Client :: METHOD_POST , '/users' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'userId' => 'unique()' ,
'name' => 'Event User'
]);
$userId = $user [ 'body' ][ '$id' ];
$this -> assertEquals ( 201 , $user [ 'headers' ][ 'status-code' ]);
// Wait for execution to occur
sleep ( 15 );
$executions = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $functionId . '/executions' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2023-09-24 12:34:27 +00:00
], $this -> getHeaders ()));
2023-09-24 11:19:31 +00:00
$execution = $executions [ 'body' ][ 'executions' ][ 0 ];
$this -> assertEquals ( 200 , $executions [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'completed' , $execution [ 'status' ]);
$this -> assertEquals ( 204 , $execution [ 'responseStatusCode' ]);
$this -> assertStringContainsString ( $userId , $execution [ 'logs' ]);
$this -> assertStringContainsString ( 'Event User' , $execution [ 'logs' ]);
// 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' ]);
// Cleanup : Delete user
$response = $this -> client -> call ( Client :: METHOD_DELETE , '/users/' . $userId , [
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ],
], []);
$this -> assertEquals ( 204 , $response [ 'headers' ][ 'status-code' ]);
}
2023-10-27 08:26:46 +00:00
public function testCookieExecution ()
{
$timeout = 5 ;
$code = realpath ( __DIR__ . '/../../../resources/functions' ) . " /php-cookie/code.tar.gz " ;
$this -> packageCode ( 'php-cookie' );
$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 Cookie 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' ]);
// Poll until deployment is built
while ( true ) {
$deployment = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $function [ 'body' ][ '$id' ] . '/deployments/' . $deploymentId , [
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ],
]);
if (
$deployment [ 'headers' ][ 'status-code' ] >= 400
|| \in_array ( $deployment [ 'body' ][ 'status' ], [ 'ready' , 'failed' ])
) {
break ;
}
\sleep ( 1 );
}
$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 );
2023-10-27 08:33:08 +00:00
2023-10-27 10:01:37 +00:00
$cookie = 'cookieName=cookieValue; cookie2=value2; cookie3=value=3; cookie4=val:ue4; cookie5=value5' ;
2023-10-27 08:33:08 +00:00
2023-10-27 08:26:46 +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 ()), [
'async' => false ,
'headers' => [
'cookie' => $cookie
]
]);
$this -> assertEquals ( 201 , $execution [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'completed' , $execution [ 'body' ][ 'status' ]);
$this -> assertEquals ( 200 , $execution [ 'body' ][ 'responseStatusCode' ]);
$this -> assertEquals ( $cookie , $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' ]);
}
2023-10-27 13:33:26 +00:00
public function testFunctionsDomain ()
{
$timeout = 5 ;
$code = realpath ( __DIR__ . '/../../../resources/functions' ) . " /php-cookie/code.tar.gz " ;
$this -> packageCode ( 'php-cookie' );
$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 Cookie executions' ,
'runtime' => 'php-8.0' ,
'entrypoint' => 'index.php' ,
'timeout' => $timeout ,
'execute' => [ 'any' ]
]);
$functionId = $function [ 'body' ][ '$id' ] ? ? '' ;
$this -> assertEquals ( 201 , $function [ 'headers' ][ 'status-code' ]);
$rules = $this -> client -> call ( Client :: METHOD_GET , '/proxy/rules' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2023-12-20 10:55:09 +00:00
'queries' => [
Query :: equal ( 'resourceId' , [ $functionId ]) -> toString (),
Query :: equal ( 'resourceType' , [ 'function' ]) -> toString (),
],
2023-10-27 13:33:26 +00:00
]);
$this -> assertEquals ( 200 , $rules [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 1 , $rules [ 'body' ][ 'total' ]);
$this -> assertCount ( 1 , $rules [ 'body' ][ 'rules' ]);
$this -> assertNotEmpty ( $rules [ 'body' ][ 'rules' ][ 0 ][ 'domain' ]);
$domain = $rules [ 'body' ][ 'rules' ][ 0 ][ 'domain' ];
$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' ]);
// Poll until deployment is built
while ( true ) {
$deployment = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $function [ 'body' ][ '$id' ] . '/deployments/' . $deploymentId , [
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ],
]);
if (
$deployment [ 'headers' ][ 'status-code' ] >= 400
|| \in_array ( $deployment [ 'body' ][ 'status' ], [ 'ready' , 'failed' ])
) {
break ;
}
\sleep ( 1 );
}
$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 );
$cookie = 'cookieName=cookieValue; cookie2=value2; cookie3=value=3; cookie4=val:ue4; cookie5=value5' ;
$proxyClient = new Client ();
$proxyClient -> setEndpoint ( 'http://' . $domain );
$response = $proxyClient -> call ( Client :: METHOD_GET , '/' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'cookie' => $cookie
]));
$this -> assertEquals ( 200 , $response [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( $cookie , $response [ 'body' ]);
2024-08-05 03:21:10 +00:00
// Await Aggregation
sleep ( App :: getEnv ( '_APP_USAGE_AGGREGATION_INTERVAL' , 30 ));
2024-08-02 07:24:54 +00:00
$response = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $functionId . '/usage' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ]
], $this -> getHeaders ()), [
'range' => '24h'
]);
2024-08-05 03:24:27 +00:00
2024-08-02 07:24:54 +00:00
$this -> assertEquals ( 200 , $response [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 19 , count ( $response [ 'body' ]));
$this -> assertEquals ( '24h' , $response [ 'body' ][ 'range' ]);
2023-10-27 13:33:26 +00:00
// 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' ]);
}
2022-04-13 12:39:31 +00:00
}