config_test.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. package etcdmain
  2. import (
  3. "net/url"
  4. "reflect"
  5. "testing"
  6. )
  7. func TestConfigParsingMemberFlags(t *testing.T) {
  8. args := []string{
  9. "-data-dir=testdir",
  10. "-name=testname",
  11. "-max-wals=10",
  12. "-max-snapshots=10",
  13. "-snapshot-count=10",
  14. "-listen-peer-urls=http://localhost:8000,https://localhost:8001",
  15. "-listen-client-urls=http://localhost:7000,https://localhost:7001",
  16. }
  17. wcfg := &config{
  18. dir: "testdir",
  19. lpurls: []url.URL{{Scheme: "http", Host: "localhost:8000"}, {Scheme: "https", Host: "localhost:8001"}},
  20. lcurls: []url.URL{{Scheme: "http", Host: "localhost:7000"}, {Scheme: "https", Host: "localhost:7001"}},
  21. maxSnapFiles: 10,
  22. maxWalFiles: 10,
  23. name: "testname",
  24. snapCount: 10,
  25. }
  26. cfg := NewConfig()
  27. err := cfg.Parse(args)
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. if cfg.dir != wcfg.dir {
  32. t.Errorf("dir = %v, want %v", cfg.dir, wcfg.dir)
  33. }
  34. if cfg.maxSnapFiles != wcfg.maxSnapFiles {
  35. t.Errorf("maxsnap = %v, want %v", cfg.maxSnapFiles, wcfg.maxSnapFiles)
  36. }
  37. if cfg.maxWalFiles != wcfg.maxWalFiles {
  38. t.Errorf("maxwal = %v, want %v", cfg.maxWalFiles, wcfg.maxWalFiles)
  39. }
  40. if cfg.name != wcfg.name {
  41. t.Errorf("name = %v, want %v", cfg.name, wcfg.name)
  42. }
  43. if cfg.snapCount != wcfg.snapCount {
  44. t.Errorf("snapcount = %v, want %v", cfg.snapCount, wcfg.snapCount)
  45. }
  46. if !reflect.DeepEqual(cfg.lpurls, wcfg.lpurls) {
  47. t.Errorf("listen-peer-urls = %v, want %v", cfg.lpurls, wcfg.lpurls)
  48. }
  49. if !reflect.DeepEqual(cfg.lcurls, wcfg.lcurls) {
  50. t.Errorf("listen-client-urls = %v, want %v", cfg.lcurls, wcfg.lcurls)
  51. }
  52. }
  53. func TestConfigParsingClusteringFlags(t *testing.T) {
  54. args := []string{
  55. "-initial-cluster=0=http://localhost:8000",
  56. "-initial-cluster-state=existing",
  57. "-initial-cluster-token=etcdtest",
  58. "-initial-advertise-peer-urls=http://localhost:8000,https://localhost:8001",
  59. "-advertise-client-urls=http://localhost:7000,https://localhost:7001",
  60. "-discovery-fallback=exit",
  61. }
  62. wcfg := NewConfig()
  63. wcfg.apurls = []url.URL{{Scheme: "http", Host: "localhost:8000"}, {Scheme: "https", Host: "localhost:8001"}}
  64. wcfg.acurls = []url.URL{{Scheme: "http", Host: "localhost:7000"}, {Scheme: "https", Host: "localhost:7001"}}
  65. wcfg.clusterState.Set(clusterStateFlagExisting)
  66. wcfg.fallback.Set(fallbackFlagExit)
  67. wcfg.initialCluster = "0=http://localhost:8000"
  68. wcfg.initialClusterToken = "etcdtest"
  69. cfg := NewConfig()
  70. err := cfg.Parse(args)
  71. if err != nil {
  72. t.Fatal(err)
  73. }
  74. if cfg.clusterState.String() != wcfg.clusterState.String() {
  75. t.Errorf("clusterState = %v, want %v", cfg.clusterState, wcfg.clusterState)
  76. }
  77. if cfg.fallback.String() != wcfg.fallback.String() {
  78. t.Errorf("fallback = %v, want %v", cfg.fallback, wcfg.fallback)
  79. }
  80. if cfg.initialCluster != wcfg.initialCluster {
  81. t.Errorf("initialCluster = %v, want %v", cfg.initialCluster, wcfg.initialCluster)
  82. }
  83. if cfg.initialClusterToken != wcfg.initialClusterToken {
  84. t.Errorf("initialClusterToken = %v, want %v", cfg.initialClusterToken, wcfg.initialClusterToken)
  85. }
  86. if !reflect.DeepEqual(cfg.apurls, wcfg.apurls) {
  87. t.Errorf("initial-advertise-peer-urls = %v, want %v", cfg.lpurls, wcfg.lpurls)
  88. }
  89. if !reflect.DeepEqual(cfg.acurls, wcfg.acurls) {
  90. t.Errorf("advertise-client-urls = %v, want %v", cfg.lcurls, wcfg.lcurls)
  91. }
  92. }
  93. func TestConfigParsingOtherFlags(t *testing.T) {
  94. args := []string{
  95. "-proxy=readonly",
  96. "-ca-file=cafile",
  97. "-cert-file=certfile",
  98. "-key-file=keyfile",
  99. "-peer-ca-file=peercafile",
  100. "-peer-cert-file=peercertfile",
  101. "-peer-key-file=peerkeyfile",
  102. "-force-new-cluster=true",
  103. }
  104. wcfg := NewConfig()
  105. wcfg.proxy.Set(proxyFlagReadonly)
  106. wcfg.clientTLSInfo.CAFile = "cafile"
  107. wcfg.clientTLSInfo.CertFile = "certfile"
  108. wcfg.clientTLSInfo.KeyFile = "keyfile"
  109. wcfg.peerTLSInfo.CAFile = "peercafile"
  110. wcfg.peerTLSInfo.CertFile = "peercertfile"
  111. wcfg.peerTLSInfo.KeyFile = "peerkeyfile"
  112. wcfg.forceNewCluster = true
  113. cfg := NewConfig()
  114. err := cfg.Parse(args)
  115. if err != nil {
  116. t.Fatal(err)
  117. }
  118. if cfg.proxy.String() != wcfg.proxy.String() {
  119. t.Errorf("proxy = %v, want %v", cfg.proxy, wcfg.proxy)
  120. }
  121. if cfg.clientTLSInfo.String() != wcfg.clientTLSInfo.String() {
  122. t.Errorf("clientTLS = %v, want %v", cfg.clientTLSInfo, wcfg.clientTLSInfo)
  123. }
  124. if cfg.peerTLSInfo.String() != wcfg.peerTLSInfo.String() {
  125. t.Errorf("peerTLS = %v, want %v", cfg.peerTLSInfo, wcfg.peerTLSInfo)
  126. }
  127. if cfg.forceNewCluster != wcfg.forceNewCluster {
  128. t.Errorf("forceNewCluster = %t, want %t", cfg.forceNewCluster, wcfg.forceNewCluster)
  129. }
  130. }
  131. func TestConfigParsingConflictClusteringFlags(t *testing.T) {
  132. conflictArgs := [][]string{
  133. []string{
  134. "-initial-cluster=0=localhost:8000",
  135. "-discovery=http://example.com/abc",
  136. },
  137. []string{
  138. "-discovery-srv=example.com",
  139. "-discovery=http://example.com/abc",
  140. },
  141. []string{
  142. "-initial-cluster=0=localhost:8000",
  143. "-discovery-srv=example.com",
  144. },
  145. []string{
  146. "-initial-cluster=0=localhost:8000",
  147. "-discovery=http://example.com/abc",
  148. "-discovery-srv=example.com",
  149. },
  150. }
  151. for i, tt := range conflictArgs {
  152. cfg := NewConfig()
  153. err := cfg.Parse(tt)
  154. if err != ErrConflictBootstrapFlags {
  155. t.Errorf("%d: err = %v, want %v", i, err, ErrConflictBootstrapFlags)
  156. }
  157. }
  158. }
  159. func TestConfigIsNewCluster(t *testing.T) {
  160. tests := []struct {
  161. state string
  162. wIsNew bool
  163. }{
  164. {clusterStateFlagExisting, false},
  165. {clusterStateFlagNew, true},
  166. }
  167. for i, tt := range tests {
  168. cfg := NewConfig()
  169. if err := cfg.clusterState.Set(tt.state); err != nil {
  170. t.Fatalf("#%d: unexpected clusterState.Set error: %v", i, err)
  171. }
  172. if g := cfg.isNewCluster(); g != tt.wIsNew {
  173. t.Errorf("#%d: isNewCluster = %v, want %v", i, g, tt.wIsNew)
  174. }
  175. }
  176. }
  177. func TestConfigIsProxy(t *testing.T) {
  178. tests := []struct {
  179. proxy string
  180. wIsProxy bool
  181. }{
  182. {proxyFlagOff, false},
  183. {proxyFlagReadonly, true},
  184. {proxyFlagOn, true},
  185. }
  186. for i, tt := range tests {
  187. cfg := NewConfig()
  188. if err := cfg.proxy.Set(tt.proxy); err != nil {
  189. t.Fatalf("#%d: unexpected proxy.Set error: %v", i, err)
  190. }
  191. if g := cfg.isProxy(); g != tt.wIsProxy {
  192. t.Errorf("#%d: isProxy = %v, want %v", i, g, tt.wIsProxy)
  193. }
  194. }
  195. }
  196. func TestConfigIsReadonlyProxy(t *testing.T) {
  197. tests := []struct {
  198. proxy string
  199. wIsReadonly bool
  200. }{
  201. {proxyFlagOff, false},
  202. {proxyFlagReadonly, true},
  203. {proxyFlagOn, false},
  204. }
  205. for i, tt := range tests {
  206. cfg := NewConfig()
  207. if err := cfg.proxy.Set(tt.proxy); err != nil {
  208. t.Fatalf("#%d: unexpected proxy.Set error: %v", i, err)
  209. }
  210. if g := cfg.isReadonlyProxy(); g != tt.wIsReadonly {
  211. t.Errorf("#%d: isReadonlyProxy = %v, want %v", i, g, tt.wIsReadonly)
  212. }
  213. }
  214. }
  215. func TestConfigShouldFallbackToProxy(t *testing.T) {
  216. tests := []struct {
  217. fallback string
  218. wFallback bool
  219. }{
  220. {fallbackFlagProxy, true},
  221. {fallbackFlagExit, false},
  222. }
  223. for i, tt := range tests {
  224. cfg := NewConfig()
  225. if err := cfg.fallback.Set(tt.fallback); err != nil {
  226. t.Fatalf("#%d: unexpected fallback.Set error: %v", i, err)
  227. }
  228. if g := cfg.shouldFallbackToProxy(); g != tt.wFallback {
  229. t.Errorf("#%d: shouldFallbackToProxy = %v, want %v", i, g, tt.wFallback)
  230. }
  231. }
  232. }