PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/spec/unit/file_format/rails_format_spec.rb

https://github.com/itsderek23/request-log-analyzer
Ruby | 180 lines | 142 code | 38 blank | 0 comment | 8 complexity | 991853f68b6951b8b06f8cca21602720 MD5 | raw file
  1. require File.expand_path('../../../spec_helper.rb', __FILE__)
  2. describe RequestLogAnalyzer::FileFormat::Rails do
  3. describe '.create' do
  4. context 'without providing a lines argument' do
  5. before(:each) { @rails = RequestLogAnalyzer::FileFormat.load(:rails) }
  6. it "should create a valid file format" do
  7. @rails.should be_valid
  8. end
  9. it "should parse the production lines" do
  10. production_rails = RequestLogAnalyzer::FileFormat.load(:rails, 'production')
  11. @rails.line_definitions.should == production_rails.line_definitions
  12. end
  13. end
  14. context 'using a comma separated list of lines as argument' do
  15. before(:each) { @rails = RequestLogAnalyzer::FileFormat.load(:rails, 'minimal,failure') }
  16. it "should return a valid language" do
  17. @rails.should be_valid
  18. end
  19. it "should at least parse :processing and :completed lines" do
  20. @rails.line_definitions.should include(:processing, :completed, :failure)
  21. end
  22. end
  23. RequestLogAnalyzer::FileFormat::Rails::LINE_COLLECTIONS.keys.each do |constant|
  24. context "using the '#{constant}' line collection constant" do
  25. before(:each) { @rails = RequestLogAnalyzer::FileFormat.load(:rails, constant) }
  26. it "should return a valid language" do
  27. @rails.should be_valid
  28. end
  29. it "should at least parse :processing and :completed lines" do
  30. @rails.line_definitions.should include(:processing, :completed)
  31. end
  32. end
  33. end
  34. end
  35. describe '#parse_line' do
  36. before(:each) { @rails = RequestLogAnalyzer::FileFormat.load(:rails, :all) }
  37. {'with prefix' => 'LINE PREFIX: ', 'without prefix' => '' }.each do |context, prefix|
  38. context context do
  39. it "should parse a :processing line correctly" do
  40. line = prefix + 'Processing PeopleController#index (for 1.1.1.1 at 2008-08-14 21:16:30) [GET]'
  41. @rails.should parse_line(line).as(:processing).and_capture(:controller => 'PeopleController', :action => 'index', :timestamp => 20080814211630, :method => 'GET', :ip => '1.1.1.1')
  42. end
  43. it "should parse a :processing line correctly when it contains ipv6 localhost address" do
  44. line = prefix + 'Processing PeopleController#index (for ::1 at 2008-08-14 21:16:30) [GET]'
  45. @rails.should parse_line(line).as(:processing).and_capture(:controller => 'PeopleController', :action => 'index', :timestamp => 20080814211630, :method => 'GET', :ip => '::1')
  46. end
  47. it "should parse a :processing line correctly when it contains ipv6 address" do
  48. line = prefix + 'Processing PeopleController#index (for 3ffe:1900:4545:3:200:f8ff:fe21:67cf at 2008-08-14 21:16:30) [GET]'
  49. @rails.should parse_line(line).as(:processing).and_capture(:controller => 'PeopleController', :action => 'index', :timestamp => 20080814211630, :method => 'GET', :ip => '3ffe:1900:4545:3:200:f8ff:fe21:67cf')
  50. end
  51. it "should parse a Rails 2.1 style :completed line correctly" do
  52. line = prefix + 'Completed in 0.21665 (4 reqs/sec) | Rendering: 0.00926 (4%) | DB: 0.00000 (0%) | 200 OK [http://demo.nu/employees]'
  53. @rails.should parse_line(line).as(:completed).and_capture(:duration => 0.21665, :db => 0.0, :view => 0.00926, :status => 200, :url => 'http://demo.nu/employees')
  54. end
  55. it "should parse a Rails 2.2 style :completed line correctly" do
  56. line = prefix + 'Completed in 614ms (View: 120, DB: 31) | 200 OK [http://floorplanner.local/demo]'
  57. @rails.should parse_line(line).as(:completed).and_capture(:duration => 0.614, :db => 0.031, :view => 0.120, :status => 200, :url => 'http://floorplanner.local/demo')
  58. end
  59. it "should parse a :failure line with exception correctly" do
  60. line = prefix + "NoMethodError (undefined method `update_domain_account' for nil:NilClass):"
  61. @rails.should parse_line(line).as(:failure).and_capture(:error => 'NoMethodError', :message => "undefined method `update_domain_account' for nil:NilClass")
  62. end
  63. it "should parse a :cache_hit line correctly with an filter instance reference" do
  64. line = prefix + 'Filter chain halted as [#<ActionController::Filters::AroundFilter:0x2a999ad120 @identifier=nil, @kind=:filter, @options={:only=>#<Set: {"cached"}>, :if=>:not_logged_in?, :unless=>nil}, @method=#<ActionController::Caching::Actions::ActionCacheFilter:0x2a999ad620 @check=nil, @options={:store_options=>{}, :layout=>nil, :cache_path=>#<Proc:0x0000002a999b8890@/app/controllers/cached_controller.rb:8>}>>] did_not_yield.'
  65. @rails.should parse_line(line).as(:cache_hit)
  66. end
  67. it "should parse a :cache_hit line correctly with an proc instance reference" do
  68. line = prefix + 'Filter chain halted as [#<ActionController::Filters::AroundFilter:0x2a9a923e38 @method=#<Proc:0x0000002a9818b3f8@/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/caching/actions.rb:64>, @kind=:filter, @identifier=nil, @options={:unless=>nil, :if=>nil, :only=>#<Set: {"show"}>}>] did_not_yield.'
  69. @rails.should parse_line(line).as(:cache_hit)
  70. end
  71. it "should parse a :parameters line correctly" do
  72. line = prefix + ' Parameters: {"action"=>"cached", "controller"=>"cached"}'
  73. @rails.should parse_line(line).as(:parameters).and_capture(:params => {:action => 'cached', :controller => 'cached'})
  74. end
  75. it "should parse a :rendered line correctly" do
  76. line = prefix + 'Rendered layouts/_footer (2.9ms)'
  77. @rails.should parse_line(line).as(:rendered).and_capture(:render_file => 'layouts/_footer', :render_duration => 0.0029)
  78. end
  79. it "should parse a :query_executed line with colors" do
  80. line = prefix + ' User Load (0.4ms) SELECT * FROM `users` WHERE (`users`.`id` = 18205844) '
  81. @rails.should parse_line(line).as(:query_executed).and_capture(:query_class => 'User', :query_duration => 0.0004, :query_sql => 'SELECT * FROM users WHERE (users.id = :int)')
  82. end
  83. it "should parse a :query_executed line without colors" do
  84. line = prefix + ' User Load (0.4ms) SELECT * FROM `users` WHERE (`users`.`id` = 18205844) '
  85. @rails.should parse_line(line).as(:query_executed).and_capture(:query_class => 'User', :query_duration => 0.0004, :query_sql => 'SELECT * FROM users WHERE (users.id = :int)')
  86. end
  87. it "should parse a :query_cached line with colors" do
  88. line = prefix + ' CACHE (0.0ms) SELECT * FROM `users` WHERE (`users`.`id` = 0) '
  89. @rails.should parse_line(line).as(:query_cached).and_capture(:cached_duration => 0.0, :cached_sql => 'SELECT * FROM users WHERE (users.id = :int)')
  90. end
  91. it "should parse a :query_cached line without colors" do
  92. line = prefix + ' CACHE (0.0ms) SELECT * FROM `users` WHERE (`users`.`id` = 0) '
  93. @rails.should parse_line(line).as(:query_cached).and_capture(:cached_duration => 0.0, :cached_sql => 'SELECT * FROM users WHERE (users.id = :int)')
  94. end
  95. it "should not parse an unsupported line" do
  96. line = prefix + 'nonsense line that should not be parsed as anything'
  97. @rails.should_not parse_line(line)
  98. end
  99. end
  100. end
  101. end
  102. describe '#parse_io' do
  103. before(:each) do
  104. @log_parser = RequestLogAnalyzer::Source::LogParser.new(
  105. RequestLogAnalyzer::FileFormat.load(:rails), :parse_strategy => 'cautious')
  106. end
  107. it "should parse a Rails 2.1 style log and find valid Rails requests without warnings" do
  108. request_counter.should_receive(:hit!).exactly(4).times
  109. @log_parser.should_not_receive(:warn)
  110. @log_parser.parse_file(log_fixture(:rails_1x)) do |request|
  111. request_counter.hit! if request.kind_of?(RequestLogAnalyzer::FileFormat::Rails::Request) && request.completed?
  112. end
  113. end
  114. it "should parse a Rails 2.2 style log and find valid Rails requests without warnings" do
  115. request_counter.should_receive(:hit!).once
  116. @log_parser.should_not_receive(:warn)
  117. @log_parser.parse_file(log_fixture(:rails_22)) do |request|
  118. request_counter.hit! if request.kind_of?(RequestLogAnalyzer::FileFormat::Rails::Request) && request.completed?
  119. end
  120. end
  121. it "should parse a Rails SyslogLogger file with prefix and find valid requests without warnings" do
  122. request_counter.should_receive(:hit!).once
  123. @log_parser.should_not_receive(:warn)
  124. @log_parser.parse_file(log_fixture(:syslog_1x)) do |request|
  125. request_counter.hit! if request.kind_of?(RequestLogAnalyzer::FileFormat::Rails::Request) && request.completed?
  126. end
  127. end
  128. it "should parse cached requests" do
  129. @log_parser.should_not_receive(:warn)
  130. @log_parser.parse_file(log_fixture(:rails_22_cached)) do |request|
  131. request.should be_completed
  132. request =~ :cache_hit
  133. end
  134. end
  135. it "should detect unordered requests in the logs" do
  136. @log_parser.should_not_receive(:handle_request)
  137. @log_parser.should_receive(:warn).with(:unclosed_request, anything).once
  138. @log_parser.should_receive(:warn).with(:no_current_request, anything).twice
  139. @log_parser.parse_file(log_fixture(:rails_unordered))
  140. end
  141. end
  142. end