|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'ostruct' |
| 4 | + |
| 5 | +require_relative '../../spec_helper' |
| 6 | + |
| 7 | +require OctocatalogDiff::Spec.require_path('/api/v1/config') |
| 8 | + |
| 9 | +describe OctocatalogDiff::API::V1::Config do |
| 10 | + before(:each) do |
| 11 | + @logger, @logger_str = OctocatalogDiff::Spec.setup_logger |
| 12 | + end |
| 13 | + |
| 14 | + after(:each) do |
| 15 | + begin |
| 16 | + OctocatalogDiff.send(:remove_const, :Config) |
| 17 | + rescue NameError # rubocop:disable Lint/HandleExceptions |
| 18 | + # Don't care if it's not defined already |
| 19 | + end |
| 20 | + end |
| 21 | + |
| 22 | + describe '#config' do |
| 23 | + end |
| 24 | + |
| 25 | + describe '#debug_config_file' do |
| 26 | + it 'should raise error if non-hash is passed in' do |
| 27 | + expect do |
| 28 | + described_class.debug_config_file(nil, @logger) |
| 29 | + end.to raise_error(ArgumentError, /Settings must be hash not NilClass/) |
| 30 | + end |
| 31 | + |
| 32 | + it 'should log keys and values in the hash' do |
| 33 | + described_class.debug_config_file({ foo: 'bar', baz: ['buzz'] }, @logger) |
| 34 | + expect(@logger_str.string).to match(/:foo => \(String\) "bar"/) |
| 35 | + expect(@logger_str.string).to match(/:baz => \(Array\) \["buzz"\]/) |
| 36 | + end |
| 37 | + end |
| 38 | + |
| 39 | + describe '#load_config_file' do |
| 40 | + context 'with a non-existent file' do |
| 41 | + let(:filename) { OctocatalogDiff::Spec.fixture_path('this-does-not-exist.rb') } |
| 42 | + |
| 43 | + it 'should raise Errno::ENOENT' do |
| 44 | + expect do |
| 45 | + described_class.load_config_file(filename, @logger) |
| 46 | + end.to raise_error(Errno::ENOENT, /File.+doesn't exist/) |
| 47 | + end |
| 48 | + |
| 49 | + it 'should log fatal message' do |
| 50 | + exception = nil |
| 51 | + begin |
| 52 | + described_class.load_config_file(filename, @logger) |
| 53 | + rescue Errno::ENOENT => exc |
| 54 | + exception = exc |
| 55 | + end |
| 56 | + expect(exception.message).to match(/this-does-not-exist.rb doesn't exist/) |
| 57 | + expect(@logger_str.string).to match(/FATAL .+ Errno::ENOENT error with .+this-does-not-exist.rb/) |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + context 'with an invalid file' do |
| 62 | + let(:filename) { OctocatalogDiff::Spec.fixture_path('cli-configs/not-ruby.rb') } |
| 63 | + |
| 64 | + it 'should raise SyntaxError' do |
| 65 | + expect do |
| 66 | + described_class.load_config_file(filename, @logger) |
| 67 | + end.to raise_error(SyntaxError) |
| 68 | + end |
| 69 | + |
| 70 | + it 'should log fatal message' do |
| 71 | + exception = nil |
| 72 | + begin |
| 73 | + described_class.load_config_file(filename, @logger) |
| 74 | + rescue SyntaxError => exc |
| 75 | + exception = exc |
| 76 | + end |
| 77 | + expect(exception).to be_a_kind_of(SyntaxError) |
| 78 | + expect(exception.message).to match(/unexpected tIDENTIFIER/) |
| 79 | + expect(@logger_str.string).to match(/DEBUG -- : Loading octocatalog-diff configuration from/) |
| 80 | + expect(@logger_str.string).to match(/FATAL .+ SyntaxError error with .+not-ruby.rb/) |
| 81 | + end |
| 82 | + end |
| 83 | + |
| 84 | + context 'with a file that throws an exception' do |
| 85 | + let(:filename) { OctocatalogDiff::Spec.fixture_path('cli-configs/invalid.rb') } |
| 86 | + |
| 87 | + it 'should raise RuntimeError' do |
| 88 | + expect do |
| 89 | + described_class.load_config_file(filename, @logger) |
| 90 | + end.to raise_error(RuntimeError, /Fizz Buzz/) |
| 91 | + end |
| 92 | + |
| 93 | + it 'should log fatal message' do |
| 94 | + exception = nil |
| 95 | + begin |
| 96 | + described_class.load_config_file(filename, @logger) |
| 97 | + rescue RuntimeError => exc |
| 98 | + exception = exc |
| 99 | + end |
| 100 | + expect(exception).to be_a_kind_of(RuntimeError) |
| 101 | + expect(exception.message).to eq('Fizz Buzz') |
| 102 | + expect(@logger_str.string).to match(/DEBUG -- : Loading octocatalog-diff configuration from/) |
| 103 | + expect(@logger_str.string).to match(/FATAL .+ RuntimeError error with .+invalid.rb/) |
| 104 | + end |
| 105 | + end |
| 106 | + |
| 107 | + context 'with a file that does not define a .config method' do |
| 108 | + let(:filename) { OctocatalogDiff::Spec.fixture_path('cli-configs/not-proper.rb') } |
| 109 | + |
| 110 | + it 'should raise ConfigurationFileContentError' do |
| 111 | + pattern = Regexp.new('must define OctocatalogDiff::Config.config!') |
| 112 | + expect do |
| 113 | + described_class.load_config_file(filename, @logger) |
| 114 | + end.to raise_error(OctocatalogDiff::API::V1::Config::ConfigurationFileContentError, pattern) |
| 115 | + end |
| 116 | + |
| 117 | + it 'should log fatal message' do |
| 118 | + exception = nil |
| 119 | + begin |
| 120 | + described_class.load_config_file(filename, @logger) |
| 121 | + rescue OctocatalogDiff::API::V1::Config::ConfigurationFileContentError => exc |
| 122 | + exception = exc |
| 123 | + end |
| 124 | + expect(exception).to be_a_kind_of(OctocatalogDiff::API::V1::Config::ConfigurationFileContentError) |
| 125 | + expect(exception.message).to eq('Configuration must define OctocatalogDiff::Config.config!') |
| 126 | + expect(@logger_str.string).to match(/Configuration must define OctocatalogDiff::Config.config!/) |
| 127 | + end |
| 128 | + end |
| 129 | + |
| 130 | + context 'with a file that does not define a hash' do |
| 131 | + end |
| 132 | + |
| 133 | + context 'with a valid file' do |
| 134 | + let(:filename) { OctocatalogDiff::Spec.fixture_path('cli-configs/valid.rb') } |
| 135 | + |
| 136 | + it 'should return the expected settings' do |
| 137 | + result = described_class.load_config_file(filename, @logger) |
| 138 | + answer = { header: :default, hiera_config: 'config/hiera.yaml', hiera_path: 'hieradata' } |
| 139 | + expect(result).to eq(answer) |
| 140 | + end |
| 141 | + |
| 142 | + it 'should log debug message' do |
| 143 | + described_class.load_config_file(filename, @logger) |
| 144 | + expect(@logger_str.string).to match(/DEBUG -- : Loading octocatalog-diff configuration from/) |
| 145 | + end |
| 146 | + end |
| 147 | + end |
| 148 | + |
| 149 | + describe '#first_file' do |
| 150 | + end |
| 151 | +end |
0 commit comments