Reputation: 9
I've google around, it appears this can be done, but I'm just doing it wrong apparently.
So I'm trying to run this snippet of powershell_script
:
powershell_script 'Unzip' do
code <<-EOH
Expand-Archive -Path 'E:\\apache-tomee-1.7.4-plus.zip' -DestinationPath "E:\\#{node['COOKBOOK']['Product']}-#{node['COOKBOOK']['Region']}-" + count.to_s.rjust(2, "0")"
EOH
guard_interpreter :powershell_script
not_if "Test-Path -Path E:\\#{node['COOKBOOK']['Product']}-#{node['COOKBOOK']['Region']}-01"
end
Now those node attributes are set in attributes default.rb
default['COOKBOOK']['Product'] = 'product'
default['COOKBOOK']['Region'] = 'region'
I'm having a problem getting from there to what's supposed to be in the spec file.
require 'spec_helper'
describe 'COOKBOOK::default' do
context 'when all attributes are default, on Windows 2012R2' do
let(:chef_run) do
# for a complete list of available platforms and versions see:
# https://github.com/customink/fauxhai/blob/master/PLATFORMS.md
runner = ChefSpec::ServerRunner.new(platform: 'windows', version: '2012R2')
runner.converge(described_recipe)
end
it 'converges successfully' do
stub_command('Test-Path -Path E:\\#{node['COOKBOOK']['Product']}-#{node['COOKBOOK']['Region']}-01').and_return(true)
expect { chef_run }.to_not raise_error
end
end
end
Can anyone give me a hand?
Thanks in advance.
Upvotes: 1
Views: 717
Reputation: 5065
First, you have an issue with the stub_command
line. You have the line surrounded with single quotes so those interpolations for attributes aren't going to work, so surround the thing in double quotes:
stub_command("Test-Path -Path E:\\#{node['COOKBOOK']['Product']}-#{node['COOKBOOK']['Region']}-01").and_return(true)
Now you will get this error:
NameError:
undefined local variable or method `node' for #<RSpec::ExampleGroups::ExampleSoDefault::WhenAllAttributesAreDefaultOnWindows2012R2:0x0000000004066720>
Your node attributes aren't available in your rspec tests. In additon, rspec
recognizes your not_if
with attributes already replaced, like this:
not_if "Test-Path -Path E:\\product-region-01"
So you need to stub that command, like follows:
require 'spec_helper'
describe 'example-so::default' do
context 'when all attributes are default, on Windows 2012R2' do
let(:chef_run) do
# for a complete list of available platforms and versions see:
# https://github.com/customink/fauxhai/blob/master/PLATFORMS.md
runner = ChefSpec::ServerRunner.new(platform: 'windows', version: '2012R2')
runner.converge(described_recipe)
end
it 'converges successfully' do
stub_command('Test-Path -Path E:\\product-region-01').and_return(true)
expect { chef_run }.to_not raise_error
end
end
end
If you need to test for multiple different attribute settings you can set attributes appropriately and run tests for each set of conditions.
Upvotes: 0