Make action interpolated.

This commit is contained in:
Akinori MUSHA 2014-10-09 20:18:39 +09:00
parent 2d790b54ea
commit 31c5570f9c
2 changed files with 49 additions and 19 deletions

View file

@ -12,11 +12,11 @@ module AgentControllerConcern
end
def control_action
options['action']
interpolated['action']
end
def validate_control_action
case control_action
case options['action']
when 'run'
control_targets.each { |target|
if target.cannot_be_scheduled?
@ -24,6 +24,10 @@ module AgentControllerConcern
end
}
when 'enable', 'disable'
when nil
errors.add(:base, "action must be specified")
when /\{[%{]/
# Liquid template
else
errors.add(:base, 'invalid action')
end
@ -59,6 +63,8 @@ module AgentControllerConcern
target.update!(disabled: true)
log "Agent '#{target.name}' is disabled"
end
when ''
# Do nothing
else
error "Unsupported action '#{control_action}' ignored for '#{target.name}'"
end

View file

@ -9,32 +9,56 @@ shared_examples_for AgentControllerConcern do
end
describe "validation" do
it "should validate action" do
['run', 'enable', 'disable'].each { |action|
agent.options['action'] = action
expect(agent).to be_valid
}
describe "of action" do
it "should allow certain values" do
['run', 'enable', 'disable', '{{ action }}'].each { |action|
agent.options['action'] = action
expect(agent).to be_valid
}
end
['delete', '', nil, 1, true].each { |action|
agent.options['action'] = action
it "should disallow obviously bad values" do
['delete', nil, 1, true].each { |action|
agent.options['action'] = action
expect(agent).not_to be_valid
}
end
it "should accept 'run' if all target agents are schedulable" do
agent.control_targets = [agents(:bob_website_agent)]
expect(agent).to be_valid
end
it "should reject 'run' if targets include an unschedulable agent" do
agent.control_targets = [agents(:bob_rain_notifier_agent)]
expect(agent).not_to be_valid
}
end
it "should not reject 'enable' or 'disable' no matter if targets include an unschedulable agent" do
['enable', 'disable'].each { |action|
agent.options['action'] = action
agent.control_targets = [agents(:bob_rain_notifier_agent)]
expect(agent).to be_valid
}
end
end
end
describe 'control_action' do
it "cannot be 'run' if any of the control targets cannot be scheduled" do
it "returns options['action']" do
expect(agent.control_action).to eq('run')
agent.control_targets = [agents(:bob_rain_notifier_agent)]
expect(agent).not_to be_valid
['run', 'enable', 'disable'].each { |action|
agent.options['action'] = action
expect(agent.control_action).to eq(action)
}
end
it "can be 'enable' or 'disable' no matter if control targets can be scheduled or not" do
['enable', 'disable'].each { |action|
agent.options['action'] = action
agent.control_targets = [agents(:bob_rain_notifier_agent)]
expect(agent).to be_valid
}
it "returns the result of interpolation" do
expect(agent.control_action).to eq('run')
agent.options['action'] = '{{ "enable" }}'
expect(agent.control_action).to eq('enable')
end
end