Refactor Sites deployment test coverage (#12531)

* Refactor Sites deployment test coverage

* fix refactor
This commit is contained in:
Chirag Aggarwal authored and GitHub committed 2026-06-08 11:33:19 +05:30
1 parent 42f9077738
commit f9e7e0e549
4 files changed
+183 -978

No files matched your search

@@ -690,25 +690,7 @@ class Builds extends Action
} else {
$outputDirectory = $deployment->getAttribute('buildOutput') ?? $resource->getAttribute('outputDirectory');
if ($resource->getCollection() === 'sites') {
$listFilesCommand = '';
// Start separation, enter build folder
$listFilesCommand .= 'echo "{APPWRITE_DETECTION_SEPARATOR_START}" && cd /usr/local/build';
// Enter output directory, if set
if (! empty($outputDirectory)) {
$listFilesCommand .= ' && cd ' . \escapeshellarg($outputDirectory);
}
// Print files, and end separation
$listFilesCommand .= ' && find . -name \'node_modules\' -prune -o -type f -print && echo "{APPWRITE_DETECTION_SEPARATOR_END}"';
// Use SSR file listing
if (empty($command)) {
$command = $listFilesCommand;
} else {
$command .= ' && ' . $listFilesCommand;
}
$command = $this->prepareSiteBuildCommand($command, $outputDirectory ?? '');
}
$command = 'tar -zxf /tmp/code.tar.gz -C /mnt/code && helpers/build.sh ' . \trim(\escapeshellarg($command));
@@ -875,31 +857,13 @@ class Builds extends Action
$logs .= $log['content'];
}
// Separate logs for SSR detection
$detectionLogs = '';
if (\str_contains($logs, '{APPWRITE_DETECTION_SEPARATOR_START}')) {
[$logsBefore, $detectionLogsStart] = \explode('{APPWRITE_DETECTION_SEPARATOR_START}', $logs, 2);
[$detectionLogs, $logsAfter] = \explode('{APPWRITE_DETECTION_SEPARATOR_END}', $detectionLogsStart, 2);
$logs = $logsBefore . $logsAfter;
}
['logs' => $logs, 'detectionLogs' => $detectionLogs] = $this->splitSiteDetectionLogs($logs);
$deployment->setAttribute('buildLogs', $logs);
$adapter = null;
if ($resource->getCollection() === 'sites' && ! empty($detectionLogs)) {
$files = \explode("\n", $detectionLogs); // Parse output
$files = \array_filter($files); // Remove empty
$files = \array_map(fn ($file) => \trim($file), $files); // Remove whitepsaces
$files = \array_map(fn ($file) => \str_starts_with($file, './') ? \substr($file, 2) : $file, $files); // Remove beginning ./
$detector = new Rendering($resource->getAttribute('framework', ''));
foreach ($files as $file) {
$detector->addInput($file);
}
$detector
->addOption(new SSR())
->addOption(new XStatic());
$detection = $detector->detect();
$detection = $this->detectSiteRendering($resource->getAttribute('framework', ''), $detectionLogs);
$adapter = $resource->getAttribute('adapter', '');
if (empty($adapter)) {
@@ -1337,6 +1301,62 @@ class Builds extends Action
return '';
}
protected function prepareSiteBuildCommand(string $command, string $outputDirectory): string
{
$listFilesCommand = 'echo "{APPWRITE_DETECTION_SEPARATOR_START}" && cd /usr/local/build';
if (! empty($outputDirectory)) {
$listFilesCommand .= ' && cd ' . \escapeshellarg($outputDirectory);
}
$listFilesCommand .= ' && find . -name \'node_modules\' -prune -o -type f -print && echo "{APPWRITE_DETECTION_SEPARATOR_END}"';
if (empty($command)) {
return $listFilesCommand;
}
return "{$command} && {$listFilesCommand}";
}
/**
* @return array{logs: string, detectionLogs: string}
*/
protected function splitSiteDetectionLogs(string $logs): array
{
if (! \str_contains($logs, '{APPWRITE_DETECTION_SEPARATOR_START}')) {
return [
'logs' => $logs,
'detectionLogs' => '',
];
}
[$logsBefore, $detectionLogsStart] = \explode('{APPWRITE_DETECTION_SEPARATOR_START}', $logs, 2);
[$detectionLogs, $logsAfter] = \explode('{APPWRITE_DETECTION_SEPARATOR_END}', $detectionLogsStart, 2);
return [
'logs' => "{$logsBefore}{$logsAfter}",
'detectionLogs' => $detectionLogs,
];
}
protected function detectSiteRendering(string $framework, string $detectionLogs): object
{
$files = \explode("\n", $detectionLogs);
$files = \array_filter($files);
$files = \array_map(\trim(...), $files);
$files = \array_map(fn ($file) => \str_starts_with($file, './') ? \substr($file, 2) : $file, $files);
$detector = new Rendering($framework);
foreach ($files as $file) {
$detector->addInput($file);
}
return $detector
->addOption(new SSR())
->addOption(new XStatic())
->detect();
}
/**
* @throws Structure
* @throws \Utopia\Database\Exception
+30 -14
View File
@@ -34,6 +34,20 @@ trait SitesBase
}
protected function setupDeployment(string $siteId, mixed $params): string
{
$deploymentId = $this->createDeploymentAccepted($siteId, $params);
$this->waitDeploymentReady($siteId, $deploymentId);
// Not === so multipart/form-data works fine too
if (($params['activate'] ?? false) == true) {
$this->waitDeploymentActivated($siteId, $deploymentId);
}
return $deploymentId;
}
protected function createDeploymentAccepted(string $siteId, mixed $params): string
{
$deployment = $this->client->call(Client::METHOD_POST, '/sites/' . $siteId . '/deployments', array_merge([
'content-type' => 'multipart/form-data',
@@ -41,8 +55,12 @@ trait SitesBase
'x-appwrite-key' => $this->getProject()['apiKey'],
]), $params);
$this->assertEquals($deployment['headers']['status-code'], 202, 'Setup deployment failed with status code: ' . $deployment['headers']['status-code'] . ' and response: ' . json_encode($deployment['body'], JSON_PRETTY_PRINT));
$deploymentId = $deployment['body']['$id'] ?? '';
return $deployment['body']['$id'] ?? '';
}
protected function waitDeploymentReady(string $siteId, string $deploymentId): void
{
$this->assertEventually(function () use ($siteId, $deploymentId) {
$deployment = $this->client->call(Client::METHOD_GET, '/sites/' . $siteId . '/deployments/' . $deploymentId, array_merge([
'content-type' => 'application/json',
@@ -66,20 +84,18 @@ trait SitesBase
$this->assertEquals('ready', $deployment['body']['status'], 'Deployment status is not ready, deployment: ' . json_encode($deployment['body'], JSON_PRETTY_PRINT));
}, 120000, 500);
}
// Not === so multipart/form-data works fine too
if (($params['activate'] ?? false) == true) {
$this->assertEventually(function () use ($siteId, $deploymentId) {
$site = $this->client->call(Client::METHOD_GET, '/sites/' . $siteId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]));
$this->assertEquals($deploymentId, $site['body']['deploymentId'], 'Deployment is not activated, deployment: ' . json_encode($site['body'], JSON_PRETTY_PRINT));
}, 120000, 500);
}
return $deploymentId;
protected function waitDeploymentActivated(string $siteId, string $deploymentId): void
{
$this->assertEventually(function () use ($siteId, $deploymentId) {
$site = $this->client->call(Client::METHOD_GET, '/sites/' . $siteId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]));
$this->assertEquals($deploymentId, $site['body']['deploymentId'], 'Deployment is not activated, deployment: ' . json_encode($site['body'], JSON_PRETTY_PRINT));
}, 120000, 500);
}
protected function cleanupSite(string $siteId): void
@@ -714,253 +714,6 @@ final class SitesCustomServerTest extends Scope
$this->cleanupSite($siteId);
}
public function testAdapterDetectionAstroSSR(): void
{
$siteId = $this->setupSite([
'siteId' => ID::unique(),
'name' => 'Astro SSR site',
'framework' => 'astro',
'buildRuntime' => 'node-22',
'outputDirectory' => './dist',
'buildCommand' => 'npm run build',
'installCommand' => 'npm ci',
]);
$this->assertNotEmpty($siteId);
$site = $this->getSite($siteId);
$this->assertEquals('200', $site['headers']['status-code']);
$this->assertArrayHasKey('adapter', $site['body']);
$this->assertEmpty($site['body']['adapter']);
$domain = $this->setupSiteDomain($siteId);
$this->assertNotEmpty($domain);
$deploymentId = $this->setupDeployment($siteId, [
'code' => $this->packageSite('astro'),
'activate' => 'true'
]);
$this->assertNotEmpty($deploymentId);
$this->assertEventually(function () use ($siteId, &$site) {
$site = $this->getSite($siteId);
$this->assertEquals('ssr', $site['body']['adapter']);
});
$proxyClient = new Client();
$proxyClient->setEndpoint('http://' . $domain);
$response = $proxyClient->call(Client::METHOD_GET, '/');
$this->assertEquals(200, $response['headers']['status-code']);
$this->cleanupSite($siteId);
}
#[Retry(count: 3)]
public function testAdapterDetectionAstroStatic(): void
{
$siteId = $this->setupSite([
'siteId' => ID::unique(),
'name' => 'Astro static site',
'framework' => 'astro',
'buildRuntime' => 'node-22',
'outputDirectory' => './dist',
'buildCommand' => 'npm run build',
'installCommand' => 'npm ci',
]);
$this->assertNotEmpty($siteId);
$site = $this->getSite($siteId);
$this->assertEquals('200', $site['headers']['status-code']);
$this->assertArrayHasKey('adapter', $site['body']);
$this->assertEmpty($site['body']['adapter']);
$domain = $this->setupSiteDomain($siteId);
$this->assertNotEmpty($domain);
$deploymentId = $this->setupDeployment($siteId, [
'code' => $this->packageSite('astro-static'),
'activate' => 'true'
]);
$this->assertNotEmpty($deploymentId);
$site = $this->getSite($siteId);
$this->assertEquals('200', $site['headers']['status-code']);
$this->assertEquals('static', $site['body']['adapter']);
$proxyClient = new Client();
$proxyClient->setEndpoint('http://' . $domain);
$response = $proxyClient->call(Client::METHOD_GET, '/');
$this->assertEquals(200, $response['headers']['status-code']);
$this->cleanupSite($siteId);
}
public function testAdapterDetectionStatic(): void
{
$siteId = $this->setupSite([
'siteId' => ID::unique(),
'name' => 'Static site',
'framework' => 'other',
'buildRuntime' => 'node-22',
'outputDirectory' => '',
'buildCommand' => '',
'installCommand' => '',
]);
$this->assertNotEmpty($siteId);
$site = $this->getSite($siteId);
$this->assertEquals('200', $site['headers']['status-code']);
$this->assertArrayHasKey('adapter', $site['body']);
$this->assertEmpty($site['body']['adapter']);
$domain = $this->setupSiteDomain($siteId);
$this->assertNotEmpty($domain);
$deploymentId = $this->setupDeployment($siteId, [
'code' => $this->packageSite('static-single-file'),
'activate' => 'true'
]);
$this->assertNotEmpty($deploymentId);
$site = $this->getSite($siteId);
$this->assertEquals('200', $site['headers']['status-code']);
$this->assertEquals('static', $site['body']['adapter']);
$proxyClient = new Client();
$proxyClient->setEndpoint('http://' . $domain);
$response = $proxyClient->call(Client::METHOD_GET, '/');
$this->assertEquals(200, $response['headers']['status-code']);
$this->cleanupSite($siteId);
}
public function testAdapterDetectionStaticSPA(): void
{
$siteId = $this->setupSite([
'siteId' => ID::unique(),
'name' => 'Static site',
'framework' => 'other',
'buildRuntime' => 'node-22',
'outputDirectory' => '',
'buildCommand' => '',
'installCommand' => '',
]);
$this->assertNotEmpty($siteId);
$site = $this->getSite($siteId);
$this->assertEquals('200', $site['headers']['status-code']);
$this->assertArrayHasKey('adapter', $site['body']);
$this->assertArrayHasKey('fallbackFile', $site['body']);
$this->assertEmpty($site['body']['adapter']);
$this->assertEmpty($site['body']['fallbackFile']);
$domain = $this->setupSiteDomain($siteId);
$this->assertNotEmpty($domain);
$deploymentId = $this->setupDeployment($siteId, [
'code' => $this->packageSite('static-single-file'),
'activate' => 'true'
]);
$this->assertNotEmpty($deploymentId);
$site = $this->getSite($siteId);
$this->assertEquals('200', $site['headers']['status-code']);
$this->assertEquals('static', $site['body']['adapter']);
$this->assertEquals('main.html', $site['body']['fallbackFile']);
$proxyClient = new Client();
$proxyClient->setEndpoint('http://' . $domain);
$response = $proxyClient->call(Client::METHOD_GET, '/');
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertStringContainsString('Main page', (string) $response['body']);
$response = $proxyClient->call(Client::METHOD_GET, '/something');
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertStringContainsString('Main page', (string) $response['body']);
$this->cleanupSite($siteId);
}
public function testSettingsForRollback(): void
{
$siteId = $this->setupSite([
'siteId' => ID::unique(),
'name' => 'Static site',
'framework' => 'astro',
'buildRuntime' => 'node-22',
'outputDirectory' => './dist',
'buildCommand' => 'npm run build',
'installCommand' => 'npm ci',
]);
$this->assertNotEmpty($siteId);
$site = $this->getSite($siteId);
$this->assertEquals('200', $site['headers']['status-code']);
$this->assertEmpty($site['body']['adapter']);
$this->assertEmpty($site['body']['fallbackFile']);
$domain = $this->setupSiteDomain($siteId);
$this->assertNotEmpty($domain);
$deploymentId1 = $this->setupDeployment($siteId, [
'code' => $this->packageSite('astro-static'),
'activate' => 'true'
]);
$this->assertNotEmpty($deploymentId1);
$site = $this->getSite($siteId);
$this->assertEquals('200', $site['headers']['status-code']);
$this->assertEquals('static', $site['body']['adapter']);
$this->assertEquals('index.html', $site['body']['fallbackFile']);
$site = $this->updateSite([
'name' => 'SSR site',
'framework' => 'astro',
'buildRuntime' => 'node-22',
'outputDirectory' => './dist',
'buildCommand' => 'npm run build',
'installCommand' => 'npm ci',
'adapter' => 'ssr',
'fallbackFile' => '',
'$id' => $siteId,
]);
$this->assertEquals('200', $site['headers']['status-code']);
$this->assertEquals('ssr', $site['body']['adapter']);
$this->assertEmpty($site['body']['fallbackFile']);
$deploymentId2 = $this->setupDeployment($siteId, [
'code' => $this->packageSite('astro'),
'activate' => 'true'
]);
$this->assertNotEmpty($deploymentId2);
$site = $this->getSite($siteId);
$this->assertEquals('200', $site['headers']['status-code']);
$this->assertEquals('ssr', $site['body']['adapter']);
$proxyClient = new Client();
$proxyClient->setEndpoint('http://' . $domain);
$response = $proxyClient->call(Client::METHOD_GET, '/');
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertStringContainsString("Astro SSR", (string) $response['body']);
$response = $proxyClient->call(Client::METHOD_GET, '/not-found');
$this->assertEquals(404, $response['headers']['status-code']);
$response = $this->updateSiteDeployment($siteId, $deploymentId1);
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertNotEmpty($response['body']['$id']);
$proxyClient = new Client();
$proxyClient->setEndpoint('http://' . $domain);
$response = $proxyClient->call(Client::METHOD_GET, '/');
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertStringContainsString("Astro static", (string) $response['body']);
$response = $proxyClient->call(Client::METHOD_GET, '/not-found');
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertStringContainsString("Astro static", (string) $response['body']);
$this->cleanupSite($siteId);
}
public function testListSites(): void
{
/**
@@ -2195,62 +1948,6 @@ final class SitesCustomServerTest extends Scope
$this->cleanupSite($siteId);
}
public function testSiteStaticSPA(): void
{
$siteId = $this->setupSite([
'siteId' => ID::unique(),
'name' => 'SPA site',
'framework' => 'other',
'adapter' => 'static',
'buildRuntime' => 'static-1',
'outputDirectory' => './',
'buildCommand' => '',
'installCommand' => '',
'fallbackFile' => '404.html',
]);
$this->assertNotEmpty($siteId);
$deploymentId = $this->setupDeployment($siteId, [
'code' => $this->packageSite('static-spa'),
'activate' => 'true'
]);
$this->assertNotEmpty($deploymentId);
$domain = $this->setupSiteDomain($siteId);
$proxyClient = new Client();
$proxyClient->setEndpoint('http://' . $domain);
$response = $proxyClient->call(Client::METHOD_GET, '/', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
]));
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertStringContainsString("Index page", (string) $response['body']);
$response = $proxyClient->call(Client::METHOD_GET, '/contact', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
]));
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertStringContainsString("Contact page", (string) $response['body']);
$response = $proxyClient->call(Client::METHOD_GET, '/non-existing', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
]));
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertStringContainsString("Customized 404 page", (string) $response['body']);
$this->assertStringNotContainsString("Powered by", (string) $response['body']); //brand
$this->cleanupSite($siteId);
}
#[Retry(count: 3)]
public function testSiteTemplate(): void
{
@@ -2322,154 +2019,9 @@ final class SitesCustomServerTest extends Scope
$this->cleanupSite($siteId);
}
public function testCreateSiteFromTemplateBranch()
{
$template = $this->getTemplate('playground-for-astro');
$this->assertEquals(200, $template['headers']['status-code']);
$template = $template['body'];
$siteId = $this->setupSite([
'siteId' => ID::unique(),
'name' => 'Astro Blog - Branch Test',
'framework' => $template['frameworks'][0]['key'],
'adapter' => $template['frameworks'][0]['adapter'],
'buildRuntime' => $template['frameworks'][0]['buildRuntime'],
'outputDirectory' => $template['frameworks'][0]['outputDirectory'],
'buildCommand' => $template['frameworks'][0]['buildCommand'],
'installCommand' => $template['frameworks'][0]['installCommand'],
'fallbackFile' => $template['frameworks'][0]['fallbackFile'],
]);
$this->assertNotEmpty($siteId);
// Deploy using branch
$deployment = $this->createTemplateDeployment($siteId, [
'repository' => $template['providerRepositoryId'],
'owner' => $template['providerOwner'],
'rootDirectory' => $template['frameworks'][0]['providerRootDirectory'],
'type' => 'branch',
'reference' => 'main',
'activate' => true
]);
$this->assertEquals(202, $deployment['headers']['status-code']);
$this->assertNotEmpty($deployment['body']['$id']);
$deployment = $this->getDeployment($siteId, $deployment['body']['$id']);
$this->assertEquals(200, $deployment['headers']['status-code']);
$this->assertEquals(0, $deployment['body']['sourceSize']);
$this->assertEquals(0, $deployment['body']['buildSize']);
$this->assertEquals(0, $deployment['body']['totalSize']);
$this->assertEventually(function () use ($siteId) {
$site = $this->getSite($siteId);
$this->assertNotEmpty($site['body']['deploymentId']);
}, 120000, 500);
$domain = $this->setupSiteDomain($siteId);
$proxyClient = new Client();
$proxyClient->setEndpoint('http://' . $domain);
$response = $proxyClient->call(Client::METHOD_GET, '/');
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertStringContainsString("Astro Blog", (string) $response['body']);
$this->assertStringContainsString("Hello, Astronaut!", (string) $response['body']);
$response = $proxyClient->call(Client::METHOD_GET, '/about');
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertStringContainsString("Astro Blog", (string) $response['body']);
$this->assertStringContainsString("About Me", (string) $response['body']);
$deployment = $this->getDeployment($siteId, $deployment['body']['$id']);
$this->assertEquals(200, $deployment['headers']['status-code']);
$this->assertGreaterThan(0, $deployment['body']['sourceSize']);
$this->assertGreaterThan(0, $deployment['body']['buildSize']);
$totalSize = $deployment['body']['sourceSize'] + $deployment['body']['buildSize'];
$this->assertEquals($totalSize, $deployment['body']['totalSize']);
$this->cleanupSite($siteId);
}
public function testCreateSiteFromTemplateCommit()
{
$template = $this->getTemplate('playground-for-astro');
$this->assertEquals(200, $template['headers']['status-code']);
// Get latest commit using helper function
$latestCommit = $this->helperGetLatestCommit(
$template['body']['providerOwner'],
$template['body']['providerRepositoryId']
);
$this->assertNotNull($latestCommit);
$template = $template['body'];
$siteId = $this->setupSite([
'siteId' => ID::unique(),
'name' => 'Astro Blog - Commit Test',
'framework' => $template['frameworks'][0]['key'],
'adapter' => $template['frameworks'][0]['adapter'],
'buildRuntime' => $template['frameworks'][0]['buildRuntime'],
'outputDirectory' => $template['frameworks'][0]['outputDirectory'],
'buildCommand' => $template['frameworks'][0]['buildCommand'],
'installCommand' => $template['frameworks'][0]['installCommand'],
'fallbackFile' => $template['frameworks'][0]['fallbackFile'],
]);
$this->assertNotEmpty($siteId);
// Deploy using commit
$deployment = $this->createTemplateDeployment($siteId, [
'repository' => $template['providerRepositoryId'],
'owner' => $template['providerOwner'],
'rootDirectory' => $template['frameworks'][0]['providerRootDirectory'],
'type' => 'commit',
'reference' => $latestCommit,
'activate' => true
]);
$this->assertEquals(202, $deployment['headers']['status-code']);
$this->assertNotEmpty($deployment['body']['$id']);
$deployment = $this->getDeployment($siteId, $deployment['body']['$id']);
$this->assertEquals(200, $deployment['headers']['status-code']);
$this->assertEquals(0, $deployment['body']['sourceSize']);
$this->assertEquals(0, $deployment['body']['buildSize']);
$this->assertEquals(0, $deployment['body']['totalSize']);
$this->assertEventually(function () use ($siteId) {
$site = $this->getSite($siteId);
$this->assertNotEmpty($site['body']['deploymentId']);
}, 120000, 500);
$domain = $this->setupSiteDomain($siteId);
$proxyClient = new Client();
$proxyClient->setEndpoint('http://' . $domain);
$response = $proxyClient->call(Client::METHOD_GET, '/');
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertStringContainsString("Astro Blog", (string) $response['body']);
$this->assertStringContainsString("Hello, Astronaut!", (string) $response['body']);
$response = $proxyClient->call(Client::METHOD_GET, '/about');
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertStringContainsString("Astro Blog", (string) $response['body']);
$this->assertStringContainsString("About Me", (string) $response['body']);
$deployment = $this->getDeployment($siteId, $deployment['body']['$id']);
$this->assertEquals(200, $deployment['headers']['status-code']);
$this->assertGreaterThan(0, $deployment['body']['sourceSize']);
$this->assertGreaterThan(0, $deployment['body']['buildSize']);
$totalSize = $deployment['body']['sourceSize'] + $deployment['body']['buildSize'];
$this->assertEquals($totalSize, $deployment['body']['totalSize']);
$this->cleanupSite($siteId);
}
public function testSiteDomainReclaiming(): void
{
@@ -2739,201 +2291,7 @@ final class SitesCustomServerTest extends Scope
$this->cleanupSite($siteId);
}
public function testSSRLogs(): void
{
$siteId = $this->setupSite([
'siteId' => ID::unique(),
'name' => 'SSR site',
'framework' => 'astro',
'adapter' => 'ssr',
'buildRuntime' => 'node-22',
'outputDirectory' => './dist',
'buildCommand' => 'npm run build',
'installCommand' => 'npm ci',
'fallbackFile' => '',
]);
$this->assertNotEmpty($siteId);
$domain = $this->setupSiteDomain($siteId);
$deploymentId = $this->setupDeployment($siteId, [
'code' => $this->packageSite('astro'),
'activate' => 'true'
]);
$this->assertNotEmpty($deploymentId);
$domain = $this->getSiteDomain($siteId);
$proxyClient = new Client();
$proxyClient->setEndpoint('http://' . $domain);
$response = $proxyClient->call(Client::METHOD_GET, '/logs-inline');
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertStringContainsString("Inline logs printed.", (string) $response['body']);
// Poll for execution logs to be written (async)
// Filter by requestPath to avoid picking up screenshot worker executions
// Wait for both the execution entry AND its logs field to be populated
$logs = null;
$timeout = 120;
$start = \time();
while (\time() - $start < $timeout) {
$logs = $this->listLogs($siteId, [
Query::orderDesc('$createdAt')->toString(),
Query::equal('requestPath', ['/logs-inline'])->toString(),
Query::limit(1)->toString(),
]);
if (!empty($logs['body']['executions']) && !empty($logs['body']['executions'][0]['logs'])) {
break;
}
\usleep(500000);
}
$this->assertNotEmpty($logs['body']['executions'], 'Execution logs were not available within timeout');
$this->assertNotNull($logs['body']['executions'][0]['logs'], 'Execution logs content was not populated within timeout');
$this->assertEquals(200, $logs['headers']['status-code']);
$this->assertStringContainsString($deploymentId, (string) $logs['body']['executions'][0]['deploymentId']);
$this->assertStringContainsString("GET", (string) $logs['body']['executions'][0]['requestMethod']);
$this->assertStringContainsString("/logs-inline", (string) $logs['body']['executions'][0]['requestPath']);
$this->assertStringContainsString("Log1", (string) $logs['body']['executions'][0]['logs']);
$this->assertStringContainsString("Log2", (string) $logs['body']['executions'][0]['logs']);
$this->assertStringContainsString("Error1", (string) $logs['body']['executions'][0]['errors']);
$this->assertStringContainsString("Error2", (string) $logs['body']['executions'][0]['errors']);
$log1Id = $logs['body']['executions'][0]['$id'];
$this->assertNotEmpty($log1Id);
$logs = $this->listLogs($siteId, [
Query::orderDesc('$createdAt')->toString(),
Query::limit(1)->toString(),
Query::equal('deploymentId', [$deploymentId])->toString()
]);
$this->assertEquals(200, $logs['headers']['status-code']);
$this->assertGreaterThanOrEqual(1, $logs['body']['total']);
$this->assertCount(1, $logs['body']['executions']);
$logs = $this->listLogs($siteId, [
Query::orderDesc('$createdAt')->toString(),
Query::limit(1)->toString(),
Query::equal('deploymentId', ['some-random-id'])->toString()
]);
$this->assertEquals(200, $logs['headers']['status-code']);
$this->assertEquals(0, $logs['body']['total']);
$this->assertCount(0, $logs['body']['executions']);
$response = $proxyClient->call(Client::METHOD_GET, '/logs-action');
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertStringContainsString("Action logs printed.", (string) $response['body']);
$logs = null;
$start = \time();
while (\time() - $start < $timeout) {
$logs = $this->listLogs($siteId, [
Query::orderDesc('$createdAt')->toString(),
Query::equal('requestPath', ['/logs-action'])->toString(),
Query::limit(1)->toString(),
]);
if (!empty($logs['body']['executions']) && !empty($logs['body']['executions'][0]['logs'])) {
break;
}
\usleep(500000);
}
$this->assertNotEmpty($logs['body']['executions'], 'Action execution logs were not available within timeout');
$this->assertNotNull($logs['body']['executions'][0]['logs'], 'Action execution logs content was not populated within timeout');
$this->assertEquals(200, $logs['headers']['status-code']);
$this->assertStringContainsString($deploymentId, (string) $logs['body']['executions'][0]['deploymentId']);
$this->assertStringContainsString("GET", (string) $logs['body']['executions'][0]['requestMethod']);
$this->assertStringContainsString("/logs-action", (string) $logs['body']['executions'][0]['requestPath']);
$this->assertStringContainsString("Log1", (string) $logs['body']['executions'][0]['logs']);
$this->assertStringContainsString("Log2", (string) $logs['body']['executions'][0]['logs']);
$this->assertStringContainsString("Error1", (string) $logs['body']['executions'][0]['errors']);
$this->assertStringContainsString("Error2", (string) $logs['body']['executions'][0]['errors']);
$log2Id = $logs['body']['executions'][0]['$id'];
$this->assertNotEmpty($log2Id);
$this->assertNotEquals($log1Id, $log2Id);
$site = $this->updateSite(
[
'$id' => $siteId,
'name' => 'SSR site',
'framework' => 'astro',
'adapter' => 'ssr',
'buildRuntime' => 'node-22',
'outputDirectory' => './dist',
'buildCommand' => 'npm run build',
'installCommand' => 'npm ci',
'fallbackFile' => '',
'logging' => false // set logging to false
]
);
$this->assertEquals(200, $site['headers']['status-code']);
// Wait for the logging config change to propagate to the site runtime
$this->assertEventually(function () use ($proxyClient) {
$response = $proxyClient->call(Client::METHOD_GET, '/logs-inline');
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertStringContainsString("Inline logs printed.", (string) $response['body']);
}, 15_000, 500);
// Poll for the NEW log entry (after logging was disabled) to appear
$timeout = 30;
$start = \time();
$newLog = null;
while (\time() - $start < $timeout) {
$logs = $this->listLogs($siteId, [
Query::orderDesc('$createdAt')->toString(),
Query::equal('requestPath', ['/logs-inline'])->toString(),
Query::limit(1)->toString(),
]);
if (
!empty($logs['body']['executions']) &&
$logs['body']['executions'][0]['$id'] !== $log1Id
) {
$newLog = $logs['body']['executions'][0];
break;
}
\usleep(500000);
}
$this->assertNotNull($newLog, 'New log entry should appear after logging-disabled request');
$this->assertEquals("GET", $newLog['requestMethod']);
$this->assertEquals("/logs-inline", $newLog['requestPath']);
$this->assertEmpty($newLog['logs']);
$this->assertEmpty($newLog['errors']);
$log1Id = $newLog['$id'];
$this->assertNotEmpty($log1Id);
$response = $proxyClient->call(Client::METHOD_GET, '/logs-action');
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertStringContainsString("Action logs printed.", (string) $response['body']);
// Poll for the NEW log entry for /logs-action
$start = \time();
$newLog = null;
while (\time() - $start < $timeout) {
$logs = $this->listLogs($siteId, [
Query::orderDesc('$createdAt')->toString(),
Query::equal('requestPath', ['/logs-action'])->toString(),
Query::limit(1)->toString(),
]);
if (
!empty($logs['body']['executions']) &&
$logs['body']['executions'][0]['$id'] !== $log2Id
) {
$newLog = $logs['body']['executions'][0];
break;
}
\usleep(500000);
}
$this->assertNotNull($newLog, 'New log entry should appear after logging-disabled /logs-action request');
$this->assertEquals("GET", $newLog['requestMethod']);
$this->assertEquals("/logs-action", $newLog['requestPath']);
$this->assertEmpty($newLog['logs']);
$this->assertEmpty($newLog['errors']);
$log2Id = $logs['body']['executions'][0]['$id'];
$this->assertNotEmpty($log2Id);
$this->cleanupSite($siteId);
}
public function testDuplicateDeployment(): void
{
@@ -3395,37 +2753,6 @@ final class SitesCustomServerTest extends Scope
$this->cleanupSite($siteId);
}
public function testDeploymentCommandEscaping(): void
{
$siteId = $this->setupSite([
'siteId' => ID::unique(),
'name' => 'A site',
'framework' => 'other',
'adapter' => 'static',
'buildRuntime' => 'static-1',
'outputDirectory' => './',
'buildCommand' => "echo 'Hello two'",
'installCommand' => 'echo "Hello one"',
'fallbackFile' => '',
]);
$this->assertNotEmpty($siteId);
$deploymentId = $this->setupDeployment($siteId, [
'code' => $this->packageSite('static-single-file'),
'activate' => 'true'
]);
$this->assertNotEmpty($deploymentId);
$deployment = $this->getDeployment($siteId, $deploymentId);
$this->assertEquals(200, $deployment['headers']['status-code']);
$this->assertStringContainsString('Hello one', (string) $deployment['body']['buildLogs']);
$this->assertStringContainsString('Hello two', (string) $deployment['body']['buildLogs']);
$this->cleanupSite($siteId);
}
#[Retry(count: 3)]
public function testErrorPages(): void
{
@@ -3582,258 +2909,6 @@ final class SitesCustomServerTest extends Scope
$this->cleanupSite($siteId);
}
public function testOutputDirectoryEmpty(): void
{
$siteId = $this->setupSite([
'siteId' => ID::unique(),
'name' => 'Empty output directory',
'framework' => 'other',
'buildRuntime' => 'node-22',
'outputDirectory' => './empty-directory',
'buildCommand' => 'mkdir -p ./empty-directory'
]);
$this->assertNotEmpty($siteId);
$deployment = $this->createDeployment($siteId, [
'code' => $this->packageSite('static-single-file'),
'activate' => true
]);
$this->assertEquals(202, $deployment['headers']['status-code']);
$deploymentId = $deployment['body']['$id'];
$this->assertNotEmpty($deploymentId);
$this->assertEventually(function () use ($siteId, $deploymentId) {
$deployment = $this->getDeployment($siteId, $deploymentId);
$this->assertEquals('failed', $deployment['body']['status'], 'Deployment status does not match: ' . json_encode($deployment['body'], JSON_PRETTY_PRINT));
$this->assertStringContainsString('Error:', (string) $deployment['body']['buildLogs'], 'Deployment logs do not match: ' . json_encode($deployment['body'], JSON_PRETTY_PRINT));
}, 100000, 500);
$this->cleanupSite($siteId);
}
public function testOutputDirectoryMissing(): void
{
$siteId = $this->setupSite([
'siteId' => ID::unique(),
'name' => 'Missing output directory',
'framework' => 'other',
'buildRuntime' => 'node-22',
'outputDirectory' => './non-existing-directory',
]);
$this->assertNotEmpty($siteId);
$deployment = $this->createDeployment($siteId, [
'code' => $this->packageSite('static-single-file'),
'activate' => true
]);
$this->assertEquals(202, $deployment['headers']['status-code']);
$deploymentId = $deployment['body']['$id'];
$this->assertNotEmpty($deploymentId);
$this->assertEventually(function () use ($siteId, $deploymentId) {
$deployment = $this->getDeployment($siteId, $deploymentId);
$this->assertEquals('failed', $deployment['body']['status'], 'Deployment status does not match: ' . json_encode($deployment['body'], JSON_PRETTY_PRINT));
$this->assertStringContainsString('No such file or directory', (string) $deployment['body']['buildLogs'], 'Deployment logs do not match: ' . json_encode($deployment['body'], JSON_PRETTY_PRINT));
}, 100000, 500);
$this->cleanupSite($siteId);
}
public function testBuildErrorLogs(): void
{
$siteId = $this->setupSite([
'siteId' => ID::unique(),
'name' => 'Astro SSR site',
'framework' => 'astro',
'buildRuntime' => 'node-22',
'outputDirectory' => './dist',
'buildCommand' => 'npm run build',
'installCommand' => 'echo "custom error" && npm ci',
'adapter' => 'ssr',
]);
$this->assertNotEmpty($siteId);
$site = $this->getSite($siteId);
$this->assertEquals('200', $site['headers']['status-code']);
$domain = $this->setupSiteDomain($siteId);
$this->assertNotEmpty($domain);
$deployment = $this->createDeployment($siteId, [
'code' => $this->packageSite('astro-static'),
'activate' => true
]);
$this->assertEquals(202, $deployment['headers']['status-code']);
$deploymentId = $deployment['body']['$id'];
$this->assertNotEmpty($deploymentId);
$this->assertEventually(function () use ($siteId, $deploymentId) {
$deployment = $this->getDeployment($siteId, $deploymentId);
$this->assertEquals('failed', $deployment['body']['status'], 'Deployment status is failed, deployment: ' . json_encode($deployment['body'], JSON_PRETTY_PRINT));
}, 100000, 500);
$deployment = $this->getDeployment($siteId, $deploymentId);
$this->assertEquals(200, $deployment['headers']['status-code']);
$this->assertStringContainsString('custom error', (string) $deployment['body']['buildLogs']);
$this->assertStringContainsString('Adapter mismatch', (string) $deployment['body']['buildLogs']);
$this->cleanupSite($siteId);
}
public function testCookieHeader()
{
$siteId = $this->setupSite([
'siteId' => ID::unique(),
'name' => 'Astro site',
'framework' => 'astro',
'adapter' => 'ssr',
'buildRuntime' => 'node-22',
'outputDirectory' => './dist',
'buildCommand' => 'npm run build',
'installCommand' => 'npm ci',
'fallbackFile' => '',
]);
$this->assertNotEmpty($siteId);
$domain = $this->setupSiteDomain($siteId);
$deploymentId = $this->setupDeployment($siteId, [
'code' => $this->packageSite('astro'),
'activate' => 'true'
]);
$this->assertNotEmpty($deploymentId);
$domain = $this->getSiteDomain($siteId);
$proxyClient = new Client();
$proxyClient->setEndpoint('http://' . $domain);
$response = $proxyClient->call(Client::METHOD_GET, '/cookies', [
'cookie' => 'custom-session-id=abcd123; custom-user-id=efgh456'
]);
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals("abcd123;efgh456", $response['body']);
$this->assertEquals("value-one", $response['cookies']['my-cookie-one']);
$this->assertEquals("value-two", $response['cookies']['my-cookie-two']);
$this->cleanupSite($siteId);
}
public function testSiteCustomStartCommand(): void
{
$siteId = $this->setupSite([
'siteId' => ID::unique(),
'name' => 'Astro site',
'framework' => 'astro',
'adapter' => 'ssr',
'startCommand' => 'node custom-server.js',
'buildRuntime' => 'node-22',
'outputDirectory' => './dist',
'buildCommand' => 'npm run build',
'installCommand' => 'npm ci',
'fallbackFile' => '',
]);
$this->assertNotEmpty($siteId);
$domain = $this->setupSiteDomain($siteId);
$deploymentId = $this->setupDeployment($siteId, [
'code' => $this->packageSite('astro-custom-start-command'),
'activate' => 'true'
]);
$this->assertNotEmpty($deploymentId);
$domain = $this->getSiteDomain($siteId);
$proxyClient = new Client();
$proxyClient->setEndpoint('http://' . $domain);
$response = $proxyClient->call(Client::METHOD_GET, '/');
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertStringContainsString("Homepage OK", (string) $response['body']);
$response = $proxyClient->call(Client::METHOD_GET, '/ssr');
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertStringContainsString("SSR OK", (string) $response['body']);
$originalBody = $response['body'];
$response = $proxyClient->call(Client::METHOD_GET, '/ssr');
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertStringContainsString("SSR OK", (string) $response['body']);
$this->assertNotEquals($originalBody, $response['body']); // Includes Date.now()
$response = $proxyClient->call(Client::METHOD_GET, '/ssr-custom');
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertStringContainsString("Custom SSR OK", (string) $response['body']);
$originalBody = $response['body'];
$response = $proxyClient->call(Client::METHOD_GET, '/ssr-custom');
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertStringContainsString("Custom SSR OK", (string) $response['body']);
$this->assertNotEquals($originalBody, $response['body']); // Includes Date.now()
$response = $proxyClient->call(Client::METHOD_GET, '/non-existing');
$this->assertEquals(500, $response['headers']['status-code']);
$this->assertStringContainsString("Custom error", (string) $response['body']);
$this->cleanupSite($siteId);
}
public function testSiteSpecifications()
{
// Check if the site specifications are correctly set in builds
$site = $this->createSite([
'siteId' => ID::unique(),
'name' => 'Astro site',
'framework' => 'astro',
'adapter' => 'ssr',
'buildRuntime' => 'node-22',
'outputDirectory' => './dist',
'buildCommand' => 'npm run build && echo $APPWRITE_SITE_MEMORY:$APPWRITE_SITE_CPUS',
'installCommand' => 'npm ci',
'fallbackFile' => '',
'buildSpecification' => Specification::S_1VCPU_1GB,
'runtimeSpecification' => Specification::S_05VCPU_512MB,
]);
$this->assertEquals(201, $site['headers']['status-code']);
$this->assertEquals(Specification::S_1VCPU_1GB, $site['body']['buildSpecification']);
$this->assertEquals(Specification::S_05VCPU_512MB, $site['body']['runtimeSpecification']);
$this->assertNotEmpty($site['body']['$id']);
$siteId = $site['body']['$id'] ?? '';
$domain = $this->setupSiteDomain($siteId);
$deploymentId = $this->setupDeployment($siteId, [
'code' => $this->packageSite('astro'),
'activate' => true
]);
$this->assertEventually(function () use ($siteId, $deploymentId) {
$deployment = $this->getDeployment($siteId, $deploymentId);
// TODO: This assertion is not testing what we set in create function, because build worker currently overrides to minimal build specs
$this->assertStringContainsString('2048:1', (string) $deployment['body']['buildLogs']);
}, 10000, 500);
// Check if the sites specifications are correctly set in executions
$proxyClient = new Client();
$proxyClient->setEndpoint('http://' . $domain);
$response = $proxyClient->call(Client::METHOD_GET, '/specs');
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertNotEmpty($response['body']);
$this->assertEquals('512', $response['body']['APPWRITE_SITE_MEMORY']);
$this->assertEquals('0.5', $response['body']['APPWRITE_SITE_CPUS']);
$this->cleanupSite($siteId);
}
public function testSiteDeploymentRetention(): void
{
$siteIds = [];
+94
View File
@@ -0,0 +1,94 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Functions;
use Appwrite\Platform\Modules\Functions\Workers\Builds;
use PHPUnit\Framework\TestCase;
use ReflectionMethod;
use Utopia\Config\Config;
use Utopia\Database\Document;
final class BuildsTest extends TestCase
{
private Builds $builds;
public function setUp(): void
{
$this->builds = new Builds();
}
public function testSiteCommandIncludesFrameworkAndDeploymentCommands(): void
{
Config::setParam('frameworks', [
'astro' => [
'envCommand' => 'cp .env.example .env',
'bundleCommand' => 'npm run bundle',
],
]);
$resource = new Document([
'$collection' => 'sites',
'framework' => 'astro',
]);
$deployment = new Document([
'buildCommands' => 'npm run build',
]);
$this->assertSame(
'cp .env.example .env && npm run build && npm run bundle',
$this->callBuilds('getCommand', $resource, $deployment)
);
}
public function testSiteBuildCommandAppendsDetectionFileListing(): void
{
$command = $this->callBuilds('prepareSiteBuildCommand', 'npm run build', './dist folder');
$this->assertStringStartsWith('npm run build && echo "{APPWRITE_DETECTION_SEPARATOR_START}"', $command);
$this->assertStringContainsString('cd /usr/local/build && cd \'./dist folder\'', (string) $command);
$this->assertStringContainsString('find . -name \'node_modules\' -prune -o -type f -print', (string) $command);
$this->assertStringEndsWith('echo "{APPWRITE_DETECTION_SEPARATOR_END}"', $command);
}
public function testSiteBuildCommandCanBeOnlyDetectionFileListing(): void
{
$command = $this->callBuilds('prepareSiteBuildCommand', '', '');
$this->assertStringStartsWith('echo "{APPWRITE_DETECTION_SEPARATOR_START}"', $command);
$this->assertStringContainsString('cd /usr/local/build && find .', (string) $command);
}
public function testSplitSiteDetectionLogsRemovesDetectionBlock(): void
{
$result = $this->callBuilds(
'splitSiteDetectionLogs',
"before\n{APPWRITE_DETECTION_SEPARATOR_START}\n./index.html\n./server/entry.mjs\n{APPWRITE_DETECTION_SEPARATOR_END}\nafter\n"
);
$this->assertSame("before\n\nafter\n", $result['logs']);
$this->assertSame("\n./index.html\n./server/entry.mjs\n", $result['detectionLogs']);
}
public function testDetectSiteRenderingFindsStaticFallbackFile(): void
{
$detection = $this->callBuilds('detectSiteRendering', 'other', "./main.html\n");
$this->assertSame('static', $detection->getName());
$this->assertSame('main.html', $detection->getFallbackFile());
}
public function testDetectSiteRenderingFindsAstroSSR(): void
{
$detection = $this->callBuilds('detectSiteRendering', 'astro', "./client/index.html\n./server/entry.mjs\n");
$this->assertSame('ssr', $detection->getName());
$this->assertNull($detection->getFallbackFile());
}
private function callBuilds(string $method, mixed ...$arguments): mixed
{
return (new ReflectionMethod($this->builds, $method))->invoke($this->builds, ...$arguments);
}
}