commander_agent_spec.rb 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. require 'rails_helper'
  2. describe Agents::CommanderAgent do
  3. let(:target) {
  4. agents(:bob_website_agent)
  5. }
  6. let(:valid_params) {
  7. {
  8. name: 'Example',
  9. schedule: 'every_1h',
  10. options: {
  11. 'action' => 'run',
  12. },
  13. user: users(:bob),
  14. control_targets: [target]
  15. }
  16. }
  17. let(:agent) {
  18. described_class.create!(valid_params)
  19. }
  20. it_behaves_like AgentControllerConcern
  21. describe "check" do
  22. it "should command targets" do
  23. allow(Agent).to receive(:async_check).with(target.id).once { nil }
  24. agent.check
  25. end
  26. end
  27. describe "receive_events" do
  28. it "should command targets" do
  29. allow(Agent).to receive(:async_check).with(target.id).once { nil }
  30. event = Event.new
  31. event.agent = agents(:bob_rain_notifier_agent)
  32. event.payload = {
  33. 'url' => 'http://xkcd.com',
  34. 'link' => 'Random',
  35. }
  36. agent.receive([event])
  37. end
  38. context "to configure" do
  39. let(:real_target) {
  40. Agents::TriggerAgent.create!(
  41. name: "somename",
  42. options: {
  43. expected_receive_period_in_days: 2,
  44. rules: [
  45. {
  46. 'type' => 'field<value',
  47. 'value' => '200.0',
  48. 'path' => 'price',
  49. }
  50. ],
  51. keep_event: 'true'
  52. },
  53. user: users(:bob)
  54. )
  55. }
  56. let(:valid_params) {
  57. {
  58. name: 'Example',
  59. schedule: 'never',
  60. options: {
  61. 'action' => '{% if target.id == agent_id %}configure{% endif %}',
  62. 'configure_options' => {
  63. 'rules' => [
  64. {
  65. 'type' => 'field<value',
  66. 'value' => "{{price}}",
  67. 'path' => 'price',
  68. }
  69. ]
  70. }
  71. },
  72. user: users(:bob),
  73. control_targets: [target, real_target]
  74. }
  75. }
  76. it "should conditionally configure targets interpolating agent attributes" do
  77. expect {
  78. event = Event.new
  79. event.agent = agents(:bob_website_agent)
  80. event.payload = {
  81. 'price' => '198.0',
  82. 'agent_id' => real_target.id
  83. }
  84. agent.receive([event])
  85. }.to change {
  86. real_target.options['rules'][0]['value']
  87. }.from('200.0').to('198.0') & not_change {
  88. target.options
  89. }
  90. end
  91. end
  92. end
  93. end