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
|
|
|
|
2017-07-15 21:15:59 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2019-06-27 20:22:07 +00:00
|
|
|
"bytes"
|
2017-07-15 21:15:59 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
2019-06-27 20:22:07 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-07-15 21:15:59 +00:00
|
|
|
)
|
|
|
|
|
2019-06-27 20:22:07 +00:00
|
|
|
func TestAgentRetryNewDiscover(t *testing.T) {
|
|
|
|
d, err := newDiscover()
|
|
|
|
require.NoError(t, err)
|
|
|
|
expected := []string{
|
2022-09-26 18:58:15 +00:00
|
|
|
"aliyun", "aws", "azure", "digitalocean", "gce", "hcp", "k8s", "linode",
|
2019-12-04 16:59:16 +00:00
|
|
|
"mdns", "os", "packet", "scaleway", "softlayer", "tencentcloud",
|
|
|
|
"triton", "vsphere",
|
2018-05-04 19:11:12 +00:00
|
|
|
}
|
2019-06-27 20:22:07 +00:00
|
|
|
require.Equal(t, expected, d.Names())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAgentRetryJoinAddrs(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2019-06-27 20:22:07 +00:00
|
|
|
d, err := newDiscover()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
input []string
|
|
|
|
expected []string
|
|
|
|
}{
|
|
|
|
{"handles nil", nil, []string{}},
|
|
|
|
{"handles empty input", []string{}, []string{}},
|
|
|
|
{"handles one element",
|
|
|
|
[]string{"192.168.0.12"},
|
|
|
|
[]string{"192.168.0.12"},
|
|
|
|
},
|
|
|
|
{"handles two elements",
|
|
|
|
[]string{"192.168.0.12", "192.168.0.13"},
|
|
|
|
[]string{"192.168.0.12", "192.168.0.13"},
|
|
|
|
},
|
|
|
|
{"tries to resolve aws things, which fails but that is fine",
|
|
|
|
[]string{"192.168.0.12", "provider=aws region=eu-west-1 tag_key=consul tag_value=tag access_key_id=a secret_access_key=a"},
|
|
|
|
[]string{"192.168.0.12"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for i, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
var buf bytes.Buffer
|
2020-01-28 23:50:41 +00:00
|
|
|
logger := testutil.LoggerWithOutput(t, &buf)
|
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
output := retryJoinAddrs(d, retryJoinSerfVariant, "LAN", test.input, logger)
|
2020-01-28 23:50:41 +00:00
|
|
|
bufout := buf.String()
|
|
|
|
require.Equal(t, test.expected, output, bufout)
|
2019-06-27 20:22:07 +00:00
|
|
|
if i == 4 {
|
2020-01-28 23:50:41 +00:00
|
|
|
require.Contains(t, bufout, `Using provider "aws"`)
|
2019-06-27 20:22:07 +00:00
|
|
|
}
|
|
|
|
})
|
2017-07-15 21:15:59 +00:00
|
|
|
}
|
2019-06-27 20:22:07 +00:00
|
|
|
t.Run("handles nil discover", func(t *testing.T) {
|
2020-03-09 20:59:02 +00:00
|
|
|
require.Equal(t, []string{}, retryJoinAddrs(nil, retryJoinSerfVariant, "LAN", []string{"a"}, nil))
|
2019-06-27 20:22:07 +00:00
|
|
|
})
|
2017-07-15 21:15:59 +00:00
|
|
|
}
|