-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #250 from slintes/fix-unit-test
Fix watchdog usage in controller unit tests
- Loading branch information
Showing
7 changed files
with
189 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package watchdog_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestWatchdog(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Watchdog Suite") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package watchdog_test | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
"github.com/medik8s/self-node-remediation/pkg/watchdog" | ||
) | ||
|
||
var _ = Describe("Watchdog", func() { | ||
|
||
var wd watchdog.FakeWatchdog | ||
var cancel context.CancelFunc | ||
|
||
BeforeEach(func() { | ||
var ctx context.Context | ||
ctx, cancel = context.WithCancel(context.Background()) | ||
|
||
wd = watchdog.NewFake(true) | ||
Expect(wd).NotTo(BeNil()) | ||
go func() { | ||
err := wd.Start(ctx) | ||
Expect(err).NotTo(HaveOccurred()) | ||
}() | ||
Eventually(func(g Gomega) { | ||
g.Expect(wd.Status()).To(Equal(watchdog.Armed)) | ||
}, 1*time.Second, 100*time.Millisecond).Should(Succeed(), "watchdog should be armed") | ||
}) | ||
|
||
AfterEach(func() { | ||
cancel() | ||
}) | ||
|
||
Context("Watchdog started", func() { | ||
It("should be fed", func() { | ||
verifyWatchdogFood(wd) | ||
}) | ||
}) | ||
|
||
Context("Watchdog triggered", func() { | ||
BeforeEach(func() { | ||
wd.Stop() | ||
}) | ||
|
||
It("should be triggered and not be fed anymore", func() { | ||
Eventually(func(g Gomega) { | ||
g.Expect(wd.Status()).To(Equal(watchdog.Triggered)) | ||
}, 1*time.Second, 100*time.Millisecond).Should(Succeed(), "watchdog should be triggered") | ||
verifyNoWatchdogFood(wd) | ||
}) | ||
}) | ||
|
||
Context("Watchdog cancelled", func() { | ||
BeforeEach(func() { | ||
cancel() | ||
}) | ||
|
||
It("should be disarmed and and not be fed anymore", func() { | ||
Eventually(func(g Gomega) { | ||
g.Expect(wd.Status()).To(Equal(watchdog.Disarmed)) | ||
}, 1*time.Second, 100*time.Millisecond).Should(Succeed(), "watchdog should be disarmed") | ||
verifyNoWatchdogFood(wd) | ||
}) | ||
}) | ||
|
||
Context("Triggered watchdog reset", func() { | ||
BeforeEach(func() { | ||
wd.Stop() | ||
wd.Reset() | ||
}) | ||
|
||
It("should be armed and fed", func() { | ||
Eventually(func(g Gomega) { | ||
g.Expect(wd.Status()).To(Equal(watchdog.Armed)) | ||
}, 1*time.Second, 100*time.Millisecond).Should(Succeed(), "watchdog should be armed") | ||
verifyWatchdogFood(wd) | ||
}) | ||
}) | ||
}) | ||
|
||
func verifyWatchdogFood(wd watchdog.Watchdog) { | ||
currentLastFoodTime := wd.LastFoodTime() | ||
EventuallyWithOffset(1, func() time.Time { | ||
return wd.LastFoodTime() | ||
}, wd.GetTimeout(), 100*time.Millisecond).Should(BeTemporally(">", currentLastFoodTime), "watchdog should receive food") | ||
} | ||
|
||
func verifyNoWatchdogFood(wd watchdog.Watchdog) { | ||
currentLastFoodTime := wd.LastFoodTime() | ||
ConsistentlyWithOffset(1, func() time.Time { | ||
return wd.LastFoodTime() | ||
}, 5*wd.GetTimeout(), 1*time.Second).Should(Equal(currentLastFoodTime), "watchdog should not receive food anymore") | ||
} |