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

make sub skipping more generic #52

Open
wants to merge 1 commit 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
23 changes: 3 additions & 20 deletions katello-attach-subscription
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,6 @@ def vdcupdate()
end

this_system_cluster = 'nil'
skip_sub = true
if @options[:density] and system_type == KatelloAttachSubscription::FactAnalyzer::HYPERVISOR
# retrieve cluster name of the hypervisor
if sys.has_key?('facts') and sys['facts'].has_key?('hypervisor::cluster')
Expand All @@ -879,26 +878,10 @@ def vdcupdate()
sys['facts']['hypervisor::cluster'] = this_system_cluster
end
end
if KatelloAttachSubscription::HostMatcher.match_hostname(sub['hostname'], system['name'])
if @options[:debug]
puts " DEBUG: System '#{system['name']}' match the regexp '#{sub['hostname']}'"
end
skip_sub = false
end

# skip the sub if setted
if skip_sub
if @options[:verbose]
puts " VERBOSE: skipping '#{system['name']}' as system cluster type doesn't match sub type or system name do not match following regexp:"
puts " '#{sub['hostname']}'"
end
next
else
if @options[:debug]
# puts " DEBUG: System '#{system['name']}' match the regexp"
puts " DEBUG: System '#{system['name']}' can be subbed"
end
end
# check if sub shall be skipped based on hostname, type and facts
next if KatelloAttachSubscription::HostMatcher.skip_sub?(sys, sub, @options)

# extract the (possible) virtual_host
if sub.has_key?('virtual_host')
virtualhostregex = Regexp.new(sub['virtual_host'])
Expand Down
4 changes: 4 additions & 0 deletions lib/katello_attach_subscription/host_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,9 @@ def self.match_version(expected, actual)
version = Gem::Version.new(actual)
requirement.satisfied_by?(version)
end

def self.skip_sub?(host, sub, options = {})
!self.match_host(host, sub)
end
end
end
42 changes: 42 additions & 0 deletions test/host_matcher_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,22 @@ class HostMatcherTest < Minitest::Test
]
}

def setup
@config = read_yaml_fixture('prod_config')

@subs = @config[:subs]
@sub_hv_prod = @subs[0]
@sub_hv_preprod = @subs[1]
@sub_hv_both = @subs[2]
@sub_phys_server_els = @subs[3]
@sub_phys_server = @subs[4]
@sub_phys_client_els = @subs[5]
@sub_phys_client = @subs[6]

@systems = read_yaml_fixture('prod_systems')
@virtwhodata = read_json_fixture('prod_virtwho')
end

def test_hostname
assert KatelloAttachSubscription::HostMatcher.match_hostname('client[0-9]+\.example\.com', 'client42.example.com')
end
Expand Down Expand Up @@ -59,4 +75,30 @@ def test_host_rhel_kvm_vm
host = read_json_fixture('rhel_kvm_vm_host')
refute KatelloAttachSubscription::HostMatcher.match_host(host, DUMMY_CONFIG)
end

def test_skip_sub_for_hypervisor
host = read_json_fixture('vmware_esx_host')
sub = {'type' => 'Hypervisor', 'cluster' => '.*'}
options = {density: true}
refute KatelloAttachSubscription::HostMatcher.skip_sub?(host, sub, options)
end

def test_skip_sub_for_host
host = read_json_fixture('rhel_kvm_vm_host')
sub = {'hostname' => '.*'}
refute KatelloAttachSubscription::HostMatcher.skip_sub?(host, sub)
end

def test_skip_sub_for_host_bad
host = read_json_fixture('rhel_kvm_vm_host')
sub = {'hostname' => '.*\.example\.org'}
assert KatelloAttachSubscription::HostMatcher.skip_sub?(host, sub)
end

def test_skip_sub_prod
refute KatelloAttachSubscription::HostMatcher.skip_sub?(@systems['physical3.rhel6.example.org'], @sub_phys_server)
assert KatelloAttachSubscription::HostMatcher.skip_sub?(@systems['physical3.rhel6.example.org'], @sub_phys_server_els)
refute KatelloAttachSubscription::HostMatcher.skip_sub?(@systems['physical1.rhel5.example.org'], @sub_phys_server_els)
assert KatelloAttachSubscription::HostMatcher.skip_sub?(@systems['physical1.rhel5.example.org'], @sub_phys_server)
end
end
6 changes: 6 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
require 'json'
require 'yaml'
require 'katello_attach_subscription'
require 'minitest/autorun'

def read_json_fixture(file)
json = File.expand_path(File.join('..', 'fixtures', "#{file}.json"), __FILE__)
JSON.parse(File.read(json))
end

def read_yaml_fixture(file)
yaml = File.expand_path(File.join('..', 'fixtures', "#{file}.yaml"), __FILE__)
YAML.load_file(yaml)
end