agent_test.go 1.6 KB

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