Skip to content
Browse files

FC038: Invalid resource action, refs #61.

  • Loading branch information...
1 parent a9ad7b5 commit 049eb66df423d7f78c7727852329f63e08cb9114 @acrmp committed Sep 16, 2012
View
39 features/038_check_for_invalid_action.feature
@@ -0,0 +1,39 @@
+Feature: Check for invalid resource actions
+
+ In order to avoid failure at converge time
+ As a developer
+ I want to identify resources that have an invalid action
+
+ Scenario: Resource with default action
+ Given a cookbook recipe with a service resource that does not specify an action
+ When I check the cookbook
+ Then the invalid resource action warning 038 should not be displayed
+
+ Scenario: Resource with variable action
+ Given a cookbook recipe with a service resource with an action specified via a variable
+ When I check the cookbook
+ Then the invalid resource action warning 038 should not be displayed
+
+ Scenario Outline: Resource actions
+ Given a cookbook recipe with a <resource> resource with action <action>
+ When I check the cookbook
+ Then the invalid resource action warning 038 <display> be displayed
+ Examples:
+ | resource | action | display |
+ | service | restart | should not |
+ | service | nothing | should not |
+ | service | none | should |
+ | service | create | should |
+ | execute | run | should not |
+ | execute | go | should |
+ | foo | none | should not |
+
+ Scenario Outline: Multiple actions
+ Given a cookbook recipe with a <resource> resource with actions <actions>
+ When I check the cookbook
+ Then the invalid resource action warning 038 <display> be displayed
+ Examples:
+ | resource | actions | display |
+ | service | enable, start | should not |
+ | service | enble, start | should |
+ | foo | energize, revitalize | should not |
View
32 features/step_definitions/cookbook_steps.rb
@@ -451,6 +451,22 @@
}
end
+Given /^a cookbook recipe with a ([^ ]+) resource with action (.*)$/ do |resource, action|
+ write_recipe %Q{
+ #{resource} "foo" do
+ action :#{action}
+ end
+ }
+end
+
+Given /^a cookbook recipe with a ([^ ]+) resource with actions (.*)$/ do |resource, actions|
+ write_recipe %Q{
+ #{resource} "foo" do
+ action [#{actions.split(', ').map{|a| ":#{a}"}.join(", ")}]
+ end
+ }
+end
+
Given 'a cookbook recipe with a case condition unrelated to platform' do
write_recipe %Q{
case day_of_week
@@ -516,6 +532,22 @@
end
end
+Given 'a cookbook recipe with a service resource that does not specify an action' do
+ write_recipe %q{
+ service "foo" do
+ start_command "/sbin/service foo start"
+ end
+ }.strip
+end
+
+Given 'a cookbook recipe with a service resource with an action specified via a variable' do
+ write_recipe %q{
+ service "foo" do
+ action action
+ end
+ }.strip
+end
+
Given 'a cookbook template that uses all variables passed' do
write_recipe %q{
template "/tmp/config.conf" do
View
1 features/support/command_helpers.rb
@@ -41,6 +41,7 @@ module CommandHelpers
'FC033' => 'Missing template',
'FC034' => 'Unused template variables',
'FC037' => 'Invalid notification action',
+ 'FC038' => 'Invalid resource action',
'FCTEST001' => 'Test Rule'
}
View
17 lib/foodcritic/rules.rb
@@ -488,3 +488,20 @@ def pry_bindings(ast)
end
end
end
+
+rule "FC038", "Invalid resource action" do
+ tags %w{correctness}
+ recipe do |ast|
+ find_resources(ast).select do |resource|
+ actions = resource_attributes(resource)['action']
+ if actions.respond_to?(:xpath)
+ actions = actions.xpath('descendant::array/descendant::symbol/ident/@value')
+ else
+ actions = Array(actions)
+ end
+ actions.reject{|a| a.to_s.empty?}.any? do |action|
+ ! resource_action?(resource_type(resource), action)
+ end
+ end
+ end
+end

0 comments on commit 049eb66

Please sign in to comment.
Something went wrong with that request. Please try again.