From 696d537239bcfb8fb1a95f055346d0423035f662 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?10284789=E5=88=81=E6=B5=A9?= Date: Tue, 14 Jun 2022 07:48:43 +0000 Subject: [PATCH] Incomplete coverage of test scenarios and bad code --- cmd/kubeadm/app/util/pkiutil/pki_helpers.go | 7 +----- .../app/util/pkiutil/pki_helpers_test.go | 22 ++++++++++++++----- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/cmd/kubeadm/app/util/pkiutil/pki_helpers.go b/cmd/kubeadm/app/util/pkiutil/pki_helpers.go index dac477429c0..d416a8b0434 100644 --- a/cmd/kubeadm/app/util/pkiutil/pki_helpers.go +++ b/cmd/kubeadm/app/util/pkiutil/pki_helpers.go @@ -247,13 +247,8 @@ func CertOrKeyExist(pkiPath, name string) bool { _, certErr := os.Stat(certificatePath) _, keyErr := os.Stat(privateKeyPath) - if os.IsNotExist(certErr) && os.IsNotExist(keyErr) { - // The cert and the key do not exist - return false - } - // Both files exist or one of them - return true + return !(os.IsNotExist(certErr) && os.IsNotExist(keyErr)) } // CSROrKeyExist returns true if one of the CSR or key exists diff --git a/cmd/kubeadm/app/util/pkiutil/pki_helpers_test.go b/cmd/kubeadm/app/util/pkiutil/pki_helpers_test.go index 9e5acc713f0..8eb8a8310c1 100644 --- a/cmd/kubeadm/app/util/pkiutil/pki_helpers_test.go +++ b/cmd/kubeadm/app/util/pkiutil/pki_helpers_test.go @@ -262,12 +262,16 @@ func TestCertOrKeyExist(t *testing.T) { } defer os.RemoveAll(tmpdir) - caCert := &x509.Certificate{} - actual := WriteCertAndKey(tmpdir, "foo", caCert, rootCAKey) - if actual != nil { + if err = WriteCertAndKey(tmpdir, "foo-0", rootCACert, rootCAKey); err != nil { t.Errorf( "failed WriteCertAndKey with an error: %v", - actual, + err, + ) + } + if err = WriteCert(tmpdir, "foo-1", rootCACert); err != nil { + t.Errorf( + "failed WriteCert with an error: %v", + err, ) } @@ -284,9 +288,15 @@ func TestCertOrKeyExist(t *testing.T) { expected: false, }, { - desc: "valid path and name", + desc: "valid path and name, both cert and key exist", path: tmpdir, - name: "foo", + name: "foo-0", + expected: true, + }, + { + desc: "valid path and name, only cert exist", + path: tmpdir, + name: "foo-1", expected: true, }, }