1
0

dry_runnable_spec.rb 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. require 'spec_helper'
  2. describe DryRunnable do
  3. class Agents::SandboxedAgent < Agent
  4. default_schedule "3pm"
  5. can_dry_run!
  6. def check
  7. log "Logging"
  8. create_event payload: { 'test' => 'foo' }
  9. error "Recording error"
  10. create_event payload: { 'test' => 'bar' }
  11. self.memory = { 'last_status' => 'ok', 'dry_run' => dry_run? }
  12. save!
  13. end
  14. end
  15. before do
  16. stub(Agents::SandboxedAgent).valid_type?("Agents::SandboxedAgent") { true }
  17. @agent = Agents::SandboxedAgent.create(name: "some agent") { |agent|
  18. agent.user = users(:bob)
  19. }
  20. end
  21. def counts
  22. [users(:bob).agents.count, users(:bob).events.count, users(:bob).logs.count]
  23. end
  24. it "does not affect normal run, with dry_run? returning false" do
  25. before = counts
  26. after = before.zip([0, 2, 2]).map { |x, d| x + d }
  27. expect {
  28. @agent.check
  29. @agent.reload
  30. }.to change { counts }.from(before).to(after)
  31. expect(@agent.memory).to eq({ 'last_status' => 'ok', 'dry_run' => false })
  32. payloads = @agent.events.reorder(:id).last(2).map(&:payload)
  33. expect(payloads).to eq([{ 'test' => 'foo' }, { 'test' => 'bar' }])
  34. messages = @agent.logs.reorder(:id).last(2).map(&:message)
  35. expect(messages).to eq(['Logging', 'Recording error'])
  36. end
  37. it "traps logging, event emission and memory updating, with dry_run? returning true" do
  38. results = nil
  39. expect {
  40. results = @agent.dry_run!
  41. @agent.reload
  42. }.not_to change {
  43. [@agent.memory, counts]
  44. }
  45. expect(results[:log]).to match(/\AI, .+ INFO -- : Logging\nE, .+ ERROR -- : Recording error\n/)
  46. expect(results[:events]).to eq([{ 'test' => 'foo' }, { 'test' => 'bar' }])
  47. expect(results[:memory]).to eq({ 'last_status' => 'ok', 'dry_run' => true })
  48. end
  49. it "does not perform dry-run if Agent does not support dry-run" do
  50. stub(@agent).can_dry_run? { false }
  51. results = nil
  52. expect {
  53. results = @agent.dry_run!
  54. @agent.reload
  55. }.not_to change {
  56. [@agent.memory, counts]
  57. }
  58. expect(results[:log]).to match(/\AE, .+ ERROR -- : Exception during dry-run. SandboxedAgent does not support dry-run: /)
  59. expect(results[:events]).to eq([])
  60. expect(results[:memory]).to eq({})
  61. end
  62. end