Skip to content

Commit 7f5020f

Browse files
author
AJ Christensen
committed
CHEF-350: Make the solo application load JSON correctly.
Specs for this behaviour in both the solo app and the client app.
1 parent ce9df19 commit 7f5020f

3 files changed

Lines changed: 81 additions & 1 deletion

File tree

‎chef/lib/chef/application/solo.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def reconfigure
9090
end
9191

9292
begin
93-
@chef_client_json = JSON.parse(json_io.read)
93+
@chef_solo_json = JSON.parse(json_io.read)
9494
rescue JSON::ParserError => error
9595
Chef::Application.fatal!("Could not parse the provided JSON file (#{Chef::Config[:json_attribs]})!: " + error.message, 2)
9696
end

‎chef/spec/unit/application/client_spec.rb‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,18 @@
8383
JSON.should_receive(:parse).with(@json.read)
8484
@app.reconfigure
8585
end
86+
87+
describe "when parsing fails" do
88+
before do
89+
JSON.stub!(:parse).with(@json.read).and_raise(JSON::ParserError)
90+
Chef::Application.stub!(:fatal!).and_return(true)
91+
end
92+
93+
it "should hard fail the application" do
94+
Chef::Application.should_receive(:fatal!).with("Could not parse the provided JSON file (/etc/chef/dna.json)!: JSON::ParserError", 2).and_return(true)
95+
@app.reconfigure
96+
end
97+
end
8698
end
8799
end
88100

@@ -91,10 +103,42 @@
91103
@chef_client = mock("Chef::Client", :null_object => true)
92104
Chef::Client.stub!(:new).and_return(@chef_client)
93105
@app = Chef::Application::Client.new
106+
# this is all stuff the reconfigure method needs
107+
@app.stub!(:configure_opt_parser).and_return(true)
108+
@app.stub!(:configure_chef).and_return(true)
109+
@app.stub!(:configure_logging).and_return(true)
110+
Chef::Config.stub!(:[]).with(:interval).and_return(false)
111+
Chef::Config.stub!(:[]).with(:splay).and_return(false)
112+
Chef::Config.stub!(:[]).with(:recipe_url).and_return(false)
113+
Chef::Config.stub!(:[]).with(:json_attribs).and_return("/etc/chef/dna.json")
114+
@json = mock("Tempfile", :read => {:a=>"b"}.to_json, :null_object => true)
115+
@app.stub!(:open).with("/etc/chef/dna.json").and_return(@json)
94116
end
95117

96118
it "should instantiate a chef::client object" do
97119
Chef::Client.should_receive(:new).and_return(@chef_client)
98120
@app.setup_application
99121
end
122+
123+
it "should assign the json attributes to the chef client instance" do
124+
@chef_client.should_receive(:json_attribs=).with({"a"=>"b"}).and_return(true)
125+
@app.reconfigure
126+
@app.setup_application
127+
end
128+
129+
it "should assign the validation token to the chef client instance" do
130+
Chef::Config.stub!(:[]).with(:validation_token).and_return("testtoken")
131+
@chef_client.should_receive(:validation_token=).with("testtoken").and_return(true)
132+
@app.setup_application
133+
end
134+
135+
it "should assign the node name to the chef client instance" do
136+
Chef::Config.stub!(:[]).with(:node_name).and_return("testnode")
137+
@chef_client.should_receive(:node_name=).with("testnode").and_return(true)
138+
@app.setup_application
139+
end
140+
141+
after do
142+
Chef::Config[:solo] = false
143+
end
100144
end

‎chef/spec/unit/application/solo_spec.rb‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@
5252
JSON.should_receive(:parse).with(@json.read)
5353
@app.reconfigure
5454
end
55+
56+
describe "when parsing fails" do
57+
before do
58+
JSON.stub!(:parse).with(@json.read).and_raise(JSON::ParserError)
59+
Chef::Application.stub!(:fatal!).and_return(true)
60+
end
61+
62+
it "should hard fail the application" do
63+
Chef::Application.should_receive(:fatal!).with("Could not parse the provided JSON file (/etc/chef/dna.json)!: JSON::ParserError", 2).and_return(true)
64+
@app.reconfigure
65+
end
66+
end
5567
end
5668

5769
describe "when the recipe_url configuration option is specified" do
@@ -95,10 +107,34 @@
95107
@chef_client = mock("Chef::Client", :null_object => true)
96108
Chef::Client.stub!(:new).and_return(@chef_client)
97109
@app = Chef::Application::Solo.new
110+
# this is all stuff the reconfigure method needs
111+
@app.stub!(:configure_opt_parser).and_return(true)
112+
@app.stub!(:configure_chef).and_return(true)
113+
@app.stub!(:configure_logging).and_return(true)
114+
Chef::Config.stub!(:[]).with(:recipe_url).and_return(false)
115+
Chef::Config.stub!(:[]).with(:json_attribs).and_return("/etc/chef/dna.json")
116+
@json = mock("Tempfile", :read => {:a=>"b"}.to_json, :null_object => true)
117+
@app.stub!(:open).with("/etc/chef/dna.json").and_return(@json)
98118
end
99119

100120
it "should instantiate a chef::client object" do
101121
Chef::Client.should_receive(:new).and_return(@chef_client)
102122
@app.setup_application
103123
end
124+
125+
it "should assign the json attributes to the chef client instance" do
126+
@chef_client.should_receive(:json_attribs=).with({"a"=>"b"}).and_return(true)
127+
@app.reconfigure
128+
@app.setup_application
129+
end
130+
131+
it "should assign the node name to the chef client instance" do
132+
Chef::Config.stub!(:[]).with(:node_name).and_return("testnode")
133+
@chef_client.should_receive(:node_name=).with("testnode").and_return(true)
134+
@app.setup_application
135+
end
136+
137+
after do
138+
Chef::Config[:solo] = false
139+
end
104140
end

0 commit comments

Comments
 (0)