From dd52b8768c41b17c6f9c9307b22b6b4a6a58d24b Mon Sep 17 00:00:00 2001 From: Lloyd Philbrook Date: Fri, 29 Sep 2017 10:24:51 -0500 Subject: [PATCH] Check for duplicate content and log it --- resources/bashrc_manager.rb | 38 ++++++++++++++++++++++++----------- spec/bashrc_manager_spec.rb | 40 ++++++++++++++++++++++++++++++------- 2 files changed, 59 insertions(+), 19 deletions(-) diff --git a/resources/bashrc_manager.rb b/resources/bashrc_manager.rb index d5656e1..16388c9 100644 --- a/resources/bashrc_manager.rb +++ b/resources/bashrc_manager.rb @@ -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 @@ -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 @@ -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 diff --git a/spec/bashrc_manager_spec.rb b/spec/bashrc_manager_spec.rb index 33a7219..e971405 100644 --- a/spec/bashrc_manager_spec.rb +++ b/spec/bashrc_manager_spec.rb @@ -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' @@ -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('') @@ -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