Class: SSHake::Mock::Session

Inherits:
BaseSession show all
Defined in:
lib/sshake/mock/session.rb

Instance Attribute Summary collapse

Attributes inherited from BaseSession

#id, #klogger, #raise_on_error

Instance Method Summary collapse

Constructor Details

#initialize(**options) {|_self| ... } ⇒ Session

Returns a new instance of Session.

Yields:

  • (_self)

Yield Parameters:



16
17
18
19
20
21
22
23
24
# 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

Instance Attribute Details

#command_setObject (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_commandsObject (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

#storeObject (readonly)

Returns the value of attribute store.



14
15
16
# File 'lib/sshake/mock/session.rb', line 14

def store
  @store
end

#written_filesObject (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

#connectObject



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

Returns:

  • (Boolean)


41
42
43
# File 'lib/sshake/mock/session.rb', line 41

def connected?
  @connected == true
end

#disconnectObject



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, 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) ⇒ 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

Returns:

  • (Boolean)


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, _options = nil)
  connect unless connected?
  @written_files[path] = data
  true
end