rpc_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 main
  15. import (
  16. "io/ioutil"
  17. "log"
  18. "net/rpc"
  19. "os"
  20. "testing"
  21. "github.com/coreos/etcd/tools/functional-tester/etcd-agent/client"
  22. )
  23. func init() {
  24. defaultAgent, err := newAgent(AgentConfig{EtcdPath: etcdPath, LogDir: "etcd.log"})
  25. if err != nil {
  26. log.Panic(err)
  27. }
  28. defaultAgent.serveRPC(":9027")
  29. }
  30. func TestRPCStart(t *testing.T) {
  31. c, err := rpc.DialHTTP("tcp", ":9027")
  32. if err != nil {
  33. t.Fatal(err)
  34. }
  35. dir, err := ioutil.TempDir(os.TempDir(), "etcd-agent")
  36. if err != nil {
  37. t.Fatal(err)
  38. }
  39. var pid int
  40. err = c.Call("Agent.RPCStart", []string{"--data-dir", dir}, &pid)
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. defer c.Call("Agent.RPCTerminate", struct{}{}, nil)
  45. _, err = os.FindProcess(pid)
  46. if err != nil {
  47. t.Errorf("unexpected error %v when find process %d", err, pid)
  48. }
  49. }
  50. func TestRPCRestart(t *testing.T) {
  51. c, err := rpc.DialHTTP("tcp", ":9027")
  52. if err != nil {
  53. t.Fatal(err)
  54. }
  55. dir, err := ioutil.TempDir(os.TempDir(), "etcd-agent")
  56. if err != nil {
  57. t.Fatal(err)
  58. }
  59. var pid int
  60. err = c.Call("Agent.RPCStart", []string{"--data-dir", dir}, &pid)
  61. if err != nil {
  62. t.Fatal(err)
  63. }
  64. defer c.Call("Agent.RPCTerminate", struct{}{}, nil)
  65. err = c.Call("Agent.RPCStop", struct{}{}, nil)
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. var npid int
  70. err = c.Call("Agent.RPCRestart", struct{}{}, &npid)
  71. if err != nil {
  72. t.Fatal(err)
  73. }
  74. if npid == pid {
  75. t.Errorf("pid = %v, want not equal to %d", npid, pid)
  76. }
  77. s, err := os.FindProcess(pid)
  78. if err != nil {
  79. t.Errorf("unexpected error %v when find process %d", err, pid)
  80. }
  81. _, err = s.Wait()
  82. if err == nil {
  83. t.Errorf("err = nil, want killed error")
  84. }
  85. _, err = os.FindProcess(npid)
  86. if err != nil {
  87. t.Errorf("unexpected error %v when find process %d", err, npid)
  88. }
  89. }
  90. func TestRPCTerminate(t *testing.T) {
  91. c, err := rpc.DialHTTP("tcp", ":9027")
  92. if err != nil {
  93. t.Fatal(err)
  94. }
  95. dir, err := ioutil.TempDir(os.TempDir(), "etcd-agent")
  96. if err != nil {
  97. t.Fatal(err)
  98. }
  99. var pid int
  100. err = c.Call("Agent.RPCStart", []string{"--data-dir", dir}, &pid)
  101. if err != nil {
  102. t.Fatal(err)
  103. }
  104. err = c.Call("Agent.RPCTerminate", struct{}{}, nil)
  105. if err != nil {
  106. t.Fatal(err)
  107. }
  108. if _, err := os.Stat(dir); !os.IsNotExist(err) {
  109. t.Fatal(err)
  110. }
  111. }
  112. func TestRPCStatus(t *testing.T) {
  113. c, err := rpc.DialHTTP("tcp", ":9027")
  114. if err != nil {
  115. t.Fatal(err)
  116. }
  117. var s client.Status
  118. err = c.Call("Agent.RPCStatus", struct{}{}, &s)
  119. if err != nil {
  120. t.Fatal(err)
  121. }
  122. if s.State != stateTerminated {
  123. t.Errorf("state = %s, want %s", s.State, stateTerminated)
  124. }
  125. dir, err := ioutil.TempDir(os.TempDir(), "etcd-agent")
  126. if err != nil {
  127. t.Fatal(err)
  128. }
  129. var pid int
  130. err = c.Call("Agent.RPCStart", []string{"--data-dir", dir}, &pid)
  131. if err != nil {
  132. t.Fatal(err)
  133. }
  134. err = c.Call("Agent.RPCStatus", struct{}{}, &s)
  135. if err != nil {
  136. t.Fatal(err)
  137. }
  138. if s.State != stateStarted {
  139. t.Errorf("state = %s, want %s", s.State, stateStarted)
  140. }
  141. err = c.Call("Agent.RPCTerminate", struct{}{}, nil)
  142. if err != nil {
  143. t.Fatal(err)
  144. }
  145. }