exception_handling 2.6.1 → 2.7.0.pre.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (33) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -1
  3. data/.jenkins/Jenkinsfile +8 -24
  4. data/CHANGELOG.md +8 -1
  5. data/Gemfile +4 -4
  6. data/Gemfile.lock +19 -29
  7. data/Rakefile +6 -7
  8. data/lib/exception_handling/log_stub_error.rb +1 -2
  9. data/lib/exception_handling/logging_methods.rb +33 -0
  10. data/lib/exception_handling/methods.rb +6 -53
  11. data/lib/exception_handling/testing.rb +20 -10
  12. data/lib/exception_handling/version.rb +1 -1
  13. data/lib/exception_handling.rb +2 -2
  14. data/{spec → test}/helpers/controller_helpers.rb +0 -0
  15. data/{spec → test}/helpers/exception_helpers.rb +2 -2
  16. data/{spec → test}/rake_test_warning_false.rb +0 -0
  17. data/{spec/spec_helper.rb → test/test_helper.rb} +39 -50
  18. data/test/unit/exception_handling/exception_catalog_test.rb +85 -0
  19. data/test/unit/exception_handling/exception_description_test.rb +82 -0
  20. data/{spec/unit/exception_handling/exception_info_spec.rb → test/unit/exception_handling/exception_info_test.rb} +107 -105
  21. data/{spec/unit/exception_handling/honeybadger_callbacks_spec.rb → test/unit/exception_handling/honeybadger_callbacks_test.rb} +20 -20
  22. data/{spec/unit/exception_handling/log_error_stub_spec.rb → test/unit/exception_handling/log_error_stub_test.rb} +22 -38
  23. data/test/unit/exception_handling/logging_methods_test.rb +37 -0
  24. data/{spec/unit/exception_handling/mailer_spec.rb → test/unit/exception_handling/mailer_test.rb} +17 -17
  25. data/test/unit/exception_handling/methods_test.rb +105 -0
  26. data/test/unit/exception_handling/sensu_test.rb +52 -0
  27. data/{spec/unit/exception_handling_spec.rb → test/unit/exception_handling_test.rb} +329 -325
  28. metadata +36 -34
  29. data/.rspec +0 -3
  30. data/spec/unit/exception_handling/exception_catalog_spec.rb +0 -85
  31. data/spec/unit/exception_handling/exception_description_spec.rb +0 -82
  32. data/spec/unit/exception_handling/methods_spec.rb +0 -84
  33. data/spec/unit/exception_handling/sensu_spec.rb +0 -51
@@ -1,27 +1,27 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require File.expand_path('../../spec_helper', __dir__)
3
+ require File.expand_path('../../test_helper', __dir__)
4
4
 
5
5
  module ExceptionHandling
6
- describe LogErrorStub do
6
+ class LogErrorStubTest < ActiveSupport::TestCase
7
7
 
8
8
  include LogErrorStub
9
9
 
10
10
  context "while running tests" do
11
- before do
11
+ setup do
12
12
  setup_log_error_stub
13
13
  end
14
14
 
15
- after do
15
+ teardown do
16
16
  teardown_log_error_stub
17
17
  end
18
18
 
19
- it "raise an error when log_error and log_warning are called" do
19
+ should "raise an error when log_error and log_warning are called" do
20
20
  begin
21
21
  ExceptionHandling.log_error("Something happened")
22
22
  flunk
23
23
  rescue Exception => ex # LogErrorStub::UnexpectedExceptionLogged => ex
24
- expect(ex.to_s.starts_with?("StandardError: Something happened")).to be_truthy
24
+ assert ex.to_s.starts_with?("StandardError: Something happened"), ex.to_s
25
25
  end
26
26
 
27
27
  begin
@@ -31,16 +31,16 @@ module ExceptionHandling
31
31
  begin
32
32
  ExceptionHandling.log_error(ex)
33
33
  rescue LogErrorStub::UnexpectedExceptionLogged => ex
34
- expect(ex.to_s.starts_with?("RaisedError: This should raise")).to be_truthy
34
+ assert ex.to_s.starts_with?("RaisedError: This should raise"), ex.to_s
35
35
  end
