mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-21 10:51:29 +00:00
kubeadm: improve test coverage by validating the data structure
data structure is what returned if everything okay, but this structure is not validated at all both in `init` and `join` cmd. Signed-off-by: Dave Chen <dave.chen@arm.com>
This commit is contained in:
parent
765ef1783b
commit
a825450707
@ -22,10 +22,16 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/google/go-cmp/cmp"
|
||||||
|
"github.com/google/go-cmp/cmp/cmpopts"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/util/sets"
|
"k8s.io/apimachinery/pkg/util/sets"
|
||||||
|
|
||||||
|
v1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/bootstraptoken/v1"
|
||||||
|
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
|
||||||
kubeadmapiv1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta3"
|
kubeadmapiv1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta3"
|
||||||
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/options"
|
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/options"
|
||||||
|
"k8s.io/kubernetes/cmd/kubeadm/app/constants"
|
||||||
)
|
)
|
||||||
|
|
||||||
var testInitConfig = fmt.Sprintf(`---
|
var testInitConfig = fmt.Sprintf(`---
|
||||||
@ -98,6 +104,39 @@ func TestNewInitData(t *testing.T) {
|
|||||||
flags: map[string]string{
|
flags: map[string]string{
|
||||||
options.CfgPath: configFilePath,
|
options.CfgPath: configFilePath,
|
||||||
},
|
},
|
||||||
|
validate: func(t *testing.T, data *initData) {
|
||||||
|
validData := &initData{
|
||||||
|
certificatesDir: kubeadmapiv1.DefaultCertificatesDir,
|
||||||
|
kubeconfigPath: constants.GetAdminKubeConfigPath(),
|
||||||
|
kubeconfigDir: constants.KubernetesDir,
|
||||||
|
ignorePreflightErrors: sets.New("c", "d"),
|
||||||
|
cfg: &kubeadmapi.InitConfiguration{
|
||||||
|
NodeRegistration: kubeadmapi.NodeRegistrationOptions{
|
||||||
|
Name: "somename",
|
||||||
|
CRISocket: "unix:///var/run/containerd/containerd.sock",
|
||||||
|
IgnorePreflightErrors: []string{"c", "d"},
|
||||||
|
ImagePullPolicy: "IfNotPresent",
|
||||||
|
},
|
||||||
|
LocalAPIEndpoint: kubeadmapi.APIEndpoint{
|
||||||
|
AdvertiseAddress: "1.2.3.4",
|
||||||
|
BindPort: 6443,
|
||||||
|
},
|
||||||
|
BootstrapTokens: []v1.BootstrapToken{
|
||||||
|
{
|
||||||
|
Token: &v1.BootstrapTokenString{ID: "abcdef", Secret: "0123456789abcdef"},
|
||||||
|
Usages: []string{"signing", "authentication"},
|
||||||
|
TTL: &metav1.Duration{
|
||||||
|
Duration: constants.DefaultTokenDuration,
|
||||||
|
},
|
||||||
|
Groups: []string{"system:bootstrappers:kubeadm:default-node-token"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
if diff := cmp.Diff(validData, data, cmp.AllowUnexported(initData{}), cmpopts.IgnoreFields(initData{}, "client", "cfg.ClusterConfiguration", "cfg.NodeRegistration.Taints")); diff != "" {
|
||||||
|
t.Fatalf("newInitData returned data (-want,+got):\n%s", diff)
|
||||||
|
}
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "--node-name flags override config from file",
|
name: "--node-name flags override config from file",
|
||||||
@ -164,7 +203,6 @@ func TestNewInitData(t *testing.T) {
|
|||||||
if err == nil && tc.expectError {
|
if err == nil && tc.expectError {
|
||||||
t.Fatal("newInitData didn't return error when expected")
|
t.Fatal("newInitData didn't return error when expected")
|
||||||
}
|
}
|
||||||
|
|
||||||
// exec additional validation on the returned value
|
// exec additional validation on the returned value
|
||||||
if tc.validate != nil {
|
if tc.validate != nil {
|
||||||
tc.validate(t, data)
|
tc.validate(t, data)
|
||||||
|
@ -24,9 +24,15 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/google/go-cmp/cmp"
|
||||||
|
"github.com/google/go-cmp/cmp/cmpopts"
|
||||||
|
|
||||||
|
v1 "k8s.io/api/core/v1"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/util/sets"
|
"k8s.io/apimachinery/pkg/util/sets"
|
||||||
"k8s.io/klog/v2"
|
"k8s.io/klog/v2"
|
||||||
|
|
||||||
|
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
|
||||||
kubeadmapiv1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta3"
|
kubeadmapiv1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta3"
|
||||||
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/options"
|
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/options"
|
||||||
kubeconfigutil "k8s.io/kubernetes/cmd/kubeadm/app/util/kubeconfig"
|
kubeconfigutil "k8s.io/kubernetes/cmd/kubeadm/app/util/kubeconfig"
|
||||||
@ -206,6 +212,37 @@ func TestNewJoinData(t *testing.T) {
|
|||||||
flags: map[string]string{
|
flags: map[string]string{
|
||||||
options.CfgPath: configFilePath,
|
options.CfgPath: configFilePath,
|
||||||
},
|
},
|
||||||
|
validate: func(t *testing.T, data *joinData) {
|
||||||
|
validData := &joinData{
|
||||||
|
cfg: &kubeadmapi.JoinConfiguration{
|
||||||
|
TypeMeta: metav1.TypeMeta{Kind: "", APIVersion: ""},
|
||||||
|
NodeRegistration: kubeadmapi.NodeRegistrationOptions{
|
||||||
|
Name: "somename",
|
||||||
|
CRISocket: "unix:///var/run/containerd/containerd.sock",
|
||||||
|
IgnorePreflightErrors: []string{"c", "d"},
|
||||||
|
ImagePullPolicy: "IfNotPresent",
|
||||||
|
Taints: []v1.Taint{{Key: "node-role.kubernetes.io/control-plane", Effect: "NoSchedule"}},
|
||||||
|
},
|
||||||
|
CACertPath: kubeadmapiv1.DefaultCACertPath,
|
||||||
|
Discovery: kubeadmapi.Discovery{
|
||||||
|
BootstrapToken: &kubeadmapi.BootstrapTokenDiscovery{
|
||||||
|
Token: "abcdef.0123456789abcdef",
|
||||||
|
APIServerEndpoint: "1.2.3.4:6443",
|
||||||
|
UnsafeSkipCAVerification: true,
|
||||||
|
},
|
||||||
|
TLSBootstrapToken: "abcdef.0123456789abcdef",
|
||||||
|
Timeout: &metav1.Duration{Duration: kubeadmapiv1.DefaultDiscoveryTimeout},
|
||||||
|
},
|
||||||
|
ControlPlane: &kubeadmapi.JoinControlPlane{
|
||||||
|
CertificateKey: "c39a18bae4a72e71b178661f437363da218a3efb83ddb03f1cd91d9ae1da41bd",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
ignorePreflightErrors: sets.New("c", "d"),
|
||||||
|
}
|
||||||
|
if diff := cmp.Diff(validData, data, cmp.AllowUnexported(joinData{}), cmpopts.IgnoreFields(joinData{}, "client", "initCfg", "cfg.ControlPlane.LocalAPIEndpoint")); diff != "" {
|
||||||
|
t.Fatalf("newJoinData returned data (-want,+got):\n%s", diff)
|
||||||
|
}
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "--node-name flags override config from file",
|
name: "--node-name flags override config from file",
|
||||||
|
Loading…
Reference in New Issue
Block a user