summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordanielsdeleo <dan@chef.io>2016-01-26 13:32:29 -0800
committerdanielsdeleo <dan@chef.io>2016-01-27 12:38:54 -0800
commit05d98b354ab995fefd9ef6bc6d3e19bb5f960ad5 (patch)
treebf75f47b1b87ddbfddcb9100a803716eca1b65fe
parent6defab990c91e07361cc27a41d506338ca915527 (diff)
downloadchef-05d98b354ab995fefd9ef6bc6d3e19bb5f960ad5.tar.gz
Force config file setting to absolute path in chef-client
-rw-r--r--lib/chef/application.rb7
-rw-r--r--lib/chef/config_fetcher.rb8
-rw-r--r--lib/chef/run_lock.rb2
-rw-r--r--spec/unit/config_fetcher_spec.rb75
4 files changed, 69 insertions, 23 deletions
diff --git a/lib/chef/application.rb b/lib/chef/application.rb
index a4d4fc209d..659c4b2f4d 100644
--- a/lib/chef/application.rb
+++ b/lib/chef/application.rb
@@ -90,6 +90,13 @@ class Chef
# Parse the config file
def load_config_file
config_fetcher = Chef::ConfigFetcher.new(config[:config_file])
+
+ # Some config settings are derived relative to the config file path; if
+ # given as a relative path, this is computed relative to cwd, but
+ # chef-client will later chdir to root, so we need to get the absolute path
+ # here.
+ config[:config_file] = config_fetcher.expanded_path
+
if config[:config_file].nil?
Chef::Log.warn("No config file found or specified on command line, using command line options.")
elsif config_fetcher.config_missing?
diff --git a/lib/chef/config_fetcher.rb b/lib/chef/config_fetcher.rb
index ac6bdc2249..13f9fc3825 100644
--- a/lib/chef/config_fetcher.rb
+++ b/lib/chef/config_fetcher.rb
@@ -12,6 +12,14 @@ class Chef
@config_location = config_location
end
+ def expanded_path
+ if config_location.nil? || remote_config?
+ config_location
+ else
+ File.expand_path(config_location)
+ end
+ end
+
def fetch_json
config_data = read_config
begin
diff --git a/lib/chef/run_lock.rb b/lib/chef/run_lock.rb
index 42b99440bc..0d94afd15d 100644
--- a/lib/chef/run_lock.rb
+++ b/lib/chef/run_lock.rb
@@ -72,7 +72,7 @@ class Chef
end
end
end
- rescue Timeout::Error => e
+ rescue Timeout::Error
exit_from_timeout
end
else
diff --git a/spec/unit/config_fetcher_spec.rb b/spec/unit/config_fetcher_spec.rb
index 794940c39a..82eca1cf1a 100644
--- a/spec/unit/config_fetcher_spec.rb
+++ b/spec/unit/config_fetcher_spec.rb
@@ -22,6 +22,21 @@ describe Chef::ConfigFetcher do
expect(fetcher.read_config).to eq(config_content)
end
+ it "gives the expanded path to the config file" do
+ expect(fetcher.expanded_path).to eq(config_location)
+ end
+
+ context "with a relative path" do
+
+ let(:config_location) { "client.rb" }
+
+ it "gives the expanded path to the config file" do
+ expected = File.join(Dir.pwd, config_location)
+ expect(fetcher.expanded_path).to eq(expected)
+ end
+
+ end
+
context "and consuming JSON" do
let(:config_location) { "/etc/chef/first-boot.json" }
@@ -53,45 +68,61 @@ describe Chef::ConfigFetcher do
end
- context "when loading a file over HTTP" do
+ context "with an HTTP URL config location" do
let(:config_location) { "https://example.com/client.rb" }
let(:config_content) { "# The client.rb content" }
- before do
- expect(Chef::HTTP::Simple).to receive(:new).
- with(config_location).
- and_return(http)
+ it "returns the config location unchanged for #expanded_path" do
+ expect(fetcher.expanded_path).to eq(config_location)
end
- it "reads the file over HTTP" do
- expect(http).to receive(:get).
- with("").and_return(config_content)
- expect(fetcher.read_config).to eq(config_content)
- end
+ describe "reading the file" do
- context "and consuming JSON" do
- let(:config_location) { "https://example.com/foo.json" }
+ before do
+ expect(Chef::HTTP::Simple).to receive(:new).
+ with(config_location).
+ and_return(http)
+ end
- it "fetches the file and parses it" do
- expect(http).to receive(:get).
- with("").and_return(valid_json)
- expect(fetcher.fetch_json).to eq({"a" => "b"})
+ it "reads the file over HTTP" do
+ expect(http).to receive(:get).
+ with("").and_return(config_content)
+ expect(fetcher.read_config).to eq(config_content)
end
- context "and the JSON is invalid" do
- it "reports the JSON error" do
+ context "and consuming JSON" do
+ let(:config_location) { "https://example.com/foo.json" }
+
+ it "fetches the file and parses it" do
expect(http).to receive(:get).
- with("").and_return(invalid_json)
+ with("").and_return(valid_json)
+ expect(fetcher.fetch_json).to eq({"a" => "b"})
+ end
- expect(Chef::Application).to receive(:fatal!).
- with(invalid_json_error_regex, 2)
- fetcher.fetch_json
+ context "and the JSON is invalid" do
+ it "reports the JSON error" do
+ expect(http).to receive(:get).
+ with("").and_return(invalid_json)
+
+ expect(Chef::Application).to receive(:fatal!).
+ with(invalid_json_error_regex, 2)
+ fetcher.fetch_json
+ end
end
end
end
end
+ context "with a nil config file argument" do
+
+ let(:config_location) { nil }
+
+ it "returns the config location unchanged for #expanded_path" do
+ expect(fetcher.expanded_path).to eq(nil)
+ end
+ end
+
end