36
36
  end
37
37
  end
38
38
 
39
- it "allow for the regex specification of an expected exception to be ignored" do
39
+ should "allow for the regex specification of an expected exception to be ignored" do
40
40
  exception_pattern = /StandardError: This is a test error/
41
- expect(exception_whitelist).to be_nil # test that exception expectations are cleared
41
+ assert_nil exception_whitelist # test that exception expectations are cleared
42
42
  expects_exception(exception_pattern)
43
- expect(exception_whitelist[0][0]).to eq(exception_pattern)
43
+ assert_equal exception_pattern, exception_whitelist[0][0]
44
44
  begin
45
45
  ExceptionHandling.log_error("This is a test error")
46
46
  rescue StandardError
@@ -48,11 +48,11 @@ module ExceptionHandling
48
48
  end
49
49
  end
50
50
 
51
- it "allow for the string specification of an expected exception to be ignored" do
51
+ should "allow for the string specification of an expected exception to be ignored" do
52
52
  exception_pattern = "StandardError: This is a test error"
53
- expect(exception_whitelist).to be_nil # test that exception expectations are cleared
53
+ assert_nil exception_whitelist # test that exception expectations are cleared
54
54
  expects_exception(exception_pattern)
55
- expect(exception_whitelist[0][0]).to eq(exception_pattern)
55
+ assert_equal exception_pattern, exception_whitelist[0][0]
56
56
  begin
57
57
  ExceptionHandling.log_error("This is a test error")
58
58
  rescue StandardError
@@ -60,9 +60,9 @@ module ExceptionHandling
60
60
  end
61
61
  end
62
62
 
63
- it "allow multiple errors to be ignored" do
63
+ should "allow multiple errors to be ignored" do
64
64
  class IgnoredError < StandardError; end
65
- expect(exception_whitelist).to be_nil # test that exception expectations are cleared
65
+ assert_nil exception_whitelist # test that exception expectations are cleared
66
66
  expects_exception(/StandardError: This is a test error/)
67
67
  expects_exception(/IgnoredError: This should be ignored/)
68
68
  ExceptionHandling.log_error("This is a test error")
@@ -73,7 +73,7 @@ module ExceptionHandling
73
73
  end
74
74
  end
75
75
 
76
- it "expect exception twice if declared twice" do
76
+ should "expect exception twice if declared twice" do
77
77
  expects_exception(/StandardError: ERROR: I love lamp/)
78
78
  expects_exception(/StandardError: ERROR: I love lamp/)
79
79
  ExceptionHandling.log_error("ERROR: I love lamp")
@@ -82,39 +82,23 @@ module ExceptionHandling
82
82
  end
83
83
 
84
84
  context "teardown_log_error_stub" do
85
- before do
86
- RSpec.configure do |config|
87
- config.mock_with :rspec do |mocks|
88
- mocks.verify_partial_doubles = false
89
- end
90
- end
91
- end
92
-
93
- after do
94
- RSpec.configure do |config|
95
- config.mock_with :rspec do |mocks|
96
- mocks.verify_partial_doubles = true
97
- end
98
- end
99
- end
100
-
101
- it "support MiniTest framework for adding a failure" do
85
+ should "support MiniTest framework for adding a failure" do
102
86
  expects_exception(/foo/)
103
87
 
104
- expect(self).to receive(:is_mini_test?) { true }
88
+ mock(self).is_mini_test?.returns { true }
105
89
 
106
- expect(self).to receive(:flunk).with("log_error expected 1 times with pattern: 'foo' found 0")
90
+ mock(self).flunk("log_error expected 1 times with pattern: 'foo' found 0")
107
91
  teardown_log_error_stub
108
92
 
109
93
  self.exception_whitelist = nil
110
94
  end
111
95
 
112
- it "support Test::Unit framework for adding a failure" do
96
+ should "support Test::Unit framework for adding a failure" do
113
97
  expects_exception(/foo/)
114
98
 
115
- expect(self).to receive(:is_mini_test?) { false }
99
+ mock(self).is_mini_test?.returns { false }
116
100
 
