1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
require 'spec_helper'
require 'puppet_spec/compiler'
require 'matchers/resource'
require 'puppet_spec/files'
describe 'the find_file function' do
include PuppetSpec::Compiler
include Matchers::Resource
include PuppetSpec::Files
def with_file_content(content)
path = tmpfile('find-file-function')
file = File.new(path, 'wb')
file.sync = true
file.print content
yield path
end
it 'finds an existing absolute file when given arguments individually' do
with_file_content('one') do |one|
with_file_content('two') do |two|
expect(compile_to_catalog("notify { find_file('#{one}', '#{two}'):}")).to have_resource("Notify[#{one}]")
end
end
end
it 'skips non existing files' do
with_file_content('one') do |one|
with_file_content('two') do |two|
expect(compile_to_catalog("notify { find_file('#{one}/nope', '#{two}'):}")).to have_resource("Notify[#{two}]")
end
end
end
it 'accepts arguments given as an array' do
with_file_content('one') do |one|
with_file_content('two') do |two|
expect(compile_to_catalog("notify { find_file(['#{one}', '#{two}']):}")).to have_resource("Notify[#{one}]")
end
end
end
it 'finds an existing file in a module' do
with_file_content('file content') do |name|
mod = double('module')
allow(mod).to receive(:file).with('myfile').and_return(name)
Puppet[:code] = "notify { find_file('mymod/myfile'):}"
node = Puppet::Node.new('localhost')
compiler = Puppet::Parser::Compiler.new(node)
allow(compiler.environment).to receive(:module).with('mymod').and_return(mod)
expect(compiler.compile().filter { |r| r.virtual? }).to have_resource("Notify[#{name}]")
end
end
it 'returns undef when none of the paths were found' do
mod = double('module')
allow(mod).to receive(:file).with('myfile').and_return(nil)
Puppet[:code] = "notify { String(type(find_file('mymod/myfile', 'nomod/nofile'))):}"
node = Puppet::Node.new('localhost')
compiler = Puppet::Parser::Compiler.new(node)
# For a module that does not have the file
allow(compiler.environment).to receive(:module).with('mymod').and_return(mod)
# For a module that does not exist
allow(compiler.environment).to receive(:module).with('nomod').and_return(nil)
expect(compiler.compile().filter { |r| r.virtual? }).to have_resource("Notify[Undef]")
end
end
|