diff --git a/test/unit/vagrant/cli_test.rb b/test/unit/vagrant/cli_test.rb index fb33ac62f..5d079278f 100644 --- a/test/unit/vagrant/cli_test.rb +++ b/test/unit/vagrant/cli_test.rb @@ -1,6 +1,7 @@ require_relative "../base" require "vagrant/cli" +require "vagrant/util" describe Vagrant::CLI do include_context "unit" @@ -9,9 +10,21 @@ describe Vagrant::CLI do let(:commands) { {} } let(:iso_env) { isolated_environment } let(:env) { iso_env.create_vagrant_env } + let(:checkpoint) { double("checkpoint") } before do allow(Vagrant.plugin("2").manager).to receive(:commands).and_return(commands) + allow(Vagrant::Util::CheckpointClient).to receive(:instance).and_return(checkpoint) + allow(checkpoint).to receive(:setup).and_return(checkpoint) + allow(checkpoint).to receive(:check) + allow(checkpoint).to receive(:display) + end + + describe "#initialize" do + it "should setup checkpoint" do + expect(checkpoint).to receive(:check) + described_class.new(["destroy"], env) + end end describe "#execute" do @@ -35,6 +48,12 @@ describe Vagrant::CLI do subject = described_class.new(["destroy"], env) expect(subject.execute).to eql(1) end + + it "displays any checkpoint information" do + commands[:destroy] = [command_lambda("destroy", 42), {}] + expect(checkpoint).to receive(:display) + described_class.new(["destroy"], env).execute + end end describe "#help" do diff --git a/test/unit/vagrant/util/checkpoint_client_test.rb b/test/unit/vagrant/util/checkpoint_client_test.rb new file mode 100644 index 000000000..c0e038c93 --- /dev/null +++ b/test/unit/vagrant/util/checkpoint_client_test.rb @@ -0,0 +1,174 @@ +require File.expand_path("../../../base", __FILE__) + +require "vagrant/util/checkpoint_client" + +describe Vagrant::Util::CheckpointClient do + include_context "unit" + + let(:iso_env) { isolated_environment } + let(:env) { iso_env.create_vagrant_env } + let(:result) { {} } + let(:prefixed_ui) { double("prefixed_ui") } + + subject{ Vagrant::Util::CheckpointClient.instance } + + after{ subject.reset! } + before do + allow(subject).to receive(:result).and_return(result) + allow(Vagrant::UI::Prefixed).to receive(:new).and_return(prefixed_ui) + end + + it "should not be enabled by default" do + expect(subject.enabled).to be(false) + end + + describe "#setup" do + before{ subject.setup(env) } + + it "should enable after setup" do + expect(subject.enabled).to be(true) + end + + it "should generate required paths" do + expect(subject.files).not_to be_empty + end + end + + describe "#check" do + context "without #setup" do + it "should not start the check" do + expect(Thread).not_to receive(:new) + subject.check + end + end + + context "with setup" do + before{ subject.setup(env) } + + it "should start the check" do + expect(Thread).to receive(:new) + subject.check + end + + it "should call checkpoint" do + expect(Thread).to receive(:new).and_yield + expect(Checkpoint).to receive(:check) + subject.check + end + end + end + + describe "#display" do + it "should only dislay once" do + expect(subject).to receive(:version_check).once + expect(subject).to receive(:alerts_check).once + + 2.times{ subject.display } + end + + it "should not display cached information" do + expect(subject).to receive(:result).and_return("cached" => true).at_least(:once) + expect(subject).not_to receive(:version_check) + expect(subject).not_to receive(:alerts_check) + + subject.display + end + end + + describe "#alerts_check" do + let(:critical){ + [{"level" => "critical", "message" => "critical message", + "url" => "http://example.com", "date" => Time.now.to_i}] + } + let(:warn){ + [{"level" => "warn", "message" => "warn message", + "url" => "http://example.com", "date" => Time.now.to_i}] + } + let(:info){ + [{"level" => "info", "message" => "info message", + "url" => "http://example.com", "date" => Time.now.to_i}] + } + + before{ subject.setup(env) } + + context "with no alerts" do + it "should not display alerts" do + expect(prefixed_ui).not_to receive(:info) + subject.alerts_check + end + end + + context "with critical alerts" do + let(:result) { {"alerts" => critical} } + + it "should display critical alert" do + expect(prefixed_ui).to receive(:error) + subject.alerts_check + end + end + + context "with warn alerts" do + let(:result) { {"alerts" => warn} } + + it "should display warn alerts" do + expect(prefixed_ui).to receive(:warn) + subject.alerts_check + end + end + + context "with info alerts" do + let(:result) { {"alerts" => info} } + + it "should display info alerts" do + expect(prefixed_ui).to receive(:info) + subject.alerts_check + end + end + + context "with mixed alerts" do + let(:result) { {"alerts" => info + warn + critical} } + + it "should display all alert types" do + expect(prefixed_ui).to receive(:info) + expect(prefixed_ui).to receive(:warn) + expect(prefixed_ui).to receive(:error) + + subject.alerts_check + end + end + end + + describe "#version_check" do + before{ subject.setup(env) } + + let(:new_version){ Gem::Version.new(Vagrant::VERSION).bump.to_s } + let(:old_version){ Gem::Version.new("1.0.0") } + + context "latest version is same as current version" do + let(:result) { {"current_version" => Vagrant::VERSION } } + + it "should not display upgrade information" do + expect(prefixed_ui).not_to receive(:info) + subject.version_check + end + end + + context "latest version is older than current version" do + let(:result) { {"current_version" => old_version} } + + it "should not display upgrade information" do + expect(prefixed_ui).not_to receive(:info) + subject.version_check + end + end + + context "latest version is newer than current version" do + let(:result) { {"current_version" => new_version} } + + it "should not display upgrade information" do + expect(prefixed_ui).not_to receive(:info).at_least(:once) + subject.version_check + end + end + end +end