summaryrefslogtreecommitdiff
path: root/spec/unit/knife
diff options
context:
space:
mode:
authorIgor Shpakov <igorshp@gmail.com>2015-02-03 16:00:48 +0000
committerIgor Shpakov <igorshp@gmail.com>2015-05-07 18:02:21 +0100
commitf0c469daab37564be9e77c4f1ffdd6ee691e0ea5 (patch)
tree2fd981cd12cbc31897ee64a9c231f5bcd1421f0a /spec/unit/knife
parent44d70ba0f1b38bc3e1170ae978ad5e9a11679df3 (diff)
downloadchef-f0c469daab37564be9e77c4f1ffdd6ee691e0ea5.tar.gz
Fix ssh attribute precedence order
Pick ssh attribute in following order: 1. command line 2. config file 3. cloud attribute 4. fqdn Prioritise manual ssh attribute over defaults Change history: - knife:ssh; fixing tests - updated based on @btm comments - Removed configure_attributes and replaced it with get_ssh_attribute - get_ssh_attribute on it's own returns only the name of the attribute to use. this makes testing easy. the order of precedence is defined in the comments - updated functional tests - changed :attribute_from_cli to :attribute - @btm correctly noted that attribute_from_cli was a temporary variable and is no longer used - updated tests to use the new format
Diffstat (limited to 'spec/unit/knife')
-rw-r--r--spec/unit/knife/ssh_spec.rb49
1 files changed, 23 insertions, 26 deletions
diff --git a/spec/unit/knife/ssh_spec.rb b/spec/unit/knife/ssh_spec.rb
index a838a21edc..723280bead 100644
--- a/spec/unit/knife/ssh_spec.rb
+++ b/spec/unit/knife/ssh_spec.rb
@@ -28,10 +28,10 @@ describe Chef::Knife::Ssh do
before do
@knife = Chef::Knife::Ssh.new
@knife.merge_configs
- @knife.config[:attribute] = "fqdn"
@node_foo = Chef::Node.new
@node_foo.automatic_attrs[:fqdn] = "foo.example.org"
@node_foo.automatic_attrs[:ipaddress] = "10.0.0.1"
+
@node_bar = Chef::Node.new
@node_bar.automatic_attrs[:fqdn] = "bar.example.org"
@node_bar.automatic_attrs[:ipaddress] = "10.0.0.2"
@@ -52,15 +52,15 @@ 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
@knife.config[:attribute] = "ipaddress"
- @knife.config[:attribute_from_cli] = "ipaddress"
+ Chef::Config[:knife][:ssh_attribute] = "ipaddress" # this value will be in the config file
configure_query([@node_foo, @node_bar])
expect(@knife).to receive(:session_from_list).with([['10.0.0.1', nil], ['10.0.0.2', nil]])
@knife.configure_session
end
it "returns an array of the attributes specified on the command line even when a config value is set" do
- @knife.config[:attribute] = "config_file" # this value will be the config file
- @knife.config[:attribute_from_cli] = "ipaddress" # this is the value of the command line via #configure_attribute
+ 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
configure_query([@node_foo, @node_bar])
expect(@knife).to receive(:session_from_list).with([['10.0.0.1', nil], ['10.0.0.2', nil]])
@knife.configure_session
@@ -83,7 +83,6 @@ describe Chef::Knife::Ssh do
@node_foo.automatic_attrs[:cloud][:public_hostname] = "ec2-10-0-0-1.compute-1.amazonaws.com"
@node_bar.automatic_attrs[:cloud][:public_hostname] = "ec2-10-0-0-2.compute-1.amazonaws.com"
end
-
it "returns an array of cloud public hostnames" do
configure_query([@node_foo, @node_bar])
expect(@knife).to receive(:session_from_list).with([
@@ -150,42 +149,40 @@ describe Chef::Knife::Ssh do
end
end
- describe "#configure_attribute" do
+ describe "#get_ssh_attribute" do
+ # Order of precedence for ssh target
+ # 1) command line attribute
+ # 2) configuration file
+ # 3) cloud attribute
+ # 4) fqdn
before do
Chef::Config[:knife][:ssh_attribute] = nil
@knife.config[:attribute] = nil
+ @node_foo.automatic_attrs[:cloud][:public_hostname] = "ec2-10-0-0-1.compute-1.amazonaws.com"
+ @node_bar.automatic_attrs[:cloud][:public_hostname] = ''
end
it "should return fqdn by default" do
- @knife.configure_attribute
- expect(@knife.config[:attribute]).to eq("fqdn")
+ expect(@knife.get_ssh_attribute(Chef::Node.new)).to eq("fqdn")
end
- it "should return the value set in the configuration file" do
- Chef::Config[:knife][:ssh_attribute] = "config_file"
- @knife.configure_attribute
- expect(@knife.config[:attribute]).to eq("config_file")
+ it "should return cloud.public_hostname attribute if available" do
+ expect(@knife.get_ssh_attribute(@node_foo)).to eq("cloud.public_hostname")
end
- it "should return the value set on the command line" do
+ it "should favor to attribute_from_cli over config file and cloud" do
@knife.config[:attribute] = "command_line"
- @knife.configure_attribute
- expect(@knife.config[:attribute]).to eq("command_line")
+ Chef::Config[:knife][:ssh_attribute] = "config_file"
+ expect( @knife.get_ssh_attribute(@node_foo)).to eq("command_line")
end
- it "should set attribute_from_cli to the value of attribute from the command line" do
- @knife.config[:attribute] = "command_line"
- @knife.configure_attribute
- expect(@knife.config[:attribute]).to eq("command_line")
- expect(@knife.config[:attribute_from_cli]).to eq("command_line")
+ it "should favor config file over cloud and default" do
+ Chef::Config[:knife][:ssh_attribute] = "config_file"
+ expect( @knife.get_ssh_attribute(@node_foo)).to eq("config_file")
end
- it "should prefer the command line over the config file for the value of attribute_from_cli" do
- Chef::Config[:knife][:ssh_attribute] = "config_file"
- @knife.config[:attribute] = "command_line"
- @knife.configure_attribute
- expect(@knife.config[:attribute]).to eq("command_line")
- expect(@knife.config[:attribute_from_cli]).to eq("command_line")
+ it "should return fqdn if cloud.hostname is empty" do
+ expect( @knife.get_ssh_attribute(@node_bar)).to eq("fqdn")
end
end