117
- expect(self).to receive(:add_failure).with("log_error expected 1 times with pattern: 'foo' found 0")
101
+ mock(self).add_failure("log_error expected 1 times with pattern: 'foo' found 0")
118
102
  teardown_log_error_stub
119
103
 
120
104
  self.exception_whitelist = nil
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../test_helper'
4
+ require_relative '../../helpers/exception_helpers'
5
+
6
+ require "exception_handling/testing"
7
+
8
+ module ExceptionHandling
9
+ class LoggingMethodsTest < ActiveSupport::TestCase
10
+ include ExceptionHelpers
11
+
12
+ def dont_stub_log_error
13
+ true
14
+ end
15
+
16
+ context "ExceptionHandling::LoggingMethods" do
17
+ setup do
18
+ @controller = Testing::LoggingMethodsControllerStub.new
19
+ ExceptionHandling.stub_handler = nil
20
+ end
21
+
22
+ context "#log_warning" do
23
+ should "be available to the controller" do
24
+ klass = Class.new
25
+ klass.include ExceptionHandling::LoggingMethods
26
+ instance = klass.new
27
+ assert instance.methods.include?(:log_warning)
28
+ end
29
+
30
+ should "call ExceptionHandling#log_warning" do
31
+ mock(ExceptionHandling).log_warning("Hi mom")
32
+ @controller.send(:log_warning, "Hi mom")
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -1,19 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require File.expand_path('../../spec_helper', __dir__)
4
- require 'rails-dom-testing'
3
+ require File.expand_path('../../test_helper', __dir__)
5
4
 
6
5
  module ExceptionHandling
7
- describe Mailer do
6
+ class MailerTest < ActionMailer::TestCase
8
7
 
9
8
  include ::Rails::Dom::Testing::Assertions::SelectorAssertions
9
+ tests ExceptionHandling::Mailer
10
10
 
11
11
  def dont_stub_log_error
12
12
  true
13
13
  end
14
14
 
15
15
  context "ExceptionHandling::Mailer" do
16
- before do
16
+ setup do
17
17
  ExceptionHandling.email_environment = 'Test'
18
18
  ExceptionHandling.sender_address = %("Test Exception Mailer" <null_exception@invoca.com>)
19
19
  ExceptionHandling.exception_recipients = ['test_exception@invoca.com']
@@ -21,22 +21,22 @@ module ExceptionHandling
21
21
  end
22
22
 
23
23
  context "log_parser_exception_notification" do
24
- it "send with string" do
24
+ should "send with string" do
25
25
  result = ExceptionHandling::Mailer.log_parser_exception_notification("This is my fake error", "My Fake Subj").deliver_now
26
- expect(result.subject).to eq("Test exception: My Fake Subj: This is my fake error")
27
- expect(result.body.to_s).to match(/This is my fake error/)
26
+ assert_equal "Test exception: My Fake Subj: This is my fake error", result.subject
27
+ assert_match(/This is my fake error/, result.body.to_s)
28
28
  assert_emails 1
29
29
  end
30
30
  end
31
31
 
32
32
  context "escalation_notification" do
33
- before do
33
+ setup do
34
34
  def document_root_element
35
35
  @body_html.root
36
36
  end
37
37
  end
38
38
 
39
- it "send all the information" do
39
+ should "send all the information" do
40
40
  ExceptionHandling.email_environment = 'Staging Full'
41
41
  ExceptionHandling.server_name = 'test-fe3'
42
42
 
@@ -46,8 +46,8 @@ module ExceptionHandling
46
46
  result = ActionMailer::Base.deliveries.last
47
47
  @body_html = Nokogiri::HTML(result.body.to_s)
48
48
  assert_equal_with_diff ['test_escalation@invoca.com'], result.to
49
- expect(result[:from].formatted).to eq(["Test Escalation Mailer <null_escalation@invoca.com>"])
50
- expect(result.subject).to eq("Staging Full Escalation: Your Favorite <b>Feature<b> Failed")
49
+ assert_equal ["Test Escalation Mailer <null_escalation@invoca.com>"], result[:from].formatted
50
+ assert_equal "Staging Full Escalation: Your Favorite <b>Feature<b> Failed", result.subject
51
51
  assert_select "title", "Exception Escalation"
