From 4936cc3ed58840893ee27ce65a451fc0aa162d65 Mon Sep 17 00:00:00 2001 From: John Bond Date: Fri, 6 Oct 2017 17:31:45 +0100 Subject: [PATCH] remove legacy functions --- manifests/backports.pp | 4 ++-- manifests/init.pp | 9 +++++---- manifests/key.pp | 11 ++++++++--- manifests/pin.pp | 2 +- manifests/setting.pp | 10 +++++++--- manifests/source.pp | 33 ++++++++++++++++----------------- spec/defines/key_compat_spec.rb | 24 ++++++++++++------------ spec/defines/key_spec.rb | 24 ++++++++++++------------ spec/defines/source_spec.rb | 2 +- 9 files changed, 64 insertions(+), 55 deletions(-) diff --git a/manifests/backports.pp b/manifests/backports.pp index 4faad61..afd4ee1 100644 --- a/manifests/backports.pp +++ b/manifests/backports.pp @@ -36,9 +36,9 @@ class apt::backports ( } } - if is_hash($pin) { + if $pin =~ Hash { $_pin = $pin - } elsif is_numeric($pin) or is_string($pin) { + } elsif $pin =~ Numeric or $pin =~ String { # apt::source defaults to pinning to origin, but we should pin to release # for backports $_pin = { diff --git a/manifests/init.pp b/manifests/init.pp index 44e66ce..319e064 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -35,10 +35,11 @@ class apt ( fail('This module only works on Debian or derivatives like Ubuntu') } - $frequency_options = ['always','daily','weekly','reluctantly'] - if $update['frequency'] { - validate_re($update['frequency'], $frequency_options) + assert_type( + Enum['always','daily','weekly','reluctantly'], + $update['frequency'], + ) } if $update['timeout'] { assert_type(Integer, $update['timeout']) @@ -66,7 +67,7 @@ class apt ( $_purge = merge($::apt::purge_defaults, $purge) if $proxy['ensure'] { - validate_re($proxy['ensure'], ['file', 'present', 'absent']) + assert_type(Enum['file', 'present', 'absent'], $proxy['ensure']) } if $proxy['host'] { assert_type(String, $proxy['host']) diff --git a/manifests/key.pp b/manifests/key.pp index 8f9d66b..0dad80b 100644 --- a/manifests/key.pp +++ b/manifests/key.pp @@ -8,14 +8,19 @@ define apt::key ( Optional[String] $options = undef, ) { - validate_re($id, ['\A(0x)?[0-9a-fA-F]{8}\Z', '\A(0x)?[0-9a-fA-F]{16}\Z', '\A(0x)?[0-9a-fA-F]{40}\Z']) + assert_type( + Pattern[ + /\A(0x)?[0-9a-fA-F]{8}\Z/, + /\A(0x)?[0-9a-fA-F]{16}\Z/, + /\A(0x)?[0-9a-fA-F]{40}\Z/, + ], $id) if $source { - validate_re($source, ['\Ahttps?:\/\/', '\Aftp:\/\/', '\A\/\w+']) + assert_type(Pattern[/\Ahttps?:\/\//, /\Aftp:\/\//, /\A\/\w+/], $source) } if $server { - validate_re($server,['\A((hkp|http|https):\/\/)?([a-z\d])([a-z\d-]{0,61}\.)+[a-z\d]+(:\d{2,5})?$']) + assert_type(Pattern[/\A((hkp|http|https):\/\/)?([a-z\d])([a-z\d-]{0,61}\.)+[a-z\d]+(:\d{2,5})?$/], $server) } case $ensure { diff --git a/manifests/pin.pp b/manifests/pin.pp index ee32de4..831d75f 100644 --- a/manifests/pin.pp +++ b/manifests/pin.pp @@ -40,7 +40,7 @@ define apt::pin( # Read the manpage 'apt_preferences(5)', especially the chapter # 'The Effect of APT Preferences' to understand the following logic # and the difference between specific and general form - if is_array($packages) { + if $packages =~ Array { $packages_string = join($packages, ' ') } else { $packages_string = $packages diff --git a/manifests/setting.pp b/manifests/setting.pp index 507ec94..91835cb 100644 --- a/manifests/setting.pp +++ b/manifests/setting.pp @@ -18,11 +18,15 @@ define apt::setting ( $setting_type = $title_array[0] $base_name = join(delete_at($title_array, 0), '-') - validate_re($setting_type, ['\Aconf\z', '\Apref\z', '\Alist\z'], "apt::setting resource name/title must start with either 'conf-', 'pref-' or 'list-'") + assert_type(Pattern[/\Aconf\z/, /\Apref\z/, /\Alist\z/], $setting_type) |$a, $b| { + fail("apt::setting resource name/title must start with either 'conf-', 'pref-' or 'list-'") + } - unless is_integer($priority) { + if $priority !~ Integer { # need this to allow zero-padded priority. - validate_re($priority, '^\d+$', 'apt::setting priority must be an integer or a zero-padded integer') + assert_type(Pattern[/^\d+$/], $priority) |$a, $b| { + fail('apt::setting priority must be an integer or a zero-padded integer') + } } if ($setting_type == 'list') or ($setting_type == 'pref') { diff --git a/manifests/source.pp b/manifests/source.pp index be743eb..a95bc59 100644 --- a/manifests/source.pp +++ b/manifests/source.pp @@ -1,17 +1,17 @@ # source.pp # add an apt source define apt::source( - Optional[String] $location = undef, - String $comment = $name, - String $ensure = present, - Optional[String] $release = undef, - String $repos = 'main', - Optional[Variant[Hash]] $include = {}, - Optional[Variant[String, Hash]] $key = undef, - $pin = undef, - Optional[String] $architecture = undef, - Boolean $allow_unsigned = false, - Boolean $notify_update = true, + Optional[String] $location = undef, + String $comment = $name, + String $ensure = present, + Optional[String] $release = undef, + String $repos = 'main', + Optional[Variant[Hash]] $include = {}, + Optional[Variant[String, Hash]] $key = undef, + Optional[Variant[Hash, Numeric, String]] $pin = undef, + Optional[String] $architecture = undef, + Boolean $allow_unsigned = false, + Boolean $notify_update = true, ) { # This is needed for compat with 1.8.x @@ -36,14 +36,13 @@ define apt::source( $includes = merge($::apt::include_defaults, $include) if $key { - if is_hash($key) { + if $key =~ Hash { unless $key['id'] { fail('key hash must contain at least an id entry') } $_key = merge($::apt::source_key_defaults, $key) } else { - validate_legacy(String, 'validate_string', $key) - $_key = { 'id' => $key } + $_key = { 'id' => assert_type(String[1], $key) } } } @@ -66,9 +65,9 @@ define apt::source( } if $pin { - if is_hash($pin) { + if $pin =~ Hash { $_pin = merge($pin, { 'ensure' => $ensure, 'before' => $_before }) - } elsif (is_numeric($pin) or is_string($pin)) { + } elsif ($pin =~ Numeric or $pin =~ String) { $url_split = split($location, '[:\/]+') $host = $url_split[1] $_pin = { @@ -85,7 +84,7 @@ define apt::source( # We do not want to remove keys when the source is absent. if $key and ($ensure == 'present') { - if is_hash($_key) { + if $_key =~ Hash { apt::key { "Add key: ${$_key['id']} from Apt::Source ${title}": ensure => present, id => $_key['id'], diff --git a/spec/defines/key_compat_spec.rb b/spec/defines/key_compat_spec.rb index 2d02bed..6134768 100644 --- a/spec/defines/key_compat_spec.rb +++ b/spec/defines/key_compat_spec.rb @@ -146,7 +146,7 @@ describe 'apt::key', type: :define do end it 'fails' do - expect { subject.call } .to raise_error(%r{does not match}) + expect { subject.call } .to raise_error(%r{expects a match}) end end @@ -158,7 +158,7 @@ describe 'apt::key', type: :define do end it 'fails' do - expect { subject.call } .to raise_error(%r{does not match}) + expect { subject.call } .to raise_error(%r{expects a match}) end end @@ -170,7 +170,7 @@ describe 'apt::key', type: :define do end it 'fails' do - expect { subject.call } .to raise_error(%r{does not match}) + expect { subject.call } .to raise_error(%r{expects a match}) end end context 'exceed character url' do @@ -181,7 +181,7 @@ describe 'apt::key', type: :define do end it 'fails' do - expect { subject.call }.to raise_error(%r{does not match}) + expect { subject.call }.to raise_error(%r{expects a match}) end end context 'incorrect port number url' do @@ -192,7 +192,7 @@ describe 'apt::key', type: :define do end it 'fails' do - expect { subject.call }.to raise_error(%r{does not match}) + expect { subject.call }.to raise_error(%r{expects a match}) end end context 'incorrect protocol for url' do @@ -203,7 +203,7 @@ describe 'apt::key', type: :define do end it 'fails' do - expect { subject.call }.to raise_error(%r{does not match}) + expect { subject.call }.to raise_error(%r{expects a match}) end end context 'missing port number url' do @@ -214,7 +214,7 @@ describe 'apt::key', type: :define do end it 'fails' do - expect { subject.call }.to raise_error(%r{does not match}) + expect { subject.call }.to raise_error(%r{expects a match}) end end context 'url ending with a dot' do @@ -225,7 +225,7 @@ describe 'apt::key', type: :define do end it 'fails' do - expect { subject.call }.to raise_error(%r{does not match}) + expect { subject.call }.to raise_error(%r{expects a match}) end end context 'url begin with a dash' do @@ -236,7 +236,7 @@ describe 'apt::key', type: :define do end it 'fails' do - expect { subject.call }.to raise_error(%r{does not match}) + expect { subject.call }.to raise_error(%r{expects a match}) end end context 'invalid key' do @@ -245,7 +245,7 @@ describe 'apt::key', type: :define do end it 'fails' do - expect { subject.call }.to raise_error(%r{does not match}) + expect { subject.call }.to raise_error(%r{expects a match}) end end @@ -257,7 +257,7 @@ describe 'apt::key', type: :define do end it 'fails' do - expect { subject.call }.to raise_error(%r{does not match}) + expect { subject.call }.to raise_error(%r{expects a match}) end end @@ -281,7 +281,7 @@ describe 'apt::key', type: :define do end it 'fails' do - expect { subject.call }.to raise_error(%r{does not match}) + expect { subject.call }.to raise_error(%r{expects a match}) end end diff --git a/spec/defines/key_spec.rb b/spec/defines/key_spec.rb index ce433a3..e9ec0c5 100644 --- a/spec/defines/key_spec.rb +++ b/spec/defines/key_spec.rb @@ -151,7 +151,7 @@ describe 'apt::key' do end it 'fails' do - expect { subject.call } .to raise_error(%r{does not match}) + expect { subject.call } .to raise_error(%r{expects a match}) end end @@ -163,7 +163,7 @@ describe 'apt::key' do end it 'fails' do - expect { subject.call } .to raise_error(%r{does not match}) + expect { subject.call } .to raise_error(%r{expects a match}) end end @@ -175,7 +175,7 @@ describe 'apt::key' do end it 'fails' do - expect { subject.call } .to raise_error(%r{does not match}) + expect { subject.call } .to raise_error(%r{expects a match}) end end context 'exceed character url' do @@ -186,7 +186,7 @@ describe 'apt::key' do end it 'fails' do - expect { subject.call }.to raise_error(%r{does not match}) + expect { subject.call }.to raise_error(%r{expects a match}) end end context 'incorrect port number url' do @@ -197,7 +197,7 @@ describe 'apt::key' do end it 'fails' do - expect { subject.call }.to raise_error(%r{does not match}) + expect { subject.call }.to raise_error(%r{expects a match}) end end context 'incorrect protocol for url' do @@ -208,7 +208,7 @@ describe 'apt::key' do end it 'fails' do - expect { subject.call }.to raise_error(%r{does not match}) + expect { subject.call }.to raise_error(%r{expects a match}) end end context 'missing port number url' do @@ -219,7 +219,7 @@ describe 'apt::key' do end it 'fails' do - expect { subject.call }.to raise_error(%r{does not match}) + expect { subject.call }.to raise_error(%r{expects a match}) end end context 'url ending with a dot' do @@ -230,7 +230,7 @@ describe 'apt::key' do end it 'fails' do - expect { subject.call }.to raise_error(%r{does not match}) + expect { subject.call }.to raise_error(%r{expects a match}) end end context 'url begin with a dash' do @@ -241,7 +241,7 @@ describe 'apt::key' do end it 'fails' do - expect { subject.call }.to raise_error(%r{does not match}) + expect { subject.call }.to raise_error(%r{expects a match}) end end context 'invalid key' do @@ -250,7 +250,7 @@ describe 'apt::key' do end it 'fails' do - expect { subject.call }.to raise_error(%r{does not match}) + expect { subject.call }.to raise_error(%r{expects a match}) end end @@ -262,7 +262,7 @@ describe 'apt::key' do end it 'fails' do - expect { subject.call }.to raise_error(%r{does not match}) + expect { subject.call }.to raise_error(%r{expects a match}) end end @@ -286,7 +286,7 @@ describe 'apt::key' do end it 'fails' do - expect { subject.call }.to raise_error(%r{does not match}) + expect { subject.call }.to raise_error(%r{expects a match}) end end diff --git a/spec/defines/source_spec.rb b/spec/defines/source_spec.rb index eaa0887..52a7ca9 100644 --- a/spec/defines/source_spec.rb +++ b/spec/defines/source_spec.rb @@ -389,7 +389,7 @@ describe 'apt::source' do it do expect { subject.call - }.to raise_error(Puppet::Error, %r{invalid value for pin}) + }.to raise_error(Puppet::Error, %r{expects a value}) end end -- 2.45.2