Skip to content

Commit

Permalink
Refactor ATOM
Browse files Browse the repository at this point in the history
Simplify creating the result object
  • Loading branch information
ytti committed Dec 23, 2024
1 parent 89a9269 commit bdf615a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 64 deletions.
22 changes: 7 additions & 15 deletions spec/model/arubainstant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,16 @@
require_relative 'atoms'

describe 'model/ArubaInstant' do
before(:each) do
init_model_helper
@node = Oxidized::Node.new(name: 'example.com',
input: 'ssh',
model: 'arubainstant')
end
before { init_model_helper }

it 'removes secrets' do
Oxidized.config.vars.remove_secret = true
mockmodel = MockSsh.new(ATOMS::TestOutput.new('arubainstant', 'IAP515_8.10.0.6_VWLC'))
Net::SSH.stubs(:start).returns mockmodel

status, result = @node.run
test = ATOMS::TestOutput.new('arubainstant', 'IAP515_8.10.0.6_VWLC')
cfg = MockSsh.get_result(self, test).to_cfg

_(status).must_equal :success
_(result.to_cfg).wont_match(/AAAAAAAAAABBBBBBBBBBCCCCCCCCCC/)
_(result.to_cfg).must_match(/snmp-server host 10.10.42.12 version 2c <secret removed> inform/)
_(result.to_cfg).must_match(/hash-mgmt-user oxidized password hash <secret removed>/)
_(result.to_cfg).must_match(/hash-mgmt-user rocks password hash <secret removed> usertype read-only/)
_(cfg).wont_match(/AAAAAAAAAABBBBBBBBBBCCCCCCCCCC/)
_(cfg).must_match(/snmp-server host 10.10.42.12 version 2c <secret removed> inform/)
_(cfg).must_match(/hash-mgmt-user oxidized password hash <secret removed>/)
_(cfg).must_match(/hash-mgmt-user rocks password hash <secret removed> usertype read-only/)
end
end
4 changes: 1 addition & 3 deletions spec/model/garderos_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
describe 'model/Garderos' do
before(:each) do
init_model_helper
@node = Oxidized::Node.new(name: 'example.com',
input: 'ssh',
model: 'garderos')
@node = MockSsh.get_node('garderos')
end

it 'matches different prompts' do
Expand Down
49 changes: 19 additions & 30 deletions spec/model/ios_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,33 @@
require_relative 'atoms'

describe 'model/IOS' do
before(:each) do
init_model_helper
@node = Oxidized::Node.new(name: 'example.com',
input: 'ssh',
model: 'ios')
end
before { init_model_helper }

it 'removes secrets' do
Oxidized.config.vars.remove_secret = true
mockmodel = MockSsh.new(ATOMS::TestOutput.new('ios', 'C9200L-24P-4G_17.09.04a'))
Net::SSH.stubs(:start).returns mockmodel

status, result = @node.run
test = ATOMS::TestOutput.new('ios', 'C9200L-24P-4G_17.09.04a')
cfg = MockSsh.get_result(self, test).to_cfg

_(status).must_equal :success
_(result.to_cfg).wont_match(/SECRET/)
_(result.to_cfg).wont_match(/public/)
_(result.to_cfg).wont_match(/AAAAAAAAAABBBBBBBBBB/)
_(cfg).wont_match(/SECRET/)
_(cfg).wont_match(/public/)
_(cfg).wont_match(/AAAAAAAAAABBBBBBBBBB/)
end

it 'removes secrets from IOS-XE WLCs' do
Oxidized.config.vars.remove_secret = true
mockmodel = MockSsh.new(ATOMS::TestOutput.new('ios', 'C9800-L-F-K9_17.06.05'))
Net::SSH.stubs(:start).returns mockmodel

status, result = @node.run
test = ATOMS::TestOutput.new('ios', 'C9800-L-F-K9_17.06.05')
cfg = MockSsh.get_result(self, test).to_cfg

