Skip to content

Commit 0c83839

Browse files
committed
Add watchdog unit test
This is the right place for testing feeding Signed-off-by: Marc Sluiter <[email protected]>
1 parent c15b385 commit 0c83839

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package watchdog_test
2+
3+
import (
4+
"testing"
5+
6+
. "github.com/onsi/ginkgo/v2"
7+
. "github.com/onsi/gomega"
8+
)
9+
10+
func TestWatchdog(t *testing.T) {
11+
RegisterFailHandler(Fail)
12+
RunSpecs(t, "Watchdog Suite")
13+
}

pkg/watchdog/watchdog_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package watchdog_test
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
. "github.com/onsi/ginkgo/v2"
8+
. "github.com/onsi/gomega"
9+
10+
"github.com/medik8s/self-node-remediation/pkg/watchdog"
11+
)
12+
13+
var _ = Describe("Watchdog", func() {
14+
15+
var wd watchdog.Watchdog
16+
var cancel context.CancelFunc
17+
18+
BeforeEach(func() {
19+
var ctx context.Context
20+
ctx, cancel = context.WithCancel(context.Background())
21+
22+
wd = watchdog.NewFake(true)
23+
Expect(wd).NotTo(BeNil())
24+
go func() {
25+
err := wd.Start(ctx)
26+
Expect(err).NotTo(HaveOccurred())
27+
}()
28+
Eventually(func(g Gomega) {
29+
g.Expect(wd.Status()).To(Equal(watchdog.Armed))
30+
}, 1*time.Second, 100*time.Millisecond).Should(Succeed(), "watchdog should be armed")
31+
})
32+
33+
AfterEach(func() {
34+
cancel()
35+
})
36+
37+
Context("Watchdog started", func() {
38+
It("should be fed", func() {
39+
verifyWatchdogFood(wd)
40+
})
41+
})
42+
43+
Context("Watchdog triggered", func() {
44+
BeforeEach(func() {
45+
wd.Stop()
46+
})
47+
48+
It("should be triggered and not be fed anymore", func() {
49+
Eventually(func(g Gomega) {
50+
g.Expect(wd.Status()).To(Equal(watchdog.Triggered))
51+
}, 1*time.Second, 100*time.Millisecond).Should(Succeed(), "watchdog should be triggered")
52+
verifyNoWatchdogFood(wd)
53+
})
54+
})
55+
56+
Context("Watchdog cancelled", func() {
57+
BeforeEach(func() {
58+
cancel()
59+
})
60+
61+
It("should be disarmed and and not be fed anymore", func() {
62+
Eventually(func(g Gomega) {
63+
g.Expect(wd.Status()).To(Equal(watchdog.Disarmed))
64+
}, 1*time.Second, 100*time.Millisecond).Should(Succeed(), "watchdog should be disarmed")
65+
verifyNoWatchdogFood(wd)
66+
})
67+
})
68+
})
69+
70+
func verifyWatchdogFood(wd watchdog.Watchdog) {
71+
currentLastFoodTime := wd.LastFoodTime()
72+
EventuallyWithOffset(1, func() time.Time {
73+
return wd.LastFoodTime()
74+
}, wd.GetTimeout(), 100*time.Millisecond).Should(BeTemporally(">", currentLastFoodTime), "watchdog should receive food")
75+
}
76+
77+
func verifyNoWatchdogFood(wd watchdog.Watchdog) {
78+
currentLastFoodTime := wd.LastFoodTime()
79+
ConsistentlyWithOffset(1, func() time.Time {
80+
return wd.LastFoodTime()
81+
}, 5*wd.GetTimeout(), 1*time.Second).Should(Equal(currentLastFoodTime), "watchdog should not receive food anymore")
82+
}

0 commit comments

Comments
 (0)