Skip to content

Commit

Permalink
Allow calling SSH mock with test or string
Browse files Browse the repository at this point in the history
Removes the need to always instantiate test first in the model specs
  • Loading branch information
ytti committed Dec 24, 2024
1 parent 5c857e9 commit 1bc3ac7
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
3 changes: 1 addition & 2 deletions spec/model/arubainstant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

it 'removes secrets' do
Oxidized.config.vars.remove_secret = true
test = ATOMS::TestOutput.new('arubainstant', 'IAP515_8.10.0.6_VWLC')
cfg = MockSsh.get_result(self, test).to_cfg
cfg = MockSsh.get_result(self, 'IAP515_8.10.0.6_VWLC').to_cfg

_(cfg).wont_match(/AAAAAAAAAABBBBBBBBBBCCCCCCCCCC/)
_(cfg).must_match(/snmp-server host 10.10.42.12 version 2c <secret removed> inform/)
Expand Down
2 changes: 1 addition & 1 deletion spec/model/garderos_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe 'model/Garderos' do
before(:each) do
init_model_helper
@node = MockSsh.get_node('garderos')
@node = MockSsh.get_node
end

it 'matches different prompts' do
Expand Down
6 changes: 2 additions & 4 deletions spec/model/ios_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

it 'removes secrets' do
Oxidized.config.vars.remove_secret = true
test = ATOMS::TestOutput.new('ios', 'C9200L-24P-4G_17.09.04a')
cfg = MockSsh.get_result(self, test).to_cfg
cfg = MockSsh.get_result(self, 'C9200L-24P-4G_17.09.04a').to_cfg

_(cfg).wont_match(/SECRET/)
_(cfg).wont_match(/public/)
Expand All @@ -15,8 +14,7 @@

it 'removes secrets from IOS-XE WLCs' do
Oxidized.config.vars.remove_secret = true
test = ATOMS::TestOutput.new('ios', 'C9800-L-F-K9_17.06.05')
cfg = MockSsh.get_result(self, test).to_cfg
cfg = MockSsh.get_result(self, 'C9800-L-F-K9_17.06.05').to_cfg

_(cfg).wont_match(/SECRET_REMOVED/)
_(cfg).wont_match(/REMOVED_SECRET/)
Expand Down
16 changes: 11 additions & 5 deletions spec/model/model_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,31 @@ def result2file(result, filename)

# Class to Simulate Net::SSH::Connection::Session
class MockSsh
def self.get_node(model)
def self.caller_model
File.basename(caller_locations[1].path).split('_').first
end

def self.get_node(model = nil)
model ||= caller_model
Oxidized::Node.new(name: 'example.com',
input: 'ssh',
model: model)
end

def self.get_result(context, test)
def self.get_result(context, test_or_desc)
test = test_or_desc
test = ATOMS::TestOutput.new(caller_model, test_or_desc) if test_or_desc.is_a?(String)
@node = get_node(test.model)
mockmodel = MockSsh.new(test)
mockmodel = MockSsh.new(test.simulation)
Net::SSH.stubs(:start).returns mockmodel
status, result = @node.run
context._(status).must_equal :success # rubocop:disable Minitest/GlobalExpectations
result
end

# Takes a yaml file with the data used to simulate the model
def initialize(test)
def initialize(model)
@commands = {}
model = test.simulation
model['commands'].each do |key, value|
@commands[key + "\n"] = interpolate_yaml(value)
end
Expand Down

0 comments on commit 1bc3ac7

Please sign in to comment.