2017-07-15 21:15:59 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2019-06-27 20:22:07 +00:00
|
|
|
"bytes"
|
|
|
|
"log"
|
2017-07-15 21:15:59 +00:00
|
|
|
"testing"
|
|
|
|
|
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{
|
|
|
|
"aliyun", "aws", "azure", "digitalocean", "gce", "k8s", "mdns",
|
|
|
|
"os", "packet", "scaleway", "softlayer", "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) {
|
|
|
|
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
|
|
|
|
logger := log.New(&buf, "logger: ", log.Lshortfile)
|
|
|
|
require.Equal(t, test.expected, retryJoinAddrs(d, "LAN", test.input, logger), buf.String())
|
|
|
|
if i == 4 {
|
|
|
|
require.Contains(t, buf.String(), `Using provider "aws"`)
|
|
|
|
}
|
|
|
|
})
|
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) {
|
|
|
|
require.Equal(t, []string{}, retryJoinAddrs(nil, "LAN", []string{"a"}, nil))
|
|
|
|
})
|
2017-07-15 21:15:59 +00:00
|
|
|
}
|