agent_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. "os"
  17. "path/filepath"
  18. "syscall"
  19. "testing"
  20. )
  21. var etcdPath = filepath.Join(os.Getenv("GOPATH"), "bin/etcd")
  22. func TestAgentStart(t *testing.T) {
  23. defer os.Remove("etcd.log")
  24. a := newTestAgent(t)
  25. defer a.terminate()
  26. err := a.start()
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  30. }
  31. func TestAgentRestart(t *testing.T) {
  32. defer os.Remove("etcd.log")
  33. a := newTestAgent(t)
  34. defer a.terminate()
  35. err := a.start()
  36. if err != nil {
  37. t.Fatal(err)
  38. }
  39. err = a.stopWithSig(syscall.SIGTERM)
  40. if err != nil {
  41. t.Fatal(err)
  42. }
  43. err = a.restart()
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. }
  48. func TestAgentTerminate(t *testing.T) {
  49. defer os.Remove("etcd.log")
  50. a := newTestAgent(t)
  51. err := a.start()
  52. if err != nil {
  53. t.Fatal(err)
  54. }
  55. err = a.terminate()
  56. if err != nil {
  57. t.Fatal(err)
  58. }
  59. if _, err := os.Stat(a.dataDir()); !os.IsNotExist(err) {
  60. t.Fatal(err)
  61. }
  62. }
  63. // newTestAgent creates a test agent
  64. func newTestAgent(t *testing.T) *Agent {
  65. a, err := newAgent(AgentConfig{EtcdPath: etcdPath, LogDir: "etcd.log"})
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. return a
  70. }