52
52
  assert_select "html" do
53
53
  assert_select "body br", { count: 4 }, result.body.to_s # plus 1 for the multiline summary
@@ -60,13 +60,13 @@ module ExceptionHandling
60
60
  end
61
61
  end
62
62
 
63
- it "use defaults for missing fields" do
63
+ should "use defaults for missing fields" do
64
64
  result = ExceptionHandling::Mailer.escalation_notification("Your Favorite Feature Failed", error_string: "It failed because of an error\n More Info")
65
65
  @body_html = Nokogiri::HTML(result.body.to_s)
66
66
 
67
67
  assert_equal_with_diff ['test_escalation@invoca.com'], result.to
68
- expect(result.from).to eq(["null_escalation@invoca.com"])
69
- expect(result.subject).to eq('Test Escalation: Your Favorite Feature Failed')
68
+ assert_equal ["null_escalation@invoca.com"], result.from
69
+ assert_equal 'Test Escalation: Your Favorite Feature Failed', result.subject
70
70
  assert_select "html" do
71
71
  assert_select "body i", true, result.body.to_s do |is|
72
72
  assert_select is, "i", 'no error #'
@@ -75,11 +75,11 @@ module ExceptionHandling
75
75
  end
76
76
 
77
77
  context "ExceptionHandling.escalate_to_production_support" do
78
- before do
78
+ setup do
79
79
  Time.now_override = Time.parse('1986-5-21 4:17 am UTC')
80
80
  end
81
81
 
82
- it "notify production support" do
82
+ should "notify production support" do
83
83
  subject = "Runtime Error found!"
84
84
  exception = RuntimeError.new("Test")
85
85
  recipients = ["prodsupport@example.com"]
@@ -87,7 +87,7 @@ module ExceptionHandling
87
87
  ExceptionHandling.production_support_recipients = recipients
88
88
  ExceptionHandling.last_exception_timestamp = Time.now.to_i
89
89
 
90
- expect(ExceptionHandling).to receive(:escalate).with(subject, exception, Time.now.to_i, recipients)
90
+ mock(ExceptionHandling).escalate(subject, exception, Time.now.to_i, recipients)
91
91
  ExceptionHandling.escalate_to_production_support(exception, subject)
92
92
  end
93
93
  end
