config_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. // Copyright 2015 The etcd Authors
  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. "fmt"
  17. "io/ioutil"
  18. "net/url"
  19. "os"
  20. "reflect"
  21. "strings"
  22. "testing"
  23. "github.com/ghodss/yaml"
  24. )
  25. func TestConfigParsingMemberFlags(t *testing.T) {
  26. args := []string{
  27. "-data-dir=testdir",
  28. "-name=testname",
  29. "-max-wals=10",
  30. "-max-snapshots=10",
  31. "-snapshot-count=10",
  32. "-listen-peer-urls=http://localhost:8000,https://localhost:8001",
  33. "-listen-client-urls=http://localhost:7000,https://localhost:7001",
  34. // it should be set if -listen-client-urls is set
  35. "-advertise-client-urls=http://localhost:7000,https://localhost:7001",
  36. }
  37. cfg := NewConfig()
  38. err := cfg.Parse(args)
  39. if err != nil {
  40. t.Fatal(err)
  41. }
  42. validateMemberFlags(t, cfg)
  43. }
  44. func TestConfigFileMemberFields(t *testing.T) {
  45. yc := struct {
  46. Dir string `json:"data-dir"`
  47. MaxSnapFiles uint `json:"max-snapshots"`
  48. MaxWalFiles uint `json:"max-wals"`
  49. Name string `json:"name"`
  50. SnapCount uint64 `json:"snapshot-count"`
  51. LPUrls string `json:"listen-peer-urls"`
  52. LCUrls string `json:"listen-client-urls"`
  53. AcurlsCfgFile string `json:"advertise-client-urls"`
  54. }{
  55. "testdir",
  56. 10,
  57. 10,
  58. "testname",
  59. 10,
  60. "http://localhost:8000,https://localhost:8001",
  61. "http://localhost:7000,https://localhost:7001",
  62. "http://localhost:7000,https://localhost:7001",
  63. }
  64. b, err := yaml.Marshal(&yc)
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. tmpfile := mustCreateCfgFile(t, b)
  69. defer os.Remove(tmpfile.Name())
  70. args := []string{
  71. fmt.Sprintf("--config-file=%s", tmpfile.Name()),
  72. }
  73. cfg := NewConfig()
  74. err = cfg.Parse(args)
  75. if err != nil {
  76. t.Fatal(err)
  77. }
  78. validateMemberFlags(t, cfg)
  79. }
  80. func TestConfigParsingClusteringFlags(t *testing.T) {
  81. args := []string{
  82. "-initial-cluster=0=http://localhost:8000",
  83. "-initial-cluster-state=existing",
  84. "-initial-cluster-token=etcdtest",
  85. "-initial-advertise-peer-urls=http://localhost:8000,https://localhost:8001",
  86. "-advertise-client-urls=http://localhost:7000,https://localhost:7001",
  87. "-discovery-fallback=exit",
  88. }
  89. cfg := NewConfig()
  90. err := cfg.Parse(args)
  91. if err != nil {
  92. t.Fatal(err)
  93. }
  94. validateClusteringFlags(t, cfg)
  95. }
  96. func TestConfigFileClusteringFields(t *testing.T) {
  97. yc := struct {
  98. InitialCluster string `json:"initial-cluster"`
  99. ClusterState string `json:"initial-cluster-state"`
  100. InitialClusterToken string `json:"initial-cluster-token"`
  101. Apurls string `json:"initial-advertise-peer-urls"`
  102. Acurls string `json:"advertise-client-urls"`
  103. Fallback string `json:"discovery-fallback"`
  104. }{
  105. "0=http://localhost:8000",
  106. "existing",
  107. "etcdtest",
  108. "http://localhost:8000,https://localhost:8001",
  109. "http://localhost:7000,https://localhost:7001",
  110. "exit",
  111. }
  112. b, err := yaml.Marshal(&yc)
  113. if err != nil {
  114. t.Fatal(err)
  115. }
  116. tmpfile := mustCreateCfgFile(t, b)
  117. defer os.Remove(tmpfile.Name())
  118. args := []string{
  119. fmt.Sprintf("--config-file=%s", tmpfile.Name()),
  120. }
  121. cfg := NewConfig()
  122. err = cfg.Parse(args)
  123. if err != nil {
  124. t.Fatal(err)
  125. }
  126. validateClusteringFlags(t, cfg)
  127. }
  128. func TestConfigParsingOtherFlags(t *testing.T) {
  129. args := []string{
  130. "-proxy=readonly",
  131. "-ca-file=cafile",
  132. "-cert-file=certfile",
  133. "-key-file=keyfile",
  134. "-peer-ca-file=peercafile",
  135. "-peer-cert-file=peercertfile",
  136. "-peer-key-file=peerkeyfile",
  137. "-force-new-cluster=true",
  138. }
  139. cfg := NewConfig()
  140. err := cfg.Parse(args)
  141. if err != nil {
  142. t.Fatal(err)
  143. }
  144. validateOtherFlags(t, cfg)
  145. }
  146. func TestConfigFileOtherFields(t *testing.T) {
  147. yc := struct {
  148. ProxyCfgFile string `json:"proxy"`
  149. ClientSecurityCfgFile securityConfig `json:"client-transport-security"`
  150. PeerSecurityCfgFile securityConfig `json:"peer-transport-security"`
  151. ForceNewCluster bool `json:"force-new-cluster"`
  152. }{
  153. "readonly",
  154. securityConfig{
  155. CAFile: "cafile",
  156. CertFile: "certfile",
  157. KeyFile: "keyfile",
  158. },
  159. securityConfig{
  160. CAFile: "peercafile",
  161. CertFile: "peercertfile",
  162. KeyFile: "peerkeyfile",
  163. },
  164. true,
  165. }
  166. b, err := yaml.Marshal(&yc)
  167. if err != nil {
  168. t.Fatal(err)
  169. }
  170. tmpfile := mustCreateCfgFile(t, b)
  171. defer os.Remove(tmpfile.Name())
  172. args := []string{
  173. fmt.Sprintf("--config-file=%s", tmpfile.Name()),
  174. }
  175. cfg := NewConfig()
  176. err = cfg.Parse(args)
  177. if err != nil {
  178. t.Fatal(err)
  179. }
  180. validateOtherFlags(t, cfg)
  181. }
  182. func TestConfigParsingV1Flags(t *testing.T) {
  183. args := []string{
  184. "-peer-addr=127.0.0.1:2380",
  185. "-addr=127.0.0.1:2379",
  186. }
  187. wcfg := NewConfig()
  188. wcfg.lpurls = []url.URL{{Scheme: "http", Host: "[::]:2380"}}
  189. wcfg.apurls = []url.URL{{Scheme: "http", Host: "127.0.0.1:2380"}}
  190. wcfg.lcurls = []url.URL{{Scheme: "http", Host: "[::]:2379"}}
  191. wcfg.acurls = []url.URL{{Scheme: "http", Host: "127.0.0.1:2379"}}
  192. cfg := NewConfig()
  193. if err := cfg.Parse(args); err != nil {
  194. t.Fatal(err)
  195. }
  196. if !reflect.DeepEqual(cfg.lpurls, wcfg.lpurls) {
  197. t.Errorf("listen peer urls = %+v, want %+v", cfg.lpurls, wcfg.lpurls)
  198. }
  199. if !reflect.DeepEqual(cfg.apurls, wcfg.apurls) {
  200. t.Errorf("advertise peer urls = %+v, want %+v", cfg.apurls, wcfg.apurls)
  201. }
  202. if !reflect.DeepEqual(cfg.lcurls, wcfg.lcurls) {
  203. t.Errorf("listen client urls = %+v, want %+v", cfg.lcurls, wcfg.lcurls)
  204. }
  205. if !reflect.DeepEqual(cfg.acurls, wcfg.acurls) {
  206. t.Errorf("advertise client urls = %+v, want %+v", cfg.acurls, wcfg.acurls)
  207. }
  208. }
  209. func TestConfigParsingConflictClusteringFlags(t *testing.T) {
  210. conflictArgs := [][]string{
  211. {
  212. "-initial-cluster=0=localhost:8000",
  213. "-discovery=http://example.com/abc",
  214. },
  215. {
  216. "-discovery-srv=example.com",
  217. "-discovery=http://example.com/abc",
  218. },
  219. {
  220. "-initial-cluster=0=localhost:8000",
  221. "-discovery-srv=example.com",
  222. },
  223. {
  224. "-initial-cluster=0=localhost:8000",
  225. "-discovery=http://example.com/abc",
  226. "-discovery-srv=example.com",
  227. },
  228. }
  229. for i, tt := range conflictArgs {
  230. cfg := NewConfig()
  231. err := cfg.Parse(tt)
  232. if err != ErrConflictBootstrapFlags {
  233. t.Errorf("%d: err = %v, want %v", i, err, ErrConflictBootstrapFlags)
  234. }
  235. }
  236. }
  237. func TestConfigFileConflictClusteringFlags(t *testing.T) {
  238. tests := []struct {
  239. InitialCluster string `json:"initial-cluster"`
  240. DnsCluster string `json:"discovery-srv"`
  241. Durl string `json:"discovery"`
  242. }{
  243. {
  244. InitialCluster: "0=localhost:8000",
  245. Durl: "http://example.com/abc",
  246. },
  247. {
  248. DnsCluster: "example.com",
  249. Durl: "http://example.com/abc",
  250. },
  251. {
  252. InitialCluster: "0=localhost:8000",
  253. DnsCluster: "example.com",
  254. },
  255. {
  256. InitialCluster: "0=localhost:8000",
  257. Durl: "http://example.com/abc",
  258. DnsCluster: "example.com",
  259. },
  260. }
  261. for i, tt := range tests {
  262. b, err := yaml.Marshal(&tt)
  263. if err != nil {
  264. t.Fatal(err)
  265. }
  266. tmpfile := mustCreateCfgFile(t, b)
  267. defer os.Remove(tmpfile.Name())
  268. args := []string{
  269. fmt.Sprintf("--config-file=%s", tmpfile.Name()),
  270. }
  271. cfg := NewConfig()
  272. err = cfg.Parse(args)
  273. if err != ErrConflictBootstrapFlags {
  274. t.Errorf("%d: err = %v, want %v", i, err, ErrConflictBootstrapFlags)
  275. }
  276. }
  277. }
  278. func TestConfigParsingMissedAdvertiseClientURLsFlag(t *testing.T) {
  279. tests := []struct {
  280. args []string
  281. werr error
  282. }{
  283. {
  284. []string{
  285. "-initial-cluster=infra1=http://127.0.0.1:2380",
  286. "-listen-client-urls=http://127.0.0.1:2379",
  287. },
  288. errUnsetAdvertiseClientURLsFlag,
  289. },
  290. {
  291. []string{
  292. "-discovery-srv=example.com",
  293. "-listen-client-urls=http://127.0.0.1:2379",
  294. },
  295. errUnsetAdvertiseClientURLsFlag,
  296. },
  297. {
  298. []string{
  299. "-discovery=http://example.com/abc",
  300. "-discovery-fallback=exit",
  301. "-listen-client-urls=http://127.0.0.1:2379",
  302. },
  303. errUnsetAdvertiseClientURLsFlag,
  304. },
  305. {
  306. []string{
  307. "-listen-client-urls=http://127.0.0.1:2379",
  308. },
  309. errUnsetAdvertiseClientURLsFlag,
  310. },
  311. {
  312. []string{
  313. "-discovery=http://example.com/abc",
  314. "-listen-client-urls=http://127.0.0.1:2379",
  315. },
  316. nil,
  317. },
  318. {
  319. []string{
  320. "-proxy=on",
  321. "-listen-client-urls=http://127.0.0.1:2379",
  322. },
  323. nil,
  324. },
  325. {
  326. []string{
  327. "-proxy=readonly",
  328. "-listen-client-urls=http://127.0.0.1:2379",
  329. },
  330. nil,
  331. },
  332. }
  333. for i, tt := range tests {
  334. cfg := NewConfig()
  335. err := cfg.Parse(tt.args)
  336. if err != tt.werr {
  337. t.Errorf("%d: err = %v, want %v", i, err, tt.werr)
  338. }
  339. }
  340. }
  341. func TestConfigIsNewCluster(t *testing.T) {
  342. tests := []struct {
  343. state string
  344. wIsNew bool
  345. }{
  346. {clusterStateFlagExisting, false},
  347. {clusterStateFlagNew, true},
  348. }
  349. for i, tt := range tests {
  350. cfg := NewConfig()
  351. if err := cfg.clusterState.Set(tt.state); err != nil {
  352. t.Fatalf("#%d: unexpected clusterState.Set error: %v", i, err)
  353. }
  354. if g := cfg.isNewCluster(); g != tt.wIsNew {
  355. t.Errorf("#%d: isNewCluster = %v, want %v", i, g, tt.wIsNew)
  356. }
  357. }
  358. }
  359. func TestConfigIsProxy(t *testing.T) {
  360. tests := []struct {
  361. proxy string
  362. wIsProxy bool
  363. }{
  364. {proxyFlagOff, false},
  365. {proxyFlagReadonly, true},
  366. {proxyFlagOn, true},
  367. }
  368. for i, tt := range tests {
  369. cfg := NewConfig()
  370. if err := cfg.proxy.Set(tt.proxy); err != nil {
  371. t.Fatalf("#%d: unexpected proxy.Set error: %v", i, err)
  372. }
  373. if g := cfg.isProxy(); g != tt.wIsProxy {
  374. t.Errorf("#%d: isProxy = %v, want %v", i, g, tt.wIsProxy)
  375. }
  376. }
  377. }
  378. func TestConfigIsReadonlyProxy(t *testing.T) {
  379. tests := []struct {
  380. proxy string
  381. wIsReadonly bool
  382. }{
  383. {proxyFlagOff, false},
  384. {proxyFlagReadonly, true},
  385. {proxyFlagOn, false},
  386. }
  387. for i, tt := range tests {
  388. cfg := NewConfig()
  389. if err := cfg.proxy.Set(tt.proxy); err != nil {
  390. t.Fatalf("#%d: unexpected proxy.Set error: %v", i, err)
  391. }
  392. if g := cfg.isReadonlyProxy(); g != tt.wIsReadonly {
  393. t.Errorf("#%d: isReadonlyProxy = %v, want %v", i, g, tt.wIsReadonly)
  394. }
  395. }
  396. }
  397. func TestConfigShouldFallbackToProxy(t *testing.T) {
  398. tests := []struct {
  399. fallback string
  400. wFallback bool
  401. }{
  402. {fallbackFlagProxy, true},
  403. {fallbackFlagExit, false},
  404. }
  405. for i, tt := range tests {
  406. cfg := NewConfig()
  407. if err := cfg.fallback.Set(tt.fallback); err != nil {
  408. t.Fatalf("#%d: unexpected fallback.Set error: %v", i, err)
  409. }
  410. if g := cfg.shouldFallbackToProxy(); g != tt.wFallback {
  411. t.Errorf("#%d: shouldFallbackToProxy = %v, want %v", i, g, tt.wFallback)
  412. }
  413. }
  414. }
  415. func TestConfigFileElectionTimeout(t *testing.T) {
  416. tests := []struct {
  417. TickMs uint `json:"heartbeat-interval"`
  418. ElectionMs uint `json:"election-timeout"`
  419. errStr string
  420. }{
  421. {
  422. ElectionMs: 1000,
  423. TickMs: 800,
  424. errStr: "should be at least as 5 times as",
  425. },
  426. {
  427. ElectionMs: 60000,
  428. errStr: "is too long, and should be set less than",
  429. },
  430. }
  431. for i, tt := range tests {
  432. b, err := yaml.Marshal(&tt)
  433. if err != nil {
  434. t.Fatal(err)
  435. }
  436. tmpfile := mustCreateCfgFile(t, b)
  437. defer os.Remove(tmpfile.Name())
  438. args := []string{
  439. fmt.Sprintf("--config-file=%s", tmpfile.Name()),
  440. }
  441. cfg := NewConfig()
  442. err = cfg.Parse(args)
  443. if !strings.Contains(err.Error(), tt.errStr) {
  444. t.Errorf("%d: Wrong err = %v", i, err)
  445. }
  446. }
  447. }
  448. func mustCreateCfgFile(t *testing.T, b []byte) *os.File {
  449. tmpfile, err := ioutil.TempFile("", "servercfg")
  450. if err != nil {
  451. t.Fatal(err)
  452. }
  453. _, err = tmpfile.Write(b)
  454. if err != nil {
  455. t.Fatal(err)
  456. }
  457. err = tmpfile.Close()
  458. if err != nil {
  459. t.Fatal(err)
  460. }
  461. return tmpfile
  462. }
  463. func validateMemberFlags(t *testing.T, cfg *config) {
  464. wcfg := &config{
  465. Dir: "testdir",
  466. lpurls: []url.URL{{Scheme: "http", Host: "localhost:8000"}, {Scheme: "https", Host: "localhost:8001"}},
  467. lcurls: []url.URL{{Scheme: "http", Host: "localhost:7000"}, {Scheme: "https", Host: "localhost:7001"}},
  468. MaxSnapFiles: 10,
  469. MaxWalFiles: 10,
  470. Name: "testname",
  471. SnapCount: 10,
  472. }
  473. if cfg.Dir != wcfg.Dir {
  474. t.Errorf("dir = %v, want %v", cfg.Dir, wcfg.Dir)
  475. }
  476. if cfg.MaxSnapFiles != wcfg.MaxSnapFiles {
  477. t.Errorf("maxsnap = %v, want %v", cfg.MaxSnapFiles, wcfg.MaxSnapFiles)
  478. }
  479. if cfg.MaxWalFiles != wcfg.MaxWalFiles {
  480. t.Errorf("maxwal = %v, want %v", cfg.MaxWalFiles, wcfg.MaxWalFiles)
  481. }
  482. if cfg.Name != wcfg.Name {
  483. t.Errorf("name = %v, want %v", cfg.Name, wcfg.Name)
  484. }
  485. if cfg.SnapCount != wcfg.SnapCount {
  486. t.Errorf("snapcount = %v, want %v", cfg.SnapCount, wcfg.SnapCount)
  487. }
  488. if !reflect.DeepEqual(cfg.lpurls, wcfg.lpurls) {
  489. t.Errorf("listen-peer-urls = %v, want %v", cfg.lpurls, wcfg.lpurls)
  490. }
  491. if !reflect.DeepEqual(cfg.lcurls, wcfg.lcurls) {
  492. t.Errorf("listen-client-urls = %v, want %v", cfg.lcurls, wcfg.lcurls)
  493. }
  494. }
  495. func validateClusteringFlags(t *testing.T, cfg *config) {
  496. wcfg := NewConfig()
  497. wcfg.apurls = []url.URL{{Scheme: "http", Host: "localhost:8000"}, {Scheme: "https", Host: "localhost:8001"}}
  498. wcfg.acurls = []url.URL{{Scheme: "http", Host: "localhost:7000"}, {Scheme: "https", Host: "localhost:7001"}}
  499. wcfg.clusterState.Set(clusterStateFlagExisting)
  500. wcfg.fallback.Set(fallbackFlagExit)
  501. wcfg.InitialCluster = "0=http://localhost:8000"
  502. wcfg.InitialClusterToken = "etcdtest"
  503. if cfg.clusterState.String() != wcfg.clusterState.String() {
  504. t.Errorf("clusterState = %v, want %v", cfg.clusterState, wcfg.clusterState)
  505. }
  506. if cfg.fallback.String() != wcfg.fallback.String() {
  507. t.Errorf("fallback = %v, want %v", cfg.fallback, wcfg.fallback)
  508. }
  509. if cfg.InitialCluster != wcfg.InitialCluster {
  510. t.Errorf("initialCluster = %v, want %v", cfg.InitialCluster, wcfg.InitialCluster)
  511. }
  512. if cfg.InitialClusterToken != wcfg.InitialClusterToken {
  513. t.Errorf("initialClusterToken = %v, want %v", cfg.InitialClusterToken, wcfg.InitialClusterToken)
  514. }
  515. if !reflect.DeepEqual(cfg.apurls, wcfg.apurls) {
  516. t.Errorf("initial-advertise-peer-urls = %v, want %v", cfg.lpurls, wcfg.lpurls)
  517. }
  518. if !reflect.DeepEqual(cfg.acurls, wcfg.acurls) {
  519. t.Errorf("advertise-client-urls = %v, want %v", cfg.lcurls, wcfg.lcurls)
  520. }
  521. }
  522. func validateOtherFlags(t *testing.T, cfg *config) {
  523. wcfg := NewConfig()
  524. wcfg.proxy.Set(proxyFlagReadonly)
  525. wcfg.clientTLSInfo.CAFile = "cafile"
  526. wcfg.clientTLSInfo.CertFile = "certfile"
  527. wcfg.clientTLSInfo.KeyFile = "keyfile"
  528. wcfg.peerTLSInfo.CAFile = "peercafile"
  529. wcfg.peerTLSInfo.CertFile = "peercertfile"
  530. wcfg.peerTLSInfo.KeyFile = "peerkeyfile"
  531. wcfg.ForceNewCluster = true
  532. if cfg.proxy.String() != wcfg.proxy.String() {
  533. t.Errorf("proxy = %v, want %v", cfg.proxy, wcfg.proxy)
  534. }
  535. if cfg.clientTLSInfo.String() != wcfg.clientTLSInfo.String() {
  536. t.Errorf("clientTLS = %v, want %v", cfg.clientTLSInfo, wcfg.clientTLSInfo)
  537. }
  538. if cfg.peerTLSInfo.String() != wcfg.peerTLSInfo.String() {
  539. t.Errorf("peerTLS = %v, want %v", cfg.peerTLSInfo, wcfg.peerTLSInfo)
  540. }
  541. if cfg.ForceNewCluster != wcfg.ForceNewCluster {
  542. t.Errorf("forceNewCluster = %t, want %t", cfg.ForceNewCluster, wcfg.ForceNewCluster)
  543. }
  544. }