config_test.go 7.5 KB

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