diff --git a/cmd/kubeadm/app/phases/certs/pkiutil/pki_helpers_test.go b/cmd/kubeadm/app/phases/certs/pkiutil/pki_helpers_test.go index c73624b181c..08658bf77c0 100644 --- a/cmd/kubeadm/app/phases/certs/pkiutil/pki_helpers_test.go +++ b/cmd/kubeadm/app/phases/certs/pkiutil/pki_helpers_test.go @@ -92,7 +92,7 @@ func TestWriteCertAndKey(t *testing.T) { if err != nil { t.Fatalf("Couldn't create tmpdir") } - defer os.Remove(tmpdir) + defer os.RemoveAll(tmpdir) caKey, err := rsa.GenerateKey(rand.Reader, 2048) if err != nil { @@ -107,3 +107,101 @@ func TestWriteCertAndKey(t *testing.T) { ) } } + +func TestCertOrKeyExist(t *testing.T) { + tmpdir, err := ioutil.TempDir("", "") + if err != nil { + t.Fatalf("Couldn't create tmpdir") + } + defer os.RemoveAll(tmpdir) + + caKey, err := rsa.GenerateKey(rand.Reader, 2048) + if err != nil { + t.Fatalf("Couldn't create rsa Private Key") + } + caCert := &x509.Certificate{} + actual := WriteCertAndKey(tmpdir, "foo", caCert, caKey) + if actual != nil { + t.Errorf( + "failed WriteCertAndKey with an error: %v", + actual, + ) + } + + var tests = []struct { + path string + name string + expected bool + }{ + { + path: "", + name: "", + expected: false, + }, + { + path: tmpdir, + name: "foo", + expected: true, + }, + } + for _, rt := range tests { + actual := CertOrKeyExist(rt.path, rt.name) + if actual != rt.expected { + t.Errorf( + "failed CertOrKeyExist:\n\texpected: %t\n\t actual: %t", + rt.expected, + actual, + ) + } + } +} + +func TestTryLoadCertAndKeyFromDisk(t *testing.T) { + tmpdir, err := ioutil.TempDir("", "") + if err != nil { + t.Fatalf("Couldn't create tmpdir") + } + defer os.RemoveAll(tmpdir) + + caCert, caKey, err := NewCertificateAuthority() + if err != nil { + t.Errorf( + "failed to create cert and key with an error: %v", + err, + ) + } + err = WriteCertAndKey(tmpdir, "foo", caCert, caKey) + if err != nil { + t.Errorf( + "failed to write cert and key with an error: %v", + err, + ) + } + + var tests = []struct { + path string + name string + expected bool + }{ + { + path: "", + name: "", + expected: false, + }, + { + path: tmpdir, + name: "foo", + expected: true, + }, + } + for _, rt := range tests { + _, _, actual := TryLoadCertAndKeyFromDisk(rt.path, rt.name) + if (actual == nil) != rt.expected { + t.Errorf( + "failed TryLoadCertAndKeyFromDisk:\n\texpected: %t\n\t actual: %t", + rt.expected, + (actual == nil), + ) + } + } +}