mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 01:06:27 +00:00
kubeadm: added tests cert/pkiutil pkg
raised coverage from ~37% to ~77%
This commit is contained in:
parent
9e427c88c4
commit
d23507f40d
@ -92,7 +92,7 @@ func TestWriteCertAndKey(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Couldn't create tmpdir")
|
t.Fatalf("Couldn't create tmpdir")
|
||||||
}
|
}
|
||||||
defer os.Remove(tmpdir)
|
defer os.RemoveAll(tmpdir)
|
||||||
|
|
||||||
caKey, err := rsa.GenerateKey(rand.Reader, 2048)
|
caKey, err := rsa.GenerateKey(rand.Reader, 2048)
|
||||||
if err != nil {
|
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),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user