Make `action` interpolated.

Akinori MUSHA 10 years ago
parent
commit
31c5570f9c

+ 8 - 2
app/concerns/agent_controller_concern.rb

@@ -12,11 +12,11 @@ module AgentControllerConcern
12 12
   end
13 13
 
14 14
   def control_action
15
-    options['action']
15
+    interpolated['action']
16 16
   end
17 17
 
18 18
   def validate_control_action
19
-    case control_action
19
+    case options['action']
20 20
     when 'run'
21 21
       control_targets.each { |target|
22 22
         if target.cannot_be_scheduled?
@@ -24,6 +24,10 @@ module AgentControllerConcern
24 24
         end
25 25
       }
26 26
     when 'enable', 'disable'
27
+    when nil
28
+      errors.add(:base, "action must be specified")
29
+    when /\{[%{]/
30
+      # Liquid template
27 31
     else
28 32
       errors.add(:base, 'invalid action')
29 33
     end
@@ -59,6 +63,8 @@ module AgentControllerConcern
59 63
             target.update!(disabled: true)
60 64
             log "Agent '#{target.name}' is disabled"
61 65
           end
66
+        when ''
67
+          # Do nothing
62 68
         else
63 69
           error "Unsupported action '#{control_action}' ignored for '#{target.name}'"
64 70
         end

+ 39 - 15
spec/support/shared_examples/agent_controller_concern.rb

@@ -9,33 +9,57 @@ shared_examples_for AgentControllerConcern do
9 9
   end
10 10
 
11 11
   describe "validation" do
12
-    it "should validate action" do
13
-      ['run', 'enable', 'disable'].each { |action|
14
-        agent.options['action'] = action
12
+    describe "of action" do
13
+      it "should allow certain values" do
14
+        ['run', 'enable', 'disable', '{{ action }}'].each { |action|
15
+          agent.options['action'] = action
16
+          expect(agent).to be_valid
17
+        }
18
+      end
19
+
20
+      it "should disallow obviously bad values" do
21
+        ['delete', nil, 1, true].each { |action|
22
+          agent.options['action'] = action
23
+          expect(agent).not_to be_valid
24
+        }
25
+      end
26
+
27
+      it "should accept 'run' if all target agents are schedulable" do
28
+        agent.control_targets = [agents(:bob_website_agent)]
15 29
         expect(agent).to be_valid
16
-      }
30
+      end
17 31
 
18
-      ['delete', '', nil, 1, true].each { |action|
19
-        agent.options['action'] = action
32
+      it "should reject 'run' if targets include an unschedulable agent" do
33
+        agent.control_targets = [agents(:bob_rain_notifier_agent)]
20 34
         expect(agent).not_to be_valid
21
-      }
35
+      end
36
+
37
+      it "should not reject 'enable' or 'disable' no matter if targets include an unschedulable agent" do
38
+        ['enable', 'disable'].each { |action|
39
+          agent.options['action'] = action
40
+          agent.control_targets = [agents(:bob_rain_notifier_agent)]
41
+          expect(agent).to be_valid
42
+        }
43
+      end
22 44
     end
23 45
   end
24 46
 
25 47
   describe 'control_action' do
26
-    it "cannot be 'run' if any of the control targets cannot be scheduled" do
48
+    it "returns options['action']" do
27 49
       expect(agent.control_action).to eq('run')
28
-      agent.control_targets = [agents(:bob_rain_notifier_agent)]
29
-      expect(agent).not_to be_valid
30
-    end
31 50
 
32
-    it "can be 'enable' or 'disable' no matter if control targets can be scheduled or not" do
33
-      ['enable', 'disable'].each { |action|
51
+      ['run', 'enable', 'disable'].each { |action|
34 52
         agent.options['action'] = action
35
-        agent.control_targets = [agents(:bob_rain_notifier_agent)]
36
-        expect(agent).to be_valid
53
+        expect(agent.control_action).to eq(action)
37 54
       }
38 55
     end
56
+
57
+    it "returns the result of interpolation" do
58
+      expect(agent.control_action).to eq('run')
59
+
60
+      agent.options['action'] = '{{ "enable" }}'
61
+      expect(agent.control_action).to eq('enable')
62
+    end
39 63
   end
40 64
 
41 65
   describe "control!" do