Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurer improved version matching (allow RHEL >= 7.4, < 8.0) #1489

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/pharos/configuration/os_release.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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_matcher, Pharos::Types.Instance(Object).optional.default(nil)

def ==(other)
return false unless id == other.id

(version || version_matcher) === other.version # rubocop:disable Style/CaseEquality
end
end
end
end
11 changes: 8 additions & 3 deletions lib/pharos/host/configurer.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'set'

module Pharos
module Host
class Configurer
Expand All @@ -13,7 +15,7 @@ def self.load_configurers

# @return [Array]
def self.configurers
@configurers ||= []
@configurers ||= Set.new
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated, but I don't see why it should iterate through the same class multiple times when searching for a match.

end

# @param [Pharos::Configuration::OsRelease]
Expand Down Expand Up @@ -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_matcher] = version

supported_os_releases << Pharos::Configuration::OsRelease.new(os_release_opts)
Pharos::Host::Configurer.configurers << self
self
end
Expand Down
5 changes: 1 addition & 4 deletions lib/pharos/host/el7/rhel7.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +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.7'
register_config 'rhel', /^7\.[4-9]|\d{2,}/ # >= 7.4, < 8.0

DOCKER_VERSION = '1.13.1'
CFSSL_VERSION = '1.2'
Expand Down
90 changes: 90 additions & 0 deletions spec/pharos/host/configurer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,96 @@
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

context 'with lambda version matcher' do
let(:test_config_class) do
Class.new(described_class) do
register_config 'test', -> v { Gem::Requirement.new('~> 7.4').satisfied_by?(Gem::Version.new(v)) }
end
end

it 'uses the matcher' do
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: '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: '101')
)
).to be_nil
end
end

describe '#update_env_file' do
let(:host) { instance_double(Pharos::Configuration::Host) }
let(:ssh) { instance_double(Pharos::Transport::SSH) }
Expand Down