factorylabs-newrelic_rpm 2.10.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (160) hide show
  1. data/CHANGELOG +354 -0
  2. data/LICENSE +37 -0
  3. data/README-2.10.2.2 +10 -0
  4. data/README.md +138 -0
  5. data/bin/mongrel_rpm +33 -0
  6. data/bin/newrelic_cmd +4 -0
  7. data/cert/cacert.pem +34 -0
  8. data/install.rb +45 -0
  9. data/lib/new_relic/agent.rb +315 -0
  10. data/lib/new_relic/agent/agent.rb +647 -0
  11. data/lib/new_relic/agent/busy_calculator.rb +86 -0
  12. data/lib/new_relic/agent/chained_call.rb +13 -0
  13. data/lib/new_relic/agent/collection_helper.rb +66 -0
  14. data/lib/new_relic/agent/error_collector.rb +117 -0
  15. data/lib/new_relic/agent/instrumentation/active_merchant.rb +18 -0
  16. data/lib/new_relic/agent/instrumentation/active_record_instrumentation.rb +91 -0
  17. data/lib/new_relic/agent/instrumentation/authlogic.rb +8 -0
  18. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +389 -0
  19. data/lib/new_relic/agent/instrumentation/data_mapper.rb +90 -0
  20. data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +12 -0
  21. data/lib/new_relic/agent/instrumentation/memcache.rb +24 -0
  22. data/lib/new_relic/agent/instrumentation/merb/controller.rb +26 -0
  23. data/lib/new_relic/agent/instrumentation/merb/errors.rb +8 -0
  24. data/lib/new_relic/agent/instrumentation/metric_frame.rb +199 -0
  25. data/lib/new_relic/agent/instrumentation/net.rb +21 -0
  26. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +20 -0
  27. data/lib/new_relic/agent/instrumentation/rack.rb +109 -0
  28. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +59 -0
  29. data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +27 -0
  30. data/lib/new_relic/agent/instrumentation/rails/errors.rb +24 -0
  31. data/lib/new_relic/agent/instrumentation/sinatra.rb +39 -0
  32. data/lib/new_relic/agent/method_tracer.rb +348 -0
  33. data/lib/new_relic/agent/patch_const_missing.rb +125 -0
  34. data/lib/new_relic/agent/sampler.rb +46 -0
  35. data/lib/new_relic/agent/samplers/cpu_sampler.rb +50 -0
  36. data/lib/new_relic/agent/samplers/memory_sampler.rb +141 -0
  37. data/lib/new_relic/agent/samplers/mongrel_sampler.rb +23 -0
  38. data/lib/new_relic/agent/samplers/object_sampler.rb +24 -0
  39. data/lib/new_relic/agent/shim_agent.rb +21 -0
  40. data/lib/new_relic/agent/stats_engine.rb +22 -0
  41. data/lib/new_relic/agent/stats_engine/metric_stats.rb +116 -0
  42. data/lib/new_relic/agent/stats_engine/samplers.rb +74 -0
  43. data/lib/new_relic/agent/stats_engine/transactions.rb +154 -0
  44. data/lib/new_relic/agent/transaction_sampler.rb +315 -0
  45. data/lib/new_relic/agent/worker_loop.rb +118 -0
  46. data/lib/new_relic/commands/deployments.rb +145 -0
  47. data/lib/new_relic/commands/new_relic_commands.rb +30 -0
  48. data/lib/new_relic/control.rb +484 -0
  49. data/lib/new_relic/control/external.rb +13 -0
  50. data/lib/new_relic/control/merb.rb +24 -0
  51. data/lib/new_relic/control/rails.rb +151 -0
  52. data/lib/new_relic/control/ruby.rb +36 -0
  53. data/lib/new_relic/control/sinatra.rb +14 -0
  54. data/lib/new_relic/histogram.rb +89 -0
  55. data/lib/new_relic/local_environment.rb +299 -0
  56. data/lib/new_relic/merbtasks.rb +6 -0
  57. data/lib/new_relic/metric_data.rb +42 -0
  58. data/lib/new_relic/metric_parser.rb +124 -0
  59. data/lib/new_relic/metric_parser/action_mailer.rb +9 -0
  60. data/lib/new_relic/metric_parser/active_merchant.rb +26 -0
  61. data/lib/new_relic/metric_parser/active_record.rb +25 -0
  62. data/lib/new_relic/metric_parser/controller.rb +54 -0
  63. data/lib/new_relic/metric_parser/controller_cpu.rb +38 -0
  64. data/lib/new_relic/metric_parser/errors.rb +6 -0
  65. data/lib/new_relic/metric_parser/external.rb +50 -0
  66. data/lib/new_relic/metric_parser/mem_cache.rb +12 -0
  67. data/lib/new_relic/metric_parser/other_transaction.rb +15 -0
  68. data/lib/new_relic/metric_parser/view.rb +61 -0
  69. data/lib/new_relic/metric_parser/web_frontend.rb +14 -0
  70. data/lib/new_relic/metric_parser/web_service.rb +9 -0
  71. data/lib/new_relic/metric_spec.rb +67 -0
  72. data/lib/new_relic/metrics.rb +7 -0
  73. data/lib/new_relic/noticed_error.rb +23 -0
  74. data/lib/new_relic/rack/metric_app.rb +56 -0
  75. data/lib/new_relic/rack/mongrel_rpm.ru +25 -0
  76. data/lib/new_relic/rack/newrelic.yml +26 -0
  77. data/lib/new_relic/rack_app.rb +5 -0
  78. data/lib/new_relic/recipes.rb +82 -0
  79. data/lib/new_relic/stats.rb +362 -0
  80. data/lib/new_relic/transaction_analysis.rb +121 -0
  81. data/lib/new_relic/transaction_sample.rb +671 -0
  82. data/lib/new_relic/version.rb +54 -0
  83. data/lib/new_relic_api.rb +276 -0
  84. data/lib/newrelic_rpm.rb +45 -0
  85. data/lib/tasks/all.rb +4 -0
  86. data/lib/tasks/install.rake +7 -0
  87. data/lib/tasks/tests.rake +15 -0
  88. data/newrelic.yml +227 -0
  89. data/newrelic_rpm.gemspec +214 -0
  90. data/recipes/newrelic.rb +6 -0
  91. data/test/active_record_fixtures.rb +55 -0
  92. data/test/config/newrelic.yml +46 -0
  93. data/test/config/test_control.rb +38 -0
  94. data/test/new_relic/agent/active_record_instrumentation_test.rb +268 -0
  95. data/test/new_relic/agent/agent_controller_test.rb +254 -0
  96. data/test/new_relic/agent/agent_test_controller.rb +78 -0
  97. data/test/new_relic/agent/busy_calculator_test.rb +79 -0
  98. data/test/new_relic/agent/classloader_patch_test.rb +56 -0
  99. data/test/new_relic/agent/collection_helper_test.rb +125 -0
  100. data/test/new_relic/agent/error_collector_test.rb +173 -0
  101. data/test/new_relic/agent/method_tracer_test.rb +340 -0
  102. data/test/new_relic/agent/metric_data_test.rb +56 -0
  103. data/test/new_relic/agent/mock_ar_connection.rb +40 -0
  104. data/test/new_relic/agent/mock_scope_listener.rb +23 -0
  105. data/test/new_relic/agent/net_instrumentation_test.rb +63 -0
  106. data/test/new_relic/agent/rpm_agent_test.rb +125 -0
  107. data/test/new_relic/agent/stats_engine/metric_stats_test.rb +79 -0
  108. data/test/new_relic/agent/stats_engine/samplers_test.rb +88 -0
  109. data/test/new_relic/agent/stats_engine/stats_engine_test.rb +184 -0
  110. data/test/new_relic/agent/task_instrumentation_test.rb +177 -0
  111. data/test/new_relic/agent/testable_agent.rb +13 -0
  112. data/test/new_relic/agent/transaction_sample_builder_test.rb +195 -0
  113. data/test/new_relic/agent/transaction_sample_test.rb +186 -0
  114. data/test/new_relic/agent/transaction_sampler_test.rb +404 -0
  115. data/test/new_relic/agent/worker_loop_test.rb +103 -0
  116. data/test/new_relic/control_test.rb +110 -0
  117. data/test/new_relic/delayed_job_test.rb +108 -0
  118. data/test/new_relic/deployments_api_test.rb +68 -0
  119. data/test/new_relic/environment_test.rb +75 -0
  120. data/test/new_relic/metric_parser_test.rb +172 -0
  121. data/test/new_relic/metric_spec_test.rb +177 -0
  122. data/test/new_relic/shim_agent_test.rb +9 -0
  123. data/test/new_relic/stats_test.rb +291 -0
  124. data/test/new_relic/version_number_test.rb +76 -0
  125. data/test/test_helper.rb +53 -0
  126. data/test/ui/newrelic_controller_test.rb +14 -0
  127. data/test/ui/newrelic_helper_test.rb +53 -0
  128. data/ui/controllers/newrelic_controller.rb +220 -0
  129. data/ui/helpers/google_pie_chart.rb +49 -0
  130. data/ui/helpers/newrelic_helper.rb +320 -0
  131. data/ui/views/layouts/newrelic_default.rhtml +47 -0
  132. data/ui/views/newrelic/_explain_plans.rhtml +27 -0
  133. data/ui/views/newrelic/_sample.rhtml +19 -0
  134. data/ui/views/newrelic/_segment.rhtml +28 -0
  135. data/ui/views/newrelic/_segment_limit_message.rhtml +1 -0
  136. data/ui/views/newrelic/_segment_row.rhtml +14 -0
  137. data/ui/views/newrelic/_show_sample_detail.rhtml +24 -0
  138. data/ui/views/newrelic/_show_sample_sql.rhtml +20 -0
  139. data/ui/views/newrelic/_show_sample_summary.rhtml +3 -0
  140. data/ui/views/newrelic/_sql_row.rhtml +11 -0
  141. data/ui/views/newrelic/_stack_trace.rhtml +30 -0
  142. data/ui/views/newrelic/_table.rhtml +12 -0
  143. data/ui/views/newrelic/explain_sql.rhtml +42 -0
  144. data/ui/views/newrelic/images/arrow-close.png +0 -0
  145. data/ui/views/newrelic/images/arrow-open.png +0 -0
  146. data/ui/views/newrelic/images/blue_bar.gif +0 -0
  147. data/ui/views/newrelic/images/file_icon.png +0 -0
  148. data/ui/views/newrelic/images/gray_bar.gif +0 -0
  149. data/ui/views/newrelic/images/new-relic-rpm-desktop.gif +0 -0
  150. data/ui/views/newrelic/images/new_relic_rpm_desktop.gif +0 -0
  151. data/ui/views/newrelic/images/textmate.png +0 -0
  152. data/ui/views/newrelic/index.rhtml +57 -0
  153. data/ui/views/newrelic/javascript/prototype-scriptaculous.js +7288 -0
  154. data/ui/views/newrelic/javascript/transaction_sample.js +107 -0
  155. data/ui/views/newrelic/sample_not_found.rhtml +2 -0
  156. data/ui/views/newrelic/show_sample.rhtml +80 -0
  157. data/ui/views/newrelic/show_source.rhtml +3 -0
  158. data/ui/views/newrelic/stylesheets/style.css +484 -0
  159. data/ui/views/newrelic/threads.rhtml +52 -0
  160. metadata +238 -0
