From 97b0219af89be2284bc951e31ee3b0e1a054ae42 Mon Sep 17 00:00:00 2001 From: Bogdan Irimie Date: Wed, 29 Apr 2020 18:02:41 +0300 Subject: [PATCH 1/2] (FACT-2590) Add Rhel to hierarchy. --- lib/framework/detector/os_detector.rb | 12 ++++++++++-- os_hierarchy.json | 3 ++- spec/framework/detector/os_detector_spec.rb | 2 ++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/framework/detector/os_detector.rb b/lib/framework/detector/os_detector.rb index 1a1ad8fb1..01a5baab5 100644 --- a/lib/framework/detector/os_detector.rb +++ b/lib/framework/detector/os_detector.rb @@ -8,6 +8,7 @@ class OsDetector attr_reader :identifier, :version, :hierarchy def initialize(*_args) + @log = Facter::Log.new(self) @os_hierarchy = Facter::OsHierarchy.new @identifier = detect end @@ -39,8 +40,15 @@ def detect def detect_hierarchy(identifier) hierarchy = @os_hierarchy.construct_hierarchy(identifier) - hierarchy = @os_hierarchy.construct_hierarchy(detect_family) if hierarchy.empty? - hierarchy = @os_hierarchy.construct_hierarchy(:linux) if hierarchy.empty? + if hierarchy.empty? + @log.debug("Could not detect hierarchy using os identifier: #{identifier} , trying with family") + hierarchy = @os_hierarchy.construct_hierarchy(detect_family) + end + + if hierarchy.empty? + @log.debug("Could not detect hierarchy using family #{detect_family}, falling back to Linux") + hierarchy = @os_hierarchy.construct_hierarchy(:linux) + end hierarchy end diff --git a/os_hierarchy.json b/os_hierarchy.json index f8f523596..5fa482fc2 100644 --- a/os_hierarchy.json +++ b/os_hierarchy.json @@ -12,7 +12,8 @@ "El": [ "Fedora", "Amzn", - "Centos" + "Centos", + "Rhel" ] }, { diff --git a/spec/framework/detector/os_detector_spec.rb b/spec/framework/detector/os_detector_spec.rb index 190156255..b6eccbf7b 100644 --- a/spec/framework/detector/os_detector_spec.rb +++ b/spec/framework/detector/os_detector_spec.rb @@ -4,10 +4,12 @@ describe OsDetector do let(:os_hierarchy) { instance_spy(Facter::OsHierarchy) } + let(:logger) { instance_spy(Facter::Log) } before do Singleton.__init__(OsDetector) + allow(Facter::Log).to receive(:new).and_return(logger) allow(Facter::OsHierarchy).to receive(:new).and_return(os_hierarchy) end From ce9251411fc1a80fe3bc4a53f0296757054f16db Mon Sep 17 00:00:00 2001 From: Bogdan Irimie Date: Thu, 30 Apr 2020 10:34:03 +0300 Subject: [PATCH 2/2] (FACT-2590) Add test for debug messages. --- spec/framework/detector/os_detector_spec.rb | 28 +++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/spec/framework/detector/os_detector_spec.rb b/spec/framework/detector/os_detector_spec.rb index b6eccbf7b..a354876e7 100644 --- a/spec/framework/detector/os_detector_spec.rb +++ b/spec/framework/detector/os_detector_spec.rb @@ -171,6 +171,26 @@ expect(OsDetector.instance.identifier).to eq(:my_linux_distro) end + context 'when no hierarchy for os identifier' do + it 'logs debug message' do + OsDetector.instance + + expect(logger) + .to have_received(:debug) + .with('Could not detect hierarchy using os identifier: my_linux_distro , trying with family') + end + end + + context 'when no os family detected' do + it 'logs debug message' do + OsDetector.instance + + expect(logger) + .to have_received(:debug) + .with('Could not detect hierarchy using family , falling back to Linux') + end + end + it 'constructs hierarchy with linux' do expect(OsDetector.instance.hierarchy).to eq(['linux']) end @@ -185,6 +205,14 @@ it 'constructs hierarchy with linux' do expect(OsDetector.instance.hierarchy).to eq(%w[Linux Debian Ubuntu]) end + + it 'logs debug message' do + OsDetector.instance + + expect(logger) + .to have_received(:debug) + .with('Could not detect hierarchy using os identifier: my_linux_distro , trying with family') + end end end end