Skip to content

Commit

Permalink
Merge pull request #114 from open-runtimes/feat-opr-specs-env-var
Browse files Browse the repository at this point in the history
Feat: Specifications OPR env var
  • Loading branch information
Meldiron authored Oct 17, 2024
2 parents d9c577f + ca7cd0f commit da1d249
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 4 deletions.
6 changes: 4 additions & 2 deletions app/http.php
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ function cleanUp(Orchestration $orchestration, Table $activeRuntimes, array $net
->param('timeout', 600, new Integer(), 'Commands execution time in seconds.', true)
->param('remove', false, new Boolean(), 'Remove a runtime after execution.', true)
->param('cpus', 1, new FloatValidator(true), 'Container CPU.', true)
->param('memory', 512, new Integer(), 'Comtainer RAM memory.', true)
->param('memory', 512, new Integer(), 'Container RAM memory.', true)
->param('version', 'v4', new WhiteList(['v2', 'v4']), 'Runtime Open Runtime version.', true)
->param('restartPolicy', DockerAPI::RESTART_NO, new WhiteList([DockerAPI::RESTART_NO, DockerAPI::RESTART_ALWAYS, DockerAPI::RESTART_ON_FAILURE, DockerAPI::RESTART_UNLESS_STOPPED], true), 'Define restart policy for the runtime once an exit code is returned. Default value is "no". Possible values are "no", "always", "on-failure", "unless-stopped".', true)
->inject('networks')
Expand Down Expand Up @@ -560,7 +560,9 @@ function cleanUp(Orchestration $orchestration, Table $activeRuntimes, array $net
'v4' => [
'OPEN_RUNTIMES_SECRET' => $secret,
'OPEN_RUNTIMES_ENTRYPOINT' => $entrypoint,
'OPEN_RUNTIMES_HOSTNAME' => System::getHostname()
'OPEN_RUNTIMES_HOSTNAME' => System::getHostname(),
'OPEN_RUNTIMES_CPUS' => $cpus,
'OPEN_RUNTIMES_MEMORY' => $memory,
]
});

Expand Down
43 changes: 41 additions & 2 deletions tests/ExecutorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,36 @@ public function provideScenarios(): array
'logging' => true,
'mimeType' => 'multipart/form-data'
],
[
'image' => 'openruntimes/node:v4-18.0',
'entrypoint' => 'index.js',
'folder' => 'node-specs',
'version' => 'v4',
'startCommand' => 'cp /tmp/code.tar.gz /mnt/code/code.tar.gz && nohup helpers/start.sh "pm2 start src/server.js --no-daemon"',
'buildCommand' => 'tar -zxf /tmp/code.tar.gz -C /mnt/code && helpers/build.sh "npm i && npm run build"',
'assertions' => function ($response) {
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals(200, $response['body']['statusCode']);

$this->assertIsString($response['body']['body']);
$this->assertNotEmpty($response['body']['body']);
$json = \json_decode($response['body']['body'], true);
$this->assertEquals("2.5", $json['cpus']);
$this->assertEquals("1024", $json['memory']);

$this->assertEmpty($response['body']['logs']);
$this->assertEmpty($response['body']['errors']);
},
'body' => null,
'logging' => true,
'mimeType' => 'application/json',
'cpus' => 2.5,
'memory' => 1024,
'buildAssertions' => function ($response) {
$this->assertStringContainsString("cpus=2.5", $response['body']['output']);
$this->assertStringContainsString("memory=1024", $response['body']['output']);
}
],
];
}

Expand All @@ -771,7 +801,7 @@ public function provideScenarios(): array
*
* @dataProvider provideScenarios
*/
public function testScenarios(string $image, string $entrypoint, string $folder, string $version, string $startCommand, string $buildCommand, callable $assertions, callable $body = null, bool $logging = true, string $mimeType = "application/json"): void
public function testScenarios(string $image, string $entrypoint, string $folder, string $version, string $startCommand, string $buildCommand, callable $assertions, callable $body = null, bool $logging = true, string $mimeType = "application/json", float $cpus = 1, int $memory = 512, callable $buildAssertions = null): void
{
/** Prepare deployment */
$output = '';
Expand All @@ -787,12 +817,18 @@ public function testScenarios(string $image, string $entrypoint, string $folder,
'image' => $image,
'workdir' => '/usr/code',
'remove' => true,
'command' => $buildCommand
'command' => $buildCommand,
'cpus' => $cpus,
'memory' => $memory
];

$response = $this->client->call(Client::METHOD_POST, '/runtimes', [], $params);
$this->assertEquals(201, $response['headers']['status-code']);

if (!is_null($buildAssertions)) {
call_user_func($buildAssertions, $response);
}

$path = $response['body']['path'];

$params = [
Expand All @@ -803,6 +839,8 @@ public function testScenarios(string $image, string $entrypoint, string $folder,
'runtimeEntrypoint' => $startCommand,
'timeout' => 45,
'logging' => $logging,
'cpus' => $cpus,
'memory' => $memory,
];

if (isset($body)) {
Expand All @@ -824,6 +862,7 @@ public function testScenarios(string $image, string $entrypoint, string $folder,
$this->assertEquals(200, $response['headers']['status-code']);
}


/**
*
* @return array<mixed>
Expand Down
2 changes: 2 additions & 0 deletions tests/resources/functions/node-specs/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
console.log(`cpus=${process.env.OPEN_RUNTIMES_CPUS}`);
console.log(`memory=${process.env.OPEN_RUNTIMES_MEMORY}`);
8 changes: 8 additions & 0 deletions tests/resources/functions/node-specs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
await new Promise((resolve) => setTimeout(resolve, 10_000));

export default async (context) => {
return context.res.json({
cpus: process.env.OPEN_RUNTIMES_CPUS,
memory: process.env.OPEN_RUNTIMES_MEMORY,
});
};
14 changes: 14 additions & 0 deletions tests/resources/functions/node-specs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "specs",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "node build.js"
},
"keywords": [],
"author": "",
"license": "ISC"
}

0 comments on commit da1d249

Please sign in to comment.