hussh 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/README.rdoc +46 -0
- data/lib/hussh.rb +119 -0
- data/lib/hussh/channel.rb +51 -0
- data/lib/hussh/configuration.rb +41 -0
- data/lib/hussh/session.rb +96 -0
- data/lib/hussh/version.rb +4 -0
- data/spec/hussh_channel_spec.rb +149 -0
- data/spec/hussh_configuration_spec.rb +62 -0
- data/spec/hussh_session_spec.rb +78 -0
- data/spec/spec_helper.rb +91 -0
- metadata +184 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ab64923b5852ed124f71625eb035668ffac73fe7
|
4
|
+
data.tar.gz: 4eb55836fcacc42470e7376a2ae6e9153e09575a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4cbac79becbae698f895437051fb3ae239859ae67ac417913e5cce135235d0f1c535e307f1c8bca8f28b59ee16151680c8c944e5fdacb172a0956bbe03a04665
|
7
|
+
data.tar.gz: 84e10d5ded1e5b56151677b6c58172ec2c1c3c0a09a729f39c485483979a7fec24922b9ba04c4f94b91fd480d48d8e9baf84c681bf1ee06aa1474aa4a270dc2c
|
data/README.rdoc
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
= Hussh
|
2
|
+
|
3
|
+
A mocking library for <tt>Net::SSH</tt> which allows testers to specify
|
4
|
+
responses and record real-life responses for later use.
|
5
|
+
|
6
|
+
{<img src="https://travis-ci.org/moneyadviceservice/hussh.svg?branch=master" alt="Build Status" />}[https://travis-ci.org/moneyadviceservice/hussh]
|
7
|
+
|
8
|
+
== Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'hussh'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself:
|
19
|
+
|
20
|
+
$ gem install hussh
|
21
|
+
|
22
|
+
== Usage
|
23
|
+
|
24
|
+
Configure Hussh for use with RSpec:
|
25
|
+
|
26
|
+
Hussh.configure do |c|
|
27
|
+
c.configure_rspec
|
28
|
+
end
|
29
|
+
|
30
|
+
And then tag a group of specs with <tt>hussh</tt>:
|
31
|
+
|
32
|
+
...
|
33
|
+
describe 'running ssh', :hussh do
|
34
|
+
...
|
35
|
+
end
|
36
|
+
...
|
37
|
+
|
38
|
+
== Contributing
|
39
|
+
|
40
|
+
1. Fork it (https://github.com/moneyadviceservice/hussh/fork)
|
41
|
+
2. Create your feature branch (<tt>git checkout -b my-new-feature</tt>)
|
42
|
+
3. Commit your changes (<tt>git commit -am 'Add some feature'</tt>)
|
43
|
+
4. Push to the branch (<tt>git push origin my-new-feature</tt>)
|
44
|
+
5. Create a new Pull Request
|
45
|
+
|
46
|
+
|
data/lib/hussh.rb
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
require 'net/ssh'
|
2
|
+
require 'pry'
|
3
|
+
require 'logger'
|
4
|
+
|
5
|
+
require 'hussh/configuration'
|
6
|
+
require 'hussh/session'
|
7
|
+
|
8
|
+
module Net
|
9
|
+
module SSH
|
10
|
+
class << self
|
11
|
+
def start_with_hussh(host, user, options = {}, &block)
|
12
|
+
# TODO: Let's do this later once everything else is working.
|
13
|
+
# if Hussh.has_recording? || Hussh.allow_connections?
|
14
|
+
session = Hussh::Session.new(host, user)
|
15
|
+
if block_given?
|
16
|
+
yield session
|
17
|
+
session.close
|
18
|
+
else
|
19
|
+
session
|
20
|
+
end
|
21
|
+
# end
|
22
|
+
end
|
23
|
+
alias_method :start_without_hussh, :start
|
24
|
+
alias_method :start, :start_with_hussh
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
def mattr_accessor(*syms)
|
31
|
+
syms.each do |sym|
|
32
|
+
class_eval(<<EOD)
|
33
|
+
@@#{sym} = nil unless defined? @@#{sym}
|
34
|
+
def self.#{sym}
|
35
|
+
@@#{sym}
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.#{sym}=(obj)
|
39
|
+
@@#{sym} = obj
|
40
|
+
end
|
41
|
+
EOD
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
module Hussh
|
46
|
+
def self.allow_connections?
|
47
|
+
@@allow_connections ||= false
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.allow_connections!
|
51
|
+
@@allow_connections = true
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.disallow_connections
|
55
|
+
@@allow_connections = false
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.configure
|
59
|
+
yield configuration
|
60
|
+
end
|
61
|
+
|
62
|
+
def self.configuration
|
63
|
+
@@configuration ||= Configuration.new
|
64
|
+
end
|
65
|
+
|
66
|
+
def self.commands_run
|
67
|
+
@@commands_run ||= []
|
68
|
+
end
|
69
|
+
|
70
|
+
@@recordings_directory = 'fixtures/hussh'
|
71
|
+
mattr_accessor :recordings_directory
|
72
|
+
mattr_accessor :stubbed_responses, :recorded_responses
|
73
|
+
|
74
|
+
def self.has_recording?
|
75
|
+
!!@@recorded_responses
|
76
|
+
end
|
77
|
+
|
78
|
+
def self.load_recording(name)
|
79
|
+
@@recording_path = self.path_for_recording(name)
|
80
|
+
@@recording_changed = false
|
81
|
+
if File.exist?(@@recording_path)
|
82
|
+
@@recorded_responses = YAML.load_file(@@recording_path)
|
83
|
+
else
|
84
|
+
self.clear_recorded_responses
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def self.save_recording_if_changed
|
89
|
+
return if !self.recording_changed?
|
90
|
+
@@recording_path ||= self.path_for_recording(name)
|
91
|
+
if !File.exist?(@@recording_path)
|
92
|
+
FileUtils.mkdir_p(File.dirname(@@recording_path))
|
93
|
+
end
|
94
|
+
File.write(@@recording_path, @@recorded_responses.to_yaml)
|
95
|
+
@@recording_changed = false
|
96
|
+
end
|
97
|
+
|
98
|
+
def self.clear_recorded_responses
|
99
|
+
@@recorded_responses = {}
|
100
|
+
end
|
101
|
+
|
102
|
+
def self.recording_changed?
|
103
|
+
@@recording_changed
|
104
|
+
end
|
105
|
+
|
106
|
+
def self.recording_changed!
|
107
|
+
@@recording_changed = true
|
108
|
+
end
|
109
|
+
|
110
|
+
def self.clear_stubbed_responses
|
111
|
+
@@stubbed_responses = {}
|
112
|
+
end
|
113
|
+
|
114
|
+
# private
|
115
|
+
def self.path_for_recording(name)
|
116
|
+
"#{@@recordings_directory}/#{name}.yaml"
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Hussh
|
2
|
+
class Channel
|
3
|
+
def initialize(session)
|
4
|
+
@session = session
|
5
|
+
@request_pty = false
|
6
|
+
end
|
7
|
+
|
8
|
+
def real_channel
|
9
|
+
@real_channel ||= @session.real_session.open_channel
|
10
|
+
end
|
11
|
+
|
12
|
+
def response_data
|
13
|
+
@data
|
14
|
+
end
|
15
|
+
|
16
|
+
attr :command
|
17
|
+
attr :exec_block
|
18
|
+
def exec(command, &block)
|
19
|
+
@command = command
|
20
|
+
@exec_block = block
|
21
|
+
end
|
22
|
+
|
23
|
+
attr :on_data_block
|
24
|
+
def on_data(&block)
|
25
|
+
@on_data_block = block
|
26
|
+
end
|
27
|
+
|
28
|
+
attr :on_extended_data_block
|
29
|
+
def on_extended_data(&block)
|
30
|
+
@on_extended_data_block = block
|
31
|
+
end
|
32
|
+
|
33
|
+
attr :request_pty_block
|
34
|
+
def request_pty(&block)
|
35
|
+
@request_pty = true
|
36
|
+
@request_pty_block = block
|
37
|
+
end
|
38
|
+
|
39
|
+
def requested_pty?
|
40
|
+
@request_pty
|
41
|
+
end
|
42
|
+
|
43
|
+
# def on_close(&block)
|
44
|
+
# @on_close = block
|
45
|
+
# end
|
46
|
+
|
47
|
+
# def close
|
48
|
+
# @real_channel.close if @real_channel
|
49
|
+
# end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module Hussh
|
2
|
+
class Configuration
|
3
|
+
def recordings_directory(directory)
|
4
|
+
Hussh.recordings_directory = directory
|
5
|
+
end
|
6
|
+
|
7
|
+
def configure_rspec
|
8
|
+
::RSpec.configure do |config|
|
9
|
+
recording_name_for = lambda do |metadata|
|
10
|
+
if metadata.has_key?(:parent_example_group)
|
11
|
+
recording_name_for[metadata[:parent_example_group]] +
|
12
|
+
metadata[:description]
|
13
|
+
elsif metadata.has_key?(:example_group)
|
14
|
+
recording_name_for[metadata[:example_group]] +
|
15
|
+
metadata[:description]
|
16
|
+
else
|
17
|
+
Pathname.new(metadata[:description])
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
config.before(:each, hussh: lambda { |v| !!v }) do |example|
|
22
|
+
options = example.metadata[:hussh]
|
23
|
+
options = options.is_a?(Hash) ? options.dup : {}
|
24
|
+
recording_name = options.delete(:recording_name) ||
|
25
|
+
recording_name_for[example.metadata]
|
26
|
+
Hussh.load_recording(recording_name)
|
27
|
+
Hussh.clear_stubbed_responses
|
28
|
+
end
|
29
|
+
|
30
|
+
config.after(:each, hussh: lambda { |v| !!v }) do |example|
|
31
|
+
options = example.metadata[:hussh]
|
32
|
+
options = options.is_a?(Hash) ? options.dup : {}
|
33
|
+
Hussh.save_recording_if_changed
|
34
|
+
Hussh.clear_recorded_responses
|
35
|
+
Hussh.clear_stubbed_responses
|
36
|
+
Hussh.commands_run.clear
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
require 'hussh/channel'
|
2
|
+
|
3
|
+
module Hussh
|
4
|
+
class Session
|
5
|
+
def initialize(host, user)
|
6
|
+
@host = host
|
7
|
+
@user = user
|
8
|
+
end
|
9
|
+
|
10
|
+
def real_session
|
11
|
+
@real_session ||= Net::SSH.start_without_hussh(@host, @user)
|
12
|
+
end
|
13
|
+
|
14
|
+
def has_response?(command)
|
15
|
+
Hussh.stubbed_responses.fetch(@host, {}).fetch(@user, {})
|
16
|
+
.has_key?(command) ||
|
17
|
+
Hussh.recorded_responses.fetch(@host, {}).fetch(@user, {})
|
18
|
+
.has_key?(command)
|
19
|
+
end
|
20
|
+
|
21
|
+
def response_for(command)
|
22
|
+
Hussh.stubbed_responses.fetch(@host, {}).fetch(@user, {}).fetch(
|
23
|
+
command,
|
24
|
+
Hussh.recorded_responses.fetch(@host, {}).fetch(@user, {})[command]
|
25
|
+
)
|
26
|
+
end
|
27
|
+
|
28
|
+
def update_recording(command, response)
|
29
|
+
Hussh.recorded_responses[@host] ||= {}
|
30
|
+
Hussh.recorded_responses[@host][@user] ||= {}
|
31
|
+
Hussh.recorded_responses[@host][@user][command] = response
|
32
|
+
Hussh.recording_changed!
|
33
|
+
end
|
34
|
+
|
35
|
+
def exec!(command)
|
36
|
+
Hussh.commands_run << command
|
37
|
+
if self.has_response?(command)
|
38
|
+
self.response_for(command)
|
39
|
+
else
|
40
|
+
response = real_session.exec!(command)
|
41
|
+
self.update_recording(command, response)
|
42
|
+
response
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def open_channel(&block)
|
47
|
+
@channel = Channel.new(self)
|
48
|
+
block.call(@channel)
|
49
|
+
Hussh.commands_run << @channel.command
|
50
|
+
if self.has_response?(@channel.command)
|
51
|
+
if @channel.exec_block.respond_to?(:call)
|
52
|
+
@channel.exec_block.call(@channel, true)
|
53
|
+
end
|
54
|
+
|
55
|
+
if @channel.on_data_block.respond_to?(:call)
|
56
|
+
@channel.on_data_block.call(@channel, self.response_for(@channel.command))
|
57
|
+
end
|
58
|
+
else
|
59
|
+
self.real_session.open_channel do |ch|
|
60
|
+
|
61
|
+
if @channel.requested_pty?
|
62
|
+
ch.request_pty do |ch, success|
|
63
|
+
if @channel.request_pty_block.respond_to?(:call)
|
64
|
+
@channel.request_pty_block.call(ch, success)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
ch.exec(@channel.command) do |ch, success|
|
70
|
+
@channel.exec_block.call(@channel, success) if @channel.exec_block
|
71
|
+
|
72
|
+
ch.on_data do |ch, output|
|
73
|
+
if @channel.on_data_block.respond_to?(:call)
|
74
|
+
@channel.on_data_block.call(@channel, output)
|
75
|
+
@on_data = output
|
76
|
+
self.update_recording(@channel.command, @on_data)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
ch.on_extended_data do |ch, output|
|
81
|
+
if @channel.on_extended_data_block.respond_to?(:call)
|
82
|
+
@channel.on_extended_data_block.call(@channel, output)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def close
|
92
|
+
@real_session.close if @real_session
|
93
|
+
@real_session = nil
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,149 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'rspec/mocks'
|
3
|
+
require 'fakefs/spec_helpers'
|
4
|
+
require 'yaml'
|
5
|
+
require 'hussh'
|
6
|
+
|
7
|
+
RSpec.describe Hussh do
|
8
|
+
include FakeFS::SpecHelpers
|
9
|
+
|
10
|
+
describe Hussh::Channel do
|
11
|
+
before do
|
12
|
+
Hussh.commands_run.clear
|
13
|
+
Hussh.clear_recorded_responses
|
14
|
+
Hussh.clear_stubbed_responses
|
15
|
+
end
|
16
|
+
|
17
|
+
let!(:channel) do
|
18
|
+
channel = instance_spy('Net::SSH::Connection::Channel')
|
19
|
+
# Setup a fake Channel object which will hopefully behave like a
|
20
|
+
# Net::SSH channel, i.e. register the command and callbacks, and then
|
21
|
+
# call them all with the appropriate data and values.
|
22
|
+
allow(channel).to receive(:exec) do |cmd, &blk|
|
23
|
+
@command = cmd
|
24
|
+
@exec = blk
|
25
|
+
end
|
26
|
+
allow(channel).to receive(:request_pty) { |&block| @request_pty = block }
|
27
|
+
allow(channel).to receive(:on_data) { |&block| @on_data = block }
|
28
|
+
allow(channel).to receive(:on_extended_data) do |&block|
|
29
|
+
@on_extended_data = block
|
30
|
+
end
|
31
|
+
# TODO: loop and wait should also end up calling the callbacks
|
32
|
+
allow(channel).to receive(:close) do
|
33
|
+
# Allow the test to specify a command that fails.
|
34
|
+
@exec.call(channel, !@command.match(/fail/)) if @exec
|
35
|
+
# Allow the test to specify a failed pty request.
|
36
|
+
@request_pty.call(channel, !@command.match(/nopty/)) if @request_pty
|
37
|
+
@on_data.call(channel, "#{@command} output") if @on_data
|
38
|
+
if @on_extended_data
|
39
|
+
@on_extended_data.call(channel, "#{@command} stderr output")
|
40
|
+
end
|
41
|
+
end
|
42
|
+
allow(channel).to receive(:wait) do
|
43
|
+
channel.close
|
44
|
+
end
|
45
|
+
channel
|
46
|
+
end
|
47
|
+
|
48
|
+
let!(:session) do
|
49
|
+
session = instance_spy('Net::SSH::Connection::Session')
|
50
|
+
# The session here is just used to stub our channel in for the real one.
|
51
|
+
allow(session).to receive(:open_channel) do |&blk|
|
52
|
+
blk.call(channel)
|
53
|
+
channel.close
|
54
|
+
end
|
55
|
+
allow(Net::SSH).to receive(:start_without_hussh).and_return(session)
|
56
|
+
session
|
57
|
+
end
|
58
|
+
|
59
|
+
context 'when using an exec block' do
|
60
|
+
before do
|
61
|
+
FileUtils.mkdir_p 'fixtures/hussh'
|
62
|
+
File.write('fixtures/hussh/saved_responses.yaml', saved_responses)
|
63
|
+
Hussh.load_recording('saved_responses')
|
64
|
+
|
65
|
+
# Simulate how we would use Hussh, which sits between the
|
66
|
+
# application code (the code below) and the mocked-out Net::SSH
|
67
|
+
# code.
|
68
|
+
Net::SSH.start('host', 'user') do |session|
|
69
|
+
session.open_channel do |ch|
|
70
|
+
ch.request_pty
|
71
|
+
ch.exec command do |ch, success|
|
72
|
+
@exec_success = success
|
73
|
+
ch.on_data { |c, data| @data = data }
|
74
|
+
ch.on_extended_data { |c, data| @extended_data = data }
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
context 'with a command that has not been run before' do
|
81
|
+
let(:saved_responses) { {}.to_yaml }
|
82
|
+
|
83
|
+
context 'and execution is succesful' do
|
84
|
+
let(:command) { 'test' }
|
85
|
+
|
86
|
+
it 'runs the command via ssh' do
|
87
|
+
expect(channel).to have_received(:exec).with('test')
|
88
|
+
end
|
89
|
+
|
90
|
+
it 'records that the command was run' do
|
91
|
+
expect(Hussh.commands_run).to include('test')
|
92
|
+
end
|
93
|
+
|
94
|
+
it 'passes command status to exec' do
|
95
|
+
expect(@exec_success).to eql(true)
|
96
|
+
end
|
97
|
+
|
98
|
+
it 'gives us the stdout' do
|
99
|
+
expect(@data).to eq 'test output'
|
100
|
+
end
|
101
|
+
|
102
|
+
it 'gives us the stderr' do
|
103
|
+
expect(@extended_data).to eq 'test stderr output'
|
104
|
+
end
|
105
|
+
|
106
|
+
it 'allows us to request a pty' do
|
107
|
+
expect(channel).to have_received(:request_pty)
|
108
|
+
end
|
109
|
+
|
110
|
+
it 'saves the result of the command' do
|
111
|
+
expect(Hussh.recorded_responses['host']['user']['test'])
|
112
|
+
.to eq 'test output'
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'flags the recording as changed' do
|
116
|
+
expect(Hussh.recording_changed?).to eql(true)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
context 'and has failed to execute' do
|
121
|
+
let(:command) { 'test-fail' }
|
122
|
+
subject { @exec_success }
|
123
|
+
it { is_expected.to eq false }
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
context 'with a command that has recorded results' do
|
128
|
+
let(:saved_responses) do
|
129
|
+
{
|
130
|
+
'host' => { 'user' => { 'test' => 'recorded test output' } }
|
131
|
+
}.to_yaml
|
132
|
+
end
|
133
|
+
|
134
|
+
context 'and execution is succesful' do
|
135
|
+
let(:command) { 'test' }
|
136
|
+
|
137
|
+
it 'gives us the stdout' do
|
138
|
+
expect(@data).to eq 'recorded test output'
|
139
|
+
end
|
140
|
+
|
141
|
+
it 'gives us the success status' do
|
142
|
+
expect(@exec_success).to eql(true)
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'rspec/mocks'
|
3
|
+
require 'fakefs/spec_helpers'
|
4
|
+
require 'yaml'
|
5
|
+
require 'hussh'
|
6
|
+
|
7
|
+
RSpec.describe Hussh do
|
8
|
+
include FakeFS::SpecHelpers
|
9
|
+
|
10
|
+
describe Hussh::Configuration do
|
11
|
+
describe :configure_rspec do
|
12
|
+
before do
|
13
|
+
allow(Hussh).to receive(:save_recording_if_changed) do
|
14
|
+
@saved_responses = Hussh.recorded_responses
|
15
|
+
end
|
16
|
+
allow(Hussh).to receive(:clear_recorded_responses)
|
17
|
+
allow(Hussh).to receive(:clear_stubbed_responses)
|
18
|
+
allow(Hussh.commands_run).to receive(:clear)
|
19
|
+
rspec_spy = instance_spy(RSpec::Core::Configuration)
|
20
|
+
allow(rspec_spy).to receive(:before) { |*args, &blk| @before = blk }
|
21
|
+
allow(rspec_spy).to receive(:after) { |*args, &blk| @after = blk }
|
22
|
+
allow(RSpec).to receive(:configure).and_yield(rspec_spy)
|
23
|
+
Hussh.configure { |c| c.configure_rspec }
|
24
|
+
@example = spy(RSpec::Core::Example)
|
25
|
+
allow(@example).to receive(:metadata).and_return(
|
26
|
+
{
|
27
|
+
hussh: true,
|
28
|
+
description: 'Dis Iz A Mock'
|
29
|
+
})
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'after block' do
|
33
|
+
before do
|
34
|
+
Hussh.recorded_responses = {
|
35
|
+
'host' => { 'user' => { 'cmd' => 'output' } }
|
36
|
+
}
|
37
|
+
@after.call(@example)
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'saves the recorded responses' do
|
41
|
+
expect(Hussh).to have_received(:save_recording_if_changed)
|
42
|
+
expect(@saved_responses).to(
|
43
|
+
eq({'host' => {'user' => {'cmd' => 'output'}}})
|
44
|
+
)
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'clears the recorded responses' do
|
48
|
+
expect(Hussh).to have_received(:clear_recorded_responses)
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'clears stubbed responses' do
|
52
|
+
expect(Hussh).to have_received(:clear_stubbed_responses)
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'clears commands run' do
|
56
|
+
expect(Hussh.commands_run).to have_received(:clear)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'rspec/mocks'
|
3
|
+
require 'fakefs/spec_helpers'
|
4
|
+
require 'yaml'
|
5
|
+
require 'hussh'
|
6
|
+
|
7
|
+
RSpec.describe Hussh do
|
8
|
+
include FakeFS::SpecHelpers
|
9
|
+
describe Hussh::Session do
|
10
|
+
describe :exec! do
|
11
|
+
let(:real_session) do
|
12
|
+
spy = instance_spy('Net::SSH::Connection::Session')
|
13
|
+
allow(spy).to receive(:exec!) { |c| "#{c} output" }
|
14
|
+
spy
|
15
|
+
end
|
16
|
+
before do
|
17
|
+
Hussh.commands_run.clear
|
18
|
+
Hussh.clear_recorded_responses
|
19
|
+
Hussh.clear_stubbed_responses
|
20
|
+
allow(Net::SSH).to receive(:start_without_hussh)
|
21
|
+
.and_return(real_session)
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'with a command that has not been run before' do
|
25
|
+
before do
|
26
|
+
Net::SSH.start('host', 'user') { |s| @output = s.exec!('hostname') }
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'runs the command via ssh' do
|
30
|
+
expect(real_session).to have_received(:exec!).with('hostname')
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'records that the command was run' do
|
34
|
+
expect(Hussh.commands_run).to include('hostname')
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'returns the result of the command' do
|
38
|
+
expect(@output).to eql('hostname output')
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'saves the result of the command' do
|
42
|
+
expect(Hussh.recorded_responses['host']['user']['hostname'])
|
43
|
+
.to eql('hostname output')
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'flags the recording as changed' do
|
47
|
+
expect(Hussh.recording_changed?).to eql(true)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
context 'with a command that has been run before' do
|
52
|
+
before do
|
53
|
+
FileUtils.mkdir_p 'fixtures/hussh'
|
54
|
+
File.write(
|
55
|
+
'fixtures/hussh/saved_responses.yaml',
|
56
|
+
{
|
57
|
+
'host' => { 'user' => { 'hostname' => "subsix\n" } }
|
58
|
+
}.to_yaml
|
59
|
+
)
|
60
|
+
Hussh.load_recording('saved_responses')
|
61
|
+
Net::SSH.start('host', 'user') { |s| s.exec!('hostname') }
|
62
|
+
end
|
63
|
+
|
64
|
+
it "doesn't run the command via ssh" do
|
65
|
+
expect(Net::SSH).to_not have_received(:start_without_hussh)
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'records that the command was run' do
|
69
|
+
expect(Hussh.commands_run).to include('hostname')
|
70
|
+
end
|
71
|
+
|
72
|
+
it "doesn't flags the recording as changed" do
|
73
|
+
expect(Hussh.recording_changed?).to eql(false)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
4
|
+
# this file to always be loaded, without a need to explicitly require it in any
|
5
|
+
# files.
|
6
|
+
#
|
7
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
8
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
9
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
10
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
11
|
+
# a separate helper file that requires the additional dependencies and performs
|
12
|
+
# the additional setup, and require it from the spec files that actually need
|
13
|
+
# it.
|
14
|
+
#
|
15
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
16
|
+
# users commonly want.
|
17
|
+
#
|
18
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
19
|
+
RSpec.configure do |config|
|
20
|
+
# rspec-expectations config goes here. You can use an alternate
|
21
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
22
|
+
# assertions if you prefer.
|
23
|
+
config.expect_with :rspec do |expectations|
|
24
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
25
|
+
# and `failure_message` of custom matchers include text for helper methods
|
26
|
+
# defined using `chain`, e.g.:
|
27
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
28
|
+
# # => "be bigger than 2 and smaller than 4"
|
29
|
+
# ...rather than:
|
30
|
+
# # => "be bigger than 2"
|
31
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
32
|
+
end
|
33
|
+
|
34
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
35
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
36
|
+
config.mock_with :rspec do |mocks|
|
37
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
38
|
+
# a real object. This is generally recommended, and will default to
|
39
|
+
# `true` in RSpec 4.
|
40
|
+
mocks.verify_partial_doubles = true
|
41
|
+
end
|
42
|
+
|
43
|
+
# The settings below are suggested to provide a good initial experience
|
44
|
+
# with RSpec, but feel free to customize to your heart's content.
|
45
|
+
=begin
|
46
|
+
# These two settings work together to allow you to limit a spec run
|
47
|
+
# to individual examples or groups you care about by tagging them with
|
48
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
49
|
+
# get run.
|
50
|
+
config.filter_run :focus
|
51
|
+
config.run_all_when_everything_filtered = true
|
52
|
+
|
53
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
54
|
+
# recommended. For more details, see:
|
55
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
56
|
+
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
57
|
+
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
58
|
+
config.disable_monkey_patching!
|
59
|
+
|
60
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
61
|
+
# be too noisy due to issues in dependencies.
|
62
|
+
config.warnings = true
|
63
|
+
|
64
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
65
|
+
# file, and it's useful to allow more verbose output when running an
|
66
|
+
# individual spec file.
|
67
|
+
if config.files_to_run.one?
|
68
|
+
# Use the documentation formatter for detailed output,
|
69
|
+
# unless a formatter has already been configured
|
70
|
+
# (e.g. via a command-line flag).
|
71
|
+
config.default_formatter = 'doc'
|
72
|
+
end
|
73
|
+
|
74
|
+
# Print the 10 slowest examples and example groups at the
|
75
|
+
# end of the spec run, to help surface which specs are running
|
76
|
+
# particularly slow.
|
77
|
+
config.profile_examples = 10
|
78
|
+
|
79
|
+
# Run specs in random order to surface order dependencies. If you find an
|
80
|
+
# order dependency and want to debug it, you can fix the order by providing
|
81
|
+
# the seed, which is printed after each run.
|
82
|
+
# --seed 1234
|
83
|
+
config.order = :random
|
84
|
+
|
85
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
86
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
87
|
+
# test failures related to randomization by passing the same `--seed` value
|
88
|
+
# as the one that triggered the failure.
|
89
|
+
Kernel.srand config.seed
|
90
|
+
=end
|
91
|
+
end
|
metadata
ADDED
@@ -0,0 +1,184 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hussh
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Misha Gorodnitzky
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-04-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rdoc
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '4.2'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '4.2'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.2'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.2'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec-mocks
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.2'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.2'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: fakefs
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0.6'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0.6'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: pry
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0.10'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0.10'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: guard-rspec
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '4.5'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '4.5'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: net-ssh
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '2.9'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '2.9'
|
139
|
+
description: Session-recording library for Net::SSH to make testing easy
|
140
|
+
email: misaka@pobox.com
|
141
|
+
executables: []
|
142
|
+
extensions: []
|
143
|
+
extra_rdoc_files:
|
144
|
+
- README.rdoc
|
145
|
+
files:
|
146
|
+
- README.rdoc
|
147
|
+
- lib/hussh.rb
|
148
|
+
- lib/hussh/channel.rb
|
149
|
+
- lib/hussh/configuration.rb
|
150
|
+
- lib/hussh/session.rb
|
151
|
+
- lib/hussh/version.rb
|
152
|
+
- spec/hussh_channel_spec.rb
|
153
|
+
- spec/hussh_configuration_spec.rb
|
154
|
+
- spec/hussh_session_spec.rb
|
155
|
+
- spec/spec_helper.rb
|
156
|
+
homepage: http://github.com/moneyadviceservice/hussh
|
157
|
+
licenses:
|
158
|
+
- New BSD
|
159
|
+
metadata: {}
|
160
|
+
post_install_message:
|
161
|
+
rdoc_options: []
|
162
|
+
require_paths:
|
163
|
+
- lib
|
164
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
165
|
+
requirements:
|
166
|
+
- - ">="
|
167
|
+
- !ruby/object:Gem::Version
|
168
|
+
version: '0'
|
169
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
170
|
+
requirements:
|
171
|
+
- - ">="
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: '0'
|
174
|
+
requirements: []
|
175
|
+
rubyforge_project:
|
176
|
+
rubygems_version: 2.2.2
|
177
|
+
signing_key:
|
178
|
+
specification_version: 4
|
179
|
+
summary: Session-recording library for Net::SSH to make testing easy
|
180
|
+
test_files:
|
181
|
+
- spec/hussh_channel_spec.rb
|
182
|
+
- spec/hussh_configuration_spec.rb
|
183
|
+
- spec/hussh_session_spec.rb
|
184
|
+
- spec/spec_helper.rb
|