Skip to content

Commit 3e44b1a

Browse files
committed
Improve negative TTL caching tests.
To better clarify usage and add a second test to ensure the negative TTL setting is actually being used, rather than the timing just happening to work for the default TTL (which we saw in a separate branch working on this with Trafficserver's DNS).
1 parent 8c7d1c5 commit 3e44b1a

File tree

2 files changed

+75
-13
lines changed

2 files changed

+75
-13
lines changed

test/proxy/dns/test_negative_caching.rb

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,71 @@ def after_all
2626
override_config_reset("--router")
2727
end
2828

29-
def test_failed_host_down_after_ttl_expires
29+
def test_caches_failed_lookups_before_retrying
30+
assert_negative_ttl(NEGATIVE_TTL)
31+
end
32+
33+
def test_negative_ttl_can_be_configured
34+
negative_ttl = 3
35+
36+
# Ensure this negative TTL is different enough than the default that we can
37+
# distinguish the results in tests.
38+
assert_operator(negative_ttl, :<=, (NEGATIVE_TTL - TTL_BUFFER_POS).floor)
39+
40+
override_config({
41+
"dns_resolver" => {
42+
"nameservers" => ["[127.0.0.1]:#{$config["unbound"]["port"]}"],
43+
"max_stale" => 0,
44+
"negative_ttl" => negative_ttl,
45+
},
46+
}, "--router") do
47+
assert_negative_ttl(negative_ttl)
48+
end
49+
end
50+
51+
private
52+
53+
def assert_negative_ttl(negative_ttl)
3054
prepend_api_backends([
3155
{
3256
:frontend_host => "127.0.0.1",
33-
:backend_host => "negative-caching-invalid-hostname-begins-resolving.ooga",
34-
:servers => [{ :host => "negative-caching-invalid-hostname-begins-resolving.ooga", :port => 9444 }],
35-
:url_matches => [{ :frontend_prefix => "/#{unique_test_id}/negative-caching-invalid-hostname-begins-resolving/", :backend_prefix => "/info/" }],
57+
:backend_host => unique_test_hostname,
58+
:servers => [{ :host => unique_test_hostname, :port => 9444 }],
59+
:url_matches => [{ :frontend_prefix => "/#{unique_test_id}/", :backend_prefix => "/info/" }],
3660
},
3761
]) do
38-
# The negative TTL caching really begins as soon as the initial
39-
# configuration is put into place by runServer (since that's when the
40-
# hostname is first seen and the unresolvable status is cached). So start
41-
# our timer here.
62+
# Make an initial request, which we expect to not succeed, since the
63+
# hostname is bad.
64+
wait_for_response("/#{unique_test_id}/", {
65+
:code => 502,
66+
})
67+
68+
# The negative TTL caching begins after TrafficServer sees the first
69+
# request and tries to resolve it. So start our timer after the first
70+
# request.
4271
start_time = Time.now.utc
4372

44-
wait_for_response("/#{unique_test_id}/negative-caching-invalid-hostname-begins-resolving/", {
73+
# Add the DNS record for the previously invalid domain.
74+
set_dns_records(["#{unique_test_hostname} 60 A 127.0.0.1"])
75+
76+
# Ensure that negative caching is in place and the hostname is still not
77+
# resolving (despite the DNS being installed now).
78+
wait_for_response("/#{unique_test_id}/", {
4579
:code => 502,
4680
})
4781

48-
set_dns_records(["negative-caching-invalid-hostname-begins-resolving.ooga 60 A 127.0.0.1"])
49-
wait_for_response("/#{unique_test_id}/negative-caching-invalid-hostname-begins-resolving/", {
82+
# Wait for the successful response to resolve once the negative TTL has
83+
# expired.
84+
wait_for_response("/#{unique_test_id}/", {
5085
:code => 200,
5186
:local_interface_ip => "127.0.0.1",
5287
})
88+
89+
# Sanity check the results to ensure the results fit within the expected
90+
# negative TTL values.
5391
duration = Time.now.utc - start_time
54-
min_duration = NEGATIVE_TTL - TTL_BUFFER_NEG
55-
max_duration = NEGATIVE_TTL + TTL_BUFFER_POS
92+
min_duration = negative_ttl - TTL_BUFFER_NEG
93+
max_duration = negative_ttl + TTL_BUFFER_POS
5694
assert_operator(min_duration, :>, 0)
5795
assert_operator(duration, :>=, min_duration)
5896
assert_operator(duration, :<, max_duration)

test/support/api_umbrella_test_helpers/setup.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,30 @@ def unique_test_id
279279
@unique_test_id ||= self.location.gsub(/[^\w]+/, "-")
280280
end
281281

282+
def unique_test_hostname
283+
unless @unique_test_hostname
284+
# Replace all non alpha-numeric chars (namely underscores that might be
285+
# in the ID) with dashes (since underscores aren't valid for
286+
# hostnames).
287+
hostname = unique_test_id.downcase.gsub(/[^a-z0-9]+/, "-")
288+
289+
# Truncate the hostname so the label will fit in unbound's 63 char
290+
# limit.
291+
hostname = hostname[-56..-1] || hostname
292+
293+
# Strip first char if it happens to be a dash.
294+
hostname.gsub!(/^-/, "")
295+
296+
# Since we've truncated the test ID, it's possible it's no longer
297+
# unique, so append some random chars (but still, fitting within the 63
298+
# char limit).
299+
hostname = "#{hostname}-#{SecureRandom.hex(3)}"
300+
301+
@unique_test_hostname = "#{hostname}.test"
302+
end
303+
@unique_test_hostname
304+
end
305+
282306
def next_unique_ip_addr
283307
@@incrementing_unique_ip_addr = @@incrementing_unique_ip_addr.succ
284308
@@incrementing_unique_ip_addr.to_s

0 commit comments

Comments
 (0)