Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

FC039: Node method cannot be accessed with key.

  • Loading branch information...
commit 6a776d969d4b84373635f4e4e60b40991a7c2a1e 1 parent 049eb66
Andrew Crump authored
25 features/039_check_for_key_access_to_node_methods.feature
View
@@ -0,0 +1,25 @@
+Feature: Check for key access to node methods
+
+ In order to avoid failure at converge time
+ As a developer
+ I want to identify attempts to access node methods as attributes
+
+ Scenario Outline: Resource actions
+ Given a cookbook recipe that refers to <node_access>
+ When I check the cookbook
+ Then the node method cannot be accessed with key warning 039 <display> be displayed
+ Examples:
+ | node_access | display |
+ | node['foo'] | should not |
+ | node[:foo] | should not |
+ | node.foo | should not |
+ | node.chef_environment | should not |
+ | node[:chef_environment] | should |
+ | node['chef_environment'] | should |
+ | node.run_state['foo'] | should not |
+ | node.run_state[:foo] | should not |
+ | node.run_state.foo | should not |
+ | node['run_state']['foo'] | should |
+ | node[:run_state][:foo] | should |
+ | node['foo']['run_state'] | should not |
+ | node[:foo][:run_state] | should not |
7 features/step_definitions/cookbook_steps.rb
View
@@ -33,6 +33,12 @@
}
end
+Given /^a cookbook recipe that refers to (node.*)$/ do |reference|
+ write_recipe %Q{
+ Chef::Log.info #{reference}
+ }
+end
+
Given /^a cookbook recipe that wraps a platform\-specific resource in a (.*) conditional$/ do |conditional|
write_recipe %Q{
if #{conditional}
@@ -1193,6 +1199,7 @@ def search(bag_name, query=nil, sort=nil, start=0, rows=1000, &block)
:attribute_access,
:checks_for_chef_solo?,
:chef_dsl_methods,
+ :chef_node_methods,
:chef_solo_search_supported?,
:cookbook_name,
:declared_dependencies,
1  features/support/command_helpers.rb
View
@@ -42,6 +42,7 @@ module CommandHelpers
'FC034' => 'Unused template variables',
'FC037' => 'Invalid notification action',
'FC038' => 'Invalid resource action',
+ 'FC039' => 'Node method cannot be accessed with key',
'FCTEST001' => 'Test Rule'
}
5 lib/foodcritic/chef.rb
View
@@ -8,6 +8,11 @@ def chef_dsl_methods
@dsl_metadata[:dsl_methods].map(&:to_sym)
end
+ def chef_node_methods
+ load_metadata
+ @dsl_metadata[:node_methods].map(&:to_sym)
+ end
+
# Is the specified action valid for the type of resource?
def resource_action?(resource_type, action)
resource_check?(:actions, resource_type, action)
15 lib/foodcritic/rules.rb
View
@@ -505,3 +505,18 @@ def pry_bindings(ast)
end
end
end
+
+rule "FC039", "Node method cannot be accessed with key" do
+ tags %w{correctness}
+ recipe do |ast|
+ (attribute_access(ast, :type => :string).select do |a|
+ chef_node_methods.include?(a.xpath('@value').to_s.to_sym)
+ end +
+ attribute_access(ast, :type => :symbol).select do |a|
+ chef_node_methods.include?(a.xpath('ident/@value').to_s.to_sym)
+ end).select do |att|
+ ! att.xpath('ancestor::args_add_block[position() = 1]
+ [preceding-sibling::vcall | preceding-sibling::var_ref]').empty?
+ end
+ end
+end
5 rakelib/dsl_methods.rake
View
@@ -5,6 +5,7 @@ METADATA_FILE = 'chef_dsl_metadata.json'
file METADATA_FILE do
require_chef
chef_dsl_metadata = {:dsl_methods => chef_dsl_methods,
+ :node_methods => chef_node_methods,
:actions => chef_resource_actions,
:attributes => chef_resource_attributes}
json = Yajl::Encoder.encode(chef_dsl_metadata, :pretty => true)
@@ -24,6 +25,10 @@ def chef_dsl_methods
end).flatten.sort.uniq
end
+def chef_node_methods
+ Chef::Node.public_instance_methods.flatten.sort.uniq
+end
+
def chef_resource_actions
chef_resources do |resource_klazz,resource|
instance = resource.new('dsl')
Please sign in to comment.
Something went wrong with that request. Please try again.