123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- package discov
- import (
- "testing"
- "github.com/stretchr/testify/assert"
- )
- func TestConfig(t *testing.T) {
- tests := []struct {
- EtcdConf
- pass bool
- }{
- {
- EtcdConf: EtcdConf{},
- pass: false,
- },
- {
- EtcdConf: EtcdConf{
- Key: "any",
- },
- pass: false,
- },
- {
- EtcdConf: EtcdConf{
- Hosts: []string{"any"},
- },
- pass: false,
- },
- {
- EtcdConf: EtcdConf{
- Hosts: []string{"any"},
- Key: "key",
- },
- pass: true,
- },
- }
- for _, test := range tests {
- if test.pass {
- assert.Nil(t, test.EtcdConf.Validate())
- } else {
- assert.NotNil(t, test.EtcdConf.Validate())
- }
- }
- }
- func TestEtcdConf_HasAccount(t *testing.T) {
- tests := []struct {
- EtcdConf
- hasAccount bool
- }{
- {
- EtcdConf: EtcdConf{
- Hosts: []string{"any"},
- Key: "key",
- },
- hasAccount: false,
- },
- {
- EtcdConf: EtcdConf{
- Hosts: []string{"any"},
- Key: "key",
- User: "foo",
- },
- hasAccount: false,
- },
- {
- EtcdConf: EtcdConf{
- Hosts: []string{"any"},
- Key: "key",
- User: "foo",
- Pass: "bar",
- },
- hasAccount: true,
- },
- }
- for _, test := range tests {
- assert.Equal(t, test.hasAccount, test.EtcdConf.HasAccount())
- }
- }
- func TestEtcdConf_HasID(t *testing.T) {
- tests := []struct {
- EtcdConf
- hasServerID bool
- }{
- {
- EtcdConf: EtcdConf{
- Hosts: []string{"any"},
- ID: -1,
- },
- hasServerID: false,
- },
- {
- EtcdConf: EtcdConf{
- Hosts: []string{"any"},
- ID: 0,
- },
- hasServerID: false,
- },
- {
- EtcdConf: EtcdConf{
- Hosts: []string{"any"},
- ID: 10000,
- },
- hasServerID: true,
- },
- }
- for _, test := range tests {
- assert.Equal(t, test.hasServerID, test.EtcdConf.HasID())
- }
- }
- func TestEtcdConf_HasTLS(t *testing.T) {
- tests := []struct {
- name string
- conf EtcdConf
- want bool
- }{
- {
- name: "empty config",
- conf: EtcdConf{},
- want: false,
- },
- {
- name: "missing CertFile",
- conf: EtcdConf{
- CertKeyFile: "key",
- CACertFile: "ca",
- },
- want: false,
- },
- {
- name: "missing CertKeyFile",
- conf: EtcdConf{
- CertFile: "cert",
- CACertFile: "ca",
- },
- want: false,
- },
- {
- name: "missing CACertFile",
- conf: EtcdConf{
- CertFile: "cert",
- CertKeyFile: "key",
- },
- want: false,
- },
- {
- name: "valid config",
- conf: EtcdConf{
- CertFile: "cert",
- CertKeyFile: "key",
- CACertFile: "ca",
- },
- want: true,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- got := tt.conf.HasTLS()
- assert.Equal(t, tt.want, got)
- })
- }
- }
|