compacted class default value class methods in ssh, and tightened up the tests
This commit is contained in:
parent
3fd2ef5e2b
commit
6ea3fe39b9
|
@ -2,27 +2,20 @@ module Hobo
|
||||||
class SSH
|
class SSH
|
||||||
SCRIPT = File.join(File.dirname(__FILE__), '..', '..', 'bin', 'hobo-ssh-expect.sh')
|
SCRIPT = File.join(File.dirname(__FILE__), '..', '..', 'bin', 'hobo-ssh-expect.sh')
|
||||||
|
|
||||||
class << self
|
def self.connect(opts={})
|
||||||
def connect(opts={})
|
Kernel.exec "#{SCRIPT} #{uname(opts)} #{pass(opts)} #{host(opts)} #{port(opts)}".strip
|
||||||
Kernel.exec "#{SCRIPT} #{opts[:uname] || uname_default} #{opts[:pass] || pass_default} #{opts[:host] || host_default}".strip
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def port_default
|
module ClassMethods
|
||||||
Hobo.config[:ssh][:port]
|
private
|
||||||
|
[:port, :host, :pass, :uname].each do |method|
|
||||||
|
define_method(method) do |opts|
|
||||||
|
opts[method] || Hobo.config[:ssh][method]
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def host_default
|
extend ClassMethods
|
||||||
Hobo.config[:ssh][:host]
|
|
||||||
end
|
|
||||||
|
|
||||||
def pass_default
|
|
||||||
Hobo.config[:ssh][:pass]
|
|
||||||
end
|
|
||||||
|
|
||||||
def uname_default
|
|
||||||
Hobo.config[:ssh][:uname]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -9,14 +9,14 @@ class SshTest < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
test "should call exec with defaults when no options are supplied" do
|
test "should call exec with defaults when no options are supplied" do
|
||||||
# NOTE HOBO_MOCK_CONFIG only contains the :uname at this stage, adding further params will break this test
|
ssh = HOBO_MOCK_CONFIG[:ssh]
|
||||||
Kernel.expects(:exec).with("#{@script} #{HOBO_MOCK_CONFIG[:ssh][:uname]}")
|
Kernel.expects(:exec).with("#{@script} #{ssh[:uname]} #{ssh[:pass]} #{ssh[:host]} #{ssh[:port]}")
|
||||||
Hobo::SSH.connect
|
Hobo::SSH.connect
|
||||||
end
|
end
|
||||||
|
|
||||||
test "should call exec with supplied params" do
|
test "should call exec with supplied params" do
|
||||||
args = {:uname => 'bar', :pass => 'baz', :host => 'bak'}
|
args = {:uname => 'bar', :pass => 'baz', :host => 'bak', :port => 'bag'}
|
||||||
Kernel.expects(:exec).with("#{@script} #{args[:uname]} #{args[:pass]} #{args[:host]}")
|
Kernel.expects(:exec).with("#{@script} #{args[:uname]} #{args[:pass]} #{args[:host]} #{args[:port]}")
|
||||||
Hobo::SSH.connect(args)
|
Hobo::SSH.connect(args)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -22,4 +22,12 @@ require File.join(File.dirname(__FILE__), '..', 'lib', 'hobo')
|
||||||
require 'contest'
|
require 'contest'
|
||||||
require 'mocha'
|
require 'mocha'
|
||||||
|
|
||||||
HOBO_MOCK_CONFIG = { :ssh => { :uname => 'foo'} }
|
HOBO_MOCK_CONFIG =
|
||||||
|
{ :ssh =>
|
||||||
|
{
|
||||||
|
:uname => 'foo',
|
||||||
|
:pass => 'bar',
|
||||||
|
:host => 'baz',
|
||||||
|
:port => 'bak'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue