mirror of
https://github.com/tiennm99/coolify.git
synced 2026-10-15 00:17:16 +00:00
Add Vultr cloud provider integration
This commit is contained in:
1 parent
2833c68af9
commit
94abbe1590
33 files changed
+2978
-44
No files matched your search
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
|
||||
use App\Models\CloudProviderToken;
|
||||
use App\Models\InstanceSettings;
|
||||
use App\Models\Team;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
@@ -9,6 +10,17 @@ use Illuminate\Support\Facades\Http;
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
beforeEach(function () {
|
||||
config([
|
||||
'app.maintenance.driver' => 'file',
|
||||
'cache.default' => 'array',
|
||||
'session.driver' => 'array',
|
||||
]);
|
||||
|
||||
InstanceSettings::unguarded(fn () => InstanceSettings::query()->create([
|
||||
'id' => 0,
|
||||
'is_api_enabled' => true,
|
||||
]));
|
||||
|
||||
// Create a team with owner
|
||||
$this->team = Team::factory()->create();
|
||||
$this->user = User::factory()->create();
|
||||
@@ -159,6 +171,30 @@ describe('POST /api/v1/cloud-tokens', function () {
|
||||
$response->assertJsonStructure(['uuid']);
|
||||
});
|
||||
|
||||
test('creates a Vultr cloud provider token', function () {
|
||||
Http::fake([
|
||||
'https://api.vultr.com/v2/account' => Http::response([], 200),
|
||||
]);
|
||||
|
||||
$response = $this->withHeaders([
|
||||
'Authorization' => 'Bearer '.$this->bearerToken,
|
||||
'Content-Type' => 'application/json',
|
||||
])->postJson('/api/v1/cloud-tokens', [
|
||||
'provider' => 'vultr',
|
||||
'token' => 'test-vultr-token',
|
||||
'name' => 'My Vultr Token',
|
||||
]);
|
||||
|
||||
$response->assertStatus(201);
|
||||
$response->assertJsonStructure(['uuid']);
|
||||
|
||||
$this->assertDatabaseHas('cloud_provider_tokens', [
|
||||
'team_id' => $this->team->id,
|
||||
'provider' => 'vultr',
|
||||
'name' => 'My Vultr Token',
|
||||
]);
|
||||
});
|
||||
|
||||
test('validates provider is required', function () {
|
||||
$response = $this->withHeaders([
|
||||
'Authorization' => 'Bearer '.$this->bearerToken,
|
||||
@@ -198,7 +234,7 @@ describe('POST /api/v1/cloud-tokens', function () {
|
||||
$response->assertJsonValidationErrors(['name']);
|
||||
});
|
||||
|
||||
test('validates provider must be hetzner or digitalocean', function () {
|
||||
test('validates provider must be hetzner digitalocean or vultr', function () {
|
||||
$response = $this->withHeaders([
|
||||
'Authorization' => 'Bearer '.$this->bearerToken,
|
||||
'Content-Type' => 'application/json',
|
||||
@@ -285,8 +321,7 @@ describe('PATCH /api/v1/cloud-tokens/{uuid}', function () {
|
||||
'Content-Type' => 'application/json',
|
||||
])->patchJson("/api/v1/cloud-tokens/{$token->uuid}", []);
|
||||
|
||||
$response->assertStatus(422);
|
||||
$response->assertJsonValidationErrors(['name']);
|
||||
$response->assertStatus(400);
|
||||
});
|
||||
|
||||
test('cannot update token from another team', function () {
|
||||
@@ -410,4 +445,23 @@ describe('POST /api/v1/cloud-tokens/{uuid}/validate', function () {
|
||||
$response->assertStatus(200);
|
||||
$response->assertJson(['valid' => true, 'message' => 'Token is valid.']);
|
||||
});
|
||||
|
||||
test('validates a valid Vultr token', function () {
|
||||
$token = CloudProviderToken::factory()->create([
|
||||
'team_id' => $this->team->id,
|
||||
'provider' => 'vultr',
|
||||
]);
|
||||
|
||||
Http::fake([
|
||||
'https://api.vultr.com/v2/account' => Http::response([], 200),
|
||||
]);
|
||||
|
||||
$response = $this->withHeaders([
|
||||
'Authorization' => 'Bearer '.$this->bearerToken,
|
||||
'Content-Type' => 'application/json',
|
||||
])->postJson("/api/v1/cloud-tokens/{$token->uuid}/validate");
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertJson(['valid' => true, 'message' => 'Token is valid.']);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,374 @@
|
||||
<?php
|
||||
|
||||
use App\Models\CloudProviderToken;
|
||||
use App\Models\InstanceSettings;
|
||||
use App\Models\PrivateKey;
|
||||
use App\Models\Team;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
beforeEach(function () {
|
||||
config([
|
||||
'app.maintenance.driver' => 'file',
|
||||
'cache.default' => 'array',
|
||||
'session.driver' => 'array',
|
||||
]);
|
||||
|
||||
InstanceSettings::unguarded(fn () => InstanceSettings::query()->create([
|
||||
'id' => 0,
|
||||
'is_api_enabled' => true,
|
||||
]));
|
||||
|
||||
$this->team = Team::factory()->create();
|
||||
$this->user = User::factory()->create();
|
||||
$this->team->members()->attach($this->user->id, ['role' => 'owner']);
|
||||
|
||||
session(['currentTeam' => $this->team]);
|
||||
$this->token = $this->user->createToken('test-token', ['*']);
|
||||
$this->bearerToken = $this->token->plainTextToken;
|
||||
|
||||
$this->vultrToken = CloudProviderToken::create([
|
||||
'team_id' => $this->team->id,
|
||||
'provider' => 'vultr',
|
||||
'token' => 'test-vultr-api-token',
|
||||
'name' => 'Test Vultr Token',
|
||||
]);
|
||||
|
||||
$this->privateKey = PrivateKey::create([
|
||||
'team_id' => $this->team->id,
|
||||
'name' => 'Test Private Key',
|
||||
'description' => 'Test private key',
|
||||
'private_key' => testPrivateKey(),
|
||||
]);
|
||||
});
|
||||
|
||||
function testPrivateKey(): string
|
||||
{
|
||||
return <<<'KEY'
|
||||
-----BEGIN OPENSSH PRIVATE KEY-----
|
||||
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
|
||||
QyNTUxOQAAACBbhpqHhqv6aI67Mj9abM3DVbmcfYhZAhC7ca4d9UCevAAAAJi/QySHv0Mk
|
||||
hwAAAAtzc2gtZWQyNTUxOQAAACBbhpqHhqv6aI67Mj9abM3DVbmcfYhZAhC7ca4d9UCevA
|
||||
AAAECBQw4jg1WRT2IGHMncCiZhURCts2s24HoDS0thHnnRKVuGmoeGq/pojrsyP1pszcNV
|
||||
uZx9iFkCELtxrh31QJ68AAAAEXNhaWxANzZmZjY2ZDJlMmRkAQIDBA==
|
||||
-----END OPENSSH PRIVATE KEY-----
|
||||
KEY;
|
||||
}
|
||||
|
||||
function testPublicKey(): string
|
||||
{
|
||||
return 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFuGmoeGq/pojrsyP1pszcNVuZx9iFkCELtxrh31QJ68';
|
||||
}
|
||||
|
||||
describe('GET /api/v1/vultr/regions', function () {
|
||||
test('gets Vultr regions', function () {
|
||||
Http::fake([
|
||||
'https://api.vultr.com/v2/regions*' => Http::response([
|
||||
'regions' => [
|
||||
['id' => 'ewr', 'city' => 'New Jersey', 'country' => 'US'],
|
||||
['id' => 'ams', 'city' => 'Amsterdam', 'country' => 'NL'],
|
||||
],
|
||||
'meta' => ['links' => ['next' => null]],
|
||||
], 200),
|
||||
]);
|
||||
|
||||
$response = $this->withHeaders([
|
||||
'Authorization' => 'Bearer '.$this->bearerToken,
|
||||
'Content-Type' => 'application/json',
|
||||
])->getJson('/api/v1/vultr/regions?cloud_provider_token_id='.$this->vultrToken->uuid);
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertJsonCount(2);
|
||||
$response->assertJsonFragment(['id' => 'ewr']);
|
||||
});
|
||||
|
||||
test('requires cloud_provider_token_id parameter', function () {
|
||||
$response = $this->withHeaders([
|
||||
'Authorization' => 'Bearer '.$this->bearerToken,
|
||||
'Content-Type' => 'application/json',
|
||||
])->getJson('/api/v1/vultr/regions');
|
||||
|
||||
$response->assertStatus(422);
|
||||
$response->assertJsonValidationErrors(['cloud_provider_token_id']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /api/v1/vultr/plans', function () {
|
||||
test('gets Vultr plans', function () {
|
||||
Http::fake([
|
||||
'https://api.vultr.com/v2/plans*' => Http::response([
|
||||
'plans' => [
|
||||
['id' => 'vc2-1c-1gb', 'vcpu_count' => 1, 'ram' => 1024, 'disk' => 25],
|
||||
['id' => 'vc2-2c-2gb', 'vcpu_count' => 2, 'ram' => 2048, 'disk' => 55],
|
||||
],
|
||||
'meta' => ['links' => ['next' => null]],
|
||||
], 200),
|
||||
]);
|
||||
|
||||
$response = $this->withHeaders([
|
||||
'Authorization' => 'Bearer '.$this->bearerToken,
|
||||
'Content-Type' => 'application/json',
|
||||
])->getJson('/api/v1/vultr/plans?cloud_provider_token_uuid='.$this->vultrToken->uuid);
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertJsonCount(2);
|
||||
$response->assertJsonFragment(['id' => 'vc2-1c-1gb']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /api/v1/vultr/os', function () {
|
||||
test('gets Vultr operating systems', function () {
|
||||
Http::fake([
|
||||
'https://api.vultr.com/v2/os*' => Http::response([
|
||||
'os' => [
|
||||
['id' => 2284, 'name' => 'Ubuntu 24.04 LTS x64'],
|
||||
['id' => 2136, 'name' => 'Debian 12 x64'],
|
||||
],
|
||||
'meta' => ['links' => ['next' => null]],
|
||||
], 200),
|
||||
]);
|
||||
|
||||
$response = $this->withHeaders([
|
||||
'Authorization' => 'Bearer '.$this->bearerToken,
|
||||
'Content-Type' => 'application/json',
|
||||
])->getJson('/api/v1/vultr/os?cloud_provider_token_id='.$this->vultrToken->uuid);
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertJsonCount(2);
|
||||
$response->assertJsonFragment(['name' => 'Ubuntu 24.04 LTS x64']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /api/v1/vultr/ssh-keys', function () {
|
||||
test('gets Vultr SSH keys', function () {
|
||||
Http::fake([
|
||||
'https://api.vultr.com/v2/ssh-keys*' => Http::response([
|
||||
'ssh_keys' => [
|
||||
['id' => 'key-1', 'name' => 'my-key', 'ssh_key' => testPublicKey()],
|
||||
['id' => 'key-2', 'name' => 'another-key', 'ssh_key' => 'ssh-ed25519 AAAAother'],
|
||||
],
|
||||
'meta' => ['links' => ['next' => null]],
|
||||
], 200),
|
||||
]);
|
||||
|
||||
$response = $this->withHeaders([
|
||||
'Authorization' => 'Bearer '.$this->bearerToken,
|
||||
'Content-Type' => 'application/json',
|
||||
])->getJson('/api/v1/vultr/ssh-keys?cloud_provider_token_id='.$this->vultrToken->uuid);
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertJsonCount(2);
|
||||
$response->assertJsonFragment(['name' => 'my-key']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('POST /api/v1/servers/vultr', function () {
|
||||
test('creates a Vultr server', function () {
|
||||
Http::fake([
|
||||
'https://api.vultr.com/v2/ssh-keys' => Http::response([
|
||||
'ssh_key' => ['id' => 'key-1', 'ssh_key' => testPublicKey()],
|
||||
], 201),
|
||||
'https://api.vultr.com/v2/ssh-keys*' => Http::response([
|
||||
'ssh_keys' => [],
|
||||
'meta' => ['links' => ['next' => null]],
|
||||
], 200),
|
||||
'https://api.vultr.com/v2/instances' => Http::response([
|
||||
'instance' => [
|
||||
'id' => 'instance-1',
|
||||
'label' => 'test-server',
|
||||
'main_ip' => '1.2.3.4',
|
||||
'v6_main_ip' => '2001:db8::1',
|
||||
'status' => 'pending',
|
||||
],
|
||||
], 202),
|
||||
]);
|
||||
|
||||
$response = $this->withHeaders([
|
||||
'Authorization' => 'Bearer '.$this->bearerToken,
|
||||
'Content-Type' => 'application/json',
|
||||
])->postJson('/api/v1/servers/vultr', [
|
||||
'cloud_provider_token_id' => $this->vultrToken->uuid,
|
||||
'region' => 'ewr',
|
||||
'plan' => 'vc2-1c-1gb',
|
||||
'os_id' => 2284,
|
||||
'name' => 'test-server',
|
||||
'private_key_uuid' => $this->privateKey->uuid,
|
||||
'enable_ipv6' => true,
|
||||
'cloud_init_script' => "#cloud-config\npackages:\n - curl",
|
||||
]);
|
||||
|
||||
$response->assertStatus(201);
|
||||
$response->assertJsonStructure(['uuid', 'vultr_instance_id', 'ip']);
|
||||
$response->assertJsonFragment(['vultr_instance_id' => 'instance-1', 'ip' => '1.2.3.4']);
|
||||
|
||||
$this->assertDatabaseHas('servers', [
|
||||
'name' => 'test-server',
|
||||
'ip' => '1.2.3.4',
|
||||
'team_id' => $this->team->id,
|
||||
'vultr_instance_id' => 'instance-1',
|
||||
'vultr_instance_status' => 'pending',
|
||||
]);
|
||||
|
||||
Http::assertSent(function ($request) {
|
||||
return $request->url() === 'https://api.vultr.com/v2/instances'
|
||||
&& $request['region'] === 'ewr'
|
||||
&& $request['plan'] === 'vc2-1c-1gb'
|
||||
&& $request['os_id'] === 2284
|
||||
&& $request['sshkey_id'] === ['key-1']
|
||||
&& $request['user_data'] === base64_encode("#cloud-config\npackages:\n - curl");
|
||||
});
|
||||
});
|
||||
|
||||
test('waits for Vultr to assign a real public IP when creation returns placeholder IP', function () {
|
||||
Http::fake([
|
||||
'https://api.vultr.com/v2/ssh-keys' => Http::response([
|
||||
'ssh_key' => ['id' => 'key-1', 'ssh_key' => testPublicKey()],
|
||||
], 201),
|
||||
'https://api.vultr.com/v2/ssh-keys*' => Http::response([
|
||||
'ssh_keys' => [],
|
||||
'meta' => ['links' => ['next' => null]],
|
||||
], 200),
|
||||
'https://api.vultr.com/v2/instances' => Http::response([
|
||||
'instance' => [
|
||||
'id' => 'instance-1',
|
||||
'main_ip' => '0.0.0.0',
|
||||
'status' => 'pending',
|
||||
],
|
||||
], 202),
|
||||
'https://api.vultr.com/v2/instances/instance-1' => Http::response([
|
||||
'instance' => [
|
||||
'id' => 'instance-1',
|
||||
'main_ip' => '1.2.3.4',
|
||||
'status' => 'active',
|
||||
],
|
||||
], 200),
|
||||
]);
|
||||
|
||||
$response = $this->withHeaders([
|
||||
'Authorization' => 'Bearer '.$this->bearerToken,
|
||||
'Content-Type' => 'application/json',
|
||||
])->postJson('/api/v1/servers/vultr', [
|
||||
'cloud_provider_token_id' => $this->vultrToken->uuid,
|
||||
'region' => 'ewr',
|
||||
'plan' => 'vc2-1c-1gb',
|
||||
'os_id' => 2284,
|
||||
'name' => 'test-server',
|
||||
'private_key_uuid' => $this->privateKey->uuid,
|
||||
]);
|
||||
|
||||
$response->assertStatus(201);
|
||||
$response->assertJsonFragment(['ip' => '1.2.3.4']);
|
||||
|
||||
$this->assertDatabaseHas('servers', [
|
||||
'name' => 'test-server',
|
||||
'ip' => '1.2.3.4',
|
||||
'vultr_instance_status' => 'active',
|
||||
]);
|
||||
});
|
||||
|
||||
test('reuses existing matching Vultr SSH key', function () {
|
||||
Http::fake([
|
||||
'https://api.vultr.com/v2/ssh-keys*' => Http::response([
|
||||
'ssh_keys' => [
|
||||
['id' => 'existing-key', 'name' => 'Existing', 'ssh_key' => testPublicKey().' comment'],
|
||||
],
|
||||
'meta' => ['links' => ['next' => null]],
|
||||
], 200),
|
||||
'https://api.vultr.com/v2/instances' => Http::response([
|
||||
'instance' => [
|
||||
'id' => 'instance-1',
|
||||
'main_ip' => '1.2.3.4',
|
||||
'status' => 'pending',
|
||||
],
|
||||
], 202),
|
||||
]);
|
||||
|
||||
$response = $this->withHeaders([
|
||||
'Authorization' => 'Bearer '.$this->bearerToken,
|
||||
'Content-Type' => 'application/json',
|
||||
])->postJson('/api/v1/servers/vultr', [
|
||||
'cloud_provider_token_id' => $this->vultrToken->uuid,
|
||||
'region' => 'ewr',
|
||||
'plan' => 'vc2-1c-1gb',
|
||||
'os_id' => 2284,
|
||||
'name' => 'test-server',
|
||||
'private_key_uuid' => $this->privateKey->uuid,
|
||||
]);
|
||||
|
||||
$response->assertStatus(201);
|
||||
|
||||
Http::assertNotSent(function ($request) {
|
||||
return $request->url() === 'https://api.vultr.com/v2/ssh-keys'
|
||||
&& $request->method() === 'POST';
|
||||
});
|
||||
Http::assertSent(function ($request) {
|
||||
return $request->url() === 'https://api.vultr.com/v2/instances'
|
||||
&& $request['sshkey_id'] === ['existing-key'];
|
||||
});
|
||||
});
|
||||
|
||||
test('validates required fields', function () {
|
||||
$response = $this->withHeaders([
|
||||
'Authorization' => 'Bearer '.$this->bearerToken,
|
||||
'Content-Type' => 'application/json',
|
||||
])->postJson('/api/v1/servers/vultr', []);
|
||||
|
||||
$response->assertStatus(400);
|
||||
});
|
||||
|
||||
test('returns 404 for non-existent Vultr token', function () {
|
||||
$response = $this->withHeaders([
|
||||
'Authorization' => 'Bearer '.$this->bearerToken,
|
||||
'Content-Type' => 'application/json',
|
||||
])->postJson('/api/v1/servers/vultr', [
|
||||
'cloud_provider_token_id' => 'non-existent-uuid',
|
||||
'region' => 'ewr',
|
||||
'plan' => 'vc2-1c-1gb',
|
||||
'os_id' => 2284,
|
||||
'private_key_uuid' => $this->privateKey->uuid,
|
||||
]);
|
||||
|
||||
$response->assertStatus(404);
|
||||
$response->assertJson(['message' => 'Vultr cloud provider token not found.']);
|
||||
});
|
||||
|
||||
test('uses IPv6 when public IPv4 is disabled', function () {
|
||||
Http::fake([
|
||||
'https://api.vultr.com/v2/ssh-keys' => Http::response([
|
||||
'ssh_key' => ['id' => 'key-1', 'ssh_key' => testPublicKey()],
|
||||
], 201),
|
||||
'https://api.vultr.com/v2/ssh-keys*' => Http::response([
|
||||
'ssh_keys' => [],
|
||||
'meta' => ['links' => ['next' => null]],
|
||||
], 200),
|
||||
'https://api.vultr.com/v2/instances' => Http::response([
|
||||
'instance' => [
|
||||
'id' => 'instance-1',
|
||||
'main_ip' => '',
|
||||
'v6_main_ip' => '2001:db8::1',
|
||||
'status' => 'pending',
|
||||
],
|
||||
], 202),
|
||||
]);
|
||||
|
||||
$response = $this->withHeaders([
|
||||
'Authorization' => 'Bearer '.$this->bearerToken,
|
||||
'Content-Type' => 'application/json',
|
||||
])->postJson('/api/v1/servers/vultr', [
|
||||
'cloud_provider_token_id' => $this->vultrToken->uuid,
|
||||
'region' => 'ewr',
|
||||
'plan' => 'vc2-1c-1gb',
|
||||
'os_id' => 2284,
|
||||
'name' => 'test-server',
|
||||
'private_key_uuid' => $this->privateKey->uuid,
|
||||
'enable_ipv6' => true,
|
||||
'disable_public_ipv4' => true,
|
||||
]);
|
||||
|
||||
$response->assertStatus(201);
|
||||
$response->assertJsonFragment(['ip' => '2001:db8::1']);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
use App\Livewire\Server\New\ByVultr;
|
||||
use App\Models\CloudProviderToken;
|
||||
use App\Models\InstanceSettings;
|
||||
use App\Models\PrivateKey;
|
||||
use App\Models\Team;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Livewire\Livewire;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
beforeEach(function () {
|
||||
config([
|
||||
'app.maintenance.driver' => 'file',
|
||||
'cache.default' => 'array',
|
||||
'session.driver' => 'array',
|
||||
]);
|
||||
|
||||
InstanceSettings::unguarded(fn () => InstanceSettings::query()->create([
|
||||
'id' => 0,
|
||||
'is_api_enabled' => true,
|
||||
]));
|
||||
|
||||
$this->team = Team::factory()->create();
|
||||
$this->user = User::factory()->create();
|
||||
$this->team->members()->attach($this->user->id, ['role' => 'owner']);
|
||||
|
||||
$this->actingAs($this->user);
|
||||
session(['currentTeam' => $this->team]);
|
||||
|
||||
$this->vultrToken = CloudProviderToken::create([
|
||||
'team_id' => $this->team->id,
|
||||
'provider' => 'vultr',
|
||||
'token' => 'test-vultr-token',
|
||||
'name' => 'Test Vultr Token',
|
||||
]);
|
||||
|
||||
$this->privateKey = PrivateKey::create([
|
||||
'team_id' => $this->team->id,
|
||||
'name' => 'Test Private Key',
|
||||
'description' => 'Test private key',
|
||||
'private_key' => vultrLivewireTestPrivateKey(),
|
||||
]);
|
||||
});
|
||||
|
||||
function vultrLivewireTestPrivateKey(): string
|
||||
{
|
||||
return <<<'KEY'
|
||||
-----BEGIN OPENSSH PRIVATE KEY-----
|
||||
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
|
||||
QyNTUxOQAAACBbhpqHhqv6aI67Mj9abM3DVbmcfYhZAhC7ca4d9UCevAAAAJi/QySHv0Mk
|
||||
hwAAAAtzc2gtZWQyNTUxOQAAACBbhpqHhqv6aI67Mj9abM3DVbmcfYhZAhC7ca4d9UCevA
|
||||
AAAECBQw4jg1WRT2IGHMncCiZhURCts2s24HoDS0thHnnRKVuGmoeGq/pojrsyP1pszcNV
|
||||
uZx9iFkCELtxrh31QJ68AAAAEXNhaWxANzZmZjY2ZDJlMmRkAQIDBA==
|
||||
-----END OPENSSH PRIVATE KEY-----
|
||||
KEY;
|
||||
}
|
||||
|
||||
function vultrLivewireTestPublicKey(): string
|
||||
{
|
||||
return 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFuGmoeGq/pojrsyP1pszcNVuZx9iFkCELtxrh31QJ68';
|
||||
}
|
||||
|
||||
it('creates a Vultr server through the Livewire flow', function () {
|
||||
Http::fake([
|
||||
'https://api.vultr.com/v2/regions*' => Http::response([
|
||||
'regions' => [
|
||||
['id' => 'ewr', 'city' => 'New Jersey', 'country' => 'US'],
|
||||
],
|
||||
'meta' => ['links' => ['next' => null]],
|
||||
], 200),
|
||||
'https://api.vultr.com/v2/plans*' => Http::response([
|
||||
'plans' => [
|
||||
['id' => 'vc2-1c-1gb', 'vcpu_count' => 1, 'ram' => 1024, 'disk' => 25, 'monthly_cost' => 6, 'locations' => ['ewr']],
|
||||
],
|
||||
'meta' => ['links' => ['next' => null]],
|
||||
], 200),
|
||||
'https://api.vultr.com/v2/os*' => Http::response([
|
||||
'os' => [
|
||||
['id' => 2284, 'name' => 'Ubuntu 24.04 LTS x64'],
|
||||
],
|
||||
'meta' => ['links' => ['next' => null]],
|
||||
], 200),
|
||||
'https://api.vultr.com/v2/ssh-keys' => Http::response([
|
||||
'ssh_key' => ['id' => 'key-1', 'ssh_key' => vultrLivewireTestPublicKey()],
|
||||
], 201),
|
||||
'https://api.vultr.com/v2/ssh-keys*' => Http::response([
|
||||
'ssh_keys' => [],
|
||||
'meta' => ['links' => ['next' => null]],
|
||||
], 200),
|
||||
'https://api.vultr.com/v2/instances' => Http::response([
|
||||
'instance' => [
|
||||
'id' => 'instance-1',
|
||||
'label' => 'test-vultr-server',
|
||||
'main_ip' => '1.2.3.4',
|
||||
'v6_main_ip' => '2001:db8::1',
|
||||
'status' => 'pending',
|
||||
],
|
||||
], 202),
|
||||
]);
|
||||
|
||||
Livewire::test(ByVultr::class)
|
||||
->set('selected_token_id', $this->vultrToken->id)
|
||||
->call('nextStep')
|
||||
->assertSet('current_step', 2)
|
||||
->set('server_name', 'test-vultr-server')
|
||||
->set('selected_region', 'ewr')
|
||||
->set('selected_plan', 'vc2-1c-1gb')
|
||||
->set('selected_os_id', 2284)
|
||||
->set('private_key_id', $this->privateKey->id)
|
||||
->set('enable_ipv6', true)
|
||||
->set('cloud_init_script', "#cloud-config\npackages:\n - curl")
|
||||
->call('submit');
|
||||
|
||||
$this->assertDatabaseHas('servers', [
|
||||
'name' => 'test-vultr-server',
|
||||
'ip' => '1.2.3.4',
|
||||
'team_id' => $this->team->id,
|
||||
'cloud_provider_token_id' => $this->vultrToken->id,
|
||||
'vultr_instance_id' => 'instance-1',
|
||||
'vultr_instance_status' => 'pending',
|
||||
]);
|
||||
|
||||
Http::assertSent(function ($request) {
|
||||
return $request->url() === 'https://api.vultr.com/v2/instances'
|
||||
&& $request['region'] === 'ewr'
|
||||
&& $request['plan'] === 'vc2-1c-1gb'
|
||||
&& $request['os_id'] === 2284
|
||||
&& $request['sshkey_id'] === ['key-1']
|
||||
&& $request['user_data'] === base64_encode("#cloud-config\npackages:\n - curl");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,296 @@
|
||||
<?php
|
||||
|
||||
use App\Actions\Server\ValidateServer as ValidateServerAction;
|
||||
use App\Livewire\Server\Show;
|
||||
use App\Livewire\Server\ValidateAndInstall;
|
||||
use App\Models\CloudProviderToken;
|
||||
use App\Models\InstanceSettings;
|
||||
use App\Models\PrivateKey;
|
||||
use App\Models\Server;
|
||||
use App\Models\Team;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Livewire\Livewire;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
beforeEach(function () {
|
||||
config([
|
||||
'app.maintenance.driver' => 'file',
|
||||
'cache.default' => 'array',
|
||||
'session.driver' => 'array',
|
||||
]);
|
||||
|
||||
InstanceSettings::unguarded(fn () => InstanceSettings::query()->create([
|
||||
'id' => 0,
|
||||
'is_api_enabled' => true,
|
||||
]));
|
||||
|
||||
$this->team = Team::factory()->create();
|
||||
$this->user = User::factory()->create();
|
||||
$this->team->members()->attach($this->user->id, ['role' => 'owner']);
|
||||
|
||||
$this->actingAs($this->user);
|
||||
session(['currentTeam' => $this->team]);
|
||||
|
||||
$this->privateKey = PrivateKey::create([
|
||||
'team_id' => $this->team->id,
|
||||
'name' => 'Test Private Key',
|
||||
'description' => 'Test private key',
|
||||
'private_key' => vultrLifecycleTestPrivateKey(),
|
||||
]);
|
||||
|
||||
$this->vultrToken = CloudProviderToken::create([
|
||||
'team_id' => $this->team->id,
|
||||
'provider' => 'vultr',
|
||||
'token' => 'test-vultr-token',
|
||||
'name' => 'Test Vultr Token',
|
||||
]);
|
||||
|
||||
$this->server = Server::factory()->create([
|
||||
'team_id' => $this->team->id,
|
||||
'private_key_id' => $this->privateKey->id,
|
||||
'cloud_provider_token_id' => $this->vultrToken->id,
|
||||
'vultr_instance_id' => 'instance-1',
|
||||
'vultr_instance_status' => 'pending',
|
||||
'ip' => '1.2.3.4',
|
||||
]);
|
||||
});
|
||||
|
||||
function vultrLifecycleTestPrivateKey(): string
|
||||
{
|
||||
return <<<'KEY'
|
||||
-----BEGIN OPENSSH PRIVATE KEY-----
|
||||
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
|
||||
QyNTUxOQAAACBbhpqHhqv6aI67Mj9abM3DVbmcfYhZAhC7ca4d9UCevAAAAJi/QySHv0Mk
|
||||
hwAAAAtzc2gtZWQyNTUxOQAAACBbhpqHhqv6aI67Mj9abM3DVbmcfYhZAhC7ca4d9UCevA
|
||||
AAAECBQw4jg1WRT2IGHMncCiZhURCts2s24HoDS0thHnnRKVuGmoeGq/pojrsyP1pszcNV
|
||||
uZx9iFkCELtxrh31QJ68AAAAEXNhaWxANzZmZjY2ZDJlMmRkAQIDBA==
|
||||
-----END OPENSSH PRIVATE KEY-----
|
||||
KEY;
|
||||
}
|
||||
|
||||
it('refreshes Vultr instance status', function () {
|
||||
Http::fake([
|
||||
'https://api.vultr.com/v2/instances/instance-1' => Http::response([
|
||||
'instance' => [
|
||||
'id' => 'instance-1',
|
||||
'status' => 'active',
|
||||
],
|
||||
], 200),
|
||||
]);
|
||||
|
||||
Livewire::test(Show::class, ['server_uuid' => $this->server->uuid])
|
||||
->call('checkVultrInstanceStatus', true)
|
||||
->assertSet('vultrInstanceStatus', 'active');
|
||||
|
||||
$this->assertDatabaseHas('servers', [
|
||||
'id' => $this->server->id,
|
||||
'vultr_instance_status' => 'active',
|
||||
]);
|
||||
});
|
||||
|
||||
it('refreshes Vultr status on server page load even when cached status is active', function () {
|
||||
$this->server->update(['vultr_instance_status' => 'active']);
|
||||
|
||||
Livewire::test(Show::class, ['server_uuid' => $this->server->uuid])
|
||||
->assertSee('$wire.checkVultrInstanceStatus();', false);
|
||||
});
|
||||
|
||||
it('updates placeholder server IP when Vultr status refresh returns assigned public IP', function () {
|
||||
$this->server->update(['ip' => '0.0.0.0']);
|
||||
|
||||
Http::fake([
|
||||
'https://api.vultr.com/v2/instances/instance-1' => Http::response([
|
||||
'instance' => [
|
||||
'id' => 'instance-1',
|
||||
'status' => 'active',
|
||||
'main_ip' => '9.8.7.6',
|
||||
],
|
||||
], 200),
|
||||
]);
|
||||
|
||||
Livewire::test(Show::class, ['server_uuid' => $this->server->uuid])
|
||||
->call('checkVultrInstanceStatus', true)
|
||||
->assertSet('ip', '9.8.7.6');
|
||||
|
||||
$this->assertDatabaseHas('servers', [
|
||||
'id' => $this->server->id,
|
||||
'ip' => '9.8.7.6',
|
||||
'vultr_instance_status' => 'active',
|
||||
]);
|
||||
});
|
||||
|
||||
it('does not overwrite an established server IP during Vultr status refresh', function () {
|
||||
Http::fake([
|
||||
'https://api.vultr.com/v2/instances/instance-1' => Http::response([
|
||||
'instance' => [
|
||||
'id' => 'instance-1',
|
||||
'status' => 'active',
|
||||
'main_ip' => '9.8.7.6',
|
||||
],
|
||||
], 200),
|
||||
]);
|
||||
|
||||
Livewire::test(Show::class, ['server_uuid' => $this->server->uuid])
|
||||
->call('checkVultrInstanceStatus', true)
|
||||
->assertSet('ip', '1.2.3.4')
|
||||
->assertSet('vultrInstanceStatus', 'active');
|
||||
|
||||
$this->assertDatabaseHas('servers', [
|
||||
'id' => $this->server->id,
|
||||
'ip' => '1.2.3.4',
|
||||
'vultr_instance_status' => 'active',
|
||||
]);
|
||||
});
|
||||
|
||||
it('blocks server page validation when Vultr instance is stopped', function () {
|
||||
Http::fake([
|
||||
'https://api.vultr.com/v2/instances/instance-1' => Http::response([
|
||||
'instance' => [
|
||||
'id' => 'instance-1',
|
||||
'status' => 'active',
|
||||
'power_status' => 'stopped',
|
||||
'main_ip' => '1.2.3.4',
|
||||
],
|
||||
], 200),
|
||||
]);
|
||||
|
||||
Livewire::test(Show::class, ['server_uuid' => $this->server->uuid])
|
||||
->call('validateServer')
|
||||
->assertSet('vultrInstanceStatus', 'stopped')
|
||||
->assertDispatched('error', 'Vultr instance is stopped. Power it on before validating.')
|
||||
->assertNotDispatched('init');
|
||||
|
||||
$this->assertDatabaseHas('servers', [
|
||||
'id' => $this->server->id,
|
||||
'vultr_instance_status' => 'stopped',
|
||||
]);
|
||||
|
||||
expect($this->server->fresh()->validation_logs)->toBeNull();
|
||||
});
|
||||
|
||||
it('marks missing Vultr instances as deleted before validation', function () {
|
||||
Http::fake([
|
||||
'https://api.vultr.com/v2/instances/instance-1' => Http::response([
|
||||
'error' => 'instance not found',
|
||||
], 404),
|
||||
]);
|
||||
|
||||
Livewire::test(Show::class, ['server_uuid' => $this->server->uuid])
|
||||
->call('validateServer')
|
||||
->assertSet('vultrInstanceStatus', 'deleted')
|
||||
->assertDispatched('error', 'Vultr instance is deleted or no longer accessible. Relink this server before validating.')
|
||||
->assertNotDispatched('init');
|
||||
|
||||
$this->assertDatabaseHas('servers', [
|
||||
'id' => $this->server->id,
|
||||
'vultr_instance_status' => 'deleted',
|
||||
]);
|
||||
});
|
||||
|
||||
it('blocks validation modal connection checks when Vultr instance is stopped', function () {
|
||||
Http::fake([
|
||||
'https://api.vultr.com/v2/instances/instance-1' => Http::response([
|
||||
'instance' => [
|
||||
'id' => 'instance-1',
|
||||
'status' => 'active',
|
||||
'power_status' => 'stopped',
|
||||
'main_ip' => '1.2.3.4',
|
||||
],
|
||||
], 200),
|
||||
]);
|
||||
|
||||
Livewire::test(ValidateAndInstall::class, ['server' => $this->server])
|
||||
->call('validateConnection')
|
||||
->assertSet('error', 'Vultr instance is stopped. Power it on before validating.')
|
||||
->assertNotDispatched('validateOS');
|
||||
|
||||
$this->assertDatabaseHas('servers', [
|
||||
'id' => $this->server->id,
|
||||
'vultr_instance_status' => 'stopped',
|
||||
'validation_logs' => 'Vultr instance is stopped. Power it on before validating.',
|
||||
]);
|
||||
});
|
||||
|
||||
it('blocks action validation when Vultr instance is stopped', function () {
|
||||
Http::fake([
|
||||
'https://api.vultr.com/v2/instances/instance-1' => Http::response([
|
||||
'instance' => [
|
||||
'id' => 'instance-1',
|
||||
'status' => 'active',
|
||||
'power_status' => 'stopped',
|
||||
'main_ip' => '1.2.3.4',
|
||||
],
|
||||
], 200),
|
||||
]);
|
||||
|
||||
expect(fn () => ValidateServerAction::run($this->server))
|
||||
->toThrow(Exception::class, 'Vultr instance is stopped. Power it on before validating.');
|
||||
|
||||
$this->assertDatabaseHas('servers', [
|
||||
'id' => $this->server->id,
|
||||
'vultr_instance_status' => 'stopped',
|
||||
'validation_logs' => 'Vultr instance is stopped. Power it on before validating.',
|
||||
]);
|
||||
});
|
||||
|
||||
it('starts a Vultr instance', function () {
|
||||
Http::fake([
|
||||
'https://api.vultr.com/v2/instances/instance-1/start' => Http::response([], 204),
|
||||
]);
|
||||
|
||||
Livewire::test(Show::class, ['server_uuid' => $this->server->uuid])
|
||||
->call('startVultrInstance')
|
||||
->assertSet('vultrInstanceStatus', 'starting');
|
||||
|
||||
$this->assertDatabaseHas('servers', [
|
||||
'id' => $this->server->id,
|
||||
'vultr_instance_status' => 'starting',
|
||||
]);
|
||||
|
||||
Http::assertSent(fn ($request) => $request->url() === 'https://api.vultr.com/v2/instances/instance-1/start');
|
||||
});
|
||||
|
||||
it('links a server to Vultr by matching IP', function () {
|
||||
$unlinkedServer = Server::factory()->create([
|
||||
'team_id' => $this->team->id,
|
||||
'private_key_id' => $this->privateKey->id,
|
||||
'ip' => '5.6.7.8',
|
||||
]);
|
||||
|
||||
Http::fake([
|
||||
'https://api.vultr.com/v2/instances?per_page=100' => Http::response([
|
||||
'instances' => [
|
||||
[
|
||||
'id' => 'instance-2',
|
||||
'label' => 'matched-server',
|
||||
'main_ip' => '5.6.7.8',
|
||||
'status' => 'active',
|
||||
'plan' => 'vc2-1c-1gb',
|
||||
],
|
||||
],
|
||||
'meta' => ['links' => ['next' => null]],
|
||||
], 200),
|
||||
'https://api.vultr.com/v2/instances/instance-2' => Http::response([
|
||||
'instance' => [
|
||||
'id' => 'instance-2',
|
||||
'status' => 'active',
|
||||
],
|
||||
], 200),
|
||||
]);
|
||||
|
||||
Livewire::test(Show::class, ['server_uuid' => $unlinkedServer->uuid])
|
||||
->set('selectedVultrTokenId', $this->vultrToken->id)
|
||||
->call('searchVultrInstance')
|
||||
->assertSet('matchedVultrInstance.id', 'instance-2')
|
||||
->call('linkToVultr');
|
||||
|
||||
$this->assertDatabaseHas('servers', [
|
||||
'id' => $unlinkedServer->id,
|
||||
'cloud_provider_token_id' => $this->vultrToken->id,
|
||||
'vultr_instance_id' => 'instance-2',
|
||||
'vultr_instance_status' => 'active',
|
||||
]);
|
||||
});
|
||||
@@ -127,6 +127,37 @@ describe('shouldSkipDueToBackoff', function () {
|
||||
});
|
||||
|
||||
describe('ServerConnectionCheckJob unreachable_count', function () {
|
||||
it('marks Vultr servers unreachable when provider status is unavailable', function () {
|
||||
Event::fake([ServerReachabilityChanged::class]);
|
||||
|
||||
$settings = Mockery::mock();
|
||||
$settings->is_reachable = true;
|
||||
$settings->force_disabled = false;
|
||||
$settings->shouldReceive('update')
|
||||
->with(['is_reachable' => false, 'is_usable' => false])
|
||||
->once();
|
||||
|
||||
$server = Mockery::mock(Server::class)->makePartial()->shouldAllowMockingProtectedMethods();
|
||||
$server->shouldReceive('getAttribute')->andReturnUsing(fn (string $key) => match ($key) {
|
||||
'settings' => $settings,
|
||||
'unreachable_notification_sent' => false,
|
||||
'vultr_instance_id' => 'instance-1',
|
||||
'cloudProviderToken' => (object) ['token' => 'test-token'],
|
||||
'id' => 1,
|
||||
'name' => 'test-server',
|
||||
'unreachable_count' => 1,
|
||||
default => null,
|
||||
});
|
||||
$server->shouldReceive('refreshVultrState')->once()->andReturn('stopped');
|
||||
$server->shouldReceive('increment')->with('unreachable_count')->once();
|
||||
$server->id = 1;
|
||||
$server->name = 'test-server';
|
||||
$server->uuid = 'server-uuid';
|
||||
|
||||
$job = new ServerConnectionCheckJob($server);
|
||||
$job->handle();
|
||||
});
|
||||
|
||||
it('increments unreachable_count on timeout', function () {
|
||||
Event::fake([ServerReachabilityChanged::class]);
|
||||
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
use App\Actions\Server\DeleteServer;
|
||||
use App\Models\CloudProviderToken;
|
||||
use App\Models\InstanceSettings;
|
||||
use App\Models\PrivateKey;
|
||||
use App\Models\Server;
|
||||
use App\Models\Team;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Tests\TestCase;
|
||||
|
||||
uses(TestCase::class, RefreshDatabase::class);
|
||||
|
||||
beforeEach(function () {
|
||||
config([
|
||||
'cache.default' => 'array',
|
||||
'session.driver' => 'array',
|
||||
]);
|
||||
|
||||
InstanceSettings::unguarded(fn () => InstanceSettings::query()->create([
|
||||
'id' => 0,
|
||||
'is_api_enabled' => true,
|
||||
]));
|
||||
|
||||
$this->team = Team::factory()->create();
|
||||
session(['currentTeam' => $this->team]);
|
||||
|
||||
$this->privateKey = PrivateKey::create([
|
||||
'team_id' => $this->team->id,
|
||||
'name' => 'Test Private Key',
|
||||
'description' => 'Test private key',
|
||||
'private_key' => vultrDeleteTestPrivateKey(),
|
||||
]);
|
||||
|
||||
$this->vultrToken = CloudProviderToken::create([
|
||||
'team_id' => $this->team->id,
|
||||
'provider' => 'vultr',
|
||||
'token' => 'test-vultr-token',
|
||||
'name' => 'Test Vultr Token',
|
||||
]);
|
||||
});
|
||||
|
||||
function vultrDeleteTestPrivateKey(): string
|
||||
{
|
||||
return <<<'KEY'
|
||||
-----BEGIN OPENSSH PRIVATE KEY-----
|
||||
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
|
||||
QyNTUxOQAAACBbhpqHhqv6aI67Mj9abM3DVbmcfYhZAhC7ca4d9UCevAAAAJi/QySHv0Mk
|
||||
hwAAAAtzc2gtZWQyNTUxOQAAACBbhpqHhqv6aI67Mj9abM3DVbmcfYhZAhC7ca4d9UCevA
|
||||
AAAECBQw4jg1WRT2IGHMncCiZhURCts2s24HoDS0thHnnRKVuGmoeGq/pojrsyP1pszcNV
|
||||
uZx9iFkCELtxrh31QJ68AAAAEXNhaWxANzZmZjY2ZDJlMmRkAQIDBA==
|
||||
-----END OPENSSH PRIVATE KEY-----
|
||||
KEY;
|
||||
}
|
||||
|
||||
it('deletes a Vultr instance when requested', function () {
|
||||
Http::fake([
|
||||
'https://api.vultr.com/v2/instances/instance-1' => Http::response([], 204),
|
||||
]);
|
||||
|
||||
$server = Server::factory()->create([
|
||||
'team_id' => $this->team->id,
|
||||
'private_key_id' => $this->privateKey->id,
|
||||
'cloud_provider_token_id' => $this->vultrToken->id,
|
||||
'vultr_instance_id' => 'instance-1',
|
||||
]);
|
||||
|
||||
$server->delete();
|
||||
|
||||
DeleteServer::run(
|
||||
serverId: $server->id,
|
||||
cloudProviderTokenId: $this->vultrToken->id,
|
||||
teamId: $this->team->id,
|
||||
deleteFromVultr: true,
|
||||
vultrInstanceId: 'instance-1'
|
||||
);
|
||||
|
||||
Http::assertSent(fn ($request) => $request->method() === 'DELETE'
|
||||
&& $request->url() === 'https://api.vultr.com/v2/instances/instance-1');
|
||||
|
||||
expect(Server::withTrashed()->find($server->id))->toBeNull();
|
||||
});
|
||||
@@ -0,0 +1,138 @@
|
||||
<?php
|
||||
|
||||
use App\Services\VultrService;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Tests\TestCase;
|
||||
|
||||
uses(TestCase::class);
|
||||
|
||||
beforeEach(function () {
|
||||
Http::preventStrayRequests();
|
||||
});
|
||||
|
||||
it('gets instances from Vultr API', function () {
|
||||
Http::fake([
|
||||
'https://api.vultr.com/v2/instances*' => Http::response([
|
||||
'instances' => [
|
||||
[
|
||||
'id' => 'instance-1',
|
||||
'label' => 'test-server-1',
|
||||
'status' => 'active',
|
||||
'main_ip' => '123.45.67.89',
|
||||
'v6_main_ip' => '2001:db8::1',
|
||||
],
|
||||
[
|
||||
'id' => 'instance-2',
|
||||
'label' => 'test-server-2',
|
||||
'status' => 'stopped',
|
||||
'main_ip' => '98.76.54.32',
|
||||
'v6_main_ip' => '2001:db8::2',
|
||||
],
|
||||
],
|
||||
'meta' => ['links' => ['next' => null]],
|
||||
], 200),
|
||||
]);
|
||||
|
||||
$service = new VultrService('fake-token');
|
||||
$instances = $service->getInstances();
|
||||
|
||||
expect($instances)->toBeArray()
|
||||
->and($instances)->toHaveCount(2)
|
||||
->and($instances[0]['id'])->toBe('instance-1')
|
||||
->and($instances[1]['id'])->toBe('instance-2');
|
||||
});
|
||||
|
||||
it('follows cursor pagination', function () {
|
||||
Http::fake([
|
||||
'https://api.vultr.com/v2/regions?per_page=100' => Http::response([
|
||||
'regions' => [
|
||||
['id' => 'ewr', 'city' => 'New Jersey'],
|
||||
],
|
||||
'meta' => [
|
||||
'links' => [
|
||||
'next' => 'https://api.vultr.com/v2/regions?cursor=next-cursor',
|
||||
],
|
||||
],
|
||||
], 200),
|
||||
'https://api.vultr.com/v2/regions?per_page=100&cursor=next-cursor' => Http::response([
|
||||
'regions' => [
|
||||
['id' => 'ams', 'city' => 'Amsterdam'],
|
||||
],
|
||||
'meta' => ['links' => ['next' => null]],
|
||||
], 200),
|
||||
]);
|
||||
|
||||
$service = new VultrService('fake-token');
|
||||
$regions = $service->getRegions();
|
||||
|
||||
expect($regions)->toHaveCount(2)
|
||||
->and($regions[0]['id'])->toBe('ewr')
|
||||
->and($regions[1]['id'])->toBe('ams');
|
||||
});
|
||||
|
||||
it('base64 encodes user data when creating an instance', function () {
|
||||
Http::fake([
|
||||
'https://api.vultr.com/v2/instances' => Http::response([
|
||||
'instance' => [
|
||||
'id' => 'instance-1',
|
||||
'main_ip' => '123.45.67.89',
|
||||
'status' => 'pending',
|
||||
],
|
||||
], 202),
|
||||
]);
|
||||
|
||||
$service = new VultrService('fake-token');
|
||||
$service->createInstance([
|
||||
'region' => 'ewr',
|
||||
'plan' => 'vc2-1c-1gb',
|
||||
'os_id' => 2284,
|
||||
'user_data' => "#cloud-config\npackages:\n - curl",
|
||||
]);
|
||||
|
||||
Http::assertSent(function ($request) {
|
||||
return $request->url() === 'https://api.vultr.com/v2/instances'
|
||||
&& $request['user_data'] === base64_encode("#cloud-config\npackages:\n - curl");
|
||||
});
|
||||
});
|
||||
|
||||
it('waits for Vultr to replace placeholder public IP', function () {
|
||||
Http::fake([
|
||||
'https://api.vultr.com/v2/instances/instance-1' => Http::response([
|
||||
'instance' => [
|
||||
'id' => 'instance-1',
|
||||
'main_ip' => '123.45.67.89',
|
||||
'status' => 'active',
|
||||
],
|
||||
], 200),
|
||||
]);
|
||||
|
||||
$service = new VultrService('fake-token');
|
||||
$instance = $service->waitForPublicIp([
|
||||
'id' => 'instance-1',
|
||||
'main_ip' => '0.0.0.0',
|
||||
'status' => 'pending',
|
||||
], sleepMilliseconds: 0);
|
||||
|
||||
expect($service->getPublicIp($instance))->toBe('123.45.67.89');
|
||||
});
|
||||
|
||||
it('finds an instance by IPv4 or IPv6', function () {
|
||||
Http::fake([
|
||||
'https://api.vultr.com/v2/instances*' => Http::response([
|
||||
'instances' => [
|
||||
[
|
||||
'id' => 'instance-1',
|
||||
'main_ip' => '123.45.67.89',
|
||||
'v6_main_ip' => '2001:db8::1',
|
||||
],
|
||||
],
|
||||
'meta' => ['links' => ['next' => null]],
|
||||
], 200),
|
||||
]);
|
||||
|
||||
$service = new VultrService('fake-token');
|
||||
|
||||
expect($service->findInstanceByIp('123.45.67.89')['id'])->toBe('instance-1')
|
||||
->and($service->findInstanceByIp('2001:db8::1')['id'])->toBe('instance-1')
|
||||
->and($service->findInstanceByIp('1.2.3.4'))->toBeNull();
|
||||
});
|
||||
Reference in new issue
Block a user