From 32fc1034b9dc84f9bc7fe4327ca43d7363df728c Mon Sep 17 00:00:00 2001 From: Bogdan Irimie Date: Tue, 28 Apr 2020 17:05:49 +0300 Subject: [PATCH 1/2] (maint) Remove rubycritic from CI (#474) --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 3f531d1a7..3dcdce2b3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,7 +21,6 @@ jobs: - rvm: 2.6 script: - bundle exec rubocop - - bundle exec rubycritic --no-browser -f console - bundle exec rake spec - bundle exec rake commits From fa6277caa5f825823a9ec0ad513b3669af8cf5a3 Mon Sep 17 00:00:00 2001 From: Bogdan Irimie Date: Tue, 28 Apr 2020 17:22:42 +0300 Subject: [PATCH 2/2] (FACT-2555)Create OS hierarchy and mechanism for loading it (#470) * (FACT-2555) If no module with the OS name was discovered, log a debug message instead of an error message. * (FACT-2555) Add OsHierarchy Class and json with Os hierarchy definition. * (FACT-2555) Reproduce existing hierarchy in os_hierarchy.json * (FACT-2555) Use OsHierarchy in OsDetector. Rubocop fixes. * (FACT-2555) If the distribution cannot be detected, default to linux * (FACT-2555) Add comments. * (FACT-2555) Add mock for OsHierarchy in OsDetector tests. * (FACT-2555) Add test for fallback to linux in case no distribution is detected. * (FACT-2555) Refactor OsDetector tests. * (FACT-2555) Try to use family to detect os placement in hierarchy. * (FACT-2555) Add mechanism for using os family if no hierarchy can be detected for OS. * (FACT-2555) Add tests for os_hierarchy. * (FACT-2555) Small refactoring of os detector. * (FACT-2555) Add test for family. * (FACT-2555) Force mount-point data to UTF-8 as it might be ASCI in some cases (e.g. when time machine is running on macOS) * (FACT-2555) Move conversion to UTF-8 in helper. * (FACT-2555) Add test for filesusyem_helper. Fix existing tests. * (FACT-2555) Fix os hierarchy for intermediary nodes. Add tests for intermediary and root nodes. * (FACT-2555) Address PR comments. * (FACT-2555) Move the action we are testing in the block. * (FACT-2555) Improve error handling for os_hierarchy. * Revert "(FACT-2555) Add test for filesusyem_helper. Fix existing tests." This reverts commit 0b220cd74fbbd3a32f5684121ce31f5e225b567e. * Revert "(FACT-2555) Move conversion to UTF-8 in helper." This reverts commit a218ac7987fee471253917116cb3e339c90f704d. * Revert "(FACT-2555) Force mount-point data to UTF-8 as it might be ASCI in some cases (e.g. when time machine is running on macOS)" This reverts commit 8cdf10d889e0930a783da3fc624f43561064715b. * (FACT-2555) Add test for cases that throw exceptions. --- .rubocop_todo.yml | 1 + .../core/fact_loaders/class_discoverer.rb | 2 +- lib/framework/core/file_loader.rb | 1 + lib/framework/detector/os_detector.rb | 80 +++---- lib/framework/detector/os_hierarchy.rb | 53 +++++ lib/framework/logging/logger.rb | 2 +- lib/resolvers/redhat_release_resolver.rb | 3 +- lib/resolvers/suse_release_resolver.rb | 4 +- os_hierarchy.json | 33 +++ .../resolvers/redhat_release_resolver_spec.rb | 4 +- .../resolvers/suse_relese_resolver_spec.rb | 4 +- spec/fixtures/broken_os_hierarchy | 32 +++ spec/fixtures/os_hierarchy | 33 +++ spec/framework/detector/os_detector_spec.rb | 204 ++++++++++++++---- spec/framework/detector/os_hierarchy_spec.rb | 93 ++++++++ spec/framework/logging/logger_spec.rb | 2 +- 16 files changed, 454 insertions(+), 97 deletions(-) create mode 100644 lib/framework/detector/os_hierarchy.rb create mode 100644 os_hierarchy.json create mode 100644 spec/fixtures/broken_os_hierarchy create mode 100644 spec/fixtures/os_hierarchy create mode 100644 spec/framework/detector/os_hierarchy_spec.rb diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 625e4ce37..8078b8503 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -90,6 +90,7 @@ RSpec/FilePath: - 'spec/framework/formatters/legacy_fact_formatter_spec.rb' - 'spec/framework/formatters/yaml_fact_formatter_spec.rb' - 'spec/framework/utils/utils_spec.rb' + - 'spec/framework/detector/os_hierarchy_spec.rb' # Offense count: 15 # Configuration parameters: AssignmentOnly. diff --git a/lib/framework/core/fact_loaders/class_discoverer.rb b/lib/framework/core/fact_loaders/class_discoverer.rb index 586ba35a4..ac140f4dc 100644 --- a/lib/framework/core/fact_loaders/class_discoverer.rb +++ b/lib/framework/core/fact_loaders/class_discoverer.rb @@ -15,7 +15,7 @@ def discover_classes(operating_system) find_nested_classes(os_module_name, discovered_classes = []) discovered_classes rescue NameError - @log.error("There is no module named #{operating_system}") + @log.debug("There is no module named #{operating_system}") [] end diff --git a/lib/framework/core/file_loader.rb b/lib/framework/core/file_loader.rb index 711b5a3e4..55818957b 100644 --- a/lib/framework/core/file_loader.rb +++ b/lib/framework/core/file_loader.rb @@ -28,6 +28,7 @@ def load_lib_dirs(*dirs) require "#{ROOT_DIR}/lib/util/file_helper" require "#{ROOT_DIR}/lib/resolvers/base_resolver" +require "#{ROOT_DIR}/lib/framework/detector/os_hierarchy" require "#{ROOT_DIR}/lib/framework/detector/os_detector" require "#{ROOT_DIR}/lib/framework/config/config_reader" diff --git a/lib/framework/detector/os_detector.rb b/lib/framework/detector/os_detector.rb index 03793d40c..1a1ad8fb1 100644 --- a/lib/framework/detector/os_detector.rb +++ b/lib/framework/detector/os_detector.rb @@ -8,31 +8,46 @@ class OsDetector attr_reader :identifier, :version, :hierarchy def initialize(*_args) + @os_hierarchy = Facter::OsHierarchy.new @identifier = detect - @hierarchy = create_hierarchy(@identifier) end + private + def detect host_os = RbConfig::CONFIG['host_os'] - @identifier = case host_os - when /mswin|msys|mingw|cygwin|bccwin|wince|emc/ - :windows - when /darwin|mac os/ - :macosx - when /linux/ - detect_distro - when /bsd/ - :bsd - when /solaris/ - :solaris - when /aix/ - :aix - else - raise "unknown os: #{host_os.inspect}" - end + identifier = case host_os + when /mswin|msys|mingw|cygwin|bccwin|wince|emc/ + :windows + when /darwin|mac os/ + :macosx + when /linux/ + detect_distro + when /bsd/ + :bsd + when /solaris/ + :solaris + when /aix/ + :aix + else + raise "unknown os: #{host_os.inspect}" + end + + @hierarchy = detect_hierarchy(identifier) + @identifier = identifier end - private + 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? + + hierarchy + end + + def detect_family + Facter::Resolvers::OsRelease.resolve(:id_like) + end def detect_distro [Facter::Resolvers::OsRelease, @@ -43,33 +58,6 @@ def detect_distro break if @identifier end - @identifier - end - - def create_hierarchy(operating_system) - return [] unless operating_system - - case operating_system.to_sym - when :ubuntu - %w[Debian] - when :elementary - %w[Debian] - when :raspbian - %w[Debian] - when :fedora - %w[El] - when :amzn - %w[El] - when :rhel - %w[El] - when :centos - %w[El] - when :opensuse - %w[Sles] - when :bsd - %w[Solaris Bsd] - else - [operating_system.to_s.capitalize] - end + @identifier&.downcase&.to_sym end end diff --git a/lib/framework/detector/os_hierarchy.rb b/lib/framework/detector/os_hierarchy.rb new file mode 100644 index 000000000..0b227df3e --- /dev/null +++ b/lib/framework/detector/os_hierarchy.rb @@ -0,0 +1,53 @@ +# frozen_string_literal: true + +module Facter + class OsHierarchy + def initialize + @log = Log.new(self) + json_file = Util::FileHelper.safe_read('os_hierarchy.json') + + begin + @json_os_hierarchy = JSON.parse(json_file) + rescue JSON::ParserError => _e + @log.error('Could not parse os_hierarchy json') + end + end + + def construct_hierarchy(searched_os) + return [] if searched_os.nil? + + searched_os = searched_os.to_s.capitalize + if @json_os_hierarchy.nil? + @log.debug("There is no os_hierarchy, will fall back to: #{searched_os}") + return [searched_os] + end + + @searched_path = [] + search(@json_os_hierarchy, searched_os, []) + + @searched_path.map { |os_name| os_name.to_s.capitalize } + end + + private + + def search(json_data, searched_element, path) + # we hit a dead end, the os was not found on this branch + # and we cannot go deeper + return unless json_data + + json_data.each do |tree_node| + # we found the searched OS, so save the path from the tree + @searched_path = path.dup << tree_node if tree_node == searched_element + + next unless tree_node.is_a?(Hash) + + tree_node.each do |k, v| + return @searched_path = path.dup << k if k == searched_element + + search(v, searched_element, path << k) + path.pop + end + end + end + end +end diff --git a/lib/framework/logging/logger.rb b/lib/framework/logging/logger.rb index a4ed12087..38c6c6d79 100644 --- a/lib/framework/logging/logger.rb +++ b/lib/framework/logging/logger.rb @@ -87,7 +87,7 @@ def warn(msg) def error(msg, colorize = false) @@has_errors = true - msg = colorize(msg, RED) if colorize && !OsDetector.instance.detect.eql?(:windows) + msg = colorize(msg, RED) if colorize && !OsDetector.instance.identifier.eql?(:windows) @@logger.error(@class_name + ' - ' + msg) end diff --git a/lib/resolvers/redhat_release_resolver.rb b/lib/resolvers/redhat_release_resolver.rb index ae4b0be3d..fb89906ae 100644 --- a/lib/resolvers/redhat_release_resolver.rb +++ b/lib/resolvers/redhat_release_resolver.rb @@ -18,7 +18,8 @@ def post_resolve(fact_name) end def read_redhat_release(fact_name) - output, _status = Open3.capture2('cat /etc/redhat-release') + output = Util::FileHelper.safe_read('/etc/redhat-release', nil) + return @fact_list[fact_name] = nil if output.nil? build_fact_list(output) diff --git a/lib/resolvers/suse_release_resolver.rb b/lib/resolvers/suse_release_resolver.rb index 7d3b0cb3f..5bde69b7d 100644 --- a/lib/resolvers/suse_release_resolver.rb +++ b/lib/resolvers/suse_release_resolver.rb @@ -18,7 +18,9 @@ def post_resolve(fact_name) end def read_suse_release(fact_name) - output, _status = Open3.capture2('cat /etc/SuSE-release') + output = Util::FileHelper.safe_read('/etc/SuSE-release', nil) + return @fact_list[fact_name] = nil if output.nil? + output_strings = output.split(' ') @fact_list[:name] = output_strings[0] diff --git a/os_hierarchy.json b/os_hierarchy.json new file mode 100644 index 000000000..f8f523596 --- /dev/null +++ b/os_hierarchy.json @@ -0,0 +1,33 @@ +[ + { + "Linux": [ + { + "Debian": [ + "Elementary", + "Ubuntu", + "Raspbian" + ] + }, + { + "El": [ + "Fedora", + "Amzn", + "Centos" + ] + }, + { + "Sles": [ + "Opensuse" + ] + } + ] + }, + { + "Solaris": [ + "Bsd" + ] + }, + "Macosx", + "Windows", + "Aix" +] diff --git a/spec/facter/resolvers/redhat_release_resolver_spec.rb b/spec/facter/resolvers/redhat_release_resolver_spec.rb index 9704a7e0b..279683c4e 100644 --- a/spec/facter/resolvers/redhat_release_resolver_spec.rb +++ b/spec/facter/resolvers/redhat_release_resolver_spec.rb @@ -2,8 +2,8 @@ describe Facter::Resolvers::RedHatRelease do before do - allow(Open3).to receive(:capture2) - .with('cat /etc/redhat-release') + allow(Facter::Util::FileHelper).to receive(:safe_read) + .with('/etc/redhat-release', nil) .and_return("Red Hat Enterprise Linux Server release 5.10 (Tikanga)\n") end diff --git a/spec/facter/resolvers/suse_relese_resolver_spec.rb b/spec/facter/resolvers/suse_relese_resolver_spec.rb index bd10e3f16..dfe6a3eae 100644 --- a/spec/facter/resolvers/suse_relese_resolver_spec.rb +++ b/spec/facter/resolvers/suse_relese_resolver_spec.rb @@ -2,8 +2,8 @@ describe Facter::Resolvers::SuseRelease do before do - allow(Open3).to receive(:capture2) - .with('cat /etc/SuSE-release') + allow(Facter::Util::FileHelper).to receive(:safe_read) + .with('/etc/SuSE-release', nil) .and_return("openSUSE 11.1 (i586) VERSION = 11.1") end diff --git a/spec/fixtures/broken_os_hierarchy b/spec/fixtures/broken_os_hierarchy new file mode 100644 index 000000000..05db29ddf --- /dev/null +++ b/spec/fixtures/broken_os_hierarchy @@ -0,0 +1,32 @@ +[ + { + "Linux": [ + "Debian": [ + "Elementary", + "Ubuntu", + "Raspbian" + ] + }, + { + "El": [ + "Fedora", + "Amzn", + "Centos" + ] + }, + { + "Sles": [ + "Opensuse" + ] + } + ] + }, + { + "Solaris": [ + "Bsd" + ] + }, + "Macosx", + "Windows", + "Aix" +] diff --git a/spec/fixtures/os_hierarchy b/spec/fixtures/os_hierarchy new file mode 100644 index 000000000..f8f523596 --- /dev/null +++ b/spec/fixtures/os_hierarchy @@ -0,0 +1,33 @@ +[ + { + "Linux": [ + { + "Debian": [ + "Elementary", + "Ubuntu", + "Raspbian" + ] + }, + { + "El": [ + "Fedora", + "Amzn", + "Centos" + ] + }, + { + "Sles": [ + "Opensuse" + ] + } + ] + }, + { + "Solaris": [ + "Bsd" + ] + }, + "Macosx", + "Windows", + "Aix" +] diff --git a/spec/framework/detector/os_detector_spec.rb b/spec/framework/detector/os_detector_spec.rb index 3d6c594d7..190156255 100644 --- a/spec/framework/detector/os_detector_spec.rb +++ b/spec/framework/detector/os_detector_spec.rb @@ -3,68 +3,188 @@ require 'rbconfig' describe OsDetector do + let(:os_hierarchy) { instance_spy(Facter::OsHierarchy) } + before do Singleton.__init__(OsDetector) - end - it 'detects os as macosx' do - RbConfig::CONFIG['host_os'] = 'darwin' - expect(OsDetector.instance.identifier).to eq(:macosx) + allow(Facter::OsHierarchy).to receive(:new).and_return(os_hierarchy) end - it 'detects os as windows' do - RbConfig::CONFIG['host_os'] = 'mingw' - expect(OsDetector.instance.identifier).to eq(:windows) - end + describe 'initialize' do + context 'when os is macosx' do + before do + RbConfig::CONFIG['host_os'] = 'darwin' + allow(os_hierarchy).to receive(:construct_hierarchy).with(:macosx).and_return(['macosx']) + end - it 'detects os as solaris' do - RbConfig::CONFIG['host_os'] = 'solaris' - expect(OsDetector.instance.identifier).to eq(:solaris) - end + it 'detects os as macosx' do + expect(OsDetector.instance.identifier).to eq(:macosx) + end - it 'detects os as aix' do - RbConfig::CONFIG['host_os'] = 'aix' - expect(OsDetector.instance.identifier).to eq(:aix) - end + it 'calls hierarchy construction with macosx identifier' do + OsDetector.instance - it 'raise error if it could not detect os' do - RbConfig::CONFIG['host_os'] = 'os' - expect { OsDetector.instance.identifier }.to raise_error(RuntimeError, 'unknown os: "os"') - end + expect(os_hierarchy).to have_received(:construct_hierarchy).with(:macosx) + end - context 'when host_os is linux' do - before do - RbConfig::CONFIG['host_os'] = 'linux' + it 'construct hierarchy with darwin identifier' do + expect(OsDetector.instance.hierarchy).to eq(['macosx']) + end + end - allow(Facter::Resolvers::OsRelease).to receive(:resolve).with(:identifier) - allow(Facter::Resolvers::RedHatRelease).to receive(:resolve).with(:identifier).and_return('RedHat') + context 'when os is windows' do + before do + RbConfig::CONFIG['host_os'] = 'mingw' + allow(os_hierarchy).to receive(:construct_hierarchy).with(:windows).and_return(['windows']) + end - allow(Facter::Resolvers::OsRelease).to receive(:resolve).with(:version) - allow(Facter::Resolvers::RedHatRelease).to receive(:resolve).with(:version) - end + it 'detects os as windows' do + expect(OsDetector.instance.identifier).to eq(:windows) + end + + it 'calls hierarchy construction with windows identifier' do + OsDetector.instance + + expect(os_hierarchy).to have_received(:construct_hierarchy).with(:windows) + end - it 'detects linux distro' do - expect(OsDetector.instance.detect).to eql('RedHat') + it 'construct hierarchy with windows identifier' do + expect(OsDetector.instance.hierarchy).to eq(['windows']) + end end - it 'calls Facter::Resolvers::OsRelease with identifier' do - OsDetector.instance - expect(Facter::Resolvers::OsRelease).to have_received(:resolve).with(:identifier) + context 'when os is solaris' do + before do + RbConfig::CONFIG['host_os'] = 'solaris' + allow(os_hierarchy).to receive(:construct_hierarchy).with(:solaris).and_return(['solaris']) + end + + it 'detects os as solaris' do + expect(OsDetector.instance.identifier).to eq(:solaris) + end + + it 'calls hierarchy construction with solaris identifier' do + OsDetector.instance + + expect(os_hierarchy).to have_received(:construct_hierarchy).with(:solaris) + end + + it 'construct hierarchy with solaris identifier' do + expect(OsDetector.instance.hierarchy).to eq(['solaris']) + end end - it 'calls Facter::Resolvers::RedHatRelease with identifier' do - OsDetector.instance - expect(Facter::Resolvers::RedHatRelease).to have_received(:resolve).with(:identifier) + context 'when os is aix' do + before do + RbConfig::CONFIG['host_os'] = 'aix' + allow(os_hierarchy).to receive(:construct_hierarchy).with(:aix).and_return(['aix']) + end + + it 'detects os as aix' do + expect(OsDetector.instance.identifier).to eq(:aix) + end + + it 'calls hierarchy construction with aix identifier' do + OsDetector.instance + + expect(os_hierarchy).to have_received(:construct_hierarchy).with(:aix) + end + + it 'construct hierarchy with aix identifier' do + expect(OsDetector.instance.hierarchy).to eq(['aix']) + end end - it 'calls Facter::Resolvers::OsRelease with version' do - OsDetector.instance - expect(Facter::Resolvers::OsRelease).to have_received(:resolve).with(:version) + context 'when os cannot be detected' do + before do + RbConfig::CONFIG['host_os'] = 'my_custom_os' + end + + it 'raises error if it could not detect os' do + expect { OsDetector.instance.identifier }.to raise_error(RuntimeError, 'unknown os: "my_custom_os"') + end end - it 'calls Facter::Resolvers::RedHatRelease with version' do - OsDetector.instance - expect(Facter::Resolvers::RedHatRelease).to have_received(:resolve).with(:version) + context 'when host_os is linux' do + before do + RbConfig::CONFIG['host_os'] = 'linux' + + allow(Facter::Resolvers::OsRelease).to receive(:resolve).with(:identifier) + allow(Facter::Resolvers::RedHatRelease).to receive(:resolve).with(:identifier).and_return(:redhat) + allow(Facter::Resolvers::SuseRelease).to receive(:resolve).with(:identifier) + + allow(Facter::Resolvers::OsRelease).to receive(:resolve).with(:version) + allow(Facter::Resolvers::RedHatRelease).to receive(:resolve).with(:version) + allow(Facter::Resolvers::SuseRelease).to receive(:resolve).with(:version) + + allow(os_hierarchy).to receive(:construct_hierarchy).with(:redhat).and_return(%w[linux redhat]) + end + + it 'detects linux distro' do + expect(OsDetector.instance.identifier).to be(:redhat) + end + + it 'calls Facter::OsHierarchy with construct_hierarchy' do + OsDetector.instance + + expect(os_hierarchy).to have_received(:construct_hierarchy).with(:redhat) + end + + it 'calls Facter::Resolvers::OsRelease with identifier' do + OsDetector.instance + + expect(Facter::Resolvers::OsRelease).to have_received(:resolve).with(:identifier) + end + + it 'calls Facter::Resolvers::RedHatRelease with identifier' do + OsDetector.instance + + expect(Facter::Resolvers::RedHatRelease).to have_received(:resolve).with(:identifier) + end + + it 'calls Facter::Resolvers::OsRelease with version' do + OsDetector.instance + + expect(Facter::Resolvers::OsRelease).to have_received(:resolve).with(:version) + end + + it 'calls Facter::Resolvers::RedHatRelease with version' do + OsDetector.instance + + expect(Facter::Resolvers::RedHatRelease).to have_received(:resolve).with(:version) + end + + context 'when distribution is not known' do + before do + allow(Facter::Resolvers::RedHatRelease).to receive(:resolve).with(:identifier).and_return('my_linux_distro') + allow(Facter::Resolvers::OsRelease).to receive(:resolve).with(:id_like).and_return(nil) + + allow(os_hierarchy).to receive(:construct_hierarchy).and_return([]) + allow(os_hierarchy).to receive(:construct_hierarchy).with(:linux).and_return(['linux']) + Singleton.__init__(OsDetector) + end + + it 'falls back to linux' do + expect(OsDetector.instance.identifier).to eq(:my_linux_distro) + end + + it 'constructs hierarchy with linux' do + expect(OsDetector.instance.hierarchy).to eq(['linux']) + end + + context 'when family is known' do + before do + allow(Facter::Resolvers::OsRelease).to receive(:resolve).with(:id_like).and_return(:ubuntu) + allow(os_hierarchy).to receive(:construct_hierarchy).with(:ubuntu).and_return(%w[Linux Debian Ubuntu]) + Singleton.__init__(OsDetector) + end + + it 'constructs hierarchy with linux' do + expect(OsDetector.instance.hierarchy).to eq(%w[Linux Debian Ubuntu]) + end + end + end end end end diff --git a/spec/framework/detector/os_hierarchy_spec.rb b/spec/framework/detector/os_hierarchy_spec.rb new file mode 100644 index 000000000..df5daf166 --- /dev/null +++ b/spec/framework/detector/os_hierarchy_spec.rb @@ -0,0 +1,93 @@ +# frozen_string_literal: true + +describe Facter::OsHierarchy do + subject(:os_hierarchy) { Facter::OsHierarchy.new } + + before do + allow(Facter::Util::FileHelper) + .to receive(:safe_read) + .with('os_hierarchy.json') + .and_return(load_fixture('os_hierarchy').read) + allow(Facter::Log).to receive(:new).and_return(log) + end + + let(:log) { instance_spy(Facter::Log) } + + describe '#initialize' do + context 'when os_hierarchy is invalid' do + before do + allow(Facter::Util::FileHelper) + .to receive(:safe_read) + .with('os_hierarchy.json') + .and_return(load_fixture('broken_os_hierarchy').read) + end + + it 'log error message' do + Facter::OsHierarchy.new + expect(log).to have_received(:error).with('Could not parse os_hierarchy json') + end + end + end + + describe '#construct_hierarchy' do + context 'when searched_os is ubuntu' do + it 'constructs hierarchy' do + hierarchy = os_hierarchy.construct_hierarchy(:ubuntu) + + expect(hierarchy).to eq(%w[Linux Debian Ubuntu]) + end + end + + context 'when searched_os is debian' do + it 'constructs hierarchy' do + hierarchy = os_hierarchy.construct_hierarchy(:debian) + + expect(hierarchy).to eq(%w[Linux Debian]) + end + end + + context 'when searched_os is linux' do + it 'constructs hierarchy' do + hierarchy = os_hierarchy.construct_hierarchy(:linux) + + expect(hierarchy).to eq(%w[Linux]) + end + end + + context 'when there is no os hierarchy' do + let(:my_os_name) { 'Myos' } + + before do + allow(JSON).to receive(:parse).and_return(nil) + end + + it 'returns the searched os as the hierarchy' do + hierarchy = os_hierarchy.construct_hierarchy(:myos) + + expect(hierarchy).to eq([my_os_name]) + end + + it 'logs debug message' do + os_hierarchy.construct_hierarchy(:myos) + + expect(log).to have_received(:debug).with("There is no os_hierarchy, will fall back to: #{my_os_name}") + end + end + + context 'when searched_os is nil' do + it 'constructs hierarchy' do + hierarchy = os_hierarchy.construct_hierarchy(nil) + + expect(hierarchy).to eq([]) + end + end + + context 'when searched_os is not in hierarchy' do + it 'constructs hierarchy' do + hierarchy = os_hierarchy.construct_hierarchy(:my_os) + + expect(hierarchy).to eq([]) + end + end + end +end diff --git a/spec/framework/logging/logger_spec.rb b/spec/framework/logging/logger_spec.rb index c421f7a11..20271245c 100644 --- a/spec/framework/logging/logger_spec.rb +++ b/spec/framework/logging/logger_spec.rb @@ -108,7 +108,7 @@ end it 'writes error message not colorized on Windows' do - allow(OsDetector.instance).to receive(:detect).and_return(:windows) + allow(OsDetector.instance).to receive(:identifier).and_return(:windows) log.error('error_message', true)