From d23507f40d2142ebd4efa0ac029f72d73a9e30f8 Mon Sep 17 00:00:00 2001 From: Derek McQuay Date: Thu, 2 Feb 2017 14:03:35 -0800 Subject: [PATCH] kubeadm: added tests cert/pkiutil pkg raised coverage from ~37% to ~77% --- .../phases/certs/pkiutil/pki_helpers_test.go | 100 +++++++++++++++++- 1 file changed, 99 insertions(+), 1 deletion(-) 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), + ) + } + } +}