[30903] | 1 | #! /usr/bin/env ruby -S rspec
|
---|
| 2 | require 'spec_helper'
|
---|
| 3 | require 'tempfile'
|
---|
| 4 | describe Puppet::Type.type(:file_line) do
|
---|
| 5 | let :file_line do
|
---|
| 6 | Puppet::Type.type(:file_line).new(:name => 'foo', :line => 'line', :path => '/tmp/path')
|
---|
| 7 | end
|
---|
| 8 | it 'should accept a line and path' do
|
---|
| 9 | file_line[:line] = 'my_line'
|
---|
| 10 | expect(file_line[:line]).to eq('my_line')
|
---|
| 11 | file_line[:path] = '/my/path'
|
---|
| 12 | expect(file_line[:path]).to eq('/my/path')
|
---|
| 13 | end
|
---|
| 14 | it 'should accept a match regex' do
|
---|
| 15 | file_line[:match] = '^foo.*$'
|
---|
| 16 | expect(file_line[:match]).to eq('^foo.*$')
|
---|
| 17 | end
|
---|
| 18 | it 'should accept a match regex that does not match the specified line' do
|
---|
| 19 | expect {
|
---|
| 20 | Puppet::Type.type(:file_line).new(
|
---|
| 21 | :name => 'foo',
|
---|
| 22 | :path => '/my/path',
|
---|
| 23 | :line => 'foo=bar',
|
---|
| 24 | :match => '^bar=blah$'
|
---|
| 25 | )}.not_to raise_error
|
---|
| 26 | end
|
---|
| 27 | it 'should accept a match regex that does match the specified line' do
|
---|
| 28 | expect {
|
---|
| 29 | Puppet::Type.type(:file_line).new(
|
---|
| 30 | :name => 'foo',
|
---|
| 31 | :path => '/my/path',
|
---|
| 32 | :line => 'foo=bar',
|
---|
| 33 | :match => '^\s*foo=.*$'
|
---|
| 34 | )}.not_to raise_error
|
---|
| 35 | end
|
---|
| 36 | it 'should accept posix filenames' do
|
---|
| 37 | file_line[:path] = '/tmp/path'
|
---|
| 38 | expect(file_line[:path]).to eq('/tmp/path')
|
---|
| 39 | end
|
---|
| 40 | it 'should not accept unqualified path' do
|
---|
| 41 | expect { file_line[:path] = 'file' }.to raise_error(Puppet::Error, /File paths must be fully qualified/)
|
---|
| 42 | end
|
---|
| 43 | it 'should require that a line is specified' do
|
---|
| 44 | expect { Puppet::Type.type(:file_line).new(:name => 'foo', :path => '/tmp/file') }.to raise_error(Puppet::Error, /line is a required attribute/)
|
---|
| 45 | end
|
---|
| 46 | it 'should not require that a line is specified when matching for absence' do
|
---|
| 47 | expect { Puppet::Type.type(:file_line).new(:name => 'foo', :path => '/tmp/file', :ensure => :absent, :match_for_absence => :true, :match => 'match') }.not_to raise_error
|
---|
| 48 | end
|
---|
| 49 | it 'should require that a file is specified' do
|
---|
| 50 | expect { Puppet::Type.type(:file_line).new(:name => 'foo', :line => 'path') }.to raise_error(Puppet::Error, /path is a required attribute/)
|
---|
| 51 | end
|
---|
| 52 | it 'should default to ensure => present' do
|
---|
| 53 | expect(file_line[:ensure]).to eq :present
|
---|
| 54 | end
|
---|
| 55 | it 'should default to replace => true' do
|
---|
| 56 | expect(file_line[:replace]).to eq :true
|
---|
| 57 | end
|
---|
| 58 |
|
---|
| 59 | it "should autorequire the file it manages" do
|
---|
| 60 | catalog = Puppet::Resource::Catalog.new
|
---|
| 61 | file = Puppet::Type.type(:file).new(:name => "/tmp/path")
|
---|
| 62 | catalog.add_resource file
|
---|
| 63 | catalog.add_resource file_line
|
---|
| 64 |
|
---|
| 65 | relationship = file_line.autorequire.find do |rel|
|
---|
| 66 | (rel.source.to_s == "File[/tmp/path]") and (rel.target.to_s == file_line.to_s)
|
---|
| 67 | end
|
---|
| 68 | expect(relationship).to be_a Puppet::Relationship
|
---|
| 69 | end
|
---|
| 70 |
|
---|
| 71 | it "should not autorequire the file it manages if it is not managed" do
|
---|
| 72 | catalog = Puppet::Resource::Catalog.new
|
---|
| 73 | catalog.add_resource file_line
|
---|
| 74 | expect(file_line.autorequire).to be_empty
|
---|
| 75 | end
|
---|
| 76 | end
|
---|