From bdf615ac664847d25888bb072cac6f3e5601c0ac Mon Sep 17 00:00:00 2001 From: Saku Ytti Date: Mon, 23 Dec 2024 20:52:29 +0200 Subject: [PATCH] Refactor ATOM Simplify creating the result object --- spec/model/arubainstant_spec.rb | 22 +++++---------- spec/model/garderos_spec.rb | 4 +-- spec/model/ios_spec.rb | 49 +++++++++++++-------------------- spec/model/model_atoms_spec.rb | 17 ++---------- spec/model/model_helper.rb | 16 +++++++++-- 5 files changed, 44 insertions(+), 64 deletions(-) diff --git a/spec/model/arubainstant_spec.rb b/spec/model/arubainstant_spec.rb index 371ee5e36..827aadadf 100644 --- a/spec/model/arubainstant_spec.rb +++ b/spec/model/arubainstant_spec.rb @@ -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 inform/) - _(result.to_cfg).must_match(/hash-mgmt-user oxidized password hash /) - _(result.to_cfg).must_match(/hash-mgmt-user rocks password hash usertype read-only/) + _(cfg).wont_match(/AAAAAAAAAABBBBBBBBBBCCCCCCCCCC/) + _(cfg).must_match(/snmp-server host 10.10.42.12 version 2c inform/) + _(cfg).must_match(/hash-mgmt-user oxidized password hash /) + _(cfg).must_match(/hash-mgmt-user rocks password hash usertype read-only/) end end diff --git a/spec/model/garderos_spec.rb b/spec/model/garderos_spec.rb index f57c5e9a7..4a0918268 100644 --- a/spec/model/garderos_spec.rb +++ b/spec/model/garderos_spec.rb @@ -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 diff --git a/spec/model/ios_spec.rb b/spec/model/ios_spec.rb index 93b31360d..709fa0f5a 100644 --- a/spec/model/ios_spec.rb +++ b/spec/model/ios_spec.rb @@ -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 diff --git a/spec/model/model_atoms_spec.rb b/spec/model/model_atoms_spec.rb index fbcb7d058..29178c09c 100644 --- a/spec/model/model_atoms_spec.rb +++ b/spec/model/model_atoms_spec.rb @@ -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})" @@ -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| diff --git a/spec/model/model_helper.rb b/spec/model/model_helper.rb index bb09a5731..e552fe61e 100644 --- a/spec/model/model_helper.rb +++ b/spec/model/model_helper.rb @@ -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) @@ -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