Skip to content

Commit

Permalink
Add tests for update_status
Browse files Browse the repository at this point in the history
  • Loading branch information
kaylareopelle committed Jan 14, 2025
1 parent aab917f commit 5499054
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
28 changes: 28 additions & 0 deletions test/new_relic/agent/agent/start_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,27 @@ def test_check_config_and_start_agent_disabled
check_config_and_start_agent
end

def test_monitoring_false_updates_health_status
with_config(:monitoring => false) do
NewRelic::Agent.agent.health_check.expects(:update_status).with(NewRelic::Agent::HealthCheck::AGENT_DISABLED)
monitoring?
end
end

def test_missing_app_name_updates_health_status
with_config(:app_name => nil) do
NewRelic::Agent.agent.health_check.expects(:update_status).with(NewRelic::Agent::HealthCheck::MISSING_APP_NAME)
agent_should_start?
end
end

def test_missing_license_key_updates_health_status
with_config(:license_key => nil) do
NewRelic::Agent.agent.health_check.expects(:update_status).with(NewRelic::Agent::HealthCheck::MISSING_LICENSE_KEY)
has_license_key?
end
end

def test_check_config_and_start_agent_incorrect_key
self.expects(:monitoring?).returns(true)
self.expects(:has_correct_license_key?).returns(false)
Expand Down Expand Up @@ -193,6 +214,13 @@ def test_correct_license_length_negative
end
end

def test_correct_license_length_negative_updates_health_status
with_config(:license_key => 'a' * 30) do
NewRelic::Agent.agent.health_check.expects(:update_status).with(NewRelic::Agent::HealthCheck::INVALID_LICENSE_KEY)
correct_license_length
end
end

def test_using_forking_dispatcher_positive
with_config(:dispatcher => :passenger) do
assert_predicate self, :using_forking_dispatcher?
Expand Down
47 changes: 47 additions & 0 deletions test/new_relic/agent/agent_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -335,13 +335,23 @@ def test_connect_retries_on_server_connection_exception
@agent.send(:connect)

assert_predicate(@agent, :connected?)

# status should be healthy because the retry successfully connects
assert_equal NewRelic::Agent::HealthCheck::HEALTHY, @agent.health_check.instance_variable_get(:@status)
end

def test_connect_does_not_retry_if_keep_retrying_false
@agent.service.expects(:connect).once.raises(Timeout::Error)
@agent.send(:connect, :keep_retrying => false)
end

def test_agent_health_status_set_to_failed_to_connect
@agent.service.expects(:connect).once.raises(Timeout::Error)
@agent.send(:connect, :keep_retrying => false)

assert_equal NewRelic::Agent::HealthCheck::FAILED_TO_CONNECT, @agent.health_check.instance_variable_get(:@status)
end

def test_connect_does_not_retry_on_license_error
@agent.service.expects(:connect).raises(NewRelic::Agent::LicenseException)
@agent.send(:connect)
Expand Down Expand Up @@ -562,6 +572,14 @@ def test_defer_start_if_no_application_name_configured
assert_match(/No application name configured/i, logmsg)
end

def test_health_status_updated_if_no_app_name_configured
with_config(:app_name => false) do
@agent.start
end

assert_equal NewRelic::Agent::HealthCheck::MISSING_APP_NAME, @agent.health_check.instance_variable_get(:@status)
end

def test_harvest_from_container
container = mock
harvested_items = %w[foo bar baz]
Expand Down Expand Up @@ -750,6 +768,15 @@ def test_force_disconnect_logs_message
refute_empty matching_lines, 'logs should say the agent is disconnecting'
end

def test_force_disconnect_sets_health_status
@agent.instance_variable_set(:@service, nil)
@agent.stubs(:sleep)
error = NewRelic::Agent::ForceDisconnectException.new
@agent.handle_force_disconnect(error)

assert_equal NewRelic::Agent::HealthCheck::FORCED_DISCONNECT, @agent.health_check.instance_variable_get(:@status)
end

def test_discarding_logs_message
@agent.service.stubs(:send).raises(UnrecoverableServerException)

Expand Down Expand Up @@ -801,6 +828,26 @@ def test_exit_connecting_worker_thread

assert worker.join(1), 'Worker thread hang on shutdown'
end

def test_agent_health_status_set_to_shutdown_when_healthy
@agent.setup_and_start_agent

assert_equal NewRelic::Agent::HealthCheck::HEALTHY, @agent.health_check.instance_variable_get(:@status)

@agent.shutdown

assert_equal NewRelic::Agent::HealthCheck::SHUTDOWN, @agent.health_check.instance_variable_get(:@status)
end

def test_agent_health_status_when_not_healthy_is_same_after_shutdown
@agent.setup_and_start_agent

@agent.health_check.instance_variable_set(:@status, NewRelic::Agent::HealthCheck::INVALID_LICENSE_KEY)

@agent.shutdown

assert_equal NewRelic::Agent::HealthCheck::SHUTDOWN, @agent.health_check.instance_variable_get(:@status)
end
end

class AgentStartingTest < Minitest::Test
Expand Down
33 changes: 33 additions & 0 deletions test/new_relic/agent/new_relic_service_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,7 @@ def self.check_status_code_handling(expected_exceptions)
method_name = "test_#{status_code}_raises_#{exception_type.name.split('::').last}"
define_method(method_name) do
@http_handle.respond_to(:metric_data, 'payload', :code => status_code)

assert_raises exception_type do
stats_hash = NewRelic::Agent::StatsHash.new
@service.metric_data(stats_hash)
Expand All @@ -608,6 +609,38 @@ def self.check_status_code_handling(expected_exceptions)
429 => NewRelic::Agent::ServerConnectionException,
431 => NewRelic::Agent::UnrecoverableServerException)

def self.check_agent_health_status_updates(expected_exceptions)
expected_exceptions.each do |status_code|
method_name = "test_#{status_code}_updates_agent_health_check"
define_method(method_name) do
NewRelic::Agent.agent.health_check.instance_variable_set(:@continue, true)
NewRelic::Agent.agent.health_check.instance_variable_set(:@status, NewRelic::Agent::HealthCheck::HEALTHY)

begin
@http_handle.respond_to(:metric_data, 'payload', :code => status_code)
stats_hash = NewRelic::Agent::StatsHash.new
@service.metric_data(stats_hash)
rescue
# no-op, raise the error
end

expected = {
healthy: false,
last_error: 'NR-APM-004',
message: "HTTP error response code [#{status_code}] recevied from New Relic while sending data type [metric_data]"
}

assert_equal expected, NewRelic::Agent.agent.health_check.instance_variable_get(:@status)
end
end
end

# Some status codes may also eventually report other health checks
# Status code 401 is also invalid license key, but that will return a different health check value if that's the reason for the 401
# Status code 410 is also for forced disconnect, but that forced disconnect is handled where forced disconnect is rescued
# In this method, however, they should report HTTP_ERROR
check_agent_health_status_updates([400, 401, 403, 405, 407, 408, 409, 410, 411, 413, 415, 417, 429, 431])

# protocol 17
def test_supportability_metrics_for_http_error_responses
NewRelic::Agent.drop_buffered_data
Expand Down

0 comments on commit 5499054

Please sign in to comment.