agent_test.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. "os"
  18. "path/filepath"
  19. "testing"
  20. )
  21. const etcdPath = filepath.Join(os.Getenv("GOPATH"), "bin/etcd")
  22. func TestAgentStart(t *testing.T) {
  23. defer os.Remove("etcd.log")
  24. a, dir := newTestAgent(t)
  25. defer a.terminate()
  26. err := a.start("--data-dir", dir)
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  30. }
  31. func TestAgentRestart(t *testing.T) {
  32. defer os.Remove("etcd.log")
  33. a, dir := newTestAgent(t)
  34. defer a.terminate()
  35. err := a.start("--data-dir", dir)
  36. if err != nil {
  37. t.Fatal(err)
  38. }
  39. err = a.stop()
  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, dir := newTestAgent(t)
  51. err := a.start("--data-dir", dir)
  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(dir); !os.IsNotExist(err) {
  60. t.Fatal(err)
  61. }
  62. }
  63. // newTestAgent creates a test agent and with a temp data directory.
  64. func newTestAgent(t *testing.T) (*Agent, string) {
  65. a, err := newAgent(etcdPath, "etcd.log")
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. dir, err := ioutil.TempDir(os.TempDir(), "etcd-agent")
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. return a, dir
  74. }