Skip to content

Commit

Permalink
Wrap Windows non-repo installs in an installed state check
Browse files Browse the repository at this point in the history
  • Loading branch information
hartmantis committed Aug 18, 2016
1 parent e7b796f commit d478f9c
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 18 deletions.
56 changes: 40 additions & 16 deletions libraries/resource_chef_dk_app_windows.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,37 @@ class ChefDkAppWindows < ChefDkApp
action :install do
case new_resource.source
when :direct
ver = if new_resource.version == 'latest'
package_metadata[:version]
else
new_resource.version
end
package "Chef Development Kit v#{ver}" do
source package_metadata[:url]
checksum package_metadata[:sha256]
new_resource.installed(true)

converge_if_changed :installed do
ver = if new_resource.version == 'latest'
package_metadata[:version]
else
new_resource.version
end
package "Chef Development Kit v#{ver}" do
source package_metadata[:url]
checksum package_metadata[:sha256]
end
end
when :repo
include_recipe 'chocolatey'
chocolatey_package 'chefdk' do
version new_resource.version unless new_resource.version == 'latest'
end
else
ver = if new_resource.version == 'latest'
package_metadata[:version]
else
new_resource.version
end
package "Chef Development Kit v#{ver}" do
source new_resource.source.to_s
checksum new_resource.checksum unless new_resource.checksum.nil?
new_resource.installed(true)

converge_if_changed :installed do
ver = if new_resource.version == 'latest'
package_metadata[:version]
else
new_resource.version
end
package "Chef Development Kit v#{ver}" do
source new_resource.source.to_s
checksum new_resource.checksum unless new_resource.checksum.nil?
end
end
end
end
Expand Down Expand Up @@ -115,6 +123,22 @@ class ChefDkAppWindows < ChefDkApp
end
end
end

#
# Powershell out and pull the version of the Chef-DK out of
# Get-WmiObject.
#
# (see Chef::Provider::ChefDkApp#installed_version)
#
def installed_version
lines = powershell_out!('Get-WmiObject -Class win32_product')
.stdout.lines
idx = lines.index do |l|
l.match(/^\W*Name\W+:\W+Chef Development Kit/)
end
return false if idx.nil?
lines[idx + 2].split(':')[1].strip.split('.')[0..2].join('.')
end
end
end
end
2 changes: 2 additions & 0 deletions spec/resources/chef_dk_app/mac_os_x.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
let(:installed_version) { nil }

before(:each) do
allow_any_instance_of(Chef::Mixin::ShellOut).to receive(:shell_out)
.and_call_original
allow_any_instance_of(Chef::Mixin::ShellOut).to receive(:shell_out)
.with('pkgutil --pkg-info com.getchef.pkg.chefdk')
.and_return(double(exitstatus: installed_version.nil? ? 1 : 0,
Expand Down
64 changes: 62 additions & 2 deletions spec/resources/chef_dk_app/windows.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,36 @@
shared_context 'resources::chef_dk_app::windows' do
include_context 'resources::chef_dk_app'

before(:each) do
stdout = <<-EOH.gsub(/^ +/, '')
IdentifyingNumber : {abc123}
Name : Test App 3.1.4
Vendor : Thumb Monkey Industries
Version : 3.1.41.1
Caption : Test App 3.1.4
EOH
installed_version && stdout << <<-EOH.gsub(/^ +/, '')
IdentifyingNumber : {456789}
Name : Chef Development Kit #{installed_version}
Vendor : Chef Software, Inc.
Version : #{installed_version}.1
Caption : Chef Development Kit v#{installed_version}
EOH
allow_any_instance_of(Chef::Mixin::PowershellOut)
.to receive(:powershell_out!)
.with('Get-WmiObject -Class win32_product')
.and_return(double(stdout: stdout))
end

shared_examples_for 'any Windows platform' do
context 'the default action (:install)' do
include_context description

context 'the default source (:direct)' do
include_context description

shared_examples_for 'any property set' do
shared_examples_for 'installs Chef-DK' do
it 'installs the correct Chef-DK package' do
pkg = "Chef Development Kit v#{version || '1.2.3'}"
expect(chef_run).to install_package(pkg).with(
Expand All @@ -21,6 +43,13 @@
end
end

shared_examples_for 'does not install Chef-DK' do
it 'does not install the correct Chef-DK package' do
pkg = "Chef Development Kit v#{version || '1.2.3'}"
expect(chef_run).to_not install_package(pkg)
end
end

[
'all default properties',
'an overridden channel property',
Expand All @@ -29,9 +58,21 @@
context c do
include_context description

it_behaves_like 'any property set'
it_behaves_like 'installs Chef-DK'
end
end

context 'the latest version already installed' do
include_context description

it_behaves_like 'does not install Chef-DK'
end

context 'an older version already installed' do
include_context description

it_behaves_like 'does not install Chef-DK'
end
end

context 'the :repo source' do
Expand Down Expand Up @@ -73,6 +114,13 @@
context 'a custom source' do
include_context description

shared_examples_for 'does not install Chef-DK' do
it 'does not install the correct Chef-DK package' do
pkg = "Chef Development Kit v#{version || '1.2.3'}"
expect(chef_run).to_not install_package(pkg)
end
end

context 'all default properties' do
include_context description

Expand Down Expand Up @@ -102,6 +150,18 @@
expect(true).to eq(false)
end
end

context 'the latest version already installed' do
include_context description

it_behaves_like 'does not install Chef-DK'
end

context 'an older version already installed' do
include_context description

it_behaves_like 'does not install Chef-DK'
end
end
end

Expand Down

0 comments on commit d478f9c

Please sign in to comment.