config_test.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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:2380",
  149. "-addr=127.0.0.1:2379",
  150. }
  151. wcfg := NewConfig()
  152. wcfg.lpurls = []url.URL{{Scheme: "http", Host: "[::]:2380"}}
  153. wcfg.apurls = []url.URL{{Scheme: "http", Host: "127.0.0.1:2380"}}
  154. wcfg.lcurls = []url.URL{{Scheme: "http", Host: "[::]:2379"}}
  155. wcfg.acurls = []url.URL{{Scheme: "http", Host: "127.0.0.1:2379"}}
  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 TestConfigIsNewCluster(t *testing.T) {
  202. tests := []struct {
  203. state string
  204. wIsNew bool
  205. }{
  206. {clusterStateFlagExisting, false},
  207. {clusterStateFlagNew, true},
  208. }
  209. for i, tt := range tests {
  210. cfg := NewConfig()
  211. if err := cfg.clusterState.Set(tt.state); err != nil {
  212. t.Fatalf("#%d: unexpected clusterState.Set error: %v", i, err)
  213. }
  214. if g := cfg.isNewCluster(); g != tt.wIsNew {
  215. t.Errorf("#%d: isNewCluster = %v, want %v", i, g, tt.wIsNew)
  216. }
  217. }
  218. }
  219. func TestConfigIsProxy(t *testing.T) {
  220. tests := []struct {
  221. proxy string
  222. wIsProxy bool
  223. }{
  224. {proxyFlagOff, false},
  225. {proxyFlagReadonly, true},
  226. {proxyFlagOn, true},
  227. }
  228. for i, tt := range tests {
  229. cfg := NewConfig()
  230. if err := cfg.proxy.Set(tt.proxy); err != nil {
  231. t.Fatalf("#%d: unexpected proxy.Set error: %v", i, err)
  232. }
  233. if g := cfg.isProxy(); g != tt.wIsProxy {
  234. t.Errorf("#%d: isProxy = %v, want %v", i, g, tt.wIsProxy)
  235. }
  236. }
  237. }
  238. func TestConfigIsReadonlyProxy(t *testing.T) {
  239. tests := []struct {
  240. proxy string
  241. wIsReadonly bool
  242. }{
  243. {proxyFlagOff, false},
  244. {proxyFlagReadonly, true},
  245. {proxyFlagOn, false},
  246. }
  247. for i, tt := range tests {
  248. cfg := NewConfig()
  249. if err := cfg.proxy.Set(tt.proxy); err != nil {
  250. t.Fatalf("#%d: unexpected proxy.Set error: %v", i, err)
  251. }
  252. if g := cfg.isReadonlyProxy(); g != tt.wIsReadonly {
  253. t.Errorf("#%d: isReadonlyProxy = %v, want %v", i, g, tt.wIsReadonly)
  254. }
  255. }
  256. }
  257. func TestConfigShouldFallbackToProxy(t *testing.T) {
  258. tests := []struct {
  259. fallback string
  260. wFallback bool
  261. }{
  262. {fallbackFlagProxy, true},
  263. {fallbackFlagExit, false},
  264. }
  265. for i, tt := range tests {
  266. cfg := NewConfig()
  267. if err := cfg.fallback.Set(tt.fallback); err != nil {
  268. t.Fatalf("#%d: unexpected fallback.Set error: %v", i, err)
  269. }
  270. if g := cfg.shouldFallbackToProxy(); g != tt.wFallback {
  271. t.Errorf("#%d: shouldFallbackToProxy = %v, want %v", i, g, tt.wFallback)
  272. }
  273. }
  274. }