Skip to content

Commit 21d6815

Browse files
committed
fix: add tests for agent re-discovery mechanism
Signed-off-by: Arjun Rajappa <arjun.rajappa@ibm.com>
1 parent e3cc72e commit 21d6815

4 files changed

Lines changed: 123 additions & 0 deletions

File tree

test/backend/host_agent_lookup_test.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,19 @@ def test_lookup_with_gateway_no_destination
7575

7676
assert_nil client
7777
end
78+
79+
def test_lookup_handles_connection_errors
80+
# Test that various connection errors result in nil client
81+
stub_request(:get, "http://10.10.10.10:42699/")
82+
.to_raise(Errno::ECONNREFUSED)
83+
84+
subject = Instana::Backend::HostAgentLookup.new('10.10.10.10', 42699)
85+
86+
client = FakeFS.with_fresh do
87+
FakeFS::FileSystem.clone('test/support/ecs', '/proc')
88+
subject.call
89+
end
90+
91+
assert_nil client
92+
end
7893
end

test/backend/host_agent_reporting_observer_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ def test_report_fail
4141
.to_return(status: 200)
4242
stub_request(:post, "http://10.10.10.10:9292/com.instana.plugin.ruby.0")
4343
.to_return(status: 500)
44+
stub_request(:get, "http://127.0.0.1:42699/")
45+
.to_return(status: 200)
46+
stub_request(:put, "http://127.0.0.1:42699/com.instana.plugin.ruby.discovery")
47+
.to_return(status: 200, body: '{"pid": 0}')
48+
stub_request(:head, "http://127.0.0.1:42699/com.instana.plugin.ruby.0")
49+
.to_return(status: 200)
4450

4551
client = Instana::Backend::RequestClient.new('10.10.10.10', 9292)
4652
discovery = Concurrent::Atom.new({'pid' => 0})
@@ -240,6 +246,13 @@ def test_report_traces_error
240246
stub_request(:post, "http://10.10.10.10:9292/com.instana.plugin.ruby/traces.1234")
241247
.to_return(status: 500)
242248

249+
stub_request(:get, "http://127.0.0.1:42699/")
250+
.to_return(status: 200)
251+
stub_request(:put, "http://127.0.0.1:42699/com.instana.plugin.ruby.discovery")
252+
.to_return(status: 200, body: '{"pid": 1234}')
253+
stub_request(:head, "http://127.0.0.1:42699/com.instana.plugin.ruby.1234")
254+
.to_return(status: 200)
255+
243256
client = Instana::Backend::RequestClient.new('10.10.10.10', 9292)
244257
discovery = Concurrent::Atom.new({'pid' => 1234})
245258

test/backend/host_agent_test.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,55 @@ def test_after_fork
8686
subject = Instana::Backend::HostAgent.new
8787
assert subject.respond_to? :after_fork
8888
end
89+
90+
def test_announce_retries_on_connection_failure
91+
agent_host = '10.10.10.10'
92+
::Instana.config[:agent_host] = agent_host
93+
94+
# Simulate connection failures followed by success
95+
stub_request(:get, "http://#{agent_host}:42699/")
96+
.to_raise(Errno::ECONNREFUSED).times(3).then
97+
.to_return(status: 200, body: "", headers: {})
98+
99+
discovery = Minitest::Mock.new
100+
discovery.expect(:delete_observers, discovery, [])
101+
discovery.expect(:observers, discovery, [])
102+
discovery.expect(:notify_and_delete_observers, discovery, [Object, nil, nil])
103+
discovery.expect(:with_observer, discovery, [Instana::Backend::HostAgentActivationObserver])
104+
discovery.expect(:with_observer, discovery, [Instana::Backend::HostAgentReportingObserver])
105+
discovery.expect(:swap, discovery, [])
106+
107+
subject = Instana::Backend::HostAgent.new(discovery: discovery)
108+
109+
FakeFS.with_fresh do
110+
FakeFS::FileSystem.clone('test/support/ecs', '/proc')
111+
client = subject.announce
112+
assert client
113+
assert_instance_of Instana::Backend::RequestClient, client
114+
end
115+
116+
discovery.verify
117+
ensure
118+
::Instana.config[:agent_host] = '127.0.0.1'
119+
end
120+
121+
def test_announce_returns_nil_after_max_retries
122+
agent_host = '10.10.10.10'
123+
::Instana.config[:agent_host] = agent_host
124+
125+
# Simulate persistent connection failures
126+
stub_request(:get, "http://#{agent_host}:42699/")
127+
.to_raise(Errno::ECONNREFUSED).times(15)
128+
129+
discovery = Concurrent::Atom.new(nil)
130+
subject = Instana::Backend::HostAgent.new(discovery: discovery)
131+
132+
FakeFS.with_fresh do
133+
FakeFS::FileSystem.clone('test/support/ecs', '/proc')
134+
client = subject.announce
135+
assert_nil client
136+
end
137+
ensure
138+
::Instana.config[:agent_host] = '127.0.0.1'
139+
end
89140
end

test/backend/request_client_test.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,48 @@ def test_send_request_failure
3636

3737
refute response.ok?
3838
end
39+
40+
def test_connection_errors_return_error_responses
41+
subject = Instana::Backend::RequestClient.new('example.com', 9292)
42+
43+
# Test ECONNREFUSED
44+
stub_request(:get, 'http://example.com:9292/refused')
45+
.to_raise(Errno::ECONNREFUSED)
46+
response = subject.send_request('GET', '/refused')
47+
refute response.ok?
48+
assert_equal '503', response.code
49+
assert_includes response.body, 'Connection refused'
50+
51+
# Test EHOSTUNREACH
52+
stub_request(:get, 'http://example.com:9292/unreachable')
53+
.to_raise(Errno::EHOSTUNREACH)
54+
response = subject.send_request('GET', '/unreachable')
55+
refute response.ok?
56+
assert_equal '503', response.code
57+
assert_includes response.body, 'Host unreachable'
58+
59+
# Test Timeout
60+
stub_request(:get, 'http://example.com:9292/timeout')
61+
.to_timeout
62+
response = subject.send_request('GET', '/timeout')
63+
refute response.ok?
64+
assert_equal '408', response.code
65+
assert_includes response.body, 'Timeout'
66+
67+
# Test SocketError
68+
stub_request(:get, 'http://example.com:9292/socket')
69+
.to_raise(SocketError.new('Name or service not known'))
70+
response = subject.send_request('GET', '/socket')
71+
refute response.ok?
72+
assert_equal '502', response.code
73+
assert_includes response.body, 'Socket error'
74+
75+
# Test StandardError
76+
stub_request(:get, 'http://example.com:9292/error')
77+
.to_raise(StandardError.new('Unexpected error'))
78+
response = subject.send_request('GET', '/error')
79+
refute response.ok?
80+
assert_equal '500', response.code
81+
assert_includes response.body, 'StandardError'
82+
end
3983
end

0 commit comments

Comments
 (0)