agent_test.go 1.8 KB

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