diff --git a/lib/facts/debian/os/distro/codename.rb b/lib/facts/debian/os/distro/codename.rb index c0e055070..2135ad8d2 100644 --- a/lib/facts/debian/os/distro/codename.rb +++ b/lib/facts/debian/os/distro/codename.rb @@ -9,12 +9,12 @@ class Codename def call_the_resolver fact_value = Facter::Resolvers::OsRelease.resolve(:version_codename) - fact_value ||= retreieve_from_version + fact_value ||= retrieve_from_version Facter::ResolvedFact.new(FACT_NAME, fact_value) end - def retreieve_from_version + def retrieve_from_version version = Facter::Resolvers::OsRelease.resolve(:version) return unless version diff --git a/spec/facter/facter_spec.rb b/spec/facter/facter_spec.rb index ce891e511..fa7736fc4 100644 --- a/spec/facter/facter_spec.rb +++ b/spec/facter/facter_spec.rb @@ -36,98 +36,86 @@ Facter.remove_instance_variable(:@logger) end + def mock_fact_manager(method, return_value) + allow(fact_manager_spy).to receive(method).and_return(return_value) + allow(fact_collection_spy) + .to receive(:build_fact_collection!) + .with(return_value) + .and_return(return_value.empty? ? empty_fact_collection : fact_collection_spy) + end + + def mock_collection(method, os_name = nil, error = nil) + if error + allow(fact_collection_spy).to receive(method).with('os', 'name').and_raise(error) + else + allow(fact_collection_spy).to receive(method).with('os', 'name').and_return(os_name) + end + end + describe '#to_hash' do it 'returns one resolved fact' do - allow(fact_manager_spy).to receive(:resolve_facts).and_return([os_fact]) - allow(fact_collection_spy) - .to receive(:build_fact_collection!) - .with([os_fact]) - .and_return(fact_collection_spy) + mock_fact_manager(:resolve_facts, [os_fact]) - resolved_facts_hash = Facter.to_hash - expect(resolved_facts_hash).to eq(fact_collection_spy) + expect(Facter.to_hash).to eq(fact_collection_spy) end it 'return no resolved facts' do - allow(fact_manager_spy).to receive(:resolve_facts).and_return([]) - allow(fact_collection_spy) - .to receive(:build_fact_collection!) - .with([]) - .and_return(empty_fact_collection) + mock_fact_manager(:resolve_facts, []) - resolved_facts_hash = Facter.to_hash - expect(resolved_facts_hash).to eq(empty_fact_collection) + expect(Facter.to_hash).to eq(empty_fact_collection) end end describe '#to_user_output' do - it 'returns one fact and status 0' do - user_query = 'os.name' - expected_json_output = '{"os" : {"name": "ubuntu"}' - - allow(fact_manager_spy).to receive(:resolve_facts).and_return([os_fact]) + before do |example| + resolved_fact = example.metadata[:resolved_fact] ? [os_fact] : [] + expected_json_output = example.metadata[:resolved_fact] ? '{"os" : {"name": "ubuntu"}' : '{}' + allow(fact_manager_spy).to receive(:resolve_facts).and_return(resolved_fact) json_fact_formatter = double(Facter::JsonFactFormatter) - allow(json_fact_formatter).to receive(:format).with([os_fact]).and_return(expected_json_output) - + allow(json_fact_formatter).to receive(:format).with(resolved_fact).and_return(expected_json_output) allow(Facter::FormatterFactory).to receive(:build).and_return(json_fact_formatter) + end + + it 'returns one fact and status 0', resolved_fact: true do + user_query = 'os.name' + expected_json_output = '{"os" : {"name": "ubuntu"}' formated_facts = Facter.to_user_output({}, [user_query]) + expect(formated_facts).to eq([expected_json_output, 0]) end - it 'returns no facts and status 0' do + it 'returns no facts and status 0', resolved_fact: false do user_query = 'os.name' expected_json_output = '{}' - allow(fact_manager_spy).to receive(:resolve_facts).and_return([]) - - json_fact_formatter = double(Facter::JsonFactFormatter) - allow(json_fact_formatter).to receive(:format).with([]).and_return(expected_json_output) - - allow(Facter::FormatterFactory).to receive(:build).and_return(json_fact_formatter) - formatted_facts = Facter.to_user_output({}, [user_query]) + expect(formatted_facts).to eq([expected_json_output, 0]) end context 'when provided with --strict option' do - before do - allow(Facter::Options).to receive(:[]).with(:config) - end - - it 'returns no fact and status 1' do + it 'returns no fact and status 1', resolved_fact: false do user_query = ['os.name', 'missing_fact'] expected_json_output = '{}' - - allow(fact_manager_spy).to receive(:resolve_facts).and_return([]) allow(Facter::Options).to receive(:[]).and_call_original allow(Facter::Options).to receive(:[]).with(:strict).and_return(true) allow(OsDetector).to receive(:detect).and_return(:solaris) - json_fact_formatter = double(Facter::JsonFactFormatter) - allow(json_fact_formatter).to receive(:format).and_return(expected_json_output) - - allow(Facter::FormatterFactory).to receive(:build).and_return(json_fact_formatter) - formatted_facts = Facter.to_user_output({}, *user_query) + expect(formatted_facts).to eq([expected_json_output, 1]) end - it 'returns one fact and status 0' do + it 'returns one fact and status 0', resolved_fact: true do user_query = 'os.name' expected_json_output = '{"os" : {"name": "ubuntu"}' - - allow(Facter::Options).to receive(:[]).with(:anything) + allow(Facter::Options).to receive(:[]).with(anything) allow(Facter::Options).to receive(:[]).with(:strict).and_return(true) - allow(fact_manager_spy).to receive(:resolve_facts).and_return([os_fact]) - - json_fact_formatter = double(Facter::JsonFactFormatter) - allow(json_fact_formatter).to receive(:format).with([os_fact]).and_return(expected_json_output) - - allow(Facter::FormatterFactory).to receive(:build).and_return(json_fact_formatter) formated_facts = Facter.to_user_output({}, user_query) + expect(formated_facts).to eq([expected_json_output, 0]) end end @@ -135,195 +123,148 @@ describe '#value' do it 'returns a value' do - user_query = 'os.name' - - allow(fact_manager_spy).to receive(:resolve_facts).and_return([os_fact]) - allow(fact_collection_spy) - .to receive(:build_fact_collection!) - .with([os_fact]) - .and_return(fact_collection_spy) - allow(fact_collection_spy).to receive(:value).with('os', 'name').and_return('Ubuntu') + mock_fact_manager(:resolve_facts, [os_fact]) + mock_collection(:value, 'Ubuntu') - resolved_facts_hash = Facter.value(user_query) - expect(resolved_facts_hash).to eq('Ubuntu') + expect(Facter.value('os.name')).to eq('Ubuntu') end it 'return no value' do - user_query = 'os.name' - - allow(fact_manager_spy).to receive(:resolve_facts).and_return([]) - allow(fact_collection_spy) - .to receive(:build_fact_collection!) - .with([]) - .and_return(empty_fact_collection) - allow(fact_collection_spy).to receive(:value).with('os', 'name').and_return(nil) + mock_fact_manager(:resolve_facts, []) + mock_collection(:value, nil) - resolved_facts_hash = Facter.value(user_query) - expect(resolved_facts_hash).to be nil + expect(Facter.value('os.name')).to be nil end end describe '#fact' do it 'returns a fact' do - user_query = 'os.name' - - allow(fact_manager_spy).to receive(:resolve_facts).and_return([os_fact]) - allow(fact_collection_spy) - .to receive(:build_fact_collection!) - .with([os_fact]) - .and_return(fact_collection_spy) - allow(fact_collection_spy).to receive(:value).with('os', 'name').and_return('Ubuntu') + mock_fact_manager(:resolve_facts, [os_fact]) + mock_collection(:value, 'Ubuntu') - result = Facter.fact(user_query) - expect(result).to be_instance_of(Facter::ResolvedFact).and(having_attributes(value: 'Ubuntu')) + expect(Facter.fact('os.name')).to be_instance_of(Facter::ResolvedFact).and have_attributes(value: 'Ubuntu') end it 'can be interpolated' do - user_query = 'os.name' + mock_fact_manager(:resolve_facts, [os_fact]) + mock_collection(:value, 'Ubuntu') - allow(fact_manager_spy).to receive(:resolve_facts).and_return([os_fact]) - allow(fact_collection_spy) - .to receive(:build_fact_collection!) - .with([os_fact]) - .and_return(fact_collection_spy) - allow(fact_collection_spy).to receive(:value).with('os', 'name').and_return('Ubuntu') # rubocop:disable Style/UnneededInterpolation - expect("#{Facter.fact(user_query)}").to eq('Ubuntu') + expect("#{Facter.fact('os.name')}").to eq('Ubuntu') # rubocop:enable Style/UnneededInterpolation end - it 'return no value' do - user_query = 'os.name' + it 'returns no value' do + mock_fact_manager(:resolve_facts, []) + mock_collection(:value, nil, key_error) - allow(fact_manager_spy).to receive(:resolve_facts).and_return([]) - allow(fact_collection_spy) - .to receive(:build_fact_collection!) - .with([]) - .and_return(fact_collection_spy) - - allow(fact_collection_spy).to receive(:value).with('os', 'name').and_raise(key_error) - - result = Facter.fact(user_query) - expect(result).to be_nil + expect(Facter.fact('os.name')).to be_nil end end describe '#[]' do it 'returns a fact' do - user_query = 'os.name' + mock_fact_manager(:resolve_facts, [os_fact]) + mock_collection(:value, 'Ubuntu') - allow(fact_manager_spy).to receive(:resolve_facts).and_return([os_fact]) - allow(fact_collection_spy) - .to receive(:build_fact_collection!) - .with([os_fact]) - .and_return(fact_collection_spy) - allow(fact_collection_spy).to receive(:value).with('os', 'name').and_return('Ubuntu') - - result = Facter[user_query] - expect(result).to be_instance_of(Facter::ResolvedFact).and(having_attributes(value: 'Ubuntu')) + expect(Facter['os.name']).to be_instance_of(Facter::ResolvedFact).and(having_attributes(value: 'Ubuntu')) end it 'return no value' do - user_query = 'os.name' + mock_fact_manager(:resolve_facts, []) + mock_collection(:value, nil, key_error) - allow(fact_manager_spy).to receive(:resolve_facts).and_return([]) - allow(fact_collection_spy) - .to receive(:build_fact_collection!) - .with([]) - .and_return(fact_collection_spy) - allow(fact_collection_spy).to receive(:value).with('os', 'name').and_raise(key_error) - - result = Facter[user_query] - expect(result).to be_nil + expect(Facter['os.name']).to be_nil end end describe '#core_value' do it 'searched in core facts and returns a value' do - user_query = 'os.name' - - allow(fact_manager_spy).to receive(:resolve_core).and_return([os_fact]) - allow(fact_collection_spy) - .to receive(:build_fact_collection!) - .with([os_fact]) - .and_return(fact_collection_spy) - allow(fact_collection_spy).to receive(:dig).with('os', 'name').and_return('Ubuntu') + mock_fact_manager(:resolve_core, [os_fact]) + mock_collection(:dig, 'Ubuntu') - resolved_facts_hash = Facter.core_value(user_query) - expect(resolved_facts_hash).to eq('Ubuntu') + expect(Facter.core_value('os.name')).to eq('Ubuntu') end - it 'searches ion core facts and return no value' do - user_query = 'os.name' - - allow(fact_manager_spy).to receive(:resolve_core).and_return([]) - allow(fact_collection_spy) - .to receive(:build_fact_collection!) - .with([]) - .and_return(fact_collection_spy) - allow(fact_collection_spy).to receive(:dig).with('os', 'name').and_return(nil) + it 'searches os core fact and returns no value' do + mock_fact_manager(:resolve_core, []) + mock_collection(:dig, nil) - resolved_facts_hash = Facter.core_value(user_query) - expect(resolved_facts_hash).to be nil + expect(Facter.core_value('os.name')).to be nil end end - describe '#clear' do - it 'sends call to LegacyFacter' do - expect(LegacyFacter).to receive(:clear).once - Facter.clear + describe 'LegacyFacter methods' do + before do + allow(LegacyFacter).to receive(:clear) + end + + describe '#clear' do + it 'sends call to LegacyFacter' do + Facter.clear + expect(LegacyFacter).to have_received(:clear).once + end end - end - describe '#search' do - it 'sends call to Facter::Options' do - dirs = ['/dir1', '/dir2'] + describe '#search' do + it 'sends call to Facter::Options' do + dirs = ['/dir1', '/dir2'] - expect(Facter::Options).to receive(:[]=).with(:custom_dir, dirs) - Facter.search(*dirs) + expect(Facter::Options).to receive(:[]=).with(:custom_dir, dirs) + Facter.search(*dirs) + end end - end - describe '#search_path' do - it 'sends call to Facter::Options' do - expect(Facter::Options).to receive(:custom_dir).once - Facter.search_path + describe '#search_path' do + it 'sends call to Facter::Options' do + expect(Facter::Options).to receive(:custom_dir).once + Facter.search_path + end end - end - describe '#search_external' do - it 'sends call to Facter::Options' do - dirs = ['/dir1', '/dir2'] - expect(Facter::Options).to receive(:[]=).with(:external_dir, dirs) + describe '#search_external' do + it 'sends call to Facter::Options' do + dirs = ['/dir1', '/dir2'] + expect(Facter::Options).to receive(:[]=).with(:external_dir, dirs) - Facter.search_external(dirs) + Facter.search_external(dirs) + end end - end - describe '#search_external_path' do - it 'sends call to Facter::Options' do - expect(Facter::Options).to receive(:external_dir).once - Facter.search_external_path - end - end + describe '#search_external_path' do + it 'sends call to Facter::Options' do + allow(Facter::Options).to receive(:external_dir) - describe '#reset' do - it 'sends call to LegacyFacter' do - allow(LegacyFacter).to receive(:reset) - Facter.reset - expect(LegacyFacter).to have_received(:reset).once - end + Facter.search_external_path - it 'adds custom facts dirs' do - allow(Facter::Options).to receive(:[]=) - Facter.reset - expect(Facter::Options).to have_received(:[]=).with(:custom_dir, []) + expect(Facter::Options).to have_received(:external_dir).once + end end - it 'add external facts dirs' do - allow(Facter::Options).to receive(:[]=) - Facter.reset - expect(Facter::Options).to have_received(:[]=).with(:external_dir, []) + describe '#reset' do + it 'sends call to LegacyFacter' do + allow(LegacyFacter).to receive(:reset) + + Facter.reset + + expect(LegacyFacter).to have_received(:reset).once + end + + it 'adds custom facts dirs' do + allow(Facter::Options).to receive(:[]=) + + Facter.reset + + expect(Facter::Options).to have_received(:[]=).with(:custom_dir, []) + end + + it 'add external facts dirs' do + allow(Facter::Options).to receive(:[]=) + + Facter.reset + + expect(Facter::Options).to have_received(:[]=).with(:external_dir, []) + end end end @@ -355,6 +296,7 @@ it 'logs a debug message' do allow(logger).to receive(:debug).with('test') + expect(Facter.debug(message)).to be(nil) end end @@ -400,7 +342,9 @@ it 'format exception to display backtrace' do exception.set_backtrace("prog.rb:2:in `a'") + Facter.log_exception(exception, message) + expect(logger).to have_received(:error).with(expected_message) end end