Skip to content

Commit

Permalink
Merge pull request #123118 from my-git9/idempotency-ut
Browse files Browse the repository at this point in the history
kubeadm: increase ut coverage for apiclient/idempotency
  • Loading branch information
k8s-ci-robot committed Feb 5, 2024
2 parents 77775d4 + deec79a commit dd301d0
Showing 1 changed file with 215 additions and 0 deletions.
215 changes: 215 additions & 0 deletions cmd/kubeadm/app/util/apiclient/idempotency_test.go
Expand Up @@ -18,15 +18,18 @@ package apiclient

import (
"context"
"reflect"
"testing"

"github.com/pkg/errors"

v1 "k8s.io/api/core/v1"
rbac "k8s.io/api/rbac/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/fake"
core "k8s.io/client-go/testing"
)
Expand Down Expand Up @@ -237,3 +240,215 @@ func TestMutateConfigMapWithConflict(t *testing.T) {
t.Fatalf("ConfigMap mutation with conflict was invalid, has: %q", cm.Data["key"])
}
}

func TestGetConfigMapWithShortRetry(t *testing.T) {
type args struct {
client clientset.Interface
namespace string
name string
}
tests := []struct {
name string
args args
want *v1.ConfigMap
wantErr bool
}{
{
name: "ConfigMap exists",
args: args{
client: newMockClientForTest(t, "default", "foo", "ConfigMap"),
namespace: "default",
name: "foo",
},
want: &v1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: configMapName,
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "default",
},
},
wantErr: false,
},
{
name: "ConfigMap does not exist",
args: args{
client: fake.NewSimpleClientset(),
namespace: "default",
name: "foo",
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetConfigMapWithShortRetry(tt.args.client, tt.args.namespace, tt.args.name)
if (err != nil) != tt.wantErr {
t.Errorf("GetConfigMapWithShortRetry() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetConfigMapWithShortRetry() = %v, want %v", got, tt.want)
}
})
}
}

func TestCreateOrUpdateClusterRole(t *testing.T) {
testClusterRole := &rbac.ClusterRole{
TypeMeta: metav1.TypeMeta{
Kind: "ClusterRole",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
},
}

type args struct {
client clientset.Interface
clusterRole *rbac.ClusterRole
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "ClusterRole does not exist",
args: args{
client: fake.NewSimpleClientset(),
clusterRole: testClusterRole,
},
wantErr: false,
},
{
name: "ClusterRole exists",
args: args{
client: newMockClientForTest(t, "", "foo", "ClusterRole"),
clusterRole: testClusterRole,
},
wantErr: false,
},
{
name: "ClusterRole is invalid",
args: args{
client: fake.NewSimpleClientset(),
clusterRole: nil,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := CreateOrUpdateClusterRole(tt.args.client, tt.args.clusterRole); (err != nil) != tt.wantErr {
t.Errorf("CreateOrUpdateClusterRole() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func TestCreateOrUpdateClusterRoleBinding(t *testing.T) {
testClusterRoleBinding := &rbac.ClusterRoleBinding{
TypeMeta: metav1.TypeMeta{
Kind: "ClusterRoleBinding",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
},
}

type args struct {
client clientset.Interface
clusterRoleBinding *rbac.ClusterRoleBinding
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "ClusterRoleBinding does not exist",
args: args{
client: fake.NewSimpleClientset(),
clusterRoleBinding: testClusterRoleBinding,
},
wantErr: false,
},
{
name: "ClusterRoleBinding exists",
args: args{
client: newMockClientForTest(t, "", "foo", "ClusterRoleBinding"),
clusterRoleBinding: testClusterRoleBinding,
},
wantErr: false,
},
{
name: "ClusterRoleBinding is invalid",
args: args{
client: fake.NewSimpleClientset(),
clusterRoleBinding: nil,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := CreateOrUpdateClusterRoleBinding(tt.args.client, tt.args.clusterRoleBinding); (err != nil) != tt.wantErr {
t.Errorf("CreateOrUpdateClusterRoleBinding() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func newMockClientForTest(t *testing.T, namepsace string, name string, kind string) *fake.Clientset {
client := fake.NewSimpleClientset()

switch kind {
case "ConfigMap":
_, err := client.CoreV1().ConfigMaps(namepsace).Create(context.Background(), &v1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: configMapName,
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namepsace,
},
}, metav1.CreateOptions{})
if err != nil {
t.Fatalf("error creating ConfigMap: %v", err)
}
case "ClusterRole":
_, err := client.RbacV1().ClusterRoles().Create(context.Background(), &rbac.ClusterRole{
TypeMeta: metav1.TypeMeta{
Kind: "ClusterRole",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
}, metav1.CreateOptions{})
if err != nil {
t.Fatalf("error creating ClusterRole: %v", err)
}
case "ClusterRoleBinding":
_, err := client.RbacV1().ClusterRoleBindings().Create(context.Background(), &rbac.ClusterRoleBinding{
TypeMeta: metav1.TypeMeta{
Kind: "ClusterRoleBinding",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
}, metav1.CreateOptions{})
if err != nil {
t.Fatalf("error creating ClusterRoleBinding: %v", err)
}
}
return client
}

0 comments on commit dd301d0

Please sign in to comment.