Store pk files in tmp dir for clean up, clean up CR resources when tests fail

Signed-off-by: Haoxiang Zhou <haoxiang.zhou@jetstack.io>
This commit is contained in:
Haoxiang Zhou 2020-06-11 16:26:09 +01:00
parent bb9d95052c
commit 92c8d9c9dc
2 changed files with 25 additions and 14 deletions

View File

@ -5,16 +5,13 @@ go_test(
srcs = [
"ctl_convert_test.go",
"ctl_create_cr_test.go",
"ctl_fetch_test.go",
"ctl_renew_test.go",
],
data = glob(["testdata/**"]),
deps = [
"//cmd/ctl/pkg/convert:go_default_library",
"//cmd/ctl/pkg/create/certificaterequest:go_default_library",
"//cmd/ctl/pkg/fetch:go_default_library",
"//cmd/ctl/pkg/renew:go_default_library",
"//cmd/ctl/pkg/util:go_default_library",
"//pkg/api/util:go_default_library",
"//pkg/apis/certmanager/v1alpha2:go_default_library",
"//pkg/apis/meta/v1:go_default_library",

View File

@ -42,6 +42,14 @@ func TestCtlCreateCR(t *testing.T) {
// Build clients
kubeClient, _, cmCl, _ := framework.NewClients(t, config)
// Create tmp directory and cd into it to store private key files
if err := os.Mkdir("tmp", 0777); err != nil {
t.Fatal(err)
}
if err := os.Chdir("tmp"); err != nil {
t.Fatal(err)
}
var (
cr1Name = "testcr-1"
cr2Name = "testcr-2"
@ -49,6 +57,8 @@ func TestCtlCreateCR(t *testing.T) {
cr4Name = "testcr-4"
ns1 = "testns-1"
ns2 = "testns-2"
testdataPath = "../testdata/"
)
// Create Namespaces
@ -70,7 +80,7 @@ func TestCtlCreateCR(t *testing.T) {
expKeyFilename string
}{
"v1alpha2 Certificate given": {
inputFile: "./testdata/create_cr_cert_with_ns1.yaml",
inputFile: testdataPath + "create_cr_cert_with_ns1.yaml",
inputArgs: []string{cr1Name},
inputNamespace: ns1,
keyFilename: "",
@ -80,7 +90,7 @@ func TestCtlCreateCR(t *testing.T) {
expKeyFilename: cr1Name + ".key",
},
"v1alpha3 Certificate given": {
inputFile: "./testdata/create_cr_v1alpha3_cert_with_ns1.yaml",
inputFile: testdataPath + "create_cr_v1alpha3_cert_with_ns1.yaml",
inputArgs: []string{cr2Name},
inputNamespace: ns1,
keyFilename: "",
@ -90,7 +100,7 @@ func TestCtlCreateCR(t *testing.T) {
expKeyFilename: cr2Name + ".key",
},
"conflicting namespaces defined in flag and file": {
inputFile: "./testdata/create_cr_cert_with_ns1.yaml",
inputFile: testdataPath + "create_cr_cert_with_ns1.yaml",
inputArgs: []string{cr3Name},
inputNamespace: ns2,
keyFilename: "",
@ -100,7 +110,7 @@ func TestCtlCreateCR(t *testing.T) {
expKeyFilename: "",
},
"file passed in defines resource other than certificate": {
inputFile: "./testdata/create_cr_issuer.yaml",
inputFile: testdataPath + "create_cr_issuer.yaml",
inputArgs: []string{cr4Name},
inputNamespace: ns1,
keyFilename: "",
@ -110,7 +120,7 @@ func TestCtlCreateCR(t *testing.T) {
expKeyFilename: "",
},
"path to file to store private key provided": {
inputFile: "./testdata/create_cr_cert_with_ns1.yaml",
inputFile: testdataPath + "create_cr_cert_with_ns1.yaml",
inputArgs: []string{cr1Name},
inputNamespace: ns1,
keyFilename: "test.key",
@ -139,7 +149,6 @@ func TestCtlCreateCR(t *testing.T) {
opts.Filenames = []string{test.inputFile}
err := opts.Run(test.inputArgs)
defer cleanupFileIfExists(test.expName + ".key")
if err != nil {
if !test.expErr {
@ -162,20 +171,24 @@ func TestCtlCreateCR(t *testing.T) {
}
if gotCr.Namespace != test.expNamespace {
cmCl.CertmanagerV1alpha2().CertificateRequests(gotCr.Namespace).Delete(ctx, gotCr.Name, metav1.DeleteOptions{})
t.Errorf("CR created in unexpected Namespace, expected: %s, actual: %s", test.expNamespace, gotCr.Namespace)
}
if gotCr.Name != test.expName {
cmCl.CertmanagerV1alpha2().CertificateRequests(gotCr.Namespace).Delete(ctx, gotCr.Name, metav1.DeleteOptions{})
t.Errorf("CR created has unexpected Name, expectedL %s, actualL %s", test.expName, gotCr.Name)
}
// Check the file where the private key is stored
keyData, err := ioutil.ReadFile(test.expKeyFilename)
if err != nil {
cmCl.CertmanagerV1alpha2().CertificateRequests(gotCr.Namespace).Delete(ctx, gotCr.Name, metav1.DeleteOptions{})
t.Errorf("error when reading file storing private key: %v", err)
}
_, err = pki.DecodePrivateKeyBytes(keyData)
if err != nil {
cmCl.CertmanagerV1alpha2().CertificateRequests(gotCr.Namespace).Delete(ctx, gotCr.Name, metav1.DeleteOptions{})
t.Errorf("invalid private key: %v", err)
}
@ -187,11 +200,12 @@ func TestCtlCreateCR(t *testing.T) {
}
})
}
}
func cleanupFileIfExists(fileName string) {
_, err := os.Stat(fileName)
if err == nil {
err = os.Remove(fileName)
// Clean up tmp folder with private key files
if err := os.Chdir(".."); err != nil {
t.Fatal(err)
}
if err := os.RemoveAll("tmp"); err != nil {
t.Fatal(err)
}
}