CluPilotCloud/tests/Feature/Console/ConfigureInstanceMailComman...

80 lines
2.8 KiB
PHP

<?php
use App\Models\Host;
use App\Models\Instance;
use App\Models\ProvisioningRun;
use Illuminate\Support\Facades\Queue;
// QUEUE_CONNECTION ist in dieser Suite `sync`: ohne das hier faehrt jedes
// `AdvanceRunJob::dispatch()` den Lauf sofort im Test, gegen eine echte,
// unerreichbare Adresse. Die Laeufe, die dieser Test wirklich fahren will,
// stoesst er unten selbst an.
beforeEach(fn () => Queue::fake());
/**
* Eine aktive Instanz auf einem Host mit vmid.
*
* Der Befehl ueberspringt Instanzen ohne Host oder ohne vmid. Ein Test, der
* die Mailversand-Nachruestung fahren will, braucht beide.
*/
function aktiveInstanzMitHost(array $attributes = []): Instance
{
$host = Host::factory()->active()->create(['datacenter' => 'fsn', 'node' => 'pve']);
return Instance::factory()->create(array_merge([
'host_id' => $host->id,
'vmid' => 101,
'status' => 'active',
], $attributes));
}
it('startet je aktiver Instanz einen Lauf', function () {
aktiveInstanzMitHost();
aktiveInstanzMitHost(['vmid' => 102]);
Instance::factory()->create(['status' => 'closed']);
$this->artisan('clupilot:configure-instance-mail')->assertSuccessful();
expect(ProvisioningRun::where('pipeline', 'instance-mail')->count())->toBe(2);
});
it('faengt unter --dry-run gar nichts an', function () {
aktiveInstanzMitHost();
aktiveInstanzMitHost(['vmid' => 102]);
aktiveInstanzMitHost(['vmid' => 103]);
$this->artisan('clupilot:configure-instance-mail', ['--dry-run' => true])->assertSuccessful();
expect(ProvisioningRun::where('pipeline', 'instance-mail')->count())->toBe(0);
});
it('nimmt mit --instance genau eine', function () {
$eine = aktiveInstanzMitHost();
aktiveInstanzMitHost(['vmid' => 102]);
$this->artisan('clupilot:configure-instance-mail', ['--instance' => $eine->uuid])->assertSuccessful();
expect(ProvisioningRun::where('pipeline', 'instance-mail')->count())->toBe(1);
});
it('ueberspringt eine Instanz ohne Host und sagt es', function () {
Instance::factory()->create(['status' => 'active', 'host_id' => null, 'vmid' => 101]);
$this->artisan('clupilot:configure-instance-mail')
->expectsOutputToContain('kein Host')
->assertSuccessful();
expect(ProvisioningRun::where('pipeline', 'instance-mail')->count())->toBe(0);
});
it('ueberspringt eine Instanz ohne vmid und sagt es', function () {
$host = Host::factory()->active()->create(['datacenter' => 'fsn', 'node' => 'pve']);
Instance::factory()->create(['status' => 'active', 'host_id' => $host->id, 'vmid' => null]);
$this->artisan('clupilot:configure-instance-mail')
->expectsOutputToContain('keine VMID')
->assertSuccessful();
expect(ProvisioningRun::where('pipeline', 'instance-mail')->count())->toBe(0);
});