ftpsite_agent_spec.rb 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. end
  15. describe "#check" do
  16. before do
  17. stub(@checker).each_entry.returns { |block|
  18. block.call("example latest.tar.gz", Time.parse("2014-04-01T10:00:01Z"))
  19. block.call("example-1.0.tar.gz", Time.parse("2013-10-01T10:00:00Z"))
  20. block.call("example-1.1.tar.gz", Time.parse("2014-04-01T10:00:00Z"))
  21. }
  22. end
  23. it "should validate the integer fields" do
  24. @checker.options['expected_update_period_in_days'] = "nonsense"
  25. expect { @checker.save! }.to raise_error;
  26. @checker.options = @site
  27. end
  28. it "should check for changes and save known entries in memory" do
  29. expect { @checker.check }.to change { Event.count }.by(3)
  30. @checker.memory['known_entries'].tap { |known_entries|
  31. expect(known_entries.size).to eq(3)
  32. expect(known_entries.sort_by(&:last)).to eq([
  33. ["example-1.0.tar.gz", "2013-10-01T10:00:00Z"],
  34. ["example-1.1.tar.gz", "2014-04-01T10:00:00Z"],
  35. ["example latest.tar.gz", "2014-04-01T10:00:01Z"],
  36. ])
  37. }
  38. expect(Event.last(2).first.payload).to eq({
  39. 'url' => 'ftp://ftp.example.org/pub/releases/example-1.1.tar.gz',
  40. 'filename' => 'example-1.1.tar.gz',
  41. 'timestamp' => '2014-04-01T10:00:00Z',
  42. })
  43. expect { @checker.check }.not_to change { Event.count }
  44. stub(@checker).each_entry.returns { |block|
  45. block.call("example latest.tar.gz", Time.parse("2014-04-02T10:00:01Z"))
  46. # In the long list format the timestamp may look going
  47. # backwards after six months: Oct 01 10:00 -> Oct 01 2013
  48. block.call("example-1.0.tar.gz", Time.parse("2013-10-01T00:00:00Z"))
  49. block.call("example-1.1.tar.gz", Time.parse("2014-04-01T10:00:00Z"))
  50. block.call("example-1.2.tar.gz", Time.parse("2014-04-02T10:00:00Z"))
  51. }
  52. expect { @checker.check }.to change { Event.count }.by(2)
  53. @checker.memory['known_entries'].tap { |known_entries|
  54. expect(known_entries.size).to eq(4)
  55. expect(known_entries.sort_by(&:last)).to eq([
  56. ["example-1.0.tar.gz", "2013-10-01T00:00:00Z"],
  57. ["example-1.1.tar.gz", "2014-04-01T10:00:00Z"],
  58. ["example-1.2.tar.gz", "2014-04-02T10:00:00Z"],
  59. ["example latest.tar.gz", "2014-04-02T10:00:01Z"],
  60. ])
  61. }
  62. expect(Event.last(2).first.payload).to eq({
  63. 'url' => 'ftp://ftp.example.org/pub/releases/example-1.2.tar.gz',
  64. 'filename' => 'example-1.2.tar.gz',
  65. 'timestamp' => '2014-04-02T10:00:00Z',
  66. })
  67. expect(Event.last.payload).to eq({
  68. 'url' => 'ftp://ftp.example.org/pub/releases/example%20latest.tar.gz',
  69. 'filename' => 'example latest.tar.gz',
  70. 'timestamp' => '2014-04-02T10:00:01Z',
  71. })
  72. expect { @checker.check }.not_to change { Event.count }
  73. end
  74. end
  75. describe "#each_entry" do
  76. before do
  77. stub.any_instance_of(Net::FTP).list.returns [ # Windows format
  78. "04-02-14 10:01AM 288720748 example latest.tar.gz",
  79. "04-01-14 10:05AM 288720710 no-match-example.tar.gz"
  80. ]
  81. stub(@checker).open_ftp.yields Net::FTP.new
  82. end
  83. it "filters out files that don't match the given format" do
  84. entries = []
  85. @checker.each_entry { |a, b| entries.push [a, b] }
  86. expect(entries.size).to eq(1)
  87. filename, mtime = entries.first
  88. expect(filename).to eq('example latest.tar.gz')
  89. expect(mtime).to eq('2014-04-02T10:01:00Z')
  90. end
  91. it "filters out files that are older than the given date" do
  92. @checker.options['after'] = '2015-10-21'
  93. entries = []
  94. @checker.each_entry { |a, b| entries.push [a, b] }
  95. expect(entries.size).to eq(0)
  96. end
  97. end
  98. end
  99. end