appsignal 2.5.0.alpha.1-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (211) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +33 -0
  3. data/.rspec +4 -0
  4. data/.rubocop.yml +66 -0
  5. data/.rubocop_todo.yml +124 -0
  6. data/.travis.yml +72 -0
  7. data/.yardopts +8 -0
  8. data/CHANGELOG.md +639 -0
  9. data/Gemfile +3 -0
  10. data/LICENSE +20 -0
  11. data/README.md +264 -0
  12. data/Rakefile +214 -0
  13. data/appsignal.gemspec +42 -0
  14. data/benchmark.rake +77 -0
  15. data/bin/appsignal +13 -0
  16. data/ext/Rakefile +27 -0
  17. data/ext/agent.yml +64 -0
  18. data/ext/appsignal_extension.c +692 -0
  19. data/ext/base.rb +79 -0
  20. data/ext/extconf.rb +35 -0
  21. data/gemfiles/capistrano2.gemfile +7 -0
  22. data/gemfiles/capistrano3.gemfile +7 -0
  23. data/gemfiles/grape.gemfile +7 -0
  24. data/gemfiles/no_dependencies.gemfile +5 -0
  25. data/gemfiles/padrino.gemfile +7 -0
  26. data/gemfiles/que.gemfile +5 -0
  27. data/gemfiles/rails-3.2.gemfile +6 -0
  28. data/gemfiles/rails-4.0.gemfile +6 -0
  29. data/gemfiles/rails-4.1.gemfile +6 -0
  30. data/gemfiles/rails-4.2.gemfile +10 -0
  31. data/gemfiles/rails-5.0.gemfile +5 -0
  32. data/gemfiles/rails-5.1.gemfile +5 -0
  33. data/gemfiles/resque.gemfile +12 -0
  34. data/gemfiles/sequel-435.gemfile +11 -0
  35. data/gemfiles/sequel.gemfile +11 -0
  36. data/gemfiles/sinatra.gemfile +6 -0
  37. data/gemfiles/webmachine.gemfile +5 -0
  38. data/lib/appsignal.rb +804 -0
  39. data/lib/appsignal/auth_check.rb +65 -0
  40. data/lib/appsignal/capistrano.rb +10 -0
  41. data/lib/appsignal/cli.rb +108 -0
  42. data/lib/appsignal/cli/demo.rb +63 -0
  43. data/lib/appsignal/cli/diagnose.rb +500 -0
  44. data/lib/appsignal/cli/helpers.rb +72 -0
  45. data/lib/appsignal/cli/install.rb +277 -0
  46. data/lib/appsignal/cli/notify_of_deploy.rb +113 -0
  47. data/lib/appsignal/config.rb +287 -0
  48. data/lib/appsignal/demo.rb +107 -0
  49. data/lib/appsignal/event_formatter.rb +74 -0
  50. data/lib/appsignal/event_formatter/action_view/render_formatter.rb +24 -0
  51. data/lib/appsignal/event_formatter/active_record/instantiation_formatter.rb +14 -0
  52. data/lib/appsignal/event_formatter/active_record/sql_formatter.rb +14 -0
  53. data/lib/appsignal/event_formatter/elastic_search/search_formatter.rb +32 -0
  54. data/lib/appsignal/event_formatter/faraday/request_formatter.rb +19 -0
  55. data/lib/appsignal/event_formatter/mongo_ruby_driver/query_formatter.rb +89 -0
  56. data/lib/appsignal/event_formatter/moped/query_formatter.rb +80 -0
  57. data/lib/appsignal/extension.rb +63 -0
  58. data/lib/appsignal/extension/jruby.rb +460 -0
  59. data/lib/appsignal/garbage_collection_profiler.rb +48 -0
  60. data/lib/appsignal/hooks.rb +105 -0
  61. data/lib/appsignal/hooks/action_cable.rb +113 -0
  62. data/lib/appsignal/hooks/active_support_notifications.rb +52 -0
  63. data/lib/appsignal/hooks/celluloid.rb +30 -0
  64. data/lib/appsignal/hooks/data_mapper.rb +18 -0
  65. data/lib/appsignal/hooks/delayed_job.rb +19 -0
  66. data/lib/appsignal/hooks/mongo_ruby_driver.rb +21 -0
  67. data/lib/appsignal/hooks/net_http.rb +29 -0
  68. data/lib/appsignal/hooks/passenger.rb +22 -0
  69. data/lib/appsignal/hooks/puma.rb +35 -0
  70. data/lib/appsignal/hooks/que.rb +21 -0
  71. data/lib/appsignal/hooks/rake.rb +39 -0
  72. data/lib/appsignal/hooks/redis.rb +30 -0
  73. data/lib/appsignal/hooks/sequel.rb +60 -0
  74. data/lib/appsignal/hooks/shoryuken.rb +43 -0
  75. data/lib/appsignal/hooks/sidekiq.rb +144 -0
  76. data/lib/appsignal/hooks/unicorn.rb +40 -0
  77. data/lib/appsignal/hooks/webmachine.rb +23 -0
  78. data/lib/appsignal/integrations/capistrano/appsignal.cap +39 -0
  79. data/lib/appsignal/integrations/capistrano/capistrano_2_tasks.rb +52 -0
  80. data/lib/appsignal/integrations/data_mapper.rb +33 -0
  81. data/lib/appsignal/integrations/delayed_job_plugin.rb +54 -0
  82. data/lib/appsignal/integrations/grape.rb +53 -0
  83. data/lib/appsignal/integrations/mongo_ruby_driver.rb +55 -0
  84. data/lib/appsignal/integrations/object.rb +35 -0
  85. data/lib/appsignal/integrations/padrino.rb +84 -0
  86. data/lib/appsignal/integrations/que.rb +43 -0
  87. data/lib/appsignal/integrations/railtie.rb +41 -0
  88. data/lib/appsignal/integrations/rake.rb +2 -0
  89. data/lib/appsignal/integrations/resque.rb +20 -0
  90. data/lib/appsignal/integrations/resque_active_job.rb +30 -0
  91. data/lib/appsignal/integrations/sinatra.rb +17 -0
  92. data/lib/appsignal/integrations/webmachine.rb +38 -0
  93. data/lib/appsignal/js_exception_transaction.rb +54 -0
  94. data/lib/appsignal/marker.rb +63 -0
  95. data/lib/appsignal/minutely.rb +42 -0
  96. data/lib/appsignal/rack/generic_instrumentation.rb +49 -0
  97. data/lib/appsignal/rack/js_exception_catcher.rb +70 -0
  98. data/lib/appsignal/rack/rails_instrumentation.rb +51 -0
  99. data/lib/appsignal/rack/sinatra_instrumentation.rb +99 -0
  100. data/lib/appsignal/rack/streaming_listener.rb +73 -0
  101. data/lib/appsignal/system.rb +81 -0
  102. data/lib/appsignal/transaction.rb +498 -0
  103. data/lib/appsignal/transmitter.rb +107 -0
  104. data/lib/appsignal/utils.rb +127 -0
  105. data/lib/appsignal/utils/params_sanitizer.rb +59 -0
  106. data/lib/appsignal/utils/query_params_sanitizer.rb +55 -0
  107. data/lib/appsignal/version.rb +3 -0
  108. data/lib/sequel/extensions/appsignal_integration.rb +3 -0
  109. data/resources/appsignal.yml.erb +39 -0
  110. data/resources/cacert.pem +3866 -0
  111. data/spec/.rubocop.yml +7 -0
  112. data/spec/lib/appsignal/auth_check_spec.rb +80 -0
  113. data/spec/lib/appsignal/capistrano2_spec.rb +224 -0
  114. data/spec/lib/appsignal/capistrano3_spec.rb +237 -0
  115. data/spec/lib/appsignal/cli/demo_spec.rb +67 -0
  116. data/spec/lib/appsignal/cli/diagnose_spec.rb +988 -0
  117. data/spec/lib/appsignal/cli/helpers_spec.rb +171 -0
  118. data/spec/lib/appsignal/cli/install_spec.rb +632 -0
  119. data/spec/lib/appsignal/cli/notify_of_deploy_spec.rb +168 -0
  120. data/spec/lib/appsignal/cli_spec.rb +56 -0
  121. data/spec/lib/appsignal/config_spec.rb +637 -0
  122. data/spec/lib/appsignal/demo_spec.rb +87 -0
  123. data/spec/lib/appsignal/event_formatter/action_view/render_formatter_spec.rb +44 -0
  124. data/spec/lib/appsignal/event_formatter/active_record/instantiation_formatter_spec.rb +21 -0
  125. data/spec/lib/appsignal/event_formatter/active_record/sql_formatter_spec.rb +21 -0
  126. data/spec/lib/appsignal/event_formatter/elastic_search/search_formatter_spec.rb +52 -0
  127. data/spec/lib/appsignal/event_formatter/faraday/request_formatter_spec.rb +21 -0
  128. data/spec/lib/appsignal/event_formatter/mongo_ruby_driver/query_formatter_spec.rb +113 -0
  129. data/spec/lib/appsignal/event_formatter/moped/query_formatter_spec.rb +112 -0
  130. data/spec/lib/appsignal/event_formatter_spec.rb +100 -0
  131. data/spec/lib/appsignal/extension/jruby_spec.rb +43 -0
  132. data/spec/lib/appsignal/extension_spec.rb +137 -0
  133. data/spec/lib/appsignal/garbage_collection_profiler_spec.rb +66 -0
  134. data/spec/lib/appsignal/hooks/action_cable_spec.rb +370 -0
  135. data/spec/lib/appsignal/hooks/active_support_notifications_spec.rb +92 -0
  136. data/spec/lib/appsignal/hooks/celluloid_spec.rb +35 -0
  137. data/spec/lib/appsignal/hooks/data_mapper_spec.rb +39 -0
  138. data/spec/lib/appsignal/hooks/delayed_job_spec.rb +358 -0
  139. data/spec/lib/appsignal/hooks/mongo_ruby_driver_spec.rb +44 -0
  140. data/spec/lib/appsignal/hooks/net_http_spec.rb +53 -0
  141. data/spec/lib/appsignal/hooks/passenger_spec.rb +30 -0
  142. data/spec/lib/appsignal/hooks/puma_spec.rb +80 -0
  143. data/spec/lib/appsignal/hooks/que_spec.rb +19 -0
  144. data/spec/lib/appsignal/hooks/rake_spec.rb +73 -0
  145. data/spec/lib/appsignal/hooks/redis_spec.rb +55 -0
  146. data/spec/lib/appsignal/hooks/sequel_spec.rb +46 -0
  147. data/spec/lib/appsignal/hooks/shoryuken_spec.rb +192 -0
  148. data/spec/lib/appsignal/hooks/sidekiq_spec.rb +419 -0
  149. data/spec/lib/appsignal/hooks/unicorn_spec.rb +52 -0
  150. data/spec/lib/appsignal/hooks/webmachine_spec.rb +35 -0
  151. data/spec/lib/appsignal/hooks_spec.rb +195 -0
  152. data/spec/lib/appsignal/integrations/data_mapper_spec.rb +65 -0
  153. data/spec/lib/appsignal/integrations/grape_spec.rb +225 -0
  154. data/spec/lib/appsignal/integrations/mongo_ruby_driver_spec.rb +127 -0
  155. data/spec/lib/appsignal/integrations/object_spec.rb +249 -0
  156. data/spec/lib/appsignal/integrations/padrino_spec.rb +323 -0
  157. data/spec/lib/appsignal/integrations/que_spec.rb +174 -0
  158. data/spec/lib/appsignal/integrations/railtie_spec.rb +129 -0
  159. data/spec/lib/appsignal/integrations/resque_active_job_spec.rb +83 -0
  160. data/spec/lib/appsignal/integrations/resque_spec.rb +92 -0
  161. data/spec/lib/appsignal/integrations/sinatra_spec.rb +73 -0
  162. data/spec/lib/appsignal/integrations/webmachine_spec.rb +69 -0
  163. data/spec/lib/appsignal/js_exception_transaction_spec.rb +128 -0
  164. data/spec/lib/appsignal/marker_spec.rb +51 -0
  165. data/spec/lib/appsignal/minutely_spec.rb +50 -0
  166. data/spec/lib/appsignal/rack/generic_instrumentation_spec.rb +90 -0
  167. data/spec/lib/appsignal/rack/js_exception_catcher_spec.rb +147 -0
  168. data/spec/lib/appsignal/rack/rails_instrumentation_spec.rb +117 -0
  169. data/spec/lib/appsignal/rack/sinatra_instrumentation_spec.rb +213 -0
  170. data/spec/lib/appsignal/rack/streaming_listener_spec.rb +161 -0
  171. data/spec/lib/appsignal/system_spec.rb +131 -0
  172. data/spec/lib/appsignal/transaction_spec.rb +1146 -0
  173. data/spec/lib/appsignal/transmitter_spec.rb +152 -0
  174. data/spec/lib/appsignal/utils/params_sanitizer_spec.rb +136 -0
  175. data/spec/lib/appsignal/utils/query_params_sanitizer_spec.rb +192 -0
  176. data/spec/lib/appsignal/utils_spec.rb +150 -0
  177. data/spec/lib/appsignal_spec.rb +1049 -0
  178. data/spec/spec_helper.rb +116 -0
  179. data/spec/support/fixtures/containers/cgroups/docker +14 -0
  180. data/spec/support/fixtures/containers/cgroups/docker_systemd +8 -0
  181. data/spec/support/fixtures/containers/cgroups/lxc +10 -0
  182. data/spec/support/fixtures/containers/cgroups/no_permission +0 -0
  183. data/spec/support/fixtures/containers/cgroups/none +1 -0
  184. data/spec/support/fixtures/generated_config.yml +24 -0
  185. data/spec/support/fixtures/uploaded_file.txt +0 -0
  186. data/spec/support/helpers/api_request_helper.rb +19 -0
  187. data/spec/support/helpers/cli_helpers.rb +26 -0
  188. data/spec/support/helpers/config_helpers.rb +21 -0
  189. data/spec/support/helpers/dependency_helper.rb +73 -0
  190. data/spec/support/helpers/directory_helper.rb +27 -0
  191. data/spec/support/helpers/env_helpers.rb +33 -0
  192. data/spec/support/helpers/example_exception.rb +13 -0
  193. data/spec/support/helpers/example_standard_error.rb +13 -0
  194. data/spec/support/helpers/log_helpers.rb +22 -0
  195. data/spec/support/helpers/std_streams_helper.rb +66 -0
  196. data/spec/support/helpers/system_helpers.rb +8 -0
  197. data/spec/support/helpers/time_helpers.rb +11 -0
  198. data/spec/support/helpers/transaction_helpers.rb +37 -0
  199. data/spec/support/matchers/contains_log.rb +7 -0
  200. data/spec/support/mocks/fake_gc_profiler.rb +19 -0
  201. data/spec/support/mocks/mock_extension.rb +6 -0
  202. data/spec/support/project_fixture/config/application.rb +0 -0
  203. data/spec/support/project_fixture/config/appsignal.yml +32 -0
  204. data/spec/support/project_fixture/config/environments/development.rb +0 -0
  205. data/spec/support/project_fixture/config/environments/production.rb +0 -0
  206. data/spec/support/project_fixture/config/environments/test.rb +0 -0
  207. data/spec/support/project_fixture/log/.gitkeep +0 -0
  208. data/spec/support/rails/my_app.rb +6 -0
  209. data/spec/support/shared_examples/instrument.rb +43 -0
  210. data/spec/support/stubs/delayed_job.rb +0 -0
  211. metadata +483 -0
