From 7447d8d32145e91d281c0be0fde79fe85dcf81cf Mon Sep 17 00:00:00 2001 From: jijun2 Date: Mon, 12 Oct 2015 20:28:03 +0800 Subject: [PATCH] test: using ioutil.TempDir in unit tests per #15176 update update mode delete /tmp update use ioutil.TempDir instead of static tmp dir use ioutil.TempDir instead of static tmp dir --- pkg/kubelet/network/cni/cni_test.go | 28 +++++---- pkg/kubelet/runonce_test.go | 10 +++- pkg/volume/aws_ebs/aws_ebs_test.go | 41 ++++++++++--- pkg/volume/cephfs/cephfs_test.go | 19 +++++- pkg/volume/cinder/cinder_test.go | 20 +++++-- pkg/volume/downwardapi/downwardapi_test.go | 68 ++++++++++++++++++---- pkg/volume/empty_dir/empty_dir_test.go | 19 ++++-- pkg/volume/gce_pd/gce_pd_test.go | 33 +++++++++-- 8 files changed, 189 insertions(+), 49 deletions(-) diff --git a/pkg/kubelet/network/cni/cni_test.go b/pkg/kubelet/network/cni/cni_test.go index 91bbf879d6cb..af10ea289ba1 100644 --- a/pkg/kubelet/network/cni/cni_test.go +++ b/pkg/kubelet/network/cni/cni_test.go @@ -41,10 +41,15 @@ import ( ) // The temp dir where test plugins will be stored. -const testNetworkConfigPath = "/tmp/fake/plugins/net/cni" -const testVendorCNIDirPrefix = "/tmp" +func tmpDirOrDie() string { + dir, err := ioutil.TempDir(os.TempDir(), "cni-test") + if err != nil { + panic(fmt.Sprintf("error creating tmp dir: %v", err)) + } + return dir +} -func installPluginUnderTest(t *testing.T, vendorName string, plugName string) { +func installPluginUnderTest(t *testing.T, testVendorCNIDirPrefix, testNetworkConfigPath, vendorName string, plugName string) { pluginDir := path.Join(testNetworkConfigPath, plugName) err := os.MkdirAll(pluginDir, 0777) if err != nil { @@ -102,13 +107,8 @@ echo -n "{ \"ip4\": { \"ip\": \"10.1.0.23/24\" } }" f.Close() } -func tearDownPlugin(plugName string, vendorName string) { - err := os.RemoveAll(testNetworkConfigPath) - if err != nil { - fmt.Printf("Error in cleaning up test: %v", err) - } - vendorCNIDir := fmt.Sprintf(VendorCNIDirTemplate, testVendorCNIDirPrefix, vendorName) - err = os.RemoveAll(vendorCNIDir) +func tearDownPlugin(tmpDir string) { + err := os.RemoveAll(tmpDir) if err != nil { fmt.Printf("Error in cleaning up test: %v", err) } @@ -166,8 +166,12 @@ func TestCNIPlugin(t *testing.T) { // install some random plugin pluginName := fmt.Sprintf("test%d", rand.Intn(1000)) vendorName := fmt.Sprintf("test_vendor%d", rand.Intn(1000)) - defer tearDownPlugin(pluginName, vendorName) - installPluginUnderTest(t, vendorName, pluginName) + + tmpDir := tmpDirOrDie() + testNetworkConfigPath := path.Join(tmpDir, "plugins", "net", "cni") + testVendorCNIDirPrefix := tmpDir + defer tearDownPlugin(tmpDir) + installPluginUnderTest(t, testVendorCNIDirPrefix, testNetworkConfigPath, vendorName, pluginName) np := probeNetworkPluginsWithVendorCNIDirPrefix(path.Join(testNetworkConfigPath, pluginName), testVendorCNIDirPrefix) plug, err := network.InitNetworkPlugin(np, "cni", NewFakeHost(nil)) diff --git a/pkg/kubelet/runonce_test.go b/pkg/kubelet/runonce_test.go index 5d36d708ddd8..ccefc7f02e62 100644 --- a/pkg/kubelet/runonce_test.go +++ b/pkg/kubelet/runonce_test.go @@ -17,6 +17,8 @@ limitations under the License. package kubelet import ( + "io/ioutil" + "os" "testing" "time" @@ -36,9 +38,13 @@ func TestRunOnce(t *testing.T) { podManager, _ := newFakePodManager() diskSpaceManager, _ := newDiskSpaceManager(cadvisor, DiskSpacePolicy{}) fakeRuntime := &kubecontainer.FakeRuntime{} - + basePath, err := ioutil.TempDir(os.TempDir(), "kubelet") + if err != nil { + t.Fatalf("can't make a temp rootdir %v", err) + } + defer os.RemoveAll(basePath) kb := &Kubelet{ - rootDirectory: "/tmp/kubelet", + rootDirectory: basePath, recorder: &record.FakeRecorder{}, cadvisor: cadvisor, nodeLister: testNodeLister{}, diff --git a/pkg/volume/aws_ebs/aws_ebs_test.go b/pkg/volume/aws_ebs/aws_ebs_test.go index 81c4938ef7d1..49180f93b431 100644 --- a/pkg/volume/aws_ebs/aws_ebs_test.go +++ b/pkg/volume/aws_ebs/aws_ebs_test.go @@ -17,7 +17,9 @@ limitations under the License. package aws_ebs import ( + "io/ioutil" "os" + "path" "testing" "k8s.io/kubernetes/pkg/api" @@ -29,8 +31,13 @@ import ( ) func TestCanSupport(t *testing.T) { + tmpDir, err := ioutil.TempDir(os.TempDir(), "awsebsTest") + if err != nil { + t.Fatalf("can't make a temp dir: %v", err) + } + defer os.RemoveAll(tmpDir) plugMgr := volume.VolumePluginMgr{} - plugMgr.InitPlugins(ProbeVolumePlugins(), volume.NewFakeVolumeHost("/tmp/fake", nil, nil)) + plugMgr.InitPlugins(ProbeVolumePlugins(), volume.NewFakeVolumeHost(tmpDir, nil, nil)) plug, err := plugMgr.FindPluginByName("kubernetes.io/aws-ebs") if err != nil { @@ -48,8 +55,13 @@ func TestCanSupport(t *testing.T) { } func TestGetAccessModes(t *testing.T) { + tmpDir, err := ioutil.TempDir(os.TempDir(), "awsebsTest") + if err != nil { + t.Fatalf("can't make a temp dir: %v", err) + } + defer os.RemoveAll(tmpDir) plugMgr := volume.VolumePluginMgr{} - plugMgr.InitPlugins(ProbeVolumePlugins(), volume.NewFakeVolumeHost("/tmp/fake", nil, nil)) + plugMgr.InitPlugins(ProbeVolumePlugins(), volume.NewFakeVolumeHost(tmpDir, nil, nil)) plug, err := plugMgr.FindPersistentPluginByName("kubernetes.io/aws-ebs") if err != nil { @@ -95,8 +107,13 @@ func (fake *fakePDManager) DetachDisk(c *awsElasticBlockStoreCleaner) error { } func TestPlugin(t *testing.T) { + tmpDir, err := ioutil.TempDir(os.TempDir(), "awsebsTest") + if err != nil { + t.Fatalf("can't make a temp dir: %v") + } + defer os.RemoveAll(tmpDir) plugMgr := volume.VolumePluginMgr{} - plugMgr.InitPlugins(ProbeVolumePlugins(), volume.NewFakeVolumeHost("/tmp/fake", nil, nil)) + plugMgr.InitPlugins(ProbeVolumePlugins(), volume.NewFakeVolumeHost(tmpDir, nil, nil)) plug, err := plugMgr.FindPluginByName("kubernetes.io/aws-ebs") if err != nil { @@ -118,9 +135,9 @@ func TestPlugin(t *testing.T) { if builder == nil { t.Errorf("Got a nil Builder") } - + volPath := path.Join(tmpDir, "pods/poduid/volumes/kubernetes.io~aws-ebs/vol1") path := builder.GetPath() - if path != "/tmp/fake/pods/poduid/volumes/kubernetes.io~aws-ebs/vol1" { + if path != volPath { t.Errorf("Got unexpected path: %s", path) } @@ -194,8 +211,13 @@ func TestPersistentClaimReadOnlyFlag(t *testing.T) { client := &testclient.Fake{} client.AddReactor("*", "*", testclient.ObjectReaction(o, testapi.Default.RESTMapper())) + tmpDir, err := ioutil.TempDir(os.TempDir(), "awsebsTest") + if err != nil { + t.Fatalf("can't make a temp dir: %v", err) + } + defer os.RemoveAll(tmpDir) plugMgr := volume.VolumePluginMgr{} - plugMgr.InitPlugins(ProbeVolumePlugins(), volume.NewFakeVolumeHost("/tmp/fake", client, nil)) + plugMgr.InitPlugins(ProbeVolumePlugins(), volume.NewFakeVolumeHost(tmpDir, client, nil)) plug, _ := plugMgr.FindPluginByName(awsElasticBlockStorePluginName) // readOnly bool is supplied by persistent-claim volume source when its builder creates other volumes @@ -209,8 +231,13 @@ func TestPersistentClaimReadOnlyFlag(t *testing.T) { } func TestBuilderAndCleanerTypeAssert(t *testing.T) { + tmpDir, err := ioutil.TempDir(os.TempDir(), "awsebsTest") + if err != nil { + t.Fatalf("can't make a temp dir: %v", err) + } + defer os.RemoveAll(tmpDir) plugMgr := volume.VolumePluginMgr{} - plugMgr.InitPlugins(ProbeVolumePlugins(), volume.NewFakeVolumeHost("/tmp/fake", nil, nil)) + plugMgr.InitPlugins(ProbeVolumePlugins(), volume.NewFakeVolumeHost(tmpDir, nil, nil)) plug, err := plugMgr.FindPluginByName("kubernetes.io/aws-ebs") if err != nil { diff --git a/pkg/volume/cephfs/cephfs_test.go b/pkg/volume/cephfs/cephfs_test.go index 042edfe7cb50..e68106cd48af 100644 --- a/pkg/volume/cephfs/cephfs_test.go +++ b/pkg/volume/cephfs/cephfs_test.go @@ -17,7 +17,9 @@ limitations under the License. package cephfs import ( + "io/ioutil" "os" + "path" "testing" "k8s.io/kubernetes/pkg/api" @@ -27,8 +29,13 @@ import ( ) func TestCanSupport(t *testing.T) { + tmpDir, err := ioutil.TempDir(os.TempDir(), "cephTest") + if err != nil { + t.Fatalf("can't make a temp dir: %v", err) + } + defer os.RemoveAll(tmpDir) plugMgr := volume.VolumePluginMgr{} - plugMgr.InitPlugins(ProbeVolumePlugins(), volume.NewFakeVolumeHost("fake", nil, nil)) + plugMgr.InitPlugins(ProbeVolumePlugins(), volume.NewFakeVolumeHost(tmpDir, nil, nil)) plug, err := plugMgr.FindPluginByName("kubernetes.io/cephfs") if err != nil { t.Errorf("Can't find the plugin by name") @@ -45,8 +52,13 @@ func TestCanSupport(t *testing.T) { } func TestPlugin(t *testing.T) { + tmpDir, err := ioutil.TempDir(os.TempDir(), "cephTest") + if err != nil { + t.Fatalf("can't make a temp dir: %v", err) + } + defer os.RemoveAll(tmpDir) plugMgr := volume.VolumePluginMgr{} - plugMgr.InitPlugins(ProbeVolumePlugins(), volume.NewFakeVolumeHost("/tmp/fake", nil, nil)) + plugMgr.InitPlugins(ProbeVolumePlugins(), volume.NewFakeVolumeHost(tmpDir, nil, nil)) plug, err := plugMgr.FindPluginByName("kubernetes.io/cephfs") if err != nil { t.Errorf("Can't find the plugin by name") @@ -71,8 +83,9 @@ func TestPlugin(t *testing.T) { if builder == nil { t.Errorf("Got a nil Builder: %v") } + volpath := path.Join(tmpDir, "pods/poduid/volumes/kubernetes.io~cephfs/vol1") path := builder.GetPath() - if path != "/tmp/fake/pods/poduid/volumes/kubernetes.io~cephfs/vol1" { + if path != volpath { t.Errorf("Got unexpected path: %s", path) } if err := builder.SetUp(); err != nil { diff --git a/pkg/volume/cinder/cinder_test.go b/pkg/volume/cinder/cinder_test.go index 5cf6dcec0bf4..c89ad0e55a1f 100644 --- a/pkg/volume/cinder/cinder_test.go +++ b/pkg/volume/cinder/cinder_test.go @@ -17,7 +17,9 @@ limitations under the License. package cinder import ( + "io/ioutil" "os" + "path" "testing" "k8s.io/kubernetes/pkg/api" @@ -27,8 +29,13 @@ import ( ) func TestCanSupport(t *testing.T) { + tmpDir, err := ioutil.TempDir(os.TempDir(), "cinderTest") + if err != nil { + t.Fatalf("can't make a temp dir: %v", err) + } + defer os.RemoveAll(tmpDir) plugMgr := volume.VolumePluginMgr{} - plugMgr.InitPlugins(ProbeVolumePlugins(), volume.NewFakeVolumeHost("/tmp/fake", nil, nil)) + plugMgr.InitPlugins(ProbeVolumePlugins(), volume.NewFakeVolumeHost(tmpDir, nil, nil)) plug, err := plugMgr.FindPluginByName("kubernetes.io/cinder") if err != nil { @@ -67,8 +74,13 @@ func (fake *fakePDManager) DetachDisk(c *cinderVolumeCleaner) error { } func TestPlugin(t *testing.T) { + tmpDir, err := ioutil.TempDir(os.TempDir(), "cinderTest") + if err != nil { + t.Fatalf("can't make a temp dir: %v", err) + } + defer os.RemoveAll(tmpDir) plugMgr := volume.VolumePluginMgr{} - plugMgr.InitPlugins(ProbeVolumePlugins(), volume.NewFakeVolumeHost("/tmp/fake", nil, nil)) + plugMgr.InitPlugins(ProbeVolumePlugins(), volume.NewFakeVolumeHost(tmpDir, nil, nil)) plug, err := plugMgr.FindPluginByName("kubernetes.io/cinder") if err != nil { @@ -90,9 +102,9 @@ func TestPlugin(t *testing.T) { if builder == nil { t.Errorf("Got a nil Builder: %v") } - + volPath := path.Join(tmpDir, "pods/poduid/volumes/kubernetes.io~cinder/vol1") path := builder.GetPath() - if path != "/tmp/fake/pods/poduid/volumes/kubernetes.io~cinder/vol1" { + if path != volPath { t.Errorf("Got unexpected path: %s", path) } diff --git a/pkg/volume/downwardapi/downwardapi_test.go b/pkg/volume/downwardapi/downwardapi_test.go index d145182921ec..6c2559e38d43 100644 --- a/pkg/volume/downwardapi/downwardapi_test.go +++ b/pkg/volume/downwardapi/downwardapi_test.go @@ -31,8 +31,6 @@ import ( "k8s.io/kubernetes/pkg/volume/empty_dir" ) -const basePath = "/tmp/fake" - func formatMap(m map[string]string) (fmtstr string) { for key, value := range m { fmtstr += fmt.Sprintf("%v=%q\n", key, value) @@ -41,7 +39,7 @@ func formatMap(m map[string]string) (fmtstr string) { return } -func newTestHost(t *testing.T, client client.Interface) volume.VolumeHost { +func newTestHost(t *testing.T, client client.Interface, basePath string) volume.VolumeHost { tempDir, err := ioutil.TempDir(basePath, "downwardApi_volume_test.") if err != nil { t.Fatalf("can't make a temp rootdir: %v", err) @@ -50,8 +48,13 @@ func newTestHost(t *testing.T, client client.Interface) volume.VolumeHost { } func TestCanSupport(t *testing.T) { + tmpDir, err := ioutil.TempDir(os.TempDir(), "downwardapiTest") + if err != nil { + t.Fatalf("can't make a temp dir: %v", err) + } + defer os.RemoveAll(tmpDir) pluginMgr := volume.VolumePluginMgr{} - pluginMgr.InitPlugins(ProbeVolumePlugins(), newTestHost(t, nil)) + pluginMgr.InitPlugins(ProbeVolumePlugins(), newTestHost(t, nil, tmpDir)) plugin, err := pluginMgr.FindPluginByName(downwardAPIPluginName) if err != nil { @@ -100,8 +103,14 @@ func TestLabels(t *testing.T) { Labels: labels, }, }) + + tmpDir, err := ioutil.TempDir(os.TempDir(), "downwardapiTest") + if err != nil { + t.Fatalf("can't make a temp dir: %v", err) + } + defer os.RemoveAll(tmpDir) pluginMgr := volume.VolumePluginMgr{} - pluginMgr.InitPlugins(ProbeVolumePlugins(), newTestHost(t, fake)) + pluginMgr.InitPlugins(ProbeVolumePlugins(), newTestHost(t, fake, tmpDir)) plugin, err := pluginMgr.FindPluginByName(downwardAPIPluginName) volumeSpec := &api.Volume{ Name: testVolumeName, @@ -174,8 +183,13 @@ func TestAnnotations(t *testing.T) { }, }) + tmpDir, err := ioutil.TempDir(os.TempDir(), "downwardapiTest") + if err != nil { + t.Fatalf("can't make a temp dir: %v", err) + } + defer os.RemoveAll(tmpDir) pluginMgr := volume.VolumePluginMgr{} - pluginMgr.InitPlugins(ProbeVolumePlugins(), newTestHost(t, fake)) + pluginMgr.InitPlugins(ProbeVolumePlugins(), newTestHost(t, fake, tmpDir)) plugin, err := pluginMgr.FindPluginByName(downwardAPIPluginName) if err != nil { t.Errorf("Can't find the plugin by name") @@ -234,8 +248,13 @@ func TestName(t *testing.T) { }, }) + tmpDir, err := ioutil.TempDir(os.TempDir(), "downwardapiTest") + if err != nil { + t.Fatalf("can't make a temp dir: %v", err) + } + defer os.RemoveAll(tmpDir) pluginMgr := volume.VolumePluginMgr{} - pluginMgr.InitPlugins(ProbeVolumePlugins(), newTestHost(t, fake)) + pluginMgr.InitPlugins(ProbeVolumePlugins(), newTestHost(t, fake, tmpDir)) plugin, err := pluginMgr.FindPluginByName(downwardAPIPluginName) if err != nil { t.Errorf("Can't find the plugin by name") @@ -295,8 +314,13 @@ func TestNamespace(t *testing.T) { }, }) + tmpDir, err := ioutil.TempDir(os.TempDir(), "downwardapiTest") + if err != nil { + t.Fatalf("can't make a temp dir: %v", err) + } + defer os.RemoveAll(tmpDir) pluginMgr := volume.VolumePluginMgr{} - pluginMgr.InitPlugins(ProbeVolumePlugins(), newTestHost(t, fake)) + pluginMgr.InitPlugins(ProbeVolumePlugins(), newTestHost(t, fake, tmpDir)) plugin, err := pluginMgr.FindPluginByName(downwardAPIPluginName) if err != nil { t.Errorf("Can't find the plugin by name") @@ -349,8 +373,13 @@ func TestWriteTwiceNoUpdate(t *testing.T) { Labels: labels, }, }) + tmpDir, err := ioutil.TempDir(os.TempDir(), "downwardapiTest") + if err != nil { + t.Fatalf("can't make a temp dir: %v", err) + } + defer os.RemoveAll(tmpDir) pluginMgr := volume.VolumePluginMgr{} - pluginMgr.InitPlugins(ProbeVolumePlugins(), newTestHost(t, fake)) + pluginMgr.InitPlugins(ProbeVolumePlugins(), newTestHost(t, fake, tmpDir)) plugin, err := pluginMgr.FindPluginByName(downwardAPIPluginName) volumeSpec := &api.Volume{ Name: testVolumeName, @@ -433,8 +462,13 @@ func TestWriteTwiceWithUpdate(t *testing.T) { Labels: labels, }, }) + tmpDir, err := ioutil.TempDir(os.TempDir(), "downwardapiTest") + if err != nil { + t.Fatalf("can't make a temp dir: %v", err) + } + defer os.RemoveAll(tmpDir) pluginMgr := volume.VolumePluginMgr{} - pluginMgr.InitPlugins(ProbeVolumePlugins(), newTestHost(t, fake)) + pluginMgr.InitPlugins(ProbeVolumePlugins(), newTestHost(t, fake, tmpDir)) plugin, err := pluginMgr.FindPluginByName(downwardAPIPluginName) volumeSpec := &api.Volume{ Name: testVolumeName, @@ -537,8 +571,13 @@ func TestWriteWithUnixPath(t *testing.T) { }, }) + tmpDir, err := ioutil.TempDir(os.TempDir(), "downwardapiTest") + if err != nil { + t.Fatalf("can't make a temp dir: %v", err) + } + defer os.RemoveAll(tmpDir) pluginMgr := volume.VolumePluginMgr{} - pluginMgr.InitPlugins(ProbeVolumePlugins(), newTestHost(t, fake)) + pluginMgr.InitPlugins(ProbeVolumePlugins(), newTestHost(t, fake, tmpDir)) plugin, err := pluginMgr.FindPluginByName(downwardAPIPluginName) volumeSpec := &api.Volume{ Name: testVolumeName, @@ -611,8 +650,13 @@ func TestWriteWithUnixPathBadPath(t *testing.T) { }, }) + tmpDir, err := ioutil.TempDir(os.TempDir(), "downwardapiTest") + if err != nil { + t.Fatalf("can't make a temp dir") + } + defer os.RemoveAll(tmpDir) pluginMgr := volume.VolumePluginMgr{} - pluginMgr.InitPlugins(ProbeVolumePlugins(), newTestHost(t, fake)) + pluginMgr.InitPlugins(ProbeVolumePlugins(), newTestHost(t, fake, tmpDir)) plugin, err := pluginMgr.FindPluginByName(downwardAPIPluginName) if err != nil { t.Errorf("Can't find the plugin by name") diff --git a/pkg/volume/empty_dir/empty_dir_test.go b/pkg/volume/empty_dir/empty_dir_test.go index 3373c91bf826..9c3fe4452ccf 100644 --- a/pkg/volume/empty_dir/empty_dir_test.go +++ b/pkg/volume/empty_dir/empty_dir_test.go @@ -42,7 +42,12 @@ func makePluginUnderTest(t *testing.T, plugName, basePath string) volume.VolumeP } func TestCanSupport(t *testing.T) { - plug := makePluginUnderTest(t, "kubernetes.io/empty-dir", "/tmp/fake") + tmpDir, err := ioutil.TempDir(os.TempDir(), "emptydirTest") + if err != nil { + t.Fatalf("can't make a temp dir: %v", err) + } + defer os.RemoveAll(tmpDir) + plug := makePluginUnderTest(t, "kubernetes.io/empty-dir", tmpDir) if plug.Name() != "kubernetes.io/empty-dir" { t.Errorf("Wrong name: %s", plug.Name()) @@ -135,10 +140,11 @@ type pluginTestConfig struct { // doTestPlugin sets up a volume and tears it back down. func doTestPlugin(t *testing.T, config pluginTestConfig) { - basePath, err := ioutil.TempDir("/tmp", "emptydir_volume_test") + basePath, err := ioutil.TempDir(os.TempDir(), "emptydir_volume_test") if err != nil { - t.Fatalf("can't make a temp rootdir") + t.Fatalf("can't make a temp rootdir: %v", err) } + defer os.RemoveAll(basePath) var ( volumePath = path.Join(basePath, "pods/poduid/volumes/kubernetes.io~empty-dir/test-volume") @@ -281,7 +287,12 @@ func doTestPlugin(t *testing.T, config pluginTestConfig) { } func TestPluginBackCompat(t *testing.T) { - basePath := "/tmp/fake" + basePath, err := ioutil.TempDir(os.TempDir(), "emptydirTest") + if err != nil { + t.Fatalf("can't make a temp dir: %v", err) + } + defer os.RemoveAll(basePath) + plug := makePluginUnderTest(t, "kubernetes.io/empty-dir", basePath) spec := &api.Volume{ diff --git a/pkg/volume/gce_pd/gce_pd_test.go b/pkg/volume/gce_pd/gce_pd_test.go index caa9a64dffe6..55ddc5d2c747 100644 --- a/pkg/volume/gce_pd/gce_pd_test.go +++ b/pkg/volume/gce_pd/gce_pd_test.go @@ -17,7 +17,9 @@ limitations under the License. package gce_pd import ( + "io/ioutil" "os" + "path" "testing" "k8s.io/kubernetes/pkg/api" @@ -29,8 +31,13 @@ import ( ) func TestCanSupport(t *testing.T) { + tmpDir, err := ioutil.TempDir(os.TempDir(), "gcepdTest") + if err != nil { + t.Fatalf("can't make a temp dir: %v", err) + } + defer os.RemoveAll(tmpDir) plugMgr := volume.VolumePluginMgr{} - plugMgr.InitPlugins(ProbeVolumePlugins(), volume.NewFakeVolumeHost("/tmp/fake", nil, nil)) + plugMgr.InitPlugins(ProbeVolumePlugins(), volume.NewFakeVolumeHost(tmpDir, nil, nil)) plug, err := plugMgr.FindPluginByName("kubernetes.io/gce-pd") if err != nil { @@ -48,8 +55,13 @@ func TestCanSupport(t *testing.T) { } func TestGetAccessModes(t *testing.T) { + tmpDir, err := ioutil.TempDir(os.TempDir(), "gcepdTest") + if err != nil { + t.Fatalf("can't make a temp dir: %v", err) + } + defer os.RemoveAll(tmpDir) plugMgr := volume.VolumePluginMgr{} - plugMgr.InitPlugins(ProbeVolumePlugins(), volume.NewFakeVolumeHost("/tmp/fake", nil, nil)) + plugMgr.InitPlugins(ProbeVolumePlugins(), volume.NewFakeVolumeHost(tmpDir, nil, nil)) plug, err := plugMgr.FindPersistentPluginByName("kubernetes.io/gce-pd") if err != nil { @@ -100,8 +112,13 @@ func (fake *fakePDManager) DetachDisk(c *gcePersistentDiskCleaner) error { } func TestPlugin(t *testing.T) { + tmpDir, err := ioutil.TempDir(os.TempDir(), "gcepdTest") + if err != nil { + t.Fatalf("can't make a temp dir: %v", err) + } + defer os.RemoveAll(tmpDir) plugMgr := volume.VolumePluginMgr{} - plugMgr.InitPlugins(ProbeVolumePlugins(), volume.NewFakeVolumeHost("/tmp/fake", nil, nil)) + plugMgr.InitPlugins(ProbeVolumePlugins(), volume.NewFakeVolumeHost(tmpDir, nil, nil)) plug, err := plugMgr.FindPluginByName("kubernetes.io/gce-pd") if err != nil { @@ -126,8 +143,9 @@ func TestPlugin(t *testing.T) { t.Errorf("Got a nil Builder") } + volPath := path.Join(tmpDir, "pods/poduid/volumes/kubernetes.io~gce-pd/vol1") path := builder.GetPath() - if path != "/tmp/fake/pods/poduid/volumes/kubernetes.io~gce-pd/vol1" { + if path != volPath { t.Errorf("Got unexpected path: %s", path) } @@ -208,8 +226,13 @@ func TestPersistentClaimReadOnlyFlag(t *testing.T) { client := &testclient.Fake{} client.AddReactor("*", "*", testclient.ObjectReaction(o, testapi.Default.RESTMapper())) + tmpDir, err := ioutil.TempDir(os.TempDir(), "gcepdTest") + if err != nil { + t.Fatalf("can't make a temp dir: %v", err) + } + defer os.RemoveAll(tmpDir) plugMgr := volume.VolumePluginMgr{} - plugMgr.InitPlugins(ProbeVolumePlugins(), volume.NewFakeVolumeHost("/tmp/fake", client, nil)) + plugMgr.InitPlugins(ProbeVolumePlugins(), volume.NewFakeVolumeHost(tmpDir, client, nil)) plug, _ := plugMgr.FindPluginByName(gcePersistentDiskPluginName) // readOnly bool is supplied by persistent-claim volume source when its builder creates other volumes