config_test.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. // it should be set if -listen-client-urls is set
  30. "-advertise-client-urls=http://localhost:7000,https://localhost:7001",
  31. }
  32. wcfg := &config{
  33. dir: "testdir",
  34. lpurls: []url.URL{{Scheme: "http", Host: "localhost:8000"}, {Scheme: "https", Host: "localhost:8001"}},
  35. lcurls: []url.URL{{Scheme: "http", Host: "localhost:7000"}, {Scheme: "https", Host: "localhost:7001"}},
  36. maxSnapFiles: 10,
  37. maxWalFiles: 10,
  38. name: "testname",
  39. snapCount: 10,
  40. }
  41. cfg := NewConfig()
  42. err := cfg.Parse(args)
  43. if err != nil {
  44. t.Fatal(err)
  45. }
  46. if cfg.dir != wcfg.dir {
  47. t.Errorf("dir = %v, want %v", cfg.dir, wcfg.dir)
  48. }
  49. if cfg.maxSnapFiles != wcfg.maxSnapFiles {
  50. t.Errorf("maxsnap = %v, want %v", cfg.maxSnapFiles, wcfg.maxSnapFiles)
  51. }
  52. if cfg.maxWalFiles != wcfg.maxWalFiles {
  53. t.Errorf("maxwal = %v, want %v", cfg.maxWalFiles, wcfg.maxWalFiles)
  54. }
  55. if cfg.name != wcfg.name {
  56. t.Errorf("name = %v, want %v", cfg.name, wcfg.name)
  57. }
  58. if cfg.snapCount != wcfg.snapCount {
  59. t.Errorf("snapcount = %v, want %v", cfg.snapCount, wcfg.snapCount)
  60. }
  61. if !reflect.DeepEqual(cfg.lpurls, wcfg.lpurls) {
  62. t.Errorf("listen-peer-urls = %v, want %v", cfg.lpurls, wcfg.lpurls)
  63. }
  64. if !reflect.DeepEqual(cfg.lcurls, wcfg.lcurls) {
  65. t.Errorf("listen-client-urls = %v, want %v", cfg.lcurls, wcfg.lcurls)
  66. }
  67. }
  68. func TestConfigParsingClusteringFlags(t *testing.T) {
  69. args := []string{
  70. "-initial-cluster=0=http://localhost:8000",
  71. "-initial-cluster-state=existing",
  72. "-initial-cluster-token=etcdtest",
  73. "-initial-advertise-peer-urls=http://localhost:8000,https://localhost:8001",
  74. "-advertise-client-urls=http://localhost:7000,https://localhost:7001",
  75. "-discovery-fallback=exit",
  76. }
  77. wcfg := NewConfig()
  78. wcfg.apurls = []url.URL{{Scheme: "http", Host: "localhost:8000"}, {Scheme: "https", Host: "localhost:8001"}}
  79. wcfg.acurls = []url.URL{{Scheme: "http", Host: "localhost:7000"}, {Scheme: "https", Host: "localhost:7001"}}
  80. wcfg.clusterState.Set(clusterStateFlagExisting)
  81. wcfg.fallback.Set(fallbackFlagExit)
  82. wcfg.initialCluster = "0=http://localhost:8000"
  83. wcfg.initialClusterToken = "etcdtest"
  84. cfg := NewConfig()
  85. err := cfg.Parse(args)
  86. if err != nil {
  87. t.Fatal(err)
  88. }
  89. if cfg.clusterState.String() != wcfg.clusterState.String() {
  90. t.Errorf("clusterState = %v, want %v", cfg.clusterState, wcfg.clusterState)
  91. }
  92. if cfg.fallback.String() != wcfg.fallback.String() {
  93. t.Errorf("fallback = %v, want %v", cfg.fallback, wcfg.fallback)
  94. }
  95. if cfg.initialCluster != wcfg.initialCluster {
  96. t.Errorf("initialCluster = %v, want %v", cfg.initialCluster, wcfg.initialCluster)
  97. }
  98. if cfg.initialClusterToken != wcfg.initialClusterToken {
  99. t.Errorf("initialClusterToken = %v, want %v", cfg.initialClusterToken, wcfg.initialClusterToken)
  100. }
  101. if !reflect.DeepEqual(cfg.apurls, wcfg.apurls) {
  102. t.Errorf("initial-advertise-peer-urls = %v, want %v", cfg.lpurls, wcfg.lpurls)
  103. }
  104. if !reflect.DeepEqual(cfg.acurls, wcfg.acurls) {
  105. t.Errorf("advertise-client-urls = %v, want %v", cfg.lcurls, wcfg.lcurls)
  106. }
  107. }
  108. func TestConfigParsingOtherFlags(t *testing.T) {
  109. args := []string{
  110. "-proxy=readonly",
  111. "-ca-file=cafile",
  112. "-cert-file=certfile",
  113. "-key-file=keyfile",
  114. "-peer-ca-file=peercafile",
  115. "-peer-cert-file=peercertfile",
  116. "-peer-key-file=peerkeyfile",
  117. "-force-new-cluster=true",
  118. }
  119. wcfg := NewConfig()
  120. wcfg.proxy.Set(proxyFlagReadonly)
  121. wcfg.clientTLSInfo.CAFile = "cafile"
  122. wcfg.clientTLSInfo.CertFile = "certfile"
  123. wcfg.clientTLSInfo.KeyFile = "keyfile"
  124. wcfg.peerTLSInfo.CAFile = "peercafile"
  125. wcfg.peerTLSInfo.CertFile = "peercertfile"
  126. wcfg.peerTLSInfo.KeyFile = "peerkeyfile"
  127. wcfg.forceNewCluster = true
  128. cfg := NewConfig()
  129. err := cfg.Parse(args)
  130. if err != nil {
  131. t.Fatal(err)
  132. }
  133. if cfg.proxy.String() != wcfg.proxy.String() {
  134. t.Errorf("proxy = %v, want %v", cfg.proxy, wcfg.proxy)
  135. }
  136. if cfg.clientTLSInfo.String() != wcfg.clientTLSInfo.String() {
  137. t.Errorf("clientTLS = %v, want %v", cfg.clientTLSInfo, wcfg.clientTLSInfo)
  138. }
  139. if cfg.peerTLSInfo.String() != wcfg.peerTLSInfo.String() {
  140. t.Errorf("peerTLS = %v, want %v", cfg.peerTLSInfo, wcfg.peerTLSInfo)
  141. }
  142. if cfg.forceNewCluster != wcfg.forceNewCluster {
  143. t.Errorf("forceNewCluster = %t, want %t", cfg.forceNewCluster, wcfg.forceNewCluster)
  144. }
  145. }
  146. func TestConfigParsingV1Flags(t *testing.T) {
  147. args := []string{
  148. "-peer-addr=127.0.0.1:7001",
  149. "-addr=127.0.0.1:4001",
  150. }
  151. wcfg := NewConfig()
  152. wcfg.lpurls = []url.URL{{Scheme: "http", Host: "[::]:7001"}}
  153. wcfg.apurls = []url.URL{{Scheme: "http", Host: "127.0.0.1:7001"}}
  154. wcfg.lcurls = []url.URL{{Scheme: "http", Host: "[::]:4001"}}
  155. wcfg.acurls = []url.URL{{Scheme: "http", Host: "127.0.0.1:4001"}}
  156. cfg := NewConfig()
  157. if err := cfg.Parse(args); err != nil {
  158. t.Fatal(err)
  159. }
  160. if !reflect.DeepEqual(cfg.lpurls, wcfg.lpurls) {
  161. t.Errorf("listen peer urls = %+v, want %+v", cfg.lpurls, wcfg.lpurls)
  162. }
  163. if !reflect.DeepEqual(cfg.apurls, wcfg.apurls) {
  164. t.Errorf("advertise peer urls = %+v, want %+v", cfg.apurls, wcfg.apurls)
  165. }
  166. if !reflect.DeepEqual(cfg.lcurls, wcfg.lcurls) {
  167. t.Errorf("listen client urls = %+v, want %+v", cfg.lcurls, wcfg.lcurls)
  168. }
  169. if !reflect.DeepEqual(cfg.acurls, wcfg.acurls) {
  170. t.Errorf("advertise client urls = %+v, want %+v", cfg.acurls, wcfg.acurls)
  171. }
  172. }
  173. func TestConfigParsingConflictClusteringFlags(t *testing.T) {
  174. conflictArgs := [][]string{
  175. []string{
  176. "-initial-cluster=0=localhost:8000",
  177. "-discovery=http://example.com/abc",
  178. },
  179. []string{
  180. "-discovery-srv=example.com",
  181. "-discovery=http://example.com/abc",
  182. },
  183. []string{
  184. "-initial-cluster=0=localhost:8000",
  185. "-discovery-srv=example.com",
  186. },
  187. []string{
  188. "-initial-cluster=0=localhost:8000",
  189. "-discovery=http://example.com/abc",
  190. "-discovery-srv=example.com",
  191. },
  192. }
  193. for i, tt := range conflictArgs {
  194. cfg := NewConfig()
  195. err := cfg.Parse(tt)
  196. if err != ErrConflictBootstrapFlags {
  197. t.Errorf("%d: err = %v, want %v", i, err, ErrConflictBootstrapFlags)
  198. }
  199. }
  200. }
  201. func TestConfigParsingMissedAdvertiseClientURLsFlag(t *testing.T) {
  202. tests := []struct {
  203. args []string
  204. werr error
  205. }{
  206. {
  207. []string{
  208. "-initial-cluster=infra1=http://127.0.0.1:2380",
  209. "-listen-client-urls=http://127.0.0.1:2379",
  210. },
  211. errUnsetAdvertiseClientURLsFlag,
  212. },
  213. {
  214. []string{
  215. "-discovery-srv=example.com",
  216. "-listen-client-urls=http://127.0.0.1:2379",
  217. },
  218. errUnsetAdvertiseClientURLsFlag,
  219. },
  220. {
  221. []string{
  222. "-discovery=http://example.com/abc",
  223. "-discovery-fallback=exit",
  224. "-listen-client-urls=http://127.0.0.1:2379",
  225. },
  226. errUnsetAdvertiseClientURLsFlag,
  227. },
  228. {
  229. []string{
  230. "-listen-client-urls=http://127.0.0.1:2379",
  231. },
  232. errUnsetAdvertiseClientURLsFlag,
  233. },
  234. {
  235. []string{
  236. "-discovery=http://example.com/abc",
  237. "-listen-client-urls=http://127.0.0.1:2379",
  238. },
  239. nil,
  240. },
  241. {
  242. []string{
  243. "-proxy=on",
  244. "-listen-client-urls=http://127.0.0.1:2379",
  245. },
  246. nil,
  247. },
  248. {
  249. []string{
  250. "-proxy=readonly",
  251. "-listen-client-urls=http://127.0.0.1:2379",
  252. },
  253. nil,
  254. },
  255. }
  256. for i, tt := range tests {
  257. cfg := NewConfig()
  258. err := cfg.Parse(tt.args)
  259. if err != tt.werr {
  260. t.Errorf("%d: err = %v, want %v", i, err, tt.werr)
  261. }
  262. }
  263. }
  264. func TestConfigIsNewCluster(t *testing.T) {
  265. tests := []struct {
  266. state string
  267. wIsNew bool
  268. }{
  269. {clusterStateFlagExisting, false},
  270. {clusterStateFlagNew, true},
  271. }
  272. for i, tt := range tests {
  273. cfg := NewConfig()
  274. if err := cfg.clusterState.Set(tt.state); err != nil {
  275. t.Fatalf("#%d: unexpected clusterState.Set error: %v", i, err)
  276. }
  277. if g := cfg.isNewCluster(); g != tt.wIsNew {
  278. t.Errorf("#%d: isNewCluster = %v, want %v", i, g, tt.wIsNew)
  279. }
  280. }
  281. }
  282. func TestConfigIsProxy(t *testing.T) {
  283. tests := []struct {
  284. proxy string
  285. wIsProxy bool
  286. }{
  287. {proxyFlagOff, false},
  288. {proxyFlagReadonly, true},
  289. {proxyFlagOn, true},
  290. }
  291. for i, tt := range tests {
  292. cfg := NewConfig()
  293. if err := cfg.proxy.Set(tt.proxy); err != nil {
  294. t.Fatalf("#%d: unexpected proxy.Set error: %v", i, err)
  295. }
  296. if g := cfg.isProxy(); g != tt.wIsProxy {
  297. t.Errorf("#%d: isProxy = %v, want %v", i, g, tt.wIsProxy)
  298. }
  299. }
  300. }
  301. func TestConfigIsReadonlyProxy(t *testing.T) {
  302. tests := []struct {
  303. proxy string
  304. wIsReadonly bool
  305. }{
  306. {proxyFlagOff, false},
  307. {proxyFlagReadonly, true},
  308. {proxyFlagOn, false},
  309. }
  310. for i, tt := range tests {
  311. cfg := NewConfig()
  312. if err := cfg.proxy.Set(tt.proxy); err != nil {
  313. t.Fatalf("#%d: unexpected proxy.Set error: %v", i, err)
  314. }
  315. if g := cfg.isReadonlyProxy(); g != tt.wIsReadonly {
  316. t.Errorf("#%d: isReadonlyProxy = %v, want %v", i, g, tt.wIsReadonly)
  317. }
  318. }
  319. }
  320. func TestConfigShouldFallbackToProxy(t *testing.T) {
  321. tests := []struct {
  322. fallback string
  323. wFallback bool
  324. }{
  325. {fallbackFlagProxy, true},
  326. {fallbackFlagExit, false},
  327. }
  328. for i, tt := range tests {
  329. cfg := NewConfig()
  330. if err := cfg.fallback.Set(tt.fallback); err != nil {
  331. t.Fatalf("#%d: unexpected fallback.Set error: %v", i, err)
  332. }
  333. if g := cfg.shouldFallbackToProxy(); g != tt.wFallback {
  334. t.Errorf("#%d: shouldFallbackToProxy = %v, want %v", i, g, tt.wFallback)
  335. }
  336. }
  337. }