From 9a968bbead99d03c826dd5fb47e206532a705137 Mon Sep 17 00:00:00 2001 From: Andreas Teuchert Date: Sat, 23 Feb 2013 16:42:14 +0100 Subject: [PATCH 1/4] Handle release parameter more sensibly in force manifest. Use release parameter to construct $release_string. The release parameter may also be set to false to use the system's default release (so just force a specific version). Use false as the default setting instead of 'testing'. Change $install_check to also check if package is installed from the right release, instead of just checking the version. --- manifests/force.pp | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/manifests/force.pp b/manifests/force.pp index 2d33e94..a1ec4a6 100644 --- a/manifests/force.pp +++ b/manifests/force.pp @@ -2,7 +2,7 @@ # force a package from a specific release define apt::force( - $release = 'testing', + $release = false, $version = false, $timeout = 300 ) { @@ -12,11 +12,27 @@ define apt::force( default => "=${version}", } - $install_check = $version ? { - false => "/usr/bin/dpkg -s ${name} | grep -q 'Status: install'", - default => "/usr/bin/dpkg -s ${name} | grep -q 'Version: ${version}'", + $release_string = $release ? { + false => undef, + default => "-t ${release}", } - exec { "/usr/bin/apt-get -y -t ${release} install ${name}${version_string}": + + if $version == false { + if $release == false { + $install_check = "/usr/bin/dpkg -s ${name} | grep -q 'Status: install'" + } else { + # If installed version and candidate version differ, this check returns 1 (false). + $install_check = "/usr/bin/test \$(/usr/bin/apt-cache policy -t ${release} ${name} | /bin/grep -E 'Installed|Candidate' | /usr/bin/uniq -s 14 | /usr/bin/wc -l) -eq 1" + } + } else { + if $release == false { + $install_check = "/usr/bin/dpkg -s ${name} | grep -q 'Version: ${version}'" + } else { + $install_check = "/usr/bin/apt-cache policy -t ${release} ${name} | /bin/grep -q 'Installed: ${version}'" + } + } + + exec { "/usr/bin/apt-get -y ${release_string} install ${name}${version_string}": unless => $install_check, logoutput => 'on_failure', timeout => $timeout, From 5f618da4bd8b2a97a28f299ad41f45237ed7240a Mon Sep 17 00:00:00 2001 From: Andreas Teuchert Date: Sat, 23 Feb 2013 16:45:31 +0100 Subject: [PATCH 2/4] Don't use hardcoded provider in force manifest. Use $apt::params::provider instead. --- manifests/force.pp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/manifests/force.pp b/manifests/force.pp index a1ec4a6..152bb67 100644 --- a/manifests/force.pp +++ b/manifests/force.pp @@ -7,6 +7,8 @@ define apt::force( $timeout = 300 ) { + $provider = $apt::params::provider + $version_string = $version ? { false => undef, default => "=${version}", @@ -32,7 +34,7 @@ define apt::force( } } - exec { "/usr/bin/apt-get -y ${release_string} install ${name}${version_string}": + exec { "${provider} -y ${release_string} install ${name}${version_string}": unless => $install_check, logoutput => 'on_failure', timeout => $timeout, From 711420739de87a8e8f31458db3ea5cd9e9236fe8 Mon Sep 17 00:00:00 2001 From: Hunter Haugen Date: Wed, 3 Jul 2013 11:00:12 -0700 Subject: [PATCH 3/4] Update specs to test all parameter cases --- spec/defines/force_spec.rb | 68 +++++++++++++++++++++++--------------- 1 file changed, 42 insertions(+), 26 deletions(-) diff --git a/spec/defines/force_spec.rb b/spec/defines/force_spec.rb index f5d6ac2..84231fa 100644 --- a/spec/defines/force_spec.rb +++ b/spec/defines/force_spec.rb @@ -1,41 +1,57 @@ require 'spec_helper' describe 'apt::force', :type => :define do + let :pre_condition do + 'include apt::params' + end + let :title do 'my_package' end let :default_params do { - :release => 'testing', + :release => false, :version => false } end - [{}, - { - :release => 'stable', - :version => '1' - } - ].each do |param_set| - describe "when #{param_set == {} ? "using default" : "specifying"} define parameters" do - let :param_hash do - default_params.merge(param_set) - end - - let :params do - param_set - end - - let :unless_query do - base_command = "/usr/bin/dpkg -s #{title} | grep -q " - base_command + (params[:version] ? "'Version: #{params[:version]}'" : "'Status: install'") - end - - let :exec_title do - base_exec = "/usr/bin/apt-get -y -t #{param_hash[:release]} install #{title}" - base_exec + (params[:version] ? "=#{params[:version]}" : "") - end - it { should contain_exec(exec_title).with_unless(unless_query) } + describe "when using default parameters" do + let :params do + default_params end + it { should contain_exec("/usr/bin/apt-get -y install #{title}").with( + :unless => "/usr/bin/dpkg -s #{title} | grep -q 'Status: install'", + :timeout => '300' + ) } + end + + describe "when specifying release parameter" do + let :params do + default_params.merge(:release => 'testing') + end + it { should contain_exec("/usr/bin/apt-get -y -t #{params[:release]} install #{title}").with( + :unless => "/usr/bin/test \$(/usr/bin/apt-cache policy -t #{params[:release]} #{title} | /bin/grep -E 'Installed|Candidate' | /usr/bin/uniq -s 14 | /usr/bin/wc -l) -eq 1" + ) } + end + + describe "when specifying version parameter" do + let :params do + default_params.merge(:version => '1') + end + it { should contain_exec("/usr/bin/apt-get -y install #{title}=#{params[:version]}").with( + :unless => "/usr/bin/dpkg -s #{title} | grep -q 'Version: #{params[:version]}'" + ) } + end + + describe "when specifying release and version parameters" do + let :params do + default_params.merge( + :release => 'testing', + :version => '1' + ) + end + it { should contain_exec("/usr/bin/apt-get -y -t #{params[:release]} install #{title}=1").with( + :unless => "/usr/bin/apt-cache policy -t #{params[:release]} #{title} | /bin/grep -q 'Installed: #{params[:version]}'" + ) } end end From e0bf6e082626ed906e3bc72e412b58005ec0efe9 Mon Sep 17 00:00:00 2001 From: Hunter Haugen Date: Wed, 3 Jul 2013 11:00:33 -0700 Subject: [PATCH 4/4] Update example apt::force tests --- tests/force.pp | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/tests/force.pp b/tests/force.pp index f331363..59ad8f1 100644 --- a/tests/force.pp +++ b/tests/force.pp @@ -1,7 +1,17 @@ # force.pp -# force a package from a specific release -apt::force { 'package': - release => 'testing', - version => false +# force a package from a specific release +apt::force { 'package1': + release => 'backports', +} + +# force a package to be a specific version +apt::force { 'package2': + version => '1.0.0-1', +} + +# force a package from a specific release to be a specific version +apt::force { 'package3': + release => 'sid', + version => '2.0.0-1', }