reduce util pkg for testing in tempdir.

Kubernetes-commit: aaa30439bdda1589eacc7be8547c297a73e346b9
This commit is contained in:
charles-chenzz 2023-04-18 11:18:10 +08:00 committed by Kubernetes Publisher
parent cf356053e4
commit 0d2299217d
1 changed files with 3 additions and 15 deletions

View File

@ -17,7 +17,6 @@ limitations under the License.
package create package create
import ( import (
"fmt"
"os" "os"
"path" "path"
"testing" "testing"
@ -25,7 +24,6 @@ import (
corev1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
apiequality "k8s.io/apimachinery/pkg/api/equality" apiequality "k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utiltesting "k8s.io/client-go/util/testing"
) )
var rsaCertPEM = `-----BEGIN CERTIFICATE----- var rsaCertPEM = `-----BEGIN CERTIFICATE-----
@ -84,17 +82,14 @@ rLTt0TTazzwBCgCD0Jkoqg==
func TestCreateSecretTLS(t *testing.T) { func TestCreateSecretTLS(t *testing.T) {
validCertTmpDir := utiltesting.MkTmpdirOrDie("tls-valid-cert-test") validCertTmpDir := t.TempDir()
validKeyPath, validCertPath := writeKeyPair(validCertTmpDir, rsaKeyPEM, rsaCertPEM, t) validKeyPath, validCertPath := writeKeyPair(validCertTmpDir, rsaKeyPEM, rsaCertPEM, t)
defer tearDown(validCertTmpDir)
invalidCertTmpDir := utiltesting.MkTmpdirOrDie("tls-invalid-cert-test") invalidCertTmpDir := t.TempDir()
invalidKeyPath, invalidCertPath := writeKeyPair(invalidCertTmpDir, "test", "test", t) invalidKeyPath, invalidCertPath := writeKeyPair(invalidCertTmpDir, "test", "test", t)
defer tearDown(invalidCertTmpDir)
mismatchCertTmpDir := utiltesting.MkTmpdirOrDie("tls-mismatch-test") mismatchCertTmpDir := t.TempDir()
mismatchKeyPath, mismatchCertPath := writeKeyPair(mismatchCertTmpDir, rsaKeyPEM, mismatchRSAKeyPEM, t) mismatchKeyPath, mismatchCertPath := writeKeyPair(mismatchCertTmpDir, rsaKeyPEM, mismatchRSAKeyPEM, t)
defer tearDown(mismatchCertTmpDir)
tests := map[string]struct { tests := map[string]struct {
tlsSecretName string tlsSecretName string
@ -189,13 +184,6 @@ func TestCreateSecretTLS(t *testing.T) {
} }
} }
func tearDown(tmpDir string) {
err := os.RemoveAll(tmpDir)
if err != nil {
fmt.Printf("Error in cleaning up test: %v", err)
}
}
func write(path, contents string, t *testing.T) { func write(path, contents string, t *testing.T) {
f, err := os.Create(path) f, err := os.Create(path)
if err != nil { if err != nil {