1
0
Fork 0
mirror of synced 2024-09-29 17:01:37 +13:00

test: refactor usagetest

This commit is contained in:
loks0n 2024-02-20 14:47:49 +00:00
parent e051717232
commit 7b88b6dbb1

View file

@ -32,6 +32,16 @@ class UsageTest extends Scope
protected static string $formatTz = 'Y-m-d\TH:i:s.vP'; protected static string $formatTz = 'Y-m-d\TH:i:s.vP';
protected function getConsoleHeaders(): array
{
return [
'origin' => 'http://localhost',
'x-appwrite-project' => 'console',
'x-appwrite-mode' => 'admin',
'cookie' => 'a_session_console=' . $this->getRoot()['session'],
];
}
protected function validateDates(array $metrics): void protected function validateDates(array $metrics): void
{ {
foreach ($metrics as $metric) { foreach ($metrics as $metric) {
@ -54,47 +64,55 @@ class UsageTest extends Scope
public function testPrepareUsersStats(): array public function testPrepareUsersStats(): array
{ {
$project = $this->getProject(true);
$projectId = $project['$id'];
$headers['x-appwrite-project'] = $project['$id'];
$headers['x-appwrite-key'] = $project['apiKey'];
$headers['content-type'] = 'application/json';
$usersTotal = 0; $usersTotal = 0;
$requestsTotal = 0; $requestsTotal = 0;
for ($i = 0; $i < self::CREATE; $i++) { for ($i = 0; $i < self::CREATE; $i++) {
$email = uniqid() . 'user@usage.test'; $params = [
$password = 'password'; 'userId' => 'unique()',
$name = uniqid() . 'User'; 'email' => uniqid() . 'user@usage.test',
$res = $this->client->call( 'password' => 'password',
'name' => uniqid() . 'User',
];
$response = $this->client->call(
Client::METHOD_POST, Client::METHOD_POST,
'/users', '/users',
$headers, array_merge([
[ 'content-type' => 'application/json',
'userId' => 'unique()', 'x-appwrite-project' => $this->getProject()['$id']
'email' => $email, ], $this->getHeaders()),
'password' => $password, $params
'name' => $name,
]
); );
$this->assertEquals($email, $res['body']['email']); $this->assertEquals(201, $response['headers']['status-code']);
$this->assertNotEmpty($res['body']['$id']); $this->assertEquals($params['email'], $response['body']['email']);
$usersTotal++; $this->assertNotEmpty($response['body']['$id']);
$requestsTotal++;
$usersTotal += 1;
$requestsTotal += 1;
if ($i < (self::CREATE / 2)) { if ($i < (self::CREATE / 2)) {
$userId = $res['body']['$id']; $userId = $response['body']['$id'];
$res = $this->client->call(Client::METHOD_DELETE, '/users/' . $userId, $headers);
$this->assertEmpty($res['body']); $response = $this->client->call(
$requestsTotal++; Client::METHOD_DELETE,
$usersTotal--; '/users/' . $userId,
array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id']
], $this->getHeaders())
);
$this->assertEquals(204, $response['headers']['status-code']);
$this->assertEmpty($response['body']);
$requestsTotal += 1;
$usersTotal -= 1;
} }
} }
return [ return [
'projectId' => $projectId,
'headers' => $headers,
'usersTotal' => $usersTotal, 'usersTotal' => $usersTotal,
'requestsTotal' => $requestsTotal 'requestsTotal' => $requestsTotal
]; ];
@ -108,74 +126,63 @@ class UsageTest extends Scope
{ {
sleep(self::WAIT); sleep(self::WAIT);
$projectId = $data['projectId'];
$headers = $data['headers'];
$usersTotal = $data['usersTotal'];
$requestsTotal = $data['requestsTotal']; $requestsTotal = $data['requestsTotal'];
$consoleHeaders = [
'origin' => 'http://localhost',
'x-appwrite-project' => 'console',
'cookie' => 'a_session_console=' . $this->getRoot()['session'],
'x-appwrite-project' => $projectId,
'x-appwrite-mode' => 'admin',
];
$res = $this->client->call( $response = $this->client->call(
Client::METHOD_GET, Client::METHOD_GET,
'/project/usage', '/project/usage',
$consoleHeaders, $this->getConsoleHeaders(),
[ [
'period' => '1h', 'period' => '1h',
'startDate' => self::getToday(), 'startDate' => self::getToday(),
'endDate' => self::getTomorrow(), 'endDate' => self::getTomorrow(),
] ]
); );
$res = $res['body'];
$this->assertEquals(12, count($res)); $this->assertEquals(200, $response['headers']['status-code']);
$this->validateDates($res['network']); $this->assertEquals(12, count($response['body']));
$this->validateDates($res['requests']); $this->validateDates($response['body']['network']);
$this->validateDates($res['users']); $this->validateDates($response['body']['requests']);
$this->assertArrayHasKey('executionsBreakdown', $res); $this->validateDates($response['body']['users']);
$this->assertArrayHasKey('bucketsBreakdown', $res); $this->assertArrayHasKey('executionsBreakdown', $response['body']);
$this->assertArrayHasKey('bucketsBreakdown', $response['body']);
$res = $this->client->call( $response = $this->client->call(
Client::METHOD_GET, Client::METHOD_GET,
'/users/usage?range=90d', '/users/usage?range=90d',
$consoleHeaders $this->getConsoleHeaders()
); );
$res = $res['body']; $this->assertEquals('90d', $response['body']['range']);
$this->assertEquals('90d', $res['range']); $this->assertEquals(90, count($response['body']['users']));
$this->assertEquals(90, count($res['users'])); $this->assertEquals(90, count($response['body']['sessions']));
$this->assertEquals(90, count($res['sessions'])); $this->assertEquals((self::CREATE / 2), $response['body']['users'][array_key_last($response['body']['users'])]['value']);
$this->assertEquals((self::CREATE / 2), $res['users'][array_key_last($res['users'])]['value']);
return [ return array_merge($data, [
'projectId' => $projectId, 'requestsTotal' => $requestsTotal
'headers' => $headers, ]);
'consoleHeaders' => $consoleHeaders,
'requestsTotal' => $requestsTotal,
];
} }
/** @depends testUsersStats */ /** @depends testUsersStats */
public function testPrepareStorageStats(array $data): array public function testPrepareStorageStats(array $data): array
{ {
$headers = $data['headers'];
$bucketsTotal = 0;
$requestsTotal = $data['requestsTotal']; $requestsTotal = $data['requestsTotal'];
$bucketsTotal = 0;
$storageTotal = 0; $storageTotal = 0;
$filesTotal = 0; $filesTotal = 0;
for ($i = 0; $i < self::CREATE; $i++) { for ($i = 0; $i < self::CREATE; $i++) {
$name = uniqid() . ' bucket'; $name = uniqid() . ' bucket';
$res = $this->client->call(
$response = $this->client->call(
Client::METHOD_POST, Client::METHOD_POST,
'/storage/buckets', '/storage/buckets',
$headers, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id']
], $this->getHeaders()),
[ [
'bucketId' => 'unique()', 'bucketId' => 'unique()',
'name' => $name, 'name' => $name,
@ -188,21 +195,30 @@ class UsageTest extends Scope
], ],
] ]
); );
$this->assertEquals($name, $res['body']['name']);
$this->assertNotEmpty($res['body']['$id']); $this->assertEquals(201, $response['headers']['status-code']);
$bucketId = $res['body']['$id']; $this->assertEquals($name, $response['body']['name']);
$bucketsTotal++; $this->assertNotEmpty($response['body']['$id']);
$requestsTotal++;
$bucketsTotal += 1;
$requestsTotal += 1;
$bucketId = $response['body']['$id'];
if ($i < (self::CREATE / 2)) { if ($i < (self::CREATE / 2)) {
$res = $this->client->call( $response = $this->client->call(
Client::METHOD_DELETE, Client::METHOD_DELETE,
'/storage/buckets/' . $bucketId, '/storage/buckets/' . $bucketId,
$headers array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id']
], $this->getHeaders()),
); );
$this->assertEmpty($res['body']);
$requestsTotal++; $this->assertEmpty($response['body']);
$bucketsTotal--;
$requestsTotal += 1;
$bucketsTotal -= 1;
} }
} }
@ -229,33 +245,42 @@ class UsageTest extends Scope
for ($i = 0; $i < self::CREATE; $i++) { for ($i = 0; $i < self::CREATE; $i++) {
$file = $files[$i % count($files)]; $file = $files[$i % count($files)];
$res = $this->client->call( $response = $this->client->call(
Client::METHOD_POST, Client::METHOD_POST,
'/storage/buckets/' . $bucketId . '/files', '/storage/buckets/' . $bucketId . '/files',
array_merge($headers, ['content-type' => 'multipart/form-data']), array_merge([
'content-type' => 'multipart/form-data',
'x-appwrite-project' => $this->getProject()['$id']
], $this->getHeaders()),
[ [
'fileId' => 'unique()', 'fileId' => 'unique()',
'file' => new CURLFile($file['path'], '', $file['name']), 'file' => new CURLFile($file['path'], '', $file['name']),
] ]
); );
$this->assertNotEmpty($res['body']['$id']); $this->assertNotEmpty($response['body']['$id']);
$fileSize = $response['body']['sizeOriginal'];
$fileSize = $res['body']['sizeOriginal'];
$storageTotal += $fileSize; $storageTotal += $fileSize;
$filesTotal++; $filesTotal = $filesTotal + 1;
$requestsTotal++; $requestsTotal = $requestsTotal + 1;
$fileId = $response['body']['$id'];
$fileId = $res['body']['$id'];
if ($i < (self::CREATE / 2)) { if ($i < (self::CREATE / 2)) {
$res = $this->client->call( $response = $this->client->call(
Client::METHOD_DELETE, Client::METHOD_DELETE,
'/storage/buckets/' . $bucketId . '/files/' . $fileId, '/storage/buckets/' . $bucketId . '/files/' . $fileId,
$headers array_merge([
'x-appwrite-project' => $this->getProject()['$id']
], $this->getHeaders()),
); );
$this->assertEmpty($res['body']);
$requestsTotal++; $this->assertEmpty($response['body']);
$filesTotal--;
$requestsTotal += 1;
$filesTotal -= 1;
$storageTotal -= $fileSize; $storageTotal -= $fileSize;
} }
} }
@ -283,49 +308,50 @@ class UsageTest extends Scope
sleep(self::WAIT); sleep(self::WAIT);
$res = $this->client->call( $response = $this->client->call(
Client::METHOD_GET, Client::METHOD_GET,
'/project/usage', '/project/usage',
$data['headers'], array_merge([
'x-appwrite-project' => $this->getProject()['$id']
], $this->getHeaders()),
[ [
'period' => '1d', 'period' => '1d',
'startDate' => self::getToday(), 'startDate' => self::getToday(),
'endDate' => self::getTomorrow(), 'endDate' => self::getTomorrow(),
] ]
); );
$res = $res['body'];
$this->assertEquals(12, count($res)); $this->assertEquals(12, count($response['body']));
$this->assertEquals(1, count($res['requests'])); $this->assertEquals(1, count($response['body']['requests']));
$this->assertEquals($requestsTotal, $res['requests'][array_key_last($res['requests'])]['value']); $this->assertEquals($requestsTotal, $response['body']['requests'][array_key_last($response['body']['requests'])]['value']);
$this->validateDates($res['requests']); $this->validateDates($response['body']['requests']);
$this->assertEquals($storageTotal, $res['filesStorageTotal']); $this->assertEquals($storageTotal, $response['body']['filesStorageTotal']);
$res = $this->client->call( $response = $this->client->call(
Client::METHOD_GET, Client::METHOD_GET,
'/storage/usage?range=30d', '/storage/usage?range=30d',
$data['headers'] array_merge([
'x-appwrite-project' => $this->getProject()['$id']
], $this->getHeaders()),
); );
$res = $res['body']; $this->assertEquals($storageTotal, $response['body']['storage'][array_key_last($response['body']['storage'])]['value']);
$this->assertEquals($storageTotal, $res['storage'][array_key_last($res['storage'])]['value']); $this->validateDates($response['body']['storage']);
$this->validateDates($res['storage']); $this->assertEquals($bucketsTotal, $response['body']['buckets'][array_key_last($response['body']['buckets'])]['value']);
$this->assertEquals($bucketsTotal, $res['buckets'][array_key_last($res['buckets'])]['value']); $this->validateDates($response['body']['buckets']);
$this->validateDates($res['buckets']); $this->assertEquals($filesTotal, $response['body']['files'][array_key_last($response['body']['files'])]['value']);
$this->assertEquals($filesTotal, $res['files'][array_key_last($res['files'])]['value']); $this->validateDates($response['body']['files']);
$this->validateDates($res['files']);
$res = $this->client->call( $response = $this->client->call(
Client::METHOD_GET, Client::METHOD_GET,
'/storage/' . $bucketId . '/usage?range=30d', '/storage/' . $bucketId . '/usage?range=30d',
$data['headers'], array_merge([
'x-appwrite-project' => $this->getProject()['$id']
], $this->getHeaders()),
); );
$res = $res['body']; $this->assertEquals($storageTotal, $response['body']['storage'][array_key_last($response['body']['storage'])]['value']);
$this->assertEquals($storageTotal, $res['storage'][array_key_last($res['storage'])]['value']); $this->assertEquals($filesTotal, $response['body']['files'][array_key_last($response['body']['files'])]['value']);
$this->assertEquals($filesTotal, $res['files'][array_key_last($res['files'])]['value']);
$data['requestsTotal'] = $requestsTotal;
return $data; return $data;
} }
@ -333,52 +359,62 @@ class UsageTest extends Scope
/** @depends testStorageStats */ /** @depends testStorageStats */
public function testPrepareDatabaseStats(array $data): array public function testPrepareDatabaseStats(array $data): array
{ {
$headers = $data['headers'];
$requestsTotal = $data['requestsTotal']; $requestsTotal = $data['requestsTotal'];
$databasesTotal = 0; $databasesTotal = 0;
$collectionsTotal = 0; $collectionsTotal = 0;
$documentsTotal = 0; $documentsTotal = 0;
for ($i = 0; $i < self::CREATE; $i++) { for ($i = 0; $i < self::CREATE; $i++) {
$name = uniqid() . ' database'; $name = uniqid() . ' database';
$res = $this->client->call(
$response = $this->client->call(
Client::METHOD_POST, Client::METHOD_POST,
'/databases', '/databases',
$headers, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id']
], $this->getHeaders()),
[ [
'databaseId' => 'unique()', 'databaseId' => 'unique()',
'name' => $name, 'name' => $name,
] ]
); );
$this->assertEquals($name, $response['body']['name']);
$this->assertNotEmpty($response['body']['$id']);
$this->assertEquals($name, $res['body']['name']); $requestsTotal += 1;
$this->assertNotEmpty($res['body']['$id']); $databasesTotal += 1;
$databaseId = $res['body']['$id'];
$requestsTotal++; $databaseId = $response['body']['$id'];
$databasesTotal++;
if ($i < (self::CREATE / 2)) { if ($i < (self::CREATE / 2)) {
$res = $this->client->call( $response = $this->client->call(
Client::METHOD_DELETE, Client::METHOD_DELETE,
'/databases/' . $databaseId, '/databases/' . $databaseId,
$headers array_merge([
'x-appwrite-project' => $this->getProject()['$id']
], $this->getHeaders()),
); );
$this->assertEmpty($res['body']);
$databasesTotal--; $this->assertEmpty($response['body']);
$requestsTotal++;
$databasesTotal -= 1;
$requestsTotal += 1;
} }
} }
for ($i = 0; $i < self::CREATE; $i++) { for ($i = 0; $i < self::CREATE; $i++) {
$name = uniqid() . ' collection'; $name = uniqid() . ' collection';
$res = $this->client->call(
$response = $this->client->call(
Client::METHOD_POST, Client::METHOD_POST,
'/databases/' . $databaseId . '/collections', '/databases/' . $databaseId . '/collections',
$headers, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id']
], $this->getHeaders()),
[ [
'collectionId' => 'unique()', 'collectionId' => 'unique()',
'name' => $name, 'name' => $name,
@ -392,29 +428,37 @@ class UsageTest extends Scope
] ]
); );
$this->assertEquals($name, $res['body']['name']); $this->assertEquals($name, $response['body']['name']);
$this->assertNotEmpty($res['body']['$id']); $this->assertNotEmpty($response['body']['$id']);
$collectionId = $res['body']['$id'];
$requestsTotal++; $requestsTotal += 1;
$collectionsTotal++; $collectionsTotal += 1;
$collectionId = $response['body']['$id'];
if ($i < (self::CREATE / 2)) { if ($i < (self::CREATE / 2)) {
$res = $this->client->call( $response = $this->client->call(
Client::METHOD_DELETE, Client::METHOD_DELETE,
'/databases/' . $databaseId . '/collections/' . $collectionId, '/databases/' . $databaseId . '/collections/' . $collectionId,
$headers array_merge([
'x-appwrite-project' => $this->getProject()['$id']
], $this->getHeaders()),
); );
$this->assertEmpty($res['body']);
$collectionsTotal--; $this->assertEmpty($response['body']);
$requestsTotal++;
$collectionsTotal -= 1;
$requestsTotal += 1;
} }
} }
$res = $this->client->call( $response = $this->client->call(
Client::METHOD_POST, Client::METHOD_POST,
'/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes' . '/string', '/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes' . '/string',
$headers, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id']
], $this->getHeaders()),
[ [
'key' => 'name', 'key' => 'name',
'size' => 255, 'size' => 255,
@ -422,38 +466,49 @@ class UsageTest extends Scope
] ]
); );
$this->assertEquals('name', $res['body']['key']); $this->assertEquals('name', $response['body']['key']);
$requestsTotal++;
$requestsTotal += 1;
sleep(self::WAIT); sleep(self::WAIT);
for ($i = 0; $i < self::CREATE; $i++) { for ($i = 0; $i < self::CREATE; $i++) {
$name = uniqid() . ' collection'; $name = uniqid() . ' collection';
$res = $this->client->call(
$response = $this->client->call(
Client::METHOD_POST, Client::METHOD_POST,
'/databases/' . $databaseId . '/collections/' . $collectionId . '/documents', '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents',
$headers, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id']
], $this->getHeaders()),
[ [
'documentId' => 'unique()', 'documentId' => 'unique()',
'data' => ['name' => $name] 'data' => ['name' => $name]
] ]
); );
$this->assertEquals($name, $res['body']['name']);
$this->assertNotEmpty($res['body']['$id']);
$documentId = $res['body']['$id'];
$requestsTotal++; $this->assertEquals($name, $response['body']['name']);
$documentsTotal++; $this->assertNotEmpty($response['body']['$id']);
$requestsTotal += 1;
$documentsTotal += 1;
$documentId = $response['body']['$id'];
if ($i < (self::CREATE / 2)) { if ($i < (self::CREATE / 2)) {
$res = $this->client->call( $response = $this->client->call(
Client::METHOD_DELETE, Client::METHOD_DELETE,
'/databases/' . $databaseId . '/collections/' . $collectionId . '/documents/' . $documentId, '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents/' . $documentId,
$headers array_merge([
'x-appwrite-project' => $this->getProject()['$id']
], $this->getHeaders()),
); );
$this->assertEmpty($res['body']);
$documentsTotal--; $this->assertEmpty($response['body']);
$requestsTotal++;
$documentsTotal -= 1;
$requestsTotal += 1;
} }
} }
@ -471,8 +526,6 @@ class UsageTest extends Scope
#[Retry(count: 1)] #[Retry(count: 1)]
public function testDatabaseStats(array $data): array public function testDatabaseStats(array $data): array
{ {
$projectId = $data['projectId'];
$databaseId = $data['databaseId']; $databaseId = $data['databaseId'];
$collectionId = $data['collectionId']; $collectionId = $data['collectionId'];
$requestsTotal = $data['requestsTotal']; $requestsTotal = $data['requestsTotal'];
@ -482,78 +535,77 @@ class UsageTest extends Scope
sleep(self::WAIT); sleep(self::WAIT);
$res = $this->client->call( $response = $this->client->call(
Client::METHOD_GET, Client::METHOD_GET,
'/project/usage', '/project/usage',
$data['consoleHeaders'], $this->getConsoleHeaders(),
[ [
'period' => '1d', 'period' => '1d',
'startDate' => self::getToday(), 'startDate' => self::getToday(),
'endDate' => self::getTomorrow(), 'endDate' => self::getTomorrow(),
] ]
); );
$res = $res['body'];
$this->assertEquals(12, count($res)); $this->assertEquals(12, count($response['body']));
$this->assertEquals(1, count($res['requests'])); $this->assertEquals(1, count($response['body']['requests']));
$this->assertEquals(1, count($res['network'])); $this->assertEquals(1, count($response['body']['network']));
$this->assertEquals($requestsTotal, $res['requests'][array_key_last($res['requests'])]['value']); $this->assertEquals($requestsTotal, $response['body']['requests'][array_key_last($response['body']['requests'])]['value']);
$this->validateDates($res['requests']); $this->validateDates($response['body']['requests']);
$this->assertEquals($databasesTotal, $res['databasesTotal']); $this->assertEquals($databasesTotal, $response['body']['databasesTotal']);
$this->assertEquals($documentsTotal, $res['documentsTotal']); $this->assertEquals($documentsTotal, $response['body']['documentsTotal']);
$res = $this->client->call( $response = $this->client->call(
Client::METHOD_GET, Client::METHOD_GET,
'/databases/usage?range=30d', '/databases/usage?range=30d',
$data['consoleHeaders'] $this->getConsoleHeaders()
); );
$res = $res['body'];
$this->assertEquals($databasesTotal, $res['databases'][array_key_last($res['databases'])]['value']); $this->assertEquals($databasesTotal, $response['body']['databases'][array_key_last($response['body']['databases'])]['value']);
$this->validateDates($res['databases']); $this->validateDates($response['body']['databases']);
$this->assertEquals($collectionsTotal, $res['collections'][array_key_last($res['collections'])]['value']); $this->assertEquals($collectionsTotal, $response['body']['collections'][array_key_last($response['body']['collections'])]['value']);
$this->validateDates($res['collections']); $this->validateDates($response['body']['collections']);
$this->assertEquals($documentsTotal, $res['documents'][array_key_last($res['documents'])]['value']); $this->assertEquals($documentsTotal, $response['body']['documents'][array_key_last($response['body']['documents'])]['value']);
$this->validateDates($res['documents']); $this->validateDates($response['body']['documents']);
$res = $this->client->call( $response = $this->client->call(
Client::METHOD_GET, Client::METHOD_GET,
'/databases/' . $databaseId . '/usage?range=30d', '/databases/' . $databaseId . '/usage?range=30d',
$data['consoleHeaders'] $this->getConsoleHeaders()
); );
$res = $res['body'];
$this->assertEquals($collectionsTotal, $res['collections'][array_key_last($res['collections'])]['value']); $this->assertEquals($collectionsTotal, $response['body']['collections'][array_key_last($response['body']['collections'])]['value']);
$this->validateDates($res['collections']); $this->validateDates($response['body']['collections']);
$this->assertEquals($documentsTotal, $res['documents'][array_key_last($res['documents'])]['value']); $this->assertEquals($documentsTotal, $response['body']['documents'][array_key_last($response['body']['documents'])]['value']);
$this->validateDates($res['documents']); $this->validateDates($response['body']['documents']);
$res = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $collectionId . '/usage?range=30d', $data['consoleHeaders']); $response = $this->client->call(
$res = $res['body']; Client::METHOD_GET,
'/databases/' . $databaseId . '/collections/' . $collectionId . '/usage?range=30d',
$this->getConsoleHeaders()
);
$this->assertEquals($documentsTotal, $res['documents'][array_key_last($res['documents'])]['value']); $this->assertEquals($documentsTotal, $response['body']['documents'][array_key_last($response['body']['documents'])]['value']);
$this->validateDates($res['documents']); $this->validateDates($response['body']['documents']);
$data['requestsTotal'] = $requestsTotal; return $requestsTotal;
return $data;
} }
/** @depends testDatabaseStats */ /** @depends testDatabaseStats */
public function testPrepareFunctionsStats(array $data): array public function testPrepareFunctionsStats(array $data): array
{ {
$dateValidator = new DatetimeValidator();
$headers = $data['headers'];
$executionTime = 0; $executionTime = 0;
$executions = 0; $executions = 0;
$failures = 0; $failures = 0;
$response1 = $this->client->call( $response = $this->client->call(
Client::METHOD_POST, Client::METHOD_POST,
'/functions', '/functions',
$headers, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id']
], $this->getHeaders()),
[ [
'functionId' => 'unique()', 'functionId' => 'unique()',
'name' => 'Test', 'name' => 'Test',
@ -572,18 +624,21 @@ class UsageTest extends Scope
] ]
); );
$functionId = $response1['body']['$id'] ?? ''; $functionId = $response['body']['$id'] ?? '';
$this->assertEquals(201, $response1['headers']['status-code']); $this->assertEquals(201, $response['headers']['status-code']);
$this->assertNotEmpty($response1['body']['$id']); $this->assertNotEmpty($response['body']['$id']);
$code = realpath(__DIR__ . '/../../resources/functions') . "/php/code.tar.gz"; $code = realpath(__DIR__ . '/../../resources/functions') . "/php/code.tar.gz";
$this->packageCode('php'); $this->packageCode('php');
$deployment = $this->client->call( $response = $this->client->call(
Client::METHOD_POST, Client::METHOD_POST,
'/functions/' . $functionId . '/deployments', '/functions/' . $functionId . '/deployments',
array_merge($headers, ['content-type' => 'multipart/form-data',]), array_merge([
'content-type' => 'multipart/form-data',
'x-appwrite-project' => $this->getProject()['$id']
], $this->getHeaders()),
[ [
'entrypoint' => 'index.php', 'entrypoint' => 'index.php',
'code' => new CURLFile($code, 'application/x-gzip', \basename($code)), 'code' => new CURLFile($code, 'application/x-gzip', \basename($code)),
@ -591,12 +646,12 @@ class UsageTest extends Scope
] ]
); );
$deploymentId = $deployment['body']['$id'] ?? ''; $deploymentId = $response['body']['$id'] ?? '';
$this->assertEquals(202, $deployment['headers']['status-code']); $this->assertEquals(202, $response['headers']['status-code']);
$this->assertNotEmpty($deployment['body']['$id']); $this->assertNotEmpty($response['body']['$id']);
$this->assertEquals(true, (new DatetimeValidator())->isValid($deployment['body']['$createdAt'])); $this->assertEquals(true, (new DatetimeValidator())->isValid($response['body']['$createdAt']));
$this->assertEquals('index.php', $deployment['body']['entrypoint']); $this->assertEquals('index.php', $response['body']['entrypoint']);
// Wait for deployment to build. // Wait for deployment to build.
sleep(self::WAIT + 20); sleep(self::WAIT + 20);
@ -604,7 +659,10 @@ class UsageTest extends Scope
$response = $this->client->call( $response = $this->client->call(
Client::METHOD_PATCH, Client::METHOD_PATCH,
'/functions/' . $functionId . '/deployments/' . $deploymentId, '/functions/' . $functionId . '/deployments/' . $deploymentId,
$headers array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id']
], $this->getHeaders()),
); );
$this->assertEquals(200, $response['headers']['status-code']); $this->assertEquals(200, $response['headers']['status-code']);
@ -614,74 +672,86 @@ class UsageTest extends Scope
$this->assertEquals(true, (new DatetimeValidator())->isValid($response['body']['$updatedAt'])); $this->assertEquals(true, (new DatetimeValidator())->isValid($response['body']['$updatedAt']));
$this->assertEquals($deploymentId, $response['body']['deployment']); $this->assertEquals($deploymentId, $response['body']['deployment']);
$execution = $this->client->call( $response = $this->client->call(
Client::METHOD_POST, Client::METHOD_POST,
'/functions/' . $functionId . '/executions', '/functions/' . $functionId . '/executions',
$headers, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id']
], $this->getHeaders()),
[ [
'async' => false, 'async' => false,
] ]
); );
$this->assertEquals(201, $execution['headers']['status-code']); $this->assertEquals(201, $response['headers']['status-code']);
$this->assertNotEmpty($execution['body']['$id']); $this->assertNotEmpty($response['body']['$id']);
$this->assertEquals($functionId, $execution['body']['functionId']); $this->assertEquals($functionId, $response['body']['functionId']);
$executionTime += (int) ($execution['body']['duration'] * 1000); $executionTime += (int) ($response['body']['duration'] * 1000);
if ($execution['body']['status'] == 'failed') { if ($response['body']['status'] == 'failed') {
$failures++; $failures += 1;
} elseif ($execution['body']['status'] == 'completed') { } elseif ($response['body']['status'] == 'completed') {
$executions++; $executions += 1;
} }
$execution = $this->client->call( $response = $this->client->call(
Client::METHOD_POST, Client::METHOD_POST,
'/functions/' . $functionId . '/executions', '/functions/' . $functionId . '/executions',
$headers, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id']
], $this->getHeaders()),
[ [
'async' => false, 'async' => false,
] ]
); );
$this->assertEquals(201, $execution['headers']['status-code']); $this->assertEquals(201, $response['headers']['status-code']);
$this->assertNotEmpty($execution['body']['$id']); $this->assertNotEmpty($response['body']['$id']);
$this->assertEquals($functionId, $execution['body']['functionId']); $this->assertEquals($functionId, $response['body']['functionId']);
if ($execution['body']['status'] == 'failed') {
$failures++;
} elseif ($execution['body']['status'] == 'completed') {
$executions++;
}
$executionTime += (int) ($execution['body']['duration'] * 1000);
$execution = $this->client->call( if ($response['body']['status'] == 'failed') {
$failures += 1;
} elseif ($response['body']['status'] == 'completed') {
$executions += 1;
}
$executionTime += (int) ($response['body']['duration'] * 1000);
$response = $this->client->call(
Client::METHOD_POST, Client::METHOD_POST,
'/functions/' . $functionId . '/executions', '/functions/' . $functionId . '/executions',
$headers, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id']
], $this->getHeaders()),
[ [
'async' => true, 'async' => true,
] ]
); );
$this->assertEquals(202, $execution['headers']['status-code']); $this->assertEquals(202, $response['headers']['status-code']);
$this->assertNotEmpty($execution['body']['$id']); $this->assertNotEmpty($response['body']['$id']);
$this->assertEquals($functionId, $execution['body']['functionId']); $this->assertEquals($functionId, $response['body']['functionId']);
sleep(self::WAIT); sleep(self::WAIT);
$execution = $this->client->call( $response = $this->client->call(
Client::METHOD_GET, Client::METHOD_GET,
'/functions/' . $functionId . '/executions/' . $execution['body']['$id'], '/functions/' . $functionId . '/executions/' . $response['body']['$id'],
$headers array_merge([
'x-appwrite-project' => $this->getProject()['$id']
], $this->getHeaders()),
); );
if ($execution['body']['status'] == 'failed') { if ($response['body']['status'] == 'failed') {
$failures++; $failures += 1;
} elseif ($execution['body']['status'] == 'completed') { } elseif ($response['body']['status'] == 'completed') {
$executions++; $executions += 1;
} }
$executionTime += (int) ($execution['body']['duration'] * 1000); $executionTime += (int) ($response['body']['duration'] * 1000);
return array_merge($data, [ return array_merge($data, [
'functionId' => $functionId, 'functionId' => $functionId,
@ -704,7 +774,7 @@ class UsageTest extends Scope
$response = $this->client->call( $response = $this->client->call(
Client::METHOD_GET, Client::METHOD_GET,
'/functions/' . $functionId . '/usage?range=30d', '/functions/' . $functionId . '/usage?range=30d',
$data['consoleHeaders'] $this->getConsoleHeaders()
); );
$this->assertEquals(200, $response['headers']['status-code']); $this->assertEquals(200, $response['headers']['status-code']);
@ -716,18 +786,15 @@ class UsageTest extends Scope
$this->assertIsArray($response['body']['buildsTime']); $this->assertIsArray($response['body']['buildsTime']);
$this->assertIsArray($response['body']['executions']); $this->assertIsArray($response['body']['executions']);
$this->assertIsArray($response['body']['executionsTime']); $this->assertIsArray($response['body']['executionsTime']);
$this->assertEquals($executions, $response['body']['executions'][array_key_last($response['body']['executions'])]['value']);
$response = $response['body']; $this->validateDates($response['body']['executions']);
$this->assertEquals($executionTime, $response['body']['executionsTime'][array_key_last($response['body']['executionsTime'])]['value']);
$this->assertEquals($executions, $response['executions'][array_key_last($response['executions'])]['value']); $this->validateDates($response['body']['executionsTime']);
$this->validateDates($response['executions']);
$this->assertEquals($executionTime, $response['executionsTime'][array_key_last($response['executionsTime'])]['value']);
$this->validateDates($response['executionsTime']);
$response = $this->client->call( $response = $this->client->call(
Client::METHOD_GET, Client::METHOD_GET,
'/functions/usage?range=30d', '/functions/usage?range=30d',
$data['consoleHeaders'] $this->getConsoleHeaders()
); );
$this->assertEquals(200, $response['headers']['status-code']); $this->assertEquals(200, $response['headers']['status-code']);
@ -740,15 +807,12 @@ class UsageTest extends Scope
$this->assertIsArray($response['body']['buildsTime']); $this->assertIsArray($response['body']['buildsTime']);
$this->assertIsArray($response['body']['executions']); $this->assertIsArray($response['body']['executions']);
$this->assertIsArray($response['body']['executionsTime']); $this->assertIsArray($response['body']['executionsTime']);
$this->assertEquals($executions, $response['body']['executions'][array_key_last($response['body']['executions'])]['value']);
$response = $response['body']; $this->validateDates($response['body']['executions']);
$this->assertEquals($executionTime, $response['body']['executionsTime'][array_key_last($response['body']['executionsTime'])]['value']);
$this->assertEquals($executions, $response['executions'][array_key_last($response['executions'])]['value']); $this->validateDates($response['body']['executionsTime']);
$this->validateDates($response['executions']); $this->assertGreaterThan(0, $response['body']['buildsTime'][array_key_last($response['body']['buildsTime'])]['value']);
$this->assertEquals($executionTime, $response['executionsTime'][array_key_last($response['executionsTime'])]['value']); $this->validateDates($response['body']['buildsTime']);
$this->validateDates($response['executionsTime']);
$this->assertGreaterThan(0, $response['buildsTime'][array_key_last($response['buildsTime'])]['value']);
$this->validateDates($response['buildsTime']);
} }
public function tearDown(): void public function tearDown(): void