@@ -0,0 +1,103 @@
1
+ require 'test/unit'
2
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','test_helper'))
3
+
4
+ NewRelic::Agent::WorkerLoop.class_eval do
5
+ public :run_next_task
6
+ end
7
+
8
+ class NewRelic::Agent::WorkerLoopTest < Test::Unit::TestCase
9
+ def setup
10
+ @log = ""
11
+ @logger = Logger.new(StringIO.new(@log))
12
+ @worker_loop = NewRelic::Agent::WorkerLoop.new(@logger)
13
+ @test_start_time = Time.now
14
+ end
15
+ def test_add_task
16
+ @x = false
17
+ period = 1.0
18
+ @worker_loop.add_task(period) do
19
+ @x = true
20
+ end
21
+
22
+ assert !@x
23
+ @worker_loop.run_next_task
24
+ assert @x
25
+ check_test_timestamp period
26
+ end
27
+
28
+ def test_add_tasks_with_different_periods
29
+ @last_executed = nil
30
+
31
+ period1 = 0.5
32
+ period2 = 0.7
33
+
34
+ @worker_loop.add_task(period1) do
35
+ @last_executed = 1
36
+ end
37
+
38
+ @worker_loop.add_task(period2) do
39
+ @last_executed = 2
40
+ end
41
+
42
+ @worker_loop.run_next_task
43
+ assert_equal @last_executed, 1
44
+ check_test_timestamp(0.5)
45
+
46
+ @worker_loop.run_next_task
47
+ assert_equal @last_executed, 2
48
+ check_test_timestamp(0.7)
49
+
50
+ @worker_loop.run_next_task
51
+ assert_equal @last_executed, 1
52
+ check_test_timestamp(1.0)
53
+
54
+ @worker_loop.run_next_task
55
+ assert_equal @last_executed, 2
56
+ check_test_timestamp(1.4)
57
+
58
+ @worker_loop.run_next_task
59
+ assert_equal @last_executed, 1
60
+ check_test_timestamp(1.5)
61
+ end
62
+
63
+ def test_task_error__standard
64
+ @worker_loop.add_task(0.2) do
65
+ raise "Standard Error Test"
66
+ end
67
+ # Should not throw
68
+ @logger.expects(:error).once
69
+ @logger.expects(:debug).never
70
+ @worker_loop.run_next_task
71
+
72
+ end
73
+ def test_task_error__runtime
74
+ @worker_loop.add_task(0.2) do
75
+ raise RuntimeError, "Runtime Error Test"
76
+ end
77
+ # Should not throw, but log at error level
78
+ # because it detects no agent listener inthe
79
+ # stack
80
+ @logger.expects(:error).once
81
+ @logger.expects(:debug).never
82
+ @worker_loop.run_next_task
83
+ end
84
+
85
+ def test_task_error__server
86
+ @worker_loop.add_task(0.2) do
87
+ raise NewRelic::Agent::ServerError, "Runtime Error Test"
88
+ end
89
+ # Should not throw
90
+ @logger.expects(:error).never
91
+ @logger.expects(:debug).once
92
+ @worker_loop.run_next_task
93
+ end
94
+
95
+ private
96
+ # The test is expected to have lasted no less than expected
97
+ # and no more than expected + 100 ms.
98
+ def check_test_timestamp(expected)
99
+ ts = Time.now - @test_start_time
100
+ delta = ts - expected
101
+ assert(delta <= 0.250, "#{ts} duration includes a delay of #{delta} that exceeds 250 milliseconds")
102
+ end
103
+ end
@@ -0,0 +1,110 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'/../test_helper'))
2
+
3
+ class NewRelic::ControlTest < Test::Unit::TestCase
4
+
5
+ attr_reader :c
6
+
7
+ def setup
8
+ NewRelic::Agent.manual_start
9
+ @c = NewRelic::Control.instance
10
+ end
11
+ def shutdown
12
+ NewRelic::Agent.shutdown
13
+ end
14
+
15
+ def test_monitor_mode
16
+ assert ! @c.monitor_mode?
17
+ @c.settings.delete 'enabled'
18
+ @c.settings.delete 'monitor_mode'
19
+ assert !@c.monitor_mode?
20
+ @c['enabled'] = false
21
+ assert ! @c.monitor_mode?
22
+ @c['enabled'] = true
23
+ assert @c.monitor_mode?
24
+ @c['monitor_mode'] = nil
25
+ assert !@c.monitor_mode?
26
+ @c['monitor_mode'] = false
27
+ assert !@c.monitor_mode?
28
+ @c['monitor_mode'] = true
29
+ assert @c.monitor_mode?
30
+ ensure
31
+ @c['enabled'] = false
32
+ @c['monitor_mode'] = false
33
+ end
34
+
35
+ def test_test_config
36
+ assert_equal :rails, c.app
37
+ assert_equal :test, c.framework
38
+ assert_match /test/i, c.dispatcher_instance_id
39
+ assert_equal nil, c.dispatcher
40
+ assert !c['enabled']
41
+ assert_equal false, c['monitor_mode']
42
+ c.local_env
43
+ end
44
+
45
+ def test_root
46
+ assert File.directory?(NewRelic::Control.newrelic_root), NewRelic::Control.newrelic_root
47
+ assert File.directory?(File.join(NewRelic::Control.newrelic_root, "lib")), NewRelic::Control.newrelic_root + "/lib"
48
+ end
49
+
50
+ def test_info
51
+ props = NewRelic::Control.instance.local_env.snapshot
52
+ assert_match /jdbc|postgres|mysql|sqlite/, props.assoc('Database adapter').last
53
+ end
54
+
55
+ def test_resolve_ip
56
+ assert_equal nil, c.send(:convert_to_ip_address, 'localhost')
57
+ assert_equal nil, c.send(:convert_to_ip_address, 'q1239988737.us')
58
+ # This might fail if you don't have a valid, accessible, DNS server
59
+ assert_equal '65.74.177.194', c.send(:convert_to_ip_address, 'rpm.newrelic.com')
60
+ end
61
+ def test_config_yaml_erb
62
+ assert_equal 'heyheyhey', c['erb_value']
63
+ assert_equal '', c['message']
64
+ assert_equal '', c['license_key']
65
+ end
66
+
67
+ def test_config_booleans
68
+ assert_equal c['tval'], true
69
+ assert_equal c['fval'], false
70
+ assert_nil c['not_in_yaml_val']
71
+ assert_equal c['yval'], true
72
+ assert_equal c['sval'], 'sure'
73
+ end
74
+ def test_config_apdex
75
+ assert_equal 1.1, c.apdex_t
76
+ end
77
+ def test_transaction_threshold
78
+ assert_equal 'Apdex_f', c['transaction_tracer']['transaction_threshold']
79
+ assert_equal 4.4, NewRelic::Agent::Agent.instance.instance_variable_get('@slowest_transaction_threshold')
80
+ end
81
+ def test_log_file_name
82
+ assert_match /newrelic_agent.log$/, c.instance_variable_get('@log_file')
83
+ end
84
+
85
+ def test_transaction_threshold__apdex
86
+ forced_start
87
+ assert_equal 'Apdex_f', c['transaction_tracer']['transaction_threshold']
88
+ assert_equal 4.4, NewRelic::Agent::Agent.instance.instance_variable_get('@slowest_transaction_threshold')
89
+ end
90
+
91
+ def test_transaction_threshold__default
92
+
93
+ forced_start :transaction_tracer => {}
94
+ assert_nil c['transaction_tracer']['transaction_threshold']
95
+ assert_equal 2.0, NewRelic::Agent::Agent.instance.instance_variable_get('@slowest_transaction_threshold')
96
+ end
97
+
98
+ def test_transaction_threshold__override
99
+ forced_start :transaction_tracer => { 'transaction_threshold' => 1}
100
+ assert_equal 1, c['transaction_tracer']['transaction_threshold']
101
+ assert_equal 1, NewRelic::Agent::Agent.instance.instance_variable_get('@slowest_transaction_threshold')
102
+ end
103
+ private
104
+ def forced_start overrides = {}
105
+ NewRelic::Agent.manual_start overrides
106
+ # This is to force the agent to start again.
107
+ NewRelic::Agent.instance.stubs(:started?).returns(nil)
108
+ NewRelic::Agent.instance.start
109
+ end
110
+ end
@@ -0,0 +1,108 @@
1
+ if defined? Delayed::Job
2
+ require File.expand_path(File.join(File.dirname(__FILE__),'/../test_helper'))
3
+
4
+ class LongRunningJob
5
+ def perform
6
+ sleep 5
7
+ end
8
+ end
9
+
10
+ class NamedJob
11
+ def display_name
12
+ 'some custom name'
13
+ end
14
+ def perform
15
+ true
16
+ end
17
+ end
18
+
19
+ class DelayedJobTest < Test::Unit::TestCase
20
+ def local_env
21
+ NewRelic::Control.instance.local_env
22
+ end
23
+
24
+ def worker_name
25
+ local_env.dispatcher_instance_id
26
+ end
27
+
28
+ def lock_n_jobs(n=1)
29
+ n.times do
30
+ job = Delayed::Job.create
31
+ job.update_attributes({
32
+ :locked_at => Time.now,
33
+ :locked_by => worker_name
34
+ })
35
+ end
36
+ end
37
+
38
+ def setup
39
+ NewRelic::Agent.manual_start
40
+ @agent = NewRelic::Agent.instance
41
+
42
+ @agent.transaction_sampler.harvest
43
+ @agent.stats_engine.clear_stats
44
+ end
45
+
46
+ def teardown
47
+ @agent.instance_variable_set("@histogram", NewRelic::Histogram.new)
48
+ end
49
+
50
+ def test_job_instrumentation
51
+ job = Delayed::Job.new(:payload_object => LongRunningJob.new)
52
+ job_name = "Controller/Task/Delayed::Job/LongRunningJob"
53
+
54
+ job.invoke_job
55
+ job_stats = @agent.stats_engine.get_stats(job_name)
56
+
57
+ assert @agent.stats_engine.metrics.include?(job_name)
58
+ assert_equal 1, job_stats.call_count
59
+ end
60
+
61
+ def test_custom_name
62
+ job = Delayed::Job.new(:payload_object => NamedJob.new)
63
+ job_name = "Controller/Task/Delayed::Job/some custom name"
64
+
65
+ job.invoke_job
66
+ job_stats = @agent.stats_engine.get_stats(job_name)
67
+
68
+ assert @agent.stats_engine.metrics.include?(job_name)
69
+ assert_equal 1, job_stats.call_count
70
+ end
71
+
72
+ def test_lock_sampler
73
+ stats_engine = NewRelic::Agent::StatsEngine.new
74
+ sampler = NewRelic::Agent::Samplers::DelayedJobLockSampler.new
75
+ sampler.stats_engine = stats_engine
76
+
77
+ lock_n_jobs(1)
78
+ sampler.poll
79
+
80
+ assert_equal 1, sampler.stats.data_point_count
81
+ assert_equal 1, sampler.stats.min_call_time
82
+ assert_equal 1, sampler.stats.max_call_time
83
+
84
+ lock_n_jobs(4)
85
+ sampler.poll
86
+
87
+ assert_equal 2, sampler.stats.data_point_count
88
+ assert_equal 1, sampler.stats.min_call_time
89
+ assert_equal 5, sampler.stats.max_call_time
90
+
91
+ lock_n_jobs(5)
92
+ sampler.poll
93
+ sampler.poll
94
+
95
+ assert_equal 4, sampler.stats.data_point_count
96
+ assert_equal 1, sampler.stats.min_call_time
97
+ assert_equal 10, sampler.stats.max_call_time
98
+
99
+ Delayed::Job.destroy_all
100
+ sampler.poll
101
+
102
+ assert_equal 5, sampler.stats.data_point_count
103
+ assert_equal 0, sampler.stats.min_call_time
104
+ assert_equal 10, sampler.stats.max_call_time
105
+ end
106
+
107
+ end
108
+ end
@@ -0,0 +1,68 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'../../lib/new_relic/commands/deployments'))
2
+ require 'rubygems'
3
+ require 'mocha'
4
+
5
+ class NewRelic::DeploymentsTest < Test::Unit::TestCase
6
+
7
+ def setup
8
+ NewRelic::Commands::Deployments.class_eval do
9
+ attr_accessor :messages, :exit_status, :errors, :revision
10
+ def err(message); @errors = @errors ? @errors + message : message; end
11
+ def info(message); @messages = @messages ? @messages + message : message; end
12
+ def just_exit(status=0); @exit_status ||= status; end
13
+ end
14
+ end
15
+ def teardown
16
+ return unless @deployment
17
+ puts @deployment.errors
18
+ puts @deployment.messages
19
+ puts @deployment.exit_status
20
+ end
21
+ def test_help
22
+ begin
23
+ NewRelic::Commands::Deployments.new "-h"
24
+ fail "should have thrown"
25
+ rescue NewRelic::Commands::CommandFailure => c
26
+ assert_match /^Usage/, c.message
27
+ end
28
+ end
29
+ def test_bad_command
30
+ assert_raise OptionParser::InvalidOption do
31
+ NewRelic::Commands::Deployments.new ["-foo", "bar"]
32
+ end
33
+ end
34
+ def test_interactive
35
+ mock_the_connection
36
+ @deployment = NewRelic::Commands::Deployments.new :appname => 'APP', :revision => 3838, :user => 'Bill', :description => "Some lengthy description"
37
+ assert_nil @deployment.exit_status
38
+ assert_nil @deployment.errors
39
+ assert_equal '3838', @deployment.revision
40
+ @deployment.run
41
+ @deployment = nil
42
+ end
43
+
44
+ def test_command_line_run
45
+ mock_the_connection
46
+ # @mock_response.expects(:body).returns("<xml>deployment</xml>")
47
+ @deployment = NewRelic::Commands::Deployments.new(%w[-a APP -r 3838 --user=Bill] << "Some lengthy description")
48
+ assert_nil @deployment.exit_status
49
+ assert_nil @deployment.errors
50
+ assert_equal '3838', @deployment.revision
51
+ @deployment.run
52
+
53
+ # This should pass because it's a bogus deployment
54
+ #assert_equal 1, @deployment.exit_status
55
+ #assert_match /Unable to upload/, @deployment.errors
56
+
57
+ @deployment = nil
58
+ end
59
+ private
60
+ def mock_the_connection
61
+ mock_connection = mock()
62
+ @mock_response = mock()
63
+ @mock_response.expects(:is_a?).with(Net::HTTPSuccess).returns(true)
64
+ mock_connection.expects(:request).returns(@mock_response)
65
+ NewRelic::Control.instance.stubs(:http_connection).returns(mock_connection)
66
+ end
67
+
68
+ end
@@ -0,0 +1,75 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..', 'test_helper'))
2
+ require "test/unit"
3
+ require "mocha"
4
+ ##require 'new_relic/local_environment'
5
+ class EnvironmentTest < ActiveSupport::TestCase
6
+
7
+ def teardown
8
+ # To remove mock server instances from ObjectSpace
9
+ ObjectSpace.garbage_collect
10
+ super
11
+ end
12
+ class MockOptions
13
+ def fetch (*args)
14
+ 1000
15
+ end
16
+ end
17
+ MOCK_OPTIONS = MockOptions.new
18
+
19
+ def test_environment
20
+ e = NewRelic::LocalEnvironment.new
21
+ assert_equal nil, e.environment
22
+ assert_match /test/i, e.dispatcher_instance_id
23
+ end
24
+ def test_no_webrick
25
+ Object.const_set :OPTIONS, 'foo'
26
+ e = NewRelic::LocalEnvironment.new
27
+ assert_equal nil, e.environment
28
+ assert_match /test/i, e.dispatcher_instance_id
29
+ Object.class_eval { remove_const :OPTIONS }
30
+ end
31
+
32
+ def test_passenger
33
+ class << self
34
+ module ::Passenger
35
+ const_set "AbstractServer", 0
36
+ end
37
+ end
38
+ e = NewRelic::LocalEnvironment.new
39
+ assert_equal :passenger, e.environment
40
+ assert_nil e.dispatcher_instance_id, "dispatcher instance id should be nil: #{e.dispatcher_instance_id}"
41
+
42
+ NewRelic::Control.instance.instance_eval do
43
+ @settings['app_name'] = 'myapp'
44
+ end
45
+
46
+ e = NewRelic::LocalEnvironment.new
47
+ assert_equal :passenger, e.environment
48
+ assert_nil e.dispatcher_instance_id
49
+
50
+ ::Passenger.class_eval { remove_const :AbstractServer }
51
+ end
52
+ def test_snapshot
53
+ e = NewRelic::LocalEnvironment.new
54
+ s = e.snapshot
55
+ assert_equal 0, s.size
56
+ e.gather_environment_info
57
+ s = e.snapshot
58
+ assert_match /1\.(8\.[67]|9\.\d)/, s.assoc('Ruby version').last, s.inspect
59
+ assert_equal 'test', s.assoc('Framework').last, s.inspect
60
+ # Make sure the processor count is determined on linux systems
61
+ if File.exists? '/proc/cpuinfo'
62
+ assert s.assoc('Processors').last.to_i > 0
63
+ end
64
+ end
65
+
66
+
67
+ def test_default_port
68
+ e = NewRelic::LocalEnvironment.new
69
+ assert_equal 3000, e.send(:default_port)
70
+ ARGV.push '--port=3121'
71
+ assert_equal '3121', e.send(:default_port)
72
+ ARGV.pop
73
+ end
74
+
75
+ end