Skip to content

Commit

Permalink
cleanup specs to mock less spec more
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsdeleo committed Jul 8, 2010
1 parent 1c725a6 commit 30e4914
Showing 1 changed file with 30 additions and 37 deletions.
67 changes: 30 additions & 37 deletions chef/spec/unit/provider/package/rpm_spec.rb
@@ -1,6 +1,7 @@
#
# Author:: Joshua Timberman (<joshua@opscode.com>)
# Copyright:: Copyright (c) 2008 Opscode, Inc.
# Author:: Daniel DeLeo (<dan@opscode.com>)
# Copyright:: Copyright (c) 2008, 2010 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -17,35 +18,30 @@
#
require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "spec_helper"))

describe Chef::Provider::Package::Rpm, "load_current_resource" do
describe Chef::Provider::Package::Rpm do
before(:each) do
@node = Chef::Node.new
@run_context = Chef::RunContext.new(@node, {})

@new_resource = Chef::Resource::Package.new("emacs")
@new_resource.source "/tmp/emacs-21.4-20.el5.i386.rpm"
@current_resource = Chef::Resource::Package.new("emacs")

@provider = Chef::Provider::Package::Rpm.new(@new_resource, @run_context)
Chef::Resource::Package.stub!(:new).and_return(@current_resource)

@stdin = mock("STDIN", :null_object => true)
@stdout = mock("STDOUT", :null_object => true)
@status = mock("Status", :exitstatus => 0)
@stderr = mock("STDERR", :null_object => true)
@pid = mock("PID", :null_object => true)
@provider.stub!(:popen4).and_return(@status)
::File.stub!(:exists?).and_return(true)
end

describe "when determining the current state of the package" do

it "should create a current resource with the name of new_resource" do
Chef::Resource::Package.should_receive(:new).and_return(@current_resource)
@provider.stub!(:popen4).and_return(@status)
@provider.load_current_resource
@provider.current_resource.name.should == "emacs"
end

it "should set the current reource package name to the new resource package name" do
@provider.stub!(:popen4).and_return(@status)
@provider.load_current_resource
@provider.current_resource.package_name.should == 'emacs'
end
Expand All @@ -56,18 +52,20 @@
end

it "should get the source package version from rpm if provided" do
@stdout.stub!(:each).and_yield("emacs 21.4-20.el5")
@provider.stub!(:popen4).with("rpm -qp --queryformat '%{NAME} %{VERSION}-%{RELEASE}\n' #{@new_resource.source}").and_yield(@pid, @stdin, @stdout, @stderr).and_return(@status)
@stdout = StringIO.new("emacs 21.4-20.el5")
@provider.should_receive(:popen4).with("rpm -qp --queryformat '%{NAME} %{VERSION}-%{RELEASE}\n' /tmp/emacs-21.4-20.el5.i386.rpm").and_yield(@pid, @stdin, @stdout, @stderr).and_return(@status)
@provider.should_receive(:popen4).with("rpm -q --queryformat '%{NAME} %{VERSION}-%{RELEASE}\n' emacs").and_return(@status)
@provider.load_current_resource
@current_resource.package_name.should == "emacs"
@provider.current_resource.package_name.should == "emacs"
@provider.new_resource.version.should == "21.4-20.el5"
end

it "should return the current version installed if found by rpm" do
@stdout.stub!(:each).and_yield("emacs 21.4-20.el5")
@provider.stub!(:popen4).with("rpm -q --queryformat '%{NAME} %{VERSION}-%{RELEASE}\n' #{@current_resource.package_name}").and_yield(@pid, @stdin, @stdout, @stderr).and_return(@status)
@current_resource.should_receive(:version).with("21.4-20.el5")
@stdout = StringIO.new("emacs 21.4-20.el5")
@provider.should_receive(:popen4).with("rpm -qp --queryformat '%{NAME} %{VERSION}-%{RELEASE}\n' /tmp/emacs-21.4-20.el5.i386.rpm").and_return(@status)
@provider.should_receive(:popen4).with("rpm -q --queryformat '%{NAME} %{VERSION}-%{RELEASE}\n' emacs").and_yield(@pid, @stdin, @stdout, @stderr).and_return(@status)
@provider.load_current_resource
@provider.current_resource.version.should == "21.4-20.el5"
end

it "should raise an exception if the source is not set but we are installing" do
Expand All @@ -77,18 +75,19 @@
end

it "should raise an exception if rpm fails to run" do
@status = mock("Status", :exitstatus => -1)
@provider.stub!(:popen4).and_return(@status)
status = mock("Status", :exitstatus => -1)
@provider.stub!(:popen4).and_return(status)
lambda { @provider.load_current_resource }.should raise_error(Chef::Exceptions::Package)
end
end

describe "once the current state of the package is known" do
describe "after the current resource is loaded" do
before do
@current_resource = Chef::Resource::Package.new("emacs")
@provider.current_resource = @current_resource
end

describe Chef::Provider::Package::Rpm, "install and upgrade" do
describe "when installing or upgrading" do
it "should run rpm -i with the package source to install" do
@provider.should_receive(:run_command_with_systems_locale).with({
:command => "rpm -i /tmp/emacs-21.4-20.el5.i386.rpm"
Expand All @@ -97,15 +96,24 @@
end

it "should run rpm -U with the package source to upgrade" do
@current_resource.stub!(:version).and_return("21.4-19.el5")
@current_resource.version("21.4-19.el5")
@provider.should_receive(:run_command_with_systems_locale).with({
:command => "rpm -U /tmp/emacs-21.4-20.el5.i386.rpm"
})
@provider.upgrade_package("emacs", "21.4-20.el5")
end

it "installs with custom options specified in the resource" do
@provider.candidate_version = '11'
@new_resource.options("--dbpath /var/lib/rpm")
@provider.should_receive(:run_command_with_systems_locale).with({
:command => "rpm --dbpath /var/lib/rpm -i /tmp/emacs-21.4-20.el5.i386.rpm"
})
@provider.install_package(@new_resource.name, @provider.candidate_version)
end
end

describe Chef::Provider::Package::Rpm, "remove" do
describe "when removing the package" do
it "should run rpm -e to remove the package" do
@provider.should_receive(:run_command_with_systems_locale).with({
:command => "rpm -e emacs-21.4-20.el5"
Expand All @@ -114,20 +122,5 @@
end
end
end

describe "execute rpm command with options" do
before do
@provider.current_resource = @current_resource
end

it "should have options set" do
@provider.candidate_version = '11'
@new_resource.stub!(:options).and_return("--dbpath /var/lib/rpm")
@provider.should_receive(:run_command_with_systems_locale).with({
:command => "rpm --dbpath /var/lib/rpm -i /tmp/emacs-21.4-20.el5.i386.rpm"
})
@provider.install_package(@new_resource.name, @provider.candidate_version)
end
end
end

0 comments on commit 30e4914

Please sign in to comment.