newrelic_rpm 3.9.7.266 → 3.9.8.273

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. data.tar.gz.sig +0 -0
  2. data/CHANGELOG +44 -1
  3. data/lib/new_relic/agent.rb +31 -0
  4. data/lib/new_relic/agent/agent.rb +34 -5
  5. data/lib/new_relic/agent/configuration/default_source.rb +25 -1
  6. data/lib/new_relic/agent/configuration/high_security_source.rb +1 -0
  7. data/lib/new_relic/agent/cross_app_monitor.rb +1 -1
  8. data/lib/new_relic/agent/custom_event_aggregator.rb +2 -4
  9. data/lib/new_relic/agent/error_collector.rb +17 -12
  10. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +9 -8
  11. data/lib/new_relic/agent/instrumentation/grape.rb +67 -0
  12. data/lib/new_relic/agent/new_relic_service.rb +93 -43
  13. data/lib/new_relic/agent/pipe_service.rb +4 -0
  14. data/lib/new_relic/agent/synthetics_event_buffer.rb +42 -0
  15. data/lib/new_relic/agent/system_info.rb +44 -18
  16. data/lib/new_relic/agent/transaction_event_aggregator.rb +9 -2
  17. data/lib/new_relic/agent/utilization_data.rb +77 -0
  18. data/lib/new_relic/agent/vm/mri_vm.rb +3 -3
  19. data/lib/new_relic/rack/agent_hooks.rb +15 -15
  20. data/lib/new_relic/recipes/capistrano3.rb +2 -2
  21. data/lib/new_relic/version.rb +1 -1
  22. data/newrelic_rpm.gemspec +0 -1
  23. data/test/agent_helper.rb +13 -3
  24. data/test/environments/lib/environments/runner.rb +3 -19
  25. data/test/environments/rails42/Gemfile +5 -1
  26. data/test/fixtures/cross_agent_tests/README.md +1 -1
  27. data/test/fixtures/cross_agent_tests/cat_map.json +154 -88
  28. data/test/fixtures/cross_agent_tests/docker_container_id/cases.json +30 -6
  29. data/test/fixtures/cross_agent_tests/docker_container_id/docker-1.1.2-lxc-driver.txt +10 -0
  30. data/test/fixtures/cross_agent_tests/docker_container_id/docker-1.1.2-native-driver-fs.txt +10 -0
  31. data/test/fixtures/cross_agent_tests/docker_container_id/docker-1.1.2-native-driver-systemd.txt +10 -0
  32. data/test/fixtures/cross_agent_tests/docker_container_id/heroku.txt +1 -0
  33. data/test/fixtures/cross_agent_tests/docker_container_id/ubuntu-14.04-lxc-container.txt +10 -0
  34. data/test/fixtures/cross_agent_tests/docker_container_id/{lxc-containers-without-docker.txt → ubuntu-14.04-no-container.txt} +0 -0
  35. data/test/fixtures/cross_agent_tests/docker_container_id/ubuntu-14.10-no-container.txt +10 -0
  36. data/test/multiverse/lib/multiverse/runner.rb +1 -0
  37. data/test/multiverse/lib/multiverse/suite.rb +6 -2
  38. data/test/multiverse/suites/active_record/.gitignore +1 -0
  39. data/test/multiverse/suites/active_record/Envfile +25 -7
  40. data/test/multiverse/suites/active_record/Rakefile +9 -0
  41. data/test/{new_relic/agent/instrumentation → multiverse/suites/active_record}/active_record_test.rb +82 -88
  42. data/test/multiverse/suites/active_record/app/models/models.rb +27 -0
  43. data/test/multiverse/suites/active_record/ar_method_aliasing.rb +5 -46
  44. data/test/multiverse/suites/active_record/before_suite.rb +23 -0
  45. data/test/multiverse/suites/active_record/config/database.rb +79 -0
  46. data/test/multiverse/suites/active_record/config/database.yml +19 -0
  47. data/test/multiverse/suites/active_record/db/migrate/20141105131800_create_users_and_aliases.rb +21 -0
  48. data/test/multiverse/suites/active_record/db/migrate/20141106082200_create_orders_and_shipments.rb +25 -0
  49. data/test/multiverse/suites/agent_only/cross_application_tracing_test.rb +4 -1
  50. data/test/multiverse/suites/agent_only/custom_analytics_events_test.rb +53 -0
  51. data/test/multiverse/suites/agent_only/keepalive_test.rb +3 -7
  52. data/test/multiverse/suites/agent_only/utilization_data_collection_test.rb +170 -0
  53. data/test/multiverse/suites/grape/Envfile +15 -0
  54. data/test/multiverse/suites/grape/config/newrelic.yml +18 -0
  55. data/test/multiverse/suites/grape/grape_test.rb +60 -0
  56. data/test/multiverse/suites/grape/grape_test_api.rb +43 -0
  57. data/test/multiverse/suites/grape/unsupported_version_test.rb +31 -0
  58. data/test/multiverse/suites/json/Envfile +3 -1
  59. data/test/multiverse/suites/rack/rack_env_mutation_test.rb +54 -0
  60. data/test/multiverse/suites/rails/Envfile +1 -1
  61. data/test/multiverse/suites/rails/view_instrumentation_test.rb +2 -1
  62. data/test/multiverse/suites/resque/resque_marshalling_test.rb +54 -0
  63. data/test/multiverse/suites/typhoeus/Envfile +4 -4
  64. data/test/new_relic/agent/agent_test.rb +37 -0
  65. data/test/new_relic/agent/configuration/default_source_test.rb +14 -0
  66. data/test/new_relic/agent/custom_event_aggregator_test.rb +3 -3
  67. data/test/new_relic/agent/error_collector/notice_error_test.rb +4 -4
  68. data/test/new_relic/agent/error_collector_test.rb +27 -4
  69. data/test/new_relic/agent/instrumentation/controller_instrumentation_test.rb +23 -0
  70. data/test/new_relic/agent/new_relic_service_test.rb +208 -103
  71. data/test/new_relic/agent/pipe_service_test.rb +7 -0
  72. data/test/new_relic/agent/synthetics_event_buffer_test.rb +54 -0
  73. data/test/new_relic/agent/synthetics_monitor_test.rb +0 -3
  74. data/test/new_relic/agent/system_info_test.rb +6 -6
  75. data/test/new_relic/agent/transaction_event_aggregator_test.rb +43 -2
  76. data/test/new_relic/agent/utilization_data_test.rb +18 -0
  77. data/test/new_relic/collection_helper_test.rb +0 -1
  78. data/test/new_relic/data_container_tests.rb +11 -7
  79. data/test/new_relic/fake_collector.rb +23 -0
  80. data/test/new_relic/fake_instance_metadata_service.rb +45 -0
  81. data/test/new_relic/license_test.rb +2 -0
  82. data/test/new_relic/marshalling_test_cases.rb +89 -4
  83. data/test/new_relic/transaction_sample_test.rb +1 -0
  84. data/test/test_helper.rb +1 -0
  85. metadata +33 -6
  86. metadata.gz.sig +1 -2
  87. data/test/active_record_fixtures.rb +0 -79
  88. data/test/new_relic/rack/all_test.rb +0 -14
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under New Relic's license terms.
3
+ # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
+
5
+ class User < ActiveRecord::Base
6
+ include NewRelic::Agent::MethodTracer
7
+ has_many :aliases
8
+
9
+ add_method_tracer :save!
10
+ add_method_tracer :persisted?
11
+ end
12
+
13
+ class Alias < ActiveRecord::Base
14
+ include NewRelic::Agent::MethodTracer
15
+
16
+ add_method_tracer :save!
17
+ add_method_tracer :persisted?
18
+ add_method_tracer :destroyed?
19
+ end
20
+
21
+ class Order < ActiveRecord::Base
22
+ has_and_belongs_to_many :shipments, :join_table => 'order_shipments'
23
+ end
24
+
25
+ class Shipment < ActiveRecord::Base
26
+ has_and_belongs_to_many :orders, :join_table => 'order_shipments'
27
+ end
@@ -7,10 +7,10 @@ require 'rubygems'
7
7
  require 'active_record'
