Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand NodeInitPackage to catch more bad systemd tests #665

Merged
merged 1 commit into from
Jul 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions lib/rubocop/cop/chef/modernize/node_init_package.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,20 @@ module ChefModernize
# ::File.open('/proc/1/comm').chomp == 'systemd'
# File.open('/proc/1/comm').gets.chomp == 'systemd'
# File.open('/proc/1/comm').chomp == 'systemd'
# File.exist?('/proc/1/comm') && File.open('/proc/1/comm').chomp == 'systemd'
#
# IO.read('/proc/1/comm').chomp == 'systemd'
# IO.read('/proc/1/comm').gets.chomp == 'systemd'
# ::IO.read('/proc/1/comm').chomp == 'systemd'
# ::IO.read('/proc/1/comm').gets.chomp == 'systemd'
# File.exist?('/proc/1/comm') && File.open('/proc/1/comm').chomp == 'systemd'
# only_if 'test -f /bin/systemctl && /bin/systemctl'
#
# # good
# node['init_package'] == 'systemd'
# only_if { node['init_package'] == 'systemd' }
#
class NodeInitPackage < Cop
class NodeInitPackage < Base
extend RuboCop::Cop::AutoCorrector

MSG = "Use node['init_package'] to check for systemd instead of reading the contents of '/proc/1/comm'".freeze

def_node_matcher :file_reads_proc_1_comm?, <<-PATTERN
Expand All @@ -58,18 +60,24 @@ class NodeInitPackage < Cop
:== (str "systemd"))
PATTERN

def_node_matcher :file_systemd_conditional?, <<~PATTERN
(send nil? {:not_if :only_if} $(str "test -f /bin/systemctl && /bin/systemctl"))
PATTERN

def on_send(node)
compare_init_system?(node) do
# if there's a ::File.exist?('/proc/1/comm') check first we want to match that as well
node = node.parent if node.parent&.and_type? && proc_1_comm_exists?(node.parent.conditions.first)

add_offense(node, location: :expression, message: MSG, severity: :refactor)
add_offense(node.loc.expression, message: MSG, severity: :refactor) do |corrector|
corrector.replace(node, "node['init_package'] == 'systemd'")
end
end
end

def autocorrect(node)
lambda do |corrector|
corrector.replace(node.loc.expression, "node['init_package'] == 'systemd'")
file_systemd_conditional?(node) do |conditional|
add_offense(node.loc.expression, message: MSG, severity: :refactor) do |corrector|
corrector.replace(conditional.loc.expression, "{ node['init_package'] == 'systemd' }")
end
end
end
end
Expand Down
22 changes: 22 additions & 0 deletions spec/rubocop/cop/chef/modernize/node_init_package_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,28 @@
RUBY
end

it "registers an offense with only_if 'test -f /bin/systemctl && /bin/systemctl'" do
expect_offense(<<~RUBY)
only_if 'test -f /bin/systemctl && /bin/systemctl'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use node['init_package'] to check for systemd instead of reading the contents of '/proc/1/comm'
RUBY

expect_correction(<<~RUBY)
only_if { node['init_package'] == 'systemd' }
RUBY
end

it "registers an offense with not_if 'test -f /bin/systemctl && /bin/systemctl'" do
expect_offense(<<~RUBY)
not_if 'test -f /bin/systemctl && /bin/systemctl'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use node['init_package'] to check for systemd instead of reading the contents of '/proc/1/comm'
RUBY

expect_correction(<<~RUBY)
not_if { node['init_package'] == 'systemd' }
RUBY
end

it "does not register an offense when comparing a non-systemd value in '/proc/1/comm'" do
expect_no_offenses(<<~RUBY)
::File.open('/proc/1/comm').gets.chomp == 'foo'
Expand Down