From f852ba30453d5fa88218178cc9e129cea99a284b Mon Sep 17 00:00:00 2001 From: John Bond Date: Mon, 13 Nov 2017 15:16:19 +0800 Subject: [PATCH] add Stdlib::Filesource --- README.md | 34 +++++++++++++++- spec/type_aliases/filesource_spec.rb | 59 ++++++++++++++++++++++++++++ types/filesource.pp | 9 +++++ 3 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 spec/type_aliases/filesource_spec.rb create mode 100644 types/filesource.pp diff --git a/README.md b/README.md index bd0802230..a905640bd 100644 --- a/README.md +++ b/README.md @@ -409,8 +409,40 @@ C:\\ Unacceptable input example: +#### `Stdlib::Filesource` + +Matches paths valid values for the source parameter of the puppet file type + +Acceptable input example: + ```shell -/usr2/username/bin:/usr/local/bin:/usr/bin:. +http://example.com + +https://example.com + +file:///hello/bla + +/usr2/username/bin + +C:\foo\bar + +/var/opt/../lib/puppet + +puppet:///modules/foo/bar.log +``` + +Unacceptable input example: + +```shell +*/Users//nope + +\Users/hc/wksp/stdlib + +C:noslashes + +puppet:///foo/bar.log + +ftp://ftp.example.com ``` #### `Stdlib::Fqdn` diff --git a/spec/type_aliases/filesource_spec.rb b/spec/type_aliases/filesource_spec.rb new file mode 100644 index 000000000..da1bc3e55 --- /dev/null +++ b/spec/type_aliases/filesource_spec.rb @@ -0,0 +1,59 @@ +require 'spec_helper' + +if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 + describe 'Stdlib::Filesource' do + describe 'valid handling' do + %w[ + https://hello.com + https://notcreative.org + https://canstillaccepthttps.co.uk + http://anhttp.com + http://runningoutofideas.gov + file:///hello/bla + file:///foo/bar.log + puppet:///modules/foo/bar.log + puppet://pm.example.com/modules/foo/bar.log + puppet://192.0.2.1/modules/foo/bar.log + /usr2/username/bin:/usr/local/bin:/usr/bin:. + C:/ + C:\\ + C:\\WINDOWS\\System32 + C:/windows/system32 + X:/foo/bar + X:\\foo\\bar + \\\\host\\windows + //host/windows + /var/tmp + /var/opt/../lib/puppet + ].each do |value| + describe value.inspect do + it { is_expected.to allow_value(value) } + end + end + end + + describe 'invalid path handling' do + context 'garbage inputs' do + [ + nil, + [nil], + [nil, nil], + { 'foo' => 'bar' }, + {}, + '', + '*/Users//nope', + '\\Users/hc/wksp/stdlib', + 'C:noslashes', + '\\var\\tmp', + 'puppet:///foo/bar.log', + 'puppet:///pm.example.com/modules/foo/bar.log', + 'puppet://bob@pm.example.com/modules/foo/bar.log', + ].each do |value| + describe value.inspect do + it { is_expected.not_to allow_value(value) } + end + end + end + end + end +end diff --git a/types/filesource.pp b/types/filesource.pp new file mode 100644 index 000000000..d480ed0c6 --- /dev/null +++ b/types/filesource.pp @@ -0,0 +1,9 @@ +# Validate the source parameter on file types +type Stdlib::Filesource = Variant[ + Stdlib::Absolutepath, + Stdlib::HTTPUrl, + Pattern[ + /^file:\/\/\/([^\/\0]+(\/)?)+$/, + /^puppet:\/\/(([\w-]+\.?)+)?\/modules\/([^\/\0]+(\/)?)+$/, + ], +]