Skip to content

Commit

Permalink
Merge pull request #1201 from tduffield/tduffield/update-net-ssh-4
Browse files Browse the repository at this point in the history
Update to latest train (and net-ssh 4)
  • Loading branch information
Salim Afiune committed Mar 3, 2017
2 parents 96fb35f + 382c1a1 commit 5e9a335
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 146 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Expand Up @@ -3,7 +3,7 @@ source "https://rubygems.org"
gemspec
gem "rack", "< 2.0"

gem "train", "~> 0.19.0"
gem "train", "~> 0.22"

group :integration do
gem "berkshelf", "~> 4.3"
Expand Down
89 changes: 14 additions & 75 deletions spec/kitchen/ssh_spec.rb
Expand Up @@ -20,6 +20,7 @@

require "kitchen/ssh"
require "tmpdir"
require "net/ssh/test"

# Hack to sort results in `Dir.entries` only within the yielded block, to limit
# the "behavior pollution" to other code. This was needed for Net::SCP, as
Expand Down Expand Up @@ -50,87 +51,21 @@ class << self
end
end

# Terrible hack to deal with Net::SSH:Test::Extensions which monkey patches
# `IO.select` with a version for testing Net::SSH code. Unfortunetly this
# impacts other code, so we'll "un-patch" this after each spec and "re-patch"
# it before the next one.
require "net/ssh/test"
def depatch_io
IO.class_exec do
class << self
alias_method :select, :select_for_real
end
end
end
# We need to immediately call depatch so that `IO.select` is in a good state
# _right now_. The require immediately monkeypatches it and we only want
# it monkey patched inside each ssh test
depatch_io

def repatch_io
IO.class_exec do
class << self
alias_method :select, :select_for_test
end
end
end

# Major hack-and-a-half to add basic `Channel#request_pty` support to
# Net::SSH's testing framework. The `Net::SSH::Test::LocalPacket` does not
# recognize the `"pty-req"` request type, so bombs out whenever this channel
# request is sent.
#
# This "make-work" fix adds a method (`#sends_request_pty`) which works just
# like `#sends_exec` expcept that it enqueues a patched subclass of
# `LocalPacket` which can deal with the `"pty-req"` type.
#
# An upstream patch to Net::SSH will be required to retire this yak shave ;)
require "net/ssh/test/channel"
module Net
module SSH
module Test
class Channel
def sends_request_pty
pty_data = ["xterm", 80, 24, 640, 480, "\0"]

script.events << Class.new(Net::SSH::Test::LocalPacket) do
def types # rubocop:disable Lint/NestedMethodDefinition
if @type == 98 && @data[1] == "pty-req"
@types ||= [
:long, :string, :bool, :string,
:long, :long, :long, :long, :string
]
else
super
end
end
end.new(:channel_request, remote_id, "pty-req", false, *pty_data)
end
end
end
end
end

describe Kitchen::SSH do
include Net::SSH::Test

let(:logged_output) { StringIO.new }
let(:logger) { Logger.new(logged_output) }
let(:opts) { Hash.new }
let(:ssh) { Kitchen::SSH.new("foo", "me", opts) }
let(:conn) { connection }
let(:conn) { Net::SSH::Test::Extensions::IO.with_test_extension { connection } }

before do
repatch_io
logger.level = Logger::DEBUG
opts[:logger] = logger
Net::SSH.stubs(:start).returns(conn)
end

after do
depatch_io
end

