Skip to content

Commit

Permalink
Merge pull request kubernetes#118941 from neolit123/1.28-restore-test…
Browse files Browse the repository at this point in the history
…-function-for-config-migrate

kubeadm: restore and generalize the TestMigrateOldConfig test
  • Loading branch information
k8s-ci-robot authored Jul 1, 2023
2 parents 86fec81 + 0537c1d commit 14b08b8
Show file tree
Hide file tree
Showing 3 changed files with 341 additions and 391 deletions.
186 changes: 0 additions & 186 deletions cmd/kubeadm/app/cmd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import (
outputapischeme "k8s.io/kubernetes/cmd/kubeadm/app/apis/output/scheme"
"k8s.io/kubernetes/cmd/kubeadm/app/constants"
kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util"
configutil "k8s.io/kubernetes/cmd/kubeadm/app/util/config"
"k8s.io/kubernetes/cmd/kubeadm/app/util/output"
utilruntime "k8s.io/kubernetes/cmd/kubeadm/app/util/runtime"
)
Expand All @@ -52,44 +51,6 @@ var (
// kubeadm lookup dl.k8s.io to resolve what the latest stable release is
dummyKubernetesVersion = constants.MinimumControlPlaneVersion
dummyKubernetesVersionStr = dummyKubernetesVersion.String()

// predefined configuration contents for migration and validation
cfgInvalidSubdomain = []byte(dedent.Dedent(fmt.Sprintf(`
apiVersion: %s
kind: InitConfiguration
nodeRegistration:
criSocket: %s
name: foo bar # not a valid subdomain
`, kubeadmapiv1.SchemeGroupVersion.String(), constants.UnknownCRISocket)))

cfgUnknownAPI = []byte(dedent.Dedent(fmt.Sprintf(`
apiVersion: foo/bar # not a valid GroupVersion
kind: zzz # not a valid Kind
nodeRegistration:
criSocket: %s
`, constants.UnknownCRISocket)))

cfgLegacyAPI = []byte(dedent.Dedent(fmt.Sprintf(`
apiVersion: kubeadm.k8s.io/v1beta1 # legacy API
kind: InitConfiguration
nodeRegistration:
criSocket: %s
`, constants.UnknownCRISocket)))

cfgUnknownField = []byte(dedent.Dedent(fmt.Sprintf(`
apiVersion: %s
kind: InitConfiguration
foo: bar
nodeRegistration:
criSocket: %s
`, kubeadmapiv1.SchemeGroupVersion.String(), constants.UnknownCRISocket)))

cfgValid = []byte(dedent.Dedent(fmt.Sprintf(`
apiVersion: %s
kind: InitConfiguration
nodeRegistration:
criSocket: %s
`, kubeadmapiv1.SchemeGroupVersion.String(), constants.UnknownCRISocket)))
)

func TestNewCmdConfigImagesList(t *testing.T) {
Expand Down Expand Up @@ -428,153 +389,6 @@ func TestImagesPull(t *testing.T) {
}
}

