2021-06-30 13:27:03 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2016-12-06 23:20:29 +01:00
|
|
|
require 'spec_helper'
|
|
|
|
|
require 'support/sharedcontext'
|
|
|
|
|
require 'support/libvirt_context'
|
2016-04-27 16:27:39 +01:00
|
|
|
|
2016-12-06 23:20:29 +01:00
|
|
|
require 'vagrant-libvirt/action/destroy_domain'
|
2016-04-27 16:27:39 +01:00
|
|
|
|
|
|
|
|
describe VagrantPlugins::ProviderLibvirt::Action::DestroyDomain do
|
|
|
|
|
subject { described_class.new(app, env) }
|
|
|
|
|
|
2016-12-06 23:20:29 +01:00
|
|
|
include_context 'unit'
|
|
|
|
|
include_context 'libvirt'
|
2016-04-27 16:27:39 +01:00
|
|
|
|
2021-09-30 13:35:30 +01:00
|
|
|
let(:driver) { double('driver') }
|
2016-12-06 23:20:29 +01:00
|
|
|
let(:libvirt_domain) { double('libvirt_domain') }
|
|
|
|
|
let(:libvirt_client) { double('libvirt_client') }
|
|
|
|
|
let(:servers) { double('servers') }
|
2016-04-27 16:27:39 +01:00
|
|
|
|
2021-09-30 13:35:30 +01:00
|
|
|
before do
|
|
|
|
|
allow(machine.provider).to receive('driver').and_return(driver)
|
|
|
|
|
allow(driver).to receive(:connection).and_return(connection)
|
|
|
|
|
end
|
|
|
|
|
|
2016-12-06 23:20:29 +01:00
|
|
|
describe '#call' do
|
2016-04-27 16:27:39 +01:00
|
|
|
before do
|
|
|
|
|
allow(connection).to receive(:client).and_return(libvirt_client)
|
2016-12-06 23:20:29 +01:00
|
|
|
allow(libvirt_client).to receive(:lookup_domain_by_uuid)
|
|
|
|
|
.and_return(libvirt_domain)
|
2016-04-27 16:27:39 +01:00
|
|
|
allow(connection).to receive(:servers).and_return(servers)
|
|
|
|
|
allow(servers).to receive(:get).and_return(domain)
|
|
|
|
|
# always see this at the start of #call
|
2016-12-06 23:20:29 +01:00
|
|
|
expect(ui).to receive(:info).with('Removing domain...')
|
2016-04-27 16:27:39 +01:00
|
|
|
end
|
|
|
|
|
|
2016-12-06 23:20:29 +01:00
|
|
|
context 'when no snapshots' do
|
|
|
|
|
let(:root_disk) { double('libvirt_root_disk') }
|
2016-04-27 16:27:39 +01:00
|
|
|
|
|
|
|
|
before do
|
|
|
|
|
allow(libvirt_domain).to receive(:list_snapshots).and_return([])
|
|
|
|
|
allow(libvirt_domain).to receive(:has_managed_save?).and_return(nil)
|
2020-08-16 16:06:05 +01:00
|
|
|
allow(root_disk).to receive(:name).and_return('test.img')
|
2016-04-27 16:27:39 +01:00
|
|
|
end
|
|
|
|
|
|
2016-12-06 23:20:29 +01:00
|
|
|
context 'when only has root disk' do
|
|
|
|
|
it 'calls fog to destroy volumes' do
|
|
|
|
|
expect(domain).to receive(:destroy).with(destroy_volumes: true)
|
2016-04-27 16:27:39 +01:00
|
|
|
expect(subject.call(env)).to be_nil
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2016-12-06 23:20:29 +01:00
|
|
|
context 'when has additional disks' do
|
|
|
|
|
let(:vagrantfile) do
|
|
|
|
|
<<-EOF
|
2016-04-27 16:27:39 +01:00
|
|
|
Vagrant.configure('2') do |config|
|
|
|
|
|
config.vm.define :test
|
|
|
|
|
config.vm.provider :libvirt do |libvirt|
|
|
|
|
|
libvirt.storage :file
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
EOF
|
2016-12-06 23:20:29 +01:00
|
|
|
end
|
2016-04-27 16:27:39 +01:00
|
|
|
|
2016-12-06 23:20:29 +01:00
|
|
|
let(:extra_disk) { double('libvirt_extra_disk') }
|
2016-04-27 16:27:39 +01:00
|
|
|
before do
|
2020-08-16 16:06:05 +01:00
|
|
|
allow(extra_disk).to receive(:name).and_return('test-vdb.qcow2')
|
2016-04-27 16:27:39 +01:00
|
|
|
end
|
|
|
|
|
|
2016-12-06 23:20:29 +01:00
|
|
|
it 'destroys disks individually' do
|
|
|
|
|
allow(libvirt_domain).to receive(:name).and_return('test')
|
2016-04-27 16:27:39 +01:00
|
|
|
allow(domain).to receive(:volumes).and_return([extra_disk], [root_disk])
|
|
|
|
|
|
2016-12-06 23:20:29 +01:00
|
|
|
expect(domain).to receive(:destroy).with(destroy_volumes: false)
|
|
|
|
|
expect(extra_disk).to receive(:destroy) # extra disk remove
|
2016-04-27 16:27:39 +01:00
|
|
|
expect(root_disk).to receive(:destroy) # root disk remove
|
|
|
|
|
expect(subject.call(env)).to be_nil
|
|
|
|
|
end
|
|
|
|
|
end
|
2015-08-13 16:03:59 +01:00
|
|
|
|
2016-12-06 23:20:29 +01:00
|
|
|
context 'when has CDROMs attached' do
|
|
|
|
|
let(:vagrantfile) do
|
|
|
|
|
<<-EOF
|
2015-08-13 16:03:59 +01:00
|
|
|
Vagrant.configure('2') do |config|
|
|
|
|
|
config.vm.define :test
|
|
|
|
|
config.vm.provider :libvirt do |libvirt|
|
|
|
|
|
libvirt.storage :file, :device => :cdrom
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
EOF
|
2016-12-06 23:20:29 +01:00
|
|
|
end
|
2015-08-13 16:03:59 +01:00
|
|
|
|
2016-12-06 23:20:29 +01:00
|
|
|
it 'uses explicit removal of disks' do
|
|
|
|
|
allow(libvirt_domain).to receive(:name).and_return('test')
|
2021-05-08 17:27:04 +01:00
|
|
|
allow(domain).to receive(:volumes).and_return([root_disk, nil])
|
2015-08-13 16:03:59 +01:00
|
|
|
|
2016-12-06 23:20:29 +01:00
|
|
|
expect(domain).to_not receive(:destroy).with(destroy_volumes: true)
|
2015-08-13 16:03:59 +01:00
|
|
|
expect(root_disk).to receive(:destroy) # root disk remove
|
|
|
|
|
expect(subject.call(env)).to be_nil
|
|
|
|
|
end
|
|
|
|
|
end
|
2016-04-27 16:27:39 +01:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|