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