Merge pull request #101559 from njuptlzf/fsstore_test
Clean up tempDir after fsstore_test.go is executed
This commit is contained in:
		| @@ -18,7 +18,6 @@ package store | |||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"io/ioutil" |  | ||||||
| 	"path/filepath" | 	"path/filepath" | ||||||
| 	"reflect" | 	"reflect" | ||||||
| 	"testing" | 	"testing" | ||||||
| @@ -39,38 +38,33 @@ import ( | |||||||
| 	utilfs "k8s.io/kubernetes/pkg/util/filesystem" | 	utilfs "k8s.io/kubernetes/pkg/util/filesystem" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| var testdir string |  | ||||||
|  |  | ||||||
| func init() { |  | ||||||
| 	tmp, err := ioutil.TempDir("", "fsstore-test") |  | ||||||
| 	if err != nil { |  | ||||||
| 		panic(err) |  | ||||||
| 	} |  | ||||||
| 	testdir = tmp |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func newInitializedFakeFsStore() (*fsStore, error) { | func newInitializedFakeFsStore() (*fsStore, error) { | ||||||
| 	// Test with the default filesystem, the fake filesystem has an issue caused by afero: https://github.com/spf13/afero/issues/141 | 	// Test with the default filesystem, the fake filesystem has an issue caused by afero: https://github.com/spf13/afero/issues/141 | ||||||
| 	// The default filesystem also behaves more like production, so we should probably not mock the filesystem for unit tests. | 	// The default filesystem also behaves more like production, so we should probably not mock the filesystem for unit tests. | ||||||
| 	fs := utilfs.DefaultFs{} | 	fs := utilfs.DefaultFs{} | ||||||
|  |  | ||||||
| 	tmpdir, err := fs.TempDir(testdir, "store-") | 	tmpDir, err := fs.TempDir("", "fsstore-test-") | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	store := NewFsStore(fs, tmpdir) | 	store := NewFsStore(fs, tmpDir) | ||||||
| 	if err := store.Initialize(); err != nil { | 	if err := store.Initialize(); err != nil { | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
| 	return store.(*fsStore), nil | 	return store.(*fsStore), nil | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func cleanupFakeFsStore(store *fsStore) { | ||||||
|  | 	_ = store.fs.RemoveAll(store.dir) | ||||||
|  | } | ||||||
|  |  | ||||||
| func TestFsStoreInitialize(t *testing.T) { | func TestFsStoreInitialize(t *testing.T) { | ||||||
| 	store, err := newInitializedFakeFsStore() | 	store, err := newInitializedFakeFsStore() | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		t.Fatalf("fsStore.Initialize() failed with error: %v", err) | 		t.Fatalf("fsStore.Initialize() failed with error: %v", err) | ||||||
| 	} | 	} | ||||||
|  | 	defer cleanupFakeFsStore(store) | ||||||
|  |  | ||||||
| 	// check that store.dir exists | 	// check that store.dir exists | ||||||
| 	if _, err := store.fs.Stat(store.dir); err != nil { | 	if _, err := store.fs.Stat(store.dir); err != nil { | ||||||
| @@ -103,6 +97,7 @@ func TestFsStoreExists(t *testing.T) { | |||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		t.Fatalf("error constructing store: %v", err) | 		t.Fatalf("error constructing store: %v", err) | ||||||
| 	} | 	} | ||||||
|  | 	defer cleanupFakeFsStore(store) | ||||||
|  |  | ||||||
| 	// checkpoint a payload | 	// checkpoint a payload | ||||||
| 	const ( | 	const ( | ||||||
| @@ -160,6 +155,7 @@ func TestFsStoreSave(t *testing.T) { | |||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		t.Fatalf("error constructing store: %v", err) | 		t.Fatalf("error constructing store: %v", err) | ||||||
| 	} | 	} | ||||||
|  | 	defer cleanupFakeFsStore(store) | ||||||
|  |  | ||||||
| 	nameTooLong := func() string { | 	nameTooLong := func() string { | ||||||
| 		s := "" | 		s := "" | ||||||
| @@ -224,6 +220,8 @@ func TestFsStoreLoad(t *testing.T) { | |||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		t.Fatalf("error constructing store: %v", err) | 		t.Fatalf("error constructing store: %v", err) | ||||||
| 	} | 	} | ||||||
|  | 	defer cleanupFakeFsStore(store) | ||||||
|  |  | ||||||
| 	// encode a kubelet configuration that has all defaults set | 	// encode a kubelet configuration that has all defaults set | ||||||
| 	expect, err := newKubeletConfiguration() | 	expect, err := newKubeletConfiguration() | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| @@ -295,6 +293,7 @@ func TestFsStoreAssignedModified(t *testing.T) { | |||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		t.Fatalf("error constructing store: %v", err) | 		t.Fatalf("error constructing store: %v", err) | ||||||
| 	} | 	} | ||||||
|  | 	defer cleanupFakeFsStore(store) | ||||||
|  |  | ||||||
| 	// create an empty assigned file, this is good enough for testing | 	// create an empty assigned file, this is good enough for testing | ||||||
| 	saveTestSourceFile(t, store, assignedFile, nil) | 	saveTestSourceFile(t, store, assignedFile, nil) | ||||||
| @@ -321,6 +320,7 @@ func TestFsStoreAssigned(t *testing.T) { | |||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		t.Fatalf("error constructing store: %v", err) | 		t.Fatalf("error constructing store: %v", err) | ||||||
| 	} | 	} | ||||||
|  | 	defer cleanupFakeFsStore(store) | ||||||
|  |  | ||||||
| 	source, _, err := checkpoint.NewRemoteConfigSource(&apiv1.NodeConfigSource{ | 	source, _, err := checkpoint.NewRemoteConfigSource(&apiv1.NodeConfigSource{ | ||||||
| 		ConfigMap: &apiv1.ConfigMapNodeConfigSource{ | 		ConfigMap: &apiv1.ConfigMapNodeConfigSource{ | ||||||
| @@ -364,6 +364,7 @@ func TestFsStoreLastKnownGood(t *testing.T) { | |||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		t.Fatalf("error constructing store: %v", err) | 		t.Fatalf("error constructing store: %v", err) | ||||||
| 	} | 	} | ||||||
|  | 	defer cleanupFakeFsStore(store) | ||||||
|  |  | ||||||
| 	source, _, err := checkpoint.NewRemoteConfigSource(&apiv1.NodeConfigSource{ | 	source, _, err := checkpoint.NewRemoteConfigSource(&apiv1.NodeConfigSource{ | ||||||
| 		ConfigMap: &apiv1.ConfigMapNodeConfigSource{ | 		ConfigMap: &apiv1.ConfigMapNodeConfigSource{ | ||||||
| @@ -407,6 +408,7 @@ func TestFsStoreSetAssigned(t *testing.T) { | |||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		t.Fatalf("error constructing store: %v", err) | 		t.Fatalf("error constructing store: %v", err) | ||||||
| 	} | 	} | ||||||
|  | 	defer cleanupFakeFsStore(store) | ||||||
|  |  | ||||||
| 	cases := []struct { | 	cases := []struct { | ||||||
| 		desc   string | 		desc   string | ||||||
| @@ -490,6 +492,7 @@ func TestFsStoreSetLastKnownGood(t *testing.T) { | |||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		t.Fatalf("error constructing store: %v", err) | 		t.Fatalf("error constructing store: %v", err) | ||||||
| 	} | 	} | ||||||
|  | 	defer cleanupFakeFsStore(store) | ||||||
|  |  | ||||||
| 	cases := []struct { | 	cases := []struct { | ||||||
| 		desc   string | 		desc   string | ||||||
| @@ -573,6 +576,7 @@ func TestFsStoreReset(t *testing.T) { | |||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		t.Fatalf("error constructing store: %v", err) | 		t.Fatalf("error constructing store: %v", err) | ||||||
| 	} | 	} | ||||||
|  | 	defer cleanupFakeFsStore(store) | ||||||
|  |  | ||||||
| 	source, _, err := checkpoint.NewRemoteConfigSource(&apiv1.NodeConfigSource{ConfigMap: &apiv1.ConfigMapNodeConfigSource{ | 	source, _, err := checkpoint.NewRemoteConfigSource(&apiv1.NodeConfigSource{ConfigMap: &apiv1.ConfigMapNodeConfigSource{ | ||||||
| 		Name:             "name", | 		Name:             "name", | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Kubernetes Prow Robot
					Kubernetes Prow Robot