Files
ubicloud/spec/prog/remove_boot_image_spec.rb
Enes Cakir e1286b290a Add create_vm_host spec helper
The VmHost model requires a Sshable record due to database constraints.
Currently, we're creating VmHost models with dummy Sshable data across
multiple test locations, leading to code duplication.

Additionally, we're creating VmHost instances using `VmHost.create(...)
{ _1.id = Sshable.create.id }`, which results in VmHost having a Sshable
UBID instead of its own UBID. While this doesn't break any test, it's
conceptually incorrect and should be fixed.
2025-02-03 08:34:22 +03:00

58 lines
1.9 KiB
Ruby

# frozen_string_literal: true
require_relative "../model/spec_helper"
RSpec.describe Prog::RemoveBootImage do
subject(:rbi) { described_class.new(Strand.new(stack: [{}])) }
let(:sshable) { vm_host.sshable }
let(:vm_host) { create_vm_host }
let(:boot_image) { BootImage.create_with_id(name: "ubuntu-jammy", version: "20220202", vm_host_id: vm_host.id, size_gib: 14) }
before do
allow(rbi).to receive(:boot_image).and_return(boot_image)
allow(boot_image).to receive(:vm_host).and_return(vm_host)
end
describe "#start" do
it "deactivates and hops to wait_volumes" do
expect { rbi.start }.to hop("wait_volumes")
expect(boot_image.reload.activated_at).to be_nil
end
end
describe "#wait_volumes" do
it "hops to remove_volumes if all volumes are removed" do
expect { rbi.wait_volumes }.to hop("remove")
end
it "waits for volumes to be removed" do
expect(boot_image).to receive(:vm_storage_volumes).and_return([1])
expect { rbi.wait_volumes }.to nap(30)
end
end
describe "#remove" do
it "removes image and pops" do
expect(sshable).to receive(:cmd).with("sudo rm -rf /var/storage/images/ubuntu-jammy-20220202.raw")
expect { rbi.remove }.to hop("update_database")
end
it "can remove unversioned image" do
boot_image.update(version: nil)
expect(sshable).to receive(:cmd).with("sudo rm -rf /var/storage/images/ubuntu-jammy.raw")
expect { rbi.remove }.to hop("update_database")
end
end
describe "#update_database" do
it "updates storage and destroys boot image" do
boot_image.id
storage_device = StorageDevice.create_with_id(vm_host_id: vm_host.id, name: "DEFAULT", available_storage_gib: 100, total_storage_gib: 200)
expect { rbi.update_database }.to exit({"msg" => "Boot image was removed."})
expect(BootImage[boot_image.id]).to be_nil
expect(storage_device.reload.available_storage_gib).to eq(114)
end
end
end