Skip to content

Commit 16c9364

Browse files
committed
Adding tests for the apt-cache policy popen4, thanks to and_yield
1 parent 9abbb2c commit 16c9364

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

chef/spec/unit/provider/package/apt_spec.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@
3939
@provider = Chef::Provider::Package::Apt.new(@node, @new_resource)
4040
Chef::Resource::Package.stub!(:new).and_return(@current_resource)
4141
@provider.stub!(:popen4).and_return(@status)
42+
@stdin = mock("STDIN", :null_object => true)
43+
@stdout = mock("STDOUT", :null_object => true)
44+
@stdout.stub!(:each).and_yield("emacs:").
45+
and_yield(" Installed: (none)").
46+
and_yield(" Candidate: (none)").
47+
and_yield(" Version Table:")
48+
@stderr = mock("STDERR", :null_object => true)
49+
@pid = mock("PID", :null_object => true)
4250
end
4351

4452
it "should create a current resource with the name of the new_resource" do
@@ -56,6 +64,44 @@
5664
@provider.load_current_resource
5765
end
5866

67+
it "should close stdin on apt-cache policy" do
68+
@provider.stub!(:popen4).and_yield(@pid, @stdin, @stdout, @stderr).and_return(@status)
69+
@stdin.should_receive(:close).and_return(true)
70+
@provider.load_current_resource
71+
end
72+
73+
it "should read stdout on apt-cache policy" do
74+
@provider.stub!(:popen4).and_yield(@pid, @stdin, @stdout, @stderr).and_return(@status)
75+
@stdout.should_receive(:each).and_return(true)
76+
@provider.load_current_resource
77+
end
78+
79+
it "should set the installed version to nil on the current resource if apt-cache policy installed version is (none)" do
80+
@provider.stub!(:popen4).and_yield(@pid, @stdin, @stdout, @stderr).and_return(@status)
81+
@current_resource.should_receive(:version).with(nil).and_return(true)
82+
@provider.load_current_resource
83+
end
84+
85+
it "should set the installed version if apt-cache policy has one" do
86+
@stdout.stub!(:each).and_yield("emacs:").
87+
and_yield(" Installed: 0.1.1").
88+
and_yield(" Candidate: (none)").
89+
and_yield(" Version Table:")
90+
@provider.stub!(:popen4).and_yield(@pid, @stdin, @stdout, @stderr).and_return(@status)
91+
@current_resource.should_receive(:version).with("0.1.1").and_return(true)
92+
@provider.load_current_resource
93+
end
94+
95+
it "should set the candidate version if apt-cache policy has one" do
96+
@stdout.stub!(:each).and_yield("emacs:").
97+
and_yield(" Installed: 0.1.1").
98+
and_yield(" Candidate: 10").
99+
and_yield(" Version Table:")
100+
@provider.stub!(:popen4).and_yield(@pid, @stdin, @stdout, @stderr).and_return(@status)
101+
@provider.load_current_resource
102+
@provider.candidate_version.should eql("10")
103+
end
104+
59105
it "should raise an exception if apt-cache policy fails" do
60106
@status.should_receive(:exitstatus).and_return(1)
61107
lambda { @provider.load_current_resource }.should raise_error(Chef::Exception::Package)

0 commit comments

Comments
 (0)