agent_test.go 1.7 KB

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