Skip to content
This repository has been archived by the owner on Feb 11, 2019. It is now read-only.

Commit

Permalink
Check for duplicate content and log it
Browse files Browse the repository at this point in the history
  • Loading branch information
phoolish committed Sep 29, 2017
1 parent 8363868 commit dd52b87
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 19 deletions.
38 changes: 26 additions & 12 deletions resources/bashrc_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,28 @@
action_class do
require 'etc'

def group_gid
Etc.getpwnam(new_resource.user).gid
end

def initial_config?
::File.directory?(bashrc_d)
def bashrc
::File.join(::Dir.home(new_resource.user), '.bashrc')
end

def bashrc_d
::File.join(::Dir.home(new_resource.user), '.bashrc.d')
end

def bashrc
::File.join(::Dir.home(new_resource.user), '.bashrc')
def bashrc_init
::File.join(bashrc_d, 'init')
end

def duplicate_content?(filename)
return ::IO.read(filename).downcase.include?(new_resource.content) if ::File.exist? filename
end

def group_gid
Etc.getpwnam(new_resource.user).gid
end

def initial_config?
::File.directory?(bashrc_d)
end
end

Expand All @@ -33,14 +41,14 @@ def bashrc
mode 0o755
end

file ::File.join(bashrc_d, 'init') do
if ::File.exist?(::File.join(bashrc))
content ::IO.read(::File.join(bashrc))
file bashrc_init do
if ::File.exist?(bashrc)
content ::IO.read(bashrc)
else
action :touch
end
mode 0o444
not_if { ::File.exist?(::File.join(bashrc_d, 'init')) }
not_if { ::File.exist?(bashrc_init) }
end

cookbook_file bashrc do
Expand All @@ -60,6 +68,12 @@ def bashrc
group group_gid
mode 0o644
end

log "bashrc_manager_#{new_resource.filename}" do
level :warn
message "bashrc_manager: #{new_resource.filename} adds duplicate content."
only_if { duplicate_content?(bashrc) || duplicate_content?(bashrc_init) }
end
end

action :remove do
Expand Down
40 changes: 33 additions & 7 deletions spec/bashrc_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
ChefSpec::ServerRunner.new(step_into: ['bashrc_manager'])
end

let(:test1_content) do
<<-TXT
# Managed by Chef. Local changes will be overwritten.
test1
TXT
end

before do
pw_user = EtcPwnam.new
pw_user.name = 'test_user'
Expand All @@ -20,12 +27,14 @@
end

context 'action create' do
before do
allow(File).to receive(:exist?).and_call_original
allow(IO).to receive(:read).and_call_original
end

context 'init' do
context 'default' do
before do
allow(File).to receive(:exist?).and_call_original
allow(IO).to receive(:read).and_call_original

allow(File).to receive(:exist?).with('/home/test_user/.bashrc').and_return(true)
allow(File).to receive(:exist?).with('/home/test_user/.bashrc.d/init').and_return(false)
allow(IO).to receive(:read).with('/home/test_user/.bashrc').and_return('')
Expand Down Expand Up @@ -67,10 +76,27 @@

it 'append chef warning to .bashrc.d/test1 file' do
expect(chef_run).to render_file('/home/test_user/.bashrc.d/test1')
.with_content <<-TXT
# Managed by Chef. Local changes will be overwritten.
test1
TXT
.with_content(test1_content)
end

it 'not log warning' do
expect(chef_run).not_to write_log('bashrc_manager_test1')
end
end

context 'log' do
before do
allow(File).to receive(:exist?).with('/home/test_user/.bashrc').and_return(true)
allow(File).to receive(:exist?).with('/home/test_user/.bashrc.d/init').and_return(true)
allow(IO).to receive(:read).with('/home/test_user/.bashrc').and_return('')
allow(IO).to receive(:read)
.with('/home/test_user/.bashrc.d/init')
.and_return(test1_content)
chef_run.converge described_recipe
end

it 'warning' do
expect(chef_run).to write_log('bashrc_manager_test1').with(level: :warn)
end
end
end
Expand Down

0 comments on commit dd52b87

Please sign in to comment.