2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 18:39:22 +00:00
|
|
|
|
2021-07-19 22:22:51 +00:00
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2022-05-19 19:00:07 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/sdk/testutil/retry"
|
2021-07-19 22:22:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDNS_Recursor_StrategyRandom(t *testing.T) {
|
|
|
|
configuredRecursors := []string{"1.1.1.1", "8.8.4.4", "8.8.8.8"}
|
|
|
|
recursorStrategy := RecursorStrategy("random")
|
|
|
|
|
|
|
|
retry.RunWith(&retry.Counter{Count: 5}, t, func(r *retry.R) {
|
|
|
|
recursorsToQuery := make([]string, 0)
|
|
|
|
for _, idx := range recursorStrategy.Indexes(len(configuredRecursors)) {
|
|
|
|
recursorsToQuery = append(recursorsToQuery, configuredRecursors[idx])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the slices contain the same elements
|
2022-05-19 19:00:07 +00:00
|
|
|
require.ElementsMatch(r, configuredRecursors, recursorsToQuery)
|
2021-07-19 22:22:51 +00:00
|
|
|
|
|
|
|
// Ensure the elements are not in the same order
|
|
|
|
require.NotEqual(r, configuredRecursors, recursorsToQuery)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNS_Recursor_StrategySequential(t *testing.T) {
|
|
|
|
expectedRecursors := []string{"1.1.1.1", "8.8.4.4", "8.8.8.8"}
|
|
|
|
recursorStrategy := RecursorStrategy("sequential")
|
|
|
|
|
|
|
|
recursorsToQuery := make([]string, 0)
|
|
|
|
for _, idx := range recursorStrategy.Indexes(len(expectedRecursors)) {
|
|
|
|
recursorsToQuery = append(recursorsToQuery, expectedRecursors[idx])
|
|
|
|
}
|
|
|
|
|
|
|
|
// The list of recursors should match the order in which they were defined
|
|
|
|
// in the configuration
|
|
|
|
require.Equal(t, recursorsToQuery, expectedRecursors)
|
|
|
|
}
|