_(status).must_equal :success
_(result.to_cfg).wont_match(/SECRET_REMOVED/)
_(result.to_cfg).wont_match(/REMOVED_SECRET/)
_(result.to_cfg).wont_match(/WLANSECR3T/)
_(result.to_cfg).wont_match(/WLAN SECR3T/)
_(result.to_cfg).wont_match(/7df35f90c92ecff2a803e79577b85e978edc0a76404f6cfb534df8d9f9f67beb/)
_(result.to_cfg).wont_match(/DOT1XPASSW0RD/)
_(result.to_cfg).wont_match(/MGMTPASSW0RD/)
_(result.to_cfg).wont_match(/MGMTSECR3T/)
_(result.to_cfg).wont_match(/DOT1X PASSW0RD/)
_(result.to_cfg).wont_match(/MGMT PASSW0RD/)
_(result.to_cfg).wont_match(/MGMT SECR3T/)
_(cfg).wont_match(/SECRET_REMOVED/)
_(cfg).wont_match(/REMOVED_SECRET/)
_(cfg).wont_match(/WLANSECR3T/)
_(cfg).wont_match(/WLAN SECR3T/)
_(cfg).wont_match(/7df35f90c92ecff2a803e79577b85e978edc0a76404f6cfb534df8d9f9f67beb/)
_(cfg).wont_match(/DOT1XPASSW0RD/)
_(cfg).wont_match(/MGMTPASSW0RD/)
_(cfg).wont_match(/MGMTSECR3T/)
_(cfg).wont_match(/DOT1X PASSW0RD/)
_(cfg).wont_match(/MGMT PASSW0RD/)
_(cfg).wont_match(/MGMT SECR3T/)
end
end
17 changes: 3 additions & 14 deletions spec/model/model_atoms_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
require_relative 'model_helper'
require_relative 'atoms'

# FIXME: why can't I do this in before block?
def get_node(model)
Oxidized::Node.new(name: 'example.com',
input: 'ssh',
model: model)
end

describe 'ATOMS tests' do
ATOMS.get.each do |test|
test_string = "ATOMS/#{test.type} (#{test.model} / #{test.desc})"
Expand All @@ -17,18 +10,14 @@ def get_node(model)
if test.type == 'output'
it "#{test_string} has expected output" do
skip("check simulation+output data file for #{test_string}") if test.skip?
@node = get_node(test.model)
mockmodel = MockSsh.new(test)
Net::SSH.stubs(:start).returns mockmodel
status, result = @node.run
_(status).must_equal :success
_(result.to_cfg).must_equal mockmodel.oxidized_output
cfg = MockSsh.get_result(self, test).to_cfg
_(cfg).must_match test.output
end

elsif test.type == 'prompt'
it "#{test_string} has working prompt detection" do
skip("check prompt data file for #{test_string}") if test.skip?
@node = get_node(test.model)
@node = MockSsh.get_node(test.model)
class_sym = Object.constants.find { |const| const.to_s.casecmp(test.model).zero? }
prompt_re = Object.const_get(class_sym).prompt
test.pass.each do |want_pass|
Expand Down
16 changes: 14 additions & 2 deletions spec/model/model_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,20 @@ def result2file(result, filename)

# Class to Simulate Net::SSH::Connection::Session
class MockSsh
attr_reader :oxidized_output
def self.get_node(model)
Oxidized::Node.new(name: 'example.com',
input: 'ssh',
model: model)
end

def self.get_result(context, test)
@node = get_node(test.model)
mockmodel = MockSsh.new(test)
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)
Expand All @@ -45,7 +58,6 @@ def initialize(test)
end

@init_prompt = interpolate_yaml(model['init_prompt'])
@oxidized_output = test.output
end

# We have to interpolate as yaml block scalars don't interpolate anything
Expand Down

0 comments on commit bdf615a

Please sign in to comment.