diff --git a/spec/unit/action/halt_domain_spec.rb b/spec/unit/action/halt_domain_spec.rb index e229003..489e6cb 100644 --- a/spec/unit/action/halt_domain_spec.rb +++ b/spec/unit/action/halt_domain_spec.rb @@ -20,72 +20,39 @@ describe VagrantPlugins::ProviderLibvirt::Action::HaltDomain do .to receive(:connection).and_return(connection) allow(connection).to receive(:servers).and_return(servers) allow(servers).to receive(:get).and_return(domain) - # always see this at the start of #call - expect(ui).to receive(:info).with('Halting domain...') + allow(ui).to receive(:info).with('Halting domain...') end - context 'with graceful timeout' do - it "should shutdown" do - expect(guest).to receive(:capability).with(:halt).and_return(true) - expect(domain).to receive(:wait_for).with(60).and_return(false) - expect(subject.call(env)).to be_nil + context "when state is not running" do + before { expect(domain).to receive(:state).at_least(1). + and_return('not_created') } + + it "should not poweroff when state is not running" do + expect(domain).not_to receive(:poweroff) + subject.call(env) end - context 'when halt fails' do - before do - expect(logger).to receive(:info).with('Trying Libvirt graceful shutdown.') - expect(guest).to receive(:capability).with(:halt).and_raise(IOError) - expect(domain).to receive(:state).and_return('running') - end - - it "should call shutdown" do - expect(domain).to receive(:shutdown) - expect(domain).to receive(:wait_for).with(60).and_return(false) - expect(subject.call(env)).to be_nil - end - - context 'when shutdown fails' do - it "should call power off" do - expect(logger).to receive(:error).with('Failed to shutdown cleanly. Calling force poweroff.') - expect(domain).to receive(:shutdown).and_raise(IOError) - expect(domain).to receive(:poweroff) - expect(subject.call(env)).to be_nil - end - end - - context 'when shutdown exceeds the timeout' do - it "should call poweroff" do - expect(logger).to receive(:info).with('VM is still running. Calling force poweroff.') - expect(domain).to receive(:shutdown).and_raise(Timeout::Error) - expect(domain).to receive(:poweroff) - expect(subject.call(env)).to be_nil - end - end - end - - context 'when halt exceeds the timeout' do - before do - expect(logger).to_not receive(:info).with('Trying Libvirt graceful shutdown.') - expect(guest).to receive(:capability).with(:halt).and_raise(Timeout::Error) - end - - it "should call poweroff" do - expect(logger).to receive(:info).with('VM is still running. Calling force poweroff.') - expect(domain).to receive(:poweroff) - expect(subject.call(env)).to be_nil - end + it "should not print halting message" do + expect(ui).not_to receive(:info) + subject.call(env) end end - context 'with force halt enabled' do + context "when state is running" do before do - allow(env).to receive(:[]).and_call_original - expect(env).to receive(:[]).with(:force_halt).and_return(true) + expect(domain).to receive(:state).at_least(1). + and_return('running') + allow(domain).to receive(:poweroff) end - it "should just call poweroff" do + it "should poweroff" do expect(domain).to receive(:poweroff) - expect(subject.call(env)).to be_nil + subject.call(env) + end + + it "should print halting message" do + expect(ui).to receive(:info).with('Halting domain...') + subject.call(env) end end end