Skip to content

Commit

Permalink
add deployment_test
Browse files Browse the repository at this point in the history
  • Loading branch information
jiayouxujin committed Jun 12, 2023
1 parent 7bd9eb1 commit 41a429b
Show file tree
Hide file tree
Showing 3 changed files with 280 additions and 0 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/openkruise/kruise-api v1.3.0
github.com/robfig/cron/v3 v3.0.1
github.com/satori/go.uuid v1.2.0
github.com/stretchr/testify v1.8.0
go.uber.org/zap v1.24.0
k8s.io/api v0.26.1
k8s.io/apimachinery v0.26.1
Expand All @@ -24,6 +25,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-logr/zapr v1.2.3 // indirect
Expand All @@ -47,6 +49,7 @@ require (
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.14.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1m
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/evanphx/json-patch v0.5.2/go.mod h1:ZWS5hhDbVDyob71nXKNL0+PWn6ToqBHMikGIFbs31qQ=
github.com/evanphx/json-patch v4.12.0+incompatible h1:4onqiflcdA9EOZ4RxV643DvftH5pOlLGNtQ5lPWQu84=
github.com/evanphx/json-patch v4.12.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/evanphx/json-patch/v5 v5.6.0 h1:b91NhWfaz02IuVxO9faSllyAtNXHMPkC5J8sJCLunww=
github.com/evanphx/json-patch/v5 v5.6.0/go.mod h1:G79N1coSVB93tBe7j6PhzjmR3/2VvlbKOFpnXhI9Bw4=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
Expand Down Expand Up @@ -282,13 +283,16 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag1KpM8ahLw8=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
273 changes: 273 additions & 0 deletions pkg/client/k8s/deployment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,273 @@
package k8s

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

func TestCreateIfNotExistsDeployment(t *testing.T) {
ns := "unit-test"
testDeployment := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: ns,
},
}

tests := []struct {
name string
deployment *appsv1.Deployment
existingDeploy *appsv1.Deployment
expErr bool
}{
{
name: "create new deployment",
deployment: testDeployment.DeepCopy(),
existingDeploy: nil,
expErr: false,
}, {
name: "create existing deployment",
deployment: testDeployment.DeepCopy(),
existingDeploy: testDeployment.DeepCopy(),
expErr: false,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert := assert.New(t)

objs := make([]client.Object, 0)
if test.existingDeploy != nil {
objs = append(objs, test.existingDeploy)
}
scheme := runtime.NewScheme()
_ = appsv1.AddToScheme(scheme)
fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(objs...).Build()
c := NewK8sClient(fakeClient, ctrl.Log.WithName("deployment-test"))

err := c.CreateIfNotExistsDeployment(test.deployment)
if test.expErr {
assert.Error(err)
} else {
assert.NoError(err)
dep := &appsv1.Deployment{}
err := fakeClient.Get(context.TODO(), types.NamespacedName{
Name: test.deployment.Name,
Namespace: test.deployment.Namespace,
}, dep)
assert.NoError(err)
assert.Equal(test.deployment.Name, dep.Name)
}
})
}
}

func TestGetDeployment(t *testing.T) {
ns := "unit-test"
testDeployment := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: ns,
},
}
tests := []struct {
name string
deployment *appsv1.Deployment
existingDeploy *appsv1.Deployment
expErr bool
}{
{
name: "get existing deployment",
deployment: testDeployment.DeepCopy(),
existingDeploy: testDeployment.DeepCopy(),
expErr: false,
}, {
name: "get non-existing deployment",
deployment: testDeployment.DeepCopy(),
existingDeploy: nil,
expErr: true,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert := assert.New(t)

objs := make([]client.Object, 0)
if test.existingDeploy != nil {
objs = append(objs, test.existingDeploy)
}
scheme := runtime.NewScheme()
_ = appsv1.AddToScheme(scheme)
fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(objs...).Build()
c := NewK8sClient(fakeClient, ctrl.Log.WithName("deployment-test"))

dep, err := c.GetDeployment(types.NamespacedName{
Name: test.deployment.Name,
Namespace: test.deployment.Namespace,
})
if test.expErr {
assert.Error(err)
assert.True(errors.IsNotFound(err))
} else {
assert.NoError(err)
assert.Equal(test.deployment.Name, dep.Name)
}
})
}
}

func TestUpdateDeployment(t *testing.T) {
ns := "unit-test"
testDeployment := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: ns,
},
Status: appsv1.DeploymentStatus{
Replicas: 1,
},
}

tests := []struct {
name string
deploy *appsv1.Deployment
existingDeploy *appsv1.Deployment
expErr bool
}{
{
name: "update existing deployment",
deploy: testDeployment.DeepCopy(),
existingDeploy: testDeployment.DeepCopy(),
expErr: false,
}, {
name: "update non-existing deployment",
deploy: testDeployment.DeepCopy(),
existingDeploy: nil,
expErr: true,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert := assert.New(t)

objs := make([]client.Object, 0)
if test.existingDeploy != nil {
objs = append(objs, test.existingDeploy)
}
scheme := runtime.NewScheme()
_ = appsv1.AddToScheme(scheme)
fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(objs...).Build()
c := NewK8sClient(fakeClient, ctrl.Log.WithName("deployment-test"))

dep, err := c.GetDeployment(types.NamespacedName{
Name: test.deploy.Name,
Namespace: test.deploy.Namespace,
})
if dep != nil {
dep.Status.Replicas = 2
err = c.UpdateDeployment(dep)
}
if test.expErr {
assert.Error(err)
assert.True(errors.IsNotFound(err))
} else {
assert.NoError(err)

dep := &appsv1.Deployment{}
err := fakeClient.Get(context.TODO(), types.NamespacedName{
Name: test.deploy.Name,
Namespace: test.deploy.Namespace,
}, dep)
assert.NoError(err)
assert.Equal(int32(2), dep.Status.Replicas)
}
})
}
}
func TestListDeploymentPods(t *testing.T) {
ns := "unit-test"
testDeployment := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: ns,
},
Spec: appsv1.DeploymentSpec{
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{"app": "test"},
},
},
}

testPod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "test-pod",
Namespace: ns,
Labels: map[string]string{"app": "test"},
},
}

tests := []struct {
name string
dep *appsv1.Deployment
pod *corev1.Pod
expPods int
expErr bool
}{
{
name: "list pods for deployment",
dep: testDeployment.DeepCopy(),
pod: testPod.DeepCopy(),
expPods: 1,
expErr: false,
}, {
name: "no pods",
dep: testDeployment.DeepCopy(),
pod: nil,
expPods: 0,
expErr: false,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert := assert.New(t)

objs := make([]client.Object, 0)
objs = append(objs, test.dep)
if test.pod != nil {
objs = append(objs, test.pod)
}
scheme := runtime.NewScheme()
_ = appsv1.AddToScheme(scheme)
_ = corev1.AddToScheme(scheme)
fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(objs...).Build()
c := NewK8sClient(fakeClient, ctrl.Log.WithName("deployment-test"))

pods, err := c.ListDeploymentPods(types.NamespacedName{
Name: test.dep.Name,
Namespace: test.dep.Namespace,
})
if test.expErr {
assert.Error(err)
assert.True(errors.IsNotFound(err))
} else {
assert.NoError(err)
assert.Equal(test.expPods, len(pods.Items))
}
})
}
}

0 comments on commit 41a429b

Please sign in to comment.