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.
virt/web/tests/CloudHostVpcTransportServic...

120 lines
4.2 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Cloud\CloudError;
use App\Services\Cloud\CloudHostVpcTransportService;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Schema\Blueprint;
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$capsule = new Capsule();
$capsule->addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'default');
$capsule->setAsGlobal();
$capsule->bootEloquent();
$schema = $capsule->schema('default');
$schema->create('cloud_hosts', static function (Blueprint $table): void {
$table->increments('id');
$table->string('name');
$table->string('region');
$table->string('country_code', 2);
$table->string('default_network');
$table->boolean('enabled');
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_host_vpc_transports',
static function (Blueprint $table): void {
$table->integer('host_id')->primary();
$table->string('transport_address', 15);
$table->integer('transport_prefix_length');
$table->string('transport_interface', 15);
$table->string('transport_mode', 16);
$table->integer('mtu');
$table->integer('desired_revision');
$table->integer('observed_revision');
$table->string('sync_state', 16);
$table->string('last_operation_id', 32)->nullable();
$table->string('last_error_code', 64);
$table->integer('last_verified_at');
$table->integer('created_at');
$table->integer('updated_at');
});
Capsule::table('cloud_hosts')->insert([
'id' => 1, 'name' => 'HKG-A', 'region' => 'HongKong',
'country_code' => 'HK', 'default_network' => 'bridge:br0',
'enabled' => 1, 'created_at' => 1, 'updated_at' => 1,
]);
$first = CloudHostVpcTransportService::save(1, [
'transport_address' => '10.20.0.11',
'transport_prefix_length' => 24,
'transport_interface' => 'br-private',
'transport_mode' => 'direct_l3',
'mtu' => 1500,
'expected_revision' => 0,
]);
$assert($first['configured'] === true && $first['desired_revision'] === 1
&& $first['observed_revision'] === 0
&& $first['sync_state'] === 'pending' && $first['ready'] === false,
'First VPC transport save did not create pending revision 1.');
$replay = CloudHostVpcTransportService::save(1, [
'transport_address' => '10.20.0.11',
'transport_prefix_length' => 24,
'transport_interface' => 'br-private',
'transport_mode' => 'direct_l3',
'mtu' => 1500,
'expected_revision' => 1,
]);
$assert($replay['desired_revision'] === 1,
'Unchanged VPC transport save unexpectedly advanced the revision.');
$second = CloudHostVpcTransportService::save(1, [
'transport_address' => '10.20.0.11',
'transport_prefix_length' => 24,
'transport_interface' => 'br-private',
'transport_mode' => 'direct_l3',
'mtu' => 9000,
'expected_revision' => 1,
]);
$assert($second['desired_revision'] === 2
&& $second['observed_revision'] === 0
&& $second['sync_state'] === 'pending',
'Changed VPC transport did not invalidate the observed generation.');
try {
CloudHostVpcTransportService::save(1, [
'transport_address' => '10.20.0.11',
'transport_prefix_length' => 24,
'transport_interface' => 'br-private',
'transport_mode' => 'direct_l3', 'mtu' => 1500,
'expected_revision' => 1,
]);
throw new RuntimeException('Stale VPC transport revision was accepted.');
} catch (CloudError $error) {
$assert($error->httpStatus === 409,
'Stale VPC transport revision did not return a conflict.');
}
try {
CloudHostVpcTransportService::save(1, [
'transport_address' => '203.0.113.11',
'transport_prefix_length' => 24,
'transport_interface' => 'br-private',
'transport_mode' => 'direct_l3', 'mtu' => 1500,
'expected_revision' => 2,
]);
throw new RuntimeException('Public VPC underlay address was accepted.');
} catch (CloudError $error) {
$assert($error->httpStatus === 422,
'Public VPC underlay rejection used the wrong status.');
}
echo "Host VPC transport desired-state service passed.\n";