mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-12 05:21:58 +00:00
reduce util pkg for testing in tempdir.
This commit is contained in:
parent
53cccbe4f9
commit
aaa30439bd
@ -21,7 +21,6 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
@ -47,8 +46,6 @@ import (
|
||||
"k8s.io/client-go/rest/fake"
|
||||
restclientwatch "k8s.io/client-go/rest/watch"
|
||||
"k8s.io/client-go/restmapper"
|
||||
utiltesting "k8s.io/client-go/util/testing"
|
||||
|
||||
// TODO we need to remove this linkage and create our own scheme
|
||||
v1 "k8s.io/api/core/v1"
|
||||
"k8s.io/client-go/kubernetes/scheme"
|
||||
@ -60,7 +57,7 @@ var (
|
||||
)
|
||||
|
||||
func stringBody(body string) io.ReadCloser {
|
||||
return ioutil.NopCloser(bytes.NewReader([]byte(body)))
|
||||
return io.NopCloser(bytes.NewReader([]byte(body)))
|
||||
}
|
||||
|
||||
func watchBody(events ...watch.Event) string {
|
||||
@ -425,7 +422,7 @@ func createTestDir(t *testing.T, path string) {
|
||||
}
|
||||
|
||||
func writeTestFile(t *testing.T, path string, contents string) {
|
||||
if err := ioutil.WriteFile(path, []byte(contents), 0644); err != nil {
|
||||
if err := os.WriteFile(path, []byte(contents), 0644); err != nil {
|
||||
t.Fatalf("error creating test file %#v", err)
|
||||
}
|
||||
}
|
||||
@ -488,10 +485,11 @@ func TestFilenameOptionsValidate(t *testing.T) {
|
||||
|
||||
func TestPathBuilderWithMultiple(t *testing.T) {
|
||||
// create test dirs
|
||||
tmpDir, err := utiltesting.MkTmpdir("recursive_test_multiple")
|
||||
tmpDir, err := os.MkdirTemp("", "recursive_test_multiple")
|
||||
if err != nil {
|
||||
t.Fatalf("error creating temp dir: %v", err)
|
||||
}
|
||||
|
||||
createTestDir(t, fmt.Sprintf("%s/%s", tmpDir, "recursive/pod/pod_1"))
|
||||
createTestDir(t, fmt.Sprintf("%s/%s", tmpDir, "recursive/rc/rc_1"))
|
||||
createTestDir(t, fmt.Sprintf("%s/%s", tmpDir, "inode/hardlink"))
|
||||
@ -556,10 +554,11 @@ func TestPathBuilderWithMultiple(t *testing.T) {
|
||||
|
||||
func TestPathBuilderWithMultipleInvalid(t *testing.T) {
|
||||
// create test dirs
|
||||
tmpDir, err := utiltesting.MkTmpdir("recursive_test_multiple_invalid")
|
||||
tmpDir, err := os.MkdirTemp("", "recursive_test_multiple_invalid")
|
||||
if err != nil {
|
||||
t.Fatalf("error creating temp dir: %v", err)
|
||||
}
|
||||
|
||||
createTestDir(t, fmt.Sprintf("%s/%s", tmpDir, "inode/symlink/pod"))
|
||||
defer os.RemoveAll(tmpDir)
|
||||
|
||||
@ -704,7 +703,7 @@ func TestErrorFilePatternBuilder(t *testing.T) {
|
||||
}
|
||||
|
||||
func setupKustomizeDirectory() (string, error) {
|
||||
path, err := ioutil.TempDir("/tmp", "")
|
||||
path, err := os.MkdirTemp("", "")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@ -775,7 +774,7 @@ resources:
|
||||
}
|
||||
|
||||
for filename, content := range contents {
|
||||
err = ioutil.WriteFile(filepath.Join(path, filename), []byte(content), 0660)
|
||||
err = os.WriteFile(filepath.Join(path, filename), []byte(content), 0660)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@ -1892,7 +1891,7 @@ func TestHasNames(t *testing.T) {
|
||||
|
||||
func TestUnstructured(t *testing.T) {
|
||||
// create test dirs
|
||||
tmpDir, err := utiltesting.MkTmpdir("unstructured_test")
|
||||
tmpDir, err := os.MkdirTemp(os.TempDir(), "unstructured_test")
|
||||
if err != nil {
|
||||
t.Fatalf("error creating temp dir: %v", err)
|
||||
}
|
||||
|
@ -17,7 +17,6 @@ limitations under the License.
|
||||
package create
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"testing"
|
||||
@ -25,7 +24,6 @@ import (
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
apiequality "k8s.io/apimachinery/pkg/api/equality"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
utiltesting "k8s.io/client-go/util/testing"
|
||||
)
|
||||
|
||||
var rsaCertPEM = `-----BEGIN CERTIFICATE-----
|
||||
@ -84,17 +82,14 @@ rLTt0TTazzwBCgCD0Jkoqg==
|
||||
|
||||
func TestCreateSecretTLS(t *testing.T) {
|
||||
|
||||
validCertTmpDir := utiltesting.MkTmpdirOrDie("tls-valid-cert-test")
|
||||
validCertTmpDir := t.TempDir()
|
||||
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)
|
||||
defer tearDown(invalidCertTmpDir)
|
||||
|
||||
mismatchCertTmpDir := utiltesting.MkTmpdirOrDie("tls-mismatch-test")
|
||||
mismatchCertTmpDir := t.TempDir()
|
||||
mismatchKeyPath, mismatchCertPath := writeKeyPair(mismatchCertTmpDir, rsaKeyPEM, mismatchRSAKeyPEM, t)
|
||||
defer tearDown(mismatchCertTmpDir)
|
||||
|
||||
tests := map[string]struct {
|
||||
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) {
|
||||
f, err := os.Create(path)
|
||||
if err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user