From 11708ed00f839e12dd8cef99f8c647ff25285c52 Mon Sep 17 00:00:00 2001 From: Ewoud Kohl van Wijngaarden Date: Wed, 11 Oct 2017 17:45:27 +0200 Subject: [PATCH 1/2] Add tests for types --- .fixtures.yml | 1 + spec/aliases/macaddress_spec.rb | 59 +++++++++++++++++++ spec/aliases/staticroute_spec.rb | 43 ++++++++++++++ .../test_module/manifests/macaddress.pp | 6 ++ .../test_module/manifests/staticroute.pp | 6 ++ types/macaddress.pp | 2 +- 6 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 spec/aliases/macaddress_spec.rb create mode 100644 spec/aliases/staticroute_spec.rb create mode 100644 spec/static_fixtures/test_module/manifests/macaddress.pp create mode 100644 spec/static_fixtures/test_module/manifests/staticroute.pp diff --git a/.fixtures.yml b/.fixtures.yml index 8fbb804a..e405545e 100644 --- a/.fixtures.yml +++ b/.fixtures.yml @@ -6,3 +6,4 @@ fixtures: symlinks: dhcp: "#{source_dir}" + test_module: "#{source_dir}/spec/static_fixtures/test_module" diff --git a/spec/aliases/macaddress_spec.rb b/spec/aliases/macaddress_spec.rb new file mode 100644 index 00000000..e0dcd496 --- /dev/null +++ b/spec/aliases/macaddress_spec.rb @@ -0,0 +1,59 @@ +require 'spec_helper' + +describe 'test_module::macaddress', type: :class do + describe 'valid handling' do + [ + 'a:a:a:a:a:a', + '00:00:00:00:00:00', + '11:11:11:11:11:11', + '22:22:22:22:22:22', + '33:33:33:33:33:33', + '44:44:44:44:44:44', + '55:55:55:55:55:55', + '66:66:66:66:66:66', + '77:77:77:77:77:77', + '88:88:88:88:88:88', + '99:99:99:99:99:99', + 'aa:aa:aa:aa:aa:aa', + 'bb:bb:bb:bb:bb:bb', + 'cc:cc:cc:cc:cc:cc', + 'dd:dd:dd:dd:dd:dd', + 'ee:ee:ee:ee:ee:ee', + 'ff:ff:ff:ff:ff:ff', + 'AA:AA:AA:AA:AA:AA', + 'BB:BB:BB:BB:BB:BB', + 'CC:CC:CC:CC:CC:CC', + 'DD:DD:DD:DD:DD:DD', + 'EE:EE:EE:EE:EE:EE', + 'FF:FF:FF:FF:FF:FF', + ].each do |value| + describe value.inspect do + let(:params) {{ value: value }} + it { is_expected.to compile } + end + end + end + + describe 'invalid value handling' do + context 'garbage inputs' do + [ + nil, + "aa:aa:aa:aa:aa", + "aaa:aa:aa:aa:aa:aa", + "aa:aaa:aa:aa:aa:aa", + "aa:aa:aaa:aa:aa:aa", + "aa:aa:aa:aaa:aa:aa", + "aa:aa:aa:aa:aaa:aa", + "aa:aa:aa:aa:aa:aaa", + "aa:aa:aa:aa:aa:aa:aa", + "gg:gg:gg:gg:gg:gg", + ].each do |value| + describe value.inspect do + let(:params) {{ value: value }} + it { is_expected.to compile.and_raise_error(/parameter 'value' expects a match for Dhcp::Macaddress/) } + end + end + end + + end +end diff --git a/spec/aliases/staticroute_spec.rb b/spec/aliases/staticroute_spec.rb new file mode 100644 index 00000000..714d1d5a --- /dev/null +++ b/spec/aliases/staticroute_spec.rb @@ -0,0 +1,43 @@ +require 'spec_helper' + +describe 'test_module::staticroute', type: :class do + describe 'valid handling' do + [ + { + 'mask' => '255.255.255.0', + 'gateway' => '192.0.2.1', + }, + { + 'mask' => '255.255.255.0', + 'gateway' => '192.0.2.1', + 'network' => '192.0.2.0', + }, + ].each do |value| + describe value.inspect do + let(:params) {{ value: value }} + it { is_expected.to compile } + end + end + end + + describe 'invalid value handling' do + context 'garbage inputs' do + [ + nil, + {}, + { + 'mask' => '255.255.255.0', + }, + { + 'gateway' => '192.0.2.1', + }, + ].each do |value| + describe value.inspect do + let(:params) {{ value: value }} + it { is_expected.to compile.and_raise_error(/parameter 'value' expects/) } + end + end + end + + end +end diff --git a/spec/static_fixtures/test_module/manifests/macaddress.pp b/spec/static_fixtures/test_module/manifests/macaddress.pp new file mode 100644 index 00000000..7191913d --- /dev/null +++ b/spec/static_fixtures/test_module/manifests/macaddress.pp @@ -0,0 +1,6 @@ +# Class to test the dhcp::Macaddress type +class test_module::macaddress( + Dhcp::Macaddress $value, +) { + notice('Success') +} diff --git a/spec/static_fixtures/test_module/manifests/staticroute.pp b/spec/static_fixtures/test_module/manifests/staticroute.pp new file mode 100644 index 00000000..6ecc55d1 --- /dev/null +++ b/spec/static_fixtures/test_module/manifests/staticroute.pp @@ -0,0 +1,6 @@ +# Class to test the dhcp::StaticRoute type +class test_module::staticroute( + Dhcp::StaticRoute $value, +) { + notice('Success') +} diff --git a/types/macaddress.pp b/types/macaddress.pp index 9d200272..8b2d0c8f 100644 --- a/types/macaddress.pp +++ b/types/macaddress.pp @@ -1 +1 @@ -type Dhcp::Macaddress = Pattern[/[0-9A-Fa-f]{1,2}(:[0-9A-Fa-f]{1,2}){5}/] +type Dhcp::Macaddress = Pattern[/^[0-9A-Fa-f]{1,2}(:[0-9A-Fa-f]{1,2}){5}$/] From 7d31525491d362f090a31fad3bfe1802adcd59f4 Mon Sep 17 00:00:00 2001 From: Ewoud Kohl van Wijngaarden Date: Wed, 11 Oct 2017 17:50:40 +0200 Subject: [PATCH 2/2] Fixes #16913 - Add validation for the dhcp range --- manifests/pool.pp | 2 +- spec/aliases/range_spec.rb | 35 +++++++++++++++++++ spec/defines/pool_spec.rb | 14 ++++---- .../test_module/manifests/range.pp | 6 ++++ types/range.pp | 1 + 5 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 spec/aliases/range_spec.rb create mode 100644 spec/static_fixtures/test_module/manifests/range.pp create mode 100644 types/range.pp diff --git a/manifests/pool.pp b/manifests/pool.pp index 46f29024..7a7339a4 100644 --- a/manifests/pool.pp +++ b/manifests/pool.pp @@ -3,7 +3,7 @@ String $mask, Optional[String] $gateway = undef, Variant[Array[String], Optional[String]] $pool_parameters = undef, - Variant[Array[String], Optional[String], Boolean] $range = undef, + Variant[Array[Dhcp::Range], Optional[Dhcp::Range], Enum[''], Boolean] $range = undef, Optional[String] $failover = undef, Variant[Array[String], Optional[String]] $options = undef, Variant[Array[String], Optional[String]] $parameters = undef, diff --git a/spec/aliases/range_spec.rb b/spec/aliases/range_spec.rb new file mode 100644 index 00000000..ca18210c --- /dev/null +++ b/spec/aliases/range_spec.rb @@ -0,0 +1,35 @@ +require 'spec_helper' + +describe 'test_module::range', type: :class do + describe 'valid handling' do + [ + '192.0.2.100', + '192.0.2.100 192.0.2.200', + '1.1.1.1 255.255.255.255', + 'dynamic-bootp 192.0.2.100 192.0.2.200', + ].each do |value| + describe value.inspect do + let(:params) {{ value: value }} + it { is_expected.to compile } + end + end + end + + describe 'invalid value handling' do + context 'garbage inputs' do + [ + nil, + "all", + "all all", + "1 192.0.2.183", + "192.0.2.100 1", + ].each do |value| + describe value.inspect do + let(:params) {{ value: value }} + it { is_expected.to compile.and_raise_error(/parameter 'value' expects a match for Dhcp::Range/) } + end + end + end + + end +end diff --git a/spec/defines/pool_spec.rb b/spec/defines/pool_spec.rb index 571b7a04..ffe398e0 100644 --- a/spec/defines/pool_spec.rb +++ b/spec/defines/pool_spec.rb @@ -32,7 +32,7 @@ let :params do { :network => '10.0.0.0', :mask => '255.255.255.0', - :range => '10.0.0.10 - 10.0.0.50', + :range => '10.0.0.10 10.0.0.50', :failover => '10.1.1.20', } end @@ -42,7 +42,7 @@ ' pool', ' {', ' failover peer "10.1.1.20";', - ' range 10.0.0.10 - 10.0.0.50;', + ' range 10.0.0.10 10.0.0.50;', ' }', ' option subnet-mask 255.255.255.0;', '}', @@ -70,7 +70,7 @@ let :params do { :network => '10.0.0.0', :mask => '255.255.255.0', - :range => ['10.0.0.10 - 10.0.0.50','10.0.0.100 - 10.0.0.150'], + :range => ['10.0.0.10 10.0.0.50','10.0.0.100 10.0.0.150'], } end it { @@ -78,8 +78,8 @@ 'subnet 10.0.0.0 netmask 255.255.255.0 {', ' pool', ' {', - ' range 10.0.0.10 - 10.0.0.50;', - ' range 10.0.0.100 - 10.0.0.150;', + ' range 10.0.0.10 10.0.0.50;', + ' range 10.0.0.100 10.0.0.150;', ' }', ' option subnet-mask 255.255.255.0;', '}', @@ -109,7 +109,7 @@ :network => '10.0.0.0', :mask => '255.255.255.0', :pool_parameters => 'allow members of "some-class"', - :range => '10.0.0.10 - 10.0.0.50', + :range => '10.0.0.10 10.0.0.50', :gateway => '10.0.0.1', :options => 'ntp-servers 10.0.0.2', :parameters => 'max-lease-time 300', @@ -132,7 +132,7 @@ " pool", " {", " allow members of \"some-class\";", - " range 10.0.0.10 - 10.0.0.50;", + " range 10.0.0.10 10.0.0.50;", " }", " option domain-name \"example.org\";", " option subnet-mask 255.255.255.0;", diff --git a/spec/static_fixtures/test_module/manifests/range.pp b/spec/static_fixtures/test_module/manifests/range.pp new file mode 100644 index 00000000..09edf8df --- /dev/null +++ b/spec/static_fixtures/test_module/manifests/range.pp @@ -0,0 +1,6 @@ +# Class to test the dhcp::Range type +class test_module::range( + Dhcp::Range $value, +) { + notice('Success') +} diff --git a/types/range.pp b/types/range.pp new file mode 100644 index 00000000..fb6611d8 --- /dev/null +++ b/types/range.pp @@ -0,0 +1 @@ +type Dhcp::Range= Pattern[/^(dynamic-bootp )?((([0-9](?!\d)|[1-9][0-9](?!\d)|1[0-9]{2}(?!\d)|2[0-4][0-9](?!\d)|25[0-5](?!\d))[.]){3}([0-9](?!\d)|[1-9][0-9](?!\d)|1[0-9]{2}(?!\d)|2[0-4][0-9](?!\d)|25[0-5](?!\d)))(\/((([0-9](?!\d)|[1-9][0-9](?!\d)|1[0-9]{2}(?!\d)|2[0-4][0-9](?!\d)|25[0-5](?!\d))[.]){3}([0-9](?!\d)|[1-9][0-9](?!\d)|1[0-9]{2}(?!\d)|2[0-4][0-9](?!\d)|25[0-5](?!\d))|[0-9]+))?( ((([0-9](?!\d)|[1-9][0-9](?!\d)|1[0-9]{2}(?!\d)|2[0-4][0-9](?!\d)|25[0-5](?!\d))[.]){3}([0-9](?!\d)|[1-9][0-9](?!\d)|1[0-9]{2}(?!\d)|2[0-4][0-9](?!\d)|25[0-5](?!\d)))(\/((([0-9](?!\d)|[1-9][0-9](?!\d)|1[0-9]{2}(?!\d)|2[0-4][0-9](?!\d)|25[0-5](?!\d))[.]){3}([0-9](?!\d)|[1-9][0-9](?!\d)|1[0-9]{2}(?!\d)|2[0-4][0-9](?!\d)|25[0-5](?!\d))|[0-9]+))?)?$/]