Skip to content

Commit

Permalink
(PUP-1691) chocolateyversion fact
Browse files Browse the repository at this point in the history
Expose a custom fact about Chocolatey's version. Do not
attempt to do configuration in versions of choco less
than 0.9.9. This does mean a second convergence will be
required to apply the configuration settings.
  • Loading branch information
ferventcoder committed Sep 9, 2015
1 parent 03f8dfa commit 736486f
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 30 deletions.
18 changes: 18 additions & 0 deletions lib/facter/chocolateyversion.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Facter.add('chocolateyversion') do
setcode do
value = nil

choco_path = "#{Facter.value(:choco_install_path)}\\bin\\choco.exe"
if Puppet::Util::Platform.windows? && File.exist?(choco_path)
begin
old_choco_message = 'Please run chocolatey /? or chocolatey help - chocolatey v'
#Facter::Core::Execution.exec is 2.0.1 forward
value = Facter::Util::Resolution.exec("#{choco_path} -v").gsub(old_choco_message,'').strip
rescue StandardError => e
value = '0'
end
end

value || '0'
end
end
30 changes: 17 additions & 13 deletions manifests/config.pp
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@
class chocolatey::config {
assert_private()

#todo: check choco version from custom choco_version fact
#if versioncmp("#{::choco_version}", '0.9.9.0') >= 0 {
#
$_choco_exe_path = "${chocolatey::choco_install_location}\\bin\\choco.exe"
# this will require a second converge when choco is not
# installed the first time through. This is on purpose
# as we don't want to try to set these values for a
# version less than 0.9.9 and we don't know what the
# user may link to - it could be an older version of
# Chocolatey
if versioncmp($::chocolateyversion, '0.9.9.0') >= 0 {
$_choco_exe_path = "${chocolatey::choco_install_location}\\bin\\choco.exe"

$_enable_autouninstaller = $chocolatey::enable_autouninstaller ? {
false => 'disable',
default => 'enable'
}
$_enable_autouninstaller = $chocolatey::enable_autouninstaller ? {
false => 'disable',
default => 'enable'
}

exec { "chocolatey_autouninstaller_${_enable_autouninstaller}":
path => $::path,
command => "${_choco_exe_path} feature -r ${_enable_autouninstaller} -n autoUninstaller",
unless => "cmd.exe /c ${_choco_exe_path} feature list -r | findstr /X /I /C:\"autoUninstaller - [${_enable_autouninstaller}d]\"",
exec { "chocolatey_autouninstaller_${_enable_autouninstaller}":
path => $::path,
command => "${_choco_exe_path} feature -r ${_enable_autouninstaller} -n autoUninstaller",
unless => "cmd.exe /c ${_choco_exe_path} feature list -r | findstr /X /I /C:\"autoUninstaller - [${_enable_autouninstaller}d]\"",

This comment has been minimized.

Copy link
@rismoney

rismoney Sep 28, 2015

Contributor

this line works for you @ferventcoder ?

This comment has been minimized.

Copy link
@ferventcoder

ferventcoder Sep 29, 2015

Author Contributor

Well it did/does for 0.9.9.8. What version are you testing on?

This comment has been minimized.

Copy link
@rismoney

rismoney Sep 29, 2015

Contributor

validated, the code within quotes is solid. yes i am on 0.9.9.8. so you want this command to run with exit 0? i am just trying to clarify, because i see the resource exec'ing every run. wanted to make sure it shouldnt be onlyif

This comment has been minimized.

Copy link
@ferventcoder

ferventcoder Sep 29, 2015

Author Contributor

When I was testing, it took awhile to get that bit right. If you are not seeing it work correctly we should try to determine why. Also, you might take a look at #79 (comment)

}
}
#}
}
79 changes: 64 additions & 15 deletions spec/classes/config_spec.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,76 @@
require 'spec_helper'

RSpec.describe 'chocolatey' do
context 'contains config.pp' do
context 'with older choco installed' do
let(:facts) {
{
:chocolateyversion => '0.9.8.33',
:choco_install_path => 'C:\ProgramData\chocolatey',
}
}

let(:facts) {
{
:choco_version => '0.9.9.8',
:choco_install_path => 'C:\ProgramData\chocolatey',
}
}
[true, false].each do |param_value|
feature_enable = 'enable'
feature_enable = 'disable' if !param_value

context 'contains config.pp with' do
[true, false].each do |param_value|
feature_enable = 'enable'
feature_enable = 'disable' if !param_value
context "enable_autouninstaller => #{param_value}" do
let(:params) {{ :enable_autouninstaller => param_value }}

context "enable_autouninstaller => #{param_value}" do
let(:params) {{ :enable_autouninstaller => param_value }}
it { is_expected.not_to contain_exec("chocolatey_autouninstaller_#{feature_enable}") }

it { is_expected.to contain_exec("chocolatey_autouninstaller_#{feature_enable}") }
it {
is_expected.not_to contain_exec("chocolatey_autouninstaller_#{feature_enable}").with_command("C:\\ProgramData\\chocolatey\\bin\\choco.exe feature -r #{feature_enable} -n autoUninstaller")
}
end
end
end

it {
is_expected.to contain_exec("chocolatey_autouninstaller_#{feature_enable}").with_command("C:\\ProgramData\\chocolatey\\bin\\choco.exe feature -r #{feature_enable} -n autoUninstaller")
context 'without choco installed' do
let(:facts) {
{
:chocolateyversion => '0',
:choco_install_path => 'C:\ProgramData\chocolatey',
}
}

[true, false].each do |param_value|
feature_enable = 'enable'
feature_enable = 'disable' if !param_value

context "enable_autouninstaller => #{param_value}" do
let(:params) {{ :enable_autouninstaller => param_value }}

it { is_expected.not_to contain_exec("chocolatey_autouninstaller_#{feature_enable}") }

it {
is_expected.not_to contain_exec("chocolatey_autouninstaller_#{feature_enable}").with_command("C:\\ProgramData\\chocolatey\\bin\\choco.exe feature -r #{feature_enable} -n autoUninstaller")
}
end
end
end

context 'with choco.exe installed' do
let(:facts) {
{
:chocolateyversion => '0.9.9.8',
:choco_install_path => 'C:\ProgramData\chocolatey',
}
}

[true, false].each do |param_value|
feature_enable = 'enable'
feature_enable = 'disable' if !param_value

context "enable_autouninstaller => #{param_value}" do
let(:params) {{ :enable_autouninstaller => param_value }}

it { is_expected.to contain_exec("chocolatey_autouninstaller_#{feature_enable}") }

it {
is_expected.to contain_exec("chocolatey_autouninstaller_#{feature_enable}").with_command("C:\\ProgramData\\chocolatey\\bin\\choco.exe feature -r #{feature_enable} -n autoUninstaller")
}
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/classes/init_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe 'chocolatey' do
let(:facts) {
{
:choco_version => '0.9.9.8',
:chocolateyversion => '0.9.9.8',
:choco_install_path => 'C:\ProgramData\chocolatey',
}
}
Expand Down
2 changes: 1 addition & 1 deletion spec/classes/install_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

let(:facts) {
{
:choco_version => '0.9.9.8',
:chocolateyversion => '0.9.9.8',
:choco_install_path => 'C:\ProgramData\chocolatey',
}
}
Expand Down
38 changes: 38 additions & 0 deletions spec/unit/facter/chocolateyversion_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'facter'
require 'rspec/its'

describe 'chocolateyversion fact' do
subject(:fact) { Facter.fact(:chocolateyversion) }

context 'on Windows', :if => Puppet::Util::Platform.windows? do
it "should return the value from running choco -v" do
expected_value = '1.2.3'
Facter::Util::Resolution.expects(:exec).returns(expected_value)

subject.value.must == expected_value
end

it "should handle cleaning up spaces" do
expected_value = '1.2.3'
Facter::Util::Resolution.expects(:exec).returns(' ' + expected_value + ' ')

subject.value.must == expected_value
end

it "should handle older versions of choco" do
expected_value = '1.2.3'
Facter::Util::Resolution.expects(:exec).returns('Please run chocolatey /? or chocolatey help - chocolatey v' + expected_value)

subject.value.must == expected_value
end
end

context 'on Linux', :if => Puppet.features.posix? do
its(:value) { should eql('0') }
end

after :each do
Facter.clear
Facter.clear_messages
end
end

0 comments on commit 736486f

Please sign in to comment.