Skip to content

Commit b789734

Browse files
committed
Adding an apt provider
1 parent 83bcada commit b789734

2 files changed

Lines changed: 180 additions & 0 deletions

File tree

chef/spec/unit/provider/file_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,4 +221,19 @@
221221
@provider.backup
222222
end
223223

224+
end
225+
226+
describe Chef::Provider::File, "action_create_if_missing" do
227+
before(:each) do
228+
@resource = Chef::Resource::File.new("seattle")
229+
@resource.path(File.join(File.dirname(__FILE__), "..", "..", "data", "seattle.txt"))
230+
@node = Chef::Node.new
231+
@node.name "latte"
232+
@provider = Chef::Provider::File.new(@node, @resource)
233+
end
234+
235+
it "should call action create, since File can only touch" do
236+
@provider.should_receive(:action_create).and_return(true)
237+
@provider.action_create_if_missing
238+
end
224239
end
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
#
2+
# Author:: Adam Jacob (<adam@hjksolutions.com>)
3+
# Copyright:: Copyright (c) 2008 HJK Solutions, LLC
4+
# License:: Apache License, Version 2.0
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
19+
require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "spec_helper"))
20+
21+
describe Chef::Provider::Package::Apt, "load_current_resource" do
22+
before(:each) do
23+
@node = mock("Chef::Node", :null_object => true)
24+
@new_resource = mock("Chef::Resource::Package",
25+
:null_object => true,
26+
:name => "emacs",
27+
:version => nil,
28+
:package_name => "emacs",
29+
:updated => nil
30+
)
31+
@current_resource = mock("Chef::Resource::Package",
32+
:null_object => true,
33+
:name => "emacs",
34+
:version => nil,
35+
:package_name => nil,
36+
:updated => nil
37+
)
38+
@status = mock("Status", :exitstatus => 0)
39+
@provider = Chef::Provider::Package::Apt.new(@node, @new_resource)
40+
Chef::Resource::Package.stub!(:new).and_return(@current_resource)
41+
@provider.stub!(:popen4).and_return(@status)
42+
end
43+
44+
it "should create a current resource with the name of the new_resource" do
45+
Chef::Resource::Package.should_receive(:new).and_return(@current_resource)
46+
@provider.load_current_resource
47+
end
48+
49+
it "should set the current resources package name to the new resources package name" do
50+
@current_resource.should_receive(:package_name).with(@new_resource.package_name)
51+
@provider.load_current_resource
52+
end
53+
54+
it "should run apt-cache policy with the package name" do
55+
@provider.should_receive(:popen4).with("apt-cache policy #{@new_resource.package_name}").and_return(@status)
56+
@provider.load_current_resource
57+
end
58+
59+
it "should raise an exception if apt-cache policy fails" do
60+
@status.should_receive(:exitstatus).and_return(1)
61+
lambda { @provider.load_current_resource }.should raise_error(Chef::Exception::Package)
62+
end
63+
64+
it "should not raise an exception if apt-cache policy succeeds" do
65+
@status.should_receive(:exitstatus).and_return(0)
66+
lambda { @provider.load_current_resource }.should_not raise_error(Chef::Exception::Package)
67+
end
68+
69+
it "should return the current resouce" do
70+
@provider.load_current_resource.should eql(@current_resource)
71+
end
72+
end
73+
74+
describe Chef::Provider::Package::Apt, "install_package" do
75+
76+
before(:each) do
77+
@node = mock("Chef::Node", :null_object => true)
78+
@new_resource = mock("Chef::Resource::Package",
79+
:null_object => true,
80+
:name => "emacs",
81+
:version => nil,
82+
:package_name => "emacs",
83+
:updated => nil
84+
)
85+
@provider = Chef::Provider::Package::Apt.new(@node, @new_resource)
86+
end
87+
88+
it "should run apt-get install with the package name and version" do
89+
@provider.should_receive(:run_command).with({
90+
:command => "apt-get -q -y install emacs=1.0",
91+
:environment => {
92+
"DEBIAN_FRONTEND" => "noninteractive"
93+
}
94+
})
95+
@provider.install_package("emacs", "1.0")
96+
end
97+
end
98+
99+
describe Chef::Provider::Package::Apt, "upgrade_package" do
100+
101+
before(:each) do
102+
@node = mock("Chef::Node", :null_object => true)
103+
@new_resource = mock("Chef::Resource::Package",
104+
:null_object => true,
105+
:name => "emacs",
106+
:version => nil,
107+
:package_name => "emacs",
108+
:updated => nil
109+
)
110+
@provider = Chef::Provider::Package::Apt.new(@node, @new_resource)
111+
end
112+
113+
it "should run install_package with the name and version" do
114+
@provider.should_receive(:install_package).with("emacs", "1.0")
115+
@provider.upgrade_package("emacs", "1.0")
116+
end
117+
end
118+
119+
describe Chef::Provider::Package::Apt, "remove_package" do
120+
before(:each) do
121+
@node = mock("Chef::Node", :null_object => true)
122+
@new_resource = mock("Chef::Resource::Package",
123+
:null_object => true,
124+
:name => "emacs",
125+
:version => nil,
126+
:package_name => "emacs",
127+
:updated => nil
128+
)
129+
@provider = Chef::Provider::Package::Apt.new(@node, @new_resource)
130+
end
131+
132+
it "should run apt-get remove with the package name" do
133+
@provider.should_receive(:run_command).with({
134+
:command => "apt-get -q -y remove emacs",
135+
:environment => {
136+
"DEBIAN_FRONTEND" => "noninteractive"
137+
}
138+
})
139+
@provider.remove_package("emacs", "1.0")
140+
end
141+
end
142+
143+
describe Chef::Provider::Package::Apt, "purge_package" do
144+
before(:each) do
145+
@node = mock("Chef::Node", :null_object => true)
146+
@new_resource = mock("Chef::Resource::Package",
147+
:null_object => true,
148+
:name => "emacs",
149+
:version => nil,
150+
:package_name => "emacs",
151+
:updated => nil
152+
)
153+
@provider = Chef::Provider::Package::Apt.new(@node, @new_resource)
154+
end
155+
156+
it "should run apt-get purge with the package name" do
157+
@provider.should_receive(:run_command).with({
158+
:command => "apt-get -q -y purge emacs",
159+
:environment => {
160+
"DEBIAN_FRONTEND" => "noninteractive"
161+
}
162+
})
163+
@provider.purge_package("emacs", "1.0")
164+
end
165+
end

0 commit comments

Comments
 (0)