diff options
author | Bryan McLellan <btm@chef.io> | 2019-04-24 14:23:07 -0400 |
---|---|---|
committer | Bryan McLellan <btm@loftninjas.org> | 2019-05-06 12:56:55 -0400 |
commit | 00464d7cc3e818ee687cae1db8c087ee95187579 (patch) | |
tree | e75dc5c528d3ae96ff4bf1531f848c319688b3b7 /spec | |
parent | 9d5f5c40362d1fd7b0323cf0880300d6165b3a94 (diff) | |
download | chef-00464d7cc3e818ee687cae1db8c087ee95187579.tar.gz |
Unit tests for experimental target mode
Signed-off-by: Bryan McLellan <btm@chef.io>
Diffstat (limited to 'spec')
-rw-r--r-- | spec/unit/node_map_spec.rb | 34 | ||||
-rw-r--r-- | spec/unit/train_transport_spec.rb | 79 |
2 files changed, 113 insertions, 0 deletions
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 |