describe "establishing a connection" do
[
Errno::EACCES, Errno::EADDRINUSE, Errno::ECONNREFUSED,
Expand Down Expand Up @@ -293,7 +228,7 @@ def types # rubocop:disable Lint/NestedMethodDefinition
end

it "raises an SSHFailed exception" do
err = proc { ssh.exec("doit") }.must_raise Kitchen::SSHFailed
err = proc { assert_scripted { ssh.exec("doit") } }.must_raise Kitchen::SSHFailed
err.message.must_equal "SSH exited (42) for command: [doit]"
end
end
Expand Down Expand Up @@ -479,8 +414,10 @@ def types # rubocop:disable Lint/NestedMethodDefinition
it "shuts down the connection when block closes" do
conn.expects(:shutdown!)

Kitchen::SSH.new("foo", "me", opts) do |ssh|
ssh.exec("doit")
Net::SSH::Test::Extensions::IO.with_test_extension do
Kitchen::SSH.new("foo", "me", opts) do |ssh|
ssh.exec("doit")
end
end
end
end
Expand Down Expand Up @@ -576,16 +513,18 @@ def types # rubocop:disable Lint/NestedMethodDefinition
it "returns a truthy value" do
TCPSocket.stubs(:new).returns(tcp_socket)

result = ssh.send(:test_ssh)
result.wont_equal nil
result.wont_equal false
Net::SSH::Test::Extensions::IO.with_test_extension do
result = ssh.send(:test_ssh)
result.wont_equal nil
result.wont_equal false
end
end

it "closes socket when finished" do
TCPSocket.stubs(:new).returns(tcp_socket)
tcp_socket.expects(:close)

ssh.send(:test_ssh)
Net::SSH::Test::Extensions::IO.with_test_extension { ssh.send(:test_ssh) }
end

[
Expand Down Expand Up @@ -631,7 +570,7 @@ def types # rubocop:disable Lint/NestedMethodDefinition

it "logs to info for each retry" do
TCPSocket.stubs(:new).returns(not_ready, not_ready, ready)
ssh.wait
Net::SSH::Test::Extensions::IO.with_test_extension { ssh.wait }

logged_output.string.lines.count do |l|
l =~ info_line_with("Waiting for foo:22...")
Expand Down
75 changes: 5 additions & 70 deletions spec/kitchen/transport/ssh_spec.rb
Expand Up @@ -19,6 +19,7 @@
require_relative "../../spec_helper"

require "kitchen/transport/ssh"
require "net/ssh/test"

# Hack to sort results in `Dir.entries` only within the yielded block, to limit
# the "behavior pollution" to other code. This was needed for Net::SCP, as
Expand Down Expand Up @@ -49,67 +50,6 @@ class << self
end
end

# Terrible hack to deal with Net::SSH:Test::Extensions which monkey patches
# `IO.select` with a version for testing Net::SSH code. Unfortunetly this
# impacts other code, so we'll "un-patch" this after each spec and "re-patch"
# it before the next one.
require "net/ssh/test"
def depatch_io
IO.class_exec do
class << self
alias_method :select, :select_for_real
end
end
end
# We need to immediately call depatch so that `IO.select` is in a good state
# _right now_. The require immediately monkeypatches it and we only want
# it monkey patched inside each ssh test
depatch_io

def repatch_io
IO.class_exec do
class << self
alias_method :select, :select_for_test
end
end
end

# Major hack-and-a-half to add basic `Channel#request_pty` support to
# Net::SSH's testing framework. The `Net::SSH::Test::LocalPacket` does not
# recognize the `"pty-req"` request type, so bombs out whenever this channel
# request is sent.
#
# This "make-work" fix adds a method (`#sends_request_pty`) which works just
# like `#sends_exec` expcept that it enqueues a patched subclass of
# `LocalPacket` which can deal with the `"pty-req"` type.
#
# An upstream patch to Net::SSH will be required to retire this yak shave ;)
require "net/ssh/test/channel"
module Net
module SSH
module Test
class Channel
def sends_request_pty
pty_data = ["xterm", 80, 24, 640, 480, "\0"]

script.events << Class.new(Net::SSH::Test::LocalPacket) do
def types # rubocop:disable Lint/NestedMethodDefinition
if @type == 98 && @data[1] == "pty-req"
@types ||= [
:long, :string, :bool, :string,
:long, :long, :long, :long, :string
]
else
super
end
end
end.new(:channel_request, remote_id, "pty-req", false, *pty_data)
end
end
end
end
end

describe Kitchen::Transport::Ssh do
let(:logged_output) { StringIO.new }
let(:logger) { Logger.new(logged_output) }
Expand All @@ -121,7 +61,7 @@ def types # rubocop:disable Lint/NestedMethodDefinition
end

let(:transport) do
Kitchen::Transport::Ssh.new(config).finalize_config!(instance)
Net::SSH::Test::Extensions::IO.with_test_extension { Kitchen::Transport::Ssh.new(config).finalize_config!(instance) }
end

it "provisioner api_version is 1" do
Expand Down Expand Up @@ -711,7 +651,7 @@ def debug_line_with(msg)

let(:logged_output) { StringIO.new }
let(:logger) { Logger.new(logged_output) }
let(:conn) { net_ssh_connection }
let(:conn) { Net::SSH::Test::Extensions::IO.with_test_extension { net_ssh_connection } }

let(:options) do
{
Expand All @@ -728,15 +668,10 @@ def debug_line_with(msg)
end

before do
repatch_io
logger.level = Logger::DEBUG
Net::SSH.stubs(:start).returns(conn)
end

after do
depatch_io
end

describe "establishing a connection" do
[
Errno::EACCES, Errno::EADDRINUSE, Errno::ECONNREFUSED, Errno::ETIMEDOUT,
Expand Down Expand Up @@ -952,14 +887,14 @@ def debug_line_with(msg)

it "raises an SshFailed exception" do
err = proc do
connection.execute("doit")
assert_scripted { connection.execute("doit") }
end.must_raise Kitchen::Transport::SshFailed
err.message.must_equal "SSH exited (42) for command: [doit]"
end

it "returns the exit code with an SshFailed exception" do
begin
connection.execute("doit")
assert_scripted { connection.execute("doit") }
rescue Kitchen::Transport::SshFailed => e
e.exit_code.must_equal 42
end
Expand Down

0 comments on commit 5e9a335

Please sign in to comment.