require 'spec_helper' require 'job_helper' describe Marty::Diagnostic::Reporter do # used to stub request object class DummyRequest attr_accessor :params, :port end def params diagnostic = 'base', scope = nil { op: diagnostic, scope: scope } end def git message = `cd #{Rails.root}; git describe --tags --always;`.strip rescue StandardError message = error('Failed accessing git') end def aggregate_data opts = {} { 'Dummy' => { 'NodeA' => { 'ImportantTest' => { 'description' => 'A', 'status' => opts[:status].nil? ? true : opts[:status], 'consistent' => opts[:consistent].nil? ? true : opts[:consistent], } } } } end def aggregate_consistency_data diagnostic = 'Base' original_a = Marty::Diagnostic::Base.create_info('A') original_b = Marty::Diagnostic::Base.create_info('B') data = { 'CONSTANTA' => original_a, 'CONSTANTB' => original_b, 'CONSTANTB2' => original_b, } different_b = Marty::Diagnostic::Base.create_info('C') test = { diagnostic => { 'NodeA' => data, 'NodeB' => data + { 'CONSTANTB' => different_b, 'CONSTANTB2' => different_b }, } } inconsistent_b = Marty::Diagnostic::Base.create_info('B', true, false) inconsistent_c = Marty::Diagnostic::Base.create_info('C', true, false) if diagnostic == 'EnvironmentVariables' expected = { diagnostic => { 'NodeA' => { 'CONSTANTB' => inconsistent_b, 'CONSTANTB2' => inconsistent_b, }, 'NodeB' => { 'CONSTANTB' => inconsistent_c, 'CONSTANTB2' => inconsistent_c, }, } } else expected = { diagnostic => { 'NodeA' => { 'CONSTANTA' => original_a + { 'consistent' => true }, 'CONSTANTB' => inconsistent_b, 'CONSTANTB2' => inconsistent_b, }, 'NodeB' => { 'CONSTANTA' => original_a + { 'consistent' => true }, 'CONSTANTB' => inconsistent_c, 'CONSTANTB2' => inconsistent_c, }, } } end [test, expected] end def info v, status, consistent Marty::Diagnostic::Base.create_info(v, status, consistent) end def version_data consistent = true Marty::Diagnostic::Base.pack(include_ip = false) do { 'Marty' => info(Marty::VERSION, true, consistent), 'Delorean' => info(Delorean::VERSION, true, true), 'Mcfly' => info(Mcfly::VERSION, true, true), 'Git' => info(git, true, true), } end end def minimize(str) str.gsub(/\s+/, '') end describe 'display mechanism for version diagnostic' do before(:all) do described_class.diagnostics = [Marty::Diagnostic::Version] end before(:each) do described_class.request = DummyRequest.new end it 'masks consistent nodes for display (version)' do described_class.request.params = params(scope = 'local') data = { 'Version' => { 'NodeA' => version_data, 'NodeB' => version_data, } } expected = <<-ERB

Version

consistent
Marty

#{Marty::VERSION}

Delorean

#{Delorean::VERSION}

Mcfly

#{Mcfly::VERSION}

Git

#{git}

ERB test = Marty::Diagnostic::Reporter.displays(data) expect(minimize(test)).to eq(minimize(expected)) end it 'displays all nodes when there is an inconsistent node (version)' do Marty:: Diagnostic::Reporter.request.params = params bad_ver = '0.0.0' data = { 'Version' => { 'NodeA' => version_data(consistent = false), 'NodeB' => version_data + { 'Marty' => Marty::Diagnostic::Base.create_info(bad_ver, true, false) }, } } expected = <<-ERB

Version

Inconsistency Detected

NodeA NodeB
Marty

#{Marty::VERSION}

#{bad_ver}

Delorean

#{Delorean::VERSION}

#{Delorean::VERSION}

Mcfly

#{Mcfly::VERSION}

#{Mcfly::VERSION}

Git

#{git}

#{git}

ERB test = described_class.displays(data) expect(minimize(test)).to eq(minimize(expected)) end it 'can detect errors in diagnostic for display and api' do described_class.request.params = params n = aggregate_data e = aggregate_data(status: false) c = aggregate_data(consistent: false) ce = aggregate_data(status: false, consistent: false) aggregate_failures do expect(described_class.errors(n)).to eq({}) expect(described_class.errors(e)).not_to eq({}) expect(described_class.errors(c)).not_to eq({}) expect(described_class.errors(ce)).not_to eq({}) end end it 'can survive and display fatal errors' do described_class.request.params = params a_err_a = Marty::Diagnostic::Fatal.message('A', node: 'NodeA') a_err_b = Marty::Diagnostic::Fatal.message('B', node: 'NodeA') b_err_c = Marty::Diagnostic::Fatal.message('C', node: 'NodeB', type: 'OtherError') c_err_d = Marty::Diagnostic::Fatal.message('D', node: 'NodeC', type: 'OtherOtherError') data = [a_err_a, a_err_b, b_err_c, c_err_d].reduce(:deep_merge) expected = <<-ERB

Fatal

Inconsistency Detected

NodeA NodeB NodeC
RuntimeError

B

N/A

N/A

OtherError

N/A

C

N/A

OtherOtherError

N/A

N/A

D

ERB result = described_class.displays(data) expect(minimize(result)).to eq(minimize(expected)) end end describe 'aggregation consistency functionality' do it 'env diagnostic' do test, expected = aggregate_consistency_data('EnvironmentVariables') expect(described_class.consistency(test)).to eq(expected) end it 'marks data as consistent/inconsistent' do test, expected = aggregate_consistency_data expect(described_class.consistency(test)).to eq(expected) end end end