mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-09 12:07:47 +00:00
add new test cases to test json output when fetch kubeadm config from server
This commit is contained in:
parent
98437cb4be
commit
ecaf269d3c
@ -26,13 +26,13 @@ import (
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/pflag"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/util/duration"
|
||||
"k8s.io/cli-runtime/pkg/genericclioptions"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/klog/v2"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
|
||||
kubeadmscheme "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/scheme"
|
||||
kubeadmapiv1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta3"
|
||||
|
@ -32,11 +32,18 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/lithammer/dedent"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/pflag"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
v1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
fakeclientset "k8s.io/client-go/kubernetes/fake"
|
||||
clientgotesting "k8s.io/client-go/testing"
|
||||
"k8s.io/client-go/tools/clientcmd"
|
||||
|
||||
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
|
||||
@ -45,6 +52,7 @@ import (
|
||||
kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
|
||||
certsphase "k8s.io/kubernetes/cmd/kubeadm/app/phases/certs"
|
||||
kubeconfigphase "k8s.io/kubernetes/cmd/kubeadm/app/phases/kubeconfig"
|
||||
kubeconfigutil "k8s.io/kubernetes/cmd/kubeadm/app/util/kubeconfig"
|
||||
"k8s.io/kubernetes/cmd/kubeadm/app/util/pkiutil"
|
||||
testutil "k8s.io/kubernetes/cmd/kubeadm/test"
|
||||
cmdtestutil "k8s.io/kubernetes/cmd/kubeadm/test/cmd"
|
||||
@ -492,6 +500,7 @@ func TestRunCmdCertsExpiration(t *testing.T) {
|
||||
if err := os.RemoveAll(kdir); err != nil {
|
||||
t.Fatalf("Failed to teardown: %s", err)
|
||||
}
|
||||
clientSetFromFile = kubeconfigutil.ClientSetFromFile
|
||||
}()
|
||||
|
||||
cfg := testutil.GetDefaultInternalConfig(t)
|
||||
@ -555,17 +564,43 @@ kubernetesVersion: %s`,
|
||||
cfg.CertificatesDir,
|
||||
kubeadmconstants.MinimumControlPlaneVersion.String())
|
||||
|
||||
// Write the minimal kubeadm config to a file
|
||||
customConfigPath := kdir + "/kubeadm.conf"
|
||||
f, err := os.Create(customConfigPath)
|
||||
require.NoError(t, err)
|
||||
_, err = f.Write([]byte(kubeadmConfig))
|
||||
require.NoError(t, err)
|
||||
|
||||
// fakeClientSetFromFile returns a fake clientset with kubeadm config map
|
||||
var fakeClientSetFromFile = func(_ string) (kubernetes.Interface, error) {
|
||||
client := fakeclientset.NewSimpleClientset()
|
||||
client.PrependReactor("get", "configmaps", func(action clientgotesting.Action) (bool, runtime.Object, error) {
|
||||
getAction := action.(clientgotesting.GetAction)
|
||||
if getAction.GetNamespace() == metav1.NamespaceSystem && getAction.GetName() == kubeadmconstants.KubeadmConfigConfigMap {
|
||||
cm := &v1.ConfigMap{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: kubeadmconstants.KubeadmConfigConfigMap,
|
||||
Namespace: metav1.NamespaceSystem,
|
||||
},
|
||||
Data: map[string]string{
|
||||
kubeadmconstants.ClusterConfigurationConfigMapKey: dedent.Dedent(kubeadmConfig),
|
||||
},
|
||||
}
|
||||
return true, cm, nil
|
||||
}
|
||||
// Return an empty config map for kubelet and kube-proxy components.
|
||||
return true, &v1.ConfigMap{}, nil
|
||||
})
|
||||
return client, nil
|
||||
}
|
||||
|
||||
// Select a certificate used to simulate a missing certificate file
|
||||
brokenCertName := kubeadmconstants.APIServerCertAndKeyBaseName
|
||||
brokenCertPath, _ := pkiutil.PathsForCertAndKey(cfg.CertificatesDir, brokenCertName)
|
||||
|
||||
type testCase struct {
|
||||
name string
|
||||
config string
|
||||
output string
|
||||
brokenCertName string
|
||||
}
|
||||
@ -575,11 +610,17 @@ kubernetesVersion: %s`,
|
||||
cmd := newCmdCertsExpiration(&output, kdir)
|
||||
args := []string{
|
||||
fmt.Sprintf("--cert-dir=%s", cfg.CertificatesDir),
|
||||
fmt.Sprintf("--config=%s", customConfigPath),
|
||||
}
|
||||
if tc.config != "" {
|
||||
args = append(args, fmt.Sprintf("--config=%s", tc.config))
|
||||
} else {
|
||||
clientSetFromFile = fakeClientSetFromFile
|
||||
defer func() {
|
||||
clientSetFromFile = kubeconfigutil.ClientSetFromFile
|
||||
}()
|
||||
}
|
||||
if tc.output != "" {
|
||||
args = append(args, fmt.Sprintf("-o=%s", tc.output))
|
||||
|
||||
}
|
||||
cmd.SetArgs(args)
|
||||
require.NoError(t, cmd.Execute())
|
||||
@ -619,21 +660,43 @@ kubernetesVersion: %s`,
|
||||
|
||||
testCases := []testCase{
|
||||
{
|
||||
name: "print columns and no missing certs",
|
||||
name: "fetch kubeadm config from file and print columns and no missing certs",
|
||||
config: customConfigPath,
|
||||
output: "",
|
||||
},
|
||||
{
|
||||
name: "print json and no missing certs",
|
||||
name: "fetch kubeadm config from server and print columns and no missing certs",
|
||||
output: "",
|
||||
},
|
||||
{
|
||||
name: "fetch kubeadm config from file and print json and no missing certs",
|
||||
config: customConfigPath,
|
||||
output: "json",
|
||||
},
|
||||
{
|
||||
name: "fetch kubeadm config from server and print json and no missing certs",
|
||||
output: "json",
|
||||
},
|
||||
// all broken cases must be at the end of the list.
|
||||
{
|
||||
name: "print columns and missing certs",
|
||||
name: "fetch kubeadm config from file and print columns and missing certs",
|
||||
config: customConfigPath,
|
||||
output: "",
|
||||
brokenCertName: brokenCertName,
|
||||
},
|
||||
{
|
||||
name: "print json and missing certs",
|
||||
name: "fetch kubeadm config from server and print columns and missing certs",
|
||||
output: "",
|
||||
brokenCertName: brokenCertName,
|
||||
},
|
||||
{
|
||||
name: "fetch kubeadm config from file and print json and missing certs",
|
||||
config: customConfigPath,
|
||||
output: "json",
|
||||
brokenCertName: brokenCertName,
|
||||
},
|
||||
{
|
||||
name: "fetch kubeadm config from server and print json and missing certs",
|
||||
output: "json",
|
||||
brokenCertName: brokenCertName,
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user