Add more test coverage for kubeadm uploadconfig especially with idempotent case.

This commit is contained in:
Feng Min 2017-09-07 13:09:36 -07:00
parent a5b3e50eac
commit e5d205717b
2 changed files with 79 additions and 16 deletions

View File

@ -41,8 +41,13 @@ go_test(
library = ":go_default_library", library = ":go_default_library",
deps = [ deps = [
"//cmd/kubeadm/app/apis/kubeadm:go_default_library", "//cmd/kubeadm/app/apis/kubeadm:go_default_library",
"//cmd/kubeadm/app/apis/kubeadm/v1alpha1:go_default_library",
"//cmd/kubeadm/app/constants:go_default_library", "//cmd/kubeadm/app/constants:go_default_library",
"//pkg/api:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", "//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//vendor/k8s.io/client-go/kubernetes/fake:go_default_library", "//vendor/k8s.io/client-go/kubernetes/fake:go_default_library",
"//vendor/k8s.io/client-go/testing:go_default_library",
], ],
) )

View File

@ -19,37 +19,95 @@ package uploadconfig
import ( import (
"testing" "testing"
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"
clientsetfake "k8s.io/client-go/kubernetes/fake" clientsetfake "k8s.io/client-go/kubernetes/fake"
core "k8s.io/client-go/testing"
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm" kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
kubeadmapiext "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1alpha1"
kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants" kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
"k8s.io/kubernetes/pkg/api"
) )
func TestUploadConfiguration(t *testing.T) { func TestUploadConfiguration(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
cfg *kubeadmapi.MasterConfiguration errOnCreate error
wantErr bool errOnUpdate error
updateExisting bool
errExpected bool
verifyResult bool
}{ }{
{ {
"basic validation with correct key", name: "basic validation with correct key",
&kubeadmapi.MasterConfiguration{ verifyResult: true,
KubernetesVersion: "1.7.3", },
}, {
false, name: "update existing should report no error",
updateExisting: true,
verifyResult: true,
},
{
name: "unexpected errors for create should be returned",
errOnCreate: apierrors.NewUnauthorized(""),
errExpected: true,
},
{
name: "update existing show report error if unexpected error for update is returned",
errOnUpdate: apierrors.NewUnauthorized(""),
updateExisting: true,
errExpected: true,
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
client := clientsetfake.NewSimpleClientset() cfg := &kubeadmapi.MasterConfiguration{
if err := UploadConfiguration(tt.cfg, client); (err != nil) != tt.wantErr { KubernetesVersion: "1.7.3",
t.Errorf("UploadConfiguration() error = %v, wantErr %v", err, tt.wantErr)
} }
masterCfg, err := client.CoreV1().ConfigMaps(metav1.NamespaceSystem).Get(kubeadmconstants.MasterConfigurationConfigMap, metav1.GetOptions{}) client := clientsetfake.NewSimpleClientset()
if err != nil { if tt.errOnCreate != nil {
t.Errorf("Fail to query ConfigMap error = %v", err) client.PrependReactor("create", "configmaps", func(action core.Action) (bool, runtime.Object, error) {
} else if masterCfg.Data[kubeadmconstants.MasterConfigurationConfigMapKey] == "" { return true, nil, tt.errOnCreate
t.Errorf("Fail to find ConfigMap key = %v", err) })
}
// For idempotent test, we check the result of the second call.
if err := UploadConfiguration(cfg, client); !tt.updateExisting && (err != nil) != tt.errExpected {
t.Errorf("UploadConfiguration() error = %v, wantErr %v", err, tt.errExpected)
}
if tt.updateExisting {
if tt.errOnUpdate != nil {
client.PrependReactor("update", "configmaps", func(action core.Action) (bool, runtime.Object, error) {
return true, nil, tt.errOnUpdate
})
}
if err := UploadConfiguration(cfg, client); (err != nil) != tt.errExpected {
t.Errorf("UploadConfiguration() error = %v", err)
}
}
if tt.verifyResult {
masterCfg, err := client.CoreV1().ConfigMaps(metav1.NamespaceSystem).Get(kubeadmconstants.MasterConfigurationConfigMap, metav1.GetOptions{})
if err != nil {
t.Errorf("Fail to query ConfigMap error = %v", err)
}
configData := masterCfg.Data[kubeadmconstants.MasterConfigurationConfigMapKey]
if configData == "" {
t.Errorf("Fail to find ConfigMap key")
}
decodedExtCfg := &kubeadmapiext.MasterConfiguration{}
decodedCfg := &kubeadmapi.MasterConfiguration{}
if err := runtime.DecodeInto(api.Codecs.UniversalDecoder(), []byte(configData), decodedExtCfg); err != nil {
t.Errorf("unable to decode config from bytes: %v", err)
}
// Default and convert to the internal version
api.Scheme.Default(decodedExtCfg)
api.Scheme.Convert(decodedExtCfg, decodedCfg, nil)
if decodedCfg.KubernetesVersion != cfg.KubernetesVersion {
t.Errorf("Decoded value doesn't match, decoded = %#v, expected = %#v", decodedCfg.KubernetesVersion, cfg.KubernetesVersion)
}
} }
}) })
} }