mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-29 22:46:12 +00:00
Merge pull request #123118 from my-git9/idempotency-ut
kubeadm: increase ut coverage for apiclient/idempotency
This commit is contained in:
commit
dd301d0f23
@ -18,15 +18,18 @@ package apiclient
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
v1 "k8s.io/api/core/v1"
|
v1 "k8s.io/api/core/v1"
|
||||||
|
rbac "k8s.io/api/rbac/v1"
|
||||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
|
clientset "k8s.io/client-go/kubernetes"
|
||||||
"k8s.io/client-go/kubernetes/fake"
|
"k8s.io/client-go/kubernetes/fake"
|
||||||
core "k8s.io/client-go/testing"
|
core "k8s.io/client-go/testing"
|
||||||
)
|
)
|
||||||
@ -237,3 +240,215 @@ func TestMutateConfigMapWithConflict(t *testing.T) {
|
|||||||
t.Fatalf("ConfigMap mutation with conflict was invalid, has: %q", cm.Data["key"])
|
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
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user