summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordanielsdeleo <dan@getchef.com>2015-06-09 09:47:27 -0700
committerdanielsdeleo <dan@chef.io>2015-06-09 16:06:19 -0700
commit942c6a24269112c37ef909c3e3dea896c79d04e2 (patch)
treef03b34a5a71b083bfebdd88c07a84a49ccc83b20
parenta0d1f90634964f7d3faab3a4de115d623750967b (diff)
downloadchef-942c6a24269112c37ef909c3e3dea896c79d04e2.tar.gz
Update tests to ensure shellout calls are stubbed
-rw-r--r--spec/unit/provider/package/rpm_spec.rb113
1 files changed, 79 insertions, 34 deletions
diff --git a/spec/unit/provider/package/rpm_spec.rb b/spec/unit/provider/package/rpm_spec.rb
index 1f7cb4501d..a3072390be 100644
--- a/spec/unit/provider/package/rpm_spec.rb
+++ b/spec/unit/provider/package/rpm_spec.rb
@@ -206,7 +206,9 @@ end
# adding tests for #3503
describe Chef::Provider::Package::Rpm do
- let(:provider) { Chef::Provider::Package::Rpm.new(new_resource, run_context) }
+
+ subject(:provider) { Chef::Provider::Package::Rpm.new(new_resource, run_context) }
+
let(:node) { Chef::Node.new }
let(:events) { Chef::EventDispatch::Dispatcher.new }
let(:run_context) { Chef::RunContext.new(node, {}, events) }
@@ -215,61 +217,104 @@ describe Chef::Provider::Package::Rpm do
resource.source "/tmp/supermarket-1.10.1~alpha.0-1.el5.x86_64.rpm"
end
end
- let(:exitstatus) { 0 }
- let(:stdout) { "supermarket 1.10.1~alpha.0-1.el5" }
- let(:status) { double('Process::Status', exitstatus: exitstatus, stdout: stdout) }
+
+ # `rpm -qp [stuff] $source`
+ let(:rpm_qp_status) { double('Process::Status', exitstatus: rpm_qp_exitstatus, stdout: rpm_qp_stdout) }
+
+ # `rpm -q [stuff] $package_name`
+ let(:rpm_q_status) { double('Process::Status', exitstatus: rpm_q_exitstatus, stdout: rpm_q_stdout) }
before(:each) do
allow(::File).to receive(:exists?).and_return(true)
- allow(provider).to receive(:shell_out!).and_return(status)
+
+ # Ensure all shell out usage is stubbed with exact arguments
+ allow(provider).to receive(:shell_out!).with("PLEASE STUB YOUR SHELLOUT CALLS").and_return(nil)
+ allow(provider).to receive(:shell_out).with("PLEASE STUB YOUR SHELLOUT CALLS").and_return(nil)
end
describe "when determining the current state of the package with a tilde (~) character in the version" do
- context "installation exists" do
- it "should get the source package version from rpm if provided" do
- expect(provider).to receive(:shell_out!).with("rpm -qp --queryformat '%{NAME} %{VERSION}-%{RELEASE}\n' /tmp/supermarket-1.10.1~alpha.0-1.el5.x86_64.rpm", timeout: 900).and_return(status)
- expect(provider).to receive(:shell_out).with("rpm -q --queryformat '%{NAME} %{VERSION}-%{RELEASE}\n' supermarket", timeout: 900).and_return(status)
- provider.load_current_resource
- expect(provider.current_resource.package_name).to eq("supermarket")
- expect(provider.new_resource.version).to eq("1.10.1~alpha.0-1.el5")
- end
- it "should return the current version installed if found by rpm" do
- expect(provider).to receive(:shell_out!).with("rpm -qp --queryformat '%{NAME} %{VERSION}-%{RELEASE}\n' /tmp/supermarket-1.10.1~alpha.0-1.el5.x86_64.rpm", timeout: 900).and_return(status)
- expect(provider).to receive(:shell_out).with("rpm -q --queryformat '%{NAME} %{VERSION}-%{RELEASE}\n' supermarket", timeout: 900).and_return(status)
- provider.load_current_resource
- expect(provider.current_resource.version).to eq("1.10.1~alpha.0-1.el5")
- end
+ before do
+ expect(provider).to receive(:shell_out!).
+ with("rpm -qp --queryformat '%{NAME} %{VERSION}-%{RELEASE}\n' /tmp/supermarket-1.10.1~alpha.0-1.el5.x86_64.rpm", timeout: 900).
+ and_return(rpm_qp_status)
+
+ expect(provider).to receive(:shell_out).
+ with("rpm -q --queryformat '%{NAME} %{VERSION}-%{RELEASE}\n' supermarket", timeout: 900).
+ and_return(rpm_q_status)
+ end
+
+ context "when rpm fails to query package install state" do
+
+ let(:rpm_qp_stdout) { "" }
+ let(:rpm_q_stdout) { "" }
+
+ let(:rpm_qp_exitstatus) { 0 }
+ let(:rpm_q_exitstatus) { -1 }
it "should not attempt an rpm installation" do
- expect(provider).to_not receive(:shell_out!).with("rpm -i /tmp/supermarket-1.10.1~alpha.0-1.el5.x86_64.rpm", timeout: 900)
- expect { provider.run_action(:any) }.to raise_error(Chef::Exceptions::Package)
+ expected_message = "Unable to determine current version due to RPM failure."
+ expect { provider.run_action(:install) }.to raise_error do |error|
+ expect(error).to be_a_kind_of(Chef::Exceptions::Package)
+ expect(error.to_s).to include(expected_message)
+ end
end
it "should not attempt an rpm upgrade" do
- expect(provider).to_not receive(:shell_out!).with("rpm -U /tmp/supermarket-1.10.1~alpha.0-1.el5.x86_64.rpm", timeout: 900)
- expect { provider.run_action(:any) }.to raise_error(Chef::Exceptions::Package)
+ expected_message = "Unable to determine current version due to RPM failure."
+ expect { provider.run_action(:upgrade) }.to raise_error do |error|
+ expect(error).to be_a_kind_of(Chef::Exceptions::Package)
+ expect(error.to_s).to include(expected_message)
+ end
end
+
end
- context "new package installation" do
- it "should run rpm -i with the package source to install" do
- provider.load_current_resource
- expect(provider.new_resource.version).to eq("1.10.1~alpha.0-1.el5")
- expect(provider).to receive(:shell_out!).with("rpm -i /tmp/supermarket-1.10.1~alpha.0-1.el5.x86_64.rpm", timeout: 900)
- provider.install_package("supermarket", "1.10.1~alpha.0-1.el5")
+ context "when the package is not installed" do
+
+ let(:rpm_qp_stdout) { "supermarket 1.10.1~alpha.0-1.el5" }
+ let(:rpm_q_stdout) { "" }
+
+ let(:rpm_qp_exitstatus) { 0 }
+ let(:rpm_q_exitstatus) { 0 }
+
+ describe "new package installation" do
+ it "should run rpm -i with the package source to install" do
+ provider.load_current_resource
+ expect(provider.new_resource.version).to eq("1.10.1~alpha.0-1.el5")
+ expect(provider).to receive(:shell_out!).with("rpm -i /tmp/supermarket-1.10.1~alpha.0-1.el5.x86_64.rpm", timeout: 900)
+ provider.install_package("supermarket", "1.10.1~alpha.0-1.el5")
+ end
end
+
end
- context "package upgrade" do
- it "should run rpm -U with the package source to upgrade" do
+ context "when the package is installed" do
+
+ let(:rpm_qp_stdout) { "supermarket 1.10.1~alpha.0-1.el5" }
+ let(:rpm_q_stdout) { "supermarket 1.10.1~alpha.0-1.el5" }
+
+ let(:rpm_qp_exitstatus) { 0 }
+ let(:rpm_q_exitstatus) { 0 }
+
+ it "should get the source package version from rpm if provided" do
provider.load_current_resource
- provider.current_resource.version("1.10.0~alpha.0-1.el5")
+ expect(provider.current_resource.package_name).to eq("supermarket")
expect(provider.new_resource.version).to eq("1.10.1~alpha.0-1.el5")
- expect(provider).to receive(:shell_out!).with("rpm -U /tmp/supermarket-1.10.1~alpha.0-1.el5.x86_64.rpm", timeout: 900)
- provider.upgrade_package("supermarket", "1.10.1~alpha.0-1.el5")
+ end
+
+ describe "package upgrade" do
+ it "should run rpm -U with the package source to upgrade" do
+ provider.load_current_resource
+ provider.current_resource.version("1.10.0~alpha.0-1.el5")
+ expect(provider.new_resource.version).to eq("1.10.1~alpha.0-1.el5")
+ expect(provider).to receive(:shell_out!).with("rpm -U /tmp/supermarket-1.10.1~alpha.0-1.el5.x86_64.rpm", timeout: 900)
+ provider.upgrade_package("supermarket", "1.10.1~alpha.0-1.el5")
+ end
+
end
end
+
end
end