Merge pull request #110712 from chendave/ignore_args

kubeadm: certs renew doesn't support positional args
This commit is contained in:
Kubernetes Prow Robot 2022-06-23 09:09:43 -07:00 committed by GitHub
commit 035f22d781
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 19 deletions

View File

@ -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)
}

View File

@ -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)
}
}
}
})

View File

@ -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 {