Skip to content

Commit 6bbaf52

Browse files
committed
Handle some new version cases, and add portage specs
1 parent b0260c6 commit 6bbaf52

2 files changed

Lines changed: 173 additions & 2 deletions

File tree

chef/lib/chef/provider/package/portage.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,15 @@ def candidate_version
8686

8787

8888
def install_package(name, version)
89+
pkg = "=#{name}-#{version}"
90+
91+
if(version =~ /^\~(.+)/)
92+
# If we start with a tilde
93+
pkg = "~#{name}-#{$1}"
94+
end
95+
8996
run_command(
90-
:command => "emerge -g --color n --nospinner --quiet =#{name}-#{version}"
97+
:command => "emerge -g --color n --nospinner --quiet #{pkg}"
9198
)
9299
end
93100

@@ -96,8 +103,14 @@ def upgrade_package(name, version)
96103
end
97104

98105
def remove_package(name, version)
106+
if(version)
107+
pkg = "=#{@new_resource.package_name}-#{version}"
108+
else
109+
pkg = "#{@new_resource.package_name}"
110+
end
111+
99112
run_command(
100-
:command => "emerge --unmerge --color n --nospinner --quiet #{@new_resource.package_name}"
113+
:command => "emerge --unmerge --color n --nospinner --quiet #{pkg}"
101114
)
102115
end
103116

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
#
2+
# Author:: Caleb Tennis (<caleb.tennis@gmail.com>)
3+
# Copyright:: Copyright (c) 2008 Opscode, Inc.
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+
require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "spec_helper"))
19+
20+
describe Chef::Provider::Package::Portage, "load_current_resource" do
21+
before(:each) do
22+
@node = mock("Chef::Node", :null_object => true)
23+
24+
@new_resource = mock("Chef::Resource::Package",
25+
:null_object => true,
26+
:name => "dev-util/git",
27+
:version => nil,
28+
:package_name => "dev-util/git",
29+
:updated => nil
30+
)
31+
@current_resource = mock("Chef::Resource::Package",
32+
:null_object => true,
33+
:name => "dev-util/git",
34+
:version => nil,
35+
:package_name => nil,
36+
:updated => nil
37+
)
38+
39+
@provider = Chef::Provider::Package::Portage.new(@node, @new_resource)
40+
Chef::Resource::Package.stub!(:new).and_return(@current_resource)
41+
42+
::File.stub!(:exists?).and_return(true)
43+
end
44+
45+
it "should create a current resource with the name of new_resource" do
46+
Chef::Resource::Package.should_receive(:new).and_return(@current_resource)
47+
@provider.load_current_resource
48+
end
49+
50+
it "should set the current resource package name to the new resource package name" do
51+
@current_resource.should_receive(:package_name).with(@new_resource.package_name)
52+
@provider.load_current_resource
53+
end
54+
55+
it "should return a current resource with a nil version if the package is not found" do
56+
::Dir.stub!(:entries).and_return("git-1.0.0")
57+
@current_resource.should_receive(:version).with("1.0.0")
58+
@provider.load_current_resource
59+
end
60+
61+
it "should return a current resource with the correct version if the package is found" do
62+
::Dir.stub!(:entries).and_return("notgit-1.0.0")
63+
@current_resource.should_receive(:version).with(nil)
64+
@provider.load_current_resource
65+
end
66+
67+
end
68+
69+
describe Chef::Provider::Package::Portage, "candidate_version" do
70+
before(:each) do
71+
@node = mock("Chef::Node", :null_object => true)
72+
@new_resource = mock("Chef::Resource::Package",
73+
:null_object => true,
74+
:name => "dev-util/git",
75+
:version => nil,
76+
:package_name => "dev-util/git",
77+
:updated => nil
78+
)
79+
@provider = Chef::Provider::Package::Portage.new(@node, @new_resource)
80+
end
81+
82+
it "should return the candidate_version variable if already setup" do
83+
@provider.candidate_version = "1.0.0"
84+
@provider.should_not_receive(:popen4)
85+
@provider.candidate_version
86+
end
87+
88+
it "should lookup the candidate_version if the variable is not already set" do
89+
@status = mock("Status", :exitstatus => 0)
90+
@provider.stub!(:popen4).and_return(@status)
91+
@provider.should_receive(:popen4)
92+
@provider.candidate_version
93+
end
94+
95+
it "should throw and exception if the exitstatus is not 0" do
96+
@status = mock("Status", :exitstatus => 1)
97+
@provider.stub!(:popen4).and_return(@status)
98+
lambda { @provider.candidate_version }.should raise_error(Chef::Exceptions::Package)
99+
end
100+
101+
end
102+
103+
describe Chef::Provider::Package::Portage, "install_package" do
104+
before(:each) do
105+
@node = mock("Chef::Node", :null_object => true)
106+
@new_resource = mock("Chef::Resource::Package",
107+
:null_object => true,
108+
:name => "dev-util/git",
109+
:version => nil,
110+
:package_name => "dev-util/git",
111+
:updated => nil
112+
)
113+
@provider = Chef::Provider::Package::Portage.new(@node, @new_resource)
114+
end
115+
116+
it "should install a normally versioned package using portage" do
117+
@provider.should_receive(:run_command).with({
118+
:command => "emerge -g --color n --nospinner --quiet =dev-util/git-1.0.0"
119+
})
120+
@provider.install_package("dev-util/git", "1.0.0")
121+
end
122+
123+
it "should install a tilde versioned package using portage" do
124+
@provider.should_receive(:run_command).with({
125+
:command => "emerge -g --color n --nospinner --quiet ~dev-util/git-1.0.0"
126+
})
127+
@provider.install_package("dev-util/git", "~1.0.0")
128+
end
129+
end
130+
131+
describe Chef::Provider::Package::Portage, "remove_package" do
132+
before(:each) do
133+
@node = mock("Chef::Node", :null_object => true)
134+
@new_resource = mock("Chef::Resource::Package",
135+
:null_object => true,
136+
:name => "dev-util/git",
137+
:version => nil,
138+
:package_name => "dev-util/git",
139+
:updated => nil
140+
)
141+
@provider = Chef::Provider::Package::Portage.new(@node, @new_resource)
142+
end
143+
144+
it "should un-emerge the package with no version specified" do
145+
@provider.should_receive(:run_command).with({
146+
:command => "emerge --unmerge --color n --nospinner --quiet dev-util/git"
147+
})
148+
@provider.remove_package("dev-util/git", nil)
149+
end
150+
151+
it "should un-emerge the package with a version specified" do
152+
@provider.should_receive(:run_command).with({
153+
:command => "emerge --unmerge --color n --nospinner --quiet =dev-util/git-1.0.0"
154+
})
155+
@provider.remove_package("dev-util/git", "1.0.0")
156+
end
157+
end
158+

0 commit comments

Comments
 (0)