From f89cb24ddcfee82e5c3c29f3b14ee91ff4688d75 Mon Sep 17 00:00:00 2001 From: Joshua Hoblitt Date: Thu, 26 Oct 2023 12:20:01 -0700 Subject: [PATCH] add --hosts param --- bin/metadata2gha | 4 +++ lib/puppet_metadata/beaker.rb | 21 +++++++++++++--- lib/puppet_metadata/github_actions.rb | 35 +++++++++++++++++++++------ spec/beaker_spec.rb | 13 ++++++++++ spec/github_actions_spec.rb | 20 +++++++++++++++ 5 files changed, 81 insertions(+), 12 deletions(-) diff --git a/bin/metadata2gha b/bin/metadata2gha index eb51f97..d212346 100755 --- a/bin/metadata2gha +++ b/bin/metadata2gha @@ -11,6 +11,7 @@ options = { domain: nil, minimum_major_puppet_version: nil, beaker_fact: nil, + hosts: nil, } OptionParser.new do |opts| @@ -40,6 +41,9 @@ OptionParser.new do |opts| options[:beaker_facter] = [fact, label, values.split(',')] end end + opts.on('--hosts VALUE,VALUE', Array, 'A list of hostnames to create -- generates multiple SUTs. Separate values using commas') do |opt| + options[:hosts] = opt unless opt.empty? + end end.parse! filename = ARGV[0] diff --git a/lib/puppet_metadata/beaker.rb b/lib/puppet_metadata/beaker.rb index ea327fb..bdf3955 100644 --- a/lib/puppet_metadata/beaker.rb +++ b/lib/puppet_metadata/beaker.rb @@ -52,20 +52,33 @@ def adjusted_os(os) # Enforce a domain to be appended to the hostname, making it an FQDN # @param [Optional[String]] puppet_version # The desired puppet version. Will be appended to the hostname + # @param [Optional[String]] hostname + # Override the automatically generated hostname. The domain may still + # be set via the `domain` param. # # @return [nil] If no setfile is available # @return [Array<(String, String)>] The beaker setfile description with a readable name - def os_release_to_setfile(os, release, use_fqdn: false, pidfile_workaround: false, domain: nil, puppet_version: nil) + def os_release_to_setfile(os, release, use_fqdn: false, pidfile_workaround: false, domain: nil, puppet_version: nil, hostname: nil) return unless os_supported?(os) aos = adjusted_os(os) - name = "#{aos}#{release.tr('.', '')}-64" - hostname = (puppet_version.nil? && puppet_version != 'none') ? name : "#{name}-#{puppet_version}" + shostname = if hostname + hostname + elsif !puppet_version.nil? + "#{name}-#{puppet_version}" + else + name + end + domain ||= 'example.com' if use_fqdn options = {} - options[:hostname] = "#{hostname}.#{domain}" if domain + if domain + options[:hostname] = "#{shostname}.#{domain}" + elsif hostname + options[:hostname] = hostname + end # Docker messes up cgroups and some systemd versions can't deal with # that when PIDFile is used. diff --git a/lib/puppet_metadata/github_actions.rb b/lib/puppet_metadata/github_actions.rb index 7a05c46..cde37ac 100644 --- a/lib/puppet_metadata/github_actions.rb +++ b/lib/puppet_metadata/github_actions.rb @@ -137,14 +137,33 @@ def puppet_version_below_minimum?(version) end def os_release_to_beaker_setfile(os, release, puppet_collection) - PuppetMetadata::Beaker.os_release_to_setfile( - os, - release, - use_fqdn: options[:beaker_use_fqdn], - pidfile_workaround: options[:beaker_pidfile_workaround], - domain: options[:domain], - puppet_version: puppet_collection, - ) + if options[:hosts] + setfiles = options[:hosts].filter_map do |host| + PuppetMetadata::Beaker.os_release_to_setfile( + os, + release, + use_fqdn: options[:beaker_use_fqdn], + pidfile_workaround: options[:beaker_pidfile_workaround], + domain: options[:domain], + puppet_version: puppet_collection, + hostname: host, + ) + end + + return if setfiles.empty? + + # merge the setfile strings + [setfiles.collect { |setfile| setfile[0] }.join('-'), setfiles[0][1]] + else + PuppetMetadata::Beaker.os_release_to_setfile( + os, + release, + use_fqdn: options[:beaker_use_fqdn], + pidfile_workaround: options[:beaker_pidfile_workaround], + domain: options[:domain], + puppet_version: puppet_collection, + ) + end end end end diff --git a/spec/beaker_spec.rb b/spec/beaker_spec.rb index 78368dd..0db34db 100644 --- a/spec/beaker_spec.rb +++ b/spec/beaker_spec.rb @@ -57,5 +57,18 @@ it { expect(described_class.os_release_to_setfile('CentOS', '8', pidfile_workaround: ['CentOS'])).to be_nil } end end + + describe 'hostname' do + describe 'without domain' do + it { expect(described_class.os_release_to_setfile('AlmaLinux', '9', hostname: 'foo')).to eq(['almalinux9-64{hostname=foo}', 'AlmaLinux 9']) } + end + + describe 'with domain' do + it { + expect(described_class.os_release_to_setfile('AlmaLinux', '9', domain: 'example.com', + hostname: 'foo')).to eq(['almalinux9-64{hostname=foo.example.com}', 'AlmaLinux 9']) + } + end + end end end diff --git a/spec/github_actions_spec.rb b/spec/github_actions_spec.rb index 2fe126e..e0d02b9 100644 --- a/spec/github_actions_spec.rb +++ b/spec/github_actions_spec.rb @@ -163,6 +163,16 @@ ) end end + + context 'with hosts option' do + let(:options) { super().merge(hosts: %w[foo bar baz]) } + + it 'is expected to contain supported os / puppet version / role combinations' do + expect(subject).to include( + { name: 'Puppet 8 - CentOS 9', env: { 'BEAKER_PUPPET_COLLECTION' => 'puppet8', 'BEAKER_SETFILE' => 'centos9-64{hostname=foo}-centos9-64{hostname=bar}-centos9-64{hostname=baz}' } }, + ) + end + end end describe 'github_action_test_matrix' do @@ -289,6 +299,16 @@ ) end end + + context 'with hosts option' do + let(:options) { super().merge(hosts: %w[foo bar baz]) } + + it 'is expected to contain supported os / puppet version combinations with hostname option' do + expect(subject).to include( + { name: 'Puppet 8 - CentOS 9', setfile: { name: 'CentOS 9', value: 'centos9-64{hostname=foo}-centos9-64{hostname=bar}-centos9-64{hostname=baz}' }, puppet: { name: 'Puppet 8', value: 8, collection: 'puppet8' } }, + ) + end + end end end # rubocop:enable Layout/LineLength,RSpec/ExampleLength