Bladeren bron

Use deep_merge instead of merge to allow configuring Agent.options within a nested hash. Useful when configuring DataOutputAgent template>item options with CommanderAgent. Also fix SchedulerAgent 'check' typo.

Brian Petro 9 jaren geleden
bovenliggende
commit
87a58827ff

+ 2 - 2
app/concerns/agent_controller_concern.rb

@@ -7,7 +7,7 @@ module AgentControllerConcern
 
 
   def default_options
   def default_options
     {
     {
-      'action' => 'run',
+      'action' => 'run'
     }
     }
   end
   end
 
 
@@ -68,7 +68,7 @@ module AgentControllerConcern
             log "Agent '#{target.name}' is disabled"
             log "Agent '#{target.name}' is disabled"
           end
           end
         when 'configure'
         when 'configure'
-          target.update!(options: target.options.merge(interpolated['configure_options']))
+          target.update! options: target.options.deep_merge(interpolated['configure_options'])
           log "Agent '#{target.name}' is configured with #{interpolated['configure_options'].inspect}"
           log "Agent '#{target.name}' is configured with #{interpolated['configure_options'].inspect}"
         when ''
         when ''
           # Do nothing
           # Do nothing

+ 1 - 1
app/models/agents/scheduler_agent.rb

@@ -87,7 +87,7 @@ module Agents
       true
       true
     end
     end
 
 
-    def check!
+    def check
       control!
       control!
     end
     end
 
 

+ 21 - 0
spec/fixtures/agents.yml

@@ -111,3 +111,24 @@ jane_basecamp_agent:
   user: jane
   user: jane
   service: generic
   service: generic
   guid: <%= SecureRandom.hex %>
   guid: <%= SecureRandom.hex %>
+
+
+bob_data_output_agent:
+  type: Agents::DataOutputAgent
+  user: bob
+  name: RSS Feed 
+  guid: <%= SecureRandom.hex %>
+  options: <%= {
+    expected_receive_period_in_days: 3,
+    secrets: ['secret'],
+    template: {
+      title: 'unchanged',
+      description: 'unchanged',
+      item: {
+        title: 'unchanged',
+        description: 'unchanged',
+        author: 'unchanged',
+        link: 'http://example.com'
+        }
+      }
+    }.to_json.inspect %>

+ 1 - 1
spec/models/agents/scheduler_agent_spec.rb

@@ -88,7 +88,7 @@ describe Agents::SchedulerAgent do
   describe "check!" do
   describe "check!" do
     it "should control targets" do
     it "should control targets" do
       stub(agent).control!.once { nil }
       stub(agent).control!.once { nil }
-      agent.check!
+      agent.check
     end
     end
   end
   end
 end
 end

+ 19 - 0
spec/support/shared_examples/agent_controller_concern.rb

@@ -128,5 +128,24 @@ shared_examples_for AgentControllerConcern do
       expect(agent.control_targets.reload).to all(satisfy { |a| a.options['url'] == 'http://some-new-url.com/SOMETHING' })
       expect(agent.control_targets.reload).to all(satisfy { |a| a.options['url'] == 'http://some-new-url.com/SOMETHING' })
       expect(agents(:bob_website_agent).reload.options).to eq(old_options.merge('url' => 'http://some-new-url.com/SOMETHING'))
       expect(agents(:bob_website_agent).reload.options).to eq(old_options.merge('url' => 'http://some-new-url.com/SOMETHING'))
     end
     end
+
+    it "should configure targets with nested objects" do
+      agent.control_targets << agents(:bob_data_output_agent)
+      agent.options['action'] = 'configure'
+      agent.options['configure_options'] = { 
+        template: {
+          item: {
+           title: "changed"
+          }
+        }
+      }
+      agent.save!
+      old_options = agents(:bob_data_output_agent).options
+
+      agent.check
+
+      expect(agent.control_targets.reload).to all(satisfy { |a| a.options['template'] && a.options['template']['item'] && (a.options['template']['item']['title'] == 'changed') })
+      expect(agents(:bob_data_output_agent).reload.options).to eq(old_options.deep_merge(agent.options['configure_options']))
+    end
   end
   end
 end
 end