Skip to content

Commit

Permalink
On Win, wait until package is installed
Browse files Browse the repository at this point in the history
  • Loading branch information
hartmantis committed Jan 3, 2015
1 parent 3810c84 commit 705e487
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 22 deletions.
17 changes: 9 additions & 8 deletions libraries/provider_private_internet_access_windows.rb
Expand Up @@ -22,7 +22,7 @@
require 'chef/resource/cookbook_file'
require 'chef/resource/execute'
require 'chef/resource/powershell_script'
require 'chef/resource/ruby'
require 'chef/resource/ruby_block'
require_relative 'provider_private_internet_access'

class Chef
Expand All @@ -45,7 +45,7 @@ def action_install
trust_cert.run_action(:run)
start_installer.run_action(:run)
wait_for_installer.run_action(:run)
new_resource.installed = true
super
end

private
Expand All @@ -59,12 +59,13 @@ def action_install
#
def wait_for_installer
unless @wait_for_installer
@wait_for_installer = Resource::Ruby.new('pia_installer_wait',
run_context)
@wait_for_installer.code(
"sleep 1 while !::File.exist?('#{WATCH_FILE}')"
)
@wait_for_installer.creates(WATCH_FILE)
@wait_for_installer = Resource::RubyBlock.new('pia_installer_wait',
run_context)
@wait_for_installer.block do
p = Chef::Provider::WindowsCookbookPackage.new(package,
run_context)
sleep 1 until p.current_installed_version
end
end
@wait_for_installer
end
Expand Down
48 changes: 34 additions & 14 deletions spec/libraries/provider_private_internet_access_windows_spec.rb
Expand Up @@ -55,28 +55,48 @@
expect(new_resource).to receive(:installed=).with(true)
provider.action_install
end

it 'takes no action on the package resource' do
expect(package).not_to receive(:run_action)
provider.action_install
end
end

describe '#wait_for_installer' do
it 'returns a ruby resource' do
expected = Chef::Resource::Ruby
let(:current_installed_version) { '1.2.3' }
let(:wcpackage) do
double(current_installed_version: current_installed_version)
end
let(:package) { double }

before(:each) do
allow(Chef::Provider::WindowsCookbookPackage).to receive(:new)
.and_return(wcpackage)
allow_any_instance_of(Chef::Resource::RubyBlock).to receive(:block)
.and_yield
allow_any_instance_of(described_class).to receive(:package)
.and_return(package)
end

it 'returns a ruby_block resource' do
expected = Chef::Resource::RubyBlock
expect(provider.send(:wait_for_installer)).to be_an_instance_of(expected)
end

it 'sets the correct code' do
expected = 'sleep 1 while !::File.exist?(\'/Program Files/pia_manager/' \
'pia_manager.exe\')'
expect(provider.send(:wait_for_installer).code).to eq(expected)
context 'uninstalled package' do
let(:current_installed_version) { nil }

it 'sleeps' do
expect(wcpackage).to receive(:current_installed_version).twice
.and_return(current_installed_version, '1.2.3')
expect_any_instance_of(described_class).to receive(:sleep).with(1)
.once.and_return(nil)
provider.send(:wait_for_installer)
end
end

it 'sets the correct watch file' do
expected = '/Program Files/pia_manager/pia_manager.exe'
expect(provider.send(:wait_for_installer).creates).to eq(expected)
context 'installed package' do
let(:current_installed_version) { '1.2.3' }

it 'does not sleep' do
expect_any_instance_of(described_class).not_to receive(:sleep)
provider.send(:wait_for_installer)
end
end
end

Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Expand Up @@ -12,6 +12,7 @@
require_relative 'support/matchers/private_internet_access'
require_relative 'support/resource/dmg_package'
require_relative 'support/provider/dmg_package'
require_relative 'support/provider/windows_cookbook_package'

RSpec.configure do |c|
c.color = true
Expand Down
13 changes: 13 additions & 0 deletions spec/support/provider/windows_cookbook_package.rb
@@ -0,0 +1,13 @@
# Encoding: UTF-8

require 'spec_helper'

class Chef
class Provider
# A fake windows_cookbook_package provider
#
# @author Jonathan Hartman <j@p4nt5.com>
class WindowsCookbookPackage < Provider
end
end
end

0 comments on commit 705e487

Please sign in to comment.