From a2fdbc56256aecd6d6d06b825ef3080bcf781575 Mon Sep 17 00:00:00 2001 From: sophia Date: Thu, 11 Feb 2021 10:46:48 -0600 Subject: [PATCH] Don't count not created machines as declined when destroying --- plugins/commands/destroy/command.rb | 6 +- .../plugins/commands/destroy/command_test.rb | 74 ++++++++++++++++++- 2 files changed, 74 insertions(+), 6 deletions(-) diff --git a/plugins/commands/destroy/command.rb b/plugins/commands/destroy/command.rb index f0fc7a961..b3b472a55 100644 --- a/plugins/commands/destroy/command.rb +++ b/plugins/commands/destroy/command.rb @@ -56,7 +56,7 @@ module VagrantPlugins end 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 end end @@ -64,10 +64,6 @@ module VagrantPlugins # Nothing was declined 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 return 1 if declined == machines.length diff --git a/test/unit/plugins/commands/destroy/command_test.rb b/test/unit/plugins/commands/destroy/command_test.rb index 782455d17..a2e801c48 100644 --- a/test/unit/plugins/commands/destroy/command_test.rb +++ b/test/unit/plugins/commands/destroy/command_test.rb @@ -7,7 +7,7 @@ describe VagrantPlugins::CommandDestroy::Command do let(:entry_klass) { Vagrant::MachineIndex::Entry } let(:argv) { [] } - let(:vagrantfile_content){ "" } + let(:vagrantfile_content) { "" } let(:iso_env) do env = isolated_environment env.vagrantfile(vagrantfile_content) @@ -36,6 +36,7 @@ describe VagrantPlugins::CommandDestroy::Command do let(:machine) do iso_env.machine(iso_env.machine_names[0], :dummy).tap do |m| allow(m).to receive(:state).and_return(state) + allow(m).to receive(:name).and_return("default") end end @@ -67,6 +68,77 @@ describe VagrantPlugins::CommandDestroy::Command do expect(subject.execute).to eq(1) 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 before do if ENV["VAGRANT_DEFAULT_PROVIDER"]