mirror of
https://github.com/kubernetes/client-go.git
synced 2025-09-04 08:35:10 +00:00
fix fd leaks and failed file removing for pkg client-go
Kubernetes-commit: 73aeed8766c2c42a6cb4fc8632b1b974f4508dde
This commit is contained in:
committed by
Kubernetes Publisher
parent
a9b2f9e9eb
commit
84dc0417b2
@@ -27,6 +27,8 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
utiltesting "k8s.io/client-go/util/testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"sigs.k8s.io/yaml"
|
||||
|
||||
@@ -197,7 +199,7 @@ func TestNoWarningMissingFiles(t *testing.T) {
|
||||
|
||||
func TestErrorReadingFile(t *testing.T) {
|
||||
commandLineFile, _ := os.CreateTemp("", "")
|
||||
defer os.Remove(commandLineFile.Name())
|
||||
defer utiltesting.CloseAndRemove(t, commandLineFile)
|
||||
|
||||
if err := os.WriteFile(commandLineFile.Name(), []byte("bogus value"), 0644); err != nil {
|
||||
t.Fatalf("Error creating tempfile: %v", err)
|
||||
@@ -238,9 +240,8 @@ func TestErrorReadingNonFile(t *testing.T) {
|
||||
|
||||
func TestConflictingCurrentContext(t *testing.T) {
|
||||
commandLineFile, _ := os.CreateTemp("", "")
|
||||
defer os.Remove(commandLineFile.Name())
|
||||
envVarFile, _ := os.CreateTemp("", "")
|
||||
defer os.Remove(envVarFile.Name())
|
||||
defer utiltesting.CloseAndRemove(t, commandLineFile, envVarFile)
|
||||
|
||||
mockCommandLineConfig := clientcmdapi.Config{
|
||||
CurrentContext: "any-context-value",
|
||||
@@ -319,7 +320,7 @@ users: null
|
||||
|
||||
func TestLoadingEmptyMaps(t *testing.T) {
|
||||
configFile, _ := os.CreateTemp("", "")
|
||||
defer os.Remove(configFile.Name())
|
||||
defer utiltesting.CloseAndRemove(t, configFile)
|
||||
|
||||
mockConfig := clientcmdapi.Config{
|
||||
CurrentContext: "any-context-value",
|
||||
@@ -345,7 +346,7 @@ func TestLoadingEmptyMaps(t *testing.T) {
|
||||
|
||||
func TestDuplicateClusterName(t *testing.T) {
|
||||
configFile, _ := os.CreateTemp("", "")
|
||||
defer os.Remove(configFile.Name())
|
||||
defer utiltesting.CloseAndRemove(t, configFile)
|
||||
|
||||
err := os.WriteFile(configFile.Name(), []byte(`
|
||||
kind: Config
|
||||
@@ -387,7 +388,7 @@ users:
|
||||
|
||||
func TestDuplicateContextName(t *testing.T) {
|
||||
configFile, _ := os.CreateTemp("", "")
|
||||
defer os.Remove(configFile.Name())
|
||||
defer utiltesting.CloseAndRemove(t, configFile)
|
||||
|
||||
err := os.WriteFile(configFile.Name(), []byte(`
|
||||
kind: Config
|
||||
@@ -429,7 +430,7 @@ users:
|
||||
|
||||
func TestDuplicateUserName(t *testing.T) {
|
||||
configFile, _ := os.CreateTemp("", "")
|
||||
defer os.Remove(configFile.Name())
|
||||
defer utiltesting.CloseAndRemove(t, configFile)
|
||||
|
||||
err := os.WriteFile(configFile.Name(), []byte(`
|
||||
kind: Config
|
||||
@@ -469,7 +470,7 @@ users:
|
||||
|
||||
func TestDuplicateExtensionName(t *testing.T) {
|
||||
configFile, _ := os.CreateTemp("", "")
|
||||
defer os.Remove(configFile.Name())
|
||||
defer utiltesting.CloseAndRemove(t, configFile)
|
||||
|
||||
err := os.WriteFile(configFile.Name(), []byte(`
|
||||
kind: Config
|
||||
@@ -625,7 +626,7 @@ func TestResolveRelativePaths(t *testing.T) {
|
||||
|
||||
func TestMigratingFile(t *testing.T) {
|
||||
sourceFile, _ := os.CreateTemp("", "")
|
||||
defer os.Remove(sourceFile.Name())
|
||||
defer utiltesting.CloseAndRemove(t, sourceFile)
|
||||
destinationFile, _ := os.CreateTemp("", "")
|
||||
// delete the file so that we'll write to it
|
||||
os.Remove(destinationFile.Name())
|
||||
@@ -639,9 +640,8 @@ func TestMigratingFile(t *testing.T) {
|
||||
if _, err := loadingRules.Load(); err != nil {
|
||||
t.Errorf("unexpected error %v", err)
|
||||
}
|
||||
|
||||
// the load should have recreated this file
|
||||
defer os.Remove(destinationFile.Name())
|
||||
defer utiltesting.CloseAndRemove(t, destinationFile)
|
||||
|
||||
sourceContent, err := os.ReadFile(sourceFile.Name())
|
||||
if err != nil {
|
||||
@@ -659,9 +659,8 @@ func TestMigratingFile(t *testing.T) {
|
||||
|
||||
func TestMigratingFileLeaveExistingFileAlone(t *testing.T) {
|
||||
sourceFile, _ := os.CreateTemp("", "")
|
||||
defer os.Remove(sourceFile.Name())
|
||||
destinationFile, _ := os.CreateTemp("", "")
|
||||
defer os.Remove(destinationFile.Name())
|
||||
defer utiltesting.CloseAndRemove(t, sourceFile, destinationFile)
|
||||
|
||||
WriteToFile(testConfigAlfa, sourceFile.Name())
|
||||
|
||||
@@ -687,7 +686,7 @@ func TestMigratingFileSourceMissingSkip(t *testing.T) {
|
||||
sourceFilename := "some-missing-file"
|
||||
destinationFile, _ := os.CreateTemp("", "")
|
||||
// delete the file so that we'll write to it
|
||||
os.Remove(destinationFile.Name())
|
||||
utiltesting.CloseAndRemove(t, destinationFile)
|
||||
|
||||
loadingRules := ClientConfigLoadingRules{
|
||||
MigrationRules: map[string]string{destinationFile.Name(): sourceFilename},
|
||||
@@ -704,7 +703,7 @@ func TestMigratingFileSourceMissingSkip(t *testing.T) {
|
||||
|
||||
func TestFileLocking(t *testing.T) {
|
||||
f, _ := os.CreateTemp("", "")
|
||||
defer os.Remove(f.Name())
|
||||
defer utiltesting.CloseAndRemove(t, f)
|
||||
|
||||
err := lockFile(f.Name())
|
||||
if err != nil {
|
||||
@@ -720,9 +719,8 @@ func TestFileLocking(t *testing.T) {
|
||||
|
||||
func Example_noMergingOnExplicitPaths() {
|
||||
commandLineFile, _ := os.CreateTemp("", "")
|
||||
defer os.Remove(commandLineFile.Name())
|
||||
envVarFile, _ := os.CreateTemp("", "")
|
||||
defer os.Remove(envVarFile.Name())
|
||||
defer utiltesting.CloseAndRemove(&testing.T{}, commandLineFile, envVarFile)
|
||||
|
||||
WriteToFile(testConfigAlfa, commandLineFile.Name())
|
||||
WriteToFile(testConfigConflictAlfa, envVarFile.Name())
|
||||
@@ -769,9 +767,8 @@ func Example_noMergingOnExplicitPaths() {
|
||||
|
||||
func Example_mergingSomeWithConflict() {
|
||||
commandLineFile, _ := os.CreateTemp("", "")
|
||||
defer os.Remove(commandLineFile.Name())
|
||||
envVarFile, _ := os.CreateTemp("", "")
|
||||
defer os.Remove(envVarFile.Name())
|
||||
defer utiltesting.CloseAndRemove(&testing.T{}, commandLineFile, envVarFile)
|
||||
|
||||
WriteToFile(testConfigAlfa, commandLineFile.Name())
|
||||
WriteToFile(testConfigConflictAlfa, envVarFile.Name())
|
||||
@@ -825,13 +822,10 @@ func Example_mergingSomeWithConflict() {
|
||||
|
||||
func Example_mergingEverythingNoConflicts() {
|
||||
commandLineFile, _ := os.CreateTemp("", "")
|
||||
defer os.Remove(commandLineFile.Name())
|
||||
envVarFile, _ := os.CreateTemp("", "")
|
||||
defer os.Remove(envVarFile.Name())
|
||||
currentDirFile, _ := os.CreateTemp("", "")
|
||||
defer os.Remove(currentDirFile.Name())
|
||||
homeDirFile, _ := os.CreateTemp("", "")
|
||||
defer os.Remove(homeDirFile.Name())
|
||||
defer utiltesting.CloseAndRemove(&testing.T{}, commandLineFile, envVarFile, currentDirFile, homeDirFile)
|
||||
|
||||
WriteToFile(testConfigAlfa, commandLineFile.Name())
|
||||
WriteToFile(testConfigBravo, envVarFile.Name())
|
||||
|
Reference in New Issue
Block a user