From 3ddedf00e5cf62021e7943ab61a91677a40b0570 Mon Sep 17 00:00:00 2001 From: Dave Chen Date: Mon, 31 Oct 2022 15:19:17 +0800 Subject: [PATCH] kubeadm: add testcases for `kubeadm reset` For parity with `init` and `join`, the test file only covers some basic scenarios and is expected to be expanded when the config file for `kubeadm reset` is ready. Signed-off-by: Dave Chen --- cmd/kubeadm/app/cmd/reset_test.go | 115 ++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 cmd/kubeadm/app/cmd/reset_test.go diff --git a/cmd/kubeadm/app/cmd/reset_test.go b/cmd/kubeadm/app/cmd/reset_test.go new file mode 100644 index 00000000000..54bac67dfef --- /dev/null +++ b/cmd/kubeadm/app/cmd/reset_test.go @@ -0,0 +1,115 @@ +/* +Copyright 2022 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package cmd + +import ( + "strings" + "testing" + + "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" + "k8s.io/apimachinery/pkg/util/sets" + + "k8s.io/kubernetes/cmd/kubeadm/app/cmd/options" +) + +func TestNewResetData(t *testing.T) { + testCases := []struct { + name string + args []string + flags map[string]string + validate func(*testing.T, *resetData) + expectError string + data *resetData + }{ + { + name: "flags parsed correctly", + flags: map[string]string{ + options.CertificatesDir: "/tmp", + options.NodeCRISocket: "unix:///var/run/crio/crio.sock", + options.IgnorePreflightErrors: "all", + options.ForceReset: "true", + options.DryRun: "true", + options.CleanupTmpDir: "true", + }, + data: &resetData{ + certificatesDir: "/tmp", + criSocketPath: "unix:///var/run/crio/crio.sock", + ignorePreflightErrors: sets.NewString("all"), + forceReset: true, + dryRun: true, + cleanupTmpDir: true, + }, + }, + { + name: "fails if preflight ignores all but has individual check", + flags: map[string]string{ + options.IgnorePreflightErrors: "all,something-else", + }, + expectError: "don't specify individual checks if 'all' is used", + }, + { + name: "pre-flights errors from CLI args", + flags: map[string]string{ + options.IgnorePreflightErrors: "a,b", + }, + validate: expectedResetIgnorePreflightErrors(sets.NewString("a", "b")), + }, + } + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + // initialize an external reset option and inject it to the reset cmd + resetOptions := newResetOptions() + cmd := newCmdReset(nil, nil, resetOptions) + + // sets cmd flags (that will be reflected on the reset options) + for f, v := range tc.flags { + cmd.Flags().Set(f, v) + } + + // test newResetData method + data, err := newResetData(cmd, resetOptions, nil, nil) + if err != nil && !strings.Contains(err.Error(), tc.expectError) { + t.Fatalf("newResetData returned unexpected error, expected: %s, got %v", tc.expectError, err) + } + if err == nil && len(tc.expectError) != 0 { + t.Fatalf("newResetData didn't return error expected %s", tc.expectError) + } + + if tc.data != nil { + if diff := cmp.Diff(tc.data, data, cmp.AllowUnexported(resetData{}), cmpopts.IgnoreFields(resetData{}, "client", "cfg")); diff != "" { + t.Fatalf("newResetData returned data (-want,+got):\n%s", diff) + } + } + // exec additional validation on the returned value + if tc.validate != nil { + tc.validate(t, data) + } + }) + } +} + +func expectedResetIgnorePreflightErrors(expected sets.String) func(t *testing.T, data *resetData) { + return func(t *testing.T, data *resetData) { + if !expected.Equal(data.ignorePreflightErrors) { + t.Errorf("Invalid ignore preflight errors. Expected: %v. Actual: %v", expected.List(), data.ignorePreflightErrors.List()) + } + if data.cfg != nil && !expected.HasAll(data.cfg.NodeRegistration.IgnorePreflightErrors...) { + t.Errorf("Invalid ignore preflight errors in InitConfiguration. Expected: %v. Actual: %v", expected.List(), data.cfg.NodeRegistration.IgnorePreflightErrors) + } + } +}