Skip to content

Commit

Permalink
add --roles param
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoblitt committed Oct 26, 2023
1 parent a1228ed commit bb316c1
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 11 deletions.
5 changes: 5 additions & 0 deletions bin/metadata2gha
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ options = {
domain: nil,
minimum_major_puppet_version: nil,
beaker_fact: nil,
roles: nil,
}

OptionParser.new do |opts|
Expand Down Expand Up @@ -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]
Expand Down
12 changes: 9 additions & 3 deletions lib/puppet_metadata/beaker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
36 changes: 28 additions & 8 deletions lib/puppet_metadata/github_actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 13 additions & 0 deletions spec/beaker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
20 changes: 20 additions & 0 deletions spec/github_actions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit bb316c1

Please sign in to comment.