forked from cloudfoundry/route-registrar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript_checker_test.go
136 lines (112 loc) · 3.22 KB
/
script_checker_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package healthchecker_test
import (
"bytes"
"errors"
"os"
"os/exec"
"path/filepath"
"time"
"code.cloudfoundry.org/route-registrar/commandrunner/fakes"
"code.cloudfoundry.org/route-registrar/healthchecker"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
"code.cloudfoundry.org/lager/v3"
"code.cloudfoundry.org/lager/v3/lagertest"
)
var _ = Describe("ScriptHealthChecker", func() {
var (
logger lager.Logger
runner *fakes.FakeRunner
tmpDir string
scriptPath string
timeout time.Duration
h healthchecker.HealthChecker
)
BeforeEach(func() {
var err error
tmpDir, err = os.MkdirTemp(os.TempDir(), "healthchecker-test")
Expect(err).ToNot(HaveOccurred())
scriptPath = filepath.Join(tmpDir, "healthchecker.sh")
logger = lagertest.NewTestLogger("Script healthchecker test")
runner = new(fakes.FakeRunner) //commandrunner.NewRunner(scriptPath)
runner.RunStub = func(outbuf, errbuf *bytes.Buffer) error {
outbuf.WriteString("my-stdout")
errbuf.WriteString("my-stderr")
return nil
}
runner.WaitStub = func() error {
return nil
}
h = healthchecker.NewHealthChecker(logger)
})
It("logs stdout and stderr from the runner", func() {
_, _ = h.Check(runner, scriptPath, timeout)
Expect(logger).Should(gbytes.Say("stderr\":\"my-stderr"))
Expect(logger).Should(gbytes.Say("stdout\":\"my-stdout"))
})
Context("When the runner returns no errors", func() {
It("returns true without error", func() {
result, err := h.Check(runner, scriptPath, timeout)
Expect(err).ShouldNot(HaveOccurred())
Expect(result).To(BeTrue())
})
})
Context("When the runner returns an error on the execution channel", func() {
BeforeEach(func() {
runner.WaitStub = func() error {
return &exec.ExitError{}
}
})
It("returns false without error", func() {
result, err := h.Check(runner, scriptPath, timeout)
Expect(err).ShouldNot(HaveOccurred())
Expect(result).To(BeFalse())
})
})
Context("when the runner returns an immediate error", func() {
BeforeEach(func() {
runner.RunStub = func(outbuf, errbuf *bytes.Buffer) error {
return errors.New("BOO")
}
})
It("returns error", func() {
_, err := h.Check(runner, scriptPath, timeout)
Expect(err).Should(HaveOccurred())
})
})
Context("when the timeout is positive", func() {
BeforeEach(func() {
timeout = 50 * time.Millisecond
})
Context("when the runner exits within timeout", func() {
BeforeEach(func() {
runner.WaitStub = func() error {
time.Sleep(20 * time.Millisecond)
return nil
}
})
It("returns true without error", func() {
result, err := h.Check(runner, scriptPath, timeout)
Expect(err).ShouldNot(HaveOccurred())
Expect(result).To(BeTrue())
})
})
Context("when the runner does not exit within the timeout", func() {
BeforeEach(func() {
runner.WaitStub = func() error {
time.Sleep(5 * time.Second)
return nil
}
})
It("returns error", func() {
_, err := h.Check(runner, scriptPath, timeout)
Expect(err).Should(HaveOccurred())
})
It("kills the healthcheck process", func() {
h.Check(runner, scriptPath, timeout)
Expect(runner.KillCallCount()).To(Equal(1))
})
})
})
})