diff --git a/cmd/kubeadm/app/cmd/certs.go b/cmd/kubeadm/app/cmd/certs.go index 62cf2fcdf0c..f83df855c1b 100644 --- a/cmd/kubeadm/app/cmd/certs.go +++ b/cmd/kubeadm/app/cmd/certs.go @@ -257,6 +257,7 @@ func getRenewSubCommands(out io.Writer, kdir string) []*cobra.Command { }(handler) // install the implementation into the command cmd.RunE = func(*cobra.Command, []string) error { return renewalFunc() } + cmd.Args = cobra.NoArgs cmdList = append(cmdList, cmd) } diff --git a/cmd/kubeadm/app/cmd/certs_test.go b/cmd/kubeadm/app/cmd/certs_test.go index 8efa58c7456..201c9a66053 100644 --- a/cmd/kubeadm/app/cmd/certs_test.go +++ b/cmd/kubeadm/app/cmd/certs_test.go @@ -143,6 +143,8 @@ func TestRunRenewCommands(t *testing.T) { command string Certs []*certsphase.KubeadmCert KubeconfigFiles []string + Args string + expectedError bool }{ { command: "all", @@ -221,6 +223,14 @@ func TestRunRenewCommands(t *testing.T) { kubeadmconstants.ControllerManagerKubeConfigFileName, }, }, + { + command: "apiserver", + Certs: []*certsphase.KubeadmCert{ + certsphase.KubeadmCertAPIServer(), + }, + Args: "args", + expectedError: true, + }, } for _, test := range tests { @@ -244,25 +254,34 @@ func TestRunRenewCommands(t *testing.T) { // exec renew renewCmds := getRenewSubCommands(os.Stdout, tmpDir) - cmdtestutil.RunSubCommand(t, renewCmds, test.command, fmt.Sprintf("--cert-dir=%s", tmpDir)) - - // check the file is modified - for _, cert := range test.Certs { - file, err := os.Stat(filepath.Join(tmpDir, fmt.Sprintf("%s.crt", cert.BaseName))) - if err != nil { - t.Fatalf("couldn't get certificate %s: %v", cert.Name, err) - } - if ModTime[cert.Name] == file.ModTime() { - t.Errorf("certificate %s was not renewed as expected", cert.Name) - } + args := fmt.Sprintf("--cert-dir=%s", tmpDir) + if len(test.Args) > 0 { + args = test.Args + " " + args } - for _, kubeConfig := range test.KubeconfigFiles { - file, err := os.Stat(filepath.Join(tmpDir, kubeConfig)) - if err != nil { - t.Fatalf("couldn't get kubeconfig %s: %v", kubeConfig, err) + err := cmdtestutil.RunSubCommand(t, renewCmds, test.command, args) + // certs renew doesn't support positional Args + if (err != nil) != test.expectedError { + t.Errorf("failed to run renew commands, expected error: %t, actual error: %v", test.expectedError, err) + } + if !test.expectedError { + // check the file is modified + for _, cert := range test.Certs { + file, err := os.Stat(filepath.Join(tmpDir, fmt.Sprintf("%s.crt", cert.BaseName))) + if err != nil { + t.Fatalf("couldn't get certificate %s: %v", cert.Name, err) + } + if ModTime[cert.Name] == file.ModTime() { + t.Errorf("certificate %s was not renewed as expected", cert.Name) + } } - if ModTime[kubeConfig] == file.ModTime() { - t.Errorf("kubeconfig %s was not renewed as expected", kubeConfig) + for _, kubeConfig := range test.KubeconfigFiles { + file, err := os.Stat(filepath.Join(tmpDir, kubeConfig)) + if err != nil { + t.Fatalf("couldn't get kubeconfig %s: %v", kubeConfig, err) + } + if ModTime[kubeConfig] == file.ModTime() { + t.Errorf("kubeconfig %s was not renewed as expected", kubeConfig) + } } } }) diff --git a/cmd/kubeadm/test/cmd/util.go b/cmd/kubeadm/test/cmd/util.go index 9cb48dc7faa..d48e82d1646 100644 --- a/cmd/kubeadm/test/cmd/util.go +++ b/cmd/kubeadm/test/cmd/util.go @@ -50,12 +50,13 @@ func RunCmd(command string, args ...string) (string, string, int, error) { } // RunSubCommand is a utility function for kubeadm testing that executes a Cobra sub command -func RunSubCommand(t *testing.T, subCmds []*cobra.Command, command string, args ...string) { +func RunSubCommand(t *testing.T, subCmds []*cobra.Command, command string, args ...string) error { subCmd := getSubCommand(t, subCmds, command) subCmd.SetArgs(args) if err := subCmd.Execute(); err != nil { - t.Fatalf("Could not execute subcommand: %s", command) + return err } + return nil } func getSubCommand(t *testing.T, subCmds []*cobra.Command, name string) *cobra.Command {