diff --git a/app/http.php b/app/http.php index eeb7d70..470cfe2 100644 --- a/app/http.php +++ b/app/http.php @@ -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') @@ -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, ] }); diff --git a/tests/ExecutorTest.php b/tests/ExecutorTest.php index 3e23dce..181958a 100644 --- a/tests/ExecutorTest.php +++ b/tests/ExecutorTest.php @@ -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']); + } + ], ]; } @@ -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 = ''; @@ -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 = [ @@ -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)) { @@ -824,6 +862,7 @@ public function testScenarios(string $image, string $entrypoint, string $folder, $this->assertEquals(200, $response['headers']['status-code']); } + /** * * @return array diff --git a/tests/resources/functions/node-specs/build.js b/tests/resources/functions/node-specs/build.js new file mode 100644 index 0000000..755b7b9 --- /dev/null +++ b/tests/resources/functions/node-specs/build.js @@ -0,0 +1,2 @@ +console.log(`cpus=${process.env.OPEN_RUNTIMES_CPUS}`); +console.log(`memory=${process.env.OPEN_RUNTIMES_MEMORY}`); diff --git a/tests/resources/functions/node-specs/index.js b/tests/resources/functions/node-specs/index.js new file mode 100644 index 0000000..6c00558 --- /dev/null +++ b/tests/resources/functions/node-specs/index.js @@ -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, + }); +}; diff --git a/tests/resources/functions/node-specs/package.json b/tests/resources/functions/node-specs/package.json new file mode 100644 index 0000000..0090952 --- /dev/null +++ b/tests/resources/functions/node-specs/package.json @@ -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" +}