8
8
  require 'active_support/multibyte'
9
9
 
10
- require 'newrelic_rpm'
11
10
  require 'multiverse/color'
12
11
  require 'multiverse_helpers'
13
- require 'minitest/unit'
12
+
13
+ require File.expand_path(File.join(__FILE__, "..", "app", "models", "models"))
14
14
 
15
15
  class InstrumentActiveRecordMethods < Minitest::Test
16
16
  extend Multiverse::Color
@@ -18,62 +18,21 @@ class InstrumentActiveRecordMethods < Minitest::Test
18
18
  include MultiverseHelpers
19
19
  setup_and_teardown_agent
20
20
 
21
- class User < ActiveRecord::Base
22
- include NewRelic::Agent::MethodTracer
23
- has_many :aliases
24
-
25
- add_method_tracer :save!
26
- add_method_tracer :persisted?
27
- end
28
-
29
- class Alias < ActiveRecord::Base
30
- include NewRelic::Agent::MethodTracer
31
-
32
- add_method_tracer :save!
33
- add_method_tracer :persisted?
34
- add_method_tracer :destroyed?
35
- end
36
-
37
- def after_setup
38
- database_name = "testdb.#{ENV["MULTIVERSE_ENV"]}.sqlite3"
39
- @db_connection = ActiveRecord::Base.establish_connection(:adapter => 'sqlite3',
40
- :database => database_name)
41
- ActiveRecord::Migration.class_eval do
42
- @connection = @db_connection
43
- create_table :users do |t|
44
- t.string :name
45
- end
46
-
47
- create_table :aliases do |t|
48
- t.integer :user_id
49
- t.string :aka
50
- end
51
- end
52
- end
53
-
54
- def after_teardown
55
- ActiveRecord::Migration.class_eval do
56
- @connection = @db_connection
57
- drop_table :users
58
- drop_table :aliases
59
- end
60
- end
61
-
62
21
  def test_basic_creation
