1
0
Fork 0
mirror of synced 2024-09-09 06:11:09 +12:00
appwrite/tests/e2e/General/UsageTest.php

748 lines
26 KiB
PHP
Raw Normal View History

2022-06-30 16:27:11 +12:00
<?php
2022-07-04 21:31:14 +12:00
namespace Tests\E2E\General;
2022-08-31 11:35:46 +12:00
use Appwrite\Tests\Retry;
2022-06-30 16:27:11 +12:00
use Tests\E2E\Client;
use Tests\E2E\Scopes\ProjectCustom;
use Tests\E2E\Scopes\Scope;
use Tests\E2E\Scopes\SideServer;
2022-07-05 18:53:57 +12:00
use CURLFile;
2022-07-10 14:12:49 +12:00
use Tests\E2E\Services\Functions\FunctionsBase;
2022-08-26 10:10:52 +12:00
use Utopia\Database\DateTime;
use Utopia\Database\Permission;
use Utopia\Database\Role;
2022-06-30 16:27:11 +12:00
2022-06-30 21:40:27 +12:00
class UsageTest extends Scope
{
2022-06-30 16:27:11 +12:00
use ProjectCustom;
use SideServer;
2022-07-10 14:12:49 +12:00
use FunctionsBase;
2022-06-30 16:27:11 +12:00
2022-12-19 21:25:49 +13:00
private const WAIT = 5;
private const CREATE = 20;
2022-06-30 21:33:40 +12:00
protected string $projectId;
2022-06-30 21:40:27 +12:00
2022-07-27 20:28:23 +12:00
protected function setUp(): void
2022-06-30 21:40:27 +12:00
{
2022-07-27 20:28:23 +12:00
parent::setUp();
}
2022-06-30 16:27:11 +12:00
2022-09-16 11:48:09 +12:00
protected static string $formatTz = 'Y-m-d\TH:i:s.vP';
protected function validateDates(array $metrics): void
{
foreach ($metrics as $metric) {
$this->assertIsObject(\DateTime::createFromFormat("Y-m-d\TH:i:s.vP", $metric['date']));
}
}
public function testPrepareUsersStats(): array
2022-07-27 20:28:23 +12:00
{
2022-07-28 19:28:00 +12:00
$project = $this->getProject(true);
$projectId = $project['$id'];
$headers['x-appwrite-project'] = $project['$id'];
$headers['x-appwrite-key'] = $project['apiKey'];
$headers['content-type'] = 'application/json';
2022-12-15 20:56:06 +13:00
$usersCount = 0;
2022-07-04 01:10:26 +12:00
$requestsCount = 0;
2022-12-16 06:00:05 +13:00
for ($i = 0; $i < self::CREATE; $i++) {
2022-06-30 16:27:11 +12:00
$email = uniqid() . 'user@usage.test';
$password = 'password';
$name = uniqid() . 'User';
2022-12-16 06:00:05 +13:00
$res = $this->client->call(
Client::METHOD_POST,
'/users',
$headers,
[
2022-12-15 20:56:06 +13:00
'userId' => 'unique()',
'email' => $email,
2022-06-30 16:27:11 +12:00
'password' => $password,
2022-12-15 20:56:06 +13:00
'name' => $name,
2022-12-16 06:00:05 +13:00
]
);
2022-06-30 21:33:40 +12:00
$this->assertEquals($email, $res['body']['email']);
$this->assertNotEmpty($res['body']['$id']);
2022-07-04 01:10:26 +12:00
$usersCount++;
$requestsCount++;
2022-07-03 14:42:13 +12:00
2022-12-16 06:00:05 +13:00
if ($i < (self::CREATE / 2)) {
2022-07-03 14:42:13 +12:00
$userId = $res['body']['$id'];
2022-07-28 19:28:00 +12:00
$res = $this->client->call(Client::METHOD_DELETE, '/users/' . $userId, $headers);
2022-07-03 14:42:13 +12:00
$this->assertEmpty($res['body']);
2022-12-15 20:56:06 +13:00
$requestsCount++;
2022-07-04 01:10:26 +12:00
$usersCount--;
2022-07-03 14:42:13 +12:00
}
2022-06-30 16:27:11 +12:00
}
2022-07-03 14:42:13 +12:00
return [
2022-12-15 20:56:06 +13:00
'projectId' => $projectId,
'headers' => $headers,
'usersCount' => $usersCount,
'requestsCount' => $requestsCount
];
}
/**
* @depends testPrepareUsersStats
*/
public function testUsersStats(array $data): array
{
sleep(self::WAIT);
2022-06-30 21:33:40 +12:00
2022-12-15 20:56:06 +13:00
$projectId = $data['projectId'];
$headers = $data['headers'];
$usersCount = $data['usersCount'];
$requestsCount = $data['requestsCount'];
2022-12-15 20:56:06 +13:00
$consoleHeaders = [
2022-06-30 21:33:40 +12:00
'origin' => 'http://localhost',
'x-appwrite-project' => 'console',
'cookie' => 'a_session_console=' . $this->getRoot()['session'],
2022-11-19 02:13:33 +13:00
'x-appwrite-project' => $projectId,
'x-appwrite-mode' => 'admin',
2022-06-30 21:33:40 +12:00
];
2022-06-30 21:40:27 +12:00
2022-12-16 06:00:05 +13:00
$res = $this->client->call(
Client::METHOD_GET,
'/project/usage?range=24h',
$consoleHeaders
);
2022-06-30 21:33:40 +12:00
$res = $res['body'];
2022-12-15 20:56:06 +13:00
2022-12-16 06:00:05 +13:00
$this->assertEquals('24h', $res['range']);
2022-11-09 02:38:43 +13:00
$this->assertEquals(9, count($res));
2022-12-16 06:00:05 +13:00
$this->assertEquals(24, count($res['requests']));
$this->assertEquals(24, count($res['users']));
2022-12-15 20:56:06 +13:00
$this->assertEquals($usersCount, $res['users'][array_key_last($res['users'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['users']);
2022-07-04 01:10:26 +12:00
$this->assertEquals($requestsCount, $res['requests'][array_key_last($res['requests'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['requests']);
2022-07-04 01:10:26 +12:00
2022-12-16 06:00:05 +13:00
$res = $this->client->call(
Client::METHOD_GET,
'/users/usage?range=90d',
$consoleHeaders
);
2022-12-15 20:56:06 +13:00
2022-07-04 01:10:26 +12:00
$res = $res['body'];
$this->assertEquals('90d', $res['range']);
$this->assertEquals(90, count($res['usersCount']));
$this->assertEquals(90, count($res['sessionsCount']));
2022-12-16 06:00:05 +13:00
$this->assertEquals((self::CREATE / 2), $res['usersCount'][array_key_last($res['usersCount'])]['value']);
2022-12-15 20:56:06 +13:00
return [
'projectId' => $projectId,
'headers' => $headers,
2022-12-15 20:56:06 +13:00
'consoleHeaders' => $consoleHeaders,
'requestsCount' => $requestsCount,
];
2022-07-04 01:10:26 +12:00
}
2022-07-28 19:28:00 +12:00
/** @depends testUsersStats */
public function testPrepareStorageStats(array $data): array
2022-07-04 21:00:07 +12:00
{
2022-07-28 19:28:00 +12:00
$headers = $data['headers'];
2022-07-04 01:10:26 +12:00
$bucketsCount = 0;
2022-07-28 21:01:03 +12:00
$requestsCount = $data['requestsCount'];
2022-07-04 01:10:26 +12:00
$storageTotal = 0;
$filesCount = 0;
2022-12-16 06:00:05 +13:00
for ($i = 0; $i < self::CREATE; $i++) {
2022-07-04 01:10:26 +12:00
$name = uniqid() . ' bucket';
2022-12-16 06:00:05 +13:00
$res = $this->client->call(
Client::METHOD_POST,
'/storage/buckets',
array_merge(
$headers,
['content-type' => 'multipart/form-data']
),
[
2022-07-04 01:10:26 +12:00
'bucketId' => 'unique()',
'name' => $name,
2022-11-19 02:13:33 +13:00
'fileSecurity' => false,
'permissions' => [
Permission::read(Role::any()),
Permission::create(Role::any()),
Permission::update(Role::any()),
Permission::delete(Role::any()),
],
2022-12-16 06:00:05 +13:00
]
);
2022-07-04 01:10:26 +12:00
$this->assertEquals($name, $res['body']['name']);
$this->assertNotEmpty($res['body']['$id']);
$bucketId = $res['body']['$id'];
$bucketsCount++;
$requestsCount++;
2022-12-16 06:00:05 +13:00
if ($i < (self::CREATE / 2)) {
$res = $this->client->call(
Client::METHOD_DELETE,
'/storage/buckets/' . $bucketId,
$headers
);
2022-07-04 01:10:26 +12:00
$this->assertEmpty($res['body']);
$requestsCount++;
2022-07-04 01:10:26 +12:00
$bucketsCount--;
}
}
// upload some files
2022-07-04 21:00:07 +12:00
$files = [
[
'path' => realpath(__DIR__ . '/../../resources/logo.png'),
'name' => 'logo.png',
],
[
'path' => realpath(__DIR__ . '/../../resources/file.png'),
'name' => 'file.png',
],
[
'path' => realpath(__DIR__ . '/../../resources/disk-a/kitten-3.gif'),
'name' => 'kitten-3.gif',
],
[
'path' => realpath(__DIR__ . '/../../resources/disk-a/kitten-1.jpg'),
'name' => 'kitten-1.jpg',
],
2022-09-16 11:48:09 +12:00
];
2022-07-04 21:00:07 +12:00
2022-12-16 06:00:05 +13:00
for ($i = 0; $i < self::CREATE; $i++) {
2022-07-04 21:00:07 +12:00
$file = $files[$i % count($files)];
2022-12-15 20:56:06 +13:00
2022-12-16 06:00:05 +13:00
$res = $this->client->call(
Client::METHOD_POST,
'/storage/buckets/' . $bucketId . '/files',
array_merge($headers, ['content-type' => 'multipart/form-data']),
[
2022-07-04 01:10:26 +12:00
'fileId' => 'unique()',
2022-07-04 21:00:07 +12:00
'file' => new CURLFile($file['path'], '', $file['name']),
2022-12-16 06:00:05 +13:00
]
);
2022-12-15 20:56:06 +13:00
2022-07-04 01:10:26 +12:00
$this->assertNotEmpty($res['body']['$id']);
2022-07-04 21:00:07 +12:00
$fileSize = $res['body']['sizeOriginal'];
$storageTotal += $fileSize;
2022-07-04 01:10:26 +12:00
$filesCount++;
$requestsCount++;
2022-07-04 21:00:07 +12:00
$fileId = $res['body']['$id'];
2022-12-16 06:00:05 +13:00
if ($i < (self::CREATE / 2)) {
$res = $this->client->call(
Client::METHOD_DELETE,
'/storage/buckets/' . $bucketId . '/files/' . $fileId,
$headers
);
2022-07-04 01:10:26 +12:00
$this->assertEmpty($res['body']);
$requestsCount++;
2022-07-04 01:10:26 +12:00
$filesCount--;
2022-07-04 21:00:07 +12:00
$storageTotal -= $fileSize;
2022-07-04 01:10:26 +12:00
}
}
2022-07-04 21:00:07 +12:00
return array_merge($data, [
'bucketId' => $bucketId,
'bucketsCount' => $bucketsCount,
'requestsCount' => $requestsCount,
'storageTotal' => $storageTotal,
'filesCount' => $filesCount,
]);
}
/**
* @depends testPrepareStorageStats
*/
public function testStorageStats(array $data): array
{
$bucketId = $data['bucketId'];
$bucketsCount = $data['bucketsCount'];
$requestsCount = $data['requestsCount'];
$storageTotal = $data['storageTotal'];
$filesCount = $data['filesCount'];
sleep(self::WAIT);
2022-07-04 01:10:26 +12:00
2022-12-16 06:00:05 +13:00
$res = $this->client->call(
Client::METHOD_GET,
'/project/usage?range=30d',
array_merge(
$data['headers'],
$data['consoleHeaders']
)
);
2022-07-04 01:10:26 +12:00
$res = $res['body'];
2022-11-09 03:16:10 +13:00
$this->assertEquals(9, count($res));
2022-07-04 01:10:26 +12:00
$this->assertEquals(30, count($res['requests']));
2022-07-04 21:00:07 +12:00
$this->assertEquals(30, count($res['storage']));
2022-07-04 01:10:26 +12:00
$this->assertEquals($requestsCount, $res['requests'][array_key_last($res['requests'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['requests']);
2022-12-15 22:45:43 +13:00
2022-07-04 21:00:07 +12:00
$this->assertEquals($storageTotal, $res['storage'][array_key_last($res['storage'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['storage']);
2022-07-03 14:42:13 +12:00
2022-12-16 06:00:05 +13:00
$res = $this->client->call(
Client::METHOD_GET,
'/storage/usage?range=30d',
array_merge(
$data['headers'],
$data['consoleHeaders']
)
);
2022-12-15 20:56:06 +13:00
2022-07-03 14:42:13 +12:00
$res = $res['body'];
2022-12-15 20:56:06 +13:00
$this->assertEquals($storageTotal, $res['filesStorage'][array_key_last($res['filesStorage'])]['value']);
$this->validateDates($res['filesStorage']);
2022-07-04 21:00:07 +12:00
$this->assertEquals($bucketsCount, $res['bucketsCount'][array_key_last($res['bucketsCount'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['bucketsCount']);
2022-07-04 21:00:07 +12:00
$this->assertEquals($filesCount, $res['filesCount'][array_key_last($res['filesCount'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['filesCount']);
2022-07-04 21:00:07 +12:00
2022-12-16 06:00:05 +13:00
$res = $this->client->call(
Client::METHOD_GET,
'/storage/' . $bucketId . '/usage?range=30d',
array_merge(
$data['headers'],
$data['consoleHeaders']
)
);
2022-12-15 20:56:06 +13:00
2022-07-04 21:00:07 +12:00
$res = $res['body'];
$this->assertEquals($storageTotal, $res['filesStorage'][array_key_last($res['filesStorage'])]['value']);
$this->assertEquals($filesCount, $res['filesCount'][array_key_last($res['filesCount'])]['value']);
2022-07-28 19:28:00 +12:00
2022-07-28 21:01:03 +12:00
$data['requestsCount'] = $requestsCount;
2022-12-15 20:56:06 +13:00
2022-07-28 19:28:00 +12:00
return $data;
2022-06-30 16:27:11 +12:00
}
2022-07-28 21:01:03 +12:00
/** @depends testStorageStats */
public function testPrepareDatabaseStats(array $data): array
2022-07-05 18:53:57 +12:00
{
2022-07-28 19:28:00 +12:00
$headers = $data['headers'];
2022-07-05 19:11:27 +12:00
2022-07-28 21:01:03 +12:00
$requestsCount = $data['requestsCount'];
2022-07-05 18:53:57 +12:00
$databasesCount = 0;
$collectionsCount = 0;
$documentsCount = 0;
2022-12-16 06:00:05 +13:00
for ($i = 0; $i < self::CREATE; $i++) {
2022-07-05 18:53:57 +12:00
$name = uniqid() . ' database';
2022-12-16 06:00:05 +13:00
$res = $this->client->call(
Client::METHOD_POST,
'/databases',
array_merge($headers, ['content-type' => 'multipart/form-data']),
[
2022-07-05 18:53:57 +12:00
'databaseId' => 'unique()',
'name' => $name,
2022-12-16 06:00:05 +13:00
]
);
2022-12-15 20:56:06 +13:00
2022-07-05 18:53:57 +12:00
$this->assertEquals($name, $res['body']['name']);
$this->assertNotEmpty($res['body']['$id']);
$databaseId = $res['body']['$id'];
$requestsCount++;
$databasesCount++;
2022-12-16 06:00:05 +13:00
if ($i < (self::CREATE / 2)) {
$res = $this->client->call(
Client::METHOD_DELETE,
'/databases/' . $databaseId,
$headers
);
2022-07-05 18:53:57 +12:00
$this->assertEmpty($res['body']);
$databasesCount--;
2022-12-15 20:56:06 +13:00
$requestsCount++;
2022-07-05 18:53:57 +12:00
}
}
2022-12-16 06:00:05 +13:00
for ($i = 0; $i < self::CREATE; $i++) {
2022-07-05 18:53:57 +12:00
$name = uniqid() . ' collection';
2022-12-16 06:00:05 +13:00
$res = $this->client->call(
Client::METHOD_POST,
'/databases/' . $databaseId . '/collections',
array_merge($headers, ['content-type' => 'multipart/form-data']),
[
2022-07-05 18:53:57 +12:00
'collectionId' => 'unique()',
'name' => $name,
2022-08-26 10:10:52 +12:00
'documentSecurity' => false,
'permissions' => [
Permission::read(Role::any()),
Permission::create(Role::any()),
Permission::update(Role::any()),
Permission::delete(Role::any()),
],
2022-12-16 06:00:05 +13:00
]
);
2022-12-15 20:56:06 +13:00
2022-07-05 18:53:57 +12:00
$this->assertEquals($name, $res['body']['name']);
$this->assertNotEmpty($res['body']['$id']);
$collectionId = $res['body']['$id'];
$requestsCount++;
$collectionsCount++;
2022-12-16 06:00:05 +13:00
if ($i < (self::CREATE / 2)) {
$res = $this->client->call(
Client::METHOD_DELETE,
'/databases/' . $databaseId . '/collections/' . $collectionId,
$headers
);
2022-07-05 18:53:57 +12:00
$this->assertEmpty($res['body']);
$collectionsCount--;
2022-12-15 20:56:06 +13:00
$requestsCount++;
2022-07-05 18:53:57 +12:00
}
}
2022-12-16 06:00:05 +13:00
$res = $this->client->call(
Client::METHOD_POST,
'/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes' . '/string',
array_merge($headers, ['content-type' => 'multipart/form-data']),
[
2022-07-05 18:53:57 +12:00
'key' => 'name',
'size' => 255,
'required' => true,
2022-12-16 06:00:05 +13:00
]
);
2022-12-15 20:56:06 +13:00
2022-07-05 18:53:57 +12:00
$this->assertEquals('name', $res['body']['key']);
$requestsCount++;
2022-12-15 20:56:06 +13:00
sleep(self::WAIT);
2022-07-05 18:53:57 +12:00
2022-12-16 06:00:05 +13:00
for ($i = 0; $i < self::CREATE; $i++) {
2022-07-05 18:53:57 +12:00
$name = uniqid() . ' collection';
2022-12-16 06:00:05 +13:00
$res = $this->client->call(
Client::METHOD_POST,
'/databases/' . $databaseId . '/collections/' . $collectionId . '/documents',
array_merge($headers, ['content-type' => 'multipart/form-data']),
[
2022-07-05 18:53:57 +12:00
'documentId' => 'unique()',
'data' => ['name' => $name]
2022-12-16 06:00:05 +13:00
]
);
2022-07-05 18:53:57 +12:00
$this->assertEquals($name, $res['body']['name']);
$this->assertNotEmpty($res['body']['$id']);
$documentId = $res['body']['$id'];
$requestsCount++;
$documentsCount++;
2022-12-16 06:00:05 +13:00
if ($i < (self::CREATE / 2)) {
$res = $this->client->call(
Client::METHOD_DELETE,
'/databases/' . $databaseId . '/collections/' . $collectionId . '/documents/' . $documentId,
$headers
);
2022-07-05 18:53:57 +12:00
$this->assertEmpty($res['body']);
$documentsCount--;
2022-12-15 20:56:06 +13:00
$requestsCount++;
2022-07-05 18:53:57 +12:00
}
}
2022-12-16 06:00:05 +13:00
return array_merge($data, [
'databaseId' => $databaseId,
'collectionId' => $collectionId,
'requestsCount' => $requestsCount,
'databasesCount' => $databasesCount,
'collectionsCount' => $collectionsCount,
'documentsCount' => $documentsCount,
]);
}
/** @depends testPrepareDatabaseStats */
2022-12-15 20:56:06 +13:00
public function testDatabaseStats(array $data): array
{
2022-12-15 20:56:06 +13:00
$projectId = $data['projectId'];
$databaseId = $data['databaseId'];
$collectionId = $data['collectionId'];
$requestsCount = $data['requestsCount'];
$databasesCount = $data['databasesCount'];
$collectionsCount = $data['collectionsCount'];
$documentsCount = $data['documentsCount'];
2022-12-15 20:56:06 +13:00
sleep(self::WAIT);
2022-11-19 02:13:33 +13:00
2022-12-16 06:00:05 +13:00
$res = $this->client->call(
Client::METHOD_GET,
'/project/usage?range=30d',
$data['consoleHeaders']
);
2022-07-05 18:53:57 +12:00
$res = $res['body'];
2022-11-09 03:16:10 +13:00
$this->assertEquals(9, count($res));
2022-07-05 18:53:57 +12:00
$this->assertEquals(30, count($res['requests']));
$this->assertEquals(30, count($res['storage']));
$this->assertEquals($requestsCount, $res['requests'][array_key_last($res['requests'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['requests']);
2022-11-09 22:29:13 +13:00
$this->assertEquals($databasesCount, $res['databases'][array_key_last($res['databases'])]['value']);
$this->validateDates($res['databases']);
2022-07-05 18:53:57 +12:00
$this->assertEquals($documentsCount, $res['documents'][array_key_last($res['documents'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['documents']);
2022-07-05 18:53:57 +12:00
2022-12-16 06:00:05 +13:00
$res = $this->client->call(
Client::METHOD_GET,
'/databases/usage?range=30d',
$data['consoleHeaders']
);
2022-07-05 18:53:57 +12:00
$res = $res['body'];
2022-12-15 20:56:06 +13:00
2022-07-05 18:53:57 +12:00
$this->assertEquals($databasesCount, $res['databasesCount'][array_key_last($res['databasesCount'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['databasesCount']);
2022-07-05 18:53:57 +12:00
$this->assertEquals($collectionsCount, $res['collectionsCount'][array_key_last($res['collectionsCount'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['collectionsCount']);
2022-07-05 18:53:57 +12:00
$this->assertEquals($documentsCount, $res['documentsCount'][array_key_last($res['documentsCount'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['documentsCount']);
2022-07-05 18:53:57 +12:00
2022-12-16 06:00:05 +13:00
$res = $this->client->call(
Client::METHOD_GET,
'/databases/' . $databaseId . '/usage?range=30d',
$data['consoleHeaders']
);
2022-07-05 18:53:57 +12:00
$res = $res['body'];
2022-12-15 20:56:06 +13:00
2022-07-05 18:53:57 +12:00
$this->assertEquals($collectionsCount, $res['collectionsCount'][array_key_last($res['collectionsCount'])]['value']);
2022-09-16 11:48:09 +12:00
$this->validateDates($res['collectionsCount']);
2022-07-05 18:53:57 +12:00
2022-12-15 20:56:06 +13:00
// $this->assertEquals($documentsCount, $res['documentsCount'][array_key_last($res['documentsCount'])]['value']);
// $this->validateDates($res['documentsCount']);
//
// $res = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $collectionId . '/usage?range=30d', $data['consoleHeaders']);
// $res = $res['body'];
//
// $this->assertEquals($documentsCount, $res['documentsCount'][array_key_last($res['documentsCount'])]['value']);
// $this->validateDates($res['documentsCount']);
2022-07-28 19:28:00 +12:00
2022-07-28 21:01:03 +12:00
$data['requestsCount'] = $requestsCount;
2022-12-15 20:56:06 +13:00
2022-07-28 19:28:00 +12:00
return $data;
2022-07-05 18:53:57 +12:00
}
2022-08-18 13:49:41 +12:00
2022-07-28 21:01:03 +12:00
/** @depends testDatabaseStats */
public function testPrepareFunctionsStats(array $data): array
2022-07-05 18:53:57 +12:00
{
2022-08-18 13:49:41 +12:00
$headers = $data['headers'];
2022-07-17 22:30:58 +12:00
$executionTime = 0;
2022-07-10 14:12:49 +12:00
$executions = 0;
$failures = 0;
2022-12-16 06:00:05 +13:00
$response1 = $this->client->call(
Client::METHOD_POST,
'/functions',
$headers,
[
2022-07-10 14:12:49 +12:00
'functionId' => 'unique()',
'name' => 'Test',
'runtime' => 'php-8.0',
'vars' => [
'funcKey1' => 'funcValue1',
'funcKey2' => 'funcValue2',
'funcKey3' => 'funcValue3',
],
'events' => [
'users.*.create',
'users.*.delete',
],
'schedule' => '0 0 1 1 *',
'timeout' => 10,
2022-12-16 06:00:05 +13:00
]
);
2022-07-10 14:12:49 +12:00
$functionId = $response1['body']['$id'] ?? '';
$this->assertEquals(201, $response1['headers']['status-code']);
$this->assertNotEmpty($response1['body']['$id']);
2022-08-18 13:49:41 +12:00
$code = realpath(__DIR__ . '/../../resources/functions') . "/php/code.tar.gz";
$this->packageCode('php');
2022-07-10 14:12:49 +12:00
2022-12-16 06:00:05 +13:00
$deployment = $this->client->call(
Client::METHOD_POST,
'/functions/' . $functionId . '/deployments',
array_merge($headers, ['content-type' => 'multipart/form-data',]),
[
2022-07-10 14:12:49 +12:00
'entrypoint' => 'index.php',
'code' => new CURLFile($code, 'application/x-gzip', \basename($code)),
2022-07-17 21:33:24 +12:00
'activate' => true
2022-12-16 06:00:05 +13:00
]
);
2022-07-10 14:12:49 +12:00
$deploymentId = $deployment['body']['$id'] ?? '';
2022-08-17 22:36:30 +12:00
$this->assertEquals(202, $deployment['headers']['status-code']);
2022-07-10 14:12:49 +12:00
$this->assertNotEmpty($deployment['body']['$id']);
2022-08-26 10:10:52 +12:00
$this->assertEquals(true, DateTime::isValid($deployment['body']['$createdAt']));
2022-07-10 14:12:49 +12:00
$this->assertEquals('index.php', $deployment['body']['entrypoint']);
// Wait for deployment to build.
2022-12-15 20:56:06 +13:00
sleep(self::WAIT + 20);
2022-07-17 21:33:24 +12:00
2022-12-16 06:00:05 +13:00
$response = $this->client->call(
Client::METHOD_PATCH,
'/functions/' . $functionId . '/deployments/' . $deploymentId,
$headers
);
2022-07-17 21:33:24 +12:00
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertNotEmpty($response['body']['$id']);
2022-08-26 10:10:52 +12:00
$this->assertEquals(true, DateTime::isValid($response['body']['$createdAt']));
$this->assertEquals(true, DateTime::isValid($response['body']['$updatedAt']));
2022-07-17 21:33:24 +12:00
$this->assertEquals($deploymentId, $response['body']['deployment']);
2022-07-17 22:30:58 +12:00
2022-12-16 06:00:05 +13:00
$execution = $this->client->call(
Client::METHOD_POST,
'/functions/' . $functionId . '/executions',
$headers,
[
2022-07-10 14:12:49 +12:00
'async' => false,
2022-12-16 06:00:05 +13:00
]
);
2022-07-17 22:30:58 +12:00
2022-07-10 14:12:49 +12:00
$this->assertEquals(201, $execution['headers']['status-code']);
$this->assertNotEmpty($execution['body']['$id']);
$this->assertEquals($functionId, $execution['body']['functionId']);
2022-12-15 20:56:06 +13:00
2022-09-10 08:19:09 +12:00
$executionTime += (int) ($execution['body']['duration'] * 1000);
2022-12-15 22:45:43 +13:00
2022-08-18 13:50:55 +12:00
if ($execution['body']['status'] == 'failed') {
2022-08-18 13:49:41 +12:00
$failures++;
2022-08-18 17:13:30 +12:00
} elseif ($execution['body']['status'] == 'completed') {
2022-08-18 13:49:41 +12:00
$executions++;
}
2022-07-17 22:30:58 +12:00
2022-12-16 06:00:05 +13:00
$execution = $this->client->call(
Client::METHOD_POST,
'/functions/' . $functionId . '/executions',
$headers,
[
2022-08-17 22:36:30 +12:00
'async' => false,
2022-12-16 06:00:05 +13:00
]
);
2022-07-17 22:30:58 +12:00
2022-07-17 21:33:24 +12:00
$this->assertEquals(201, $execution['headers']['status-code']);
$this->assertNotEmpty($execution['body']['$id']);
$this->assertEquals($functionId, $execution['body']['functionId']);
2022-08-18 13:50:55 +12:00
if ($execution['body']['status'] == 'failed') {
2022-08-18 13:49:41 +12:00
$failures++;
2022-11-15 22:33:07 +13:00
} elseif ($execution['body']['status'] == 'completed') {
$executions++;
}
$executionTime += (int) ($execution['body']['duration'] * 1000);
2022-12-16 06:00:05 +13:00
$execution = $this->client->call(
Client::METHOD_POST,
'/functions/' . $functionId . '/executions',
$headers,
[
2022-11-15 22:33:07 +13:00
'async' => true,
2022-12-16 06:00:05 +13:00
]
);
2022-11-15 22:33:07 +13:00
2022-11-15 22:41:55 +13:00
$this->assertEquals(202, $execution['headers']['status-code']);
2022-11-15 22:33:07 +13:00
$this->assertNotEmpty($execution['body']['$id']);
$this->assertEquals($functionId, $execution['body']['functionId']);
2022-12-15 20:56:06 +13:00
sleep(self::WAIT);
2022-11-15 22:33:07 +13:00
2022-12-16 06:00:05 +13:00
$execution = $this->client->call(
Client::METHOD_GET,
'/functions/' . $functionId . '/executions/' . $execution['body']['$id'],
$headers
);
2022-11-15 22:33:07 +13:00
if ($execution['body']['status'] == 'failed') {
$failures++;
2022-08-18 17:13:30 +12:00
} elseif ($execution['body']['status'] == 'completed') {
2022-08-18 13:49:41 +12:00
$executions++;
}
2022-12-15 22:45:43 +13:00
2022-09-10 09:47:24 +12:00
$executionTime += (int) ($execution['body']['duration'] * 1000);
2022-07-17 21:33:24 +12:00
2022-12-16 06:00:05 +13:00
return array_merge($data, [
'functionId' => $functionId,
'executionTime' => $executionTime,
'executions' => $executions,
'failures' => $failures,
]);
}
/** @depends testPrepareFunctionsStats */
public function testFunctionsStats(array $data): void
{
$functionId = $data['functionId'];
$executionTime = $data['executionTime'];
$executions = $data['executions'];
2022-07-10 14:12:49 +12:00
2022-12-15 20:56:06 +13:00
sleep(self::WAIT);
2022-12-16 06:00:05 +13:00
$response = $this->client->call(
Client::METHOD_GET,
'/functions/' . $functionId . '/usage?range=30d',
$data['consoleHeaders']
);
2022-07-10 14:12:49 +12:00
2022-07-17 22:30:58 +12:00
$this->assertEquals(200, $response['headers']['status-code']);
2022-12-15 20:56:06 +13:00
$this->assertEquals(7, count($response['body']));
2022-07-17 22:30:58 +12:00
$this->assertEquals('30d', $response['body']['range']);
2022-12-15 20:56:06 +13:00
$this->assertIsArray($response['body']['deployments']);
$this->assertIsArray($response['body']['deploymentsStorage']);
$this->assertIsArray($response['body']['builds']);
$this->assertIsArray($response['body']['buildsCompute']);
$this->assertIsArray($response['body']['executions']);
$this->assertIsArray($response['body']['executionsCompute']);
2022-07-10 14:12:49 +12:00
2022-12-15 20:56:06 +13:00
$response = $response['body'];
2022-12-15 22:45:43 +13:00
2022-12-15 20:56:06 +13:00
$this->assertEquals($executions, $response['executions'][array_key_last($response['executions'])]['value']);
$this->validateDates($response['executions']);
2022-12-15 22:45:43 +13:00
2022-12-15 20:56:06 +13:00
$this->assertEquals($executionTime, $response['executionsCompute'][array_key_last($response['executionsCompute'])]['value']);
$this->validateDates($response['executionsCompute']);
2022-07-10 14:12:49 +12:00
2022-12-16 06:00:05 +13:00
$response = $this->client->call(
Client::METHOD_GET,
'/functions/usage?range=30d',
$data['consoleHeaders']
);
2022-07-17 22:30:58 +12:00
$this->assertEquals(200, $response['headers']['status-code']);
2022-12-15 22:45:43 +13:00
$this->assertEquals(8, count($response['body']));
2022-07-17 22:30:58 +12:00
$this->assertEquals($response['body']['range'], '30d');
2022-12-15 20:56:06 +13:00
$this->assertIsArray($response['body']['functions']);
$this->assertIsArray($response['body']['deployments']);
$this->assertIsArray($response['body']['deploymentsStorage']);
$this->assertIsArray($response['body']['builds']);
$this->assertIsArray($response['body']['buildsCompute']);
$this->assertIsArray($response['body']['executions']);
$this->assertIsArray($response['body']['executionsCompute']);
2022-07-17 22:30:58 +12:00
$response = $response['body'];
2022-12-15 20:56:06 +13:00
$this->assertEquals($executions, $response['executions'][array_key_last($response['executions'])]['value']);
$this->validateDates($response['executions']);
$this->assertEquals($executionTime, $response['executionsCompute'][array_key_last($response['executionsCompute'])]['value']);
$this->validateDates($response['executionsCompute']);
$this->assertGreaterThan(0, $response['buildsCompute'][array_key_last($response['buildsCompute'])]['value']);
$this->validateDates($response['buildsCompute']);
2022-08-17 22:36:30 +12:00
}
2022-07-27 20:28:23 +12:00
protected function tearDown(): void
{
$this->usersCount = 0;
$this->requestsCount = 0;
2022-07-28 19:28:00 +12:00
$projectId = '';
$headers = [];
2022-07-27 20:28:23 +12:00
}
2022-06-30 21:40:27 +12:00
}