Skip to content

Commit

Permalink
test: ListDeployment
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 committed Nov 28, 2024
1 parent e47e1d1 commit 7302965
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions kubernetes/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,103 @@ func (s *TestSuite) TestGetDeployment(c *C) {
c.Assert(deployment.Name, Equals, testCase.deployment.Name, Commentf(test.ErrResultFmt, testName))
}
}

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

type testCase struct {
deployments []*appsv1.Deployment
labelSelector map[string]string
skipCreate bool
expectError bool
}
testCases := map[string]testCase{
"ListDeployments(...):": {
deployments: []*appsv1.Deployment{
{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "default",
},
},
},
},
"ListDeployments(...): not found": {
deployments: []*appsv1.Deployment{
{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "default",
},
},
},
skipCreate: true,
},
"ListDeployments(...): with single label": {
deployments: []*appsv1.Deployment{
{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "default",
Labels: map[string]string{
"foo": "bar",
"baz": "qux",
"quux": "corge",
},
},
},
},
labelSelector: map[string]string{
"foo": "bar",
},
},
"ListDeployments(...): with multiple labels": {
deployments: []*appsv1.Deployment{
{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "default",
Labels: map[string]string{
"foo": "bar",
"baz": "qux",
"quux": "corge",
},
},
},
},
labelSelector: map[string]string{
"foo": "bar",
"baz": "qux",
},
},
}
for testName, testCase := range testCases {
c.Logf("testing kubernetes.%v", testName)

kubeClient := fake.NewSimpleClientset()

for _, deployment := range testCase.deployments {
if testCase.skipCreate {
continue
}

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

deployments, err := ListDeployments(kubeClient, testCase.deployments[0].Namespace, nil)
if testCase.expectError {
c.Assert(err, NotNil, Commentf(test.ErrErrorFmt, testName))
continue
}
c.Assert(err, IsNil, Commentf(test.ErrErrorFmt, testName))

if testCase.skipCreate {
c.Assert(len(deployments.Items), Equals, 0, Commentf(test.ErrResultFmt, testName))
continue
}

c.Assert(len(deployments.Items), Equals, len(testCase.deployments), Commentf(test.ErrResultFmt, testName))
}
}

0 comments on commit 7302965

Please sign in to comment.