-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmonitor_test.go
More file actions
141 lines (121 loc) · 3.93 KB
/
monitor_test.go
File metadata and controls
141 lines (121 loc) · 3.93 KB
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
137
138
139
140
141
// Copyright (c) Codesphere Inc.
// SPDX-License-Identifier: Apache-2.0
package cmd_test
import (
"context"
"log"
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/stretchr/testify/mock"
"github.com/codesphere-cloud/cs-go/api"
"github.com/codesphere-cloud/cs-go/cli/cmd"
csio "github.com/codesphere-cloud/cs-go/pkg/io"
)
var _ = Describe("Monitor", func() {
var (
mockHttpServer *csio.MockHttpServer
mockTime *api.MockTime
mockExec *csio.MockExec
c *cmd.MonitorCmd
listenAddress string
maxRestarts int
currentTime time.Time
forward string
skipTLS bool
caCertFile string
)
BeforeEach(func() {
mockHttpServer = csio.NewMockHttpServer(GinkgoT())
mockTime = api.NewMockTime(GinkgoT())
mockExec = csio.NewMockExec(GinkgoT())
maxRestarts = 0 //to make tests finite
listenAddress = ":3000"
skipTLS = true
})
JustBeforeEach(func() {
c = &cmd.MonitorCmd{
Time: mockTime,
Http: mockHttpServer,
Exec: mockExec,
Opts: cmd.MonitorOpts{
ListenAddress: &listenAddress,
MaxRestarts: &maxRestarts,
Forward: &forward,
InsecureSkipVerify: &skipTLS,
CaCertFile: &caCertFile,
},
}
currentTime = time.Unix(1746190963, 0)
mockTime.EXPECT().Now().RunAndReturn(func() time.Time {
return currentTime
}).Maybe()
mockTime.EXPECT().Sleep(mock.Anything).Run(func(t time.Duration) {
currentTime = currentTime.Add(t)
}).Maybe()
mockHttpServer.EXPECT().ListenAndServe(mock.Anything, mock.Anything).Return(nil).Maybe()
})
Context("With default healthcheck endpoint", func() {
JustBeforeEach(func() {
mockHttpServer.EXPECT().Handle(mock.Anything, mock.Anything)
mockHttpServer.EXPECT().HandleFunc(mock.Anything, mock.Anything)
})
Context("Command exits after 10 seconds with exit code 0", func() {
It("Doesn't return an error", func() {
mockExec.EXPECT().ExecuteCommand(mock.Anything, mock.Anything).RunAndReturn(
func(ctx context.Context, args []string) (int, error) {
mockTime.Sleep(10 * time.Second)
return 0, nil
})
err := c.RunCommandWithHealthcheck(context.TODO(), []string{"fake-sleep", "10s"})
Expect(err).NotTo(HaveOccurred())
})
})
Context("Command exits after 0.3 seconds", func() {
BeforeEach(func() {
maxRestarts = 1
})
Context("Command doesn't return an error", func() {
It("Restarts immediately", func() {
mockExec.EXPECT().ExecuteCommand(mock.Anything, mock.Anything).RunAndReturn(
func(ctx context.Context, args []string) (int, error) {
log.Println("lol")
mockTime.Sleep(300 * time.Millisecond)
return 0, nil
}).Twice()
err := c.RunCommandWithHealthcheck(context.TODO(), []string{"fake-sleep", "300ms"})
Expect(err).NotTo(HaveOccurred())
})
})
Context("Command immediately returns an error", func() {
It("Restarts after a delay of 5 seconds", func() {
mockExec.EXPECT().ExecuteCommand(mock.Anything, mock.Anything).RunAndReturn(
func(ctx context.Context, args []string) (int, error) {
return 1, nil
}).Twice()
startTime := currentTime
err := c.RunCommandWithHealthcheck(context.TODO(), []string{"fake-exit", "1"})
endTime := currentTime
Expect(err).NotTo(HaveOccurred())
Expect(endTime.Sub(startTime)).To(Equal(5 * time.Second))
})
})
})
})
Context("With healthcheck forwarding", func() {
BeforeEach(func() {
forward = "http://localhost:3000"
})
Context("Command exits after 10 seconds with exit code 0", func() {
It("Doesn't return an error", func() {
mockExec.EXPECT().ExecuteCommand(mock.Anything, mock.Anything).RunAndReturn(
func(ctx context.Context, args []string) (int, error) {
mockTime.Sleep(10 * time.Second)
return 0, nil
})
err := c.RunCommandWithHealthcheck(context.TODO(), []string{"fake-sleep", "10s"})
Expect(err).NotTo(HaveOccurred())
})
})
})
})