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/CloudInstanceNetworkPolicyT...

684 lines
23 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Models\CloudInstance;
use App\Models\CloudInstanceNetworkPolicyState;
use App\Models\CloudInstanceNetworkRestriction;
use App\Models\CloudServicePeriod;
use App\Services\Cloud\CloudInstanceNetworkPolicy;
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_traffic_pool_groups', static function (Blueprint $t): void {
$t->increments('id');
$t->string('code');
$t->string('name');
$t->string('mode');
$t->integer('created_at');
$t->integer('updated_at');
});
$schema->create('cloud_traffic_pools', static function (Blueprint $t): void {
$t->increments('id');
$t->integer('user_id');
$t->integer('traffic_pool_group_id');
$t->string('state');
$t->integer('available_bytes');
$t->integer('overage_bytes');
$t->integer('version');
$t->integer('last_sequence')->default(0);
$t->integer('last_event_id')->nullable();
$t->integer('created_at');
$t->integer('updated_at');
});
$schema->create('cloud_instances', static function (Blueprint $t): void {
$t->increments('id');
$t->integer('user_id');
$t->integer('current_period_id')->nullable();
$t->string('service_status');
$t->string('network_status');
$t->integer('network_suspended_at')->nullable();
$t->integer('expires_at')->nullable();
$t->integer('updated_at');
});
$schema->create('cloud_service_periods', static function (Blueprint $t): void {
$t->increments('id');
$t->integer('instance_id');
$t->integer('user_id');
$t->integer('traffic_pool_group_id')->nullable();
$t->string('traffic_quota_mode');
$t->string('status');
$t->integer('starts_at');
$t->integer('ends_at');
});
$schema->create('cloud_instance_network_restrictions',
static function (Blueprint $t): void {
$t->integer('instance_id');
$t->integer('user_id');
$t->string('reason_code');
$t->boolean('desired_active');
$t->integer('revision');
$t->string('source_type');
$t->string('source_id');
$t->integer('created_at');
$t->integer('updated_at');
$t->primary(['instance_id', 'reason_code']);
});
$schema->create('cloud_instance_network_policy_states',
static function (Blueprint $t): void {
$t->integer('instance_id')->primary();
$t->integer('user_id');
$t->boolean('desired_suspended');
$t->boolean('applied_suspended');
$t->integer('revision');
$t->string('state');
$t->string('operation_id')->nullable()->unique();
$t->integer('created_at');
$t->integer('updated_at');
});
Capsule::table('cloud_traffic_pool_groups')->insert([
['id' => 1, 'code' => 'shadow', 'name' => 'Shadow', 'mode' => 'shadow',
'created_at' => 1, 'updated_at' => 1],
['id' => 2, 'code' => 'shared', 'name' => 'Shared', 'mode' => 'enforced',
'created_at' => 1, 'updated_at' => 1],
]);
$instance = new CloudInstance();
$instance->id = 10;
$instance->user_id = 20;
$instance->service_status = 'active';
$instance->network_status = 'active';
$instance->network_suspended_at = null;
$instance->expires_at = 2000;
$instance->updated_at = 1000;
$instance->save();
$period = new CloudServicePeriod();
$period->id = 30;
$period->instance_id = 10;
$period->user_id = 20;
$period->traffic_pool_group_id = 1;
$period->traffic_quota_mode = 'finite';
$period->status = 'active';
$period->starts_at = 900;
$period->ends_at = 2000;
$period->save();
$shadow = $instance->getConnection()->transaction(
static function () use ($instance, $period): array {
$locked = CloudInstance::where('id', $instance->id)
->lockForUpdate()->firstOrFail();
return CloudInstanceNetworkPolicy::synchronizeLocked(
$locked,
$period,
1000
);
}
);
$assert($shadow['managed'] === false
&& CloudInstanceNetworkPolicyState::count() === 0,
'Shadow mode created an enforced network-policy projection.');
$instance->network_status = 'block_failed';
$instance->save();
$period->traffic_pool_group_id = 2;
$period->save();
$deferred = $instance->getConnection()->transaction(
static function () use ($instance, $period): array {
$locked = CloudInstance::where('id', $instance->id)
->lockForUpdate()->firstOrFail();
return CloudInstanceNetworkPolicy::synchronizeLocked(
$locked,
CloudServicePeriod::findOrFail($period->id),
1000
);
}
);
$assert(($deferred['deferred'] ?? false) === true
&& CloudInstanceNetworkPolicyState::count() === 0,
'An unresolved legacy network generation bypassed its recovery fence.');
$instance->network_status = 'active';
$instance->save();
Capsule::table('cloud_traffic_pools')->insert([
'id' => 40,
'user_id' => 20,
'traffic_pool_group_id' => 2,
'state' => 'available',
'available_bytes' => 100,
'overage_bytes' => 0,
'version' => 1,
'last_sequence' => 0,
'last_event_id' => null,
'created_at' => 1000,
'updated_at' => 1000,
]);
$available = $instance->getConnection()->transaction(
static function () use ($instance, $period): array {
$locked = CloudInstance::where('id', $instance->id)
->lockForUpdate()->firstOrFail();
$freshPeriod = CloudServicePeriod::findOrFail($period->id);
$result = CloudInstanceNetworkPolicy::synchronizeLocked(
$locked,
$freshPeriod,
1001
);
$locked->save();
return $result;
}
);
$assert($available['managed'] === true
&& $available['desired_suspended'] === false
&& $available['action'] === 'none',
'An available enforced pool requested a network suspension.');
Capsule::table('cloud_traffic_pools')->where('id', 40)->update([
'state' => 'exhausted',
'available_bytes' => 0,
'overage_bytes' => 5,
'version' => 2,
]);
$exhausted = $instance->getConnection()->transaction(
static function () use ($instance, $period): array {
$locked = CloudInstance::where('id', $instance->id)
->lockForUpdate()->firstOrFail();
$result = CloudInstanceNetworkPolicy::synchronizeLocked(
$locked,
CloudServicePeriod::findOrFail($period->id),
1002
);
$locked->save();
return $result;
}
);
$traffic = CloudInstanceNetworkRestriction::where(
'instance_id', 10
)->where('reason_code', 'traffic_pool_exhausted')->firstOrFail();
$trafficRevision = (int) $traffic->revision;
$assert($exhausted['desired_suspended'] === true
&& $exhausted['action'] === 'suspend'
&& (string) CloudInstance::find(10)->network_status === 'blocking'
&& (bool) $traffic->desired_active,
'Pool exhaustion did not create one pending aggregate suspension.');
$instance->getConnection()->transaction(static function () use ($instance): void {
$locked = CloudInstance::where('id', $instance->id)
->lockForUpdate()->firstOrFail();
CloudInstanceNetworkPolicy::setReasonLocked(
$locked,
CloudInstanceNetworkPolicy::REASON_TRAFFIC,
true,
'traffic_pool',
'pool:40:v2',
1003
);
$locked->save();
});
$assert((int) CloudInstanceNetworkRestriction::where('instance_id', 10)
->where('reason_code', 'traffic_pool_exhausted')->firstOrFail()->revision
=== $trafficRevision,
'Idempotent reason synchronization changed its revision.');
$firstOperation = str_repeat('a', 32);
$marker = $instance->getConnection()->transaction(
static function () use ($instance, $firstOperation): array {
$locked = CloudInstance::where('id', $instance->id)
->lockForUpdate()->firstOrFail();
CloudInstanceNetworkPolicy::setReasonLocked(
$locked,
CloudInstanceNetworkPolicy::REASON_SERVICE,
true,
'instance_expiry',
'2000',
1004
);
$marker = CloudInstanceNetworkPolicy::prepareJobLocked(
$locked,
'network_suspend',
$firstOperation,
1004
);
$locked->save();
return $marker;
}
);
$assert($marker['suspend_reason'] === 'expired'
&& CloudInstanceNetworkPolicy::jobStillDesired(
10,
'network_suspend',
$firstOperation
) === true,
'Multiple reasons did not collapse into one expiry-priority operation.');
// Both reasons disappear while the suspend is in flight. The successful
// suspend must be compensated by a later resume, never projected as active.
$instance->getConnection()->transaction(static function () use ($instance): void {
$locked = CloudInstance::where('id', $instance->id)
->lockForUpdate()->firstOrFail();
foreach ([
CloudInstanceNetworkPolicy::REASON_SERVICE,
CloudInstanceNetworkPolicy::REASON_TRAFFIC,
] as $reason) {
CloudInstanceNetworkPolicy::setReasonLocked(
$locked,
$reason,
false,
$reason === CloudInstanceNetworkPolicy::REASON_SERVICE
? 'instance_expiry' : 'traffic_pool',
$reason === CloudInstanceNetworkPolicy::REASON_SERVICE
? '2000' : 'pool:40:v3',
1005
);
}
$locked->save();
});
$compensation = $instance->getConnection()->transaction(
static function () use ($instance, $firstOperation): array {
$locked = CloudInstance::where('id', $instance->id)
->lockForUpdate()->firstOrFail();
$result = CloudInstanceNetworkPolicy::finishJobLocked(
$locked,
'network_suspend',
$firstOperation,
'succeeded',
false,
false,
1006
);
$locked->save();
return $result;
}
);
$assert($compensation['action'] === 'resume'
&& $compensation['applied_suspended'] === true
&& (string) CloudInstance::find(10)->network_status === 'restore_pending',
'A cleared in-flight suspend did not request compensating restoration.');
$resumeOperation = str_repeat('b', 32);
$instance->getConnection()->transaction(
static function () use ($instance, $resumeOperation): void {
$locked = CloudInstance::where('id', $instance->id)
->lockForUpdate()->firstOrFail();
CloudInstanceNetworkPolicy::prepareJobLocked(
$locked,
'network_resume',
$resumeOperation,
1007
);
CloudInstanceNetworkPolicy::setReasonLocked(
$locked,
CloudInstanceNetworkPolicy::REASON_ADMIN,
true,
'administrator',
'user:1',
1008
);
$locked->save();
}
);
$reblock = $instance->getConnection()->transaction(
static function () use ($instance, $resumeOperation): array {
$locked = CloudInstance::where('id', $instance->id)
->lockForUpdate()->firstOrFail();
$result = CloudInstanceNetworkPolicy::finishJobLocked(
$locked,
'network_resume',
$resumeOperation,
'succeeded',
false,
false,
1009
);
$locked->save();
return $result;
}
);
$assert($reblock['action'] === 'suspend'
&& $reblock['applied_suspended'] === false
&& (string) CloudInstance::find(10)->network_status === 'blocking',
'A reason added during resume left the instance connected.');
$secondSuspend = str_repeat('c', 32);
$instance->getConnection()->transaction(
static function () use ($instance, $secondSuspend): void {
$locked = CloudInstance::where('id', $instance->id)
->lockForUpdate()->firstOrFail();
CloudInstanceNetworkPolicy::prepareJobLocked(
$locked,
'network_suspend',
$secondSuspend,
1010
);
CloudInstanceNetworkPolicy::finishJobLocked(
$locked,
'network_suspend',
$secondSuspend,
'succeeded',
false,
false,
1011
);
$locked->save();
}
);
$assert((string) CloudInstance::find(10)->network_status === 'blocked_traffic',
'An applied administrator restriction was not projected as blocked.');
// An attention pool cannot clear an already asserted traffic reason.
$instance->getConnection()->transaction(static function () use ($instance): void {
$locked = CloudInstance::where('id', $instance->id)
->lockForUpdate()->firstOrFail();
CloudInstanceNetworkPolicy::setReasonLocked(
$locked,
CloudInstanceNetworkPolicy::REASON_TRAFFIC,
true,
'traffic_pool',
'pool:40:v3',
1012
);
$locked->save();
});
Capsule::table('cloud_traffic_pools')->where('id', 40)->update([
'state' => 'attention',
'available_bytes' => 100,
'overage_bytes' => 0,
'version' => 4,
]);
$attention = $instance->getConnection()->transaction(
static function () use ($instance, $period): array {
$locked = CloudInstance::where('id', $instance->id)
->lockForUpdate()->firstOrFail();
$result = CloudInstanceNetworkPolicy::synchronizeLocked(
$locked,
CloudServicePeriod::findOrFail($period->id),
1013
);
$locked->save();
return $result;
}
);
$assert($attention['traffic_decision'] === 'fenced'
&& in_array('traffic_pool_exhausted',
$attention['desired_reasons'], true),
'An attention projection guessed that traffic access could be restored.');
$fenceEndsAt = time() + 3600;
$fencedInstance = new CloudInstance();
$fencedInstance->id = 11;
$fencedInstance->user_id = 20;
$fencedInstance->current_period_id = 31;
$fencedInstance->service_status = 'active';
$fencedInstance->network_status = 'blocked_traffic';
$fencedInstance->network_suspended_at = 900;
$fencedInstance->expires_at = $fenceEndsAt;
$fencedInstance->updated_at = 1000;
$fencedInstance->save();
$fencedPeriod = new CloudServicePeriod();
$fencedPeriod->id = 31;
$fencedPeriod->instance_id = 11;
$fencedPeriod->user_id = 20;
$fencedPeriod->traffic_pool_group_id = 2;
$fencedPeriod->traffic_quota_mode = 'finite';
$fencedPeriod->status = 'active';
$fencedPeriod->starts_at = 900;
$fencedPeriod->ends_at = $fenceEndsAt;
$fencedPeriod->save();
$fenced = $fencedInstance->getConnection()->transaction(
static function () use ($fencedInstance, $fencedPeriod): array {
$locked = CloudInstance::where('id', $fencedInstance->id)
->lockForUpdate()->firstOrFail();
$result = CloudInstanceNetworkPolicy::synchronizeLocked(
$locked,
$fencedPeriod,
1013
);
$locked->save();
return $result;
}
);
$assert($fenced['traffic_decision'] === 'fenced'
&& $fenced['desired_suspended'] === true
&& $fenced['applied_suspended'] === true,
'An attention pool restored an already blocked instance during takeover.');
Capsule::table('cloud_traffic_pools')->where('id', 40)->update([
'state' => 'available',
'available_bytes' => 100,
'version' => 5,
]);
$fencedResume = str_repeat('f', 32);
$fencedInstance->getConnection()->transaction(
static function () use (
$fencedInstance,
$fencedPeriod,
$fencedResume
): void {
$locked = CloudInstance::where('id', $fencedInstance->id)
->lockForUpdate()->firstOrFail();
CloudInstanceNetworkPolicy::synchronizeLocked(
$locked,
$fencedPeriod,
1014
);
CloudInstanceNetworkPolicy::prepareJobLocked(
$locked,
'network_resume',
$fencedResume,
1014
);
$locked->save();
}
);
$assert(CloudInstanceNetworkPolicy::jobStillDesired(
(int) $fencedInstance->id,
'network_resume',
$fencedResume
) === true,
'An available current pool unexpectedly fenced a queued resume.');
Capsule::table('cloud_traffic_pools')->where('id', 40)->update([
'state' => 'attention',
'available_bytes' => 0,
'version' => 6,
]);
$assert(CloudInstanceNetworkPolicy::jobStillDesired(
(int) $fencedInstance->id,
'network_resume',
$fencedResume
) === false,
'A queued resume crossed an attention pool before instance convergence.');
$fencedInstance->getConnection()->transaction(
static function () use (
$fencedInstance,
$fencedPeriod,
$fencedResume
): void {
$locked = CloudInstance::where('id', $fencedInstance->id)
->lockForUpdate()->firstOrFail();
$result = CloudInstanceNetworkPolicy::synchronizeLocked(
$locked,
$fencedPeriod,
1015
);
if (($result['traffic_decision'] ?? null) !== 'fenced'
|| CloudInstanceNetworkPolicy::jobStillDesired(
(int) $fencedInstance->id,
'network_resume',
$fencedResume
) !== false) {
throw new RuntimeException(
'Attention did not invalidate a queued automatic resume.'
);
}
CloudInstanceNetworkPolicy::finishJobLocked(
$locked,
'network_resume',
$fencedResume,
'succeeded',
true,
false,
1015
);
$locked->save();
}
);
$assert((string) CloudInstance::find(11)->network_status === 'blocked_traffic'
&& (int) CloudInstance::find(11)->network_suspended_at === 900,
'A queued resume crossed an attention fence or rewrote block timing.');
// Unknown effect retains the exact operation fence. A fresh generation is
// forbidden; resolving that same operation may then converge the projection.
$instance->getConnection()->transaction(static function () use ($instance): void {
$locked = CloudInstance::where('id', $instance->id)
->lockForUpdate()->firstOrFail();
foreach ([
CloudInstanceNetworkPolicy::REASON_ADMIN,
CloudInstanceNetworkPolicy::REASON_TRAFFIC,
] as $reason) {
CloudInstanceNetworkPolicy::setReasonLocked(
$locked,
$reason,
false,
$reason === CloudInstanceNetworkPolicy::REASON_ADMIN
? 'administrator' : 'traffic_pool',
$reason === CloudInstanceNetworkPolicy::REASON_ADMIN
? 'user:1' : 'pool:40:v4',
1014
);
}
$locked->save();
});
$unknownOperation = str_repeat('d', 32);
$instance->getConnection()->transaction(
static function () use ($instance, $unknownOperation): void {
$locked = CloudInstance::where('id', $instance->id)
->lockForUpdate()->firstOrFail();
CloudInstanceNetworkPolicy::prepareJobLocked(
$locked,
'network_resume',
$unknownOperation,
1015
);
CloudInstanceNetworkPolicy::finishJobLocked(
$locked,
'network_resume',
$unknownOperation,
'failed',
false,
true,
1016
);
$locked->save();
}
);
$unknownState = CloudInstanceNetworkPolicyState::findOrFail(10);
$assert((string) $unknownState->state === 'unknown'
&& (string) $unknownState->operation_id === $unknownOperation
&& (string) CloudInstance::find(10)->network_status === 'restore_failed',
'An ambiguous resume released its exact operation fence.');
$assert($instance->getConnection()->transaction(
static function () use ($instance): bool {
return CloudInstanceNetworkPolicy::allowRetryLocked(
CloudInstance::where('id', $instance->id)
->lockForUpdate()->firstOrFail(),
'network_resume',
1017
);
}
) === false, 'An unknown-effect operation was converted into a fresh retry.');
$instance->getConnection()->transaction(
static function () use ($instance, $unknownOperation): void {
$locked = CloudInstance::where('id', $instance->id)
->lockForUpdate()->firstOrFail();
CloudInstanceNetworkPolicy::finishJobLocked(
$locked,
'network_resume',
$unknownOperation,
'succeeded',
false,
false,
1018
);
$locked->save();
}
);
$assert((string) CloudInstance::find(10)->network_status === 'active',
'Resolving the original resume operation did not restore the projection.');
// A policy mutation skipped before dispatch changes no remote state.
$skippedOperation = str_repeat('e', 32);
$instance->getConnection()->transaction(
static function () use ($instance, $skippedOperation): void {
$locked = CloudInstance::where('id', $instance->id)
->lockForUpdate()->firstOrFail();
CloudInstanceNetworkPolicy::setReasonLocked(
$locked,
CloudInstanceNetworkPolicy::REASON_ADMIN,
true,
'administrator',
'user:1',
1019
);
CloudInstanceNetworkPolicy::prepareJobLocked(
$locked,
'network_suspend',
$skippedOperation,
1019
);
CloudInstanceNetworkPolicy::setReasonLocked(
$locked,
CloudInstanceNetworkPolicy::REASON_ADMIN,
false,
'administrator',
'user:1',
1020
);
CloudInstanceNetworkPolicy::finishJobLocked(
$locked,
'network_suspend',
$skippedOperation,
'succeeded',
true,
false,
1021
);
$locked->save();
}
);
$assert((string) CloudInstance::find(10)->network_status === 'active'
&& (bool) CloudInstanceNetworkPolicyState::findOrFail(10)
->applied_suspended === false,
'A pre-dispatch skip was finalized as a real network suspension.');
$instance->getConnection()->transaction(static function () use ($instance): void {
CloudInstanceNetworkPolicy::releaseAfterInstanceDeletedLocked(
CloudInstance::where('id', $instance->id)
->lockForUpdate()->firstOrFail()
);
});
$fencedInstance->getConnection()->transaction(
static function () use ($fencedInstance): void {
CloudInstanceNetworkPolicy::releaseAfterInstanceDeletedLocked(
CloudInstance::where('id', $fencedInstance->id)
->lockForUpdate()->firstOrFail()
);
}
);
$assert(CloudInstanceNetworkPolicyState::count() === 0
&& CloudInstanceNetworkRestriction::count() === 0,
'Deleting an instance left aggregate network-policy state behind.');
echo "Cloud instance network policy checks passed.\n";