Don't count not created machines as declined when destroying

This commit is contained in:
sophia 2021-02-11 10:46:48 -06:00
parent 81b7d1524b
commit a2fdbc5625
2 changed files with 74 additions and 6 deletions

View File

@ -56,7 +56,7 @@ module VagrantPlugins
end end
machines.each do |m| machines.each do |m|
if m.state.id == init_states[m.name] if init_states[m.name] != :not_created && m.state.id == init_states[m.name]
declined += 1 declined += 1
end end
end end
@ -64,10 +64,6 @@ module VagrantPlugins
# Nothing was declined # Nothing was declined
return 0 if declined == 0 return 0 if declined == 0
# Everything was declined, and all states are `not_created`
return 0 if declined == machines.length &&
declined == init_states.values.count(:not_created)
# Everything was declined, state was not changed # Everything was declined, state was not changed
return 1 if declined == machines.length return 1 if declined == machines.length

View File

@ -7,7 +7,7 @@ describe VagrantPlugins::CommandDestroy::Command do
let(:entry_klass) { Vagrant::MachineIndex::Entry } let(:entry_klass) { Vagrant::MachineIndex::Entry }
let(:argv) { [] } let(:argv) { [] }
let(:vagrantfile_content){ "" } let(:vagrantfile_content) { "" }
let(:iso_env) do let(:iso_env) do
env = isolated_environment env = isolated_environment
env.vagrantfile(vagrantfile_content) env.vagrantfile(vagrantfile_content)
@ -36,6 +36,7 @@ describe VagrantPlugins::CommandDestroy::Command do
let(:machine) do let(:machine) do
iso_env.machine(iso_env.machine_names[0], :dummy).tap do |m| iso_env.machine(iso_env.machine_names[0], :dummy).tap do |m|
allow(m).to receive(:state).and_return(state) allow(m).to receive(:state).and_return(state)
allow(m).to receive(:name).and_return("default")
end end
end end
@ -67,6 +68,77 @@ describe VagrantPlugins::CommandDestroy::Command do
expect(subject.execute).to eq(1) expect(subject.execute).to eq(1)
end end
context "with multiple machines" do
let(:vagrantfile_content) do
<<-VF
Vagrant.configure("2") do |config|
config.vm.box = "base"
config.vm.define :machine1
config.vm.define :machine2
end
VF
end
let(:state2) { "" }
let(:machine2) do
iso_env.machine(iso_env.machine_names[1], :dummy).tap do |m|
allow(m).to receive(:state).and_return(state2)
allow(m).to receive(:name).and_return("not_default")
end
end
before do
allow(subject).to receive(:with_target_vms).and_yield(machine).and_yield(machine2)
allow_any_instance_of(Vagrant::BatchAction).to receive(:action) .with(machine2, :destroy, anything)
allow_any_instance_of(Vagrant::BatchAction).to receive(:action) .with(machine, :destroy, anything)
end
context "second machine is not created" do
let(:state2) { double("state", id: :not_created) }
let(:state) { double("state", id: :not_created) }
it "exits 0 if vms are successfully destroyed" do
expect(machine.state).to receive(:id).and_return(:running)
expect(machine.state).to receive(:id).and_return(:dead)
expect(machine2.state).to receive(:id).and_return(:not_created)
expect(subject.execute).to eq(0)
end
it "exits 0 if vms are not created" do
expect(machine.state).to receive(:id).and_return(:not_created)
expect(machine2.state).to receive(:id).and_return(:not_created)
expect(subject.execute).to eq(0)
end
end
context "second machine is running" do
let(:state2) { double("state", id: :running) }
it "exits 0 if vms are not successfully destroyed" do
expect(machine.state).to receive(:id).and_return(:running)
expect(machine.state).to receive(:id).and_return(:dead)
expect(machine2.state).to receive(:id).and_return(:running)
expect(machine2.state).to receive(:id).and_return(:dead)
expect(subject.execute).to eq(0)
end
it "exits 1 if vms are not successfully destroyed" do
expect(machine.state).to receive(:id).and_return(:running)
expect(machine2.state).to receive(:id).and_return(:running)
expect(subject.execute).to eq(1)
end
it "exits 2 if some vms are not successfully destroyed" do
expect(machine.state).to receive(:id).and_return(:running)
expect(machine.state).to receive(:id).and_return(:dead)
expect(machine2.state).to receive(:id).and_return(:running)
expect(subject.execute).to eq(2)
end
end
end
context "with VAGRANT_DEFAULT_PROVIDER set" do context "with VAGRANT_DEFAULT_PROVIDER set" do
before do before do
if ENV["VAGRANT_DEFAULT_PROVIDER"] if ENV["VAGRANT_DEFAULT_PROVIDER"]