Skip to content

Commit 0fc95bf

Browse files
sdelanoSeth Falcon
authored andcommitted
environment from file w/ cookbook version dsl
1 parent ef2b889 commit 0fc95bf

4 files changed

Lines changed: 157 additions & 13 deletions

File tree

chef/lib/chef/environment.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class Chef
2424
class Environment
2525

2626
include Chef::Mixin::ParamsValidate
27+
include Chef::Mixin::FromFile
2728

2829
attr_accessor :couchdb, :couchdb_rev
2930
attr_reader :couchdb_id
@@ -103,6 +104,17 @@ def cookbook_versions(arg=nil)
103104
)
104105
end
105106

107+
def cookbook(cookbook, version)
108+
validate({
109+
:version => version
110+
},{
111+
:version => {
112+
:callbacks => { "should be a valid version number" => lambda { |v| Chef::Environment.validate_cookbook_version(v) } }
113+
}
114+
})
115+
@cookbook_versions[cookbook] = version
116+
end
117+
106118
def to_hash
107119
result = {
108120
"name" => @name,
@@ -199,9 +211,14 @@ def to_s
199211
def self.validate_cookbook_versions(cv)
200212
return false unless cv.kind_of?(Hash)
201213
cv.each do |cookbook, version|
202-
return false unless version.kind_of?(String)
214+
return false unless Chef::Environment.validate_cookbook_version(version)
203215
end
204216
true
205217
end
218+
219+
def self.validate_cookbook_version(version)
220+
# TODO: check if version syntax is like rubygems syntax
221+
version.kind_of?(String)
222+
end
206223
end
207224
end
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#
2+
# Author:: Stephen Delano (<stephen@opscode.com>)
3+
# Copyright:: Copyright (c) 2010 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+
19+
class Chef
20+
class Knife
21+
class EnvironmentFromFile < Knife
22+
23+
banner "knife environment from file FILE (options)"
24+
25+
def run
26+
if @name_args[0].nil?
27+
show_usage
28+
Chef::Log.fatal("You must specify a file to load")
29+
exit 1
30+
end
31+
32+
updated = load_from_file(Chef::Environment, @name_args[0])
33+
updated.save
34+
output(format_for_display(updated)) if config[:print_after]
35+
Chef::Log.warn("Updated Environment #{updated.name}!")
36+
end
37+
end
38+
end
39+
end

chef/spec/unit/environment_spec.rb

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,18 @@
9898
end
9999
end
100100

101+
describe "cookbook" do
102+
it "should set the version of the cookbook in the cookbook_versions hash" do
103+
@environment.cookbook("apt", "1.2.3")
104+
@environment.cookbook_versions["apt"].should == "1.2.3"
105+
end
106+
107+
it "should validate the cookbook version it is passed" do
108+
Chef::Environment.should_receive(:validate_cookbook_version).with("1.2.3").and_return true
109+
@environment.cookbook("apt", "1.2.3")
110+
end
111+
end
112+
101113
describe "to_hash" do
102114
before(:each) do
103115
@environment.name("spec")
@@ -180,22 +192,31 @@
180192
}
181193
end
182194

183-
it "should return true when all versions are strings" do
184-
Chef::Environment.validate_cookbook_versions(@cookbook_versions).should == true
195+
it "should validate the version string of each cookbook" do
196+
@cookbook_versions.each do |cookbook, version|
197+
Chef::Environment.should_receive(:validate_cookbook_version).with(version).and_return true
198+
end
199+
Chef::Environment.validate_cookbook_versions(@cookbook_versions)
185200
end
186201

187-
it "should return false when anything other than a string is passed as a version" do
188-
a = @cookbook_versions.dup.merge :number => 2
189-
Chef::Environment.validate_cookbook_versions(a).should == false
190-
191-
b = @cookbook_versions.dup.merge :hash => {:foo => 'bar'}
192-
Chef::Environment.validate_cookbook_versions(b).should == false
202+
it "should return false if anything other than a hash is passed as the argument" do
203+
Chef::Environment.validate_cookbook_versions(Array.new).should == false
204+
Chef::Environment.validate_cookbook_versions(42).should == false
205+
Chef::Environment.validate_cookbook_versions(Chef::CookbookVersion.new("meta")).should == false
206+
Chef::Environment.validate_cookbook_versions("cookbook => 1.2.3").should == false
207+
end
208+
end
193209

194-
c = @cookbook_versions.dup.merge :array => ['this', 'is', 'a', 'bad', 'version']
195-
Chef::Environment.validate_cookbook_versions(c).should == false
210+
describe "self.validate_cookbook_version" do
211+
it "should return true when the value is a string" do
212+
Chef::Environment.validate_cookbook_version("1.2.3").should == true
213+
end
196214

197-
d = @cookbook_versions.dup.merge :cookbook_versions => Chef::CookbookVersion.new("meta")
198-
Chef::Environment.validate_cookbook_versions(d).should == false
215+
it "should return false when anything other than a string is passed as a version" do
216+
Chef::Environment.validate_cookbook_version(42).should == false
217+
Chef::Environment.validate_cookbook_version(Hash.new).should == false
218+
Chef::Environment.validate_cookbook_version(Array.new).should == false
219+
Chef::Environment.validate_cookbook_version(Chef::CookbookVersion.new("meta")).should == false
199220
end
200221
end
201222
end
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#
2+
# Author:: Stephen Delano (<stephen@ospcode.com>)
3+
# Copyright:: Copyright (c) 2010 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+
19+
require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
20+
21+
describe Chef::Knife::EnvironmentFromFile do
22+
before(:each) do
23+
@knife = Chef::Knife::EnvironmentFromFile.new
24+
@knife.stub!(:msg).and_return true
25+
@knife.stub!(:output).and_return true
26+
@knife.stub!(:show_usage).and_return true
27+
@knife.name_args = [ "spec.rb" ]
28+
29+
@environment = Chef::Environment.new
30+
@environment.name("spec")
31+
@environment.description("runs the unit tests")
32+
@environment.cookbook_versions({"apt" => "1.2.3"})
33+
@environment.stub!(:save).and_return true
34+
@knife.stub!(:load_from_file).and_return @environment
35+
end
36+
37+
describe "run" do
38+
it "should load from a file" do
39+
@knife.should_receive(:load_from_file)
40+
@knife.run
41+
end
42+
43+
it "should save the environment" do
44+
@environment.should_receive(:save)
45+
@knife.run
46+
end
47+
48+
it "should not print the environment" do
49+
@knife.should_not_receive(:output)
50+
@knife.run
51+
end
52+
53+
it "should show usage and exit if not filename is provided" do
54+
@knife.name_args = []
55+
@knife.should_receive(:show_usage)
56+
lambda { @knife.run }.should raise_error(SystemExit)
57+
end
58+
59+
describe "with --print-after" do
60+
it "should pretty print the environment, formatted for display" do
61+
@knife.config[:print_after] = true
62+
@knife.should_receive(:output)
63+
@knife.run
64+
end
65+
end
66+
end
67+
end

0 commit comments

Comments
 (0)