ftpsite_agent_spec.rb 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. require 'spec_helper'
  2. require 'time'
  3. describe Agents::FtpsiteAgent do
  4. describe "checking anonymous FTP" do
  5. before do
  6. @site = {
  7. 'expected_update_period_in_days' => 1,
  8. 'url' => "ftp://ftp.example.org/pub/releases/",
  9. 'patterns' => ["example-*.tar.gz"],
  10. }
  11. @checker = Agents::FtpsiteAgent.new(:name => "Example", :options => @site, :keep_events_for => 2)
  12. @checker.user = users(:bob)
  13. @checker.save!
  14. stub(@checker).each_entry.returns { |block|
  15. block.call("example-latest.tar.gz", Time.parse("2014-04-01T10:00:01Z"))
  16. block.call("example-1.0.tar.gz", Time.parse("2013-10-01T10:00:00Z"))
  17. block.call("example-1.1.tar.gz", Time.parse("2014-04-01T10:00:00Z"))
  18. }
  19. end
  20. describe "#check" do
  21. it "should validate the integer fields" do
  22. @checker.options['expected_update_period_in_days'] = "nonsense"
  23. lambda { @checker.save! }.should raise_error;
  24. @checker.options = @site
  25. end
  26. it "should check for changes and save known entries in memory" do
  27. lambda { @checker.check }.should change { Event.count }.by(3)
  28. @checker.memory['known_entries'].tap { |known_entries|
  29. known_entries.size.should == 3
  30. known_entries.sort_by(&:last).should == [
  31. ["example-1.0.tar.gz", "2013-10-01T10:00:00Z"],
  32. ["example-1.1.tar.gz", "2014-04-01T10:00:00Z"],
  33. ["example-latest.tar.gz", "2014-04-01T10:00:01Z"],
  34. ]
  35. }
  36. Event.last(2).first.payload.should == {
  37. 'url' => 'ftp://ftp.example.org/pub/releases/example-1.1.tar.gz',
  38. 'filename' => 'example-1.1.tar.gz',
  39. 'timestamp' => '2014-04-01T10:00:00Z',
  40. }
  41. lambda { @checker.check }.should_not change { Event.count }
  42. stub(@checker).each_entry.returns { |block|
  43. block.call("example-latest.tar.gz", Time.parse("2014-04-02T10:00:01Z"))
  44. # In the long list format the timestamp may look going
  45. # backwards after six months: Oct 01 10:00 -> Oct 01 2013
  46. block.call("example-1.0.tar.gz", Time.parse("2013-10-01T00:00:00Z"))
  47. block.call("example-1.1.tar.gz", Time.parse("2014-04-01T10:00:00Z"))
  48. block.call("example-1.2.tar.gz", Time.parse("2014-04-02T10:00:00Z"))
  49. }
  50. lambda { @checker.check }.should change { Event.count }.by(2)
  51. @checker.memory['known_entries'].tap { |known_entries|
  52. known_entries.size.should == 4
  53. known_entries.sort_by(&:last).should == [
  54. ["example-1.0.tar.gz", "2013-10-01T00:00:00Z"],
  55. ["example-1.1.tar.gz", "2014-04-01T10:00:00Z"],
  56. ["example-1.2.tar.gz", "2014-04-02T10:00:00Z"],
  57. ["example-latest.tar.gz", "2014-04-02T10:00:01Z"],
  58. ]
  59. }
  60. Event.last(2).first.payload.should == {
  61. 'url' => 'ftp://ftp.example.org/pub/releases/example-1.2.tar.gz',
  62. 'filename' => 'example-1.2.tar.gz',
  63. 'timestamp' => '2014-04-02T10:00:00Z',
  64. }
  65. lambda { @checker.check }.should_not change { Event.count }
  66. end
  67. end
  68. end
  69. end