Skip to content

Commit 2c8f018

Browse files
authored
Merge branch 'main' into CONT-801-Puppet_8_support
2 parents 564cf4b + 03bcefd commit 2c8f018

File tree

9 files changed

+231
-18
lines changed

9 files changed

+231
-18
lines changed

REFERENCE.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,8 @@ OpenSSL.
289289
* [`Stdlib::HTTPSUrl`](#Stdlib--HTTPSUrl): Validate a HTTPS URL
290290
* [`Stdlib::HTTPUrl`](#Stdlib--HTTPUrl): Validate a HTTP(S) URL
291291
* [`Stdlib::Host`](#Stdlib--Host): Validate a host (FQDN or IP address)
292+
* [`Stdlib::Http::Method`](#Stdlib--Http--Method): Valid HTTP method verbs
293+
* [`Stdlib::Http::Status`](#Stdlib--Http--Status): A valid HTTP status code per RFC9110
292294
* [`Stdlib::HttpStatus`](#Stdlib--HttpStatus): Validate a HTTP status code
293295
* [`Stdlib::IP::Address`](#Stdlib--IP--Address): Validate an IP address
294296
* [`Stdlib::IP::Address::Nosubnet`](#Stdlib--IP--Address--Nosubnet): Validate an IP address without subnet
@@ -7796,11 +7798,32 @@ Validate a host (FQDN or IP address)
77967798

77977799
Alias of `Variant[Stdlib::Fqdn, Stdlib::Compat::Ip_address]`
77987800

7801+
### <a name="Stdlib--Http--Method"></a>`Stdlib::Http::Method`
7802+
7803+
Valid HTTP method verbs
7804+
7805+
* **See also**
7806+
* https://www.iana.org/assignments/http-methods/http-methods.xhtml
7807+
7808+
Alias of `Enum['ACL', 'BASELINE-CONTROL', 'BIND', 'CHECKIN', 'CHECKOUT', 'CONNECT', 'COPY', 'DELETE', 'GET', 'HEAD', 'LABEL', 'LINK', 'LOCK', 'MERGE', 'MKACTIVITY', 'MKCALENDAR', 'MKCOL', 'MKREDIRECTREF', 'MKWORKSPACE', 'MOVE', 'OPTIONS', 'ORDERPATCH', 'PATCH', 'POST', 'PRI', 'PROPFIND', 'PROPPATCH', 'PUT', 'REBIND', 'REPORT', 'SEARCH', 'TRACE', 'UNBIND', 'UNCHECKOUT', 'UNLINK', 'UNLOCK', 'UPDATE', 'UPDATEREDIRECTREF', 'VERSION-CONTROL']`
7809+
7810+
### <a name="Stdlib--Http--Status"></a>`Stdlib::Http::Status`
7811+
7812+
A valid HTTP status code per RFC9110
7813+
7814+
* **See also**
7815+
* https://httpwg.org/specs/rfc9110.html#overview.of.status.codes
7816+
7817+
Alias of `Integer[100, 599]`
7818+
77997819
### <a name="Stdlib--HttpStatus"></a>`Stdlib::HttpStatus`
78007820

78017821
Validate a HTTP status code
78027822

7803-
Alias of `Integer[100, 599]`
7823+
* **See also**
7824+
* Stdlib::Http::Status
7825+
7826+
Alias of `Stdlib::Http::Status`
78047827

78057828
### <a name="Stdlib--IP--Address"></a>`Stdlib::IP::Address`
78067829

lib/puppet/functions/ensure_packages.rb

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,56 @@
66
# third argument to the ensure_resource() function.
77
Puppet::Functions.create_function(:ensure_packages, Puppet::Functions::InternalFunction) do
88
# @param packages
9-
# The packages to ensure are installed. If it's a Hash it will be passed to `ensure_resource`
9+
# The packages to ensure are installed.
1010
# @param default_attributes
1111
# Default attributes to be passed to the `ensure_resource()` function
1212
# @return [Undef] Returns nothing.
1313
dispatch :ensure_packages do
1414
scope_param
15-
param 'Variant[String[1], Array[String[1]], Hash[String[1], Any]]', :packages
15+
param 'Variant[String[1], Array[String[1]]]', :packages
1616
optional_param 'Hash', :default_attributes
1717
return_type 'Undef'
1818
end
1919

20-
def ensure_packages(scope, packages, default_attributes = nil)
21-
if default_attributes
20+
# @param packages
21+
# The packages to ensure are installed. The keys are packages and values are the attributes specific to that package.
22+
# @param default_attributes
23+
# Default attributes. Package specific attributes from the `packages` parameter will take precedence.
24+
# @return [Undef] Returns nothing.
25+
dispatch :ensure_packages_hash do
26+
scope_param
27+
param 'Hash[String[1], Any]', :packages
28+
optional_param 'Hash', :default_attributes
29+
return_type 'Undef'
30+
end
31+
32+
def ensure_packages(scope, packages, default_attributes = {})
33+
Array(packages).each do |package_name|
2234
defaults = { 'ensure' => 'installed' }.merge(default_attributes)
23-
if defaults['ensure'] == 'present'
24-
defaults['ensure'] = 'installed'
25-
end
26-
else
27-
defaults = { 'ensure' => 'installed' }
35+
36+
# `present` and `installed` are aliases for the `ensure` attribute. If `ensure` is set to either of these values replace
37+
# with `installed` by default but `present` if this package is already in the catalog with `ensure => present`
38+
defaults['ensure'] = default_ensure(package_name) if ['present', 'installed'].include?(defaults['ensure'])
39+
40+
scope.call_function('ensure_resource', ['package', package_name, defaults])
2841
end
42+
nil
43+
end
2944

30-
if packages.is_a?(Hash)
31-
scope.call_function('ensure_resources', ['package', packages.dup, defaults])
32-
else
33-
Array(packages).each do |package_name|
34-
scope.call_function('ensure_resource', ['package', package_name, defaults])
35-
end
45+
def ensure_packages_hash(scope, packages, default_attributes = {})
46+
packages.each do |package, attributes|
47+
ensure_packages(scope, package, default_attributes.merge(attributes))
3648
end
3749
nil
3850
end
51+
52+
private
53+
54+
def default_ensure(package_name)
55+
if call_function('defined_with_params', "Package[#{package_name}]", { 'ensure' => 'present' })
56+
'present'
57+
else
58+
'installed'
59+
end
60+
end
3961
end

spec/functions/ensure_packages_spec.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@
3232
subject.execute({ 'foo' => { 'provider' => 'rpm' }, 'bar' => { 'provider' => 'gem' } }, 'ensure' => 'present')
3333
subject.execute('パッケージ' => { 'ensure' => 'absent' })
3434
subject.execute('ρǻ¢κầģẻ' => { 'ensure' => 'absent' })
35+
subject.execute(
36+
{
37+
'package_one' => {},
38+
'package_two' => {},
39+
'package_three' => { 'provider' => 'puppetserver_gem' },
40+
},
41+
{ 'provider' => 'puppet_gem' },
42+
)
3543
end
3644

3745
# this lambda is required due to strangeness within rspec-puppet's expectation handling
@@ -42,6 +50,14 @@
4250
it { expect(-> { catalogue }).to contain_package('パッケージ').with('ensure' => 'absent') }
4351
it { expect(-> { catalogue }).to contain_package('ρǻ¢κầģẻ').with('ensure' => 'absent') }
4452
end
53+
54+
describe 'default attributes' do
55+
it 'package specific attributes take precedence' do
56+
expect(-> { catalogue }).to contain_package('package_one').with('provider' => 'puppet_gem')
57+
expect(-> { catalogue }).to contain_package('package_two').with('provider' => 'puppet_gem')
58+
expect(-> { catalogue }).to contain_package('package_three').with('provider' => 'puppetserver_gem')
59+
end
60+
end
4561
end
4662

4763
context 'when given a catalog with "package { puppet: ensure => installed }"' do
@@ -54,4 +70,26 @@
5470
it { expect(-> { catalogue }).to contain_package('puppet').with_ensure('installed') }
5571
end
5672
end
73+
74+
context 'when given a catalog with "package { puppet: ensure => present }"' do
75+
let(:pre_condition) { 'package { puppet: ensure => present }' }
76+
77+
describe 'after running ensure_package("puppet", { "ensure" => "present" })' do
78+
before(:each) { subject.execute('puppet', 'ensure' => 'present') }
79+
80+
it { expect(-> { catalogue }).to contain_package('puppet').with_ensure('present') }
81+
end
82+
83+
describe 'after running ensure_package("puppet", { "ensure" => "installed" })' do
84+
before(:each) { subject.execute('puppet', 'ensure' => 'installed') }
85+
86+
it { expect(-> { catalogue }).to contain_package('puppet').with_ensure('present') }
87+
end
88+
89+
describe 'after running ensure_package(["puppet"])' do
90+
before(:each) { subject.execute(['puppet']) }
91+
92+
it { expect(-> { catalogue }).to contain_package('puppet').with_ensure('present') }
93+
end
94+
end
5795
end

spec/functions/fqdn_rand_string_spec.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ def fqdn_rand_string(max, args = {})
5757

5858
# workaround not being able to use let(:facts) because some tests need
5959
# multiple different hostnames in one context
60-
allow(scope).to receive(:lookupvar).with('facts', {}).and_return(host)
60+
if Gem::Version.new(Puppet::PUPPETVERSION) < Gem::Version.new('7.23.0')
61+
allow(scope).to receive(:lookupvar).with('::fqdn', {}).and_return(host)
62+
else
63+
allow(scope).to receive(:lookupvar).with('facts', {}).and_return({ 'networking' => { 'fqdn' => host } })
64+
end
6165

6266
function_args = [max]
6367
if args.key?(:charset) || !extra.empty?
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
require 'spec_helper'
2+
3+
describe 'Stdlib::Http::Method' do
4+
describe 'valid HTTP Methods' do
5+
[
6+
'HEAD',
7+
'GET',
8+
'PUT',
9+
'DELETE',
10+
'TRACE',
11+
].each do |value|
12+
describe value.inspect do
13+
it { is_expected.to allow_value(value) }
14+
end
15+
end
16+
end
17+
18+
describe 'invalid path handling' do
19+
context 'garbage inputs' do
20+
[
21+
nil,
22+
[nil],
23+
[nil, nil],
24+
{ 'foo' => 'bar' },
25+
{},
26+
'',
27+
'https',
28+
'199',
29+
600,
30+
1_000,
31+
'Ok',
32+
'get',
33+
].each do |value|
34+
describe value.inspect do
35+
it { is_expected.not_to allow_value(value) }
36+
end
37+
end
38+
end
39+
end
40+
end
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
require 'spec_helper'
2+
3+
describe 'Stdlib::Http::Status' do
4+
describe 'valid HTTP Status' do
5+
[
6+
200,
7+
302,
8+
404,
9+
418,
10+
503,
11+
].each do |value|
12+
describe value.inspect do
13+
it { is_expected.to allow_value(value) }
14+
end
15+
end
16+
end
17+
18+
describe 'invalid path handling' do
19+
context 'garbage inputs' do
20+
[
21+
nil,
22+
[nil],
23+
[nil, nil],
24+
{ 'foo' => 'bar' },
25+
{},
26+
'',
27+
'https',
28+
'199',
29+
600,
30+
1_000,
31+
].each do |value|
32+
describe value.inspect do
33+
it { is_expected.not_to allow_value(value) }
34+
end
35+
end
36+
end
37+
end
38+
end

types/http/method.pp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# @summary Valid HTTP method verbs
2+
# @see https://www.iana.org/assignments/http-methods/http-methods.xhtml
3+
type Stdlib::Http::Method = Enum[
4+
'ACL',
5+
'BASELINE-CONTROL',
6+
'BIND',
7+
'CHECKIN',
8+
'CHECKOUT',
9+
'CONNECT',
10+
'COPY',
11+
'DELETE',
12+
'GET',
13+
'HEAD',
14+
'LABEL',
15+
'LINK',
16+
'LOCK',
17+
'MERGE',
18+
'MKACTIVITY',
19+
'MKCALENDAR',
20+
'MKCOL',
21+
'MKREDIRECTREF',
22+
'MKWORKSPACE',
23+
'MOVE',
24+
'OPTIONS',
25+
'ORDERPATCH',
26+
'PATCH',
27+
'POST',
28+
'PRI',
29+
'PROPFIND',
30+
'PROPPATCH',
31+
'PUT',
32+
'REBIND',
33+
'REPORT',
34+
'SEARCH',
35+
'TRACE',
36+
'UNBIND',
37+
'UNCHECKOUT',
38+
'UNLINK',
39+
'UNLOCK',
40+
'UPDATE',
41+
'UPDATEREDIRECTREF',
42+
'VERSION-CONTROL',
43+
]

types/http/status.pp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# @summary A valid HTTP status code per RFC9110
2+
# @see https://httpwg.org/specs/rfc9110.html#overview.of.status.codes
3+
type Stdlib::Http::Status = Integer[100, 599]

types/httpstatus.pp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
# @summary Validate a HTTP status code
2-
type Stdlib::HttpStatus = Integer[100, 599]
2+
# @deprecated Use Stdlib::Http::Status
3+
# @see Stdlib::Http::Status
4+
type Stdlib::HttpStatus = Stdlib::Http::Status

0 commit comments

Comments
 (0)