Skip to content

Commit

Permalink
test: func GetDeployment
Browse files Browse the repository at this point in the history
longhorn/longhorn-9752

Signed-off-by: Chin-Ya Huang <[email protected]>
  • Loading branch information
c3y1huang authored and derekbit committed Nov 24, 2024
1 parent a2a5b3e commit 6c1c43e
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions kubernetes/deployment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package kubernetes

import (
"context"

"github.com/longhorn/go-common-libs/test"
. "gopkg.in/check.v1"

"k8s.io/client-go/kubernetes/fake"

appsv1 "k8s.io/api/apps/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func (s *TestSuite) TestGetDeployment(c *C) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

type testCase struct {
deployment *appsv1.Deployment
expectNotFound bool
}
testCases := map[string]testCase{
"GetDeployment(...):": {
deployment: &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "default",
},
},
},
"GetDeployment(...): not found": {
deployment: &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "default",
},
},
expectNotFound: true,
},
}
for testName, testCase := range testCases {
c.Logf("testing kubernetes.%v", testName)

kubeClient := fake.NewSimpleClientset()

if !testCase.expectNotFound {
_, err := kubeClient.AppsV1().Deployments(testCase.deployment.Namespace).Create(ctx, testCase.deployment, metav1.CreateOptions{})
c.Assert(err, IsNil, Commentf(test.ErrErrorFmt, testName))
}

deployment, err := GetDeployment(kubeClient, testCase.deployment.Namespace, testCase.deployment.Name)
if testCase.expectNotFound {
c.Assert(apierrors.IsNotFound(err), Equals, true, Commentf(test.ErrResultFmt, testName))
continue
}
c.Assert(err, IsNil, Commentf(test.ErrErrorFmt, testName))
c.Assert(deployment.Name, Equals, testCase.deployment.Name, Commentf(test.ErrResultFmt, testName))
}
}

0 comments on commit 6c1c43e

Please sign in to comment.