config_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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 TestConfigParsingConflictClusteringFlags(t *testing.T) {
  183. conflictArgs := [][]string{
  184. {
  185. "-initial-cluster=0=localhost:8000",
  186. "-discovery=http://example.com/abc",
  187. },
  188. {
  189. "-discovery-srv=example.com",
  190. "-discovery=http://example.com/abc",
  191. },
  192. {
  193. "-initial-cluster=0=localhost:8000",
  194. "-discovery-srv=example.com",
  195. },
  196. {
  197. "-initial-cluster=0=localhost:8000",
  198. "-discovery=http://example.com/abc",
  199. "-discovery-srv=example.com",
  200. },
  201. }
  202. for i, tt := range conflictArgs {
  203. cfg := NewConfig()
  204. err := cfg.Parse(tt)
  205. if err != ErrConflictBootstrapFlags {
  206. t.Errorf("%d: err = %v, want %v", i, err, ErrConflictBootstrapFlags)
  207. }
  208. }
  209. }
  210. func TestConfigFileConflictClusteringFlags(t *testing.T) {
  211. tests := []struct {
  212. InitialCluster string `json:"initial-cluster"`
  213. DnsCluster string `json:"discovery-srv"`
  214. Durl string `json:"discovery"`
  215. }{
  216. {
  217. InitialCluster: "0=localhost:8000",
  218. Durl: "http://example.com/abc",
  219. },
  220. {
  221. DnsCluster: "example.com",
  222. Durl: "http://example.com/abc",
  223. },
  224. {
  225. InitialCluster: "0=localhost:8000",
  226. DnsCluster: "example.com",
  227. },
  228. {
  229. InitialCluster: "0=localhost:8000",
  230. Durl: "http://example.com/abc",
  231. DnsCluster: "example.com",
  232. },
  233. }
  234. for i, tt := range tests {
  235. b, err := yaml.Marshal(&tt)
  236. if err != nil {
  237. t.Fatal(err)
  238. }
  239. tmpfile := mustCreateCfgFile(t, b)
  240. defer os.Remove(tmpfile.Name())
  241. args := []string{
  242. fmt.Sprintf("--config-file=%s", tmpfile.Name()),
  243. }
  244. cfg := NewConfig()
  245. err = cfg.Parse(args)
  246. if err != ErrConflictBootstrapFlags {
  247. t.Errorf("%d: err = %v, want %v", i, err, ErrConflictBootstrapFlags)
  248. }
  249. }
  250. }
  251. func TestConfigParsingMissedAdvertiseClientURLsFlag(t *testing.T) {
  252. tests := []struct {
  253. args []string
  254. werr error
  255. }{
  256. {
  257. []string{
  258. "-initial-cluster=infra1=http://127.0.0.1:2380",
  259. "-listen-client-urls=http://127.0.0.1:2379",
  260. },
  261. errUnsetAdvertiseClientURLsFlag,
  262. },
  263. {
  264. []string{
  265. "-discovery-srv=example.com",
  266. "-listen-client-urls=http://127.0.0.1:2379",
  267. },
  268. errUnsetAdvertiseClientURLsFlag,
  269. },
  270. {
  271. []string{
  272. "-discovery=http://example.com/abc",
  273. "-discovery-fallback=exit",
  274. "-listen-client-urls=http://127.0.0.1:2379",
  275. },
  276. errUnsetAdvertiseClientURLsFlag,
  277. },
  278. {
  279. []string{
  280. "-listen-client-urls=http://127.0.0.1:2379",
  281. },
  282. errUnsetAdvertiseClientURLsFlag,
  283. },
  284. {
  285. []string{
  286. "-discovery=http://example.com/abc",
  287. "-listen-client-urls=http://127.0.0.1:2379",
  288. },
  289. nil,
  290. },
  291. {
  292. []string{
  293. "-proxy=on",
  294. "-listen-client-urls=http://127.0.0.1:2379",
  295. },
  296. nil,
  297. },
  298. {
  299. []string{
  300. "-proxy=readonly",
  301. "-listen-client-urls=http://127.0.0.1:2379",
  302. },
  303. nil,
  304. },
  305. }
  306. for i, tt := range tests {
  307. cfg := NewConfig()
  308. err := cfg.Parse(tt.args)
  309. if err != tt.werr {
  310. t.Errorf("%d: err = %v, want %v", i, err, tt.werr)
  311. }
  312. }
  313. }
  314. func TestConfigIsNewCluster(t *testing.T) {
  315. tests := []struct {
  316. state string
  317. wIsNew bool
  318. }{
  319. {clusterStateFlagExisting, false},
  320. {clusterStateFlagNew, true},
  321. }
  322. for i, tt := range tests {
  323. cfg := NewConfig()
  324. if err := cfg.clusterState.Set(tt.state); err != nil {
  325. t.Fatalf("#%d: unexpected clusterState.Set error: %v", i, err)
  326. }
  327. if g := cfg.isNewCluster(); g != tt.wIsNew {
  328. t.Errorf("#%d: isNewCluster = %v, want %v", i, g, tt.wIsNew)
  329. }
  330. }
  331. }
  332. func TestConfigIsProxy(t *testing.T) {
  333. tests := []struct {
  334. proxy string
  335. wIsProxy bool
  336. }{
  337. {proxyFlagOff, false},
  338. {proxyFlagReadonly, true},
  339. {proxyFlagOn, true},
  340. }
  341. for i, tt := range tests {
  342. cfg := NewConfig()
  343. if err := cfg.proxy.Set(tt.proxy); err != nil {
  344. t.Fatalf("#%d: unexpected proxy.Set error: %v", i, err)
  345. }
  346. if g := cfg.isProxy(); g != tt.wIsProxy {
  347. t.Errorf("#%d: isProxy = %v, want %v", i, g, tt.wIsProxy)
  348. }
  349. }
  350. }
  351. func TestConfigIsReadonlyProxy(t *testing.T) {
  352. tests := []struct {
  353. proxy string
  354. wIsReadonly bool
  355. }{
  356. {proxyFlagOff, false},
  357. {proxyFlagReadonly, true},
  358. {proxyFlagOn, false},
  359. }
  360. for i, tt := range tests {
  361. cfg := NewConfig()
  362. if err := cfg.proxy.Set(tt.proxy); err != nil {
  363. t.Fatalf("#%d: unexpected proxy.Set error: %v", i, err)
  364. }
  365. if g := cfg.isReadonlyProxy(); g != tt.wIsReadonly {
  366. t.Errorf("#%d: isReadonlyProxy = %v, want %v", i, g, tt.wIsReadonly)
  367. }
  368. }
  369. }
  370. func TestConfigShouldFallbackToProxy(t *testing.T) {
  371. tests := []struct {
  372. fallback string
  373. wFallback bool
  374. }{
  375. {fallbackFlagProxy, true},
  376. {fallbackFlagExit, false},
  377. }
  378. for i, tt := range tests {
  379. cfg := NewConfig()
  380. if err := cfg.fallback.Set(tt.fallback); err != nil {
  381. t.Fatalf("#%d: unexpected fallback.Set error: %v", i, err)
  382. }
  383. if g := cfg.shouldFallbackToProxy(); g != tt.wFallback {
  384. t.Errorf("#%d: shouldFallbackToProxy = %v, want %v", i, g, tt.wFallback)
  385. }
  386. }
  387. }
  388. func TestConfigFileElectionTimeout(t *testing.T) {
  389. tests := []struct {
  390. TickMs uint `json:"heartbeat-interval"`
  391. ElectionMs uint `json:"election-timeout"`
  392. errStr string
  393. }{
  394. {
  395. ElectionMs: 1000,
  396. TickMs: 800,
  397. errStr: "should be at least as 5 times as",
  398. },
  399. {
  400. ElectionMs: 60000,
  401. errStr: "is too long, and should be set less than",
  402. },
  403. }
  404. for i, tt := range tests {
  405. b, err := yaml.Marshal(&tt)
  406. if err != nil {
  407. t.Fatal(err)
  408. }
  409. tmpfile := mustCreateCfgFile(t, b)
  410. defer os.Remove(tmpfile.Name())
  411. args := []string{
  412. fmt.Sprintf("--config-file=%s", tmpfile.Name()),
  413. }
  414. cfg := NewConfig()
  415. err = cfg.Parse(args)
  416. if !strings.Contains(err.Error(), tt.errStr) {
  417. t.Errorf("%d: Wrong err = %v", i, err)
  418. }
  419. }
  420. }
  421. func mustCreateCfgFile(t *testing.T, b []byte) *os.File {
  422. tmpfile, err := ioutil.TempFile("", "servercfg")
  423. if err != nil {
  424. t.Fatal(err)
  425. }
  426. _, err = tmpfile.Write(b)
  427. if err != nil {
  428. t.Fatal(err)
  429. }
  430. err = tmpfile.Close()
  431. if err != nil {
  432. t.Fatal(err)
  433. }
  434. return tmpfile
  435. }
  436. func validateMemberFlags(t *testing.T, cfg *config) {
  437. wcfg := &config{
  438. Dir: "testdir",
  439. lpurls: []url.URL{{Scheme: "http", Host: "localhost:8000"}, {Scheme: "https", Host: "localhost:8001"}},
  440. lcurls: []url.URL{{Scheme: "http", Host: "localhost:7000"}, {Scheme: "https", Host: "localhost:7001"}},
  441. MaxSnapFiles: 10,
  442. MaxWalFiles: 10,
  443. Name: "testname",
  444. SnapCount: 10,
  445. }
  446. if cfg.Dir != wcfg.Dir {
  447. t.Errorf("dir = %v, want %v", cfg.Dir, wcfg.Dir)
  448. }
  449. if cfg.MaxSnapFiles != wcfg.MaxSnapFiles {
  450. t.Errorf("maxsnap = %v, want %v", cfg.MaxSnapFiles, wcfg.MaxSnapFiles)
  451. }
  452. if cfg.MaxWalFiles != wcfg.MaxWalFiles {
  453. t.Errorf("maxwal = %v, want %v", cfg.MaxWalFiles, wcfg.MaxWalFiles)
  454. }
  455. if cfg.Name != wcfg.Name {
  456. t.Errorf("name = %v, want %v", cfg.Name, wcfg.Name)
  457. }
  458. if cfg.SnapCount != wcfg.SnapCount {
  459. t.Errorf("snapcount = %v, want %v", cfg.SnapCount, wcfg.SnapCount)
  460. }
  461. if !reflect.DeepEqual(cfg.lpurls, wcfg.lpurls) {
  462. t.Errorf("listen-peer-urls = %v, want %v", cfg.lpurls, wcfg.lpurls)
  463. }
  464. if !reflect.DeepEqual(cfg.lcurls, wcfg.lcurls) {
  465. t.Errorf("listen-client-urls = %v, want %v", cfg.lcurls, wcfg.lcurls)
  466. }
  467. }
  468. func validateClusteringFlags(t *testing.T, cfg *config) {
  469. wcfg := NewConfig()
  470. wcfg.apurls = []url.URL{{Scheme: "http", Host: "localhost:8000"}, {Scheme: "https", Host: "localhost:8001"}}
  471. wcfg.acurls = []url.URL{{Scheme: "http", Host: "localhost:7000"}, {Scheme: "https", Host: "localhost:7001"}}
  472. wcfg.clusterState.Set(clusterStateFlagExisting)
  473. wcfg.fallback.Set(fallbackFlagExit)
  474. wcfg.InitialCluster = "0=http://localhost:8000"
  475. wcfg.InitialClusterToken = "etcdtest"
  476. if cfg.clusterState.String() != wcfg.clusterState.String() {
  477. t.Errorf("clusterState = %v, want %v", cfg.clusterState, wcfg.clusterState)
  478. }
  479. if cfg.fallback.String() != wcfg.fallback.String() {
  480. t.Errorf("fallback = %v, want %v", cfg.fallback, wcfg.fallback)
  481. }
  482. if cfg.InitialCluster != wcfg.InitialCluster {
  483. t.Errorf("initialCluster = %v, want %v", cfg.InitialCluster, wcfg.InitialCluster)
  484. }
  485. if cfg.InitialClusterToken != wcfg.InitialClusterToken {
  486. t.Errorf("initialClusterToken = %v, want %v", cfg.InitialClusterToken, wcfg.InitialClusterToken)
  487. }
  488. if !reflect.DeepEqual(cfg.apurls, wcfg.apurls) {
  489. t.Errorf("initial-advertise-peer-urls = %v, want %v", cfg.lpurls, wcfg.lpurls)
  490. }
  491. if !reflect.DeepEqual(cfg.acurls, wcfg.acurls) {
  492. t.Errorf("advertise-client-urls = %v, want %v", cfg.lcurls, wcfg.lcurls)
  493. }
  494. }
  495. func validateOtherFlags(t *testing.T, cfg *config) {
  496. wcfg := NewConfig()
  497. wcfg.proxy.Set(proxyFlagReadonly)
  498. wcfg.clientTLSInfo.CAFile = "cafile"
  499. wcfg.clientTLSInfo.CertFile = "certfile"
  500. wcfg.clientTLSInfo.KeyFile = "keyfile"
  501. wcfg.peerTLSInfo.CAFile = "peercafile"
  502. wcfg.peerTLSInfo.CertFile = "peercertfile"
  503. wcfg.peerTLSInfo.KeyFile = "peerkeyfile"
  504. wcfg.ForceNewCluster = true
  505. if cfg.proxy.String() != wcfg.proxy.String() {
  506. t.Errorf("proxy = %v, want %v", cfg.proxy, wcfg.proxy)
  507. }
  508. if cfg.clientTLSInfo.String() != wcfg.clientTLSInfo.String() {
  509. t.Errorf("clientTLS = %v, want %v", cfg.clientTLSInfo, wcfg.clientTLSInfo)
  510. }
  511. if cfg.peerTLSInfo.String() != wcfg.peerTLSInfo.String() {
  512. t.Errorf("peerTLS = %v, want %v", cfg.peerTLSInfo, wcfg.peerTLSInfo)
  513. }
  514. if cfg.ForceNewCluster != wcfg.ForceNewCluster {
  515. t.Errorf("forceNewCluster = %t, want %t", cfg.ForceNewCluster, wcfg.ForceNewCluster)
  516. }
  517. }