class SSHake::Mock::Session

Attributes

command_set[R]
executed_commands[R]
store[R]
written_files[R]

Public Class Methods

new(**options) { |self| ... } click to toggle source
# File lib/sshake/mock/session.rb, line 16
def initialize(**options)
  @options = options
  @command_set = options[:command_set] || CommandSet.new
  @executed_commands = []
  @store = {}
  @written_files = {}
  @connected = false
  yield(self) if block_given?
end

Public Instance Methods

connect() click to toggle source
# 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?() click to toggle source
# File lib/sshake/mock/session.rb, line 41
def connected?
  @connected == true
end
disconnect() click to toggle source
# File lib/sshake/mock/session.rb, line 45
def disconnect
  @connected = false
end
execute(commands, options = nil, &block) click to toggle source
# File lib/sshake/mock/session.rb, line 53
def execute(commands, options = nil, &block)
  connect unless connected?

  environment = Environment.new(self)

  environment.options = create_options(options, block)
  environment.command = prepare_commands(commands, environment.options, 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.options.file_to_stream.size if environment.options.file_to_stream

  @executed_commands << ExecutedCommand.new(command, environment, response)
  handle_response(response, environment.options)
end
find_executed_commands(matcher) click to toggle source
# 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) click to toggle source

rubocop:disable Naming/PredicateName

# File lib/sshake/mock/session.rb, line 91
def has_executed_command?(matcher)
  find_executed_commands(matcher).size.positive?
end
kill!() click to toggle source
# File lib/sshake/mock/session.rb, line 49
def kill!
  disconnect
end
write_data(path, data, _options = nil) click to toggle source
# File lib/sshake/mock/session.rb, line 73
def write_data(path, data, _options = nil)
  connect unless connected?
  @written_files[path] = data
  true
end