@@ -0,0 +1,7 @@
1
+ inherit_from: ../.rubocop.yml
2
+
3
+ Metrics/BlockLength:
4
+ Enabled: false
5
+
6
+ # Metrics/LineLength:
7
+ # Max: 100
@@ -0,0 +1,80 @@
1
+ describe Appsignal::AuthCheck do
2
+ let(:config) { project_fixture_config }
3
+ let(:auth_check) { Appsignal::AuthCheck.new(config) }
4
+ let(:auth_url) do
5
+ query = {
6
+ :api_key => config[:push_api_key],
7
+ :environment => config.env,
8
+ :gem_version => Appsignal::VERSION,
9
+ :hostname => config[:hostname],
10
+ :name => config[:name]
11
+ }.map { |k, v| "#{k}=#{v}" }.join("&")
12
+
13
+ URI(config[:endpoint]).tap do |uri|
14
+ uri.path = "/1/auth"
15
+ uri.query = query
16
+ end.to_s
17
+ end
18
+ let(:stubbed_request) do
19
+ WebMock.stub_request(:post, auth_url).with(:body => "{}")
20
+ end
21
+
22
+ describe "#perform" do
23
+ subject { auth_check.perform }
24
+
25
+ context "when performing a request against the push api" do
26
+ before { stubbed_request.to_return(:status => 200) }
27
+
28
+ it "returns status code" do
29
+ is_expected.to eq("200")
30
+ end
31
+ end
32
+
33
+ context "when encountering an exception" do
34
+ before { stubbed_request.to_timeout }
35
+
36
+ it "raises an error" do
37
+ expect { subject }.to raise_error(Net::OpenTimeout)
38
+ end
39
+ end
40
+ end
41
+
42
+ describe "#perform_with_result" do
43
+ subject { auth_check.perform_with_result }
44
+
45
+ context "when successful response" do
46
+ before { stubbed_request.to_return(:status => 200) }
47
+
48
+ it "returns success tuple" do
49
+ is_expected.to eq ["200", "AppSignal has confirmed authorization!"]
50
+ end
51
+ end
52
+
53
+ context "when unauthorized response" do
54
+ before { stubbed_request.to_return(:status => 401) }
55
+
56
+ it "returns unauthorirzed tuple" do
57
+ is_expected.to eq ["401", "API key not valid with AppSignal..."]
58
+ end
59
+ end
60
+
61
+ context "when unrecognized response" do
62
+ before { stubbed_request.to_return(:status => 500) }
63
+
64
+ it "returns an error tuple" do
65
+ is_expected.to eq ["500", "Could not confirm authorization: 500"]
66
+ end
67
+ end
68
+
69
+ context "when encountering an exception" do
70
+ before { stubbed_request.to_timeout }
71
+
72
+ it "returns an error tuple" do
73
+ is_expected.to eq [
74
+ nil,
75
+ "Something went wrong while trying to authenticate with AppSignal: execution expired"
76
+ ]
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,224 @@
1
+ if DependencyHelper.capistrano2_present?
2
+ require "capistrano"
3
+ require "capistrano/configuration"
4
+ require "appsignal/capistrano"
5
+
6
+ describe "Capistrano 2 integration" do
7
+ let(:out_stream) { std_stream }
8
+ let(:output) { out_stream.read }
9
+ let(:config) { project_fixture_config }
10
+ let(:capistrano_config) do
11
+ Capistrano::Configuration.new.tap do |c|
12
+ c.set(:rails_env, "production")
13
+ c.set(:repository, "master")
14
+ c.set(:deploy_to, "/home/username/app")
15
+ c.set(:current_release, "")
16
+ c.set(:current_revision, "503ce0923ed177a3ce000005")
17
+ c.dry_run = false
18
+ end
19
+ end
20
+ before { Appsignal::Capistrano.tasks(capistrano_config) }
21
+
22
+ def run
23
+ capture_stdout(out_stream) do
24
+ capistrano_config.find_and_execute_task("appsignal:deploy")
25
+ end
26
+ end
27
+
28
+ it "has a deploy task" do
29
+ expect(capistrano_config.find_task("appsignal:deploy")).to_not be_nil
30
+ end
31
+
32
+ describe "appsignal:deploy task" do
33
+ before do
34
+ ENV["USER"] = "batman"
35
+ ENV["PWD"] = project_fixture_path
36
+ end
37
+
38
+ context "config" do
39
+ before do
40
+ capistrano_config.dry_run = true
41
+ end
42
+
43
+ context "when appsignal_config is available" do
44
+ before do
45
+ capistrano_config.set(
46
+ :appsignal_config,
47
+ :name => "AppName",
48
+ :active => true,
49
+ :push_api_key => "abc"
50
+ )
51
+ end
52
+
53
+ it "overrides the default config with the custom appsignal_config" do
54
+ original_new = Appsignal::Marker.method(:new)
55
+
56
+ expect(Appsignal::Marker).to receive(:new) do |data, given_config|
57
+ expect(given_config[:name]).to eq("AppName")
58
+ original_new.call(data, given_config)
59
+ end
60
+
61
+ run
62
+ end
63
+
64
+ context "when rack_env is used instead of rails_env" do
65
+ before do
66
+ capistrano_config.unset(:rails_env)
67
+ capistrano_config.set(:rack_env, "rack_production")
68
+ end
69
+
70
+ it "uses the rack_env as the env" do
71
+ original_new = Appsignal::Marker.method(:new)
72
+
73
+ expect(Appsignal::Marker).to receive(:new) do |data, given_config|
74
+ expect(given_config.env).to eq("rack_production")
75
+ original_new.call(data, given_config)
76
+ end
77
+
78
+ run
79
+ end
80
+ end
81
+
82
+ context "when stage is used instead of rack_env / rails_env" do
83
+ before do
84
+ capistrano_config.unset(:rails_env)
85
+ capistrano_config.set(:stage, "stage_production")
86
+ end
87
+
88
+ it "uses the stage as the env" do
89
+ original_new = Appsignal::Marker.method(:new)
90
+
91
+ expect(Appsignal::Marker).to receive(:new) do |data, given_config|
92
+ expect(given_config.env).to eq("stage_production")
93
+ original_new.call(data, given_config)
94
+ end
95
+
96
+ run
97
+ end
98
+ end
99
+
100
+ context "when appsignal_env is set" do
101
+ before do
102
+ capistrano_config.set(:rack_env, "rack_production")
103
+ capistrano_config.set(:stage, "stage_production")
104
+ capistrano_config.set(:appsignal_env, "appsignal_production")
105
+ end
106
+
107
+ it "uses the appsignal_env as the env" do
108
+ original_new = Appsignal::Marker.method(:new)
109
+
110
+ expect(Appsignal::Marker).to receive(:new) do |data, given_config|
111
+ expect(given_config.env).to eq("appsignal_production")
112
+ original_new.call(data, given_config)
113
+ end
114
+
115
+ run
116
+ end
117
+ end
118
+
119
+ context "with invalid config" do
120
+ before do
121
+ capistrano_config.set(:appsignal_config, :push_api_key => nil)
122
+ end
123
+
124
+ it "does not continue with invalid config" do
125
+ run
126
+ expect(output).to include \
127
+ "Not notifying of deploy, config is not active for environment: production"
128
+ end
129
+ end
130
+ end
131
+ end
132
+
133
+ describe "markers" do
134
+ def stub_marker_request(data = {})
135
+ stub_api_request config, "markers", marker_data.merge(data)
136
+ end
137
+
138
+ let(:marker_data) do
139
+ {
140
+ :revision => "503ce0923ed177a3ce000005",
141
+ :user => "batman"
142
+ }
143
+ end
144
+
145
+ context "when active for this environment" do
146
+ it "transmits marker" do
147
+ stub_marker_request.to_return(:status => 200)
148
+ run
149
+
150
+ expect(output).to include \
151
+ "Notifying AppSignal of deploy with: revision: 503ce0923ed177a3ce000005, user: batman",
152
+ "AppSignal has been notified of this deploy!"
153
+ end
154
+
155
+ context "with overridden revision" do
156
+ before do
157
+ capistrano_config.set(:appsignal_revision, "abc123")
158
+ stub_marker_request(:revision => "abc123").to_return(:status => 200)
159
+ run
160
+ end
161
+
162
+ it "transmits the overriden revision" do
163
+ expect(output).to include \
164
+ "Notifying AppSignal of deploy with: revision: abc123, user: batman",
165
+ "AppSignal has been notified of this deploy!"
166
+ end
167
+ end
168
+
169
+ context "with overridden deploy user" do
170
+ before do
171
+ capistrano_config.set(:appsignal_user, "robin")
172
+ stub_marker_request(:user => "robin").to_return(:status => 200)
173
+ run
174
+ end
175
+
176
+ it "transmits the overriden deploy user" do
177
+ expect(output).to include \
178
+ "Notifying AppSignal of deploy with: revision: 503ce0923ed177a3ce000005, user: robin",
179
+ "AppSignal has been notified of this deploy!"
180
+ end
181
+ end
182
+
183
+ context "with failed request" do
184
+ before do
185
+ stub_marker_request.to_return(:status => 500)
186
+ run
187
+ end
188
+
189
+ it "does not transmit marker" do
190
+ expect(output).to include \
191
+ "Notifying AppSignal of deploy with: revision: 503ce0923ed177a3ce000005, user: batman",
192
+ "Something went wrong while trying to notify AppSignal:"
193
+ expect(output).to_not include "AppSignal has been notified of this deploy!"
194
+ end
195
+ end
196
+
197
+ context "when dry run" do
198
+ before do
199
+ capistrano_config.dry_run = true
200
+ run
201
+ end
202
+
203
+ it "does not transmit marker" do
204
+ expect(output).to include \
205
+ "Dry run: AppSignal deploy marker not actually sent."
206
+ end
207
+ end
208
+ end
209
+
210
+ context "when not active for this environment" do
211
+ before do
212
+ capistrano_config.set(:rails_env, "nonsense")
213
+ run
214
+ end
215
+
216
+ it "does not transmit marker" do
217
+ expect(output).to include \
218
+ "Not notifying of deploy, config is not active for environment: nonsense"
219
+ end
220
+ end
221
+ end
222
+ end
223
+ end
224
+ end
@@ -0,0 +1,237 @@
1
+ if DependencyHelper.capistrano3_present?
2
+ require "capistrano/all"
3
+ require "capistrano/deploy"
4
+ require "appsignal/capistrano"
5
+
6
+ describe "Capistrano 3 integration" do
7
+ let(:capistrano) { Class.new.extend(Capistrano::DSL) }
8
+ let(:config) { project_fixture_config }
9
+ let(:out_stream) { std_stream }
10
+ let(:output) { out_stream.read }
11
+ let(:logger) { Logger.new(out_stream) }
12
+ let!(:capistrano_config) do
13
+ Capistrano::Configuration.reset!
14
+ Capistrano::Configuration.env.tap do |c|
15
+ c.set(:log_level, :error)
16
+ c.set(:logger, logger)
17
+ c.set(:rails_env, "production")
18
+ c.set(:repository, "master")
19
+ c.set(:deploy_to, "/home/username/app")
20
+ c.set(:current_release, "")
21
+ c.set(:current_revision, "503ce0923ed177a3ce000005")
22
+ end
23
+ end
24
+ let(:marker_data) do
25
+ {
26
+ :revision => "503ce0923ed177a3ce000005",
27
+ :user => "batman"
28
+ }
29
+ end
30
+ before { Rake::Task["appsignal:deploy"].reenable }
31
+
32
+ def run
33
+ capture_std_streams(out_stream, out_stream) do
34
+ capistrano.invoke("appsignal:deploy")
35
+ end
36
+ end
37
+
38
+ it "should have a deploy task" do
39
+ expect(Rake::Task.task_defined?("appsignal:deploy")).to be_truthy
40
+ end
41
+
42
+ describe "appsignal:deploy task" do
43
+ before do
44
+ ENV["USER"] = "batman"
45
+ ENV["PWD"] = project_fixture_path
46
+ end
47
+
48
+ context "config" do
49
+ let(:env) { "production" }
50
+ before do
51
+ capistrano_config.set(
52
+ :appsignal_config,
53
+ :name => "AppName",
54
+ :active => true,
55
+ :push_api_key => "abc"
56
+ )
57
+ config[:name] = "AppName"
58
+ config.instance_variable_set(:@env, env)
59
+ stub_marker_request.to_return(:status => 200)
60
+ end
61
+
62
+ context "when rack_env is the only env set" do
63
+ let(:env) { "rack_production" }
64
+ before do
65
+ capistrano_config.delete(:rails_env)
66
+ capistrano_config.set(:rack_env, env)
67
+ end
68
+
69
+ it "uses the rack_env as the env" do
70
+ original_new = Appsignal::Marker.method(:new)
71
+
72
+ expect(Appsignal::Marker).to receive(:new) do |data, given_config|
73
+ expect(given_config.env).to eq("rack_production")
74
+ original_new.call(data, given_config)
75
+ end
76
+
77
+ run
78
+ end
79
+ end
80
+
81
+ context "when stage is set" do
82
+ let(:env) { "stage_production" }
83
+ before do
84
+ capistrano_config.set(:rack_env, "rack_production")
85
+ capistrano_config.set(:stage, env)
86
+ end
87
+
88
+ it "prefers the Capistrano stage rather than rails_env and rack_env" do
89
+ original_new = Appsignal::Marker.method(:new)
90
+
91
+ expect(Appsignal::Marker).to receive(:new) do |data, given_config|
92
+ expect(given_config.env).to eq("stage_production")
93
+ original_new.call(data, given_config)
94
+ end
95
+
96
+ run
97
+ end
98
+ end
99
+
100
+ context "when `appsignal_config` is set" do
101
+ before do
102
+ ENV["APPSIGNAL_APP_NAME"] = "EnvName"
103
+ capistrano_config.set(:appsignal_config, :name => "AppName")
104
+ config[:name] = "AppName"
105
+ end
106
+
107
+ it "overrides the default config with the custom appsignal_config" do
108
+ original_new = Appsignal::Marker.method(:new)
109
+
110
+ expect(Appsignal::Marker).to receive(:new) do |data, given_config|
111
+ expect(given_config[:name]).to eq("AppName")
112
+ original_new.call(data, given_config)
113
+ end
114
+
115
+ run
116
+ end
117
+
118
+ context "with invalid config" do
119
+ before do
120
+ capistrano_config.set(:appsignal_config, :push_api_key => nil)
121
+ end
122
+
123
+ it "does not continue with invalid config" do
124
+ run
125
+ expect(output).to include \
126
+ "Not notifying of deploy, config is not active for environment: production"
127
+ end
128
+ end
129
+ end
130
+
131
+ context "when `appsignal_env` is set" do
132
+ let(:env) { "appsignal_production" }
133
+ before do
134
+ capistrano_config.set(:rack_env, "rack_production")
135
+ capistrano_config.set(:stage, "stage_production")
136
+ capistrano_config.set(:appsignal_env, env)
137
+ end
138
+
139
+ it "prefers the appsignal_env rather than stage, rails_env and rack_env" do
140
+ original_new = Appsignal::Marker.method(:new)
141
+
142
+ expect(Appsignal::Marker).to receive(:new) do |data, given_config|
143
+ expect(given_config.env).to eq("appsignal_production")
144
+ original_new.call(data, given_config)
145
+ end
146
+
147
+ run
148
+ end
149
+ end
150
+ end
151
+
152
+ describe "markers" do
153
+ context "when active for this environment" do
154
+ it "transmits marker" do
155
+ stub_marker_request.to_return(:status => 200)
156
+ run
157
+
158
+ expect(output).to include \
159
+ "Notifying AppSignal of deploy with: revision: 503ce0923ed177a3ce000005, user: batman",
160
+ "AppSignal has been notified of this deploy!"
161
+ end
162
+
163
+ context "with overridden revision" do
164
+ before do
165
+ capistrano_config.set(:appsignal_revision, "abc123")
166
+ stub_marker_request(:revision => "abc123").to_return(:status => 200)
167
+ run
168
+ end
169
+
170
+ it "transmits the overriden revision" do
171
+ expect(output).to include \
172
+ "Notifying AppSignal of deploy with: revision: abc123, user: batman",
173
+ "AppSignal has been notified of this deploy!"
174
+ end
175
+ end
176
+
177
+ context "with overridden deploy user" do
178
+ before do
179
+ capistrano_config.set(:appsignal_user, "robin")
180
+ stub_marker_request(:user => "robin").to_return(:status => 200)
181
+ run
182
+ end
183
+
184
+ it "transmits the overriden deploy user" do
185
+ expect(output).to include \
186
+ "Notifying AppSignal of deploy with: revision: 503ce0923ed177a3ce000005, user: robin",
187
+ "AppSignal has been notified of this deploy!"
188
+ end
189
+ end
190
+
191
+ if Gem::Version.new(Capistrano::VERSION) >= Gem::Version.new("3.5.0")
192
+ context "when dry run" do
193
+ before do
194
+ expect(capistrano_config).to receive(:dry_run?).and_return(true)
195
+ run
196
+ end
197
+
198
+ it "does not transmit the marker" do
199
+ expect(output).to include "Dry run: AppSignal deploy marker not actually sent."
200
+ end
201
+ end
202
+ end
203
+
204
+ context "with failed request" do
205
+ before do
206
+ stub_marker_request.to_return(:status => 500)
207
+ run
208
+ end
209
+
210
+ it "does not transmit marker" do
211
+ expect(output).to include \
212
+ "Notifying AppSignal of deploy with: revision: 503ce0923ed177a3ce000005, user: batman",
213
+ "Something went wrong while trying to notify AppSignal:"
214
+ expect(output).to_not include "AppSignal has been notified of this deploy!"
215
+ end
216
+ end
217
+ end
218
+
219
+ context "when not active for this environment" do
220
+ before do
221
+ capistrano_config.set(:rails_env, "nonsense")
222
+ run
223
+ end
224
+
225
+ it "should not send deploy marker" do
226
+ expect(output).to include \
227
+ "Not notifying of deploy, config is not active for environment: nonsense"
228
+ end
229
+ end
230
+ end
231
+ end
232
+ end
233
+
234
+ def stub_marker_request(data = {})
235
+ stub_api_request config, "markers", marker_data.merge(data)
236
+ end
237
+ end