diff options
author | Tim Smith <tsmith@chef.io> | 2019-05-06 11:46:42 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-06 11:46:42 -0700 |
commit | c23a9d65f4bf5aba2fe7cf384e04f7703631fb0b (patch) | |
tree | ae33e0ce0c34a20834f8dfdc3083ecb49015159b /spec/unit | |
parent | 3ba19c97144832d44a1e77c3183b8b8c9b46f004 (diff) | |
parent | 2b12cda0693ad20780156d35a5ae316a90c5174b (diff) | |
download | chef-c23a9d65f4bf5aba2fe7cf384e04f7703631fb0b.tar.gz |
Merge pull request #7758 from chef/btm/target-mode
Initial target_mode implementation
Diffstat (limited to 'spec/unit')
-rw-r--r-- | spec/unit/node_map_spec.rb | 40 | ||||
-rw-r--r-- | spec/unit/train_transport_spec.rb | 79 |
2 files changed, 116 insertions, 3 deletions
diff --git a/spec/unit/node_map_spec.rb b/spec/unit/node_map_spec.rb index 9c161f3893..7c867857dc 100644 --- a/spec/unit/node_map_spec.rb +++ b/spec/unit/node_map_spec.rb @@ -145,14 +145,14 @@ describe Chef::NodeMap do describe "deleting classes" do it "deletes a class and removes the mapping completely" do node_map.set(:thing, Bar) - expect( node_map.delete_class(Bar) ).to include({ thing: [{ klass: Bar, cookbook_override: false, core_override: false }] }) + expect( node_map.delete_class(Bar) ).to include({ thing: [{ klass: Bar, cookbook_override: false, core_override: false, target_mode: nil }] }) expect( node_map.get(node, :thing) ).to eql(nil) end it "deletes a class and leaves the mapping that still has an entry" do node_map.set(:thing, Bar) node_map.set(:thing, Foo) - expect( node_map.delete_class(Bar) ).to eql({ thing: [{ klass: Bar, cookbook_override: false, core_override: false }] }) + expect( node_map.delete_class(Bar) ).to eql({ thing: [{ klass: Bar, cookbook_override: false, core_override: false, target_mode: nil }] }) expect( node_map.get(node, :thing) ).to eql(Foo) end @@ -160,7 +160,7 @@ describe Chef::NodeMap do node_map.set(:thing1, Bar) node_map.set(:thing2, Bar) node_map.set(:thing2, Foo) - expect( node_map.delete_class(Bar) ).to eql({ thing1: [{ klass: Bar, cookbook_override: false, core_override: false }], thing2: [{ klass: Bar, cookbook_override: false, core_override: false }] }) + expect( node_map.delete_class(Bar) ).to eql({ thing1: [{ klass: Bar, cookbook_override: false, core_override: false, target_mode: nil }], thing2: [{ klass: Bar, cookbook_override: false, core_override: false, target_mode: nil }] }) expect( node_map.get(node, :thing1) ).to eql(nil) expect( node_map.get(node, :thing2) ).to eql(Foo) end @@ -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..b56c7e1104 --- /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) { { "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({ "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 |