summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorroot <root@sfdchef04.military.com>2010-01-16 13:53:34 +0000
committerroot <root@sfdchef04.military.com>2010-01-16 13:53:34 +0000
commitc18fc522a72ef8bfbe1c59bbfe5ce17b7a1e2c45 (patch)
treefcd8479f853c4ce7598be91772816745d72bb1cd
parent26ba7ba47b97b2ae04b6e4f6502156c64990e893 (diff)
parent73d3bcddea38209905110a7927449cf9a117857c (diff)
downloadchef-c18fc522a72ef8bfbe1c59bbfe5ce17b7a1e2c45.tar.gz
Merge branch 'master' of git://github.com/opscode/chef into CHEF-859
-rw-r--r--chef-server-api/app/controllers/data_item.rb2
-rw-r--r--chef/lib/chef/application/client.rb12
-rw-r--r--chef/lib/chef/application/solo.rb12
-rw-r--r--chef/lib/chef/mixin/params_validate.rb37
-rw-r--r--chef/lib/chef/provider/ruby_block.rb1
-rw-r--r--chef/lib/chef/provider/template.rb2
-rw-r--r--chef/spec/unit/application/client_spec.rb43
-rw-r--r--chef/spec/unit/application/solo_spec.rb46
-rw-r--r--chef/spec/unit/mixin/params_validate_spec.rb32
-rw-r--r--chef/spec/unit/provider/template_spec.rb7
10 files changed, 155 insertions, 39 deletions
diff --git a/chef-server-api/app/controllers/data_item.rb b/chef-server-api/app/controllers/data_item.rb
index e03bd54522..edd03803bd 100644
--- a/chef-server-api/app/controllers/data_item.rb
+++ b/chef-server-api/app/controllers/data_item.rb
@@ -27,7 +27,7 @@ class ChefServerApi::DataItem < ChefServerApi::Application
before :populate_data_bag
before :authenticate_every
- before :is_admin, :only => [ :create, :destroy ]
+ before :is_admin, :only => [ :create, :update, :destroy ]
def populate_data_bag
begin
diff --git a/chef/lib/chef/application/client.rb b/chef/lib/chef/application/client.rb
index a058f9ddc1..4b19335880 100644
--- a/chef/lib/chef/application/client.rb
+++ b/chef/lib/chef/application/client.rb
@@ -21,8 +21,7 @@ require 'chef/client'
require 'chef/config'
require 'chef/daemon'
require 'chef/log'
-require 'net/http'
-require 'open-uri'
+require 'chef/rest'
class Chef::Application::Client < Chef::Application
@@ -142,7 +141,13 @@ class Chef::Application::Client < Chef::Application
if Chef::Config[:json_attribs]
begin
- json_io = open(Chef::Config[:json_attribs])
+ json_io = case Chef::Config[:json_attribs]
+ when /^(http|https):\/\//
+ @rest = Chef::REST.new(Chef::Config[:json_attribs], nil, nil)
+ @rest.get_rest(Chef::Config[:json_attribs], true).open
+ else
+ open(Chef::Config[:json_attribs])
+ end
rescue SocketError => error
Chef::Application.fatal!("I cannot connect to #{Chef::Config[:json_attribs]}", 2)
rescue Errno::ENOENT => error
@@ -155,6 +160,7 @@ class Chef::Application::Client < Chef::Application
begin
@chef_client_json = JSON.parse(json_io.read)
+ json_io.close unless json_io.closed?
rescue JSON::ParserError => error
Chef::Application.fatal!("Could not parse the provided JSON file (#{Chef::Config[:json_attribs]})!: " + error.message, 2)
end
diff --git a/chef/lib/chef/application/solo.rb b/chef/lib/chef/application/solo.rb
index 74e46aab35..43cece2c4c 100644
--- a/chef/lib/chef/application/solo.rb
+++ b/chef/lib/chef/application/solo.rb
@@ -20,8 +20,7 @@ require 'chef/client'
require 'chef/config'
require 'chef/daemon'
require 'chef/log'
-require 'net/http'
-require 'open-uri'
+require 'chef/rest'
require 'fileutils'
class Chef::Application::Solo < Chef::Application
@@ -132,7 +131,13 @@ class Chef::Application::Solo < Chef::Application
if Chef::Config[:json_attribs]
begin
- json_io = open(Chef::Config[:json_attribs])
+ json_io = case Chef::Config[:json_attribs]
+ when /^(http|https):\/\//
+ @rest = Chef::REST.new(Chef::Config[:json_attribs], nil, nil)
+ @rest.get_rest(Chef::Config[:json_attribs], true).open
+ else
+ open(Chef::Config[:json_attribs])
+ end
rescue SocketError => error
Chef::Application.fatal!("I cannot connect to #{Chef::Config[:json_attribs]}", 2)
rescue Errno::ENOENT => error
@@ -145,6 +150,7 @@ class Chef::Application::Solo < Chef::Application
begin
@chef_solo_json = JSON.parse(json_io.read)
+ json_io.close unless json_io.closed?
rescue JSON::ParserError => error
Chef::Application.fatal!("Could not parse the provided JSON file (#{Chef::Config[:json_attribs]})!: " + error.message, 2)
end
diff --git a/chef/lib/chef/mixin/params_validate.rb b/chef/lib/chef/mixin/params_validate.rb
index c7ab7139bb..e978f04171 100644
--- a/chef/lib/chef/mixin/params_validate.rb
+++ b/chef/lib/chef/mixin/params_validate.rb
@@ -80,11 +80,12 @@ class Chef
map = {
symbol => validation
}
- if arg == nil
+
+ if arg == nil && self.instance_variable_defined?(iv_symbol) == true
self.instance_variable_get(iv_symbol)
else
- validate({ symbol => arg }, { symbol => validation })
- self.instance_variable_set(iv_symbol, arg)
+ opts = validate({ symbol => arg }, { symbol => validation })
+ self.instance_variable_set(iv_symbol, opts[symbol])
end
end
@@ -104,7 +105,8 @@ class Chef
# Raise an exception if the parameter is not found.
def _pv_required(opts, key, is_required=true)
if is_required
- if opts.has_key?(key.to_s) || opts.has_key?(key.to_sym)
+ if (opts.has_key?(key.to_s) && opts[key.to_s] != nil) ||
+ (opts.has_key?(key.to_sym) && opts[key.to_sym] != nil)
true
else
raise ArgumentError, "Required argument #{key} is missing!"
@@ -166,16 +168,18 @@ class Chef
# Check a parameter against a regular expression.
def _pv_regex(opts, key, regex)
value = _pv_opts_lookup(opts, key)
- passes = false
- [ regex ].flatten.each do |r|
- if value != nil
- if r.match(value.to_s)
- passes = true
+ if value != nil
+ passes = false
+ [ regex ].flatten.each do |r|
+ if value != nil
+ if r.match(value.to_s)
+ passes = true
+ end
end
end
- end
- unless passes
- raise ArgumentError, "Option #{key}'s value #{value} does not match regular expression #{regex.to_s}"
+ unless passes
+ raise ArgumentError, "Option #{key}'s value #{value} does not match regular expression #{regex.to_s}"
+ end
end
end
@@ -191,6 +195,15 @@ class Chef
end
end
end
+
+ # Allow a parameter to default to @name
+ def _pv_name_attribute(opts, key, is_name_attribute=true)
+ if is_name_attribute
+ if opts[key] == nil
+ opts[key] = self.instance_variable_get("@name")
+ end
+ end
+ end
end
end
end
diff --git a/chef/lib/chef/provider/ruby_block.rb b/chef/lib/chef/provider/ruby_block.rb
index 76b180a0fb..d8d8f1bc20 100644
--- a/chef/lib/chef/provider/ruby_block.rb
+++ b/chef/lib/chef/provider/ruby_block.rb
@@ -21,7 +21,6 @@ class Chef
class Provider
class RubyBlock < Chef::Provider
def load_current_resource
- Chef::Log.debug(@new_resource.inspect)
true
end
diff --git a/chef/lib/chef/provider/template.rb b/chef/lib/chef/provider/template.rb
index 026148646c..abe2d62a34 100644
--- a/chef/lib/chef/provider/template.rb
+++ b/chef/lib/chef/provider/template.rb
@@ -105,7 +105,7 @@ class Chef
end
def cookbook_name
- @cookbook_name ||= (@new_resource.cookbook || @new_resource.cookbook_name)
+ @cookbook_name = (@new_resource.cookbook || @new_resource.cookbook_name)
end
def render_with_context(cache_file_name)
diff --git a/chef/spec/unit/application/client_spec.rb b/chef/spec/unit/application/client_spec.rb
index b64dff4fb3..509c6f3001 100644
--- a/chef/spec/unit/application/client_spec.rb
+++ b/chef/spec/unit/application/client_spec.rb
@@ -56,19 +56,46 @@ describe Chef::Application::Client, "reconfigure" do
end
describe "when the json_attribs configuration option is specified" do
- before do
- Chef::Config.stub!(:[]).with(:json_attribs).and_return("/etc/chef/dna.json")
- @json = mock("Tempfile", :read => {:a=>"b"}.to_json, :null_object => true)
- @app.stub!(:open).with("/etc/chef/dna.json").and_return(@json)
+
+ describe "and the json_attribs matches a HTTP regex" do
+ before do
+ @json = mock("Tempfile", :read => {:a=>"b"}.to_json, :null_object => true)
+ @rest = mock("Chef::REST", :get_rest => @json, :null_object => true)
+
+ Chef::Config.stub!(:[]).with(:json_attribs).and_return("https://foo.com/foo.json")
+ Chef::REST.stub!(:new).with("https://foo.com/foo.json", nil, nil).and_return(@rest)
+ @app.stub!(:open).with("/etc/chef/dna.json").and_return(@json)
+ end
+
+ it "should create a new Chef::REST" do
+ Chef::REST.should_receive(:new).with("https://foo.com/foo.json", nil, nil).and_return(@rest)
+ @app.reconfigure
+ end
+
+ it "should perform a RESTful GET on the supplied URL" do
+ @rest.should_receive(:get_rest).with("https://foo.com/foo.json", true).and_return(@json)
+ @app.reconfigure
+ end
end
-
- it "should parse the json out of the file" do
- JSON.should_receive(:parse).with(@json.read)
- @app.reconfigure
+
+ describe "and the json_attribs does not match the HTTP regex" do
+ before do
+ Chef::Config.stub!(:[]).with(:json_attribs).and_return("/etc/chef/dna.json")
+ @json = mock("Tempfile", :read => {:a=>"b"}.to_json, :null_object => true)
+ @app.stub!(:open).with("/etc/chef/dna.json").and_return(@json)
+ end
+
+ it "should parse the json out of the file" do
+ JSON.should_receive(:parse).with(@json.read)
+ @app.reconfigure
+ end
end
describe "when parsing fails" do
before do
+ Chef::Config.stub!(:[]).with(:json_attribs).and_return("/etc/chef/dna.json")
+ @json = mock("Tempfile", :read => {:a=>"b"}.to_json, :null_object => true)
+ @app.stub!(:open).with("/etc/chef/dna.json").and_return(@json)
JSON.stub!(:parse).with(@json.read).and_raise(JSON::ParserError)
Chef::Application.stub!(:fatal!).and_return(true)
end
diff --git a/chef/spec/unit/application/solo_spec.rb b/chef/spec/unit/application/solo_spec.rb
index a717ec5d58..bf664d4e32 100644
--- a/chef/spec/unit/application/solo_spec.rb
+++ b/chef/spec/unit/application/solo_spec.rb
@@ -65,21 +65,47 @@ describe Chef::Application::Solo, "reconfigure" do
end
end
-
describe "when the json_attribs configuration option is specified" do
- before do
- Chef::Config.stub!(:[]).with(:json_attribs).and_return("/etc/chef/dna.json")
- @json = mock("Tempfile", :read => {:a=>"b"}.to_json, :null_object => true)
- @app.stub!(:open).with("/etc/chef/dna.json").and_return(@json)
+
+ describe "and the json_attribs matches a HTTP regex" do
+ before do
+ @json = mock("Tempfile", :read => {:a=>"b"}.to_json, :null_object => true)
+ @rest = mock("Chef::REST", :get_rest => @json, :null_object => true)
+
+ Chef::Config.stub!(:[]).with(:json_attribs).and_return("https://foo.com/foo.json")
+ Chef::REST.stub!(:new).with("https://foo.com/foo.json", nil, nil).and_return(@rest)
+ @app.stub!(:open).with("/etc/chef/dna.json").and_return(@json)
+ end
+
+ it "should create a new Chef::REST" do
+ Chef::REST.should_receive(:new).with("https://foo.com/foo.json", nil, nil).and_return(@rest)
+ @app.reconfigure
+ end
+
+ it "should perform a RESTful GET on the supplied URL" do
+ @rest.should_receive(:get_rest).with("https://foo.com/foo.json", true).and_return(@json)
+ @app.reconfigure
+ end
end
-
- it "should parse the json out of the file" do
- JSON.should_receive(:parse).with(@json.read)
- @app.reconfigure
+
+ describe "and the json_attribs does not match the HTTP regex" do
+ before do
+ Chef::Config.stub!(:[]).with(:json_attribs).and_return("/etc/chef/dna.json")
+ @json = mock("Tempfile", :read => {:a=>"b"}.to_json, :null_object => true)
+ @app.stub!(:open).with("/etc/chef/dna.json").and_return(@json)
+ end
+
+ it "should parse the json out of the file" do
+ JSON.should_receive(:parse).with(@json.read)
+ @app.reconfigure
+ end
end
describe "when parsing fails" do
before do
+ Chef::Config.stub!(:[]).with(:json_attribs).and_return("/etc/chef/dna.json")
+ @json = mock("Tempfile", :read => {:a=>"b"}.to_json, :null_object => true)
+ @app.stub!(:open).with("/etc/chef/dna.json").and_return(@json)
JSON.stub!(:parse).with(@json.read).and_raise(JSON::ParserError)
Chef::Application.stub!(:fatal!).and_return(true)
end
@@ -90,6 +116,8 @@ describe Chef::Application::Solo, "reconfigure" do
end
end
end
+
+
describe "when the recipe_url configuration option is specified" do
before do
diff --git a/chef/spec/unit/mixin/params_validate_spec.rb b/chef/spec/unit/mixin/params_validate_spec.rb
index b5e68e04ec..79882f2932 100644
--- a/chef/spec/unit/mixin/params_validate_spec.rb
+++ b/chef/spec/unit/mixin/params_validate_spec.rb
@@ -327,5 +327,35 @@ describe Chef::Mixin::ParamsValidate do
)
}.should raise_error(ArgumentError)
end
+
+ it "should set and return a value, then return the same value" do
+ value = "meow"
+ @vo.set_or_return(:test, value, {}).object_id.should == value.object_id
+ @vo.set_or_return(:test, nil, {}).object_id.should == value.object_id
+ end
+
+ it "should set and return a default value when the argument is nil, then return the same value" do
+ value = "meow"
+ @vo.set_or_return(:test, nil, { :default => value }).object_id.should == value.object_id
+ @vo.set_or_return(:test, nil, {}).object_id.should == value.object_id
+ end
+
+ it "should raise an ArgumentError when argument is nil and required is true" do
+ lambda {
+ @vo.set_or_return(:test, nil, { :required => true })
+ }.should raise_error(ArgumentError)
+ end
+
+ it "should not raise an error when argument is nil and required is false" do
+ lambda {
+ @vo.set_or_return(:test, nil, { :required => false })
+ }.should_not raise_error(ArgumentError)
+ end
+
+ it "should set and return @name, then return @name for foo when argument is nil" do
+ value = "meow"
+ @vo.set_or_return(:name, value, { }).object_id.should == value.object_id
+ @vo.set_or_return(:foo, nil, { :name_attribute => true }).object_id.should == value.object_id
+ end
-end \ No newline at end of file
+end
diff --git a/chef/spec/unit/provider/template_spec.rb b/chef/spec/unit/provider/template_spec.rb
index f2b33a815a..1826415eb8 100644
--- a/chef/spec/unit/provider/template_spec.rb
+++ b/chef/spec/unit/provider/template_spec.rb
@@ -69,6 +69,13 @@ describe Chef::Provider::Template do
do_action_create
end
+ it "should use the cookbook name if defined in the template resource" do
+ @resource.cookbook "jane"
+ @resource.source "template.erb"
+ @provider.should_receive(:fetch_template_via_rest).with("cookbooks/jane/templates/default/template.erb", "jane_template.erb")
+ do_action_create
+ end
+
it "should set the checksum of the new resource to the value of the returned template" do
@resource.should_receive(:checksum).with("0fd012fdc96e96f8f7cf2046522a54aed0ce470224513e45da6bc1a17a4924aa").once
@resource.should_receive(:checksum).twice