-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
PuppetLint.new_check(:source_without_rights) do | ||
def token_attr(resource, name) | ||
resource[:tokens].select do |t| | ||
t.type == :NAME && t.value == name && \ | ||
t.next_code_token && t.next_code_token.type == :FARROW | ||
end | ||
end | ||
|
||
def check_attr(resource, name, source_t) | ||
if token_attr(resource, name).empty? | ||
notify :warning, { | ||
:message => "file with source missing #{name}", | ||
:line => source_t.line, | ||
:column => source_t.column, | ||
:token => source_t, | ||
} | ||
end | ||
end | ||
|
||
def check | ||
resource_indexes.each do |r| | ||
next unless r[:type].value == 'file' | ||
source_t = token_attr(r, 'source') | ||
next if source_t.empty? | ||
|
||
check_attr(r, 'owner', source_t[0]) | ||
check_attr(r, 'group', source_t[0]) | ||
check_attr(r, 'mode', source_t[0]) | ||
end | ||
end | ||
end |
47 changes: 47 additions & 0 deletions
47
spec/puppet-lint/plugins/check_file_source_rights/source_without_rights_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
require 'spec_helper' | ||
|
||
describe 'source_without_rights' do | ||
context 'when rights are passed' do | ||
let(:code) do | ||
<<-EOS | ||
file { '/tmp/foo': | ||
ensure => file, | ||
owner => 'root', | ||
group => '0', | ||
mode => '0644', | ||
source => 'puppet:///modules/bar/foo', | ||
} | ||
file { '/tmp/bar': | ||
ensure => file, | ||
content => 'qux', | ||
} | ||
EOS | ||
end | ||
|
||
it 'should not detect any problems' do | ||
expect(problems).to have(0).problems | ||
end | ||
end | ||
|
||
context 'when rights are not passed' do | ||
let(:code) do | ||
<<-EOS | ||
file { '/tmp/foo': | ||
ensure => file, | ||
source => 'puppet:///modules/bar/foo', | ||
} | ||
EOS | ||
end | ||
|
||
it 'should detect 3 problems' do | ||
expect(problems).to have(3).problems | ||
end | ||
|
||
it 'should create warnings' do | ||
expect(problems).to contain_warning('file with source missing owner').on_line(3).in_column(11) | ||
expect(problems).to contain_warning('file with source missing group').on_line(3).in_column(11) | ||
expect(problems).to contain_warning('file with source missing mode').on_line(3).in_column(11) | ||
end | ||
end | ||
end |