func TestMigrate(t *testing.T) {
cfgFileInvalidSubdomain, cleanup := tempConfig(t, cfgInvalidSubdomain)
defer cleanup()
cfgFileUnknownAPI, cleanup := tempConfig(t, cfgUnknownAPI)
defer cleanup()
cfgFileLegacyAPI, cleanup := tempConfig(t, cfgLegacyAPI)
defer cleanup()
cfgFileUnknownField, cleanup := tempConfig(t, cfgUnknownField)
defer cleanup()
cfgFileValid, cleanup := tempConfig(t, cfgValid)
defer cleanup()

testcases := []struct {
name string
cfg string
expectedError bool
}{
{
name: "invalid subdomain",
cfg: cfgFileInvalidSubdomain,
expectedError: true,
},
{
name: "unknown API GVK",
cfg: cfgFileUnknownAPI,
expectedError: true,
},
{
name: "legacy API GVK",
cfg: cfgFileLegacyAPI,
expectedError: true,
},
{
name: "unknown field",
cfg: cfgFileUnknownField,
expectedError: true,
},
{
name: "valid",
cfg: cfgFileValid,
expectedError: false,
},
}

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
var output bytes.Buffer
command := newCmdConfigMigrate(&output)
if err := command.Flags().Set("old-config", tc.cfg); err != nil {
t.Fatalf("failed to set old-config flag")
}
newConfigPath := filepath.Join(filepath.Dir(tc.cfg), "new-migrated-config")
if err := command.Flags().Set("new-config", newConfigPath); err != nil {
t.Fatalf("failed to set new-config flag")
}
err := command.RunE(nil, nil)
if (err != nil) != tc.expectedError {
t.Fatalf("Expected error from validate command: %v, got: %v, error: %v",
tc.expectedError, err != nil, err)
}
if err != nil {
return
}
if _, err := configutil.LoadInitConfigurationFromFile(newConfigPath); err != nil {
t.Fatalf("Could not read output back into internal type: %v", err)
}
})
}

}

func TestValidate(t *testing.T) {
cfgFileInvalidSubdomain, cleanup := tempConfig(t, cfgInvalidSubdomain)
defer cleanup()
cfgFileUnknownAPI, cleanup := tempConfig(t, cfgUnknownAPI)
defer cleanup()
cfgFileLegacyAPI, cleanup := tempConfig(t, cfgLegacyAPI)
defer cleanup()
cfgFileUnknownField, cleanup := tempConfig(t, cfgUnknownField)
defer cleanup()
cfgFileValid, cleanup := tempConfig(t, cfgValid)
defer cleanup()

testcases := []struct {
name string
cfg string
expectedError bool
}{
{
name: "invalid subdomain",
cfg: cfgFileInvalidSubdomain,
expectedError: true,
},
{
name: "unknown API GVK",
cfg: cfgFileUnknownAPI,
expectedError: true,
},
{
name: "legacy API GVK",
cfg: cfgFileLegacyAPI,
expectedError: true,
},
{
name: "unknown field",
cfg: cfgFileUnknownField,
expectedError: true,
},
{
name: "valid",
cfg: cfgFileValid,
expectedError: false,
},
}

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
var output bytes.Buffer
command := newCmdConfigValidate(&output)
if err := command.Flags().Set("config", tc.cfg); err != nil {
t.Fatalf("Failed to set config flag")
}
if err := command.RunE(nil, nil); (err != nil) != tc.expectedError {
t.Fatalf("Expected error from validate command: %v, got: %v, error: %v",
tc.expectedError, err != nil, err)
}
})
}
}

// Returns the name of the file created and a cleanup callback
func tempConfig(t *testing.T, config []byte) (string, func()) {
t.Helper()
tmpDir, err := os.MkdirTemp("", "kubeadm-migration-test")
if err != nil {
t.Fatalf("Unable to create temporary directory: %v", err)
}
configFilePath := filepath.Join(tmpDir, "test-config-file")
if err := os.WriteFile(configFilePath, config, 0644); err != nil {
os.RemoveAll(tmpDir)
t.Fatalf("Failed writing a config file: %v", err)
}
return configFilePath, func() {
os.RemoveAll(tmpDir)
}
}

func TestNewCmdConfigPrintActionDefaults(t *testing.T) {
tests := []struct {
name string
Expand Down
4 changes: 2 additions & 2 deletions cmd/kubeadm/app/util/config/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,8 @@ func MigrateOldConfig(oldConfig []byte, allowExperimental bool) ([]byte, error)

// ValidateConfig takes a byte slice containing a kubeadm configuration and performs conversion
// to internal types and validation.
func ValidateConfig(oldConfig []byte, allowExperimental bool) error {
gvkmap, err := kubeadmutil.SplitYAMLDocuments(oldConfig)
func ValidateConfig(config []byte, allowExperimental bool) error {
gvkmap, err := kubeadmutil.SplitYAMLDocuments(config)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 14b08b8

Please sign in to comment.