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 ;
2022-02-20 02:57:48 +13:00
use Utopia\CLI\Console ;
2021-10-05 23:30:33 +13:00
use Utopia\Database\Database ;
2020-05-06 08:37:59 +12:00
2020-12-09 11:24:22 +13:00
class FunctionsCustomServerTest extends Scope
2020-05-06 08:37:59 +12:00
{
use FunctionsBase ;
use ProjectCustom ;
use SideServer ;
2022-04-14 00:39:31 +12:00
public function testCreate () : array
2020-12-11 06:47:32 +13: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 ()), [
2021-07-19 20:24:32 +12:00
'functionId' => 'unique()' ,
2020-12-11 06:47:32 +13:00
'name' => 'Test' ,
2021-06-22 02:42:39 +12:00
'runtime' => 'php-8.0' ,
2020-12-11 06:47:32 +13:00
'vars' => [
'funcKey1' => 'funcValue1' ,
'funcKey2' => 'funcValue2' ,
'funcKey3' => 'funcValue3' ,
],
'events' => [
2022-04-14 00:39:31 +12:00
'users.*.create' ,
'users.*.delete' ,
2020-12-11 06:47:32 +13:00
],
2021-04-24 03:05:17 +12:00
'schedule' => '0 0 1 1 *' ,
2020-12-11 06:47:32 +13: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-23 03:56:05 +12:00
$this -> assertEquals ( 'php-8.0' , $response1 [ 'body' ][ 'runtime' ]);
2020-12-11 06:47:32 +13:00
$this -> assertIsInt ( $response1 [ 'body' ][ 'dateCreated' ]);
$this -> assertIsInt ( $response1 [ 'body' ][ 'dateUpdated' ]);
2022-01-25 12:46:13 +13:00
$this -> assertEquals ( '' , $response1 [ 'body' ][ 'deployment' ]);
2020-12-11 06:47:32 +13:00
$this -> assertEquals ([
'funcKey1' => 'funcValue1' ,
'funcKey2' => 'funcValue2' ,
'funcKey3' => 'funcValue3' ,
], $response1 [ 'body' ][ 'vars' ]);
$this -> assertEquals ([
2022-04-14 00:39:31 +12:00
'users.*.create' ,
'users.*.delete' ,
2020-12-11 06:47:32 +13:00
], $response1 [ 'body' ][ 'events' ]);
2021-04-24 03:05:17 +12:00
$this -> assertEquals ( '0 0 1 1 *' , $response1 [ 'body' ][ 'schedule' ]);
2020-12-11 06:47:32 +13:00
$this -> assertEquals ( 10 , $response1 [ 'body' ][ 'timeout' ]);
2022-04-14 00:39:31 +12:00
2020-12-11 06:47:32 +13:00
/**
* Test for FAILURE
*/
return [
'functionId' => $functionId ,
];
}
/**
* @ depends testCreate
*/
2022-04-14 00:39:31 +12:00
public function testList ( array $data ) : array
2020-12-11 06:47:32 +13:00
{
/**
* Test for SUCCESS
*/
2021-08-10 00:33:16 +12:00
2021-09-23 19:01:10 +12:00
/**
* Test search queries
*/
2021-09-21 20:22:13 +12: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' );
$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 19:01:10 +12:00
/**
* Test pagination
*/
2021-08-10 00:33:16 +12:00
$response = $this -> client -> call ( Client :: METHOD_POST , '/functions' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'functionId' => 'unique()' ,
'name' => 'Test 2' ,
'runtime' => 'php-8.0' ,
'vars' => [
'funcKey1' => 'funcValue1' ,
'funcKey2' => 'funcValue2' ,
'funcKey3' => 'funcValue3' ,
],
'events' => [
2022-04-14 00:39:31 +12:00
'users.*.create' ,
'users.*.delete' ,
2021-08-10 00:33:16 +12:00
],
'schedule' => '0 0 1 1 *' ,
'timeout' => 10 ,
]);
$this -> assertNotEmpty ( $response [ 'body' ][ '$id' ]);
$functions = $this -> client -> call ( Client :: METHOD_GET , '/functions' , array_merge ([
2020-12-11 06:47:32 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
2021-08-10 00:33:16 +12:00
$this -> assertEquals ( $functions [ 'headers' ][ 'status-code' ], 200 );
2022-02-27 22:57:09 +13:00
$this -> assertEquals ( $functions [ 'body' ][ 'total' ], 2 );
2021-08-10 00:33:16 +12: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 ()), [
2021-10-05 23:30:33 +13:00
'cursor' => $functions [ 'body' ][ 'functions' ][ 0 ][ '$id' ]
2021-08-10 00:33:16 +12: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 23:30:33 +13:00
$response = $this -> client -> call ( Client :: METHOD_GET , '/functions' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'cursor' => $functions [ 'body' ][ 'functions' ][ 1 ][ '$id' ],
'cursorDirection' => Database :: CURSOR_BEFORE
]);
$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 ()), [
'cursor' => 'unknown' ,
]);
$this -> assertEquals ( $response [ 'headers' ][ 'status-code' ], 400 );
2020-12-11 06:47:32 +13:00
return $data ;
}
/**
* @ depends testList
*/
2022-04-14 00:39:31 +12:00
public function testGet ( array $data ) : array
2020-12-11 06:47:32 +13: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-03-01 04:17:44 +13:00
2020-12-11 06:47:32 +13: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-14 00:39:31 +12:00
public function testUpdate ( $data ) : array
2020-12-11 06:47:32 +13:00
{
/**
* Test for SUCCESS
*/
2022-04-14 00:39:31 +12:00
$response1 = $this -> client -> call ( Client :: METHOD_PUT , '/functions/' . $data [ 'functionId' ], array_merge ([
2020-12-11 06:47:32 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'name' => 'Test1' ,
'vars' => [
'key4' => 'value4' ,
'key5' => 'value5' ,
'key6' => 'value6' ,
],
'events' => [
2022-04-14 00:39:31 +12:00
'users.*.update.name' ,
'users.*.update.email' ,
2020-12-11 06:47:32 +13:00
],
2021-04-24 03:08:18 +12:00
'schedule' => '0 0 1 1 *' ,
2020-12-11 06:47:32 +13:00
'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' ]);
2022-01-25 12:46:13 +13:00
$this -> assertEquals ( '' , $response1 [ 'body' ][ 'deployment' ]);
2020-12-11 06:47:32 +13:00
$this -> assertEquals ([
'key4' => 'value4' ,
'key5' => 'value5' ,
'key6' => 'value6' ,
], $response1 [ 'body' ][ 'vars' ]);
$this -> assertEquals ([
2022-04-14 00:39:31 +12:00
'users.*.update.name' ,
'users.*.update.email' ,
2020-12-11 06:47:32 +13:00
], $response1 [ 'body' ][ 'events' ]);
2021-04-24 03:08:18 +12:00
$this -> assertEquals ( '0 0 1 1 *' , $response1 [ 'body' ][ 'schedule' ]);
2020-12-11 06:47:32 +13:00
$this -> assertEquals ( 5 , $response1 [ 'body' ][ 'timeout' ]);
2022-04-14 00:39:31 +12:00
2020-12-11 06:47:32 +13:00
/**
* Test for FAILURE
*/
return $data ;
}
/**
* @ depends testUpdate
*/
2022-04-14 00:39:31 +12:00
public function testCreateDeployment ( $data ) : array
2020-12-11 06:47:32 +13:00
{
/**
* Test for SUCCESS
*/
2022-02-20 02:57:48 +13:00
$folder = 'php' ;
2022-04-14 00:39:31 +12:00
$code = realpath ( __DIR__ . '/../../../resources/functions' ) . " / $folder /code.tar.gz " ;
2022-02-20 02:57:48 +13:00
$this -> packageCode ( $folder );
2022-03-01 04:17:44 +13:00
2022-04-14 00:39:31 +12:00
$deployment = $this -> client -> call ( Client :: METHOD_POST , '/functions/' . $data [ 'functionId' ] . '/deployments' , array_merge ([
2020-12-11 06:47:32 +13:00
'content-type' => 'multipart/form-data' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2021-09-01 21:48:56 +12:00
'entrypoint' => 'index.php' ,
2022-02-20 02:57:48 +13:00
'code' => new CURLFile ( $code , 'application/x-gzip' , \basename ( $code )),
2020-12-11 06:47:32 +13:00
]);
2022-01-25 12:46:13 +13:00
$deploymentId = $deployment [ 'body' ][ '$id' ] ? ? '' ;
2020-12-11 06:47:32 +13:00
2022-01-25 12:46:13 +13:00
$this -> assertEquals ( 201 , $deployment [ 'headers' ][ 'status-code' ]);
$this -> assertNotEmpty ( $deployment [ 'body' ][ '$id' ]);
$this -> assertIsInt ( $deployment [ 'body' ][ 'dateCreated' ]);
$this -> assertEquals ( 'index.php' , $deployment [ 'body' ][ 'entrypoint' ]);
2021-12-10 23:43:56 +13:00
2022-01-25 12:46:13 +13:00
// Wait for deployment to build.
2022-03-01 04:17:44 +13:00
sleep ( 30 );
2021-09-28 20:51:09 +13:00
2022-02-23 19:07:15 +13:00
return array_merge ( $data , [ 'deploymentId' => $deploymentId ]);
}
/**
* @ depends testUpdate
*/
2022-04-14 00:39:31 +12:00
public function testCreateDeploymentLarge ( $data ) : array
{
2021-09-28 20:51:09 +13:00
/**
* Test for Large Code File SUCCESS
*/
2022-02-23 19:07:15 +13:00
$folder = 'php-large' ;
2022-04-14 00:39:31 +12:00
$code = realpath ( __DIR__ . '/../../../resources/functions' ) . " / $folder /code.tar.gz " ;
2022-02-23 19:07:15 +13:00
$this -> packageCode ( $folder );
2022-04-14 00:39:31 +12:00
$chunkSize = 5 * 1024 * 1024 ;
2022-02-23 19:07:15 +13:00
$handle = @ fopen ( $code , " rb " );
2021-09-28 20:51:09 +13:00
$mimeType = 'application/x-gzip' ;
$counter = 0 ;
2022-02-23 19:07:15 +13:00
$size = filesize ( $code );
2021-09-28 20:51:09 +13: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' );
$headers [ 'content-range' ] = 'bytes ' . ( $counter * $chunkSize ) . '-' . min (((( $counter * $chunkSize ) + $chunkSize ) - 1 ), $size ) . '/' . $size ;
2022-04-14 00:39:31 +12:00
if ( ! empty ( $id )) {
2021-09-28 20:51:09 +13:00
$headers [ 'x-appwrite-id' ] = $id ;
}
2022-04-14 00:39:31 +12:00
$largeTag = $this -> client -> call ( Client :: METHOD_POST , '/functions/' . $data [ 'functionId' ] . '/deployments' , array_merge ( $headers , $this -> getHeaders ()), [
2022-02-19 00:16:10 +13:00
'entrypoint' => 'index.php' ,
2021-09-28 20:51:09 +13:00
'code' => $curlFile ,
]);
$counter ++ ;
$id = $largeTag [ 'body' ][ '$id' ];
}
@ fclose ( $handle );
$this -> assertEquals ( 201 , $largeTag [ 'headers' ][ 'status-code' ]);
$this -> assertNotEmpty ( $largeTag [ 'body' ][ '$id' ]);
$this -> assertIsInt ( $largeTag [ 'body' ][ 'dateCreated' ]);
2022-02-19 00:16:10 +13:00
$this -> assertEquals ( 'index.php' , $largeTag [ 'body' ][ 'entrypoint' ]);
2021-09-28 20:51:09 +13:00
$this -> assertGreaterThan ( 10000 , $largeTag [ 'body' ][ 'size' ]);
2022-04-14 00:39:31 +12:00
2022-02-23 19:07:15 +13:00
return $data ;
2020-12-11 06:47:32 +13:00
}
/**
2022-01-25 12:46:13 +13:00
* @ depends testCreateDeployment
2020-12-11 06:47:32 +13:00
*/
2022-04-14 00:39:31 +12:00
public function testUpdateDeployment ( $data ) : array
2020-12-11 06:47:32 +13:00
{
/**
* Test for SUCCESS
*/
2022-04-14 00:39:31 +12:00
$response = $this -> client -> call ( Client :: METHOD_PATCH , '/functions/' . $data [ 'functionId' ] . '/deployments/' . $data [ 'deploymentId' ], array_merge ([
2020-12-11 06:47:32 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2022-02-15 22:16:32 +13:00
], $this -> getHeaders ()), []);
2020-12-11 06:47:32 +13:00
$this -> assertEquals ( 200 , $response [ 'headers' ][ 'status-code' ]);
$this -> assertNotEmpty ( $response [ 'body' ][ '$id' ]);
$this -> assertIsInt ( $response [ 'body' ][ 'dateCreated' ]);
$this -> assertIsInt ( $response [ 'body' ][ 'dateUpdated' ]);
2022-01-25 12:46:13 +13:00
$this -> assertEquals ( $data [ 'deploymentId' ], $response [ 'body' ][ 'deployment' ]);
2022-04-14 00:39:31 +12:00
2020-12-11 06:47:32 +13:00
/**
* Test for FAILURE
*/
return $data ;
}
/**
2022-01-25 12:46:13 +13:00
* @ depends testCreateDeployment
2020-12-11 06:47:32 +13:00
*/
2022-04-14 00:39:31 +12:00
public function testListDeployments ( array $data ) : array
2020-12-11 06:47:32 +13:00
{
/**
* Test for SUCCESS
*/
2022-04-14 00:39:31 +12:00
$function = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $data [ 'functionId' ] . '/deployments' , array_merge ([
2020-12-11 06:47:32 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
$this -> assertEquals ( $function [ 'headers' ][ 'status-code' ], 200 );
2022-02-27 22:57:09 +13:00
$this -> assertEquals ( $function [ 'body' ][ 'total' ], 2 );
2022-01-25 12:46:13 +13:00
$this -> assertIsArray ( $function [ 'body' ][ 'deployments' ]);
2022-02-19 00:16:10 +13:00
$this -> assertCount ( 2 , $function [ 'body' ][ 'deployments' ]);
2020-12-11 06:47:32 +13:00
2021-09-23 19:01:10 +12:00
/**
* Test search queries
*/
2022-04-14 00:39:31 +12:00
$function = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $data [ 'functionId' ] . '/deployments' , array_merge ([
2021-09-21 20:22:13 +12: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 22:57:09 +13:00
$this -> assertEquals ( 2 , $function [ 'body' ][ 'total' ]);
2022-01-25 12:46:13 +13:00
$this -> assertIsArray ( $function [ 'body' ][ 'deployments' ]);
2022-02-19 00:16:10 +13:00
$this -> assertCount ( 2 , $function [ 'body' ][ 'deployments' ]);
2022-01-25 12:46:13 +13:00
$this -> assertEquals ( $function [ 'body' ][ 'deployments' ][ 0 ][ '$id' ], $data [ 'deploymentId' ]);
2021-09-21 20:22:13 +12:00
2022-04-14 00:39:31 +12:00
$function = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $data [ 'functionId' ] . '/deployments' , array_merge ([
2021-09-21 20:22:13 +12: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 22:57:09 +13:00
$this -> assertEquals ( 2 , $function [ 'body' ][ 'total' ]);
2022-01-25 12:46:13 +13:00
$this -> assertIsArray ( $function [ 'body' ][ 'deployments' ]);
2022-02-19 00:16:10 +13:00
$this -> assertCount ( 2 , $function [ 'body' ][ 'deployments' ]);
2022-01-25 12:46:13 +13:00
$this -> assertEquals ( $function [ 'body' ][ 'deployments' ][ 0 ][ '$id' ], $data [ 'deploymentId' ]);
2021-09-21 20:22:13 +12:00
2022-04-14 00:39:31 +12:00
$function = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $data [ 'functionId' ] . '/deployments' , array_merge ([
2021-09-21 20:22:13 +12: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 22:57:09 +13:00
$this -> assertEquals ( 2 , $function [ 'body' ][ 'total' ]);
2022-01-25 12:46:13 +13:00
$this -> assertIsArray ( $function [ 'body' ][ 'deployments' ]);
2022-02-19 00:16:10 +13:00
$this -> assertCount ( 2 , $function [ 'body' ][ 'deployments' ]);
2022-01-25 12:46:13 +13:00
$this -> assertEquals ( $function [ 'body' ][ 'deployments' ][ 0 ][ '$id' ], $data [ 'deploymentId' ]);
2021-09-21 20:22:13 +12:00
2020-12-11 06:47:32 +13:00
return $data ;
}
/**
2022-01-25 12:46:13 +13:00
* @ depends testCreateDeployment
2020-12-11 06:47:32 +13:00
*/
2022-04-14 00:39:31 +12:00
public function testGetDeployment ( array $data ) : array
2020-12-11 06:47:32 +13:00
{
/**
* Test for SUCCESS
*/
2022-04-14 00:39:31 +12:00
$function = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $data [ 'functionId' ] . '/deployments/' . $data [ 'deploymentId' ], array_merge ([
2020-12-11 06:47:32 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
$this -> assertEquals ( 200 , $function [ 'headers' ][ 'status-code' ]);
/**
* Test for FAILURE
*/
2022-04-14 00:39:31 +12:00
$function = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $data [ 'functionId' ] . '/deployments/x' , array_merge ([
2020-12-11 06:47:32 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
$this -> assertEquals ( $function [ 'headers' ][ 'status-code' ], 404 );
return $data ;
}
/**
2022-01-25 12:46:13 +13:00
* @ depends testUpdateDeployment
2020-12-11 06:47:32 +13:00
*/
2022-04-14 00:39:31 +12:00
public function testCreateExecution ( $data ) : array
2020-12-11 06:47:32 +13:00
{
/**
* Test for SUCCESS
*/
2022-04-14 00:39:31 +12:00
$execution = $this -> client -> call ( Client :: METHOD_POST , '/functions/' . $data [ 'functionId' ] . '/executions' , array_merge ([
2020-12-11 06:47:32 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2021-12-07 04:04:00 +13:00
'async' => true ,
2020-12-11 06:47:32 +13:00
]);
$executionId = $execution [ 'body' ][ '$id' ] ? ? '' ;
$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' ]);
2021-12-10 23:43:56 +13:00
$this -> assertEquals ( 0 , $execution [ 'body' ][ 'statusCode' ]);
2022-04-25 21:12:58 +12:00
$this -> assertEquals ( '' , $execution [ 'body' ][ 'response' ]);
2020-12-11 06:47:32 +13:00
$this -> assertEquals ( '' , $execution [ 'body' ][ 'stderr' ]);
$this -> assertEquals ( 0 , $execution [ 'body' ][ 'time' ]);
2022-02-01 12:44:55 +13:00
sleep ( 5 );
2021-04-24 03:05:17 +12:00
2022-04-14 00:39:31 +12:00
$execution = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $data [ 'functionId' ] . '/executions/' . $executionId , array_merge ([
2021-04-24 03:05:17 +12:00
'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' ]);
2021-12-10 23:43:56 +13:00
$this -> assertEquals ( 200 , $execution [ 'body' ][ 'statusCode' ]);
2022-04-25 21:12:58 +12:00
$this -> assertStringContainsString ( $execution [ 'body' ][ 'functionId' ], $execution [ 'body' ][ 'response' ]);
$this -> assertStringContainsString ( $data [ 'deploymentId' ], $execution [ 'body' ][ 'response' ]);
$this -> assertStringContainsString ( 'Test1' , $execution [ 'body' ][ 'response' ]);
$this -> assertStringContainsString ( 'http' , $execution [ 'body' ][ 'response' ]);
$this -> assertStringContainsString ( 'PHP' , $execution [ 'body' ][ 'response' ]);
$this -> assertStringContainsString ( '8.0' , $execution [ 'body' ][ 'response' ]);
2022-05-13 06:41:04 +12:00
$this -> assertStringContainsString ( 'êä' , $execution [ 'body' ][ 'response' ]); // tests unknown utf-8 chars
2021-04-24 03:05:17 +12:00
$this -> assertEquals ( '' , $execution [ 'body' ][ 'stderr' ]);
$this -> assertLessThan ( 0.500 , $execution [ 'body' ][ 'time' ]);
2020-12-11 06:47:32 +13:00
/**
* Test for FAILURE
*/
2020-12-12 01:17:30 +13:00
sleep ( 10 );
2020-12-11 06:47:32 +13:00
return array_merge ( $data , [ 'executionId' => $executionId ]);
}
/**
* @ depends testCreateExecution
*/
2022-04-14 00:39:31 +12:00
public function testListExecutions ( array $data ) : array
2020-12-11 06:47:32 +13:00
{
/**
* Test for SUCCESS
*/
2022-04-14 00:39:31 +12:00
$function = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $data [ 'functionId' ] . '/executions' , array_merge ([
2020-12-11 06:47:32 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
$this -> assertEquals ( $function [ 'headers' ][ 'status-code' ], 200 );
2022-02-27 22:57:09 +13:00
$this -> assertEquals ( $function [ 'body' ][ 'total' ], 1 );
2020-12-11 06:47:32 +13:00
$this -> assertIsArray ( $function [ 'body' ][ 'executions' ]);
$this -> assertCount ( 1 , $function [ 'body' ][ 'executions' ]);
$this -> assertEquals ( $function [ 'body' ][ 'executions' ][ 0 ][ '$id' ], $data [ 'executionId' ]);
2021-09-27 23:12:42 +13:00
/**
* Test search queries
*/
2022-04-14 00:39:31 +12:00
$response = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $data [ 'functionId' ] . '/executions' , array_merge ([
2021-09-27 23:12:42 +13: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 22:57:09 +13:00
$this -> assertEquals ( 1 , $response [ 'body' ][ 'total' ]);
$this -> assertIsInt ( $response [ 'body' ][ 'total' ]);
2021-09-27 23:12:42 +13:00
$this -> assertCount ( 1 , $response [ 'body' ][ 'executions' ]);
$this -> assertEquals ( $data [ 'functionId' ], $response [ 'body' ][ 'executions' ][ 0 ][ 'functionId' ]);
2022-04-14 00:39:31 +12:00
$response = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $data [ 'functionId' ] . '/executions' , array_merge ([
2021-09-27 23:12:42 +13: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 22:57:09 +13:00
$this -> assertEquals ( 1 , $response [ 'body' ][ 'total' ]);
$this -> assertIsInt ( $response [ 'body' ][ 'total' ]);
2021-09-27 23:12:42 +13:00
$this -> assertCount ( 1 , $response [ 'body' ][ 'executions' ]);
$this -> assertEquals ( $data [ 'executionId' ], $response [ 'body' ][ 'executions' ][ 0 ][ '$id' ]);
2020-12-11 06:47:32 +13:00
return $data ;
}
2021-11-24 23:01:43 +13:00
/**
2022-01-25 12:46:13 +13:00
* @ depends testUpdateDeployment
2021-11-24 23:01:43 +13:00
*/
2022-04-14 00:39:31 +12:00
public function testSyncCreateExecution ( $data ) : array
2021-11-24 23:01:43 +13:00
{
/**
* Test for SUCCESS
*/
2022-04-14 00:39:31 +12:00
$execution = $this -> client -> call ( Client :: METHOD_POST , '/functions/' . $data [ 'functionId' ] . '/executions' , array_merge ([
2021-11-24 23:01:43 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2021-12-07 04:04:00 +13:00
'async' => false ,
2021-11-24 23:01:43 +13:00
]);
2022-02-28 10:09:13 +13:00
$this -> assertEquals ( 201 , $execution [ 'headers' ][ 'status-code' ]);
2021-11-24 23:01:43 +13:00
$this -> assertEquals ( 'completed' , $execution [ 'body' ][ 'status' ]);
2022-04-25 21:12:58 +12:00
$this -> assertStringContainsString ( $data [ 'deploymentId' ], $execution [ 'body' ][ 'response' ]);
$this -> assertStringContainsString ( 'Test1' , $execution [ 'body' ][ 'response' ]);
$this -> assertStringContainsString ( 'http' , $execution [ 'body' ][ 'response' ]);
$this -> assertStringContainsString ( 'PHP' , $execution [ 'body' ][ 'response' ]);
$this -> assertStringContainsString ( '8.0' , $execution [ 'body' ][ 'response' ]);
2022-05-13 06:41:04 +12:00
$this -> assertStringContainsString ( 'êä' , $execution [ 'body' ][ 'response' ]); // tests unknown utf-8 chars
2021-11-24 23:01:43 +13:00
$this -> assertLessThan ( 0.500 , $execution [ 'body' ][ 'time' ]);
return $data ;
}
2020-12-11 06:47:32 +13:00
/**
* @ depends testListExecutions
*/
2022-04-14 00:39:31 +12:00
public function testGetExecution ( array $data ) : array
2020-12-11 06:47:32 +13:00
{
/**
* Test for SUCCESS
*/
2022-04-14 00:39:31 +12:00
$function = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $data [ 'functionId' ] . '/executions/' . $data [ 'executionId' ], array_merge ([
2020-12-11 06:47:32 +13: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-14 00:39:31 +12:00
$function = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $data [ 'functionId' ] . '/executions/x' , array_merge ([
2020-12-11 06:47:32 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
$this -> assertEquals ( $function [ 'headers' ][ 'status-code' ], 404 );
return $data ;
}
/**
* @ depends testGetExecution
*/
2022-04-14 00:39:31 +12:00
public function testDeleteDeployment ( $data ) : array
2020-12-11 06:47:32 +13:00
{
/**
* Test for SUCCESS
*/
2022-04-14 00:39:31 +12:00
$function = $this -> client -> call ( Client :: METHOD_DELETE , '/functions/' . $data [ 'functionId' ] . '/deployments/' . $data [ 'deploymentId' ], array_merge ([
2020-12-11 06:47:32 +13: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-14 00:39:31 +12:00
$function = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $data [ 'functionId' ] . '/deployments/' . $data [ 'deploymentId' ], array_merge ([
2020-12-11 06:47:32 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
2022-04-14 00:39:31 +12:00
2020-12-11 06:47:32 +13:00
$this -> assertEquals ( 404 , $function [ 'headers' ][ 'status-code' ]);
/**
* Test for FAILURE
*/
return $data ;
}
/**
2022-01-25 12:46:13 +13:00
* @ depends testCreateDeployment
2020-12-11 06:47:32 +13:00
*/
2022-04-14 00:39:31 +12:00
public function testDelete ( $data ) : array
2020-12-11 06:47:32 +13:00
{
/**
* Test for SUCCESS
*/
2022-04-14 00:39:31 +12:00
$function = $this -> client -> call ( Client :: METHOD_DELETE , '/functions/' . $data [ 'functionId' ], array_merge ([
2020-12-11 06:47:32 +13: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 13:54:47 +13: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-14 00:39:31 +12:00
2022-01-29 13:54:47 +13:00
$this -> assertEquals ( 404 , $function [ 'headers' ][ 'status-code' ]);
2020-12-11 06:47:32 +13:00
/**
* Test for FAILURE
*/
return $data ;
}
2020-12-12 18:42:29 +13:00
public function testTimeout ()
{
$name = 'php-8.0' ;
2021-09-06 12:37:20 +12:00
$entrypoint = 'index.php' ;
2020-12-12 18:42:29 +13:00
$timeout = 2 ;
2022-02-20 02:57:48 +13:00
$folder = 'timeout' ;
2022-04-14 00:39:31 +12:00
$code = realpath ( __DIR__ . '/../../../resources/functions' ) . " / $folder /code.tar.gz " ;
2022-02-20 02:57:48 +13:00
$this -> packageCode ( $folder );
2022-04-14 00:39:31 +12:00
2020-12-12 18:42:29 +13:00
$function = $this -> client -> call ( Client :: METHOD_POST , '/functions' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2021-07-30 18:02:59 +12:00
'functionId' => 'unique()' ,
2022-04-14 00:39:31 +12:00
'name' => 'Test ' . $name ,
2021-06-22 02:42:39 +12:00
'runtime' => $name ,
2020-12-12 18:42:29 +13:00
'vars' => [],
'events' => [],
'schedule' => '' ,
'timeout' => $timeout ,
]);
$functionId = $function [ 'body' ][ '$id' ] ? ? '' ;
$this -> assertEquals ( 201 , $function [ 'headers' ][ 'status-code' ]);
2022-04-14 00:39:31 +12:00
$deployment = $this -> client -> call ( Client :: METHOD_POST , '/functions/' . $functionId . '/deployments' , array_merge ([
2020-12-12 18:42:29 +13:00
'content-type' => 'multipart/form-data' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2021-09-01 21:48:56 +12:00
'entrypoint' => $entrypoint ,
2021-02-01 08:11:03 +13:00
'code' => new CURLFile ( $code , 'application/x-gzip' , basename ( $code )),
2022-02-20 05:14:31 +13:00
'activate' => true ,
2020-12-12 18:42:29 +13:00
]);
2022-01-25 12:46:13 +13:00
$this -> assertEquals ( 201 , $deployment [ 'headers' ][ 'status-code' ]);
2020-12-12 18:42:29 +13:00
2021-12-10 23:43:56 +13:00
// Allow build step to run
2022-03-01 05:40:05 +13:00
sleep ( 20 );
2022-04-14 00:39:31 +12:00
$execution = $this -> client -> call ( Client :: METHOD_POST , '/functions/' . $functionId . '/executions' , array_merge ([
2020-12-12 18:42:29 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2021-12-07 04:04:00 +13:00
'async' => true ,
2020-12-12 18:42:29 +13:00
]);
$executionId = $execution [ 'body' ][ '$id' ] ? ? '' ;
2022-04-14 00:39:31 +12:00
2020-12-12 18:42:29 +13:00
$this -> assertEquals ( 201 , $execution [ 'headers' ][ 'status-code' ]);
2022-05-13 05:55:48 +12:00
sleep ( 10 );
2020-12-12 18:42:29 +13:00
2022-04-14 00:39:31 +12:00
$executions = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $functionId . '/executions' , array_merge ([
2020-12-12 18:42:29 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
$this -> assertEquals ( $executions [ 'headers' ][ 'status-code' ], 200 );
2022-02-27 22:57:09 +13:00
$this -> assertEquals ( $executions [ 'body' ][ 'total' ], 1 );
2020-12-12 18:42:29 +13: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' );
2022-03-16 02:06:05 +13:00
$this -> assertEquals ( $executions [ 'body' ][ 'executions' ][ 0 ][ 'statusCode' ], 500 );
2020-12-12 18:42:29 +13:00
$this -> assertGreaterThan ( 2 , $executions [ 'body' ][ 'executions' ][ 0 ][ 'time' ]);
2022-05-31 22:30:30 +12:00
$this -> assertLessThan ( 6 , $executions [ 'body' ][ 'executions' ][ 0 ][ 'time' ]);
$this -> assertGreaterThan ( 4 , $executions [ 'body' ][ 'executions' ][ 0 ][ 'time' ]);
2022-05-13 05:37:15 +12:00
$this -> assertEquals ( $executions [ 'body' ][ 'executions' ][ 0 ][ 'response' ], '' );
2022-03-16 03:51:32 +13:00
$this -> assertEquals ( $executions [ 'body' ][ 'executions' ][ 0 ][ 'stderr' ], 'An internal curl error has occurred within the executor! Error Msg: Operation timed out' );
2022-01-29 13:00:25 +13:00
2022-05-17 01:05:58 +12:00
// Cleanup : Delete function
2022-04-14 00:39:31 +12:00
$response = $this -> client -> call ( Client :: METHOD_DELETE , '/functions/' . $functionId , [
2022-01-29 13:00:25 +13: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 18:42:29 +13:00
}
2021-03-11 08:43:15 +13:00
/**
* @ depends testTimeout
*/
2022-02-20 02:57:48 +13:00
public function testCreateCustomPHPExecution ()
2021-03-11 08:43:15 +13:00
{
$name = 'php-8.0' ;
2021-09-01 21:48:56 +12:00
$entrypoint = 'index.php' ;
2021-03-11 08:43:15 +13:00
$timeout = 2 ;
2022-02-20 02:57:48 +13:00
$folder = 'php-fn' ;
2022-04-14 00:39:31 +12:00
$code = realpath ( __DIR__ . '/../../../resources/functions' ) . " / $folder /code.tar.gz " ;
2022-02-20 02:57:48 +13:00
$this -> packageCode ( $folder );
2021-03-11 08:43:15 +13:00
$function = $this -> client -> call ( Client :: METHOD_POST , '/functions' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2021-07-19 20:24:32 +12:00
'functionId' => 'unique()' ,
2022-04-14 00:39:31 +12:00
'name' => 'Test ' . $name ,
2021-06-22 02:42:39 +12:00
'runtime' => $name ,
2021-03-11 08:43:15 +13:00
'vars' => [],
'events' => [],
'schedule' => '' ,
'timeout' => $timeout ,
]);
$functionId = $function [ 'body' ][ '$id' ] ? ? '' ;
$this -> assertEquals ( 201 , $function [ 'headers' ][ 'status-code' ]);
2022-04-14 00:39:31 +12:00
$deployment = $this -> client -> call ( Client :: METHOD_POST , '/functions/' . $functionId . '/deployments' , array_merge ([
2021-03-11 08:43:15 +13:00
'content-type' => 'multipart/form-data' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2021-09-01 21:48:56 +12:00
'entrypoint' => $entrypoint ,
2021-03-11 08:43:15 +13:00
'code' => new CURLFile ( $code , 'application/x-gzip' , basename ( $code )),
]);
2022-01-25 12:46:13 +13:00
$deploymentId = $deployment [ 'body' ][ '$id' ] ? ? '' ;
$this -> assertEquals ( 201 , $deployment [ 'headers' ][ 'status-code' ]);
2021-03-11 08:43:15 +13:00
2021-12-10 23:43:56 +13:00
// Allow build step to run
2022-02-01 12:44:55 +13:00
sleep ( 10 );
2021-12-10 23:43:56 +13:00
2022-04-14 00:39:31 +12:00
$deployment = $this -> client -> call ( Client :: METHOD_PATCH , '/functions/' . $functionId . '/deployments/' . $deploymentId , array_merge ([
2021-03-11 08:43:15 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
2022-02-15 22:16:32 +13:00
], $this -> getHeaders ()), []);
2021-03-11 08:43:15 +13:00
2022-01-25 12:46:13 +13:00
$this -> assertEquals ( 200 , $deployment [ 'headers' ][ 'status-code' ]);
2022-04-14 00:39:31 +12:00
$execution = $this -> client -> call ( Client :: METHOD_POST , '/functions/' . $functionId . '/executions' , array_merge ([
2021-03-11 08:43:15 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'data' => 'foobar' ,
]);
$executionId = $execution [ 'body' ][ '$id' ] ? ? '' ;
2022-04-14 00:39:31 +12:00
2021-03-11 08:43:15 +13:00
$this -> assertEquals ( 201 , $execution [ 'headers' ][ 'status-code' ]);
2021-07-23 02:49:52 +12:00
$executionId = $execution [ 'body' ][ '$id' ] ? ? '' ;
2022-04-14 00:39:31 +12:00
2021-03-11 08:43:15 +13:00
sleep ( 10 );
2022-04-14 00:39:31 +12:00
$executions = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $functionId . '/executions/' . $executionId , array_merge ([
2021-07-23 02:49:52 +12:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
2022-04-25 21:12:58 +12:00
$output = json_decode ( $executions [ 'body' ][ 'response' ], true );
2021-07-23 02:49:52 +12:00
$this -> assertEquals ( 200 , $executions [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'completed' , $executions [ 'body' ][ 'status' ]);
$this -> assertEquals ( $functionId , $output [ 'APPWRITE_FUNCTION_ID' ]);
2022-04-14 00:39:31 +12:00
$this -> assertEquals ( 'Test ' . $name , $output [ 'APPWRITE_FUNCTION_NAME' ]);
2022-01-25 13:36:33 +13:00
$this -> assertEquals ( $deploymentId , $output [ 'APPWRITE_FUNCTION_DEPLOYMENT' ]);
2021-07-23 02:49:52 +12:00
$this -> assertEquals ( 'http' , $output [ 'APPWRITE_FUNCTION_TRIGGER' ]);
$this -> assertEquals ( 'PHP' , $output [ 'APPWRITE_FUNCTION_RUNTIME_NAME' ]);
$this -> assertEquals ( '8.0' , $output [ 'APPWRITE_FUNCTION_RUNTIME_VERSION' ]);
$this -> assertEquals ( '' , $output [ 'APPWRITE_FUNCTION_EVENT' ]);
$this -> assertEquals ( '' , $output [ 'APPWRITE_FUNCTION_EVENT_DATA' ]);
$this -> assertEquals ( 'foobar' , $output [ 'APPWRITE_FUNCTION_DATA' ]);
$this -> assertEquals ( '' , $output [ 'APPWRITE_FUNCTION_USER_ID' ]);
$this -> assertEmpty ( $output [ 'APPWRITE_FUNCTION_JWT' ]);
2021-09-03 23:41:42 +12:00
$this -> assertEquals ( $this -> getProject ()[ '$id' ], $output [ 'APPWRITE_FUNCTION_PROJECT_ID' ]);
2021-07-23 02:49:52 +12:00
2022-04-14 00:39:31 +12:00
$executions = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $functionId . '/executions' , array_merge ([
2021-03-11 08:43:15 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
2022-04-14 00:39:31 +12:00
2021-03-11 08:43:15 +13:00
$this -> assertEquals ( $executions [ 'headers' ][ 'status-code' ], 200 );
2022-02-27 22:57:09 +13:00
$this -> assertEquals ( $executions [ 'body' ][ 'total' ], 1 );
2021-03-11 08:43:15 +13: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' );
2022-04-25 21:12:58 +12:00
$this -> assertStringContainsString ( 'foobar' , $executions [ 'body' ][ 'executions' ][ 0 ][ 'response' ]);
2022-01-29 13:00:25 +13:00
2022-05-24 02:54:50 +12:00
// Cleanup : Delete function
2022-04-14 00:39:31 +12:00
$response = $this -> client -> call ( Client :: METHOD_DELETE , '/functions/' . $functionId , [
2022-01-29 13:00:25 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ],
], []);
$this -> assertEquals ( 204 , $response [ 'headers' ][ 'status-code' ]);
2021-03-11 08:43:15 +13:00
}
2021-12-10 02:02:12 +13:00
2022-02-20 05:14:31 +13:00
2022-02-20 02:57:48 +13:00
public function testCreateCustomNodeExecution ()
2021-12-10 02:02:12 +13:00
{
2022-02-20 02:57:48 +13:00
$name = 'node-17.0' ;
$folder = 'node' ;
2022-04-14 00:39:31 +12:00
$code = realpath ( __DIR__ . '/../../../resources/functions' ) . " / $folder /code.tar.gz " ;
2022-02-20 02:57:48 +13:00
$this -> packageCode ( $folder );
2022-04-14 00:39:31 +12:00
2022-02-20 02:57:48 +13:00
$entrypoint = 'index.js' ;
$timeout = 2 ;
$function = $this -> client -> call ( Client :: METHOD_POST , '/functions' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'functionId' => 'unique()' ,
2022-04-14 00:39:31 +12:00
'name' => 'Test ' . $name ,
2022-02-20 02:57:48 +13:00
'runtime' => $name ,
2022-02-20 04:14:35 +13:00
'vars' => [
'CUSTOM_VARIABLE' => 'variable' ,
],
2022-02-20 02:57:48 +13:00
'events' => [],
'schedule' => '' ,
'timeout' => $timeout ,
]);
$functionId = $function [ 'body' ][ '$id' ] ? ? '' ;
$this -> assertEquals ( 201 , $function [ 'headers' ][ 'status-code' ]);
2022-04-14 00:39:31 +12:00
$deployment = $this -> client -> call ( Client :: METHOD_POST , '/functions/' . $functionId . '/deployments' , array_merge ([
2022-02-20 02:57:48 +13: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 )),
2022-02-20 05:14:31 +13:00
'activate' => true ,
2022-02-20 02:57:48 +13:00
]);
$deploymentId = $deployment [ 'body' ][ '$id' ] ? ? '' ;
$this -> assertEquals ( 201 , $deployment [ 'headers' ][ 'status-code' ]);
// Allow build step to run
sleep ( 10 );
2021-12-10 02:02:12 +13:00
2022-04-14 00:39:31 +12:00
$execution = $this -> client -> call ( Client :: METHOD_POST , '/functions/' . $functionId . '/executions' , array_merge ([
2022-02-20 02:57:48 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'data' => 'foobar' ,
]);
$executionId = $execution [ 'body' ][ '$id' ] ? ? '' ;
2022-04-14 00:39:31 +12:00
2022-02-20 02:57:48 +13:00
$this -> assertEquals ( 201 , $execution [ 'headers' ][ 'status-code' ]);
$executionId = $execution [ 'body' ][ '$id' ] ? ? '' ;
2022-04-14 00:39:31 +12:00
2022-02-20 02:57:48 +13:00
sleep ( 10 );
2022-04-14 00:39:31 +12:00
$executions = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $functionId . '/executions/' . $executionId , array_merge ([
2022-02-20 02:57:48 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
2022-04-25 21:12:58 +12:00
$output = json_decode ( $executions [ 'body' ][ 'response' ], true );
2022-02-20 02:57:48 +13:00
$this -> assertEquals ( 200 , $executions [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'completed' , $executions [ 'body' ][ 'status' ]);
$this -> assertEquals ( $functionId , $output [ 'APPWRITE_FUNCTION_ID' ]);
2022-04-14 00:39:31 +12:00
$this -> assertEquals ( 'Test ' . $name , $output [ 'APPWRITE_FUNCTION_NAME' ]);
2022-02-20 02:57:48 +13:00
$this -> assertEquals ( $deploymentId , $output [ 'APPWRITE_FUNCTION_DEPLOYMENT' ]);
$this -> assertEquals ( 'http' , $output [ 'APPWRITE_FUNCTION_TRIGGER' ]);
$this -> assertEquals ( 'Node.js' , $output [ 'APPWRITE_FUNCTION_RUNTIME_NAME' ]);
$this -> assertEquals ( '17.0' , $output [ 'APPWRITE_FUNCTION_RUNTIME_VERSION' ]);
$this -> assertEquals ( '' , $output [ 'APPWRITE_FUNCTION_EVENT' ]);
$this -> assertEquals ( '' , $output [ 'APPWRITE_FUNCTION_EVENT_DATA' ]);
$this -> assertEquals ( 'foobar' , $output [ 'APPWRITE_FUNCTION_DATA' ]);
2022-02-20 04:14:35 +13:00
$this -> assertEquals ( 'variable' , $output [ 'CUSTOM_VARIABLE' ]);
2022-02-20 02:57:48 +13:00
$this -> assertEquals ( '' , $output [ 'APPWRITE_FUNCTION_USER_ID' ]);
$this -> assertEmpty ( $output [ 'APPWRITE_FUNCTION_JWT' ]);
$this -> assertEquals ( $this -> getProject ()[ '$id' ], $output [ 'APPWRITE_FUNCTION_PROJECT_ID' ]);
2022-04-14 00:39:31 +12:00
$executions = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $functionId . '/executions' , array_merge ([
2022-02-24 03:39:09 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
2022-04-14 00:39:31 +12:00
2022-02-24 03:39:09 +13:00
$this -> assertEquals ( $executions [ 'headers' ][ 'status-code' ], 200 );
2022-02-27 22:57:09 +13:00
$this -> assertEquals ( $executions [ 'body' ][ 'total' ], 1 );
2022-02-24 03:39:09 +13: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' );
2022-04-25 21:12:58 +12:00
$this -> assertStringContainsString ( 'foobar' , $executions [ 'body' ][ 'executions' ][ 0 ][ 'response' ]);
2022-02-24 03:39:09 +13:00
2022-05-24 02:54:50 +12:00
// Cleanup : Delete function
2022-04-14 00:39:31 +12:00
$response = $this -> client -> call ( Client :: METHOD_DELETE , '/functions/' . $functionId , [
2022-02-24 03:39:09 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ],
], []);
$this -> assertEquals ( 204 , $response [ 'headers' ][ 'status-code' ]);
}
public function testCreateCustomPythonExecution ()
{
$name = 'python-3.9' ;
$folder = 'python' ;
2022-04-14 00:39:31 +12:00
$code = realpath ( __DIR__ . '/../../../resources/functions' ) . " / $folder /code.tar.gz " ;
2022-02-24 03:39:09 +13:00
$this -> packageCode ( $folder );
2022-03-01 04:43:00 +13:00
2022-02-24 03:39:09 +13:00
$entrypoint = 'main.py' ;
$timeout = 2 ;
$function = $this -> client -> call ( Client :: METHOD_POST , '/functions' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'functionId' => 'unique()' ,
2022-04-14 00:39:31 +12:00
'name' => 'Test ' . $name ,
2022-02-24 03:39:09 +13:00
'runtime' => $name ,
'vars' => [
'CUSTOM_VARIABLE' => 'variable' ,
],
'events' => [],
'schedule' => '' ,
'timeout' => $timeout ,
]);
$functionId = $function [ 'body' ][ '$id' ] ? ? '' ;
$this -> assertEquals ( 201 , $function [ 'headers' ][ 'status-code' ]);
2022-04-14 00:39:31 +12:00
$deployment = $this -> client -> call ( Client :: METHOD_POST , '/functions/' . $functionId . '/deployments' , array_merge ([
2022-02-24 03:39:09 +13: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 )),
'activate' => true ,
]);
$deploymentId = $deployment [ 'body' ][ '$id' ] ? ? '' ;
$this -> assertEquals ( 201 , $deployment [ 'headers' ][ 'status-code' ]);
// Allow build step to run
2022-03-01 06:03:52 +13:00
sleep ( 30 );
2022-02-24 03:39:09 +13:00
2022-04-14 00:39:31 +12:00
$execution = $this -> client -> call ( Client :: METHOD_POST , '/functions/' . $functionId . '/executions' , array_merge ([
2022-02-24 03:39:09 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'data' => 'foobar' ,
]);
$executionId = $execution [ 'body' ][ '$id' ] ? ? '' ;
2022-04-14 00:39:31 +12:00
2022-02-24 03:39:09 +13:00
$this -> assertEquals ( 201 , $execution [ 'headers' ][ 'status-code' ]);
$executionId = $execution [ 'body' ][ '$id' ] ? ? '' ;
2022-04-14 00:39:31 +12:00
2022-03-01 04:43:00 +13:00
sleep ( 30 );
2022-02-24 03:39:09 +13:00
2022-04-14 00:39:31 +12:00
$executions = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $functionId . '/executions/' . $executionId , array_merge ([
2022-02-24 03:39:09 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
2022-04-25 21:12:58 +12:00
$output = json_decode ( $executions [ 'body' ][ 'response' ], true );
2022-02-24 03:39:09 +13:00
$this -> assertEquals ( 200 , $executions [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'completed' , $executions [ 'body' ][ 'status' ]);
$this -> assertEquals ( $functionId , $output [ 'APPWRITE_FUNCTION_ID' ]);
2022-04-14 00:39:31 +12:00
$this -> assertEquals ( 'Test ' . $name , $output [ 'APPWRITE_FUNCTION_NAME' ]);
2022-02-24 03:39:09 +13:00
$this -> assertEquals ( $deploymentId , $output [ 'APPWRITE_FUNCTION_DEPLOYMENT' ]);
$this -> assertEquals ( 'http' , $output [ 'APPWRITE_FUNCTION_TRIGGER' ]);
$this -> assertEquals ( 'Python' , $output [ 'APPWRITE_FUNCTION_RUNTIME_NAME' ]);
$this -> assertEquals ( '3.9' , $output [ 'APPWRITE_FUNCTION_RUNTIME_VERSION' ]);
$this -> assertEquals ( '' , $output [ 'APPWRITE_FUNCTION_EVENT' ]);
$this -> assertEquals ( '' , $output [ 'APPWRITE_FUNCTION_EVENT_DATA' ]);
$this -> assertEquals ( 'foobar' , $output [ 'APPWRITE_FUNCTION_DATA' ]);
$this -> assertEquals ( 'variable' , $output [ 'CUSTOM_VARIABLE' ]);
$this -> assertEquals ( '' , $output [ 'APPWRITE_FUNCTION_USER_ID' ]);
$this -> assertEmpty ( $output [ 'APPWRITE_FUNCTION_JWT' ]);
$this -> assertEquals ( $this -> getProject ()[ '$id' ], $output [ 'APPWRITE_FUNCTION_PROJECT_ID' ]);
2022-04-14 00:39:31 +12:00
$executions = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $functionId . '/executions' , array_merge ([
2022-02-24 05:22:58 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
2022-04-14 00:39:31 +12:00
2022-02-24 05:22:58 +13:00
$this -> assertEquals ( $executions [ 'headers' ][ 'status-code' ], 200 );
2022-02-27 22:57:09 +13:00
$this -> assertEquals ( $executions [ 'body' ][ 'total' ], 1 );
2022-02-24 05:22:58 +13: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' );
2022-04-25 21:12:58 +12:00
$this -> assertStringContainsString ( 'foobar' , $executions [ 'body' ][ 'executions' ][ 0 ][ 'response' ]);
2022-02-24 05:22:58 +13:00
2022-05-24 02:54:50 +12:00
// Cleanup : Delete function
2022-04-14 00:39:31 +12:00
$response = $this -> client -> call ( Client :: METHOD_DELETE , '/functions/' . $functionId , [
2022-02-24 05:22:58 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ],
], []);
$this -> assertEquals ( 204 , $response [ 'headers' ][ 'status-code' ]);
}
public function testCreateCustomDartExecution ()
{
$name = 'dart-2.15' ;
$folder = 'dart' ;
2022-04-14 00:39:31 +12:00
$code = realpath ( __DIR__ . '/../../../resources/functions' ) . " / $folder /code.tar.gz " ;
2022-02-24 05:22:58 +13:00
$this -> packageCode ( $folder );
2022-04-14 00:39:31 +12:00
2022-02-24 05:22:58 +13:00
$entrypoint = 'main.dart' ;
$timeout = 2 ;
$function = $this -> client -> call ( Client :: METHOD_POST , '/functions' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'functionId' => 'unique()' ,
2022-04-14 00:39:31 +12:00
'name' => 'Test ' . $name ,
2022-02-24 05:22:58 +13:00
'runtime' => $name ,
'vars' => [
'CUSTOM_VARIABLE' => 'variable' ,
],
'events' => [],
'schedule' => '' ,
'timeout' => $timeout ,
]);
$functionId = $function [ 'body' ][ '$id' ] ? ? '' ;
$this -> assertEquals ( 201 , $function [ 'headers' ][ 'status-code' ]);
2022-04-14 00:39:31 +12:00
$deployment = $this -> client -> call ( Client :: METHOD_POST , '/functions/' . $functionId . '/deployments' , array_merge ([
2022-02-24 05:22:58 +13: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 )),
'activate' => true ,
]);
$deploymentId = $deployment [ 'body' ][ '$id' ] ? ? '' ;
$this -> assertEquals ( 201 , $deployment [ 'headers' ][ 'status-code' ]);
// Allow build step to run
2022-02-28 22:55:09 +13:00
sleep ( 40 );
2022-02-24 05:22:58 +13:00
2022-04-14 00:39:31 +12:00
$execution = $this -> client -> call ( Client :: METHOD_POST , '/functions/' . $functionId . '/executions' , array_merge ([
2022-02-24 05:22:58 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'data' => 'foobar' ,
]);
$executionId = $execution [ 'body' ][ '$id' ] ? ? '' ;
2022-04-14 00:39:31 +12:00
2022-02-24 05:22:58 +13:00
$this -> assertEquals ( 201 , $execution [ 'headers' ][ 'status-code' ]);
$executionId = $execution [ 'body' ][ '$id' ] ? ? '' ;
2022-04-14 00:39:31 +12:00
2022-02-24 05:22:58 +13:00
sleep ( 10 );
2022-04-14 00:39:31 +12:00
$executions = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $functionId . '/executions/' . $executionId , array_merge ([
2022-02-24 05:22:58 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
2022-04-25 21:12:58 +12:00
$output = json_decode ( $executions [ 'body' ][ 'response' ], true );
2022-02-24 05:22:58 +13:00
$this -> assertEquals ( 200 , $executions [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'completed' , $executions [ 'body' ][ 'status' ]);
$this -> assertEquals ( $functionId , $output [ 'APPWRITE_FUNCTION_ID' ]);
2022-04-14 00:39:31 +12:00
$this -> assertEquals ( 'Test ' . $name , $output [ 'APPWRITE_FUNCTION_NAME' ]);
2022-02-24 05:22:58 +13:00
$this -> assertEquals ( $deploymentId , $output [ 'APPWRITE_FUNCTION_DEPLOYMENT' ]);
$this -> assertEquals ( 'http' , $output [ 'APPWRITE_FUNCTION_TRIGGER' ]);
$this -> assertEquals ( 'Dart' , $output [ 'APPWRITE_FUNCTION_RUNTIME_NAME' ]);
$this -> assertEquals ( '2.15' , $output [ 'APPWRITE_FUNCTION_RUNTIME_VERSION' ]);
$this -> assertEquals ( '' , $output [ 'APPWRITE_FUNCTION_EVENT' ]);
$this -> assertEquals ( '' , $output [ 'APPWRITE_FUNCTION_EVENT_DATA' ]);
$this -> assertEquals ( 'foobar' , $output [ 'APPWRITE_FUNCTION_DATA' ]);
$this -> assertEquals ( 'variable' , $output [ 'CUSTOM_VARIABLE' ]);
$this -> assertEquals ( '' , $output [ 'APPWRITE_FUNCTION_USER_ID' ]);
$this -> assertEmpty ( $output [ 'APPWRITE_FUNCTION_JWT' ]);
$this -> assertEquals ( $this -> getProject ()[ '$id' ], $output [ 'APPWRITE_FUNCTION_PROJECT_ID' ]);
2022-04-14 00:39:31 +12:00
$executions = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $functionId . '/executions' , array_merge ([
2022-02-24 05:22:58 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
2022-04-14 00:39:31 +12:00
2022-02-24 05:22:58 +13:00
$this -> assertEquals ( $executions [ 'headers' ][ 'status-code' ], 200 );
2022-02-27 22:57:09 +13:00
$this -> assertEquals ( $executions [ 'body' ][ 'total' ], 1 );
2022-02-24 05:22:58 +13: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' );
2022-04-25 21:12:58 +12:00
$this -> assertStringContainsString ( 'foobar' , $executions [ 'body' ][ 'executions' ][ 0 ][ 'response' ]);
2022-02-24 05:22:58 +13:00
2022-05-24 02:54:50 +12:00
// Cleanup : Delete function
2022-04-14 00:39:31 +12:00
$response = $this -> client -> call ( Client :: METHOD_DELETE , '/functions/' . $functionId , [
2022-02-24 05:22:58 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ],
], []);
$this -> assertEquals ( 204 , $response [ 'headers' ][ 'status-code' ]);
}
public function testCreateCustomRubyExecution ()
{
$name = 'ruby-3.1' ;
$folder = 'ruby' ;
2022-04-14 00:39:31 +12:00
$code = realpath ( __DIR__ . '/../../../resources/functions' ) . " / $folder /code.tar.gz " ;
2022-02-24 05:22:58 +13:00
$this -> packageCode ( $folder );
2022-04-14 00:39:31 +12:00
2022-02-24 05:22:58 +13:00
$entrypoint = 'main.rb' ;
$timeout = 2 ;
$function = $this -> client -> call ( Client :: METHOD_POST , '/functions' , array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'functionId' => 'unique()' ,
2022-04-14 00:39:31 +12:00
'name' => 'Test ' . $name ,
2022-02-24 05:22:58 +13:00
'runtime' => $name ,
'vars' => [
'CUSTOM_VARIABLE' => 'variable' ,
],
'events' => [],
'schedule' => '' ,
'timeout' => $timeout ,
]);
$functionId = $function [ 'body' ][ '$id' ] ? ? '' ;
$this -> assertEquals ( 201 , $function [ 'headers' ][ 'status-code' ]);
2022-04-14 00:39:31 +12:00
$deployment = $this -> client -> call ( Client :: METHOD_POST , '/functions/' . $functionId . '/deployments' , array_merge ([
2022-02-24 05:22:58 +13: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 )),
'activate' => true ,
]);
$deploymentId = $deployment [ 'body' ][ '$id' ] ? ? '' ;
$this -> assertEquals ( 201 , $deployment [ 'headers' ][ 'status-code' ]);
// Allow build step to run
sleep ( 30 );
2022-04-14 00:39:31 +12:00
$execution = $this -> client -> call ( Client :: METHOD_POST , '/functions/' . $functionId . '/executions' , array_merge ([
2022-02-24 05:22:58 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'data' => 'foobar' ,
]);
$executionId = $execution [ 'body' ][ '$id' ] ? ? '' ;
2022-04-14 00:39:31 +12:00
2022-02-24 05:22:58 +13:00
$this -> assertEquals ( 201 , $execution [ 'headers' ][ 'status-code' ]);
$executionId = $execution [ 'body' ][ '$id' ] ? ? '' ;
2022-04-14 00:39:31 +12:00
2022-02-24 05:22:58 +13:00
sleep ( 10 );
2022-04-14 00:39:31 +12:00
$executions = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $functionId . '/executions/' . $executionId , array_merge ([
2022-02-24 05:22:58 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
2022-04-25 21:12:58 +12:00
$output = json_decode ( $executions [ 'body' ][ 'response' ], true );
2022-02-24 05:22:58 +13:00
$this -> assertEquals ( 200 , $executions [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'completed' , $executions [ 'body' ][ 'status' ]);
$this -> assertEquals ( $functionId , $output [ 'APPWRITE_FUNCTION_ID' ]);
2022-04-14 00:39:31 +12:00
$this -> assertEquals ( 'Test ' . $name , $output [ 'APPWRITE_FUNCTION_NAME' ]);
2022-02-24 05:22:58 +13:00
$this -> assertEquals ( $deploymentId , $output [ 'APPWRITE_FUNCTION_DEPLOYMENT' ]);
$this -> assertEquals ( 'http' , $output [ 'APPWRITE_FUNCTION_TRIGGER' ]);
$this -> assertEquals ( 'Ruby' , $output [ 'APPWRITE_FUNCTION_RUNTIME_NAME' ]);
$this -> assertEquals ( '3.1' , $output [ 'APPWRITE_FUNCTION_RUNTIME_VERSION' ]);
$this -> assertEquals ( '' , $output [ 'APPWRITE_FUNCTION_EVENT' ]);
$this -> assertEquals ( '' , $output [ 'APPWRITE_FUNCTION_EVENT_DATA' ]);
$this -> assertEquals ( 'foobar' , $output [ 'APPWRITE_FUNCTION_DATA' ]);
$this -> assertEquals ( 'variable' , $output [ 'CUSTOM_VARIABLE' ]);
$this -> assertEquals ( '' , $output [ 'APPWRITE_FUNCTION_USER_ID' ]);
$this -> assertEmpty ( $output [ 'APPWRITE_FUNCTION_JWT' ]);
$this -> assertEquals ( $this -> getProject ()[ '$id' ], $output [ 'APPWRITE_FUNCTION_PROJECT_ID' ]);
2022-04-14 00:39:31 +12:00
$executions = $this -> client -> call ( Client :: METHOD_GET , '/functions/' . $functionId . '/executions' , array_merge ([
2022-02-24 18:57:25 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
2022-04-14 00:39:31 +12:00
2022-02-24 18:57:25 +13:00
$this -> assertEquals ( $executions [ 'headers' ][ 'status-code' ], 200 );
2022-02-27 22:57:09 +13:00
$this -> assertEquals ( $executions [ 'body' ][ 'total' ], 1 );
2022-02-24 18:57:25 +13: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' );
2022-04-25 21:12:58 +12:00
$this -> assertStringContainsString ( 'foobar' , $executions [ 'body' ][ 'executions' ][ 0 ][ 'response' ]);
2022-02-24 18:57:25 +13:00
2022-05-24 02:54:50 +12:00
// Cleanup : Delete function
2022-04-14 00:39:31 +12:00
$response = $this -> client -> call ( Client :: METHOD_DELETE , '/functions/' . $functionId , [
2022-02-24 18:57:25 +13:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ],
], []);
$this -> assertEquals ( 204 , $response [ 'headers' ][ 'status-code' ]);
}
2022-02-26 15:01:02 +13:00
// public function testCreateCustomSwiftExecution()
// {
// $name = 'swift-5.5';
// $folder = 'swift';
// $code = realpath(__DIR__ . '/../../../resources/functions'). "/$folder/code.tar.gz";
// $this->packageCode($folder);
2022-04-14 00:39:31 +12:00
2022-02-26 15:01:02 +13:00
// $entrypoint = 'index.swift';
// $timeout = 5;
// $function = $this->client->call(Client::METHOD_POST, '/functions', array_merge([
// 'content-type' => 'application/json',
// 'x-appwrite-project' => $this->getProject()['$id'],
// ], $this->getHeaders()), [
// 'functionId' => 'unique()',
// 'name' => 'Test '.$name,
// 'runtime' => $name,
// 'vars' => [
// 'CUSTOM_VARIABLE' => 'variable',
// ],
// 'events' => [],
// 'schedule' => '',
// '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' => $entrypoint,
// 'code' => new CURLFile($code, 'application/x-gzip', basename($code)),
// 'activate' => true,
// ]);
// $deploymentId = $deployment['body']['$id'] ?? '';
// $this->assertEquals(201, $deployment['headers']['status-code']);
// // Allow (slow) build step to run
// sleep(300);
// $execution = $this->client->call(Client::METHOD_POST, '/functions/'.$functionId.'/executions', array_merge([
// 'content-type' => 'application/json',
// 'x-appwrite-project' => $this->getProject()['$id'],
// ], $this->getHeaders()), [
// 'data' => 'foobar',
// ]);
// $executionId = $execution['body']['$id'] ?? '';
2022-04-14 00:39:31 +12:00
2022-02-26 15:01:02 +13:00
// $this->assertEquals(201, $execution['headers']['status-code']);
2022-02-24 18:57:25 +13:00
2022-02-26 15:01:02 +13:00
// $executionId = $execution['body']['$id'] ?? '';
2022-04-14 00:39:31 +12:00
2022-02-26 15:01:02 +13:00
// sleep(10);
// $executions = $this->client->call(Client::METHOD_GET, '/functions/'.$functionId.'/executions/'.$executionId, array_merge([
// 'content-type' => 'application/json',
// 'x-appwrite-project' => $this->getProject()['$id'],
// ], $this->getHeaders()));
2022-04-25 21:12:58 +12:00
// $output = json_decode($executions['body']['response'], true);
2022-02-26 15:01:02 +13:00
// $this->assertEquals(200, $executions['headers']['status-code']);
// $this->assertEquals('completed', $executions['body']['status']);
// $this->assertEquals($functionId, $output['APPWRITE_FUNCTION_ID']);
// $this->assertEquals('Test '.$name, $output['APPWRITE_FUNCTION_NAME']);
// $this->assertEquals($deploymentId, $output['APPWRITE_FUNCTION_DEPLOYMENT']);
// $this->assertEquals('http', $output['APPWRITE_FUNCTION_TRIGGER']);
// $this->assertEquals('Swift', $output['APPWRITE_FUNCTION_RUNTIME_NAME']);
// $this->assertEquals('5.5', $output['APPWRITE_FUNCTION_RUNTIME_VERSION']);
// $this->assertEquals('', $output['APPWRITE_FUNCTION_EVENT']);
// $this->assertEquals('', $output['APPWRITE_FUNCTION_EVENT_DATA']);
// $this->assertEquals('foobar', $output['APPWRITE_FUNCTION_DATA']);
// $this->assertEquals('variable', $output['CUSTOM_VARIABLE']);
// $this->assertEquals('', $output['APPWRITE_FUNCTION_USER_ID']);
// $this->assertEmpty($output['APPWRITE_FUNCTION_JWT']);
// $this->assertEquals($this->getProject()['$id'], $output['APPWRITE_FUNCTION_PROJECT_ID']);
// $executions = $this->client->call(Client::METHOD_GET, '/functions/'.$functionId.'/executions', array_merge([
// 'content-type' => 'application/json',
// 'x-appwrite-project' => $this->getProject()['$id'],
// ], $this->getHeaders()));
2022-04-14 00:39:31 +12:00
2022-02-26 15:01:02 +13:00
// $this->assertEquals($executions['headers']['status-code'], 200);
2022-02-27 22:57:09 +13:00
// $this->assertEquals($executions['body']['total'], 1);
2022-02-26 15:01:02 +13: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');
2022-04-25 21:12:58 +12:00
// $this->assertStringContainsString('foobar', $executions['body']['executions'][0]['response']);
2022-02-26 15:01:02 +13:00
2022-05-24 02:54:50 +12:00
// // Cleanup : Delete function
2022-02-26 15:01:02 +13:00
// $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']);
// }
2021-12-10 02:02:12 +13: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 22:57:09 +13:00
$this -> assertGreaterThan ( 0 , $runtimes [ 'body' ][ 'total' ]);
2021-12-10 02:02:12 +13: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 );
}
2022-04-14 00:39:31 +12:00
}