summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBryan McLellan <btm@chef.io>2019-04-24 14:23:07 -0400
committerBryan McLellan <btm@loftninjas.org>2019-05-06 12:56:55 -0400
commit00464d7cc3e818ee687cae1db8c087ee95187579 (patch)
treee75dc5c528d3ae96ff4bf1531f848c319688b3b7
parent9d5f5c40362d1fd7b0323cf0880300d6165b3a94 (diff)
downloadchef-00464d7cc3e818ee687cae1db8c087ee95187579.tar.gz
Unit tests for experimental target mode
Signed-off-by: Bryan McLellan <btm@chef.io>
-rw-r--r--Gemfile.lock4
-rw-r--r--chef-config/spec/unit/config_spec.rb64
-rw-r--r--spec/unit/node_map_spec.rb34
-rw-r--r--spec/unit/train_transport_spec.rb79
4 files changed, 174 insertions, 7 deletions
diff --git a/Gemfile.lock b/Gemfile.lock
index f66c654f0c..1059ae74f1 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -55,7 +55,6 @@ PATH
syslog-logger (~> 1.6)
train-core (~> 2.0, >= 2.0.12)
tty-screen (~> 0.6)
- train-core
uuidtools (~> 2.1.5)
chef (15.0.263-universal-mingw32)
addressable
@@ -84,9 +83,8 @@ PATH
plist (~> 3.2)
proxifier (~> 1.0)
syslog-logger (~> 1.6)
- train-core (~> 2.0, >= 2.0.12)
- tty-screen (~> 0.6)
train-core
+ tty-screen (~> 0.6)
uuidtools (~> 2.1.5)
win32-api (~> 1.5.3)
win32-certstore (~> 0.3)
diff --git a/chef-config/spec/unit/config_spec.rb b/chef-config/spec/unit/config_spec.rb
index 0e17753185..378cc4a4a1 100644
--- a/chef-config/spec/unit/config_spec.rb
+++ b/chef-config/spec/unit/config_spec.rb
@@ -248,9 +248,10 @@ RSpec.describe ChefConfig::Config do
end
describe "default values" do
+ let(:system_drive) { ChefConfig::Config.env["SYSTEMDRIVE"] } if is_windows
let :primary_cache_path do
if is_windows
- "#{ChefConfig::Config.env['SYSTEMDRIVE']}\\chef"
+ "#{system_drive}\\chef"
else
"/var/chef"
end
@@ -275,6 +276,35 @@ RSpec.describe ChefConfig::Config do
allow(ChefConfig::Config).to receive(:path_accessible?).and_return(false)
end
+ describe "ChefConfig::Config[:client_key]" do
+ let(:path_to_client_key) { to_platform("/etc/chef") + ChefConfig::PathHelper.path_separator }
+
+ it "sets the default path to the client key" do
+ expect(ChefConfig::Config.client_key).to eq(path_to_client_key + "client.pem")
+ end
+
+ context "when target mode is enabled" do
+ let(:target_mode_host) { "fluffy.kittens.org" }
+
+ before do
+ ChefConfig::Config.target_mode.enabled = true
+ ChefConfig::Config.target_mode.host = target_mode_host
+ end
+
+ it "sets the default path to the client key with the target host name" do
+ expect(ChefConfig::Config.client_key).to eq(path_to_client_key + target_mode_host + ChefConfig::PathHelper.path_separator + "client.pem")
+ end
+ end
+
+ context "when local mode is enabled" do
+ before { ChefConfig::Config[:local_mode] = true }
+
+ it "returns nil" do
+ expect(ChefConfig::Config.client_key).to be_nil
+ end
+ end
+ end
+
describe "ChefConfig::Config[:fips]" do
let(:fips_enabled) { false }
@@ -370,16 +400,32 @@ RSpec.describe ChefConfig::Config do
end
describe "ChefConfig::Config[:cache_path]" do
+ let(:target_mode_host) { "fluffy.kittens.org" }
+ let(:target_mode_primary_cache_path) { "#{primary_cache_path}/#{target_mode_host}" }
+ let(:target_mode_secondary_cache_path) { "#{secondary_cache_path}/#{target_mode_host}" }
+
before do
if is_windows
- allow(File).to receive(:expand_path).and_return("#{ChefConfig::Config.env["SYSTEMDRIVE"]}/Path/To/Executable")
+ allow(File).to receive(:expand_path).and_return("#{system_drive}/Path/To/Executable")
end
end
+
context "when /var/chef exists and is accessible" do
- it "defaults to /var/chef" do
+ before do
allow(ChefConfig::Config).to receive(:path_accessible?).with(to_platform("/var/chef")).and_return(true)
+ end
+
+ it "defaults to /var/chef" do
expect(ChefConfig::Config[:cache_path]).to eq(primary_cache_path)
end
+
+ context "and target mode is enabled" do
+ it "cache path includes the target host name" do
+ ChefConfig::Config.target_mode.enabled = true
+ ChefConfig::Config.target_mode.host = target_mode_host
+ expect(ChefConfig::Config[:cache_path]).to eq(target_mode_primary_cache_path)
+ end
+ end
end
context "when /var/chef does not exist and /var is accessible" do
@@ -399,13 +445,23 @@ RSpec.describe ChefConfig::Config do
end
context "when /var/chef exists and is not accessible" do
- it "defaults to $HOME/.chef" do
+ before do
allow(File).to receive(:exists?).with(to_platform("/var/chef")).and_return(true)
allow(File).to receive(:readable?).with(to_platform("/var/chef")).and_return(true)
allow(File).to receive(:writable?).with(to_platform("/var/chef")).and_return(false)
+ end
+ it "defaults to $HOME/.chef" do
expect(ChefConfig::Config[:cache_path]).to eq(secondary_cache_path)
end
+
+ context "and target mode is enabled" do
+ it "cache path defaults to $HOME/.chef with the target host name" do
+ ChefConfig::Config.target_mode.enabled = true
+ ChefConfig::Config.target_mode.host = target_mode_host
+ expect(ChefConfig::Config[:cache_path]).to eq(target_mode_secondary_cache_path)
+ end
+ end
end
context "when chef is running in local mode" do
diff --git a/spec/unit/node_map_spec.rb b/spec/unit/node_map_spec.rb
index d486d913aa..7c867857dc 100644
--- a/spec/unit/node_map_spec.rb
+++ b/spec/unit/node_map_spec.rb
@@ -210,6 +210,40 @@ describe Chef::NodeMap do
end
end
+ # When in target mode, only match when target_mode is explicitly supported
+ context "when target mode is enabled" do
+ before do
+ allow(Chef::Config).to receive(:target_mode?).and_return(true)
+ end
+
+ it "returns the value when target_mode matches" do
+ node_map.set(:something, :network, target_mode: true)
+ expect(node_map.get(node, :something)).to eql(:network)
+ end
+
+ it "returns nil when target_mode does not match" do
+ node_map.set(:something, :local, target_mode: false)
+ expect(node_map.get(node, :something)).to eql(nil)
+ end
+ end
+
+ # When not in target mode, match regardless of target_mode filter
+ context "when target mode is not enabled" do
+ before do
+ allow(Chef::Config).to receive(:target_mode?).and_return(false)
+ end
+
+ it "returns the value if target_mode matches" do
+ node_map.set(:something, :local, target_mode: true)
+ expect(node_map.get(node, :something)).to eql(:local)
+ end
+
+ it "returns the value if target_mode does not match" do
+ node_map.set(:something, :local, target_mode: false)
+ expect(node_map.get(node, :something)).to eql(:local)
+ end
+ end
+
describe "locked mode" do
context "while unlocked" do
it "allows setting the same key twice" do
diff --git a/spec/unit/train_transport_spec.rb b/spec/unit/train_transport_spec.rb
new file mode 100644
index 0000000000..f24c0aaf0e
--- /dev/null
+++ b/spec/unit/train_transport_spec.rb
@@ -0,0 +1,79 @@
+#
+# Author:: Bryan McLellan (<btm@loftninjas.org>)
+# Copyright:: Copyright 2019, Chef Software Inc.
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+require "spec_helper"
+
+describe Chef::TrainTransport do
+ describe "load_credentials" do
+ let(:transport) { Chef::TrainTransport.new }
+ let(:good_credentials) { Mash.from_hash({ "switch.cisco.com": { user: "cisco", password: "cisco", enable_password: "secret" } }) }
+
+ before do
+ allow(Chef::TrainTransport).to receive(:parse_credentials_file).and_return(good_credentials)
+ end
+
+ it "matches credentials when they exist" do
+ expect(Chef::TrainTransport.load_credentials("switch.cisco.com")[:user]).to eq("cisco")
+ expect(Chef::TrainTransport.load_credentials("switch.cisco.com")[:password]).to eq("cisco")
+ expect(Chef::TrainTransport.load_credentials("switch.cisco.com")[:enable_password]).to eq("secret")
+ end
+
+ it "returns nil if there is no match" do
+ expect(Chef::TrainTransport.load_credentials("router.unicorns.com")).to be_nil
+ end
+
+ # [foo.example.org] => {"foo"=>{"example"=>{"org"=>{}}}}
+ # ['foo.example.org'] => {"foo.example.org"=>{}}
+ it "warns if the host has been split by toml" do
+ allow(Chef::TrainTransport).to receive(:parse_credentials_file).and_return(Mash.from_hash( { "foo" => { "example" => { "org" => {} } } }))
+ expect(Chef::Log).to receive(:warn).with(/as a Hash/)
+ expect(Chef::Log).to receive(:warn).with(/Hostnames must be surrounded by single quotes/)
+ expect(Chef::TrainTransport.load_credentials("foo.example.org")).to be_nil
+ end
+ end
+
+ describe "credentials_file_path" do
+
+ context "when a file path is specified by a config" do
+ let(:cred_file_path) { "/somewhere/credentials" }
+
+ before do
+ tm_config = double("Config Context", host: "foo.example.org", credentials_file: cred_file_path)
+ allow(Chef::Config).to receive(:target_mode).and_return(tm_config)
+ end
+
+ it "returns the path if it exists" do
+ allow(File).to receive(:exists?).with(cred_file_path).and_return(true)
+ expect(Chef::TrainTransport.credentials_file_path).to eq(cred_file_path)
+ end
+
+ it "raises an error if it does not exist" do
+ allow(File).to receive(:exists?).with(cred_file_path).and_return(false)
+ expect { Chef::TrainTransport.credentials_file_path }.to raise_error(ArgumentError, /does not exist/)
+ end
+ end
+
+ it "returns the path to the default config file if it exists" do
+ cred_file_path = Chef::Config.platform_specific_path("/etc/chef/foo.example.org/credentials")
+ tm_config = double("Config Context", host: "foo.example.org", credentials_file: nil)
+ allow(Chef::Config).to receive(:target_mode).and_return(tm_config)
+ allow(File).to receive(:exists?).with(cred_file_path).and_return(true)
+ expect(Chef::TrainTransport.credentials_file_path).to eq(cred_file_path)
+ end
+ end
+end