|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tests\Feature; |
| 6 | + |
| 7 | +use App\Models\WorkflowNamespace; |
| 8 | +use App\Support\ControlPlaneProtocol; |
| 9 | +use App\Support\WorkerProtocol; |
| 10 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 11 | +use Tests\TestCase; |
| 12 | +use Workflow\V2\Support\WorkerCompatibilityFleet; |
| 13 | + |
| 14 | +class WorkflowBootstrapRouteGatingTest extends TestCase |
| 15 | +{ |
| 16 | + use RefreshDatabase; |
| 17 | + |
| 18 | + protected function setUp(): void |
| 19 | + { |
| 20 | + parent::setUp(); |
| 21 | + |
| 22 | + config([ |
| 23 | + 'server.auth.driver' => 'token', |
| 24 | + 'server.auth.token' => null, |
| 25 | + 'server.auth.role_tokens' => [ |
| 26 | + 'worker' => 'worker-token', |
| 27 | + 'operator' => 'operator-token', |
| 28 | + 'admin' => 'admin-token', |
| 29 | + ], |
| 30 | + 'server.auth.backward_compatible' => true, |
| 31 | + ]); |
| 32 | + |
| 33 | + WorkflowNamespace::query()->updateOrCreate( |
| 34 | + ['name' => 'default'], |
| 35 | + [ |
| 36 | + 'description' => 'Default namespace', |
| 37 | + 'retention_days' => 30, |
| 38 | + 'status' => 'active', |
| 39 | + ], |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + public function test_pending_rollout_safety_migrations_block_control_plane_routes(): void |
| 44 | + { |
| 45 | + $this->blockWorkflowBootstrap(); |
| 46 | + |
| 47 | + $this->withHeaders($this->controlHeaders('operator-token')) |
| 48 | + ->getJson('/api/workflows') |
| 49 | + ->assertStatus(503) |
| 50 | + ->assertHeader(ControlPlaneProtocol::HEADER, ControlPlaneProtocol::VERSION) |
| 51 | + ->assertJsonPath('reason', 'workflow_v2_blocked') |
| 52 | + ->assertJsonPath('blocked_by.0', 'migrations') |
| 53 | + ->assertJsonPath( |
| 54 | + 'remediation', |
| 55 | + 'Restore database connectivity and migrate the workflow tables before relying on workflow v2 rollout-safety health.', |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + public function test_pending_rollout_safety_migrations_block_worker_protocol_routes(): void |
| 60 | + { |
| 61 | + $this->blockWorkflowBootstrap(); |
| 62 | + |
| 63 | + $this->withHeaders($this->workerHeaders('worker-token')) |
| 64 | + ->postJson('/api/worker/register', [ |
| 65 | + 'worker_id' => 'bootstrap-blocked-worker', |
| 66 | + 'task_queue' => 'default', |
| 67 | + 'runtime' => 'python', |
| 68 | + 'build_id' => 'build-a', |
| 69 | + ]) |
| 70 | + ->assertStatus(503) |
| 71 | + ->assertHeader(WorkerProtocol::HEADER, WorkerProtocol::VERSION) |
| 72 | + ->assertJsonPath('reason', 'workflow_v2_blocked') |
| 73 | + ->assertJsonPath('blocked_by.0', 'migrations'); |
| 74 | + } |
| 75 | + |
| 76 | + public function test_bootstrap_gate_runs_before_namespace_resolution_on_hosted_routes(): void |
| 77 | + { |
| 78 | + $this->blockWorkflowBootstrap(); |
| 79 | + |
| 80 | + $this->withHeaders($this->controlHeaders('operator-token', 'ghost-namespace')) |
| 81 | + ->getJson('/api/workflows') |
| 82 | + ->assertStatus(503) |
| 83 | + ->assertJsonPath('reason', 'workflow_v2_blocked') |
| 84 | + ->assertJsonMissing(['reason' => 'namespace_not_found']); |
| 85 | + } |
| 86 | + |
| 87 | + public function test_worker_registration_can_recover_fail_mode_compatibility_health(): void |
| 88 | + { |
| 89 | + config()->set('queue.default', 'redis'); |
| 90 | + config()->set('queue.connections.redis.driver', 'redis'); |
| 91 | + config()->set('workflows.v2.compatibility.current', 'build-a'); |
| 92 | + config()->set('workflows.v2.compatibility.supported', ['build-a']); |
| 93 | + config()->set('workflows.v2.fleet.validation_mode', 'fail'); |
| 94 | + |
| 95 | + WorkerCompatibilityFleet::clear(); |
| 96 | + |
| 97 | + $this->withHeaders($this->workerHeaders('worker-token')) |
| 98 | + ->postJson('/api/worker/register', [ |
| 99 | + 'worker_id' => 'build-a-worker', |
| 100 | + 'task_queue' => 'default', |
| 101 | + 'runtime' => 'python', |
| 102 | + 'build_id' => 'build-a', |
| 103 | + ]) |
| 104 | + ->assertCreated() |
| 105 | + ->assertHeader(WorkerProtocol::HEADER, WorkerProtocol::VERSION) |
| 106 | + ->assertJsonPath('worker_id', 'build-a-worker') |
| 107 | + ->assertJsonPath('registered', true); |
| 108 | + |
| 109 | + WorkerCompatibilityFleet::clear(); |
| 110 | + } |
| 111 | + |
| 112 | + private function blockWorkflowBootstrap(): void |
| 113 | + { |
| 114 | + \Illuminate\Support\Facades\DB::table('migrations') |
| 115 | + ->where('migration', '2026_04_21_000300_add_workflow_definition_fingerprints_to_worker_registrations') |
| 116 | + ->delete(); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * @return array<string, string> |
| 121 | + */ |
| 122 | + private function workerHeaders(string $token): array |
| 123 | + { |
| 124 | + return [ |
| 125 | + 'Authorization' => "Bearer {$token}", |
| 126 | + 'X-Namespace' => 'default', |
| 127 | + WorkerProtocol::HEADER => WorkerProtocol::VERSION, |
| 128 | + ]; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * @return array<string, string> |
| 133 | + */ |
| 134 | + private function controlHeaders(string $token, string $namespace = 'default'): array |
| 135 | + { |
| 136 | + return [ |
| 137 | + 'Authorization' => "Bearer {$token}", |
| 138 | + 'X-Namespace' => $namespace, |
| 139 | + ControlPlaneProtocol::HEADER => ControlPlaneProtocol::VERSION, |
| 140 | + ]; |
| 141 | + } |
| 142 | +} |
0 commit comments