diff options
author | danielsdeleo <dan@opscode.com> | 2013-10-14 12:14:47 -0700 |
---|---|---|
committer | danielsdeleo <dan@opscode.com> | 2013-10-16 13:53:37 -0700 |
commit | a142843fe5637faed05669e098dc296642b30331 (patch) | |
tree | a218da2b2576308d8c9bea23c8a48ceab2956e81 /spec | |
parent | 939e4fe1f8d27dba77ef8208ad792860d9766b9e (diff) | |
download | chef-a142843fe5637faed05669e098dc296642b30331.tar.gz |
Move config fetching tests to their own file.
Diffstat (limited to 'spec')
-rw-r--r-- | spec/unit/application/client_spec.rb | 46 | ||||
-rw-r--r-- | spec/unit/application/solo_spec.rb | 46 | ||||
-rw-r--r-- | spec/unit/config_fetcher_spec.rb | 96 |
3 files changed, 116 insertions, 72 deletions
diff --git a/spec/unit/application/client_spec.rb b/spec/unit/application/client_spec.rb index 25ed8b36b6..c4387890cd 100644 --- a/spec/unit/application/client_spec.rb +++ b/spec/unit/application/client_spec.rb @@ -69,45 +69,19 @@ describe Chef::Application::Client, "reconfigure" do describe "when the json_attribs configuration option is specified" do - describe "and the json_attribs matches a HTTP regex" do - before do - @json = {:a=>"b"}.to_json - @http = double("Chef::HTTP::Simple", :get => @json) - - Chef::Config[:json_attribs] = "https://foo.com/foo.json" - Chef::HTTP::Simple.should_receive(:new).with("https://foo.com/foo.json").and_return(@http) - end - - it "should perform a RESTful GET on the supplied URL" do - @app.reconfigure - @app.chef_client_json.should == {"a" => "b"} - end - end + let(:json_attribs) { {"a" => "b"} } + let(:config_fetcher) { double(Chef::ConfigFetcher, :fetch_json => json_attribs) } + let(:json_source) { "https://foo.com/foo.json" } - describe "and the json_attribs does not match the HTTP regex" do - before do - Chef::Config[:json_attribs] = "/etc/chef/dna.json" - @json = {:a=>"b"}.to_json - ::File.should_receive(:read).with("/etc/chef/dna.json").and_return(@json) - end - - it "should parse the json out of the file" do - @app.reconfigure - @app.chef_client_json.should == {"a" => "b"} - end + before do + Chef::Config[:json_attribs] = json_source + Chef::ConfigFetcher.should_receive(:new).with(json_source). + and_return(config_fetcher) end - describe "when parsing fails" do - before do - Chef::Config[:json_attribs] = "/etc/chef/dna.json" - @json = %q[{"syntax-error": "missing quote}] - ::File.should_receive(:read).with("/etc/chef/dna.json").and_return(@json) - end - - it "should hard fail the application" do - Chef::Application.should_receive(:fatal!).with(%r[Could not parse the provided JSON file \(/etc/chef/dna.json\)], 2) - @app.reconfigure - end + it "reads the JSON attributes from the specified source" do + @app.reconfigure + @app.chef_client_json.should == json_attribs end end end diff --git a/spec/unit/application/solo_spec.rb b/spec/unit/application/solo_spec.rb index 5fe5ec9702..6dd8c8f147 100644 --- a/spec/unit/application/solo_spec.rb +++ b/spec/unit/application/solo_spec.rb @@ -48,45 +48,19 @@ describe Chef::Application::Solo do describe "when the json_attribs configuration option is specified" do - describe "and the json_attribs matches a HTTP regex" do - before do - @json = {:a=>"b"}.to_json - @http = double("Chef::HTTP::Simple", :get => @json) - - Chef::Config[:json_attribs] = "https://foo.com/foo.json" - Chef::HTTP::Simple.should_receive(:new).with("https://foo.com/foo.json").and_return(@http) - end - - it "should perform a RESTful GET on the supplied URL" do - @app.reconfigure - @app.chef_solo_json.should == {"a" => "b"} - end - end + let(:json_attribs) { {"a" => "b"} } + let(:config_fetcher) { double(Chef::ConfigFetcher, :fetch_json => json_attribs) } + let(:json_source) { "https://foo.com/foo.json" } - describe "and the json_attribs does not match the HTTP regex" do - before do - Chef::Config[:json_attribs] = "/etc/chef/dna.json" - @json = {:a=>"b"}.to_json - ::File.should_receive(:read).with("/etc/chef/dna.json").and_return(@json) - end - - it "should parse the json out of the file" do - @app.reconfigure - @app.chef_solo_json.should == {"a" => "b"} - end + before do + Chef::Config[:json_attribs] = json_source + Chef::ConfigFetcher.should_receive(:new).with(json_source). + and_return(config_fetcher) end - describe "when parsing fails" do - before do - Chef::Config[:json_attribs] = "/etc/chef/dna.json" - @json = %q[{"syntax-error": "missing quote}] - ::File.should_receive(:read).with("/etc/chef/dna.json").and_return(@json) - end - - it "should hard fail the application" do - Chef::Application.should_receive(:fatal!).with(%r[Could not parse the provided JSON file \(/etc/chef/dna.json\)], 2) - @app.reconfigure - end + it "reads the JSON attributes from the specified source" do + @app.reconfigure + @app.chef_solo_json.should == json_attribs end end diff --git a/spec/unit/config_fetcher_spec.rb b/spec/unit/config_fetcher_spec.rb new file mode 100644 index 0000000000..f6d5436a11 --- /dev/null +++ b/spec/unit/config_fetcher_spec.rb @@ -0,0 +1,96 @@ +require 'spec_helper' +require 'chef/config_fetcher' +describe Chef::ConfigFetcher do + let(:valid_json) { {:a=>"b"}.to_json } + let(:invalid_json) { %q[{"syntax-error": "missing quote}] } + let(:http) { double("Chef::HTTP::Simple") } + + let(:config_location_regex) { Regexp.escape(config_location) } + let(:invalid_json_error_regex) { %r[Could not parse the provided JSON file \(#{config_location_regex}\)] } + + let(:fetcher) { Chef::ConfigFetcher.new(config_location) } + + context "when loading a local file" do + let(:config_location) { "/etc/chef/client.rb" } + let(:config_content) { "# The client.rb content" } + + it "reads the file from disk" do + ::File.should_receive(:read). + with(config_location). + and_return(config_content) + fetcher.read_config.should == config_content + end + + context "and consuming JSON" do + + let(:config_location) { "/etc/chef/first-boot.json" } + + + it "returns the parsed JSON" do + ::File.should_receive(:read). + with(config_location). + and_return(valid_json) + + fetcher.fetch_json.should == {"a" => "b"} + end + + context "and the JSON is invalid" do + + it "reports the JSON error" do + + + ::File.should_receive(:read). + with(config_location). + and_return(invalid_json) + + Chef::Application.should_receive(:fatal!). + with(invalid_json_error_regex, 2) + fetcher.fetch_json + end + end + end + + end + + context "when loading a file over HTTP" do + + let(:config_location) { "https://example.com/client.rb" } + let(:config_content) { "# The client.rb content" } + + before do + Chef::HTTP::Simple.should_receive(:new). + with(config_location). + and_return(http) + end + + it "reads the file over HTTP" do + http.should_receive(:get). + with("").and_return(config_content) + fetcher.read_config.should == config_content + end + + context "and consuming JSON" do + let(:config_location) { "https://example.com/foo.json" } + + it "fetches the file and parses it" do + http.should_receive(:get). + with("").and_return(valid_json) + fetcher.fetch_json.should == {"a" => "b"} + end + + context "and the JSON is invalid" do + it "reports the JSON error" do + http.should_receive(:get). + with("").and_return(invalid_json) + + Chef::Application.should_receive(:fatal!). + with(invalid_json_error_regex, 2) + fetcher.fetch_json + end + end + end + + end + + +end |