config_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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_HasServerID(t *testing.T) {
  78. tests := []struct {
  79. EtcdConf
  80. hasServerID bool
  81. }{
  82. {
  83. EtcdConf: EtcdConf{
  84. Hosts: []string{"any"},
  85. ServerID: -1,
  86. },
  87. hasServerID: false,
  88. },
  89. {
  90. EtcdConf: EtcdConf{
  91. Hosts: []string{"any"},
  92. ServerID: 0,
  93. },
  94. hasServerID: false,
  95. },
  96. {
  97. EtcdConf: EtcdConf{
  98. Hosts: []string{"any"},
  99. ServerID: 10000,
  100. },
  101. hasServerID: true,
  102. },
  103. }
  104. for _, test := range tests {
  105. assert.Equal(t, test.hasServerID, test.EtcdConf.HasServerID())
  106. }
  107. }