etcd_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. package main
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "net/http"
  6. "net/http/httptest"
  7. "net/url"
  8. "os"
  9. "strconv"
  10. "strings"
  11. "testing"
  12. "time"
  13. "github.com/coreos/etcd/test"
  14. "github.com/coreos/go-etcd/etcd"
  15. )
  16. // Create a single node and try to set value
  17. func TestSingleNode(t *testing.T) {
  18. procAttr := new(os.ProcAttr)
  19. procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}
  20. args := []string{"etcd", "-n=node1", "-f", "-d=/tmp/node1"}
  21. process, err := os.StartProcess("etcd", args, procAttr)
  22. if err != nil {
  23. t.Fatal("start process failed:" + err.Error())
  24. return
  25. }
  26. defer process.Kill()
  27. time.Sleep(time.Second)
  28. c := etcd.NewClient()
  29. c.SyncCluster()
  30. // Test Set
  31. result, err := c.Set("foo", "bar", 100)
  32. if err != nil || result.Key != "/foo" || result.Value != "bar" || result.TTL < 95 {
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. t.Fatalf("Set 1 failed with %s %s %v", result.Key, result.Value, result.TTL)
  37. }
  38. time.Sleep(time.Second)
  39. result, err = c.Set("foo", "bar", 100)
  40. if err != nil || result.Key != "/foo" || result.Value != "bar" || result.PrevValue != "bar" || result.TTL != 99 {
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. t.Fatalf("Set 2 failed with %s %s %v", result.Key, result.Value, result.TTL)
  45. }
  46. }
  47. // TestInternalVersionFail will ensure that etcd does not come up if the internal raft
  48. // versions do not match.
  49. func TestInternalVersionFail(t *testing.T) {
  50. checkedVersion := false
  51. testMux := http.NewServeMux()
  52. testMux.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) {
  53. fmt.Fprintln(w, "This is not a version number")
  54. checkedVersion = true
  55. })
  56. testMux.HandleFunc("/join", func(w http.ResponseWriter, r *http.Request) {
  57. t.Fatal("should not attempt to join!")
  58. })
  59. ts := httptest.NewServer(testMux)
  60. defer ts.Close()
  61. fakeURL, _ := url.Parse(ts.URL)
  62. procAttr := new(os.ProcAttr)
  63. procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}
  64. args := []string{"etcd", "-n=node1", "-f", "-d=/tmp/node1", "-vv", "-C=" + fakeURL.Host}
  65. process, err := os.StartProcess("etcd", args, procAttr)
  66. if err != nil {
  67. t.Fatal("start process failed:" + err.Error())
  68. return
  69. }
  70. defer process.Kill()
  71. time.Sleep(time.Second)
  72. _, err = http.Get("http://127.0.0.1:4001")
  73. if err == nil {
  74. t.Fatal("etcd node should not be up")
  75. return
  76. }
  77. if checkedVersion == false {
  78. t.Fatal("etcd did not check the version")
  79. return
  80. }
  81. }
  82. // This test creates a single node and then set a value to it.
  83. // Then this test kills the node and restart it and tries to get the value again.
  84. func TestSingleNodeRecovery(t *testing.T) {
  85. procAttr := new(os.ProcAttr)
  86. procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}
  87. args := []string{"etcd", "-n=node1", "-d=/tmp/node1"}
  88. process, err := os.StartProcess("etcd", append(args, "-f"), procAttr)
  89. if err != nil {
  90. t.Fatal("start process failed:" + err.Error())
  91. return
  92. }
  93. time.Sleep(time.Second)
  94. c := etcd.NewClient()
  95. c.SyncCluster()
  96. // Test Set
  97. result, err := c.Set("foo", "bar", 100)
  98. if err != nil || result.Key != "/foo" || result.Value != "bar" || result.TTL < 95 {
  99. if err != nil {
  100. t.Fatal(err)
  101. }
  102. t.Fatalf("Set 1 failed with %s %s %v", result.Key, result.Value, result.TTL)
  103. }
  104. time.Sleep(time.Second)
  105. process.Kill()
  106. process, err = os.StartProcess("etcd", args, procAttr)
  107. defer process.Kill()
  108. if err != nil {
  109. t.Fatal("start process failed:" + err.Error())
  110. return
  111. }
  112. time.Sleep(time.Second)
  113. results, err := c.Get("foo")
  114. if err != nil {
  115. t.Fatal("get fail: " + err.Error())
  116. return
  117. }
  118. result = results[0]
  119. if err != nil || result.Key != "/foo" || result.Value != "bar" || result.TTL > 99 {
  120. if err != nil {
  121. t.Fatal(err)
  122. }
  123. t.Fatalf("Recovery Get failed with %s %s %v", result.Key, result.Value, result.TTL)
  124. }
  125. }
  126. // Create a three nodes and try to set value
  127. func templateTestSimpleMultiNode(t *testing.T, tls bool) {
  128. procAttr := new(os.ProcAttr)
  129. procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}
  130. clusterSize := 3
  131. _, etcds, err := test.CreateCluster(clusterSize, procAttr, tls)
  132. if err != nil {
  133. t.Fatal("cannot create cluster")
  134. }
  135. defer test.DestroyCluster(etcds)
  136. time.Sleep(time.Second)
  137. c := etcd.NewClient()
  138. c.SyncCluster()
  139. // Test Set
  140. result, err := c.Set("foo", "bar", 100)
  141. if err != nil || result.Key != "/foo" || result.Value != "bar" || result.TTL < 95 {
  142. if err != nil {
  143. t.Fatal(err)
  144. }
  145. t.Fatalf("Set 1 failed with %s %s %v", result.Key, result.Value, result.TTL)
  146. }
  147. time.Sleep(time.Second)
  148. result, err = c.Set("foo", "bar", 100)
  149. if err != nil || result.Key != "/foo" || result.Value != "bar" || result.PrevValue != "bar" || result.TTL != 99 {
  150. if err != nil {
  151. t.Fatal(err)
  152. }
  153. t.Fatalf("Set 2 failed with %s %s %v", result.Key, result.Value, result.TTL)
  154. }
  155. }
  156. func TestSimpleMultiNode(t *testing.T) {
  157. templateTestSimpleMultiNode(t, false)
  158. }
  159. func TestSimpleMultiNodeTls(t *testing.T) {
  160. templateTestSimpleMultiNode(t, true)
  161. }
  162. // Create a five nodes
  163. // Kill all the nodes and restart
  164. func TestMultiNodeKillAllAndRecovery(t *testing.T) {
  165. procAttr := new(os.ProcAttr)
  166. procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}
  167. clusterSize := 5
  168. argGroup, etcds, err := test.CreateCluster(clusterSize, procAttr, false)
  169. if err != nil {
  170. t.Fatal("cannot create cluster")
  171. }
  172. c := etcd.NewClient()
  173. c.SyncCluster()
  174. time.Sleep(time.Second)
  175. // send 10 commands
  176. for i := 0; i < 10; i++ {
  177. // Test Set
  178. _, err := c.Set("foo", "bar", 0)
  179. if err != nil {
  180. panic(err)
  181. }
  182. }
  183. time.Sleep(time.Second)
  184. // kill all
  185. test.DestroyCluster(etcds)
  186. time.Sleep(time.Second)
  187. stop := make(chan bool)
  188. leaderChan := make(chan string, 1)
  189. all := make(chan bool, 1)
  190. time.Sleep(time.Second)
  191. for i := 0; i < clusterSize; i++ {
  192. etcds[i], err = os.StartProcess("etcd", argGroup[i], procAttr)
  193. }
  194. go test.Monitor(clusterSize, 1, leaderChan, all, stop)
  195. <-all
  196. <-leaderChan
  197. result, err := c.Set("foo", "bar", 0)
  198. if err != nil {
  199. panic(err)
  200. }
  201. if result.Index != 18 {
  202. t.Fatalf("recovery failed! [%d/18]", result.Index)
  203. }
  204. // kill all
  205. test.DestroyCluster(etcds)
  206. }
  207. // Create a five nodes
  208. // Randomly kill one of the node and keep on sending set command to the cluster
  209. func TestMultiNodeKillOne(t *testing.T) {
  210. procAttr := new(os.ProcAttr)
  211. procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}
  212. clusterSize := 5
  213. argGroup, etcds, err := test.CreateCluster(clusterSize, procAttr, false)
  214. if err != nil {
  215. t.Fatal("cannot create cluster")
  216. }
  217. defer test.DestroyCluster(etcds)
  218. time.Sleep(2 * time.Second)
  219. c := etcd.NewClient()
  220. c.SyncCluster()
  221. stop := make(chan bool)
  222. // Test Set
  223. go test.Set(stop)
  224. for i := 0; i < 10; i++ {
  225. num := rand.Int() % clusterSize
  226. fmt.Println("kill node", num+1)
  227. // kill
  228. etcds[num].Kill()
  229. etcds[num].Release()
  230. time.Sleep(time.Second)
  231. // restart
  232. etcds[num], err = os.StartProcess("etcd", argGroup[num], procAttr)
  233. if err != nil {
  234. panic(err)
  235. }
  236. time.Sleep(time.Second)
  237. }
  238. fmt.Println("stop")
  239. stop <- true
  240. <-stop
  241. }
  242. // This test will kill the current leader and wait for the etcd cluster to elect a new leader for 200 times.
  243. // It will print out the election time and the average election time.
  244. func TestKillLeader(t *testing.T) {
  245. procAttr := new(os.ProcAttr)
  246. procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}
  247. clusterSize := 5
  248. argGroup, etcds, err := test.CreateCluster(clusterSize, procAttr, false)
  249. if err != nil {
  250. t.Fatal("cannot create cluster")
  251. }
  252. defer test.DestroyCluster(etcds)
  253. stop := make(chan bool)
  254. leaderChan := make(chan string, 1)
  255. all := make(chan bool, 1)
  256. time.Sleep(time.Second)
  257. go test.Monitor(clusterSize, 1, leaderChan, all, stop)
  258. var totalTime time.Duration
  259. leader := "http://127.0.0.1:7001"
  260. for i := 0; i < clusterSize; i++ {
  261. fmt.Println("leader is ", leader)
  262. port, _ := strconv.Atoi(strings.Split(leader, ":")[2])
  263. num := port - 7001
  264. fmt.Println("kill server ", num)
  265. etcds[num].Kill()
  266. etcds[num].Release()
  267. start := time.Now()
  268. for {
  269. newLeader := <-leaderChan
  270. if newLeader != leader {
  271. leader = newLeader
  272. break
  273. }
  274. }
  275. take := time.Now().Sub(start)
  276. totalTime += take
  277. avgTime := totalTime / (time.Duration)(i+1)
  278. fmt.Println("Leader election time is ", take, "with election timeout", ElectionTimeout)
  279. fmt.Println("Leader election time average is", avgTime, "with election timeout", ElectionTimeout)
  280. etcds[num], err = os.StartProcess("etcd", argGroup[num], procAttr)
  281. }
  282. stop <- true
  283. }
  284. // TestKillRandom kills random machines in the cluster and
  285. // restart them after all other machines agree on the same leader
  286. func TestKillRandom(t *testing.T) {
  287. procAttr := new(os.ProcAttr)
  288. procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}
  289. clusterSize := 9
  290. argGroup, etcds, err := test.CreateCluster(clusterSize, procAttr, false)
  291. if err != nil {
  292. t.Fatal("cannot create cluster")
  293. }
  294. defer test.DestroyCluster(etcds)
  295. stop := make(chan bool)
  296. leaderChan := make(chan string, 1)
  297. all := make(chan bool, 1)
  298. time.Sleep(3 * time.Second)
  299. go test.Monitor(clusterSize, 4, leaderChan, all, stop)
  300. toKill := make(map[int]bool)
  301. for i := 0; i < 20; i++ {
  302. fmt.Printf("TestKillRandom Round[%d/20]\n", i)
  303. j := 0
  304. for {
  305. r := rand.Int31n(9)
  306. if _, ok := toKill[int(r)]; !ok {
  307. j++
  308. toKill[int(r)] = true
  309. }
  310. if j > 3 {
  311. break
  312. }
  313. }
  314. for num, _ := range toKill {
  315. err := etcds[num].Kill()
  316. if err != nil {
  317. panic(err)
  318. }
  319. etcds[num].Wait()
  320. }
  321. time.Sleep(ElectionTimeout)
  322. <-leaderChan
  323. for num, _ := range toKill {
  324. etcds[num], err = os.StartProcess("etcd", argGroup[num], procAttr)
  325. }
  326. toKill = make(map[int]bool)
  327. <-all
  328. }
  329. stop <- true
  330. }
  331. // remove the node and node rejoin with previous log
  332. func TestRemoveNode(t *testing.T) {
  333. procAttr := new(os.ProcAttr)
  334. procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}
  335. clusterSize := 3
  336. argGroup, etcds, _ := test.CreateCluster(clusterSize, procAttr, false)
  337. time.Sleep(time.Second)
  338. c := etcd.NewClient()
  339. c.SyncCluster()
  340. rmReq, _ := http.NewRequest("DELETE", "http://127.0.0.1:7001/remove/node3", nil)
  341. client := &http.Client{}
  342. for i := 0; i < 2; i++ {
  343. for i := 0; i < 2; i++ {
  344. client.Do(rmReq)
  345. etcds[2].Wait()
  346. resp, err := c.Get("_etcd/machines")
  347. if err != nil {
  348. panic(err)
  349. }
  350. if len(resp) != 2 {
  351. t.Fatal("cannot remove machine")
  352. }
  353. if i == 1 {
  354. // rejoin with log
  355. etcds[2], err = os.StartProcess("etcd", argGroup[2], procAttr)
  356. } else {
  357. // rejoin without log
  358. etcds[2], err = os.StartProcess("etcd", append(argGroup[2], "-f"), procAttr)
  359. }
  360. if err != nil {
  361. panic(err)
  362. }
  363. time.Sleep(time.Second)
  364. resp, err = c.Get("_etcd/machines")
  365. if err != nil {
  366. panic(err)
  367. }
  368. if len(resp) != 3 {
  369. t.Fatal("add machine fails")
  370. }
  371. }
  372. // first kill the node, then remove it, then add it back
  373. for i := 0; i < 2; i++ {
  374. etcds[2].Kill()
  375. etcds[2].Wait()
  376. client.Do(rmReq)
  377. resp, err := c.Get("_etcd/machines")
  378. if err != nil {
  379. panic(err)
  380. }
  381. if len(resp) != 2 {
  382. t.Fatal("cannot remove machine")
  383. }
  384. if i == 1 {
  385. // rejoin with log
  386. etcds[2], err = os.StartProcess("etcd", append(argGroup[2]), procAttr)
  387. } else {
  388. // rejoin without log
  389. etcds[2], err = os.StartProcess("etcd", append(argGroup[2], "-f"), procAttr)
  390. }
  391. if err != nil {
  392. panic(err)
  393. }
  394. time.Sleep(time.Second)
  395. resp, err = c.Get("_etcd/machines")
  396. if err != nil {
  397. panic(err)
  398. }
  399. if len(resp) != 3 {
  400. t.Fatal("add machine fails")
  401. }
  402. }
  403. }
  404. test.DestroyCluster(etcds)
  405. }
  406. func templateBenchmarkEtcdDirectCall(b *testing.B, tls bool) {
  407. procAttr := new(os.ProcAttr)
  408. procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}
  409. clusterSize := 3
  410. _, etcds, _ := test.CreateCluster(clusterSize, procAttr, tls)
  411. defer test.DestroyCluster(etcds)
  412. time.Sleep(time.Second)
  413. b.ResetTimer()
  414. for i := 0; i < b.N; i++ {
  415. resp, _ := http.Get("http://127.0.0.1:4001/test/speed")
  416. resp.Body.Close()
  417. }
  418. }
  419. func BenchmarkEtcdDirectCall(b *testing.B) {
  420. templateBenchmarkEtcdDirectCall(b, false)
  421. }
  422. func BenchmarkEtcdDirectCallTls(b *testing.B) {
  423. templateBenchmarkEtcdDirectCall(b, true)
  424. }