etcd_test.go 13 KB

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