diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2017-12-01 09:48:11 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-01 09:48:11 -0800 |
commit | ee70d58e52f89e7b7b1369fcd170946b54f41f4b (patch) | |
tree | 24bf6158c7a7cce323c8bf3c240c466c3bca84f6 /spec | |
parent | fb579ea88499daaf04a97d49b1fc1c44040eec58 (diff) | |
parent | e01022409857d219d9e40438c900531ef433d622 (diff) | |
download | chef-ee70d58e52f89e7b7b1369fcd170946b54f41f4b.tar.gz |
Merge pull request #6590 from mal/knife-ssh-prefix
Knife SSH prefix option
Diffstat (limited to 'spec')
-rw-r--r-- | spec/functional/knife/ssh_spec.rb | 61 | ||||
-rw-r--r-- | spec/unit/knife/ssh_spec.rb | 107 |
2 files changed, 122 insertions, 46 deletions
diff --git a/spec/functional/knife/ssh_spec.rb b/spec/functional/knife/ssh_spec.rb index 9d6fd3ae10..3defbe781f 100644 --- a/spec/functional/knife/ssh_spec.rb +++ b/spec/functional/knife/ssh_spec.rb @@ -181,11 +181,11 @@ describe Chef::Knife::Ssh do it "uses the ssh_attribute" do @knife.run - expect(@knife.get_ssh_attribute({ "knife_config" => "ec2.public_hostname" })).to eq("ec2.public_hostname") + expect(@knife.get_ssh_attribute({ "target" => "ec2.public_hostname" })).to eq("ec2.public_hostname") end end - context "when knife[:ssh_attribute] is not provided]" do + context "when knife[:ssh_attribute] is not provided" do before do setup_knife(["*:*", "uptime"]) Chef::Config[:knife][:ssh_attribute] = nil @@ -199,22 +199,69 @@ describe Chef::Knife::Ssh do context "when -a ec2.public_public_hostname is provided" do before do - setup_knife(["-a ec2.public_hostname", "*:*", "uptime"]) + setup_knife(["-a", "ec2.public_hostname", "*:*", "uptime"]) Chef::Config[:knife][:ssh_attribute] = nil end it "should use the value on the command line" do @knife.run - expect(@knife.config[:attribute]).to eq("ec2.public_hostname") + expect(@knife.config[:ssh_attribute]).to eq("ec2.public_hostname") end it "should override what is set in knife.rb" do # This is the setting imported from knife.rb Chef::Config[:knife][:ssh_attribute] = "fqdn" # Then we run knife with the -a flag, which sets the above variable - setup_knife(["-a ec2.public_hostname", "*:*", "uptime"]) + setup_knife(["-a", "ec2.public_hostname", "*:*", "uptime"]) @knife.run - expect(@knife.config[:attribute]).to eq("ec2.public_hostname") + expect(@knife.config[:ssh_attribute]).to eq("ec2.public_hostname") + end + end + end + + describe "prefix" do + context "when knife[:prefix_attribute] is set" do + before do + setup_knife(["*:*", "uptime"]) + Chef::Config[:knife][:prefix_attribute] = "name" + end + + it "uses the prefix_attribute" do + @knife.run + expect(@knife.get_prefix_attribute({ "prefix" => "name" })).to eq("name") + end + end + + context "when knife[:prefix_attribute] is not provided" do + before do + setup_knife(["*:*", "uptime"]) + Chef::Config[:knife][:prefix_attribute] = nil + end + + it "falls back to nil" do + @knife.run + expect(@knife.get_prefix_attribute({})).to eq(nil) + end + end + + context "when --prefix-attribute ec2.public_public_hostname is provided" do + before do + setup_knife(["--prefix-attribute", "ec2.public_hostname", "*:*", "uptime"]) + Chef::Config[:knife][:prefix_attribute] = nil + end + + it "should use the value on the command line" do + @knife.run + expect(@knife.config[:prefix_attribute]).to eq("ec2.public_hostname") + end + + it "should override what is set in knife.rb" do + # This is the setting imported from knife.rb + Chef::Config[:knife][:prefix_attribute] = "fqdn" + # Then we run knife with the -b flag, which sets the above variable + setup_knife(["--prefix-attribute", "ec2.public_hostname", "*:*", "uptime"]) + @knife.run + expect(@knife.config[:prefix_attribute]).to eq("ec2.public_hostname") end end end @@ -305,7 +352,7 @@ describe Chef::Knife::Ssh do Chef::Config[:chef_server_url] = "http://localhost:9000" @api.post("/search/node?q=*:*&start=0&rows=1000", 200) do - %({"total":1, "start":0, "rows":[{"data": {"fqdn":"the.fqdn", "config": "the_public_hostname", "knife_config": "the_public_hostname" }}]}) + %({"total":1, "start":0, "rows":[{"data": {"fqdn":"the.fqdn", "target": "the_public_hostname"}}]}) end end diff --git a/spec/unit/knife/ssh_spec.rb b/spec/unit/knife/ssh_spec.rb index 815995b687..af54115ac7 100644 --- a/spec/unit/knife/ssh_spec.rb +++ b/spec/unit/knife/ssh_spec.rb @@ -49,28 +49,36 @@ describe Chef::Knife::Ssh do def self.should_return_specified_attributes it "returns an array of the attributes specified on the command line OR config file, if only one is set" do - @node_bar["config"] = "10.0.0.2" - @node_foo["config"] = "10.0.0.1" - @knife.config[:attribute] = "ipaddress" + @node_bar["target"] = "10.0.0.2" + @node_foo["target"] = "10.0.0.1" + @node_bar["prefix"] = "bar" + @node_foo["prefix"] = "foo" + @knife.config[:ssh_attribute] = "ipaddress" + @knife.config[:prefix_attribute] = "name" Chef::Config[:knife][:ssh_attribute] = "ipaddress" # this value will be in the config file - expect(@knife).to receive(:session_from_list).with([["10.0.0.1", nil], ["10.0.0.2", nil]]) + Chef::Config[:knife][:prefix_attribute] = "name" # this value will be in the config file + expect(@knife).to receive(:session_from_list).with([["10.0.0.1", nil, "foo"], ["10.0.0.2", nil, "bar"]]) @knife.configure_session end it "returns an array of the attributes specified on the command line even when a config value is set" do - @node_bar["config"] = "10.0.0.2" - @node_foo["config"] = "10.0.0.1" + @node_bar["target"] = "10.0.0.2" + @node_foo["target"] = "10.0.0.1" + @node_bar["prefix"] = "bar" + @node_foo["prefix"] = "foo" Chef::Config[:knife][:ssh_attribute] = "config_file" # this value will be in the config file - @knife.config[:attribute] = "ipaddress" # this is the value of the command line via #configure_attribute - expect(@knife).to receive(:session_from_list).with([["10.0.0.1", nil], ["10.0.0.2", nil]]) + Chef::Config[:knife][:prefix_attribute] = "config_file" # this value will be in the config file + @knife.config[:ssh_attribute] = "ipaddress" # this is the value of the command line via #configure_attribute + @knife.config[:prefix_attribute] = "name" # this is the value of the command line via #configure_attribute + expect(@knife).to receive(:session_from_list).with([["10.0.0.1", nil, "foo"], ["10.0.0.2", nil, "bar"]]) @knife.configure_session end end - it "searchs for and returns an array of fqdns" do + it "searches for and returns an array of fqdns" do expect(@knife).to receive(:session_from_list).with([ - ["foo.example.org", nil], - ["bar.example.org", nil], + ["foo.example.org", nil, nil], + ["bar.example.org", nil, nil], ]) @knife.configure_session end @@ -84,8 +92,8 @@ describe Chef::Knife::Ssh do end it "returns an array of cloud public hostnames" do expect(@knife).to receive(:session_from_list).with([ - ["ec2-10-0-0-1.compute-1.amazonaws.com", nil], - ["ec2-10-0-0-2.compute-1.amazonaws.com", nil], + ["ec2-10-0-0-1.compute-1.amazonaws.com", nil, nil], + ["ec2-10-0-0-2.compute-1.amazonaws.com", nil, nil], ]) @knife.configure_session end @@ -101,8 +109,8 @@ describe Chef::Knife::Ssh do it "returns an array of fqdns" do expect(@knife).to receive(:session_from_list).with([ - ["foo.example.org", nil], - ["bar.example.org", nil], + ["foo.example.org", nil, nil], + ["bar.example.org", nil, nil], ]) @knife.configure_session end @@ -144,15 +152,35 @@ describe Chef::Knife::Ssh do end end + describe "#get_prefix_attribute" do + # Order of precedence for prefix + # 1) config value (cli or knife config) + # 2) nil + before do + Chef::Config[:knife][:prefix_attribute] = nil + @knife.config[:prefix_attribute] = nil + @node_foo["cloud"]["public_hostname"] = "ec2-10-0-0-1.compute-1.amazonaws.com" + @node_bar["cloud"]["public_hostname"] = "" + end + + it "should return nil by default" do + expect(@knife.get_prefix_attribute({})).to eq(nil) + end + + it "should favor config over nil" do + @node_foo["prefix"] = "config" + expect( @knife.get_prefix_attribute(@node_foo)).to eq("config") + end + end + describe "#get_ssh_attribute" do # Order of precedence for ssh target - # 1) command line attribute - # 2) configuration file - # 3) cloud attribute - # 4) fqdn + # 1) config value (cli or knife config) + # 2) cloud attribute + # 3) fqdn before do Chef::Config[:knife][:ssh_attribute] = nil - @knife.config[:attribute] = nil + @knife.config[:ssh_attribute] = nil @node_foo["cloud"]["public_hostname"] = "ec2-10-0-0-1.compute-1.amazonaws.com" @node_bar["cloud"]["public_hostname"] = "" end @@ -165,18 +193,9 @@ describe Chef::Knife::Ssh do expect(@knife.get_ssh_attribute(@node_foo)).to eq("ec2-10-0-0-1.compute-1.amazonaws.com") end - it "should favor to attribute_from_cli over config file and cloud" do - @knife.config[:attribute] = "command_line" - Chef::Config[:knife][:ssh_attribute] = "config_file" - @node_foo["config"] = "command_line" - @node_foo["knife_config"] = "config_file" - expect( @knife.get_ssh_attribute(@node_foo)).to eq("command_line") - end - - it "should favor config file over cloud and default" do - Chef::Config[:knife][:ssh_attribute] = "config_file" - @node_foo["knife_config"] = "config_file" - expect( @knife.get_ssh_attribute(@node_foo)).to eq("config_file") + it "should favor config over cloud and default" do + @node_foo["target"] = "config" + expect( @knife.get_ssh_attribute(@node_foo)).to eq("config") end it "should return fqdn if cloud.hostname is empty" do @@ -192,40 +211,50 @@ describe Chef::Knife::Ssh do end it "uses the port from an ssh config file" do - @knife.session_from_list([["the.b.org", nil]]) + @knife.session_from_list([["the.b.org", nil, nil]]) expect(@knife.session.servers[0].port).to eq(23) end it "uses the port from a cloud attr" do - @knife.session_from_list([["the.b.org", 123]]) + @knife.session_from_list([["the.b.org", 123, nil]]) expect(@knife.session.servers[0].port).to eq(123) end + it "uses the prefix from list" do + @knife.session_from_list([["the.b.org", nil, "b-team"]]) + expect(@knife.session.servers[0][:prefix]).to eq("b-team") + end + + it "defaults to a prefix of host" do + @knife.session_from_list([["the.b.org", nil, nil]]) + expect(@knife.session.servers[0][:prefix]).to eq("the.b.org") + end + it "defaults to a timeout of 120 seconds" do - @knife.session_from_list([["the.b.org", nil]]) + @knife.session_from_list([["the.b.org", nil, nil]]) expect(@knife.session.servers[0].options[:timeout]).to eq(120) end it "uses the timeout from Chef Config" do Chef::Config[:knife][:ssh_timeout] = 5 @knife.config[:ssh_timeout] = nil - @knife.session_from_list([["the.b.org", nil]]) + @knife.session_from_list([["the.b.org", nil, nil]]) expect(@knife.session.servers[0].options[:timeout]).to eq(5) end it "uses the timeout from knife config" do @knife.config[:ssh_timeout] = 6 - @knife.session_from_list([["the.b.org", nil]]) + @knife.session_from_list([["the.b.org", nil, nil]]) expect(@knife.session.servers[0].options[:timeout]).to eq(6) end it "uses the user from an ssh config file" do - @knife.session_from_list([["the.b.org", 123]]) + @knife.session_from_list([["the.b.org", 123, nil]]) expect(@knife.session.servers[0].user).to eq("locutus") end it "uses keepalive settings from an ssh config file" do - @knife.session_from_list([["the.b.org", 123]]) + @knife.session_from_list([["the.b.org", 123, nil]]) expect(@knife.session.servers[0].options[:keepalive]).to be true expect(@knife.session.servers[0].options[:keepalive_interval]).to eq 60 end |