rpc_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2015 CoreOS, Inc.
  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. )
  22. func init() {
  23. defaultAgent, err := newAgent(etcdPath)
  24. if err != nil {
  25. log.Panic(err)
  26. }
  27. defaultAgent.serveRPC()
  28. }
  29. func TestRPCStart(t *testing.T) {
  30. c, err := rpc.DialHTTP("tcp", ":9027")
  31. if err != nil {
  32. t.Fatal(err)
  33. }
  34. dir, err := ioutil.TempDir(os.TempDir(), "etcd-agent")
  35. if err != nil {
  36. t.Fatal(err)
  37. }
  38. var pid int
  39. err = c.Call("Agent.RPCStart", []string{"-data-dir", dir}, &pid)
  40. if err != nil {
  41. t.Fatal(err)
  42. }
  43. defer c.Call("Agent.RPCTerminate", struct{}{}, nil)
  44. _, err = os.FindProcess(pid)
  45. if err != nil {
  46. t.Errorf("unexpected error %v when find process %d", err, pid)
  47. }
  48. }
  49. func TestRPCRestart(t *testing.T) {
  50. c, err := rpc.DialHTTP("tcp", ":9027")
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. dir, err := ioutil.TempDir(os.TempDir(), "etcd-agent")
  55. if err != nil {
  56. t.Fatal(err)
  57. }
  58. var pid int
  59. err = c.Call("Agent.RPCStart", []string{"-data-dir", dir}, &pid)
  60. if err != nil {
  61. t.Fatal(err)
  62. }
  63. defer c.Call("Agent.RPCTerminate", struct{}{}, nil)
  64. err = c.Call("Agent.RPCStop", struct{}{}, nil)
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. var npid int
  69. err = c.Call("Agent.RPCRestart", struct{}{}, &npid)
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. if npid == pid {
  74. t.Errorf("pid = %v, want not equal to %d", npid, pid)
  75. }
  76. s, err := os.FindProcess(pid)
  77. if err != nil {
  78. t.Errorf("unexpected error %v when find process %d", err, pid)
  79. }
  80. _, err = s.Wait()
  81. if err == nil {
  82. t.Errorf("err = nil, want killed error")
  83. }
  84. _, err = os.FindProcess(npid)
  85. if err != nil {
  86. t.Errorf("unexpected error %v when find process %d", err, npid)
  87. }
  88. }
  89. func TestRPCTerminate(t *testing.T) {
  90. c, err := rpc.DialHTTP("tcp", ":9027")
  91. if err != nil {
  92. t.Fatal(err)
  93. }
  94. dir, err := ioutil.TempDir(os.TempDir(), "etcd-agent")
  95. if err != nil {
  96. t.Fatal(err)
  97. }
  98. var pid int
  99. err = c.Call("Agent.RPCStart", []string{"-data-dir", dir}, &pid)
  100. if err != nil {
  101. t.Fatal(err)
  102. }
  103. err = c.Call("Agent.RPCTerminate", struct{}{}, nil)
  104. if err != nil {
  105. t.Fatal(err)
  106. }
  107. if _, err := os.Stat(dir); !os.IsNotExist(err) {
  108. t.Fatal(err)
  109. }
  110. }