Skip to content

Commit

Permalink
Better assert_optional_dependency (#272)
Browse files Browse the repository at this point in the history
* Fixed:
  * Allow `assert_optional_dependency` to handle extended version strings
    (Alpha, Beta, RC, 1.2.3.4, etc...)
  * Allow `module_metadata::os_supported` to handle metadata where the
    version is not specified (all versions supported)
  * Fix windows tests
* Changed:
  * Support all versions of known OSs
  * Facter 4 test updates
  * Windows Beaker SUTs bumped from 2012r2 to 2022 Standard 

Closes #271
Needs simp/rubygem-simp-rspec-puppet-facts#43

Co-authored-by: op-ct <[email protected]>
  • Loading branch information
trevor-vaughan and op-ct authored Jul 6, 2022
1 parent 71b9bd2 commit 56f0d53
Show file tree
Hide file tree
Showing 24 changed files with 193 additions and 143 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
* Thu Dec 16 2021 Chris Tessmer <[email protected]> - 4.10.3
* Fri Jun 24 2022 Trevor Vaughan <[email protected]> - 4.10.3
- Fixed
- Allow `assert_optional_dependency` to handle extended version strings
(Alpha, Beta, RC, 1.2.3.4, etc...)

* Fri Jun 24 2022 Chris Tessmer <[email protected]> - 4.10.3
- Fixed
- Permit root user to run `puppet lookup --compile` without borking passgen

Expand Down
6 changes: 3 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ group :test do
gem 'puppet-strings'
gem 'puppet-lint-empty_string-check', :require => false
gem 'puppet-lint-trailing_comma-check', :require => false
gem 'simp-rspec-puppet-facts', ENV['SIMP_RSPEC_PUPPET_FACTS_VERSION'] || '~> 3.1'
gem 'simp-rake-helpers', ENV['SIMP_RAKE_HELPERS_VERSION'] || ['>= 5.12.1', '< 6']
gem 'simp-rspec-puppet-facts', ENV['SIMP_RSPEC_PUPPET_FACTS_VERSION'] || '~> 3.5'
gem 'simp-rake-helpers', ENV['SIMP_RAKE_HELPERS_VERSION'] || ['>= 5.15.0', '< 6']
gem( 'pdk', ENV['PDK_VERSION'] || '~> 2.0', :require => false) if major_puppet_version > 5
gem 'pathspec', '~> 0.2' if Gem::Requirement.create('< 2.6').satisfied_by?(Gem::Version.new(RUBY_VERSION.dup))
end
Expand All @@ -38,7 +38,7 @@ end
group :system_tests do
gem 'beaker'
gem 'beaker-rspec'
gem 'simp-beaker-helpers', ENV['SIMP_BEAKER_HELPERS_VERSION'] || ['>= 1.23.2', '< 2']
gem 'simp-beaker-helpers', ENV['SIMP_BEAKER_HELPERS_VERSION'] || ['>= 1.25.0', '< 2']
end

# Evaluate extra gemfiles if they exist
Expand Down
13 changes: 8 additions & 5 deletions functions/module_metadata/os_supported.pp
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,17 @@ function simplib::module_metadata::os_supported (
if $os_info['operatingsystem'] == $facts['os']['name'] {
$memo and case $_options['release_match'] {
'full': {
$facts['os']['release']['full'] in $os_info['operatingsystemrelease']
!$os_info['operatingsystemrelease'] or ($facts['os']['release']['full'] in $os_info['operatingsystemrelease'])
}
'major': {
$_os_major_releases = $os_info['operatingsystemrelease'].map |$os_release| {
split($os_release, '\.')[0]
}
if $os_info['operatingsystemrelease'] {
$_os_major_releases = $os_info['operatingsystemrelease'].map |$os_release| {
split($os_release, '\.')[0]
}

$facts['os']['release']['major'] in $_os_major_releases
$facts['os']['release']['major'] in $_os_major_releases
}
else { true }
}
default: { true }
}
Expand Down
11 changes: 10 additions & 1 deletion lib/puppet/functions/simplib/assert_optional_dependency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ def get_module_dependencies(dependency_tree_levels, module_metadata)
return metadata_level[current_level]
end

# Concept lifted from 'node-semver'
def coerce(version)
version
.split('-')
.first
.split('.')[0, 3]
.join('.')
end

def check_dependency(module_name, module_dependency)
require 'semantic_puppet'

Expand Down Expand Up @@ -103,7 +112,7 @@ def check_dependency(module_name, module_dependency)
return %(invalid version range '#{module_dependency['version_requirement']}' for '#{_module_name}')
end

module_version = module_metadata['version']
module_version = coerce(module_metadata['version'])

begin
module_version = SemanticPuppet::Version.parse(module_version)
Expand Down
111 changes: 22 additions & 89 deletions metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,95 +26,28 @@
}
],
"operatingsystem_support": [
{
"operatingsystem": "RedHat",
"operatingsystemrelease": [
"7",
"8"
]
},
{
"operatingsystem": "CentOS",
"operatingsystemrelease": [
"7",
"8"
]
},
{
"operatingsystem": "OracleLinux",
"operatingsystemrelease": [
"7",
"8"
]
},
{
"operatingsystem": "Scientific",
"operatingsystemrelease": [
"7"
]
},
{
"operatingsystem": "SLES",
"operatingsystemrelease": [
"11 SP1",
"12",
"15"
]
},
{
"operatingsystem": "Debian",
"operatingsystemrelease": [
"8",
"9",
"10",
"11"
]
},
{
"operatingsystem": "Ubuntu",
"operatingsystemrelease": [
"14.04",
"16.04",
"18.04",
"19.04",
"20.04"
]
},
{
"operatingsystem": "Solaris",
"operatingsystemrelease": [
"11",
"12"
]
},
{
"operatingsystem": "Windows",
"operatingsystemrelease": [
"Server 2008",
"Server 2008 R2",
"Server 2012",
"Server 2012 R2",
"2016",
"2019",
"7",
"8",
"10"
]
},
{
"operatingsystem": "AIX",
"operatingsystemrelease": [
"6.1",
"7.1",
"7.2"
]
},
{
"operatingsystem": "Amazon",
"operatingsystemrelease": [
"2"
]
}
{ "operatingsystem": "AlmaLinux" },
{ "operatingsystem": "Amazon" },
{ "operatingsystem": "Archlinux" },
{ "operatingsystem": "CentOS" },
{ "operatingsystem": "Darwin" },
{ "operatingsystem": "Debian" },
{ "operatingsystem": "Fedora" },
{ "operatingsystem": "FreeBSD" },
{ "operatingsystem": "Gentoo" },
{ "operatingsystem": "LinuxMint" },
{ "operatingsystem": "OpenBSD" },
{ "operatingsystem": "OpenSUSE" },
{ "operatingsystem": "OracleLinux" },
{ "operatingsystem": "RedHat" },
{ "operatingsystem": "Pop!_OS" },
{ "operatingsystem": "Rocky" },
{ "operatingsystem": "SLES" },
{ "operatingsystem": "Scientific" },
{ "operatingsystem": "Solaris" },
{ "operatingsystem": "Ubuntu" },
{ "operatingsystem": "VirtuozzoLinux" },
{ "operatingsystem": "Windows" }
],
"requirements": [
{
Expand Down
23 changes: 13 additions & 10 deletions spec/acceptance/suites/windows/nodesets/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,33 @@
end
-%>
HOSTS:
el7:
roles:
- default
platform: el-7-x86_64
box: centos/7
hypervisor: <%= hypervisor %>

win:
roles:
- windows
platform: windows-server-amd64
box: devopsgroup-io/windows_server-2012r2-standard-amd64-nocm
box: gusztavvargadr/windows-server
box_version: 2102.0.2205
hypervisor: <%= hypervisor %>
vagrant_memsize: 2048
vagrant_cpus: 2
user: vagrant
communicator: winrm
is_cygwin: false
ssh:
host_key: '+ssh-dss'

CONFIG:
log_level: verbose
type: aio
<% if ENV['BEAKER_PUPPET_ENVIRONMENT'] -%>
puppet_environment: <%= ENV['BEAKER_PUPPET_ENVIRONMENT'] %>
<% end -%>
ssh:
keepalive: true
keepalive_interval: 10
host_key:
- <%= Net::SSH::Transport::Algorithms::ALGORITHMS[:host_key].join("\n#{' '*6}- ") %>
kex:
- <%= Net::SSH::Transport::Algorithms::ALGORITHMS[:kex].join("\n#{' '*6}- ") %>
encryption:
- <%= Net::SSH::Transport::Algorithms::ALGORITHMS[:encryption].join("\n#{' '*6}- ") %>
hmac:
- <%= Net::SSH::Transport::Algorithms::ALGORITHMS[:hmac].join("\n#{' '*6}- ") %>
22 changes: 22 additions & 0 deletions spec/functions/assert_metadata_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
require 'spec_helper'

describe 'simplib::assert_metadata' do
module_metadata = {
'name' => 'simp-simplib',
'version' => '1.2.3',
'author' => 'Yep',
'summary' => 'Stubby',
'license' => 'Apache-2.0',
'operatingsystem_support' => [
{
'operatingsystem' => 'Ubuntu',
'operatingsystemrelease' => ['14.04']
}
]
}.to_json

valid_facts = {
:os => {
'name' => 'Ubuntu',
Expand Down Expand Up @@ -57,6 +71,14 @@
}
}

let(:pre_condition) do
<<~PRE_CONDITION
function load_module_metadata(String $any) {
parsejson('#{module_metadata}')
}
PRE_CONDITION
end

context 'with no version matching' do
context 'on a supported OS' do
let(:facts) { valid_facts }
Expand Down
22 changes: 22 additions & 0 deletions spec/functions/module_metadata/assert_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
require 'spec_helper'

describe 'simplib::module_metadata::assert' do
module_metadata = {
'name' => 'simp-simplib',
'version' => '1.2.3',
'author' => 'Yep',
'summary' => 'Stubby',
'license' => 'Apache-2.0',
'operatingsystem_support' => [
{
'operatingsystem' => 'Ubuntu',
'operatingsystemrelease' => ['14.04']
}
]
}.to_json

valid_facts = {
:os => {
'name' => 'Ubuntu',
Expand Down Expand Up @@ -91,6 +105,14 @@
}
}

let(:pre_condition) do
<<~PRE_CONDITION
function load_module_metadata(String $any) {
parsejson('#{module_metadata}')
}
PRE_CONDITION
end

context 'with no version matching' do
context 'on a supported OS' do
let(:facts) { valid_facts }
Expand Down
17 changes: 16 additions & 1 deletion spec/functions/simplib/assert_optional_dependency_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
},
{
'name' => 'dep/two'
},
{
'name' => 'dep-three',
'version_requirement' => '>= 1.2.3 < 2.0.0'
}
]
}
Expand All @@ -35,6 +39,13 @@
}
}

