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 ( | ||||
| 	"fmt" | ||||
| 	"io/ioutil" | ||||
| 	"path/filepath" | ||||
| 	"reflect" | ||||
| 	"testing" | ||||
| @@ -39,38 +38,33 @@ import ( | ||||
| 	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) { | ||||
| 	// 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. | ||||
| 	fs := utilfs.DefaultFs{} | ||||
|  | ||||
| 	tmpdir, err := fs.TempDir(testdir, "store-") | ||||
| 	tmpDir, err := fs.TempDir("", "fsstore-test-") | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| 	store := NewFsStore(fs, tmpdir) | ||||
| 	store := NewFsStore(fs, tmpDir) | ||||
| 	if err := store.Initialize(); err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	return store.(*fsStore), nil | ||||
| } | ||||
|  | ||||
| func cleanupFakeFsStore(store *fsStore) { | ||||
| 	_ = store.fs.RemoveAll(store.dir) | ||||
| } | ||||
|  | ||||
| func TestFsStoreInitialize(t *testing.T) { | ||||
| 	store, err := newInitializedFakeFsStore() | ||||
| 	if err != nil { | ||||
| 		t.Fatalf("fsStore.Initialize() failed with error: %v", err) | ||||
| 	} | ||||
| 	defer cleanupFakeFsStore(store) | ||||
|  | ||||
| 	// check that store.dir exists | ||||
| 	if _, err := store.fs.Stat(store.dir); err != nil { | ||||
| @@ -103,6 +97,7 @@ func TestFsStoreExists(t *testing.T) { | ||||
| 	if err != nil { | ||||
| 		t.Fatalf("error constructing store: %v", err) | ||||
| 	} | ||||
| 	defer cleanupFakeFsStore(store) | ||||
|  | ||||
| 	// checkpoint a payload | ||||
| 	const ( | ||||
| @@ -160,6 +155,7 @@ func TestFsStoreSave(t *testing.T) { | ||||
| 	if err != nil { | ||||
| 		t.Fatalf("error constructing store: %v", err) | ||||
| 	} | ||||
| 	defer cleanupFakeFsStore(store) | ||||
|  | ||||
| 	nameTooLong := func() string { | ||||
| 		s := "" | ||||
| @@ -224,6 +220,8 @@ func TestFsStoreLoad(t *testing.T) { | ||||
| 	if err != nil { | ||||
| 		t.Fatalf("error constructing store: %v", err) | ||||
| 	} | ||||
| 	defer cleanupFakeFsStore(store) | ||||
|  | ||||
| 	// encode a kubelet configuration that has all defaults set | ||||
| 	expect, err := newKubeletConfiguration() | ||||
| 	if err != nil { | ||||
| @@ -295,6 +293,7 @@ func TestFsStoreAssignedModified(t *testing.T) { | ||||
| 	if err != nil { | ||||
| 		t.Fatalf("error constructing store: %v", err) | ||||
| 	} | ||||
| 	defer cleanupFakeFsStore(store) | ||||
|  | ||||
| 	// create an empty assigned file, this is good enough for testing | ||||
| 	saveTestSourceFile(t, store, assignedFile, nil) | ||||
| @@ -321,6 +320,7 @@ func TestFsStoreAssigned(t *testing.T) { | ||||
| 	if err != nil { | ||||
| 		t.Fatalf("error constructing store: %v", err) | ||||
| 	} | ||||
| 	defer cleanupFakeFsStore(store) | ||||
|  | ||||
| 	source, _, err := checkpoint.NewRemoteConfigSource(&apiv1.NodeConfigSource{ | ||||
| 		ConfigMap: &apiv1.ConfigMapNodeConfigSource{ | ||||
| @@ -364,6 +364,7 @@ func TestFsStoreLastKnownGood(t *testing.T) { | ||||
| 	if err != nil { | ||||
| 		t.Fatalf("error constructing store: %v", err) | ||||
| 	} | ||||
| 	defer cleanupFakeFsStore(store) | ||||
|  | ||||
| 	source, _, err := checkpoint.NewRemoteConfigSource(&apiv1.NodeConfigSource{ | ||||
| 		ConfigMap: &apiv1.ConfigMapNodeConfigSource{ | ||||
| @@ -407,6 +408,7 @@ func TestFsStoreSetAssigned(t *testing.T) { | ||||
| 	if err != nil { | ||||
| 		t.Fatalf("error constructing store: %v", err) | ||||
| 	} | ||||
| 	defer cleanupFakeFsStore(store) | ||||
|  | ||||
| 	cases := []struct { | ||||
| 		desc   string | ||||
| @@ -490,6 +492,7 @@ func TestFsStoreSetLastKnownGood(t *testing.T) { | ||||
| 	if err != nil { | ||||
| 		t.Fatalf("error constructing store: %v", err) | ||||
| 	} | ||||
| 	defer cleanupFakeFsStore(store) | ||||
|  | ||||
| 	cases := []struct { | ||||
| 		desc   string | ||||
| @@ -573,6 +576,7 @@ func TestFsStoreReset(t *testing.T) { | ||||
| 	if err != nil { | ||||
| 		t.Fatalf("error constructing store: %v", err) | ||||
| 	} | ||||
| 	defer cleanupFakeFsStore(store) | ||||
|  | ||||
| 	source, _, err := checkpoint.NewRemoteConfigSource(&apiv1.NodeConfigSource{ConfigMap: &apiv1.ConfigMapNodeConfigSource{ | ||||
| 		Name:             "name", | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Kubernetes Prow Robot
					Kubernetes Prow Robot