config_test.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 TestConfigParsingV1Flags(t *testing.T) {
  145. args := []string{
  146. "-peer-addr=127.0.0.1:2380",
  147. "-addr=127.0.0.1:2379",
  148. }
  149. wcfg := NewConfig()
  150. wcfg.lpurls = []url.URL{{Scheme: "http", Host: "[::]:2380"}}
  151. wcfg.apurls = []url.URL{{Scheme: "http", Host: "127.0.0.1:2380"}}
  152. wcfg.lcurls = []url.URL{{Scheme: "http", Host: "[::]:2379"}}
  153. wcfg.acurls = []url.URL{{Scheme: "http", Host: "127.0.0.1:2379"}}
  154. cfg := NewConfig()
  155. if err := cfg.Parse(args); err != nil {
  156. t.Fatal(err)
  157. }
  158. if !reflect.DeepEqual(cfg.lpurls, wcfg.lpurls) {
  159. t.Errorf("listen peer urls = %+v, want %+v", cfg.lpurls, wcfg.lpurls)
  160. }
  161. if !reflect.DeepEqual(cfg.apurls, wcfg.apurls) {
  162. t.Errorf("advertise peer urls = %+v, want %+v", cfg.apurls, wcfg.apurls)
  163. }
  164. if !reflect.DeepEqual(cfg.lcurls, wcfg.lcurls) {
  165. t.Errorf("listen client urls = %+v, want %+v", cfg.lcurls, wcfg.lcurls)
  166. }
  167. if !reflect.DeepEqual(cfg.acurls, wcfg.acurls) {
  168. t.Errorf("advertise client urls = %+v, want %+v", cfg.acurls, wcfg.acurls)
  169. }
  170. }
  171. func TestConfigParsingConflictClusteringFlags(t *testing.T) {
  172. conflictArgs := [][]string{
  173. []string{
  174. "-initial-cluster=0=localhost:8000",
  175. "-discovery=http://example.com/abc",
  176. },
  177. []string{
  178. "-discovery-srv=example.com",
  179. "-discovery=http://example.com/abc",
  180. },
  181. []string{
  182. "-initial-cluster=0=localhost:8000",
  183. "-discovery-srv=example.com",
  184. },
  185. []string{
  186. "-initial-cluster=0=localhost:8000",
  187. "-discovery=http://example.com/abc",
  188. "-discovery-srv=example.com",
  189. },
  190. }
  191. for i, tt := range conflictArgs {
  192. cfg := NewConfig()
  193. err := cfg.Parse(tt)
  194. if err != ErrConflictBootstrapFlags {
  195. t.Errorf("%d: err = %v, want %v", i, err, ErrConflictBootstrapFlags)
  196. }
  197. }
  198. }
  199. func TestConfigIsNewCluster(t *testing.T) {
  200. tests := []struct {
  201. state string
  202. wIsNew bool
  203. }{
  204. {clusterStateFlagExisting, false},
  205. {clusterStateFlagNew, true},
  206. }
  207. for i, tt := range tests {
  208. cfg := NewConfig()
  209. if err := cfg.clusterState.Set(tt.state); err != nil {
  210. t.Fatalf("#%d: unexpected clusterState.Set error: %v", i, err)
  211. }
  212. if g := cfg.isNewCluster(); g != tt.wIsNew {
  213. t.Errorf("#%d: isNewCluster = %v, want %v", i, g, tt.wIsNew)
  214. }
  215. }
  216. }
  217. func TestConfigIsProxy(t *testing.T) {
  218. tests := []struct {
  219. proxy string
  220. wIsProxy bool
  221. }{
  222. {proxyFlagOff, false},
  223. {proxyFlagReadonly, true},
  224. {proxyFlagOn, true},
  225. }
  226. for i, tt := range tests {
  227. cfg := NewConfig()
  228. if err := cfg.proxy.Set(tt.proxy); err != nil {
  229. t.Fatalf("#%d: unexpected proxy.Set error: %v", i, err)
  230. }
  231. if g := cfg.isProxy(); g != tt.wIsProxy {
  232. t.Errorf("#%d: isProxy = %v, want %v", i, g, tt.wIsProxy)
  233. }
  234. }
  235. }
  236. func TestConfigIsReadonlyProxy(t *testing.T) {
  237. tests := []struct {
  238. proxy string
  239. wIsReadonly bool
  240. }{
  241. {proxyFlagOff, false},
  242. {proxyFlagReadonly, true},
  243. {proxyFlagOn, false},
  244. }
  245. for i, tt := range tests {
  246. cfg := NewConfig()
  247. if err := cfg.proxy.Set(tt.proxy); err != nil {
  248. t.Fatalf("#%d: unexpected proxy.Set error: %v", i, err)
  249. }
  250. if g := cfg.isReadonlyProxy(); g != tt.wIsReadonly {
  251. t.Errorf("#%d: isReadonlyProxy = %v, want %v", i, g, tt.wIsReadonly)
  252. }
  253. }
  254. }
  255. func TestConfigShouldFallbackToProxy(t *testing.T) {
  256. tests := []struct {
  257. fallback string
  258. wFallback bool
  259. }{
  260. {fallbackFlagProxy, true},
  261. {fallbackFlagExit, false},
  262. }
  263. for i, tt := range tests {
  264. cfg := NewConfig()
  265. if err := cfg.fallback.Set(tt.fallback); err != nil {
  266. t.Fatalf("#%d: unexpected fallback.Set error: %v", i, err)
  267. }
  268. if g := cfg.shouldFallbackToProxy(); g != tt.wFallback {
  269. t.Errorf("#%d: shouldFallbackToProxy = %v, want %v", i, g, tt.wFallback)
  270. }
  271. }
  272. }