You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
153 lines
5.6 KiB
PHP
153 lines
5.6 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud {
|
|
use App\Models\CloudInstance;
|
|
use App\Models\CloudJob;
|
|
|
|
final class AdminSettingsJobCapture
|
|
{
|
|
public static array $calls = [];
|
|
}
|
|
|
|
final class CloudJobService
|
|
{
|
|
public static function enqueue(CloudInstance $instance, array $actor,
|
|
string $kind, string $method, string $suffix, array $body = [],
|
|
?array $credential = null): CloudJob
|
|
{
|
|
AdminSettingsJobCapture::$calls[] = compact(
|
|
'kind', 'method', 'suffix', 'body', 'credential'
|
|
);
|
|
$job = new CloudJob();
|
|
$job->id = 91;
|
|
return $job;
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace {
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Controllers\CloudAdminInstanceController;
|
|
use App\Services\Cloud\AdminSettingsJobCapture;
|
|
use App\Services\Cloud\CloudError;
|
|
use Illuminate\Database\Capsule\Manager as Capsule;
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
use Slim\Http\Environment;
|
|
use Slim\Http\Request;
|
|
use Slim\Http\Response;
|
|
|
|
$assert = static function (bool $condition, string $message): void {
|
|
if (!$condition) { throw new RuntimeException($message); }
|
|
};
|
|
$rejects = static function (callable $callback, string $message): void {
|
|
try { $callback(); } catch (CloudError $error) {
|
|
if ($error->httpStatus === 409) { return; }
|
|
throw new RuntimeException($message . ': unexpected status');
|
|
}
|
|
throw new RuntimeException($message);
|
|
};
|
|
|
|
$capsule = new Capsule();
|
|
$capsule->addConnection([
|
|
'driver' => 'sqlite', 'database' => ':memory:',
|
|
], 'default');
|
|
$capsule->setAsGlobal();
|
|
$capsule->bootEloquent();
|
|
$capsule->schema('default')->create('cloud_instances', static function (
|
|
Blueprint $t
|
|
): void {
|
|
$t->increments('id'); $t->integer('user_id'); $t->string('uuid');
|
|
$t->string('status'); $t->string('service_status');
|
|
$t->integer('expires_at')->nullable(); $t->text('spec_json');
|
|
$t->text('sealed_credential')->nullable();
|
|
});
|
|
$now = time();
|
|
$network = [
|
|
'contract' => 1, 'network_selector' => 'bridge:br0',
|
|
'ipv4' => [
|
|
'pool_key' => str_repeat('a', 32),
|
|
'pool_cidr' => '8.8.8.0/24',
|
|
'address_cidr' => '8.8.8.10/24',
|
|
'gateway' => '8.8.8.1', 'gateway_on_link' => false,
|
|
],
|
|
'ipv6' => [
|
|
'pool_key' => str_repeat('b', 32),
|
|
'pool_cidr' => '2404:c140:156:3a::/64',
|
|
'address_cidr' => '2404:c140:156:3a::10/64',
|
|
'gateway' => '2404:c140:156::1', 'gateway_on_link' => true,
|
|
],
|
|
'dns_servers' => ['1.1.1.1', '2606:4700:4700::1111'],
|
|
];
|
|
$spec = json_encode([
|
|
'managed' => true, 'os_source' => 'cloud_template',
|
|
'public_network' => $network,
|
|
], JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES);
|
|
Capsule::table('cloud_instances')->insert([
|
|
[
|
|
'id' => 1, 'user_id' => 7,
|
|
'uuid' => '00000000-0000-4000-8000-000000000001',
|
|
'status' => 'active', 'service_status' => 'active',
|
|
'expires_at' => $now + 3600, 'spec_json' => $spec,
|
|
'sealed_credential' => null,
|
|
],
|
|
[
|
|
'id' => 2, 'user_id' => 7,
|
|
'uuid' => '00000000-0000-4000-8000-000000000002',
|
|
'status' => 'active', 'service_status' => 'active',
|
|
'expires_at' => $now + 3600,
|
|
'spec_json' => json_encode([
|
|
'managed' => true, 'os_source' => 'cloud_template',
|
|
'public_ip_cidr' => '8.8.8.20/24',
|
|
], JSON_THROW_ON_ERROR),
|
|
'sealed_credential' => null,
|
|
],
|
|
[
|
|
'id' => 3, 'user_id' => 7,
|
|
'uuid' => '00000000-0000-4000-8000-000000000003',
|
|
'status' => 'active', 'service_status' => 'expired',
|
|
'expires_at' => $now - 1, 'spec_json' => $spec,
|
|
'sealed_credential' => null,
|
|
],
|
|
]);
|
|
$actor = [
|
|
'user_id' => 99, 'is_admin' => true,
|
|
'is_super_admin' => true, 'can_manage' => true,
|
|
];
|
|
$request = static function (int $id) use ($actor): Request {
|
|
return Request::createFromEnvironment(Environment::mock([
|
|
'REQUEST_METHOD' => 'POST',
|
|
'REQUEST_URI' => '/api/v1/admin/instances/' . $id . '/settings',
|
|
]))->withAttribute('cloud_actor', $actor)->withParsedBody([
|
|
'hostname' => 'canonical-host', 'confirm_restart' => true,
|
|
]);
|
|
};
|
|
$controller = new CloudAdminInstanceController();
|
|
$response = $controller->settings(
|
|
$request(1), new Response(), ['id' => '1']
|
|
);
|
|
$payload = json_decode((string) $response->getBody(), true);
|
|
$assert($response->getStatusCode() === 202
|
|
&& ($payload['task_id'] ?? null) === '91'
|
|
&& (AdminSettingsJobCapture::$calls[0]['body']['hostname'] ?? null)
|
|
=== 'canonical-host',
|
|
'Canonical-only dual-stack metadata did not admit guest settings.');
|
|
$before = count(AdminSettingsJobCapture::$calls);
|
|
$rejects(
|
|
static fn () => $controller->settings(
|
|
$request(2), new Response(), ['id' => '2']
|
|
),
|
|
'Legacy scalar-only metadata admitted guest settings.'
|
|
);
|
|
$rejects(
|
|
static fn () => $controller->settings(
|
|
$request(3), new Response(), ['id' => '3']
|
|
),
|
|
'An expired instance admitted guest settings.'
|
|
);
|
|
$assert(count(AdminSettingsJobCapture::$calls) === $before,
|
|
'Rejected settings requests queued a job.');
|
|
|
|
echo "Canonical administrator settings behavior passed.\n";
|
|
}
|