diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2014-01-17 16:12:39 -0800 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2014-01-17 16:12:39 -0800 |
commit | 9d26cb2622b4d148296b3c3cbe6171cd67b30972 (patch) | |
tree | 15335d2fbb7f31dec218fb0ee341107692d30089 | |
parent | 2274ae7f2c5ef6c23e4a8b02f5b677f487a2a0d9 (diff) | |
download | chef-9d26cb2622b4d148296b3c3cbe6171cd67b30972.tar.gz |
add tests and moar comment
-rw-r--r-- | lib/chef/knife/ssh.rb | 6 | ||||
-rw-r--r-- | spec/unit/knife/ssh_spec.rb | 107 |
2 files changed, 113 insertions, 0 deletions
diff --git a/lib/chef/knife/ssh.rb b/lib/chef/knife/ssh.rb index dd392388bc..83c1735b4a 100644 --- a/lib/chef/knife/ssh.rb +++ b/lib/chef/knife/ssh.rb @@ -436,6 +436,12 @@ class Chef Chef::Config[:knife][:ssh_user]) end + # This is a bit overly complicated because of the way we want knife ssh to work with -P causing a password prompt for + # the user, but we have to be conscious that this code gets included in knife bootstrap and knife * server create as + # well. We want to change the semantics so that the default is false and 'nil' means -P without an argument on the + # command line. But the other utilities expect nil to be the default and we can't prompt in that case. So we effectively + # use ssh_password_ng to determine if we're coming from knife ssh or from the other utilities. The other utilties can + # also be patched to use ssh_password_ng easily as long they follow the convention that the default is false. def configure_password if config.has_key?(:ssh_password_ng) && config[:ssh_password_ng].nil? # If the parameter is called on the command line with no value diff --git a/spec/unit/knife/ssh_spec.rb b/spec/unit/knife/ssh_spec.rb index 0d67f33ee7..01dd742da0 100644 --- a/spec/unit/knife/ssh_spec.rb +++ b/spec/unit/knife/ssh_spec.rb @@ -280,4 +280,111 @@ describe Chef::Knife::Ssh do end end end + + describe "#configure_password" do + before do + @knife.config.delete(:ssh_password_ng) + @knife.config.delete(:ssh_password) + end + + context "when setting ssh_password_ng from knife ssh" do + # in this case ssh_password_ng exists, but ssh_password does not + it "should prompt for a password when ssh_passsword_ng is nil" do + @knife.config[:ssh_password_ng] = nil + @knife.should_receive(:get_password).and_return("mysekretpassw0rd") + @knife.configure_password + @knife.config[:ssh_password].should == "mysekretpassw0rd" + end + + it "should set ssh_password to false if ssh_password_ng is false" do + @knife.config[:ssh_password_ng] = false + @knife.should_not_receive(:get_password) + @knife.configure_password + @knife.config[:ssh_password].should be_false + end + + it "should set ssh_password to ssh_password_ng if we set a password" do + @knife.config[:ssh_password_ng] = "mysekretpassw0rd" + @knife.should_not_receive(:get_password) + @knife.configure_password + @knife.config[:ssh_password].should == "mysekretpassw0rd" + end + end + + context "when setting ssh_password from knife bootstrap / knife * server create" do + # in this case ssh_password exists, but ssh_password_ng does not + it "should set ssh_password to nil when ssh_password is nil" do + @knife.config[:ssh_password] = nil + @knife.should_not_receive(:get_password) + @knife.configure_password + @knife.config[:ssh_password].should be_nil + end + + it "should set ssh_password to false when ssh_password is false" do + @knife.config[:ssh_password] = false + @knife.should_not_receive(:get_password) + @knife.configure_password + @knife.config[:ssh_password].should be_false + end + + it "should set ssh_password to ssh_password if we set a password" do + @knife.config[:ssh_password] = "mysekretpassw0rd" + @knife.should_not_receive(:get_password) + @knife.configure_password + @knife.config[:ssh_password].should == "mysekretpassw0rd" + end + end + context "when setting ssh_password in the config variable" do + before(:each) do + Chef::Config[:knife][:ssh_password] = "my_knife_passw0rd" + end + context "when setting ssh_password_ng from knife ssh" do + # in this case ssh_password_ng exists, but ssh_password does not + it "should prompt for a password when ssh_passsword_ng is nil" do + @knife.config[:ssh_password_ng] = nil + @knife.should_receive(:get_password).and_return("mysekretpassw0rd") + @knife.configure_password + @knife.config[:ssh_password].should == "mysekretpassw0rd" + end + + it "should set ssh_password to the configured knife.rb value if ssh_password_ng is false" do + @knife.config[:ssh_password_ng] = false + @knife.should_not_receive(:get_password) + @knife.configure_password + @knife.config[:ssh_password].should == "my_knife_passw0rd" + end + + it "should set ssh_password to ssh_password_ng if we set a password" do + @knife.config[:ssh_password_ng] = "mysekretpassw0rd" + @knife.should_not_receive(:get_password) + @knife.configure_password + @knife.config[:ssh_password].should == "mysekretpassw0rd" + end + end + + context "when setting ssh_password from knife bootstrap / knife * server create" do + # in this case ssh_password exists, but ssh_password_ng does not + it "should set ssh_password to the configured knife.rb value when ssh_password is nil" do + @knife.config[:ssh_password] = nil + @knife.should_not_receive(:get_password) + @knife.configure_password + @knife.config[:ssh_password].should == "my_knife_passw0rd" + end + + it "should set ssh_password to the configured knife.rb value when ssh_password is false" do + @knife.config[:ssh_password] = false + @knife.should_not_receive(:get_password) + @knife.configure_password + @knife.config[:ssh_password].should == "my_knife_passw0rd" + end + + it "should set ssh_password to ssh_password if we set a password" do + @knife.config[:ssh_password] = "mysekretpassw0rd" + @knife.should_not_receive(:get_password) + @knife.configure_password + @knife.config[:ssh_password].should == "mysekretpassw0rd" + end + end + end + end end |