Class: Net::SSH::Test::Channel

Inherits:
Object
  • Object
show all
Defined in:
lib/net/ssh/test/channel.rb

Overview

A mock channel, used for scripting actions in tests. It wraps a Net::SSH::Test::Script instance, and delegates to it for the most part. This class has little real functionality on its own, but rather acts as a convenience for scripting channel-related activity for later comparison in a unit test.

story do |session|
  channel = session.opens_channel
  channel.sends_exec "ls"
  channel.gets_data "result of ls"
  channel.gets_extended_data "some error coming from ls"
  channel.gets_close
  channel.sends_close
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(script) ⇒ Channel

Creates a new Test::Channel instance on top of the given script (which must be a Net::SSH::Test::Script instance).


31
32
33
34
# File 'lib/net/ssh/test/channel.rb', line 31

def initialize(script)
  @script = script
  @local_id = @remote_id = nil
end

Instance Attribute Details

#local_idObject

Returns the local (client-assigned) id for this channel, or a Proc object that will return the local-id later if the local id has not yet been set. (See Net::SSH::Test::Packet#instantiate!.)


39
40
41
# File 'lib/net/ssh/test/channel.rb', line 39

def local_id
  @local_id || Proc.new { @local_id or raise "local-id has not been set yet!" }
end

#remote_idObject

Returns the remote (server-assigned) id for this channel, or a Proc object that will return the remote-id later if the remote id has not yet been set. (See Net::SSH::Test::Packet#instantiate!.)


46
47
48
# File 'lib/net/ssh/test/channel.rb', line 46

def remote_id
  @remote_id || Proc.new { @remote_id or raise "remote-id has not been set yet!" }
end

#scriptObject (readonly)

The Net::SSH::Test::Script instance employed by this mock channel.


21
22
23
# File 'lib/net/ssh/test/channel.rb', line 21

def script
  @script
end

Instance Method Details

#gets_closeObject

Scripts the reception of a “channel close” packet from the remote end.

channel.gets_close

142
143
144
# File 'lib/net/ssh/test/channel.rb', line 142

def gets_close
  script.gets_channel_close(self)
end

#gets_data(data) ⇒ Object

Scripts the reception of a channel data packet from the remote end.

channel.gets_data "bar"

113
114
115
# File 'lib/net/ssh/test/channel.rb', line 113

def gets_data(data)
  script.gets_channel_data(self, data)
end

#gets_eofObject

Scripts the reception of an EOF packet from the remote end.

channel.gets_eof

135
136
137
# File 'lib/net/ssh/test/channel.rb', line 135

def gets_eof
  script.gets_channel_eof(self)
end

#gets_exit_status(status = 0) ⇒ Object

Scripts the reception of an “exit-status” channel request packet.

channel.gets_exit_status(127)

128
129
130
# File 'lib/net/ssh/test/channel.rb', line 128

def gets_exit_status(status=0)
  script.gets_channel_request(self, "exit-status", false, status)
end

#gets_extended_data(data) ⇒ Object

Scripts the reception of a channel extended data packet from the remote end.

channel.gets_extended_data "whoops"

121
122
123
# File 'lib/net/ssh/test/channel.rb', line 121

def gets_extended_data(data)
  script.gets_channel_extended_data(self, data)
end

#inject_remote_delay!Object

Because adjacent calls to #gets_data will sometimes cause the data packets to be concatenated (causing expectations in tests to fail), you may need to separate those calls with calls to #inject_remote_delay! (which essentially just mimics receiving an empty data packet):

channel.gets_data "abcdefg"
channel.inject_remote_delay!
channel.gets_data "hijklmn"

58
59
60
# File 'lib/net/ssh/test/channel.rb', line 58

def inject_remote_delay!
  gets_data("")
end

#sends_closeObject

Scripts the sending of a “channel close” packet across the channel.

channel.sends_close

99
100
101
# File 'lib/net/ssh/test/channel.rb', line 99

def sends_close
  script.sends_channel_close(self)
end

#sends_data(data) ⇒ Object

Scripts the sending of a data packet across the channel.

channel.sends_data "foo"

85
86
87
# File 'lib/net/ssh/test/channel.rb', line 85

def sends_data(data)
  script.sends_channel_data(self, data)
end

#sends_eofObject

Scripts the sending of an EOF packet across the channel.

channel.sends_eof

92
93
94
# File 'lib/net/ssh/test/channel.rb', line 92

def sends_eof
  script.sends_channel_eof(self)
end

#sends_exec(command, reply = true, success = true) ⇒ Object

Scripts the sending of an “exec” channel request packet to the mock server. If reply is true, then the server is expected to reply to the request, otherwise no response to this request will be sent. If success is true, then the request will be successful, otherwise a failure will be scripted.

channel.sends_exec "ls -l"

69
70
71
# File 'lib/net/ssh/test/channel.rb', line 69

def sends_exec(command, reply=true, success=true)
  script.sends_channel_request(self, "exec", reply, command, success)
end

#sends_request_ptyObject

Scripts the sending of a “request pty” request packet across the channel.

channel.sends_request_pty

106
107
108
# File 'lib/net/ssh/test/channel.rb', line 106

def sends_request_pty
  script.sends_channel_request_pty(self)
end

#sends_subsystem(subsystem, reply = true, success = true) ⇒ Object

Scripts the sending of a “subsystem” channel request packet to the mock server. See #sends_exec for a discussion of the meaning of the reply and success arguments.

channel.sends_subsystem "sftp"

78
79
80
# File 'lib/net/ssh/test/channel.rb', line 78

def sends_subsystem(subsystem, reply=true, success=true)
  script.sends_channel_request(self, "subsystem", reply, subsystem, success)
end