|
| 1 | +// Copyright © 2026 Kaleido, Inc. |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | + |
| 17 | +package ffdns |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "net" |
| 22 | + "strings" |
| 23 | + "testing" |
| 24 | + "time" |
| 25 | + |
| 26 | + "github.com/hyperledger/firefly-common/pkg/config" |
| 27 | + "github.com/hyperledger/firefly-common/pkg/metric" |
| 28 | + "github.com/stretchr/testify/assert" |
| 29 | + "github.com/stretchr/testify/require" |
| 30 | +) |
| 31 | + |
| 32 | +// counterTotal sums the values of all series of a counter whose metric family name ends with |
| 33 | +// the given suffix (the registry prefixes names with component + subsystem). |
| 34 | +func counterTotal(t *testing.T, mr metric.MetricsRegistry, nameSuffix string) float64 { |
| 35 | + families, err := mr.GetGatherer().Gather() |
| 36 | + require.NoError(t, err) |
| 37 | + var total float64 |
| 38 | + for _, mf := range families { |
| 39 | + if strings.HasSuffix(mf.GetName(), nameSuffix) { |
| 40 | + for _, m := range mf.GetMetric() { |
| 41 | + if c := m.GetCounter(); c != nil { |
| 42 | + total += c.GetValue() |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + return total |
| 48 | +} |
| 49 | + |
| 50 | +var utConf = config.RootSection("dns_unit_tests") |
| 51 | + |
| 52 | +func resetConf() { |
| 53 | + config.RootConfigReset() |
| 54 | + InitConfig(utConf) |
| 55 | +} |
| 56 | + |
| 57 | +func TestWithDefaultDNSPort(t *testing.T) { |
| 58 | + assert.Equal(t, "8.8.8.8:53", withDefaultDNSPort("8.8.8.8")) |
| 59 | + assert.Equal(t, "8.8.8.8:5353", withDefaultDNSPort("8.8.8.8:5353")) |
| 60 | + assert.Equal(t, "[2001:db8::1]:53", withDefaultDNSPort("2001:db8::1")) |
| 61 | + assert.Equal(t, "[2001:db8::1]:5353", withDefaultDNSPort("[2001:db8::1]:5353")) |
| 62 | +} |
| 63 | + |
| 64 | +func TestNewResolverWithConfig(t *testing.T) { |
| 65 | + // No servers -> nil, leaving Go's default system resolver selection in place |
| 66 | + assert.Nil(t, NewResolverWithConfig(&Config{})) |
| 67 | + |
| 68 | + // Servers configured -> pure-Go resolver |
| 69 | + r := NewResolverWithConfig(&Config{Servers: []string{"8.8.8.8"}}) |
| 70 | + require.NotNil(t, r) |
| 71 | + assert.True(t, r.PreferGo) |
| 72 | + assert.NotNil(t, r.Dial) |
| 73 | +} |
| 74 | + |
| 75 | +func TestNewResolverFromConfigSection(t *testing.T) { |
| 76 | + resetConf() |
| 77 | + utConf.Set(DNSServers, []string{"8.8.8.8", "1.1.1.1:53"}) |
| 78 | + r := NewResolver(utConf) |
| 79 | + require.NotNil(t, r) |
| 80 | + assert.True(t, r.PreferGo) |
| 81 | + |
| 82 | + resetConf() |
| 83 | + assert.Nil(t, NewResolver(utConf)) |
| 84 | +} |
| 85 | + |
| 86 | +func TestResolverDialFailover(t *testing.T) { |
| 87 | + // Stand up a listener acting as the "good" DNS server |
| 88 | + ln, err := net.Listen("tcp", "127.0.0.1:0") |
| 89 | + require.NoError(t, err) |
| 90 | + defer ln.Close() |
| 91 | + |
| 92 | + accepted := make(chan struct{}, 1) |
| 93 | + go func() { |
| 94 | + conn, acceptErr := ln.Accept() |
| 95 | + if acceptErr == nil { |
| 96 | + accepted <- struct{}{} |
| 97 | + _ = conn.Close() |
| 98 | + } |
| 99 | + }() |
| 100 | + |
| 101 | + // First server is unroutable so the dialer must fail over to the live listener |
| 102 | + r := NewResolverWithConfig(&Config{ |
| 103 | + Timeout: 5 * time.Second, |
| 104 | + Servers: []string{"127.0.0.1:1", ln.Addr().String()}, |
| 105 | + }) |
| 106 | + require.NotNil(t, r) |
| 107 | + |
| 108 | + conn, err := r.Dial(context.Background(), "tcp", "ignored:53") |
| 109 | + require.NoError(t, err) |
| 110 | + defer conn.Close() |
| 111 | + assert.Equal(t, ln.Addr().String(), conn.RemoteAddr().String()) |
| 112 | + |
| 113 | + select { |
| 114 | + case <-accepted: |
| 115 | + case <-time.After(5 * time.Second): |
| 116 | + t.Fatal("DNS dial did not reach the configured server") |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +func TestResolverDialAllFail(t *testing.T) { |
| 121 | + r := NewResolverWithConfig(&Config{ |
| 122 | + Timeout: 250 * time.Millisecond, |
| 123 | + Servers: []string{"127.0.0.1:1"}, |
| 124 | + }) |
| 125 | + require.NotNil(t, r) |
| 126 | + _, err := r.Dial(context.Background(), "tcp", "ignored:53") |
| 127 | + assert.Error(t, err) |
| 128 | +} |
| 129 | + |
| 130 | +func TestEnableResolverMetrics(t *testing.T) { |
| 131 | + metricsManager = nil |
| 132 | + defer func() { metricsManager = nil }() |
| 133 | + |
| 134 | + ctx := context.Background() |
| 135 | + mr := metric.NewPrometheusMetricsRegistry("test") |
| 136 | + EnableResolverMetrics(ctx, mr) |
| 137 | + require.NotNil(t, metricsManager) |
| 138 | + |
| 139 | + // Idempotent - a second call is a no-op rather than re-registering |
| 140 | + EnableResolverMetrics(ctx, mr) |
| 141 | +} |
| 142 | + |
| 143 | +func TestResolverDialRecordsMetrics(t *testing.T) { |
| 144 | + metricsManager = nil |
| 145 | + defer func() { metricsManager = nil }() |
| 146 | + |
| 147 | + ctx := context.Background() |
| 148 | + mr := metric.NewPrometheusMetricsRegistry("test") |
| 149 | + EnableResolverMetrics(ctx, mr) |
| 150 | + |
| 151 | + // Live listener acts as the second (good) DNS server; the first is unroutable so a single |
| 152 | + // Dial exercises the request, error (failover), and response metric paths together. |
| 153 | + ln, err := net.Listen("tcp", "127.0.0.1:0") |
| 154 | + require.NoError(t, err) |
| 155 | + defer ln.Close() |
| 156 | + go func() { |
| 157 | + if conn, acceptErr := ln.Accept(); acceptErr == nil { |
| 158 | + _ = conn.Close() |
| 159 | + } |
| 160 | + }() |
| 161 | + |
| 162 | + r := NewResolverWithConfig(&Config{ |
| 163 | + Timeout: 5 * time.Second, |
| 164 | + Servers: []string{"127.0.0.1:1", ln.Addr().String()}, |
| 165 | + }) |
| 166 | + require.NotNil(t, r) |
| 167 | + conn, err := r.Dial(ctx, "tcp", "ignored:53") |
| 168 | + require.NoError(t, err) |
| 169 | + defer conn.Close() |
| 170 | + |
| 171 | + assert.GreaterOrEqual(t, counterTotal(t, mr, "dns_requests_total"), float64(2), "one request per server attempted") |
| 172 | + assert.GreaterOrEqual(t, counterTotal(t, mr, "dns_responses_total"), float64(1), "one successful response") |
| 173 | + assert.GreaterOrEqual(t, counterTotal(t, mr, "dns_errors_total"), float64(1), "first server failed over") |
| 174 | +} |
| 175 | + |
| 176 | +func TestResolverDialNoMetricsWhenDisabled(t *testing.T) { |
| 177 | + metricsManager = nil // metrics not enabled -> recording is a no-op, no panic |
| 178 | + r := NewResolverWithConfig(&Config{ |
| 179 | + Timeout: 250 * time.Millisecond, |
| 180 | + Servers: []string{"127.0.0.1:1"}, |
| 181 | + }) |
| 182 | + require.NotNil(t, r) |
| 183 | + _, err := r.Dial(context.Background(), "tcp", "ignored:53") |
| 184 | + assert.Error(t, err) |
| 185 | +} |
| 186 | + |
| 187 | +func TestClassifyDNSError(t *testing.T) { |
| 188 | + assert.Equal(t, "error", classifyDNSError(assertAnErr{})) |
| 189 | + assert.Equal(t, "timeout", classifyDNSError(timeoutErr{})) |
| 190 | +} |
| 191 | + |
| 192 | +type assertAnErr struct{} |
| 193 | + |
| 194 | +func (assertAnErr) Error() string { return "boom" } |
| 195 | + |
| 196 | +type timeoutErr struct{} |
| 197 | + |
| 198 | +func (timeoutErr) Error() string { return "i/o timeout" } |
| 199 | +func (timeoutErr) Timeout() bool { return true } |
| 200 | +func (timeoutErr) Temporary() bool { return true } |
0 commit comments