mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-04 09:49:50 +00:00
Merge pull request #122875 from my-git9/readwriterut
kubeadm: increase ut coverage certs/renewal/readwriter
This commit is contained in:
commit
918f7c28af
@ -19,6 +19,7 @@ package renewal
|
|||||||
import (
|
import (
|
||||||
"crypto"
|
"crypto"
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -199,3 +200,147 @@ func writeTestKubeconfig(t *testing.T, dir, name string, caCert *x509.Certificat
|
|||||||
|
|
||||||
return cert
|
return cert
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFileExists(t *testing.T) {
|
||||||
|
tmpdir, err := os.MkdirTemp("", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Couldn't create tmpdir: %v", err)
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
err = os.RemoveAll(tmpdir)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Fail to remove tmpdir: %v", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
tmpfile, err := os.CreateTemp(tmpdir, "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Couldn't create tmpfile: %v", err)
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
filename string
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "file exist",
|
||||||
|
filename: tmpfile.Name(),
|
||||||
|
want: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "file does not exist",
|
||||||
|
filename: "foo",
|
||||||
|
want: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "file path is a dir",
|
||||||
|
filename: tmpdir,
|
||||||
|
want: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if got := fileExists(tt.filename); got != tt.want {
|
||||||
|
t.Errorf("fileExists() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPKICertificateReadWriterExists(t *testing.T) {
|
||||||
|
tmpdir, err := os.MkdirTemp("", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Couldn't create tmpdir: %v", err)
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
err = os.RemoveAll(tmpdir)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Fail to remove tmpdir: %v", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
filename := "testfile"
|
||||||
|
tmpfilepath := filepath.Join(tmpdir, fmt.Sprintf(filename+".crt"))
|
||||||
|
err = os.WriteFile(tmpfilepath, nil, 0644)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Couldn't write file: %v", err)
|
||||||
|
}
|
||||||
|
type fields struct {
|
||||||
|
baseName string
|
||||||
|
certificateDir string
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
fields fields
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "cert file exists",
|
||||||
|
fields: fields{
|
||||||
|
baseName: filename,
|
||||||
|
certificateDir: tmpdir,
|
||||||
|
},
|
||||||
|
want: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "cert file does not exist",
|
||||||
|
fields: fields{
|
||||||
|
baseName: "foo",
|
||||||
|
certificateDir: tmpdir,
|
||||||
|
},
|
||||||
|
want: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
rw := &pkiCertificateReadWriter{
|
||||||
|
baseName: tt.fields.baseName,
|
||||||
|
certificateDir: tt.fields.certificateDir,
|
||||||
|
}
|
||||||
|
if got := rw.Exists(); got != tt.want {
|
||||||
|
t.Errorf("pkiCertificateReadWriter.Exists() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestKubeConfigReadWriterExists(t *testing.T) {
|
||||||
|
tmpdir, err := os.MkdirTemp("", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Couldn't create tmpdir: %v", err)
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
err = os.RemoveAll(tmpdir)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Fail to remove tmpdir: %v", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
tmpfile, err := os.CreateTemp(tmpdir, "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Couldn't create tmpfile: %v", err)
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
kubeConfigFilePath string
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "file exists",
|
||||||
|
kubeConfigFilePath: tmpfile.Name(),
|
||||||
|
want: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "file does not exist",
|
||||||
|
kubeConfigFilePath: "foo",
|
||||||
|
want: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
rw := &kubeConfigReadWriter{
|
||||||
|
kubeConfigFilePath: tt.kubeConfigFilePath,
|
||||||
|
}
|
||||||
|
if got := rw.Exists(); got != tt.want {
|
||||||
|
t.Errorf("kubeConfigReadWriter.Exists() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user