63
22
  a_user = User.new :name => "Bob"
64
23
  assert a_user.new_record?
65
24
  a_user.save!
25
+
66
26
  assert User.connected?
67
27
  assert a_user.persisted? if a_user.respond_to?(:persisted?)
68
- assert a_user.id == 1
69
28
  end
70
29
 
71
30
  def test_alias_collection_query_method
72
31
  a_user = User.new :name => "Bob"
73
32
  a_user.save!
74
- a_user = User.find(1)
33
+
34
+ a_user = User.first
75
35
  assert User.connected?
76
- assert a_user.id = 1
77
36
 
78
37
  an_alias = Alias.new :user_id => a_user.id, :aka => "the Blob"
79
38
  assert an_alias.new_record?
@@ -0,0 +1,23 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under New Relic's license terms.
3
+ # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
+
5
+ begin
6
+ load 'Rakefile'
7
+ Rake::Task['db:create'].invoke
8
+ Rake::Task['db:migrate'].invoke
9
+ rescue => e
10
+ puts e
11
+ puts e.backtrace.join("\n\t")
12
+ raise
13
+ end
14
+
15
+ class Minitest::Test
16
+ def after_teardown
17
+ super
18
+ User.delete_all
19
+ Alias.delete_all
20
+ Order.delete_all
21
+ Shipment.delete_all
22
+ end
23
+ end
@@ -0,0 +1,79 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under New Relic's license terms.
3
+ # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
+
5
+ require 'active_record'
6
+ require 'erb'
7
+ require 'newrelic_rpm'
8
+
9
+ DependencyDetection.detect!
10
+
11
+ db_dir = File.expand_path('../../db', __FILE__)
12
+ config_dir = File.expand_path(File.dirname(__FILE__))
13
+
14
+ if defined?(ActiveRecord::VERSION)
15
+ ENV['DATABASE_NAME'] = "multiverse_activerecord_#{ActiveRecord::VERSION::STRING}_#{RUBY_VERSION}".gsub(".", "_")
16
+ else
17
+ ENV['DATABASE_NAME'] = "multiverse_activerecord_2_x_#{ENV["MULTIVERSE_ENV"]}_#{RUBY_VERSION}".gsub(".", "_")
18
+ end
19
+
20
+ config_raw = File.read(File.join(config_dir, 'database.yml'))
21
+ config_erb = ERB.new(config_raw).result
22
+ config_yml = YAML.load(config_erb)
23
+
24
+ # Rails 2.x didn't keep the Rails out of ActiveRecord much...
25
+ RAILS_ENV = "test"
26
+ RAILS_ROOT = File.join(db_dir, "..")
27
+
28
+ ActiveRecord::Base.configurations = config_yml
29
+ ActiveRecord::Base.establish_connection :test
30
+ ActiveRecord::Base.logger = Logger.new(ENV["VERBOSE"] ? STDOUT : StringIO.new)
31
+
32
+ begin
33
+ load 'active_record/railties/databases.rake'
34
+ rescue LoadError, StandardError
35
+ load 'tasks/databases.rake'
36
+ end
37
+
38
+ if defined?(ActiveRecord::Tasks)
39
+ include ActiveRecord::Tasks
40
+
41
+ module Seeder
42
+ def self.load_seed
43
+ # Nope
44
+ end
45
+ end
46
+
47
+ DatabaseTasks.env = "test"
48
+ DatabaseTasks.db_dir = db_dir
49
+ DatabaseTasks.migrations_paths = File.join(db_dir, 'migrate')
50
+ DatabaseTasks.database_configuration = config_yml
51
+ DatabaseTasks.seed_loader = Seeder
52
+ else
53
+ # Hattip to https://github.com/janko-m/sinatra-activerecord/blob/master/lib/sinatra/activerecord/rake/activerecord_3.rb
54
+ module Rails
55
+ extend self
56
+
57
+ def root
58
+ Pathname.new(Rake.application.original_dir)
59
+ end
60
+
61
+ def env
62
+ ActiveSupport::StringInquirer.new(ENV["RACK_ENV"] || "development")
63
+ end
64
+
65
+ def application
66
+ seed_loader = Object.new
67
+ seed_loader.instance_eval do
68
+ def load_seed
69
+ # Nope
70
+ end
71
+ end
72
+ seed_loader
73
+ end
74
+ end
75
+
76
+ Rake::Task.define_task("db:environment")
77
+ Rake::Task["db:load_config"].clear if Rake::Task.task_defined? "db:load_config"
78
+ Rake::Task.define_task("db:rails_env")
79
+ end
@@ -0,0 +1,19 @@
1
+ mysql: &mysql
2
+ <% if defined?(JRuby) %>
3
+ adapter: jdbcmysql
4
+ <% else %>
5
+ adapter: mysql
6
+ <% end %>
7
+ socket: <%= (`uname -s` =~ /Linux/ ) ? "" :"/tmp/mysql.sock" %>
8
+ username: root
9
+ host: localhost
10
+ database: <%= db = "#{ENV['DATABASE_NAME']}"; db.empty? ? "rails_blog" : db %>
11
+
12
+ development:
13
+ <<: *mysql
14
+
15
+ test:
16
+ <<: *mysql
17
+
18
+ production:
19
+ <<: *mysql
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under New Relic's license terms.
3
+ # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
+
5
+ class CreateUsersAndAliases < ActiveRecord::Migration
6
+ def self.up
7
+ create_table :users do |t|
8
+ t.string :name
9
+ end
10
+
11
+ create_table :aliases do |t|
12
+ t.integer :user_id
13
+ t.string :aka
14
+ end
15
+ end
16
+
17
+ def self.down
18
+ drop_table :users
19
+ drop_table :aliases
20
+ end
21
+ end
@@ -0,0 +1,25 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under New Relic's license terms.
3
+ # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
+
5
+ class CreateOrdersAndShipments < ActiveRecord::Migration
6
+ def self.up
7
+ create_table :orders do |t|
8
+ t.string :name
9
+ end
10
+
11
+ create_table :shipments, :force => true do |t|
12
+ end
13
+
14
+ create_table :order_shipments, :force => true, :id => false do |t|
15
+ t.integer :order_id
16
+ t.integer :shipment_id, :integer
17
+ end
18
+ end
19
+
20
+ def self.down
21
+ drop_table :orders
22
+ drop_table :shipments
23
+ drop_table :order_shipments
24
+ end
25
+ end
@@ -70,7 +70,10 @@ class CrossApplicationTracingTest < Minitest::Test
70
70
  end
