From d87f47ee3c272fa7b6ea3260a026d314da65cbc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 19 Aug 2024 14:18:57 +0000 Subject: [PATCH] Improve tests --- src/Appwrite/Platform/Workers/Builds.php | 2 +- tests/e2e/Services/Account/AccountBase.php | 1 + .../Functions/FunctionsCustomServerTest.php | 77 +++++++++++++++++- .../php-large/{Blue - 27239.mp4 => blue.mp4} | Bin 4 files changed, 77 insertions(+), 3 deletions(-) rename tests/resources/functions/php-large/{Blue - 27239.mp4 => blue.mp4} (100%) diff --git a/src/Appwrite/Platform/Workers/Builds.php b/src/Appwrite/Platform/Workers/Builds.php index c10640d2e3..ac636e9329 100644 --- a/src/Appwrite/Platform/Workers/Builds.php +++ b/src/Appwrite/Platform/Workers/Builds.php @@ -262,7 +262,7 @@ class Builds extends Action Console::execute('rm -rf ' . \escapeshellarg($tmpTemplateDirectory), '', $stdout, $stderr); $build = $dbForProject->updateDocument('builds', $build->getId(), $build->setAttribute('source', $source)); - $deployment = $dbForProject->updateDocument('deployments', $deployment->getId(), $deployment->setAttribute('path', $source)); + $deployment = $dbForProject->updateDocument('deployments', $deployment->getId(), $deployment->setAttribute('path', $source)->setAttribute('size', $directorySize)); } } elseif ($isNewBuild && $isVcsEnabled) { // VCS and VCS+Temaplte diff --git a/tests/e2e/Services/Account/AccountBase.php b/tests/e2e/Services/Account/AccountBase.php index 2d72625121..ad078298a9 100644 --- a/tests/e2e/Services/Account/AccountBase.php +++ b/tests/e2e/Services/Account/AccountBase.php @@ -13,6 +13,7 @@ trait AccountBase $email = uniqid() . 'user@localhost.test'; $password = 'password'; $name = 'User Name'; + \var_dump($email); /** * Test for SUCCESS diff --git a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php index e6908efc43..885c2151bf 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php @@ -730,7 +730,8 @@ class FunctionsCustomServerTest extends Scope $largeTag = $this->client->call(Client::METHOD_POST, '/functions/' . $data['functionId'] . '/deployments', array_merge($headers, $this->getHeaders()), [ 'entrypoint' => 'index.php', 'code' => $curlFile, - 'activate' => true + 'activate' => true, + 'commands' => 'cp blue.mp4 copy.mp4 && ls -al' // +7MB buildSize ]); $counter++; $id = $largeTag['body']['$id']; @@ -741,7 +742,23 @@ class FunctionsCustomServerTest extends Scope $this->assertNotEmpty($largeTag['body']['$id']); $this->assertEquals(true, (new DatetimeValidator())->isValid($largeTag['body']['$createdAt'])); $this->assertEquals('index.php', $largeTag['body']['entrypoint']); - $this->assertGreaterThan(10000, $largeTag['body']['size']); + $this->assertGreaterThan(1024 * 1024 * 5, $largeTag['body']['size']); // ~7MB video file + $this->assertLessThan(1024 * 1024 * 10, $largeTag['body']['size']); // ~7MB video file + + $deploymentSize = $largeTag['body']['size']; + + $deploymentId = $largeTag['body']['$id']; + + $this->awaitDeploymentIsBuilt($data['functionId'], $deploymentId, true); + + $response = $this->client->call(Client::METHOD_GET, '/functions/' . $data['functionId'] . '/deployments/' . $deploymentId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), []); + + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals($deploymentSize, $response['body']['size']); + $this->assertGreaterThan(1024 * 1024 * 10, $response['body']['buildSize']); // ~7MB video file + 10MB sample file return $data; } @@ -790,6 +807,8 @@ class FunctionsCustomServerTest extends Scope $this->assertEquals($function['body']['total'], 3); $this->assertIsArray($function['body']['deployments']); $this->assertCount(3, $function['body']['deployments']); + $this->assertArrayHasKey('size', $function['body']['deployments'][0]); + $this->assertArrayHasKey('buildSize', $function['body']['deployments'][0]); /** * Test search queries @@ -995,6 +1014,58 @@ class FunctionsCustomServerTest extends Scope $this->assertEquals($function['headers']['status-code'], 200); $this->assertEquals(3, $function['body']['total']); + /** + * Ensure size output and size filters work exactly. + * Prevents buildSize being counted towards deployemtn size + */ + $response = $this->client->call( + Client::METHOD_GET, + '/functions/' . $data['functionId'] . '/deployments', + array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), + [ + Query::limit(1)->toString(), + ] + ); + + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertGreaterThanOrEqual(1, $response['body']['total']); + $this->assertNotEmpty($response['body']['deployments'][0]['$id']); + $this->assertNotEmpty($response['body']['deployments'][0]['size']); + + $deploymentId = $function['body']['deployments'][0]['$id']; + $deploymentSize = $function['body']['deployments'][0]['size']; + + $response = $this->client->call( + Client::METHOD_GET, + '/functions/' . $data['functionId'] . '/deployments', + array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), + [ + 'queries' => [ + Query::equal('size', [$deploymentSize])->toString(), + ], + ] + ); + + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertGreaterThan(0, $response['body']['total']); + + $found = false; + foreach ($response['body']['deployments'] as $deployment) { + if($deployment['$id'] === $deploymentId) { + $found = true; + $this->assertEquals($deploymentSize, $deployment['size']); + break; + } + } + + $this->assertTrue($found); + return $data; } @@ -1015,6 +1086,8 @@ class FunctionsCustomServerTest extends Scope $this->assertGreaterThan(0, $function['body']['buildTime']); $this->assertNotEmpty($function['body']['status']); $this->assertNotEmpty($function['body']['buildLogs']); + $this->assertArrayHasKey('size', $function['body']); + $this->assertArrayHasKey('buildSize', $function['body']); /** * Test for FAILURE diff --git a/tests/resources/functions/php-large/Blue - 27239.mp4 b/tests/resources/functions/php-large/blue.mp4 similarity index 100% rename from tests/resources/functions/php-large/Blue - 27239.mp4 rename to tests/resources/functions/php-large/blue.mp4