From 6a68eddb3e1025b763d4a24976f16c7e7729d29b Mon Sep 17 00:00:00 2001 From: Chin-Ya Huang Date: Mon, 29 Apr 2024 16:51:16 +0800 Subject: [PATCH] test(kubernetes): IsDaemonSetReady Signed-off-by: Chin-Ya Huang --- kubernetes/daemonset_test.go | 48 ++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/kubernetes/daemonset_test.go b/kubernetes/daemonset_test.go index d8c5d632..05bef6d9 100644 --- a/kubernetes/daemonset_test.go +++ b/kubernetes/daemonset_test.go @@ -155,3 +155,51 @@ func (s *TestSuite) TestGetDaemonSet(c *C) { c.Assert(daemonSet.Name, Equals, testCase.daemonSet.Name, Commentf(test.ErrResultFmt, testName)) } } + +func (s *TestSuite) TestIsDaemonSetReady(c *C) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + type testCase struct { + daemonSet *appsv1.DaemonSet + expectedReady bool + } + testCases := map[string]testCase{ + "IsDaemonSetReady(...):": { + daemonSet: &appsv1.DaemonSet{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test", + Namespace: "default", + }, + Status: appsv1.DaemonSetStatus{ + NumberReady: 1, + DesiredNumberScheduled: 1, + }, + }, + expectedReady: true, + }, + "IsDaemonSetReady(...): not ready": { + daemonSet: &appsv1.DaemonSet{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test", + Namespace: "default", + }, + Status: appsv1.DaemonSetStatus{ + NumberReady: 0, + DesiredNumberScheduled: 1, + }, + }, + expectedReady: false, + }, + } + for testName, testCase := range testCases { + c.Logf("testing kubernetes.%v", testName) + + kubeClient := fake.NewSimpleClientset() + _, err := kubeClient.AppsV1().DaemonSets(testCase.daemonSet.Namespace).Create(ctx, testCase.daemonSet, metav1.CreateOptions{}) + c.Assert(err, IsNil, Commentf(test.ErrErrorFmt, testName)) + + ready := IsDaemonSetReady(testCase.daemonSet) + c.Assert(ready, Equals, testCase.expectedReady, Commentf(test.ErrResultFmt, testName)) + } +}