etcd_test.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/coreos/etcd/test"
  5. "github.com/coreos/go-etcd/etcd"
  6. "math/rand"
  7. "net/http"
  8. "os"
  9. "strconv"
  10. "strings"
  11. "testing"
  12. "time"
  13. )
  14. // Create a single node and try to set value
  15. func TestSingleNode(t *testing.T) {
  16. procAttr := new(os.ProcAttr)
  17. procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}
  18. args := []string{"etcd", "-n=node1", "-f", "-d=/tmp/node1"}
  19. process, err := os.StartProcess("etcd", args, procAttr)
  20. if err != nil {
  21. t.Fatal("start process failed:" + err.Error())
  22. return
  23. }
  24. defer process.Kill()
  25. time.Sleep(time.Second)
  26. c := etcd.NewClient()
  27. c.SyncCluster()
  28. // Test Set
  29. result, err := c.Set("foo", "bar", 100)
  30. if err != nil || result.Key != "/foo" || result.Value != "bar" || result.TTL < 95 {
  31. if err != nil {
  32. t.Fatal(err)
  33. }
  34. t.Fatalf("Set 1 failed with %s %s %v", result.Key, result.Value, result.TTL)
  35. }
  36. time.Sleep(time.Second)
  37. result, err = c.Set("foo", "bar", 100)
  38. if err != nil || result.Key != "/foo" || result.Value != "bar" || result.PrevValue != "bar" || result.TTL != 99 {
  39. if err != nil {
  40. t.Fatal(err)
  41. }
  42. t.Fatalf("Set 2 failed with %s %s %v", result.Key, result.Value, result.TTL)
  43. }
  44. }
  45. // This test creates a single node and then set a value to it.
  46. // Then this test kills the node and restart it and tries to get the value again.
  47. func TestSingleNodeRecovery(t *testing.T) {
  48. procAttr := new(os.ProcAttr)
  49. procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}
  50. args := []string{"etcd", "-n=node1", "-d=/tmp/node1"}
  51. process, err := os.StartProcess("etcd", append(args, "-f"), procAttr)
  52. if err != nil {
  53. t.Fatal("start process failed:" + err.Error())
  54. return
  55. }
  56. time.Sleep(time.Second)
  57. c := etcd.NewClient()
  58. c.SyncCluster()
  59. // Test Set
  60. result, err := c.Set("foo", "bar", 100)
  61. if err != nil || result.Key != "/foo" || result.Value != "bar" || result.TTL < 95 {
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. t.Fatalf("Set 1 failed with %s %s %v", result.Key, result.Value, result.TTL)
  66. }
  67. time.Sleep(time.Second)
  68. process.Kill()
  69. process, err = os.StartProcess("etcd", args, procAttr)
  70. defer process.Kill()
  71. if err != nil {
  72. t.Fatal("start process failed:" + err.Error())
  73. return
  74. }
  75. time.Sleep(time.Second)
  76. results, err := c.Get("foo")
  77. if err != nil {
  78. t.Fatal("get fail: " + err.Error())
  79. return
  80. }
  81. result = results[0]
  82. if err != nil || result.Key != "/foo" || result.Value != "bar" || result.TTL > 99 {
  83. if err != nil {
  84. t.Fatal(err)
  85. }
  86. t.Fatalf("Recovery Get failed with %s %s %v", result.Key, result.Value, result.TTL)
  87. }
  88. }
  89. // Create a three nodes and try to set value
  90. func templateTestSimpleMultiNode(t *testing.T, tls bool) {
  91. procAttr := new(os.ProcAttr)
  92. procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}
  93. clusterSize := 3
  94. _, etcds, err := test.CreateCluster(clusterSize, procAttr, tls)
  95. if err != nil {
  96. t.Fatal("cannot create cluster")
  97. }
  98. defer test.DestroyCluster(etcds)
  99. time.Sleep(time.Second)
  100. c := etcd.NewClient()
  101. c.SyncCluster()
  102. // Test Set
  103. result, err := c.Set("foo", "bar", 100)
  104. if err != nil || result.Key != "/foo" || result.Value != "bar" || result.TTL < 95 {
  105. if err != nil {
  106. t.Fatal(err)
  107. }
  108. t.Fatalf("Set 1 failed with %s %s %v", result.Key, result.Value, result.TTL)
  109. }
  110. time.Sleep(time.Second)
  111. result, err = c.Set("foo", "bar", 100)
  112. if err != nil || result.Key != "/foo" || result.Value != "bar" || result.PrevValue != "bar" || result.TTL != 99 {
  113. if err != nil {
  114. t.Fatal(err)
  115. }
  116. t.Fatalf("Set 2 failed with %s %s %v", result.Key, result.Value, result.TTL)
  117. }
  118. }
  119. func TestSimpleMultiNode(t *testing.T) {
  120. templateTestSimpleMultiNode(t, false)
  121. }
  122. func TestSimpleMultiNodeTls(t *testing.T) {
  123. templateTestSimpleMultiNode(t, true)
  124. }
  125. // Create a five nodes
  126. // Randomly kill one of the node and keep on sending set command to the cluster
  127. func TestMultiNodeRecovery(t *testing.T) {
  128. procAttr := new(os.ProcAttr)
  129. procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}
  130. clusterSize := 5
  131. argGroup, etcds, err := test.CreateCluster(clusterSize, procAttr, false)
  132. if err != nil {
  133. t.Fatal("cannot create cluster")
  134. }
  135. defer test.DestroyCluster(etcds)
  136. time.Sleep(2 * time.Second)
  137. c := etcd.NewClient()
  138. c.SyncCluster()
  139. stop := make(chan bool)
  140. // Test Set
  141. go test.Set(stop)
  142. for i := 0; i < 10; i++ {
  143. num := rand.Int() % clusterSize
  144. fmt.Println("kill node", num+1)
  145. // kill
  146. etcds[num].Kill()
  147. etcds[num].Release()
  148. time.Sleep(time.Second)
  149. // restart
  150. etcds[num], err = os.StartProcess("etcd", argGroup[num], procAttr)
  151. if err != nil {
  152. panic(err)
  153. }
  154. time.Sleep(time.Second)
  155. }
  156. fmt.Println("stop")
  157. stop <- true
  158. <-stop
  159. }
  160. // This test will kill the current leader and wait for the etcd cluster to elect a new leader for 200 times.
  161. // It will print out the election time and the average election time.
  162. func TestKillLeader(t *testing.T) {
  163. procAttr := new(os.ProcAttr)
  164. procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}
  165. clusterSize := 5
  166. argGroup, etcds, err := test.CreateCluster(clusterSize, procAttr, false)
  167. if err != nil {
  168. t.Fatal("cannot create cluster")
  169. }
  170. defer test.DestroyCluster(etcds)
  171. stop := make(chan bool)
  172. leaderChan := make(chan string, 1)
  173. all := make(chan bool, 1)
  174. time.Sleep(time.Second)
  175. go test.Monitor(clusterSize, 1, leaderChan, all, stop)
  176. var totalTime time.Duration
  177. leader := "http://127.0.0.1:7001"
  178. for i := 0; i < clusterSize; i++ {
  179. fmt.Println("leader is ", leader)
  180. port, _ := strconv.Atoi(strings.Split(leader, ":")[2])
  181. num := port - 7001
  182. fmt.Println("kill server ", num)
  183. etcds[num].Kill()
  184. etcds[num].Release()
  185. start := time.Now()
  186. for {
  187. newLeader := <-leaderChan
  188. if newLeader != leader {
  189. leader = newLeader
  190. break
  191. }
  192. }
  193. take := time.Now().Sub(start)
  194. totalTime += take
  195. avgTime := totalTime / (time.Duration)(i+1)
  196. fmt.Println("Leader election time is ", take, "with election timeout", ElectionTimeout)
  197. fmt.Println("Leader election time average is", avgTime, "with election timeout", ElectionTimeout)
  198. etcds[num], err = os.StartProcess("etcd", argGroup[num], procAttr)
  199. }
  200. stop <- true
  201. }
  202. // TestKillRandom kills random machines in the cluster and
  203. // restart them after all other machines agree on the same leader
  204. func TestKillRandom(t *testing.T) {
  205. procAttr := new(os.ProcAttr)
  206. procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}
  207. clusterSize := 9
  208. argGroup, etcds, err := test.CreateCluster(clusterSize, procAttr, false)
  209. if err != nil {
  210. t.Fatal("cannot create cluster")
  211. }
  212. defer test.DestroyCluster(etcds)
  213. stop := make(chan bool)
  214. leaderChan := make(chan string, 1)
  215. all := make(chan bool, 1)
  216. time.Sleep(3 * time.Second)
  217. go test.Monitor(clusterSize, 4, leaderChan, all, stop)
  218. toKill := make(map[int]bool)
  219. for i := 0; i < 20; i++ {
  220. fmt.Printf("TestKillRandom Round[%d/20]\n", i)
  221. j := 0
  222. for {
  223. r := rand.Int31n(9)
  224. if _, ok := toKill[int(r)]; !ok {
  225. j++
  226. toKill[int(r)] = true
  227. }
  228. if j > 3 {
  229. break
  230. }
  231. }
  232. for num, _ := range toKill {
  233. err := etcds[num].Kill()
  234. if err != nil {
  235. panic(err)
  236. }
  237. etcds[num].Wait()
  238. }
  239. time.Sleep(ElectionTimeout)
  240. <-leaderChan
  241. for num, _ := range toKill {
  242. etcds[num], err = os.StartProcess("etcd", argGroup[num], procAttr)
  243. }
  244. toKill = make(map[int]bool)
  245. <-all
  246. }
  247. stop <- true
  248. }
  249. func templateBenchmarkEtcdDirectCall(b *testing.B, tls bool) {
  250. procAttr := new(os.ProcAttr)
  251. procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}
  252. clusterSize := 3
  253. _, etcds, _ := test.CreateCluster(clusterSize, procAttr, tls)
  254. defer test.DestroyCluster(etcds)
  255. time.Sleep(time.Second)
  256. b.ResetTimer()
  257. for i := 0; i < b.N; i++ {
  258. resp, _ := http.Get("http://127.0.0.1:4001/test/speed")
  259. resp.Body.Close()
  260. }
  261. }
  262. func BenchmarkEtcdDirectCall(b *testing.B) {
  263. templateBenchmarkEtcdDirectCall(b, false)
  264. }
  265. func BenchmarkEtcdDirectCallTls(b *testing.B) {
  266. templateBenchmarkEtcdDirectCall(b, true)
  267. }