71
71
 
72
72
  define_method("test_#{test_case['name']}") do
73
- txn_category, txn_name = test_case['transactionName'].split('/')
73
+ txn_name_parts = test_case['transactionName'].split('/')
74
+ txn_category = txn_name_parts[0..1].join('/')
75
+ txn_name = txn_name_parts[2..-1].join('/')
76
+
74
77
  request_params = {
75
78
  'transaction_name' => txn_name,
76
79
  'transaction_category' => txn_category,
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under New Relic's license terms.
3
+ # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
+
5
+ require 'multiverse_helpers'
6
+
7
+ class CustomAnalyticsEventsTest < Minitest::Test
8
+ include MultiverseHelpers
9
+
10
+ setup_and_teardown_agent
11
+
12
+ def test_custom_analytics_events_are_submitted
13
+ t0 = freeze_time
14
+ NewRelic::Agent.record_custom_event(:DummyType, :foo => :bar, :baz => :qux)
15
+
16
+ NewRelic::Agent.agent.send(:harvest_and_send_analytic_event_data)
17
+ events = last_custom_event_submission
18
+
19
+ expected_event = [{'type' => 'DummyType', 'timestamp' => t0.to_i },
20
+ {'foo' => 'bar', 'baz' => 'qux'}]
21
+ assert_equal(expected_event, events.first)
22
+ end
23
+
24
+ def test_record_custom_event_returns_falsy_if_event_was_dropped
25
+ max_samples = NewRelic::Agent.config[:'custom_insights_events.max_samples_stored']
26
+ max_samples.times do
27
+ NewRelic::Agent.record_custom_event(:DummyType, :foo => :bar)
28
+ end
29
+
30
+ result = NewRelic::Agent.record_custom_event(:DummyType, :foo => :bar)
31
+ refute(result)
32
+ end
33
+
34
+ def test_record_doesnt_record_if_invalid_event_type
35
+ bad_event_type = 'bad$news'
36
+ good_event_type = 'good news'
37
+
38
+ NewRelic::Agent.record_custom_event(bad_event_type, :foo => :bar)
39
+ NewRelic::Agent.record_custom_event(good_event_type, :foo => :bar)
40
+
41
+ NewRelic::Agent.agent.send(:harvest_and_send_analytic_event_data)
42
+ events = last_custom_event_submission
43
+
44
+ assert_equal(1, events.size)
45
+ assert_equal(good_event_type, events.first[0]['type'])
46
+ end
47
+
48
+ def last_custom_event_submission
49
+ submissions = $collector.calls_for('custom_event_data')
50
+ assert_equal(1, submissions.size)
51
+ submissions.first.events
52
+ end
53
+ end
@@ -13,17 +13,13 @@ class KeepaliveTest < Minitest::Test
13
13
 
14
14
  NewRelic::Agent.agent.send(:transmit_data)
15
15
 
16
- # This is the closest I can easily get to closing the underlying TCP
17
- # connection from the server side in between harvests.
18
- conn0 = NewRelic::Agent.agent.service.http_connection
19
- conn0.instance_variable_get(:@socket).close
16
+ # This is simulating the remote peer closing the TCP connection between
17
+ # harvest cycles.
18
+ $collector.last_socket.close
20
19
 
21
20
  NewRelic::Agent.agent.send(:transmit_data)
22
21
 
23
- conn1 = NewRelic::Agent.agent.service.http_connection
24
-
25
22
  metric_data_calls = $collector.calls_for('metric_data')
26
23
  assert_equal(2, metric_data_calls.size)
27
- assert_same(conn0, conn1)
28
24
  end
29
25
  end
@@ -0,0 +1,170 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under New Relic's license terms.
3
+ # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
+
5
+ require 'newrelic_rpm'
6
+ require 'multiverse_helpers'
7
+ require 'fake_instance_metadata_service'
8
+
9
+ class UtilizationDataCollectionTest < Minitest::Test
10
+ include MultiverseHelpers
11
+
12
+ setup_and_teardown_agent do
13
+ @config = NewRelic::Agent::Configuration::DottedHash.new({:collect_utilization => true }, true)
14
+ NewRelic::Agent.config.add_config_for_testing(@config, true)
15
+ end
16
+
17
+ def after_teardown
18
+ NewRelic::Agent.config.remove_config(@config)
19
+ end
20
+
21
+ def test_hostname
22
+ NewRelic::Agent::Hostname.stubs(:get).returns("hostile")
23
+ trigger_usage_data_collection_and_submission
24
+
25
+ data = last_submitted_utilization_data
26
+ assert_equal("hostile", data.hostname)
27
+ end
28
+
29
+ def test_gathers_instance_metadata
30
+ instance_type = 'test.type'
31
+
32
+ with_fake_metadata_service do |service|
33
+ service.set_response_for_path('/2008-02-01/meta-data/instance-type', instance_type)
34
+ trigger_usage_data_collection_and_submission
35
+ end
36
+
37
+ data = last_submitted_utilization_data
38
+ assert_equal(instance_type, data.instance_type)
39
+ end
40
+
41
+ def test_omits_instance_metadata_if_contains_invalid_characters
42
+ instance_type = '<script>lol</script>'
43
+
44
+ with_fake_metadata_service do |service|
45
+ service.set_response_for_path('/2008-02-01/meta-data/instance-type', instance_type)
46
+ trigger_usage_data_collection_and_submission
47
+ end
48
+
49
+ data = last_submitted_utilization_data
50
+ assert_nil(data.instance_type)
51
+ end
52
+
53
+ def test_omits_instance_metadata_if_too_long
54
+ instance_type = 'a' * 1024
55
+
56
+ with_fake_metadata_service do |service|
57
+ service.set_response_for_path('/2008-02-01/meta-data/instance-type', instance_type)
58
+ trigger_usage_data_collection_and_submission
59
+ end
60
+
61
+ data = last_submitted_utilization_data
62
+ assert_nil(data.instance_type)
63
+ end
64
+
65
+ def test_gathers_cpu_metadata
66
+ fake_processor_info = { :num_logical_processors => 8 }
67
+ NewRelic::Agent::SystemInfo.stubs(:get_processor_info).returns(fake_processor_info)
68
+
69
+ trigger_usage_data_collection_and_submission
70
+
71
+ data = last_submitted_utilization_data
72
+ assert_equal(fake_processor_info[:num_logical_processors], data.cpu_count)
73
+ end
74
+
75
+ def test_nil_cpu_values_reported
76
+ fake_processor_info = { :num_logical_processors => nil }
77
+ NewRelic::Agent::SystemInfo.stubs(:get_processor_info).returns(fake_processor_info)
78
+
79
+ trigger_usage_data_collection_and_submission
80
+
81
+ data = last_submitted_utilization_data
82
+ assert_nil(data.cpu_count)
83
+ end
84
+
85
+ def test_gathers_docker_container_id
86
+ NewRelic::Agent::SystemInfo.stubs(:docker_container_id).returns("whale")
87
+
88
+ trigger_usage_data_collection_and_submission
89
+
90
+ data = last_submitted_utilization_data
91
+ assert_equal "whale", data.container_id
92
+ end
93
+
94
+ def test_nil_docker_container_id
95
+ NewRelic::Agent::SystemInfo.stubs(:docker_container_id).returns(nil)
96
+
97
+ trigger_usage_data_collection_and_submission
98
+
99
+ data = last_submitted_utilization_data
100
+ assert_nil data.container_id
101
+ end
102
+
103
+ def test_retries_upon_failure_to_submit_usage_data
104
+ $collector.stub_exception('utilization_data', nil, 503).once
105
+
106
+ trigger_usage_data_collection_and_submission
107
+ first_attempt = last_submitted_utilization_data
108
+
109
+ $collector.reset
110
+
111
+ trigger_usage_data_collection_and_submission
112
+ next_attempt = last_submitted_utilization_data
113
+
114
+ assert_equal(first_attempt, next_attempt)
115
+ end
116
+
117
+ def last_submitted_utilization_data
118
+ submissions = $collector.calls_for(:utilization_data)
119
+ assert_equal(1, submissions.size)
120
+
121
+ data = submissions.last
122
+ assert_equal(4, data.body.size)
123
+
124
+ data
125
+ end
126
+
127
+ def trigger_usage_data_collection_and_submission
128
+ agent.send(:transmit_utilization_data)
129
+ end
130
+
131
+ def with_fake_metadata_service
132
+ metadata_service = NewRelic::FakeInstanceMetadataService.new
133
+ metadata_service.run
134
+
135
+ redirect_link_local_address(metadata_service.port)
136
+
137
+ yield metadata_service
138
+ ensure
139
+ metadata_service.stop if metadata_service
140
+ unredirect_link_local_address
141
+ end
142
+
143
+ def redirect_link_local_address(port)
144
+ Net::HTTP.class_exec(port) do |p|
145
+ @dummy_port = p
146
+
147
+ class << self
148
+ def get_with_patch(uri)
149
+ if uri.host == '169.254.169.254'
150
+ uri.host = 'localhost'
151
+ uri.port = @dummy_port
152
+ end
153
+ get_without_patch(uri)
154
+ end
155
+
156
+ alias_method :get_without_patch, :get
157
+ alias_method :get, :get_with_patch
158
+ end
159
+ end
160
+ end
161
+
162
+ def unredirect_link_local_address
163
+ Net::HTTP.class_eval do
164
+ class << self
165
+ alias_method :get, :get_without_patch
166
+ undef_method :get_with_patch
167
+ end
168
+ end
169
+ end
170
+ end