dry_runnable_spec.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. require 'rails_helper'
  2. describe DryRunnable do
  3. class Agents::SandboxedAgent < Agent
  4. default_schedule "3pm"
  5. can_dry_run!
  6. def check
  7. perform
  8. end
  9. def receive(events)
  10. events.each do |event|
  11. perform(event.payload['prefix'])
  12. end
  13. end
  14. private
  15. def perform(prefix = nil)
  16. log "Logging"
  17. create_event payload: { 'test' => "#{prefix}foo" }
  18. error "Recording error"
  19. create_event payload: { 'test' => "#{prefix}bar" }
  20. self.memory = { 'last_status' => 'ok', 'dry_run' => dry_run? }
  21. save!
  22. end
  23. end
  24. before do
  25. stub(Agents::SandboxedAgent).valid_type?("Agents::SandboxedAgent") { true }
  26. @agent = Agents::SandboxedAgent.create(name: "some agent") { |agent|
  27. agent.user = users(:bob)
  28. }
  29. end
  30. def counts
  31. [users(:bob).agents.count, users(:bob).events.count, users(:bob).logs.count]
  32. end
  33. it "does not affect normal run, with dry_run? returning false" do
  34. before = counts
  35. after = before.zip([0, 2, 2]).map { |x, d| x + d }
  36. expect {
  37. @agent.check
  38. @agent.reload
  39. }.to change { counts }.from(before).to(after)
  40. expect(@agent.memory).to eq({ 'last_status' => 'ok', 'dry_run' => false })
  41. payloads = @agent.events.reorder(:id).last(2).map(&:payload)
  42. expect(payloads).to eq([{ 'test' => 'foo' }, { 'test' => 'bar' }])
  43. messages = @agent.logs.reorder(:id).last(2).map(&:message)
  44. expect(messages).to eq(['Logging', 'Recording error'])
  45. end
  46. it "does not perform dry-run if Agent does not support dry-run" do
  47. stub(@agent).can_dry_run? { false }
  48. results = nil
  49. expect {
  50. results = @agent.dry_run!
  51. @agent.reload
  52. }.not_to change {
  53. [@agent.memory, counts]
  54. }
  55. expect(results[:log]).to match(/\A\[\d\d:\d\d:\d\d\] INFO -- : Dry Run failed\n\[\d\d:\d\d:\d\d\] ERROR -- : Exception during dry-run. SandboxedAgent does not support dry-run: /)
  56. expect(results[:events]).to eq([])
  57. expect(results[:memory]).to eq({})
  58. end
  59. describe "dry_run!" do
  60. it "traps any destructive operations during a run" do
  61. results = nil
  62. expect {
  63. results = @agent.dry_run!
  64. @agent.reload
  65. }.not_to change {
  66. [@agent.memory, counts]
  67. }
  68. expect(results[:log]).to match(/\A\[\d\d:\d\d:\d\d\] INFO -- : Dry Run started\n\[\d\d:\d\d:\d\d\] INFO -- : Logging\n\[\d\d:\d\d:\d\d\] ERROR -- : Recording error\n/)
  69. expect(results[:events]).to eq([{ 'test' => 'foo' }, { 'test' => 'bar' }])
  70. expect(results[:memory]).to eq({ 'last_status' => 'ok', 'dry_run' => true })
  71. end
  72. it "traps any destructive operations during a run when an event is given" do
  73. results = nil
  74. expect {
  75. results = @agent.dry_run!(Event.new(payload: { 'prefix' => 'super' }))
  76. @agent.reload
  77. }.not_to change {
  78. [@agent.memory, counts]
  79. }
  80. expect(results[:log]).to match(/\A\[\d\d:\d\d:\d\d\] INFO -- : Dry Run started\n\[\d\d:\d\d:\d\d\] INFO -- : Logging\n\[\d\d:\d\d:\d\d\] ERROR -- : Recording error\n/)
  81. expect(results[:events]).to eq([{ 'test' => 'superfoo' }, { 'test' => 'superbar' }])
  82. expect(results[:memory]).to eq({ 'last_status' => 'ok', 'dry_run' => true })
  83. end
  84. end
  85. end