config_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package etcdmain
  2. import (
  3. "net/url"
  4. "reflect"
  5. "testing"
  6. )
  7. func TestConfigParsingMemberFlags(t *testing.T) {
  8. args := []string{
  9. "-data-dir=testdir",
  10. "-name=testname",
  11. "-max-wals=10",
  12. "-max-snapshots=10",
  13. "-snapshot-count=10",
  14. "-listen-peer-urls=http://localhost:8000,https://localhost:8001",
  15. "-listen-client-urls=http://localhost:7000,https://localhost:7001",
  16. }
  17. wcfg := &config{
  18. dir: "testdir",
  19. lpurls: []url.URL{{Scheme: "http", Host: "localhost:8000"}, {Scheme: "https", Host: "localhost:8001"}},
  20. lcurls: []url.URL{{Scheme: "http", Host: "localhost:7000"}, {Scheme: "https", Host: "localhost:7001"}},
  21. maxSnapFiles: 10,
  22. maxWalFiles: 10,
  23. name: "testname",
  24. snapCount: 10,
  25. }
  26. cfg := NewConfig()
  27. err := cfg.Parse(args)
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. if cfg.dir != wcfg.dir {
  32. t.Errorf("dir = %v, want %v", cfg.dir, wcfg.dir)
  33. }
  34. if cfg.maxSnapFiles != wcfg.maxSnapFiles {
  35. t.Errorf("maxsnap = %v, want %v", cfg.maxSnapFiles, wcfg.maxSnapFiles)
  36. }
  37. if cfg.maxWalFiles != wcfg.maxWalFiles {
  38. t.Errorf("maxwal = %v, want %v", cfg.maxWalFiles, wcfg.maxWalFiles)
  39. }
  40. if cfg.name != wcfg.name {
  41. t.Errorf("name = %v, want %v", cfg.name, wcfg.name)
  42. }
  43. if cfg.snapCount != wcfg.snapCount {
  44. t.Errorf("snapcount = %v, want %v", cfg.snapCount, wcfg.snapCount)
  45. }
  46. if !reflect.DeepEqual(cfg.lpurls, wcfg.lpurls) {
  47. t.Errorf("listen-peer-urls = %v, want %v", cfg.lpurls, wcfg.lpurls)
  48. }
  49. if !reflect.DeepEqual(cfg.lcurls, wcfg.lcurls) {
  50. t.Errorf("listen-client-urls = %v, want %v", cfg.lcurls, wcfg.lcurls)
  51. }
  52. }
  53. func TestConfigParsingClusteringFlags(t *testing.T) {
  54. args := []string{
  55. "-initial-cluster=0=http://localhost:8000",
  56. "-initial-cluster-state=existing",
  57. "-initial-cluster-token=etcdtest",
  58. "-initial-advertise-peer-urls=http://localhost:8000,https://localhost:8001",
  59. "-advertise-client-urls=http://localhost:7000,https://localhost:7001",
  60. "-discovery-fallback=exit",
  61. }
  62. wcfg := NewConfig()
  63. wcfg.apurls = []url.URL{{Scheme: "http", Host: "localhost:8000"}, {Scheme: "https", Host: "localhost:8001"}}
  64. wcfg.acurls = []url.URL{{Scheme: "http", Host: "localhost:7000"}, {Scheme: "https", Host: "localhost:7001"}}
  65. wcfg.clusterState.Set(clusterStateFlagExisting)
  66. wcfg.fallback.Set(fallbackFlagExit)
  67. wcfg.initialCluster = "0=http://localhost:8000"
  68. wcfg.initialClusterToken = "etcdtest"
  69. cfg := NewConfig()
  70. err := cfg.Parse(args)
  71. if err != nil {
  72. t.Fatal(err)
  73. }
  74. if cfg.clusterState.String() != wcfg.clusterState.String() {
  75. t.Errorf("clusterState = %v, want %v", cfg.clusterState, wcfg.clusterState)
  76. }
  77. if cfg.fallback.String() != wcfg.fallback.String() {
  78. t.Errorf("fallback = %v, want %v", cfg.fallback, wcfg.fallback)
  79. }
  80. if cfg.initialCluster != wcfg.initialCluster {
  81. t.Errorf("initialCluster = %v, want %v", cfg.initialCluster, wcfg.initialCluster)
  82. }
  83. if cfg.initialClusterToken != wcfg.initialClusterToken {
  84. t.Errorf("initialClusterToken = %v, want %v", cfg.initialClusterToken, wcfg.initialClusterToken)
  85. }
  86. if !reflect.DeepEqual(cfg.apurls, wcfg.apurls) {
  87. t.Errorf("initial-advertise-peer-urls = %v, want %v", cfg.lpurls, wcfg.lpurls)
  88. }
  89. if !reflect.DeepEqual(cfg.acurls, wcfg.acurls) {
  90. t.Errorf("advertise-client-urls = %v, want %v", cfg.lcurls, wcfg.lcurls)
  91. }
  92. }
  93. func TestConfigParsingOtherFlags(t *testing.T) {
  94. args := []string{
  95. "-proxy=readonly",
  96. "-ca-file=cafile",
  97. "-cert-file=certfile",
  98. "-key-file=keyfile",
  99. "-peer-ca-file=peercafile",
  100. "-peer-cert-file=peercertfile",
  101. "-peer-key-file=peerkeyfile",
  102. "-force-new-cluster=true",
  103. }
  104. wcfg := NewConfig()
  105. wcfg.proxy.Set(proxyFlagReadonly)
  106. wcfg.clientTLSInfo.CAFile = "cafile"
  107. wcfg.clientTLSInfo.CertFile = "certfile"
  108. wcfg.clientTLSInfo.KeyFile = "keyfile"
  109. wcfg.peerTLSInfo.CAFile = "peercafile"
  110. wcfg.peerTLSInfo.CertFile = "peercertfile"
  111. wcfg.peerTLSInfo.KeyFile = "peerkeyfile"
  112. wcfg.forceNewCluster = true
  113. cfg := NewConfig()
  114. err := cfg.Parse(args)
  115. if err != nil {
  116. t.Fatal(err)
  117. }
  118. if cfg.proxy.String() != wcfg.proxy.String() {
  119. t.Errorf("proxy = %v, want %v", cfg.proxy, wcfg.proxy)
  120. }
  121. if cfg.clientTLSInfo.String() != wcfg.clientTLSInfo.String() {
  122. t.Errorf("clientTLS = %v, want %v", cfg.clientTLSInfo, wcfg.clientTLSInfo)
  123. }
  124. if cfg.peerTLSInfo.String() != wcfg.peerTLSInfo.String() {
  125. t.Errorf("peerTLS = %v, want %v", cfg.peerTLSInfo, wcfg.peerTLSInfo)
  126. }
  127. if cfg.forceNewCluster != wcfg.forceNewCluster {
  128. t.Errorf("forceNewCluster = %t, want %t", cfg.forceNewCluster, wcfg.forceNewCluster)
  129. }
  130. }
  131. func TestConfigParsingConflictClusteringFlags(t *testing.T) {
  132. conflictArgs := [][]string{
  133. []string{
  134. "-initial-cluster=0=localhost:8000",
  135. "-discovery=http://example.com/abc",
  136. },
  137. []string{
  138. "-discovery-srv=example.com",
  139. "-discovery=http://example.com/abc",
  140. },
  141. []string{
  142. "-initial-cluster=0=localhost:8000",
  143. "-discovery-srv=example.com",
  144. },
  145. []string{
  146. "-initial-cluster=0=localhost:8000",
  147. "-discovery=http://example.com/abc",
  148. "-discovery-srv=example.com",
  149. },
  150. }
  151. for i, tt := range conflictArgs {
  152. cfg := NewConfig()
  153. err := cfg.Parse(tt)
  154. if err != ErrConflictBootstrapFlags {
  155. t.Errorf("%d: err = %v, want %v", i, err, ErrConflictBootstrapFlags)
  156. }
  157. }
  158. }