@@ -0,0 +1,105 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../test_helper'
4
+ require_relative '../../helpers/exception_helpers'
5
+
6
+ require "exception_handling/testing"
7
+
8
+ module ExceptionHandling
9
+ class MethodsTest < ActiveSupport::TestCase
10
+ include ExceptionHelpers
11
+
12
+ def dont_stub_log_error
13
+ true
14
+ end
15
+
16
+ context "ExceptionHandling::Methods" do
17
+ setup do
18
+ @controller = Testing::MethodsControllerStub.new
19
+ ExceptionHandling.stub_handler = nil
20
+ end
21
+
22
+ should "set the around filter" do
23
+ assert_equal :set_current_controller, Testing::MethodsControllerStub.around_filter_method
24
+ assert_nil ExceptionHandling.current_controller
25
+ @controller.simulate_around_filter do
26
+ assert_equal @controller, ExceptionHandling.current_controller
27
+ end
28
+ assert_nil ExceptionHandling.current_controller
29
+ end
30
+
31
+ should "use the current_controller when available" do
32
+ capture_notifications
33
+
34
+ mock(ExceptionHandling.logger).fatal(/blah/, anything)
35
+ @controller.simulate_around_filter do
36
+ ExceptionHandling.log_error(ArgumentError.new("blah"))
37
+ assert_equal 1, sent_notifications.size, sent_notifications.inspect
38
+ assert_match(@controller.request.request_uri, sent_notifications.last.enhanced_data['request'].to_s)
39
+ end
40
+ end
41
+
42
+ should "report long running controller action" do
43
+ assert_equal 2, @controller.send(:long_controller_action_timeout)
44
+ mock(ExceptionHandling).log_error(/Long controller action detected in #{@controller.class.name.split("::").last}::test_action/, anything, anything)
45
+ @controller.simulate_around_filter do
46
+ sleep(3)
47
+ end
48
+ end
49
+
50
+ should "not report long running controller actions if it is less than the timeout" do
51
+ assert_equal 2, @controller.send(:long_controller_action_timeout)
52
+ stub(ExceptionHandling).log_error { flunk "Should not timeout" }
53
+ @controller.simulate_around_filter do
54
+ sleep(1)
55
+ end
56
+ end
57
+
58
+ should "default long running controller action(300/30 for test/prod)" do
59
+ class DummyController
60
+ include ExceptionHandling::Methods
61
+ end
62
+
63
+ controller = DummyController.new
64
+
65
+ Rails.env = 'production'
66
+ assert_equal 30, controller.send(:long_controller_action_timeout)
67
+
68
+ Rails.env = 'test'
69
+ assert_equal 300, controller.send(:long_controller_action_timeout)
70
+ end
71
+
72
+ context "#log_warning" do
73
+ should "be available to the controller" do
74
+ assert @controller.methods.include?(:log_warning)
75
+ end
76
+ end
77
+ end
78
+
79
+ context "included deprecation" do
80
+ setup do
81
+ mock_deprecation_3_0
82
+ end
83
+
84
+ should "deprecate when no around_filter in included hook" do
85
+ k = Class.new
86
+ k.include ExceptionHandling::Methods
87
+ end
88
+
89
+ should "deprecate controller around_filter in included hook" do
90
+ controller = Class.new
91
+ class << controller
92
+ def around_filter(*)
93
+ end
94
+ end
95
+ controller.include ExceptionHandling::Methods
96
+ end
97
+ end
98
+
99
+ private
100
+
101
+ def mock_deprecation_3_0
102
+ mock(STDERR).puts(/DEPRECATION WARNING: ExceptionHandling::Methods is deprecated and will be removed from exception_handling 3\.0/)
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require File.expand_path('../../test_helper', __dir__)
4
+
5
+ module ExceptionHandling
6
+ class SensuTest < ActiveSupport::TestCase
7
+ context "#generate_event" do
8
+ should "create an event" do
9
+ mock(ExceptionHandling::Sensu).send_event(name: "world_is_ending", output: "stick head between knees and kiss ass goodbye", status: 1)
10
+
11
+ ExceptionHandling::Sensu.generate_event("world_is_ending", "stick head between knees and kiss ass goodbye")
12
+ end
13
+
14
+ should "add the sensu prefix" do
15
+ ExceptionHandling.sensu_prefix = "cnn_"
16
+
17
+ mock(ExceptionHandling::Sensu).send_event(name: "cnn_world_is_ending", output: "stick head between knees and kiss ass goodbye", status: 1)
18
+
19
+ ExceptionHandling::Sensu.generate_event("world_is_ending", "stick head between knees and kiss ass goodbye")
20
+ end
21
+
22
+ should "allow the level to be set to critical" do
23
+ mock(ExceptionHandling::Sensu).send_event(name: "world_is_ending", output: "stick head between knees and kiss ass goodbye", status: 2)
24
+
25
+ ExceptionHandling::Sensu.generate_event("world_is_ending", "stick head between knees and kiss ass goodbye", :critical)
26
+ end
27
+
28
+ should "error if an invalid level is supplied" do
29
+ dont_allow(ExceptionHandling::Sensu).send_event
30
+
31
+ assert_raise(RuntimeError, "Invalid alert level") do
32
+ ExceptionHandling::Sensu.generate_event("world_is_ending", "stick head between knees and kiss ass goodbye", :hair_on_fire)
33
+ end
34
+ end
35
+ end
36
+
37
+ context "#send_event" do
38
+ setup do
39
+ @event = { name: "world_is_ending", output: "stick head between knees and kiss ass goodbye", status: 1 }
40
+ @socket = SocketStub.new
41
+ end
42
+
43
+ should "send event json to sensu client" do
44
+ mock.any_instance_of(Addrinfo).connect.with_any_args { @socket }
45
+
46
+ ExceptionHandling::Sensu.send_event(@event)
47
+
48
+ assert_equal @event.to_json, @socket.sent.first
49
+ end
50
+ end
51
+ end
52
+ end