From d55f8d349680fc11b5d27650ff5ff01149560d4c Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Mon, 17 Jun 2019 11:02:57 -0700 Subject: [PATCH] Fixes #10912: Update chef install check for guests Prior to this commit, the chef_installed capability was looking for a string that has recently changed in newer versions of chef. This commit fixes that by instead just looking for the right version that was configured for the chef client, rather than the specific string that could change again in the future. --- .../chef/cap/freebsd/chef_installed.rb | 2 +- .../chef/cap/linux/chef_installed.rb | 2 +- .../chef/cap/omnios/chef_installed.rb | 2 +- .../chef/cap/windows/chef_installed.rb | 4 +- .../chef/cap/freebsd/chef_installed_test.rb | 41 +++++++++++++++++++ .../chef/cap/linux/chef_installed_test.rb | 41 +++++++++++++++++++ .../chef/cap/omnios/chef_installed_test.rb | 41 +++++++++++++++++++ .../chef/cap/windows/chef_installed_test.rb | 41 +++++++++++++++++++ 8 files changed, 169 insertions(+), 5 deletions(-) create mode 100644 test/unit/plugins/provisioners/chef/cap/freebsd/chef_installed_test.rb create mode 100644 test/unit/plugins/provisioners/chef/cap/linux/chef_installed_test.rb create mode 100644 test/unit/plugins/provisioners/chef/cap/omnios/chef_installed_test.rb create mode 100644 test/unit/plugins/provisioners/chef/cap/windows/chef_installed_test.rb diff --git a/plugins/provisioners/chef/cap/freebsd/chef_installed.rb b/plugins/provisioners/chef/cap/freebsd/chef_installed.rb index bb58083ca..9de19ef57 100644 --- a/plugins/provisioners/chef/cap/freebsd/chef_installed.rb +++ b/plugins/provisioners/chef/cap/freebsd/chef_installed.rb @@ -10,7 +10,7 @@ module VagrantPlugins command = "test -x #{knife}" if version != :latest - command << "&& #{knife} --version | grep 'Chef: #{version}'" + command << "&& #{knife} --version | grep '#{version}'" end machine.communicate.test(command, sudo: true) diff --git a/plugins/provisioners/chef/cap/linux/chef_installed.rb b/plugins/provisioners/chef/cap/linux/chef_installed.rb index c664f90bb..b5c8adff1 100644 --- a/plugins/provisioners/chef/cap/linux/chef_installed.rb +++ b/plugins/provisioners/chef/cap/linux/chef_installed.rb @@ -10,7 +10,7 @@ module VagrantPlugins command = "test -x #{knife}" if version != :latest - command << "&& #{knife} --version | grep 'Chef: #{version}'" + command << "&& #{knife} --version | grep '#{version}'" end machine.communicate.test(command, sudo: true) diff --git a/plugins/provisioners/chef/cap/omnios/chef_installed.rb b/plugins/provisioners/chef/cap/omnios/chef_installed.rb index c1e55ba53..b6a453d28 100644 --- a/plugins/provisioners/chef/cap/omnios/chef_installed.rb +++ b/plugins/provisioners/chef/cap/omnios/chef_installed.rb @@ -11,7 +11,7 @@ module VagrantPlugins command = "test -x #{knife}" if version != :latest - command << "&& #{knife} --version | grep 'Chef: #{version}'" + command << "&& #{knife} --version | grep '#{version}'" end machine.communicate.test(command, sudo: true) diff --git a/plugins/provisioners/chef/cap/windows/chef_installed.rb b/plugins/provisioners/chef/cap/windows/chef_installed.rb index 61335a5cd..25a817607 100644 --- a/plugins/provisioners/chef/cap/windows/chef_installed.rb +++ b/plugins/provisioners/chef/cap/windows/chef_installed.rb @@ -7,9 +7,9 @@ module VagrantPlugins # @return [true, false] def self.chef_installed(machine, product, version) if version != :latest - command = 'if ((&knife --version) -Match "Chef: ' + version.to_s + '"){ exit 0 } else { exit 1 }' + command = 'if ((&knife --version) -Match "' + version.to_s + '"){ exit 0 } else { exit 1 }' else - command = 'if ((&knife --version) -Match "Chef: *"){ exit 0 } else { exit 1 }' + command = 'if ((&knife --version) -Match "Chef*"){ exit 0 } else { exit 1 }' end machine.communicate.test(command, sudo: true) end diff --git a/test/unit/plugins/provisioners/chef/cap/freebsd/chef_installed_test.rb b/test/unit/plugins/provisioners/chef/cap/freebsd/chef_installed_test.rb new file mode 100644 index 000000000..b0dc209c4 --- /dev/null +++ b/test/unit/plugins/provisioners/chef/cap/freebsd/chef_installed_test.rb @@ -0,0 +1,41 @@ +require_relative "../../../../../base" + +require Vagrant.source_root.join("plugins/provisioners/chef/cap/freebsd/chef_installed") + +describe VagrantPlugins::Chef::Cap::FreeBSD::ChefInstalled do + include_context "unit" + + let(:iso_env) do + # We have to create a Vagrantfile so there is a root path + env = isolated_environment + env.vagrantfile("") + env.create_vagrant_env + end + + let(:machine) { iso_env.machine(iso_env.machine_names[0], :dummy) } + let(:communicator) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + let(:config) { double("config") } + + subject { described_class } + + before do + allow(machine).to receive(:communicate).and_return(communicator) + end + + describe "#chef_installed" do + let(:version) { "15.0.0" } + let(:command) { "test -x /opt/chef_solo/bin/knife&& /opt/chef_solo/bin/knife --version | grep '15.0.0'" } + + it "returns true if installed" do + expect(machine.communicate).to receive(:test). + with(command, sudo: true).and_return(true) + subject.chef_installed(machine, "chef_solo", version) + end + + it "returns false if not installed" do + expect(machine.communicate).to receive(:test). + with(command, sudo: true).and_return(false) + expect(subject.chef_installed(machine, "chef_solo", version)).to be_falsey + end + end +end diff --git a/test/unit/plugins/provisioners/chef/cap/linux/chef_installed_test.rb b/test/unit/plugins/provisioners/chef/cap/linux/chef_installed_test.rb new file mode 100644 index 000000000..4b449fce1 --- /dev/null +++ b/test/unit/plugins/provisioners/chef/cap/linux/chef_installed_test.rb @@ -0,0 +1,41 @@ +require_relative "../../../../../base" + +require Vagrant.source_root.join("plugins/provisioners/chef/cap/linux/chef_installed") + +describe VagrantPlugins::Chef::Cap::Linux::ChefInstalled do + include_context "unit" + + let(:iso_env) do + # We have to create a Vagrantfile so there is a root path + env = isolated_environment + env.vagrantfile("") + env.create_vagrant_env + end + + let(:machine) { iso_env.machine(iso_env.machine_names[0], :dummy) } + let(:communicator) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + let(:config) { double("config") } + + subject { described_class } + + before do + allow(machine).to receive(:communicate).and_return(communicator) + end + + describe "#chef_installed" do + let(:version) { "15.0.0" } + let(:command) { "test -x /opt/chef_solo/bin/knife&& /opt/chef_solo/bin/knife --version | grep '15.0.0'" } + + it "returns true if installed" do + expect(machine.communicate).to receive(:test). + with(command, sudo: true).and_return(true) + subject.chef_installed(machine, "chef_solo", version) + end + + it "returns false if not installed" do + expect(machine.communicate).to receive(:test). + with(command, sudo: true).and_return(false) + expect(subject.chef_installed(machine, "chef_solo", version)).to be_falsey + end + end +end diff --git a/test/unit/plugins/provisioners/chef/cap/omnios/chef_installed_test.rb b/test/unit/plugins/provisioners/chef/cap/omnios/chef_installed_test.rb new file mode 100644 index 000000000..3c00f870a --- /dev/null +++ b/test/unit/plugins/provisioners/chef/cap/omnios/chef_installed_test.rb @@ -0,0 +1,41 @@ +require_relative "../../../../../base" + +require Vagrant.source_root.join("plugins/provisioners/chef/cap/omnios/chef_installed") + +describe VagrantPlugins::Chef::Cap::OmniOS::ChefInstalled do + include_context "unit" + + let(:iso_env) do + # We have to create a Vagrantfile so there is a root path + env = isolated_environment + env.vagrantfile("") + env.create_vagrant_env + end + + let(:machine) { iso_env.machine(iso_env.machine_names[0], :dummy) } + let(:communicator) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + let(:config) { double("config") } + + subject { described_class } + + before do + allow(machine).to receive(:communicate).and_return(communicator) + end + + describe "#chef_installed" do + let(:version) { "15.0.0" } + let(:command) { "test -x /opt/chef_solo/bin/knife&& /opt/chef_solo/bin/knife --version | grep '15.0.0'" } + + it "returns true if installed" do + expect(machine.communicate).to receive(:test). + with(command, sudo: true).and_return(true) + subject.chef_installed(machine, "chef_solo", version) + end + + it "returns false if not installed" do + expect(machine.communicate).to receive(:test). + with(command, sudo: true).and_return(false) + expect(subject.chef_installed(machine, "chef_solo", version)).to be_falsey + end + end +end diff --git a/test/unit/plugins/provisioners/chef/cap/windows/chef_installed_test.rb b/test/unit/plugins/provisioners/chef/cap/windows/chef_installed_test.rb new file mode 100644 index 000000000..3a487933f --- /dev/null +++ b/test/unit/plugins/provisioners/chef/cap/windows/chef_installed_test.rb @@ -0,0 +1,41 @@ +require_relative "../../../../../base" + +require Vagrant.source_root.join("plugins/provisioners/chef/cap/windows/chef_installed") + +describe VagrantPlugins::Chef::Cap::Windows::ChefInstalled do + include_context "unit" + + let(:iso_env) do + # We have to create a Vagrantfile so there is a root path + env = isolated_environment + env.vagrantfile("") + env.create_vagrant_env + end + + let(:machine) { iso_env.machine(iso_env.machine_names[0], :dummy) } + let(:communicator) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + let(:config) { double("config") } + + subject { described_class } + + before do + allow(machine).to receive(:communicate).and_return(communicator) + end + + describe "#chef_installed" do + let(:version) { "15.0.0" } + let(:command) { "if ((&knife --version) -Match \"15.0.0\"){ exit 0 } else { exit 1 }" } + + it "returns true if installed" do + expect(machine.communicate).to receive(:test). + with(command, sudo: true).and_return(true) + subject.chef_installed(machine, "chef_solo", version) + end + + it "returns false if not installed" do + expect(machine.communicate).to receive(:test). + with(command, sudo: true).and_return(false) + expect(subject.chef_installed(machine, "chef_solo", version)).to be_falsey + end + end +end