config_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package discov
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. )
  6. func TestConfig(t *testing.T) {
  7. tests := []struct {
  8. EtcdConf
  9. pass bool
  10. }{
  11. {
  12. EtcdConf: EtcdConf{},
  13. pass: false,
  14. },
  15. {
  16. EtcdConf: EtcdConf{
  17. Key: "any",
  18. },
  19. pass: false,
  20. },
  21. {
  22. EtcdConf: EtcdConf{
  23. Hosts: []string{"any"},
  24. },
  25. pass: false,
  26. },
  27. {
  28. EtcdConf: EtcdConf{
  29. Hosts: []string{"any"},
  30. Key: "key",
  31. },
  32. pass: true,
  33. },
  34. }
  35. for _, test := range tests {
  36. if test.pass {
  37. assert.Nil(t, test.EtcdConf.Validate())
  38. } else {
  39. assert.NotNil(t, test.EtcdConf.Validate())
  40. }
  41. }
  42. }
  43. func TestEtcdConf_HasAccount(t *testing.T) {
  44. tests := []struct {
  45. EtcdConf
  46. hasAccount bool
  47. }{
  48. {
  49. EtcdConf: EtcdConf{
  50. Hosts: []string{"any"},
  51. Key: "key",
  52. },
  53. hasAccount: false,
  54. },
  55. {
  56. EtcdConf: EtcdConf{
  57. Hosts: []string{"any"},
  58. Key: "key",
  59. User: "foo",
  60. },
  61. hasAccount: false,
  62. },
  63. {
  64. EtcdConf: EtcdConf{
  65. Hosts: []string{"any"},
  66. Key: "key",
  67. User: "foo",
  68. Pass: "bar",
  69. },
  70. hasAccount: true,
  71. },
  72. }
  73. for _, test := range tests {
  74. assert.Equal(t, test.hasAccount, test.EtcdConf.HasAccount())
  75. }
  76. }
  77. func TestEtcdConf_HasID(t *testing.T) {
  78. tests := []struct {
  79. EtcdConf
  80. hasServerID bool
  81. }{
  82. {
  83. EtcdConf: EtcdConf{
  84. Hosts: []string{"any"},
  85. ID: -1,
  86. },
  87. hasServerID: false,
  88. },
  89. {
  90. EtcdConf: EtcdConf{
  91. Hosts: []string{"any"},
  92. ID: 0,
  93. },
  94. hasServerID: false,
  95. },
  96. {
  97. EtcdConf: EtcdConf{
  98. Hosts: []string{"any"},
  99. ID: 10000,
  100. },
  101. hasServerID: true,
  102. },
  103. }
  104. for _, test := range tests {
  105. assert.Equal(t, test.hasServerID, test.EtcdConf.HasID())
  106. }
  107. }
  108. func TestEtcdConf_HasTLS(t *testing.T) {
  109. tests := []struct {
  110. name string
  111. conf EtcdConf
  112. want bool
  113. }{
  114. {
  115. name: "empty config",
  116. conf: EtcdConf{},
  117. want: false,
  118. },
  119. {
  120. name: "missing CertFile",
  121. conf: EtcdConf{
  122. CertKeyFile: "key",
  123. CACertFile: "ca",
  124. },
  125. want: false,
  126. },
  127. {
  128. name: "missing CertKeyFile",
  129. conf: EtcdConf{
  130. CertFile: "cert",
  131. CACertFile: "ca",
  132. },
  133. want: false,
  134. },
  135. {
  136. name: "missing CACertFile",
  137. conf: EtcdConf{
  138. CertFile: "cert",
  139. CertKeyFile: "key",
  140. },
  141. want: false,
  142. },
  143. {
  144. name: "valid config",
  145. conf: EtcdConf{
  146. CertFile: "cert",
  147. CertKeyFile: "key",
  148. CACertFile: "ca",
  149. },
  150. want: true,
  151. },
  152. }
  153. for _, tt := range tests {
  154. t.Run(tt.name, func(t *testing.T) {
  155. got := tt.conf.HasTLS()
  156. assert.Equal(t, tt.want, got)
  157. })
  158. }
  159. }