require 'test_helper' require 'observation_mover' class ObservationMoverTest < Test::Unit::TestCase context "ObservationMover" do setup do setup_log_dir @mover = ObservationMover.new(log_file_path) end context "#rotate" do context "with a non-empty log file" do should "move the log file" do @mover.rotate_current rotated = rotated_path! assert_not_nil rotated assert_equal "loggy\n", File.read(rotated) end should "create a new, empty log file" do @mover.rotate_current assert File.exists?(log_file_path) assert ! File.size?(log_file_path) end should "return true" do assert @mover.rotate_current end end context "with an empty log file" do setup do File.open(log_file_path, "w").close end should "not rename the log file" do @mover.rotate_current assert_equal [log_file_path], Dir[log_file_path('*')] end should "return false" do assert ! @mover.rotate_current end end end context "#prepare_rotated_for_archiving" do setup do @mover.rotate_current end should "rename a rotated file" do rotated = rotated_path! @mover.prepare_rotated_for_archiving assert ! File.exists?(rotated), "#{rotated} exists!" end should "compress a rotated file" do rotated = rotated_path! @mover.prepare_rotated_for_archiving assert ! Dir[rotated + '*.gz'].empty? end end context '#move_prepared_to_archive' do context "with one file with a sha" do setup do @shad_name = log_file_path('observer-log-2010-01-05-21-13-11-1234567890abcdef.gz') File.open(@shad_name, 'w') { |f| f << "logline\n" } end should "move that file to s3" do @mover.expects(:store_file_on_s3).with(@shad_name).returns(true) @mover.move_prepared_to_archive end should "delete the file if the move succeeds" do @mover.expects(:store_file_on_s3).with(@shad_name).returns(true) @mover.move_prepared_to_archive assert ! File.exists?(@shad_name) end should "not delete the file if the move fails" do @mover.expects(:store_file_on_s3).with(@shad_name).returns(false) @mover.move_prepared_to_archive assert File.exists?(@shad_name) end # should "talk to s3" do # #WebMock.disable_net_connect! # #WebMock.allow_net_connect! # # stub_request(:put, %r'https://rwdata-logs.s3.amazonaws.com/.*').to_return do |request| # # raise request.inspect # # end # # PUT https://rwdata-logs.s3.amazonaws.com/observer-log-2010-01-05-21-13-11-1234567890abcdef with body 'logline\n' with headers {'Accept'=>'*/*', 'Authorization'=>'AWS AAAAAAAAAA:4aYODn+8Yx7nIGnGf1coqWbEjWY=', 'Content-Length'=>'8', 'Content-Md5'=>'2ee0c171dab3e30627525ff535355eba', 'Content-Type'=>'', 'Date'=>'Thu, 'User-Agent'=>'', 22 Apr 2010 12:11:09 GMT'} # @mover.move_prepared_to_archive # end # should "talk to s3" do # WebMock.disable_net_connect! # #WebMock.allow_net_connect! # stub_request(:any, 'https://rwdata-logs.s3.amazonaws.com/?location').to_return(:status => [500, 'Internal Server knas']) # @mover.s3.bucket_location('rwdata-logs') # end end end context '#aws_keys' do should "read from a yaml file and return a list" do file = fixture_file('aws_keys_yaml.txt') key, secret = @mover.aws_keys(file) assert_equal 'PUBLICKEY', key assert_equal 'SECRET/KEY', secret end end context '#md5' do should "return a Base64-coded string" do file = fixture_file('../fixtures/log-for-md5.log') assert_equal 'X301NTIE25GbZGzT7u7cog==', @mover.md5(file) end end end def log_file_path (file_name = 'observer.log') File.join(log_dir, file_name) end def log_dir '/tmp/test/logs' end def setup_log_dir FileUtils.mkdir_p log_dir FileUtils.rm_r log_dir FileUtils.mkdir log_dir File.open(log_file_path, "w") { |f| f.puts "loggy" } end def rotated_path! paths = Dir[log_file_path('*')] - [log_file_path] assert_equal 1, paths.size, "Not exactly one path in #{paths.inspect}" paths.first end end