1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
package client
import (
"crypto/tls"
"testing"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestWithTLSConfig(t *testing.T) {
config := &tls.Config{
InsecureSkipVerify: true,
}
opts := &options{}
fn := WithTLSConfig(config)
err := fn(opts)
require.Nil(t, err)
assert.Equal(t, config, opts.tlsConfig)
}
func TestNewOptions(t *testing.T) {
tests := []struct {
name string
opts []Option
want *options
}{
{
"no endpoints",
[]Option{},
&options{
endpoints: []string{defaultUnixEndpoint},
},
},
{
"single endpoints",
[]Option{WithEndpoint("ssl:192.168.1.1:6443")},
&options{
endpoints: []string{"ssl:192.168.1.1:6443"},
},
},
{
"multiple endpoints",
[]Option{WithEndpoint("ssl:192.168.1.1:6443"), WithEndpoint("ssl:192.168.1.2:6443")},
&options{
endpoints: []string{"ssl:192.168.1.1:6443", "ssl:192.168.1.2:6443"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := newOptions(tt.opts...)
got.logger = nil // hack; we don't care
require.Nil(t, err)
assert.Equal(t, tt.want, got)
})
}
}
func TestWithEndpoint(t *testing.T) {
tests := []struct {
name string
endpoint string
want []string
wantErr bool
}{
{
"default unix",
"unix:",
[]string{defaultUnixEndpoint},
false,
},
{
"default tcp",
"tcp:",
[]string{defaultTCPEndpoint},
false,
},
{
"default ssl",
"ssl:",
[]string{defaultSSLEndpoint},
false,
},
{
"invalid",
"foo : ",
nil,
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
opts := &options{}
fn := WithEndpoint(tt.endpoint)
err := fn(opts)
if tt.wantErr {
require.Error(t, err)
} else {
require.Nil(t, err)
}
assert.Equal(t, tt.want, opts.endpoints)
})
}
}
func TestWithReconnect(t *testing.T) {
timeout := 2 * time.Second
opts := &options{}
fn := WithReconnect(timeout, &backoff.ZeroBackOff{})
err := fn(opts)
require.NoError(t, err)
assert.Equal(t, timeout, opts.timeout)
assert.Equal(t, true, opts.reconnect)
assert.Equal(t, &backoff.ZeroBackOff{}, opts.backoff)
}
|