let(:dep_three_metadata) {
{
'name' => 'dep-three',
'version' => '1.2.3-alpha'
}
}

let(:dep_one_bad_author) {
{
'name' => 'narp/one',
Expand All @@ -57,6 +68,8 @@
expect(func).to receive(:call_function).with('load_module_metadata', 'one').and_return(dep_one_metadata)
expect(func).to receive(:call_function).with('simplib::module_exist', 'two').and_return(true)
expect(func).to receive(:call_function).with('load_module_metadata', 'two').and_return(dep_two_metadata)
expect(func).to receive(:call_function).with('simplib::module_exist', 'three').and_return(true)
expect(func).to receive(:call_function).with('load_module_metadata', 'three').and_return(dep_three_metadata)
is_expected.to run.with_params('my/module')
end

Expand Down Expand Up @@ -125,7 +138,7 @@
it 'should fail' do
expect(func).to_not receive(:call_function).with('simplib::module_exist', 'one')

expect{is_expected.to run.with_params('my/module', 'three')}.to raise_error(%r('three' not found in metadata.json))
expect{is_expected.to run.with_params('my/module', 'badmod')}.to raise_error(%r('badmod' not found in metadata.json))
end
end
end
Expand All @@ -137,6 +150,8 @@
expect(func).to receive(:call_function).with('load_module_metadata', 'one').and_return(dep_one_bad_version)
expect(func).to receive(:call_function).with('simplib::module_exist', 'two').and_return(true)
expect(func).to receive(:call_function).with('load_module_metadata', 'two').and_return(dep_two_metadata)
expect(func).to receive(:call_function).with('simplib::module_exist', 'three').and_return(true)
expect(func).to receive(:call_function).with('load_module_metadata', 'three').and_return(dep_three_metadata)

expect{ is_expected.to run.with_params('my/module') }.to raise_error(%r('one-.+' does not satisfy)m)
end
Expand Down
1 change: 1 addition & 0 deletions spec/functions/simplib/simp_version_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
context 'a valid version exists in simp.version' do
before(:each) do
allow(File).to receive(:read).with(any_args).and_call_original
allow(File).to receive(:readable?).with(any_args).and_call_original
end

it 'should return the version with whitespace retained' do
Expand Down
Loading

0 comments on commit 56f0d53

Please sign in to comment.