Class: SSHake::Mock::Session
- Inherits:
-
BaseSession
- Object
- BaseSession
- SSHake::Mock::Session
- Defined in:
- lib/sshake/mock/session.rb
Instance Attribute Summary collapse
-
#command_set ⇒ Object
readonly
Returns the value of attribute command_set.
-
#executed_commands ⇒ Object
readonly
Returns the value of attribute executed_commands.
-
#store ⇒ Object
readonly
Returns the value of attribute store.
-
#written_files ⇒ Object
readonly
Returns the value of attribute written_files.
Attributes inherited from BaseSession
#id, #klogger, #raise_on_error
Instance Method Summary collapse
- #connect ⇒ Object
- #connected? ⇒ Boolean
- #disconnect ⇒ Object
- #execute(commands, options = nil, &block) ⇒ Object
- #find_executed_commands(matcher) ⇒ Object
-
#has_executed_command?(matcher) ⇒ Boolean
rubocop:disable Naming/PredicateName.
-
#initialize(**options) {|_self| ... } ⇒ Session
constructor
A new instance of Session.
- #kill! ⇒ Object
- #write_data(path, data, _options = nil) ⇒ Object
Constructor Details
#initialize(**options) {|_self| ... } ⇒ Session
Returns a new instance of Session.
16 17 18 19 20 21 22 23 24 |
# File 'lib/sshake/mock/session.rb', line 16 def initialize(**) @options = @command_set = [:command_set] || CommandSet.new @executed_commands = [] @store = {} @written_files = {} @connected = false yield(self) if block_given? end |
Instance Attribute Details
#command_set ⇒ Object (readonly)
Returns the value of attribute command_set.
14 15 16 |
# File 'lib/sshake/mock/session.rb', line 14 def command_set @command_set end |
#executed_commands ⇒ Object (readonly)
Returns the value of attribute executed_commands.
14 15 16 |
# File 'lib/sshake/mock/session.rb', line 14 def executed_commands @executed_commands end |
#store ⇒ Object (readonly)
Returns the value of attribute store.
14 15 16 |
# File 'lib/sshake/mock/session.rb', line 14 def store @store end |
#written_files ⇒ Object (readonly)
Returns the value of attribute written_files.
14 15 16 |
# File 'lib/sshake/mock/session.rb', line 14 def written_files @written_files end |
Instance Method Details
#connect ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/sshake/mock/session.rb', line 26 def connect case @options[:connection_error] when :timeout raise Net::SSH::ConnectionTimeout when :authentication_failed raise Net::SSH::AuthenticationFailed when :connection_refused raise Errno::ECONNREFUSED when :host_unreachable raise Errno::EHOSTUNREACH else @connected = true end end |
#connected? ⇒ Boolean
41 42 43 |
# File 'lib/sshake/mock/session.rb', line 41 def connected? @connected == true end |
#disconnect ⇒ Object
45 46 47 |
# File 'lib/sshake/mock/session.rb', line 45 def disconnect @connected = false end |
#execute(commands, options = nil, &block) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/sshake/mock/session.rb', line 53 def execute(commands, = nil, &block) connect unless connected? environment = Environment.new(self) environment. = (, block) environment.command = prepare_commands(commands, environment., add_sudo: false) command, environment.captures = @command_set.match(environment.command) raise UnsupportedCommandError, environment.command if command.nil? response = command.make_response(environment) response.bytes_streamed = environment..file_to_stream.size if environment..file_to_stream @executed_commands << ExecutedCommand.new(command, environment, response) handle_response(response, environment.) end |
#find_executed_commands(matcher) ⇒ Object
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/sshake/mock/session.rb', line 79 def find_executed_commands(matcher) if matcher.is_a?(Regexp) matcher = /\A#{matcher}\z/ else matcher = /\A#{Regexp.escape(matcher.to_s)}\z/ end @executed_commands.select do |command| command.environment.command =~ matcher end end |
#has_executed_command?(matcher) ⇒ Boolean
rubocop:disable Naming/PredicateName
91 92 93 |
# File 'lib/sshake/mock/session.rb', line 91 def has_executed_command?(matcher) find_executed_commands(matcher).size.positive? end |
#kill! ⇒ Object
49 50 51 |
# File 'lib/sshake/mock/session.rb', line 49 def kill! disconnect end |
#write_data(path, data, _options = nil) ⇒ Object
73 74 75 76 77 |
# File 'lib/sshake/mock/session.rb', line 73 def write_data(path, data, = nil) connect unless connected? @written_files[path] = data true end |