diff --git a/bin/metadata2gha b/bin/metadata2gha index eb51f97..bcb983f 100755 --- a/bin/metadata2gha +++ b/bin/metadata2gha @@ -11,6 +11,7 @@ options = { domain: nil, minimum_major_puppet_version: nil, beaker_fact: nil, + roles: nil, } OptionParser.new do |opts| @@ -40,6 +41,10 @@ OptionParser.new do |opts| options[:beaker_facter] = [fact, label, values.split(',')] end end + opts.on('--roles VALUE,VALUE', Array, 'A list of node "roles" -- generates multiple SUTs. Separate values using commas') do |opt| + # If an empty string is passed, e.g. --role '', consider it a noop + options[:roles] = (opt == []) ? nil : opt + end end.parse! filename = ARGV[0] diff --git a/lib/puppet_metadata/beaker.rb b/lib/puppet_metadata/beaker.rb index ea327fb..15c29d8 100644 --- a/lib/puppet_metadata/beaker.rb +++ b/lib/puppet_metadata/beaker.rb @@ -52,20 +52,26 @@ 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_prefix + # String to prepend to the hostname. # # @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_prefix: 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}" + hostname = (puppet_version.nil? && puppet_version != 'none') ? "#{hostname_prefix}#{name}" : "#{hostname_prefix}#{name}-#{puppet_version}" domain ||= 'example.com' if use_fqdn options = {} - options[:hostname] = "#{hostname}.#{domain}" if domain + if domain + options[:hostname] = "#{hostname}.#{domain}" + elsif hostname_prefix + 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..3e9f0e8 100644 --- a/lib/puppet_metadata/github_actions.rb +++ b/lib/puppet_metadata/github_actions.rb @@ -137,14 +137,34 @@ 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[:roles] + setfiles = options[:roles].collect do |role| + 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_prefix: "#{role}-", + ) + end + + # check for os/release pairs that don't have a setfile + return setfiles[0] if setfiles[0].nil? + + # 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..4fbeaeb 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_prefix' do + describe 'without domain' do + it { expect(described_class.os_release_to_setfile('AlmaLinux', '9', hostname_prefix: 'foo-')).to eq(['almalinux9-64{hostname=foo-almalinux9-64}', 'AlmaLinux 9']) } + end + + describe 'with domain' do + it { + expect(described_class.os_release_to_setfile('AlmaLinux', '9', domain: 'example.com', + hostname_prefix: 'foo-')).to eq(['almalinux9-64{hostname=foo-almalinux9-64.example.com}', 'AlmaLinux 9']) + } + end + end end end diff --git a/spec/github_actions_spec.rb b/spec/github_actions_spec.rb index 2fe126e..042d138 100644 --- a/spec/github_actions_spec.rb +++ b/spec/github_actions_spec.rb @@ -163,6 +163,16 @@ ) end end + + context 'with roles option' do + let(:options) { super().merge(roles: %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-puppet8}-centos9-64{hostname=bar-centos9-64-puppet8}-centos9-64{hostname=baz-centos9-64-puppet8}' } }, + ) + end + end end describe 'github_action_test_matrix' do @@ -289,6 +299,16 @@ ) end end + + context 'with roles option' do + let(:options) { super().merge(roles: %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-puppet8}-centos9-64{hostname=bar-centos9-64-puppet8}-centos9-64{hostname=baz-centos9-64-puppet8}' }, puppet: { name: 'Puppet 8', value: 8, collection: 'puppet8' } }, + ) + end + end end end # rubocop:enable Layout/LineLength,RSpec/ExampleLength