Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions pkg/operator/staticpod/prune/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"reflect"
"sort"
"testing"
"time"

"github.com/fvbommel/sortorder"
)
Expand Down Expand Up @@ -83,6 +84,60 @@ func TestRun(t *testing.T) {
}
}

func TestTmpClusterNameInCertificate(t *testing.T) {
testDir, err := os.MkdirTemp("", "prune-tmpclustername-test")
if err != nil {
t.Fatal(err)
}

t.Cleanup(func() {
_ = os.RemoveAll(testDir)
})

resourceDir := path.Join(testDir, "resources")
err = os.Mkdir(resourceDir, os.ModePerm)
if err != nil {
t.Error(err)
}

certDirRel := path.Join("etcd-secrets", "secrets", "etcd-all-certs")
certDirAbs := path.Join(resourceDir, certDirRel)
err = os.MkdirAll(certDirAbs, os.ModePerm)
if err != nil {
t.Error(err)
}

// this is a file we should not delete, but it has .tmp in its name which used to be a problem
peerCert, err := os.Create(path.Join(certDirAbs, "etcd-peer-master-2.tmp-1337.gg.mf.crt"))
if err != nil {
t.Error(err)
}
_ = peerCert.Close()

// set the file back by an hour to trigger the actual deletion logic
err = os.Chtimes(peerCert.Name(), time.Now().Add(-time.Hour), time.Now().Add(-time.Hour))
if err != nil {
t.Error(err)
}

// do NOT expect this file to be deleted, this is not a temporary left-over file
expected := []string{"etcd-peer-master-2.tmp-1337.gg.mf.crt"}
o := PruneOptions{
MaxEligibleRevision: 5,
ProtectedRevisions: []int{},
ResourceDir: resourceDir,
CertDir: certDirRel,
StaticPodName: "master-2.tmp-1337.gg.mf",
}

err = o.Run()
if err != nil {
t.Error(err)
}

checkPruned(t, certDirAbs, expected)
}

func checkPruned(t *testing.T, resourceDir string, expected []string) {
files, err := os.ReadDir(resourceDir)
if err != nil {
Expand Down