From 912bdd862598f469cca72ed2fe842258bdad9869 Mon Sep 17 00:00:00 2001 From: Kimmo Lehto Date: Tue, 1 Oct 2019 15:52:25 +0300 Subject: [PATCH 1/3] Configurer version regex matching (allow RHEL >= 7.4, < 8.0) --- lib/pharos/configuration/os_release.rb | 9 ++++++- lib/pharos/host/configurer.rb | 11 ++++++--- lib/pharos/host/el7/rhel7.rb | 4 +-- spec/pharos/host/configurer_spec.rb | 34 ++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 7 deletions(-) diff --git a/lib/pharos/configuration/os_release.rb b/lib/pharos/configuration/os_release.rb index 31da93f21..d5c0c8c8c 100644 --- a/lib/pharos/configuration/os_release.rb +++ b/lib/pharos/configuration/os_release.rb @@ -6,7 +6,14 @@ class OsRelease < Pharos::Configuration::Struct attribute :id, Pharos::Types::Strict::String attribute :id_like, Pharos::Types::Strict::String.optional.default(nil) attribute :name, Pharos::Types::Strict::String.optional.default(nil) - attribute :version, Pharos::Types::Strict::String + attribute :version, Pharos::Types::Strict::String.optional.default(nil) + attribute :version_regex, Pharos::Types.Instance(Regexp).optional.default(nil) + + def ==(other) + return false unless id == other.id + + (version || version_regex) === other.version # rubocop:disable Style/CaseEquality + end end end end diff --git a/lib/pharos/host/configurer.rb b/lib/pharos/host/configurer.rb index ef57cbd7f..e3b7078fa 100644 --- a/lib/pharos/host/configurer.rb +++ b/lib/pharos/host/configurer.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require 'set' + module Pharos module Host class Configurer @@ -13,7 +15,7 @@ def self.load_configurers # @return [Array] def self.configurers - @configurers ||= [] + @configurers ||= Set.new end # @param [Pharos::Configuration::OsRelease] @@ -249,11 +251,14 @@ def supported_os_releases # @param [Pharos::Configuration::OsRelease] # @return [Boolean] def supported_os?(os_release) - supported_os_releases.any? { |release| release.id == os_release.id && release.version == os_release.version } + supported_os_releases.any? { |release| release == os_release } end def register_config(name, version) - supported_os_releases << Pharos::Configuration::OsRelease.new(id: name, version: version) + os_release_opts = { id: name } + os_release_opts[version.is_a?(String) ? :version : :version_regex] = version + + supported_os_releases << Pharos::Configuration::OsRelease.new(os_release_opts) Pharos::Host::Configurer.configurers << self self end diff --git a/lib/pharos/host/el7/rhel7.rb b/lib/pharos/host/el7/rhel7.rb index 6859607d4..74a5dae1a 100644 --- a/lib/pharos/host/el7/rhel7.rb +++ b/lib/pharos/host/el7/rhel7.rb @@ -5,9 +5,7 @@ module Pharos module Host class Rhel7 < El7 - register_config 'rhel', '7.4' - register_config 'rhel', '7.5' - register_config 'rhel', '7.6' + register_config 'rhel', /^7\.[4-9]|\d{2,}/ # >= 7.4, < 8.0 DOCKER_VERSION = '1.13.1' CFSSL_VERSION = '1.2' diff --git a/spec/pharos/host/configurer_spec.rb b/spec/pharos/host/configurer_spec.rb index 5ec73c08d..e124f7741 100644 --- a/spec/pharos/host/configurer_spec.rb +++ b/spec/pharos/host/configurer_spec.rb @@ -56,6 +56,40 @@ end end + context 'with regex version matcher' do + let(:test_config_class) do + Class.new(described_class) do + register_config 'test', /^7\.[4-9]|\d{2,}/ + end + end + + it 'uses the matcher' do + expect( + described_class.for_os_release( + Pharos::Configuration::OsRelease.new(id: 'test', version: '7.3') + ) + ).to be_nil + + expect( + described_class.for_os_release( + Pharos::Configuration::OsRelease.new(id: 'test', version: '7.4') + ).new(host) + ).to be_a test_config_class + + expect( + described_class.for_os_release( + Pharos::Configuration::OsRelease.new(id: 'test', version: '7.12') + ).new(host) + ).to be_a test_config_class + + expect( + described_class.for_os_release( + Pharos::Configuration::OsRelease.new(id: 'test', version: '8.0') + ) + ).to be_nil + end + end + describe '#update_env_file' do let(:host) { instance_double(Pharos::Configuration::Host) } let(:ssh) { instance_double(Pharos::Transport::SSH) } From fa83057c60d9c0ffe1e47311d91f91beebaccc42 Mon Sep 17 00:00:00 2001 From: Kimmo Lehto Date: Tue, 1 Oct 2019 16:14:03 +0300 Subject: [PATCH 2/3] Too easy to make it support more types --- lib/pharos/configuration/os_release.rb | 4 ++-- lib/pharos/host/configurer.rb | 2 +- spec/pharos/host/configurer_spec.rb | 28 ++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/lib/pharos/configuration/os_release.rb b/lib/pharos/configuration/os_release.rb index d5c0c8c8c..5c186f484 100644 --- a/lib/pharos/configuration/os_release.rb +++ b/lib/pharos/configuration/os_release.rb @@ -7,12 +7,12 @@ class OsRelease < Pharos::Configuration::Struct attribute :id_like, Pharos::Types::Strict::String.optional.default(nil) attribute :name, Pharos::Types::Strict::String.optional.default(nil) attribute :version, Pharos::Types::Strict::String.optional.default(nil) - attribute :version_regex, Pharos::Types.Instance(Regexp).optional.default(nil) + attribute :version_matcher, Pharos::Types.Instance(Object).optional.default(nil) def ==(other) return false unless id == other.id - (version || version_regex) === other.version # rubocop:disable Style/CaseEquality + (version || version_matcher) === other.version # rubocop:disable Style/CaseEquality end end end diff --git a/lib/pharos/host/configurer.rb b/lib/pharos/host/configurer.rb index e3b7078fa..decc30d8a 100644 --- a/lib/pharos/host/configurer.rb +++ b/lib/pharos/host/configurer.rb @@ -256,7 +256,7 @@ def supported_os?(os_release) def register_config(name, version) os_release_opts = { id: name } - os_release_opts[version.is_a?(String) ? :version : :version_regex] = version + os_release_opts[version.is_a?(String) ? :version : :version_matcher] = version supported_os_releases << Pharos::Configuration::OsRelease.new(os_release_opts) Pharos::Host::Configurer.configurers << self diff --git a/spec/pharos/host/configurer_spec.rb b/spec/pharos/host/configurer_spec.rb index e124f7741..837c28a37 100644 --- a/spec/pharos/host/configurer_spec.rb +++ b/spec/pharos/host/configurer_spec.rb @@ -90,6 +90,34 @@ end end + context 'with proc version matcher' do + let(:test_config_class) do + Class.new(described_class) do + register_config 'test', -> (v) { v.start_with?('7') } + end + end + + it 'uses the matcher' do + expect( + described_class.for_os_release( + Pharos::Configuration::OsRelease.new(id: 'test', version: '7.0') + ).new(host) + ).to be_a test_config_class + + expect( + described_class.for_os_release( + Pharos::Configuration::OsRelease.new(id: 'test', version: '7abcd') + ).new(host) + ).to be_a test_config_class + + expect( + described_class.for_os_release( + Pharos::Configuration::OsRelease.new(id: 'test', version: 'abcd') + ) + ).to be_nil + end + end + describe '#update_env_file' do let(:host) { instance_double(Pharos::Configuration::Host) } let(:ssh) { instance_double(Pharos::Transport::SSH) } From adbc61b0ce13f926006b2d4bee907942f2a5e9fe Mon Sep 17 00:00:00 2001 From: Kimmo Lehto Date: Tue, 1 Oct 2019 16:27:06 +0300 Subject: [PATCH 3/3] More spec examples --- spec/pharos/host/configurer_spec.rb | 36 +++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/spec/pharos/host/configurer_spec.rb b/spec/pharos/host/configurer_spec.rb index 837c28a37..93841a821 100644 --- a/spec/pharos/host/configurer_spec.rb +++ b/spec/pharos/host/configurer_spec.rb @@ -90,10 +90,10 @@ end end - context 'with proc version matcher' do + context 'with lambda version matcher' do let(:test_config_class) do Class.new(described_class) do - register_config 'test', -> (v) { v.start_with?('7') } + register_config 'test', -> v { Gem::Requirement.new('~> 7.4').satisfied_by?(Gem::Version.new(v)) } end end @@ -101,18 +101,46 @@ expect( described_class.for_os_release( Pharos::Configuration::OsRelease.new(id: 'test', version: '7.0') + ) + ).to be_nil + + expect( + described_class.for_os_release( + Pharos::Configuration::OsRelease.new(id: 'test', version: '7.4') ).new(host) ).to be_a test_config_class expect( described_class.for_os_release( - Pharos::Configuration::OsRelease.new(id: 'test', version: '7abcd') + Pharos::Configuration::OsRelease.new(id: 'test', version: '7.9.5') + ).new(host) + ).to be_a test_config_class + end + end + + context 'with range version matcher' do + let(:test_config_class) do + Class.new(described_class) do + register_config 'test', ("10".."100") + end + end + + it 'uses the matcher' do + expect( + described_class.for_os_release( + Pharos::Configuration::OsRelease.new(id: 'test', version: '9') + ) + ).to be_nil + + expect( + described_class.for_os_release( + Pharos::Configuration::OsRelease.new(id: 'test', version: '11') ).new(host) ).to be_a test_config_class expect( described_class.for_os_release( - Pharos::Configuration::OsRelease.new(id: 'test', version: 'abcd') + Pharos::Configuration::OsRelease.new(id: 'test', version: '101') ) ).to be_nil end