From 84dde24b763de2555c60e442b90adc3e996a272d Mon Sep 17 00:00:00 2001 From: John Bond Date: Mon, 13 Nov 2017 16:09:04 +0800 Subject: [PATCH] add Stdlib::Fqdn and Stdlib::Host --- README.md | 52 ++++++++++++++++++++++++++++ spec/aliases/fqdn_spec.rb | 37 ++++++++++++++++++++ spec/aliases/host_spec.rb | 46 ++++++++++++++++++++++++ spec/fixtures/test/manifests/fqdn.pp | 6 ++++ spec/fixtures/test/manifests/host.pp | 6 ++++ types/fqdn.pp | 1 + types/host.pp | 1 + 7 files changed, 149 insertions(+) create mode 100644 spec/aliases/fqdn_spec.rb create mode 100644 spec/aliases/host_spec.rb create mode 100644 spec/fixtures/test/manifests/fqdn.pp create mode 100644 spec/fixtures/test/manifests/host.pp create mode 100644 types/fqdn.pp create mode 100644 types/host.pp diff --git a/README.md b/README.md index ffbd7cd51..8b8f95a02 100644 --- a/README.md +++ b/README.md @@ -413,6 +413,58 @@ Unacceptable input example: /usr2/username/bin:/usr/local/bin:/usr/bin:. ``` +#### `Stdlib::Fqdn` + +Matches paths on fully quallified domain name + +Acceptable input example: + +```shell +localhost + +example.com + +www.example.com +``` + +Unacceptable input example: + +```shell +'www www.example.com' + +2001:DB8::1 +``` + +#### `Stdlib::host` + +Matches a valid host which could be a valid ipv4, ipv6 or fqdn + +Acceptable input example: + +```shell +localhost + +example.com + +www.example.com + +2001:0db8::1 + +192.0.2.1 +``` + +Unacceptable input example: + +```shell +'www www.example.com' + +2001:0d8 + +%.example.com + +bob@example.com +``` + ### Facts #### `package_provider` diff --git a/spec/aliases/fqdn_spec.rb b/spec/aliases/fqdn_spec.rb new file mode 100644 index 000000000..ede62474e --- /dev/null +++ b/spec/aliases/fqdn_spec.rb @@ -0,0 +1,37 @@ +require 'spec_helper' + +if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 + describe 'test::fqdn', type: :class do + describe 'valid handling' do + %w( + example + example.com + www.example.com + ).each do |value| + describe value.inspect do + let(:params) { { value: value } } + it { is_expected.to compile } + end + end + end + + describe 'invalid path handling' do + context 'garbage inputs' do + [ + [nil], + [nil, nil], + { 'foo' => 'bar' }, + {}, + '', + '2001:DB8::1', + 'www www.example.com' + ].each do |value| + describe value.inspect do + let(:params) { { value: value } } + it { is_expected.to compile.and_raise_error(%r{parameter 'value' expects a match for Stdlib::Fqdn}) } + end + end + end + end + end +end diff --git a/spec/aliases/host_spec.rb b/spec/aliases/host_spec.rb new file mode 100644 index 000000000..c4b7d6b67 --- /dev/null +++ b/spec/aliases/host_spec.rb @@ -0,0 +1,46 @@ +require 'spec_helper' + +if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 + describe 'test::host', type: :class do + describe 'valid handling' do + %w( + example + example.com + www.example.com + 2001:0db8:85a3:0000:0000:8a2e:0370:7334 + fa76:8765:34ac:0823:ab76:eee9:0987:1111 + 2001:0db8::1 + 224.0.0.0 + 255.255.255.255 + 0.0.0.0 + 192.88.99.0 + ).each do |value| + describe value.inspect do + let(:params) { { value: value } } + it { is_expected.to compile } + end + end + end + + describe 'invalid handling' do + context 'garbage inputs' do + [ + [nil], + [nil, nil], + { 'foo' => 'bar' }, + {}, + '', + 'www www.example.com', + 'bob@example.com', + '%.example.com', + '2001:0d8' + ].each do |value| + describe value.inspect do + let(:params) { { value: value } } + it { is_expected.to compile.and_raise_error(%r{parameter 'value' expects a Stdlib::Host}) } + end + end + end + end + end +end diff --git a/spec/fixtures/test/manifests/fqdn.pp b/spec/fixtures/test/manifests/fqdn.pp new file mode 100644 index 000000000..c1e5fd7ce --- /dev/null +++ b/spec/fixtures/test/manifests/fqdn.pp @@ -0,0 +1,6 @@ +# Class to test the Stdlib::Fqdn type alias +class test::fqdn ( + Stdlib::Fqdn $value, +) { + notice('Success') +} diff --git a/spec/fixtures/test/manifests/host.pp b/spec/fixtures/test/manifests/host.pp new file mode 100644 index 000000000..4460d49ce --- /dev/null +++ b/spec/fixtures/test/manifests/host.pp @@ -0,0 +1,6 @@ +# Class to test the Stdlib::Host type alias +class test::host ( + Stdlib::Host $value, +) { + notice('Success') +} diff --git a/types/fqdn.pp b/types/fqdn.pp new file mode 100644 index 000000000..840ec5cec --- /dev/null +++ b/types/fqdn.pp @@ -0,0 +1 @@ +type Stdlib::Fqdn = Pattern[/^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/] diff --git a/types/host.pp b/types/host.pp new file mode 100644 index 000000000..8f6919e70 --- /dev/null +++ b/types/host.pp @@ -0,0 +1 @@ +type Stdlib::Host = Variant[Stdlib::Fqdn, Stdlib::Compat::Ip_address]