71 lines
2.6 KiB
PHP
71 lines
2.6 KiB
PHP
<?php
|
|
|
|
use App\Actions\BookAddon;
|
|
use App\Models\Host;
|
|
use App\Models\Instance;
|
|
use App\Models\Order;
|
|
use App\Models\Subscription;
|
|
use App\Services\Billing\AddonCatalogue;
|
|
|
|
/**
|
|
* Ein Block ist Platz auf einer bestimmten Maschine.
|
|
*
|
|
* Eine laufende Instanz zieht nicht um — es gibt keinen Umzug zwischen Hosts.
|
|
* Die Frage ist deshalb nicht "hat irgendein Host Platz", sondern "hat DIESER
|
|
* Host Platz", und sie muss gestellt werden, bevor Geld fließt.
|
|
*/
|
|
function packedInstanceOn(Host $host): Subscription
|
|
{
|
|
$order = Order::factory()->create(['plan' => 'start', 'datacenter' => $host->datacenter, 'status' => 'paid']);
|
|
$subscription = app(App\Actions\OpenSubscription::class)($order);
|
|
|
|
$instance = Instance::factory()->create([
|
|
'customer_id' => $subscription->customer_id,
|
|
'host_id' => $host->id,
|
|
'status' => 'active',
|
|
'quota_gb' => 30,
|
|
'disk_gb' => 40,
|
|
]);
|
|
|
|
$subscription->update(['instance_id' => $instance->id]);
|
|
|
|
return $subscription->refresh();
|
|
}
|
|
|
|
it('lehnt einen Block ab, für den der Host keinen Platz hat', function () {
|
|
config(['provisioning.storage_addon.gb' => 20, 'provisioning.storage_addon.disk_gb' => 22]);
|
|
|
|
// 60 GB gesamt, davon 40 durch die Instanz gebunden: für 22 weitere ist
|
|
// kein Platz. `active()` setzt total_gb/RAM/Kerne — ohne diesen Zustand
|
|
// hat ein Host gar keine Größe.
|
|
$host = Host::factory()->active()->create(['datacenter' => 'fsn', 'total_gb' => 60]);
|
|
$subscription = packedInstanceOn($host);
|
|
|
|
expect(fn () => app(BookAddon::class)($subscription, AddonCatalogue::STORAGE, 1))
|
|
->toThrow(RuntimeException::class);
|
|
|
|
expect($subscription->addons()->count())->toBe(0);
|
|
});
|
|
|
|
it('lässt einen Block durch, für den der Host Platz hat', function () {
|
|
config(['provisioning.storage_addon.gb' => 20, 'provisioning.storage_addon.disk_gb' => 22]);
|
|
|
|
$host = Host::factory()->active()->create(['datacenter' => 'fsn', 'total_gb' => 500]);
|
|
$subscription = packedInstanceOn($host);
|
|
|
|
app(BookAddon::class)($subscription, AddonCatalogue::STORAGE, 1);
|
|
|
|
expect((int) $subscription->addons()->active()->sum('quantity'))->toBe(1);
|
|
});
|
|
|
|
it('fragt nicht, solange es keine Maschine gibt', function () {
|
|
config(['provisioning.storage_addon.gb' => 20, 'provisioning.storage_addon.disk_gb' => 22]);
|
|
|
|
$order = Order::factory()->create(['plan' => 'start', 'datacenter' => 'fsn', 'status' => 'paid']);
|
|
$subscription = app(App\Actions\OpenSubscription::class)($order);
|
|
|
|
app(BookAddon::class)($subscription, AddonCatalogue::STORAGE, 1);
|
|
|
|
expect((int) $subscription->addons()->active()->sum('quantity'))->toBe(1);
|
|
});
|