From 7205a160acff646b28b34694b23c3c6b3966d753 Mon Sep 17 00:00:00 2001 From: Fabio Yeon Date: Mon, 25 Jan 2016 13:57:42 -0800 Subject: [PATCH] Remove all instances of "/tmp" from unit tests and replace with a common tmp directory creator. Exception is documented. --- contrib/mesos/pkg/executor/executor_test.go | 5 +- .../persistent-volumes/volumes/local-01.yaml | 2 +- .../persistent-volumes/volumes/local-02.yaml | 2 +- .../persistent-volumes/volumes/nfs.yaml | 2 +- .../providers/mesos/client_test.go | 2 +- ...tentvolume_claim_binder_controller_test.go | 36 +++++++-- ...stentvolume_provisioner_controller_test.go | 2 +- ...rsistentvolume_recycler_controller_test.go | 2 +- pkg/kubelet/config/file_test.go | 3 +- pkg/kubelet/dockertools/labels_test.go | 2 +- pkg/kubelet/network/cni/cni_test.go | 12 +-- pkg/kubelet/network/exec/exec_test.go | 3 +- pkg/kubelet/rkt/rkt_test.go | 16 +++- pkg/kubelet/runonce_test.go | 4 +- pkg/master/tunneler_test.go | 2 +- pkg/util/io/io_test.go | 6 +- pkg/util/testing/tmpdir.go | 44 ++++++++++ pkg/volume/aws_ebs/aws_ebs_test.go | 12 +-- pkg/volume/cephfs/cephfs_test.go | 6 +- pkg/volume/cinder/cinder_test.go | 6 +- pkg/volume/downwardapi/downwardapi_test.go | 77 +++++------------- pkg/volume/empty_dir/empty_dir_test.go | 10 +-- pkg/volume/fc/fc_test.go | 62 +++++++++++--- pkg/volume/flexvolume/flexvolume_test.go | 80 ++++++++++++++----- pkg/volume/flocker/plugin_test.go | 4 +- pkg/volume/gce_pd/gce_pd_test.go | 10 +-- pkg/volume/glusterfs/glusterfs_test.go | 39 +++++++-- pkg/volume/host_path/host_path_test.go | 3 +- pkg/volume/iscsi/iscsi_test.go | 62 +++++++++++--- pkg/volume/metrics_du_test.go | 4 +- pkg/volume/nfs/nfs_test.go | 51 +++++++++--- .../persistent_claim/persistent_claim_test.go | 27 ++++--- pkg/volume/rbd/rbd_test.go | 53 +++++++++--- 33 files changed, 454 insertions(+), 197 deletions(-) create mode 100644 pkg/util/testing/tmpdir.go diff --git a/contrib/mesos/pkg/executor/executor_test.go b/contrib/mesos/pkg/executor/executor_test.go index 9e07281531..8d86762943 100644 --- a/contrib/mesos/pkg/executor/executor_test.go +++ b/contrib/mesos/pkg/executor/executor_test.go @@ -40,6 +40,7 @@ import ( "k8s.io/kubernetes/pkg/kubelet/dockertools" "k8s.io/kubernetes/pkg/runtime" "k8s.io/kubernetes/pkg/util" + utiltesting "k8s.io/kubernetes/pkg/util/testing" "k8s.io/kubernetes/pkg/watch" "github.com/mesos/mesos-go/mesosproto" @@ -288,7 +289,7 @@ func TestExecutorLaunchAndKillTask(t *testing.T) { // as a zip archive with pod definitions. func TestExecutorInitializeStaticPodsSource(t *testing.T) { // create some zip with static pod definition - givenPodsDir, err := ioutil.TempDir("/tmp", "executor-givenpods") + givenPodsDir, err := utiltesting.MkTmpdir("executor-givenpods") assert.NoError(t, err) defer os.RemoveAll(givenPodsDir) @@ -339,7 +340,7 @@ func TestExecutorInitializeStaticPodsSource(t *testing.T) { expectedStaticPodsNum := 2 // subdirectories are ignored by FileSource, hence only 2 // temporary directory which is normally located in the executor sandbox - staticPodsConfigPath, err := ioutil.TempDir("/tmp", "executor-k8sm-archive") + staticPodsConfigPath, err := utiltesting.MkTmpdir("executor-k8sm-archive") assert.NoError(t, err) defer os.RemoveAll(staticPodsConfigPath) diff --git a/docs/user-guide/persistent-volumes/volumes/local-01.yaml b/docs/user-guide/persistent-volumes/volumes/local-01.yaml index a465c65149..25acecca93 100644 --- a/docs/user-guide/persistent-volumes/volumes/local-01.yaml +++ b/docs/user-guide/persistent-volumes/volumes/local-01.yaml @@ -10,4 +10,4 @@ spec: accessModes: - ReadWriteOnce hostPath: - path: "/tmp/data01" + path: "/somepath/data01" diff --git a/docs/user-guide/persistent-volumes/volumes/local-02.yaml b/docs/user-guide/persistent-volumes/volumes/local-02.yaml index b946e7105d..e72d2e7d17 100644 --- a/docs/user-guide/persistent-volumes/volumes/local-02.yaml +++ b/docs/user-guide/persistent-volumes/volumes/local-02.yaml @@ -10,5 +10,5 @@ spec: accessModes: - ReadWriteOnce hostPath: - path: "/tmp/data02" + path: "/somepath/data02" persistentVolumeReclaimPolicy: Recycle diff --git a/docs/user-guide/persistent-volumes/volumes/nfs.yaml b/docs/user-guide/persistent-volumes/volumes/nfs.yaml index a6dc2ed250..eae2e7abac 100644 --- a/docs/user-guide/persistent-volumes/volumes/nfs.yaml +++ b/docs/user-guide/persistent-volumes/volumes/nfs.yaml @@ -8,5 +8,5 @@ spec: accessModes: - ReadWriteOnce nfs: - path: /tmp + path: /somepath server: 172.17.0.2 diff --git a/pkg/cloudprovider/providers/mesos/client_test.go b/pkg/cloudprovider/providers/mesos/client_test.go index c7c930569b..83209924be 100644 --- a/pkg/cloudprovider/providers/mesos/client_test.go +++ b/pkg/cloudprovider/providers/mesos/client_test.go @@ -105,7 +105,7 @@ const ( "finished_tasks": 0, "flags": { "zk_session_timeout": "10secs", - "work_dir": "/tmp/mesos/local/Lc9arz", + "work_dir": "/somepath/mesos/local/Lc9arz", "webui_dir": "/usr/local/share/mesos/webui", "version": "false", "user_sorter": "drf", diff --git a/pkg/controller/persistentvolume/persistentvolume_claim_binder_controller_test.go b/pkg/controller/persistentvolume/persistentvolume_claim_binder_controller_test.go index 32a0bc7093..a851f89769 100644 --- a/pkg/controller/persistentvolume/persistentvolume_claim_binder_controller_test.go +++ b/pkg/controller/persistentvolume/persistentvolume_claim_binder_controller_test.go @@ -17,6 +17,8 @@ limitations under the License. package persistentvolume import ( + "fmt" + "os" "reflect" "testing" "time" @@ -27,6 +29,7 @@ import ( "k8s.io/kubernetes/pkg/api/testapi" "k8s.io/kubernetes/pkg/client/cache" "k8s.io/kubernetes/pkg/client/unversioned/testclient" + utiltesting "k8s.io/kubernetes/pkg/util/testing" "k8s.io/kubernetes/pkg/volume" "k8s.io/kubernetes/pkg/volume/host_path" ) @@ -53,6 +56,11 @@ func TestRunStop(t *testing.T) { } func TestClaimRace(t *testing.T) { + tmpDir, err := utiltesting.MkTmpdir("claimbinder-test") + if err != nil { + t.Fatalf("error creating temp dir: %v", err) + } + defer os.RemoveAll(tmpDir) c1 := &api.PersistentVolumeClaim{ ObjectMeta: api.ObjectMeta{ Name: "c1", @@ -100,7 +108,7 @@ func TestClaimRace(t *testing.T) { }, PersistentVolumeSource: api.PersistentVolumeSource{ HostPath: &api.HostPathVolumeSource{ - Path: "/tmp/data01", + Path: fmt.Sprintf("%s/data01", tmpDir), }, }, }, @@ -113,7 +121,7 @@ func TestClaimRace(t *testing.T) { mockClient := &mockBinderClient{} plugMgr := volume.VolumePluginMgr{} - plugMgr.InitPlugins(host_path.ProbeRecyclableVolumePlugins(newMockRecycler, volume.VolumeConfig{}), volume.NewFakeVolumeHost("/tmp/fake", nil, nil)) + plugMgr.InitPlugins(host_path.ProbeRecyclableVolumePlugins(newMockRecycler, volume.VolumeConfig{}), volume.NewFakeVolumeHost(tmpDir, nil, nil)) // adds the volume to the index, making the volume available syncVolume(volumeIndex, mockClient, v) @@ -125,7 +133,7 @@ func TestClaimRace(t *testing.T) { } // an initial sync for a claim matches the volume - err := syncClaim(volumeIndex, mockClient, c1) + err = syncClaim(volumeIndex, mockClient, c1) if err != nil { t.Errorf("Unexpected error: %v", err) } @@ -144,6 +152,12 @@ func TestClaimRace(t *testing.T) { } func TestClaimSyncAfterVolumeProvisioning(t *testing.T) { + tmpDir, err := utiltesting.MkTmpdir("claimbinder-test") + if err != nil { + t.Fatalf("error creating temp dir: %v", err) + } + defer os.RemoveAll(tmpDir) + // Tests that binder.syncVolume will also syncClaim if the PV has completed // provisioning but the claim is still Pending. We want to advance to Bound // without having to wait until the binder's next sync period. @@ -184,7 +198,7 @@ func TestClaimSyncAfterVolumeProvisioning(t *testing.T) { }, PersistentVolumeSource: api.PersistentVolumeSource{ HostPath: &api.HostPathVolumeSource{ - Path: "/tmp/data01", + Path: fmt.Sprintf("%s/data01", tmpDir), }, }, ClaimRef: claimRef, @@ -200,7 +214,7 @@ func TestClaimSyncAfterVolumeProvisioning(t *testing.T) { } plugMgr := volume.VolumePluginMgr{} - plugMgr.InitPlugins(host_path.ProbeRecyclableVolumePlugins(newMockRecycler, volume.VolumeConfig{}), volume.NewFakeVolumeHost("/tmp/fake", nil, nil)) + plugMgr.InitPlugins(host_path.ProbeRecyclableVolumePlugins(newMockRecycler, volume.VolumeConfig{}), volume.NewFakeVolumeHost(tmpDir, nil, nil)) // adds the volume to the index, making the volume available. // pv also completed provisioning, so syncClaim should cause claim's phase to advance to Bound @@ -250,7 +264,7 @@ func TestExampleObjects(t *testing.T) { }, PersistentVolumeSource: api.PersistentVolumeSource{ HostPath: &api.HostPathVolumeSource{ - Path: "/tmp/data01", + Path: "/somepath/data01", }, }, }, @@ -265,7 +279,7 @@ func TestExampleObjects(t *testing.T) { }, PersistentVolumeSource: api.PersistentVolumeSource{ HostPath: &api.HostPathVolumeSource{ - Path: "/tmp/data02", + Path: "/somepath/data02", }, }, PersistentVolumeReclaimPolicy: api.PersistentVolumeReclaimRecycle, @@ -333,6 +347,12 @@ func TestExampleObjects(t *testing.T) { } func TestBindingWithExamples(t *testing.T) { + tmpDir, err := utiltesting.MkTmpdir("claimbinder-test") + if err != nil { + t.Fatalf("error creating temp dir: %v", err) + } + defer os.RemoveAll(tmpDir) + codec := api.Codecs.UniversalDecoder() o := testclient.NewObjects(api.Scheme, codec) if err := testclient.AddObjectsFromPath("../../../docs/user-guide/persistent-volumes/claims/claim-01.yaml", o, codec); err != nil { @@ -370,7 +390,7 @@ func TestBindingWithExamples(t *testing.T) { } plugMgr := volume.VolumePluginMgr{} - plugMgr.InitPlugins(host_path.ProbeRecyclableVolumePlugins(newMockRecycler, volume.VolumeConfig{}), volume.NewFakeVolumeHost("/tmp/fake", nil, nil)) + plugMgr.InitPlugins(host_path.ProbeRecyclableVolumePlugins(newMockRecycler, volume.VolumeConfig{}), volume.NewFakeVolumeHost(tmpDir, nil, nil)) recycler := &PersistentVolumeRecycler{ kubeClient: client, diff --git a/pkg/controller/persistentvolume/persistentvolume_provisioner_controller_test.go b/pkg/controller/persistentvolume/persistentvolume_provisioner_controller_test.go index e86daf7ae7..988f7895ce 100644 --- a/pkg/controller/persistentvolume/persistentvolume_provisioner_controller_test.go +++ b/pkg/controller/persistentvolume/persistentvolume_provisioner_controller_test.go @@ -66,7 +66,7 @@ func makeTestVolume() *api.PersistentVolume { }, PersistentVolumeSource: api.PersistentVolumeSource{ HostPath: &api.HostPathVolumeSource{ - Path: "/tmp/data01", + Path: "/somepath/data01", }, }, }, diff --git a/pkg/controller/persistentvolume/persistentvolume_recycler_controller_test.go b/pkg/controller/persistentvolume/persistentvolume_recycler_controller_test.go index 0aa22e6f1c..ec9b1c8d9e 100644 --- a/pkg/controller/persistentvolume/persistentvolume_recycler_controller_test.go +++ b/pkg/controller/persistentvolume/persistentvolume_recycler_controller_test.go @@ -34,7 +34,7 @@ func TestFailedRecycling(t *testing.T) { }, PersistentVolumeSource: api.PersistentVolumeSource{ HostPath: &api.HostPathVolumeSource{ - Path: "/tmp/data02", + Path: "/somepath/data02", }, }, PersistentVolumeReclaimPolicy: api.PersistentVolumeReclaimRecycle, diff --git a/pkg/kubelet/config/file_test.go b/pkg/kubelet/config/file_test.go index 1b41f0729b..bfa26291ed 100644 --- a/pkg/kubelet/config/file_test.go +++ b/pkg/kubelet/config/file_test.go @@ -30,6 +30,7 @@ import ( "k8s.io/kubernetes/pkg/runtime" "k8s.io/kubernetes/pkg/securitycontext" "k8s.io/kubernetes/pkg/util" + utiltesting "k8s.io/kubernetes/pkg/util/testing" ) func TestExtractFromNonExistentFile(t *testing.T) { @@ -174,7 +175,7 @@ func TestExtractFromBadDataFile(t *testing.T) { } func TestExtractFromEmptyDir(t *testing.T) { - dirName, err := ioutil.TempDir("", "foo") + dirName, err := utiltesting.MkTmpdir("file-test") if err != nil { t.Fatalf("Unexpected error: %v", err) } diff --git a/pkg/kubelet/dockertools/labels_test.go b/pkg/kubelet/dockertools/labels_test.go index 41e8a4b105..74d38552fe 100644 --- a/pkg/kubelet/dockertools/labels_test.go +++ b/pkg/kubelet/dockertools/labels_test.go @@ -52,7 +52,7 @@ func TestLabels(t *testing.T) { } container := &api.Container{ Name: "test_container", - TerminationMessagePath: "/tmp", + TerminationMessagePath: "/somepath", Lifecycle: lifecycle, } pod := &api.Pod{ diff --git a/pkg/kubelet/network/cni/cni_test.go b/pkg/kubelet/network/cni/cni_test.go index e94cdb925e..7287abdb66 100644 --- a/pkg/kubelet/network/cni/cni_test.go +++ b/pkg/kubelet/network/cni/cni_test.go @@ -39,17 +39,9 @@ import ( "k8s.io/kubernetes/pkg/kubelet/network" proberesults "k8s.io/kubernetes/pkg/kubelet/prober/results" kubetypes "k8s.io/kubernetes/pkg/kubelet/types" + utiltesting "k8s.io/kubernetes/pkg/util/testing" ) -// The temp dir where test plugins will be stored. -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, testVendorCNIDirPrefix, testNetworkConfigPath, vendorName string, plugName string) { pluginDir := path.Join(testNetworkConfigPath, plugName) err := os.MkdirAll(pluginDir, 0777) @@ -173,7 +165,7 @@ func TestCNIPlugin(t *testing.T) { pluginName := fmt.Sprintf("test%d", rand.Intn(1000)) vendorName := fmt.Sprintf("test_vendor%d", rand.Intn(1000)) - tmpDir := tmpDirOrDie() + tmpDir := utiltesting.MkTmpdirOrDie("cni-test") testNetworkConfigPath := path.Join(tmpDir, "plugins", "net", "cni") testVendorCNIDirPrefix := tmpDir defer tearDownPlugin(tmpDir) diff --git a/pkg/kubelet/network/exec/exec_test.go b/pkg/kubelet/network/exec/exec_test.go index f564c952ba..22dccb5a5c 100644 --- a/pkg/kubelet/network/exec/exec_test.go +++ b/pkg/kubelet/network/exec/exec_test.go @@ -31,10 +31,11 @@ import ( "k8s.io/kubernetes/pkg/kubelet/network" "k8s.io/kubernetes/pkg/util/sets" + utiltesting "k8s.io/kubernetes/pkg/util/testing" ) func tmpDirOrDie() string { - dir, err := ioutil.TempDir(os.TempDir(), "exec-test") + dir, err := utiltesting.MkTmpdir("exec-test") if err != nil { panic(fmt.Sprintf("error creating tmp dir: %v", err)) } diff --git a/pkg/kubelet/rkt/rkt_test.go b/pkg/kubelet/rkt/rkt_test.go index 224d4edf0f..3bf7748849 100644 --- a/pkg/kubelet/rkt/rkt_test.go +++ b/pkg/kubelet/rkt/rkt_test.go @@ -19,6 +19,7 @@ package rkt import ( "encoding/json" "fmt" + "os" "sort" "testing" "time" @@ -30,6 +31,7 @@ import ( "k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/api/resource" kubecontainer "k8s.io/kubernetes/pkg/kubelet/container" + utiltesting "k8s.io/kubernetes/pkg/util/testing" ) func mustMarshalPodManifest(man *appcschema.PodManifest) []byte { @@ -755,6 +757,12 @@ func sortAppFields(app *appctypes.App) { } func TestSetApp(t *testing.T) { + tmpDir, err := utiltesting.MkTmpdir("rkt_test") + if err != nil { + t.Fatalf("error creating temp dir: %v", err) + } + defer os.RemoveAll(tmpDir) + rootUser := int64(0) nonRootUser := int64(42) runAsNonRootTrue := true @@ -796,7 +804,7 @@ func TestSetApp(t *testing.T) { container: &api.Container{ Command: []string{"/bin/bar"}, Args: []string{"hello", "world"}, - WorkingDir: "/tmp", + WorkingDir: tmpDir, Resources: api.ResourceRequirements{ Limits: api.ResourceList{"cpu": resource.MustParse("50m"), "memory": resource.MustParse("50M")}, Requests: api.ResourceList{"cpu": resource.MustParse("5m"), "memory": resource.MustParse("5M")}, @@ -830,7 +838,7 @@ func TestSetApp(t *testing.T) { User: "42", Group: "22", SupplementaryGIDs: []int{1, 2, 3}, - WorkingDirectory: "/tmp", + WorkingDirectory: tmpDir, Environment: []appctypes.EnvironmentVariable{ {"env-foo", "bar"}, {"env-bar", "foo"}, @@ -857,7 +865,7 @@ func TestSetApp(t *testing.T) { container: &api.Container{ Command: []string{"/bin/bar"}, Args: []string{"hello", "world"}, - WorkingDir: "/tmp", + WorkingDir: tmpDir, Resources: api.ResourceRequirements{ Limits: api.ResourceList{"cpu": resource.MustParse("50m"), "memory": resource.MustParse("50M")}, Requests: api.ResourceList{"cpu": resource.MustParse("5m"), "memory": resource.MustParse("5M")}, @@ -891,7 +899,7 @@ func TestSetApp(t *testing.T) { User: "42", Group: "22", SupplementaryGIDs: []int{1, 2, 3}, - WorkingDirectory: "/tmp", + WorkingDirectory: tmpDir, Environment: []appctypes.EnvironmentVariable{ {"env-foo", "foo"}, }, diff --git a/pkg/kubelet/runonce_test.go b/pkg/kubelet/runonce_test.go index 7f6c814aca..df7edb71dc 100644 --- a/pkg/kubelet/runonce_test.go +++ b/pkg/kubelet/runonce_test.go @@ -17,7 +17,6 @@ limitations under the License. package kubelet import ( - "io/ioutil" "os" "testing" "time" @@ -32,6 +31,7 @@ import ( "k8s.io/kubernetes/pkg/kubelet/network" kubepod "k8s.io/kubernetes/pkg/kubelet/pod" "k8s.io/kubernetes/pkg/kubelet/status" + utiltesting "k8s.io/kubernetes/pkg/util/testing" ) func TestRunOnce(t *testing.T) { @@ -49,7 +49,7 @@ func TestRunOnce(t *testing.T) { podManager := kubepod.NewBasicPodManager(kubepod.NewFakeMirrorClient()) diskSpaceManager, _ := newDiskSpaceManager(cadvisor, DiskSpacePolicy{}) fakeRuntime := &kubecontainer.FakeRuntime{} - basePath, err := ioutil.TempDir(os.TempDir(), "kubelet") + basePath, err := utiltesting.MkTmpdir("kubelet") if err != nil { t.Fatalf("can't make a temp rootdir %v", err) } diff --git a/pkg/master/tunneler_test.go b/pkg/master/tunneler_test.go index d7ff597134..24822f2e06 100644 --- a/pkg/master/tunneler_test.go +++ b/pkg/master/tunneler_test.go @@ -74,7 +74,7 @@ func TestRefreshTunnels(t *testing.T) { assert := assert.New(t) // Fail case (no addresses associated with nodes) - assert.Error(tunneler.refreshTunnels("test", "/tmp/undefined")) + assert.Error(tunneler.refreshTunnels("test", "/somepath/undefined")) // TODO: pass case without needing actual connections? } diff --git a/pkg/util/io/io_test.go b/pkg/util/io/io_test.go index 466f436b8c..1b9d4c67a3 100644 --- a/pkg/util/io/io_test.go +++ b/pkg/util/io/io_test.go @@ -26,6 +26,7 @@ import ( "k8s.io/kubernetes/pkg/apimachinery/registered" "k8s.io/kubernetes/pkg/runtime" "k8s.io/kubernetes/pkg/util/io" + utiltesting "k8s.io/kubernetes/pkg/util/testing" "k8s.io/kubernetes/pkg/volume" ) @@ -37,8 +38,9 @@ func TestSavePodToFile(t *testing.T) { encoded, err := runtime.Encode(codec, pod) runtime.DecodeInto(codec, encoded, pod) - path := fmt.Sprintf("/tmp/kube-io-test-%s", uuid.New()) - defer os.Remove(path) + tmpDir := utiltesting.MkTmpdirOrDie("kube-io-test") + defer os.RemoveAll(tmpDir) + path := fmt.Sprintf("/%s/kube-io-test-%s", tmpDir, uuid.New()) if err := io.SavePodToFile(pod, path, 777); err != nil { t.Fatalf("failed to save pod to file: %v", err) diff --git a/pkg/util/testing/tmpdir.go b/pkg/util/testing/tmpdir.go new file mode 100644 index 0000000000..8ad6733e66 --- /dev/null +++ b/pkg/util/testing/tmpdir.go @@ -0,0 +1,44 @@ +/* +Copyright 2016 The Kubernetes Authors All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package testing + +import ( + "io/ioutil" + "os" +) + +// MkTmpdir creates a temporary directory based upon the prefix passed in. +// If successful, it returns the temporary directory path. The directory can be +// deleted with a call to "os.RemoveAll(...)". +// In case of error, it'll return an empty string and the error. +func MkTmpdir(prefix string) (string, error) { + tmpDir, err := ioutil.TempDir(os.TempDir(), prefix) + if err != nil { + return "", err + } + return tmpDir, nil +} + +// MkTmpdir does the same work as "MkTmpdir", except in case of +// errors, it'll trigger a panic. +func MkTmpdirOrDie(prefix string) string { + tmpDir, err := MkTmpdir(prefix) + if err != nil { + panic(err) + } + return tmpDir +} diff --git a/pkg/volume/aws_ebs/aws_ebs_test.go b/pkg/volume/aws_ebs/aws_ebs_test.go index 73c34a25b0..52a5f1852e 100644 --- a/pkg/volume/aws_ebs/aws_ebs_test.go +++ b/pkg/volume/aws_ebs/aws_ebs_test.go @@ -18,7 +18,6 @@ package aws_ebs import ( "fmt" - "io/ioutil" "os" "path" "testing" @@ -28,11 +27,12 @@ import ( "k8s.io/kubernetes/pkg/client/unversioned/testclient" "k8s.io/kubernetes/pkg/types" "k8s.io/kubernetes/pkg/util/mount" + utiltesting "k8s.io/kubernetes/pkg/util/testing" "k8s.io/kubernetes/pkg/volume" ) func TestCanSupport(t *testing.T) { - tmpDir, err := ioutil.TempDir(os.TempDir(), "awsebsTest") + tmpDir, err := utiltesting.MkTmpdir("awsebsTest") if err != nil { t.Fatalf("can't make a temp dir: %v", err) } @@ -56,7 +56,7 @@ func TestCanSupport(t *testing.T) { } func TestGetAccessModes(t *testing.T) { - tmpDir, err := ioutil.TempDir(os.TempDir(), "awsebsTest") + tmpDir, err := utiltesting.MkTmpdir("awsebsTest") if err != nil { t.Fatalf("can't make a temp dir: %v", err) } @@ -119,7 +119,7 @@ func (fake *fakePDManager) DeleteVolume(cd *awsElasticBlockStoreDeleter) error { } func TestPlugin(t *testing.T) { - tmpDir, err := ioutil.TempDir(os.TempDir(), "awsebsTest") + tmpDir, err := utiltesting.MkTmpdir("awsebsTest") if err != nil { t.Fatalf("can't make a temp dir: %v") } @@ -260,7 +260,7 @@ func TestPersistentClaimReadOnlyFlag(t *testing.T) { client := testclient.NewSimpleFake(pv, claim) - tmpDir, err := ioutil.TempDir(os.TempDir(), "awsebsTest") + tmpDir, err := utiltesting.MkTmpdir("awsebsTest") if err != nil { t.Fatalf("can't make a temp dir: %v", err) } @@ -280,7 +280,7 @@ func TestPersistentClaimReadOnlyFlag(t *testing.T) { } func TestBuilderAndCleanerTypeAssert(t *testing.T) { - tmpDir, err := ioutil.TempDir(os.TempDir(), "awsebsTest") + tmpDir, err := utiltesting.MkTmpdir("awsebsTest") if err != nil { t.Fatalf("can't make a temp dir: %v", err) } diff --git a/pkg/volume/cephfs/cephfs_test.go b/pkg/volume/cephfs/cephfs_test.go index b2e8d5bf72..8fc8d16d2a 100644 --- a/pkg/volume/cephfs/cephfs_test.go +++ b/pkg/volume/cephfs/cephfs_test.go @@ -17,7 +17,6 @@ limitations under the License. package cephfs import ( - "io/ioutil" "os" "path" "testing" @@ -25,11 +24,12 @@ import ( "k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/types" "k8s.io/kubernetes/pkg/util/mount" + utiltesting "k8s.io/kubernetes/pkg/util/testing" "k8s.io/kubernetes/pkg/volume" ) func TestCanSupport(t *testing.T) { - tmpDir, err := ioutil.TempDir(os.TempDir(), "cephTest") + tmpDir, err := utiltesting.MkTmpdir("cephTest") if err != nil { t.Fatalf("can't make a temp dir: %v", err) } @@ -52,7 +52,7 @@ func TestCanSupport(t *testing.T) { } func TestPlugin(t *testing.T) { - tmpDir, err := ioutil.TempDir(os.TempDir(), "cephTest") + tmpDir, err := utiltesting.MkTmpdir("cephTest") if err != nil { t.Fatalf("can't make a temp dir: %v", err) } diff --git a/pkg/volume/cinder/cinder_test.go b/pkg/volume/cinder/cinder_test.go index 3613ed5c3a..33a426ec48 100644 --- a/pkg/volume/cinder/cinder_test.go +++ b/pkg/volume/cinder/cinder_test.go @@ -18,7 +18,6 @@ package cinder import ( "fmt" - "io/ioutil" "os" "path" "testing" @@ -27,11 +26,12 @@ import ( "k8s.io/kubernetes/pkg/api/resource" "k8s.io/kubernetes/pkg/types" "k8s.io/kubernetes/pkg/util/mount" + utiltesting "k8s.io/kubernetes/pkg/util/testing" "k8s.io/kubernetes/pkg/volume" ) func TestCanSupport(t *testing.T) { - tmpDir, err := ioutil.TempDir(os.TempDir(), "cinderTest") + tmpDir, err := utiltesting.MkTmpdir("cinderTest") if err != nil { t.Fatalf("can't make a temp dir: %v", err) } @@ -87,7 +87,7 @@ func (fake *fakePDManager) DeleteVolume(cd *cinderVolumeDeleter) error { } func TestPlugin(t *testing.T) { - tmpDir, err := ioutil.TempDir(os.TempDir(), "cinderTest") + tmpDir, err := utiltesting.MkTmpdir("cinderTest") if err != nil { t.Fatalf("can't make a temp dir: %v", err) } diff --git a/pkg/volume/downwardapi/downwardapi_test.go b/pkg/volume/downwardapi/downwardapi_test.go index ca857bf504..a94e4e940c 100644 --- a/pkg/volume/downwardapi/downwardapi_test.go +++ b/pkg/volume/downwardapi/downwardapi_test.go @@ -27,6 +27,7 @@ import ( client "k8s.io/kubernetes/pkg/client/unversioned" "k8s.io/kubernetes/pkg/client/unversioned/testclient" "k8s.io/kubernetes/pkg/types" + utiltesting "k8s.io/kubernetes/pkg/util/testing" "k8s.io/kubernetes/pkg/volume" "k8s.io/kubernetes/pkg/volume/empty_dir" ) @@ -39,8 +40,8 @@ func formatMap(m map[string]string) (fmtstr string) { return } -func newTestHost(t *testing.T, client client.Interface, basePath string) (string, volume.VolumeHost) { - tempDir, err := ioutil.TempDir(basePath, "downwardApi_volume_test.") +func newTestHost(t *testing.T, client client.Interface) (string, volume.VolumeHost) { + tempDir, err := utiltesting.MkTmpdir("downwardApi_volume_test.") if err != nil { t.Fatalf("can't make a temp rootdir: %v", err) } @@ -48,13 +49,9 @@ func newTestHost(t *testing.T, client client.Interface, basePath string) (string } 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{} - _, host := newTestHost(t, nil, tmpDir) + tmpDir, host := newTestHost(t, nil) + defer os.RemoveAll(tmpDir) pluginMgr.InitPlugins(ProbeVolumePlugins(), host) plugin, err := pluginMgr.FindPluginByName(downwardAPIPluginName) @@ -105,13 +102,9 @@ func TestLabels(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{} - rootDir, host := newTestHost(t, fake, tmpDir) + rootDir, host := newTestHost(t, fake) + defer os.RemoveAll(rootDir) pluginMgr.InitPlugins(ProbeVolumePlugins(), host) plugin, err := pluginMgr.FindPluginByName(downwardAPIPluginName) volumeSpec := &api.Volume{ @@ -196,13 +189,9 @@ 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{} - _, host := newTestHost(t, fake, tmpDir) + tmpDir, host := newTestHost(t, fake) + defer os.RemoveAll(tmpDir) pluginMgr.InitPlugins(ProbeVolumePlugins(), host) plugin, err := pluginMgr.FindPluginByName(downwardAPIPluginName) if err != nil { @@ -262,13 +251,9 @@ 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{} - _, host := newTestHost(t, fake, tmpDir) + tmpDir, host := newTestHost(t, fake) + defer os.RemoveAll(tmpDir) pluginMgr.InitPlugins(ProbeVolumePlugins(), host) plugin, err := pluginMgr.FindPluginByName(downwardAPIPluginName) if err != nil { @@ -329,13 +314,9 @@ 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{} - _, host := newTestHost(t, fake, tmpDir) + tmpDir, host := newTestHost(t, fake) + defer os.RemoveAll(tmpDir) pluginMgr.InitPlugins(ProbeVolumePlugins(), host) plugin, err := pluginMgr.FindPluginByName(downwardAPIPluginName) if err != nil { @@ -389,13 +370,9 @@ 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{} - _, host := newTestHost(t, fake, tmpDir) + tmpDir, host := newTestHost(t, fake) + defer os.RemoveAll(tmpDir) pluginMgr.InitPlugins(ProbeVolumePlugins(), host) plugin, err := pluginMgr.FindPluginByName(downwardAPIPluginName) volumeSpec := &api.Volume{ @@ -479,13 +456,9 @@ 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{} - _, host := newTestHost(t, fake, tmpDir) + tmpDir, host := newTestHost(t, fake) + defer os.RemoveAll(tmpDir) pluginMgr.InitPlugins(ProbeVolumePlugins(), host) plugin, err := pluginMgr.FindPluginByName(downwardAPIPluginName) volumeSpec := &api.Volume{ @@ -589,13 +562,9 @@ 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{} - _, host := newTestHost(t, fake, tmpDir) + tmpDir, host := newTestHost(t, fake) + defer os.RemoveAll(tmpDir) pluginMgr.InitPlugins(ProbeVolumePlugins(), host) plugin, err := pluginMgr.FindPluginByName(downwardAPIPluginName) volumeSpec := &api.Volume{ @@ -669,13 +638,9 @@ 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{} - _, host := newTestHost(t, fake, tmpDir) + tmpDir, host := newTestHost(t, fake) + defer os.RemoveAll(tmpDir) pluginMgr.InitPlugins(ProbeVolumePlugins(), host) plugin, err := pluginMgr.FindPluginByName(downwardAPIPluginName) if err != nil { diff --git a/pkg/volume/empty_dir/empty_dir_test.go b/pkg/volume/empty_dir/empty_dir_test.go index a1e4044bb4..143f43e4ee 100644 --- a/pkg/volume/empty_dir/empty_dir_test.go +++ b/pkg/volume/empty_dir/empty_dir_test.go @@ -19,7 +19,6 @@ limitations under the License. package empty_dir import ( - "io/ioutil" "os" "path" "testing" @@ -27,6 +26,7 @@ import ( "k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/types" "k8s.io/kubernetes/pkg/util/mount" + utiltesting "k8s.io/kubernetes/pkg/util/testing" "k8s.io/kubernetes/pkg/volume" "k8s.io/kubernetes/pkg/volume/util" ) @@ -44,7 +44,7 @@ func makePluginUnderTest(t *testing.T, plugName, basePath string) volume.VolumeP } func TestCanSupport(t *testing.T) { - tmpDir, err := ioutil.TempDir(os.TempDir(), "emptydirTest") + tmpDir, err := utiltesting.MkTmpdir("emptydirTest") if err != nil { t.Fatalf("can't make a temp dir: %v", err) } @@ -119,7 +119,7 @@ type pluginTestConfig struct { // doTestPlugin sets up a volume and tears it back down. func doTestPlugin(t *testing.T, config pluginTestConfig) { - basePath, err := ioutil.TempDir(os.TempDir(), "emptydir_volume_test") + basePath, err := utiltesting.MkTmpdir("emptydir_volume_test") if err != nil { t.Fatalf("can't make a temp rootdir: %v", err) } @@ -251,7 +251,7 @@ func doTestPlugin(t *testing.T, config pluginTestConfig) { } func TestPluginBackCompat(t *testing.T) { - basePath, err := ioutil.TempDir(os.TempDir(), "emptydirTest") + basePath, err := utiltesting.MkTmpdir("emptydirTest") if err != nil { t.Fatalf("can't make a temp dir: %v", err) } @@ -280,7 +280,7 @@ func TestPluginBackCompat(t *testing.T) { // TestMetrics tests that MetricProvider methods return sane values. func TestMetrics(t *testing.T) { // Create an empty temp directory for the volume - tmpDir, err := ioutil.TempDir(os.TempDir(), "empty_dir_test") + tmpDir, err := utiltesting.MkTmpdir("empty_dir_test") if err != nil { t.Fatalf("Can't make a tmp dir: %v", err) } diff --git a/pkg/volume/fc/fc_test.go b/pkg/volume/fc/fc_test.go index 2d2e29f70c..36628a375c 100644 --- a/pkg/volume/fc/fc_test.go +++ b/pkg/volume/fc/fc_test.go @@ -17,6 +17,7 @@ limitations under the License. package fc import ( + "fmt" "os" "testing" @@ -24,12 +25,19 @@ import ( "k8s.io/kubernetes/pkg/client/unversioned/testclient" "k8s.io/kubernetes/pkg/types" "k8s.io/kubernetes/pkg/util/mount" + utiltesting "k8s.io/kubernetes/pkg/util/testing" "k8s.io/kubernetes/pkg/volume" ) func TestCanSupport(t *testing.T) { + tmpDir, err := utiltesting.MkTmpdir("fc_test") + if err != nil { + t.Fatalf("error creating 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/fc") if err != nil { @@ -44,8 +52,14 @@ func TestCanSupport(t *testing.T) { } func TestGetAccessModes(t *testing.T) { + tmpDir, err := utiltesting.MkTmpdir("fc_test") + if err != nil { + t.Fatalf("error creating 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/fc") if err != nil { @@ -66,12 +80,23 @@ func contains(modes []api.PersistentVolumeAccessMode, mode api.PersistentVolumeA } type fakeDiskManager struct { + tmpDir string attachCalled bool detachCalled bool } +func NewFakeDiskManager() *fakeDiskManager { + return &fakeDiskManager{ + tmpDir: utiltesting.MkTmpdirOrDie("fc_test"), + } +} + +func (fake *fakeDiskManager) Cleanup() { + os.RemoveAll(fake.tmpDir) +} + func (fake *fakeDiskManager) MakeGlobalPDName(disk fcDisk) string { - return "/tmp/fake_fc_path" + return fake.tmpDir } func (fake *fakeDiskManager) AttachDisk(b fcDiskBuilder) error { globalPath := b.manager.MakeGlobalPDName(*b.fcDisk) @@ -98,14 +123,21 @@ func (fake *fakeDiskManager) DetachDisk(c fcDiskCleaner, mntPath string) error { } func doTestPlugin(t *testing.T, spec *volume.Spec) { + tmpDir, err := utiltesting.MkTmpdir("fc_test") + if err != nil { + t.Fatalf("error creating 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/fc") if err != nil { t.Errorf("Can't find the plugin by name") } - fakeManager := &fakeDiskManager{} + fakeManager := NewFakeDiskManager() + defer fakeManager.Cleanup() fakeMounter := &mount.FakeMounter{} builder, err := plug.(*fcPlugin).newBuilderInternal(spec, types.UID("poduid"), fakeManager, fakeMounter) if err != nil { @@ -116,8 +148,9 @@ func doTestPlugin(t *testing.T, spec *volume.Spec) { } path := builder.GetPath() - if path != "/tmp/fake/pods/poduid/volumes/kubernetes.io~fc/vol1" { - t.Errorf("Got unexpected path: %s", path) + expectedPath := fmt.Sprintf("%s/pods/poduid/volumes/kubernetes.io~fc/vol1", tmpDir) + if path != expectedPath { + t.Errorf("Unexpected path, expected %q, got: %q", expectedPath, path) } if err := builder.SetUp(nil); err != nil { @@ -141,8 +174,9 @@ func doTestPlugin(t *testing.T, spec *volume.Spec) { t.Errorf("Attach was not called") } - fakeManager = &fakeDiskManager{} - cleaner, err := plug.(*fcPlugin).newCleanerInternal("vol1", types.UID("poduid"), fakeManager, fakeMounter) + fakeManager2 := NewFakeDiskManager() + defer fakeManager2.Cleanup() + cleaner, err := plug.(*fcPlugin).newCleanerInternal("vol1", types.UID("poduid"), fakeManager2, fakeMounter) if err != nil { t.Errorf("Failed to make a new Cleaner: %v", err) } @@ -158,7 +192,7 @@ func doTestPlugin(t *testing.T, spec *volume.Spec) { } else if !os.IsNotExist(err) { t.Errorf("SetUp() failed: %v", err) } - if !fakeManager.detachCalled { + if !fakeManager2.detachCalled { t.Errorf("Detach was not called") } } @@ -198,6 +232,12 @@ func TestPluginPersistentVolume(t *testing.T) { } func TestPersistentClaimReadOnlyFlag(t *testing.T) { + tmpDir, err := utiltesting.MkTmpdir("fc_test") + if err != nil { + t.Fatalf("error creating temp dir: %v", err) + } + defer os.RemoveAll(tmpDir) + lun := 0 pv := &api.PersistentVolume{ ObjectMeta: api.ObjectMeta{ @@ -233,7 +273,7 @@ func TestPersistentClaimReadOnlyFlag(t *testing.T) { client := testclient.NewSimpleFake(pv, claim) plugMgr := volume.VolumePluginMgr{} - plugMgr.InitPlugins(ProbeVolumePlugins(), volume.NewFakeVolumeHost("/tmp/fake", client, nil)) + plugMgr.InitPlugins(ProbeVolumePlugins(), volume.NewFakeVolumeHost(tmpDir, client, nil)) plug, _ := plugMgr.FindPluginByName(fcPluginName) // readOnly bool is supplied by persistent-claim volume source when its builder creates other volumes diff --git a/pkg/volume/flexvolume/flexvolume_test.go b/pkg/volume/flexvolume/flexvolume_test.go index fe01c7a347..3598c85c0f 100644 --- a/pkg/volume/flexvolume/flexvolume_test.go +++ b/pkg/volume/flexvolume/flexvolume_test.go @@ -28,12 +28,10 @@ import ( "k8s.io/kubernetes/pkg/types" "k8s.io/kubernetes/pkg/util/exec" "k8s.io/kubernetes/pkg/util/mount" + utiltesting "k8s.io/kubernetes/pkg/util/testing" "k8s.io/kubernetes/pkg/volume" ) -// The temp dir where test plugins will be stored. -const testPluginPath = "/tmp/fake/plugins/volume" - const execScriptTempl1 = `#!/bin/bash if [ "$1" == "init" -a $# -eq 1 ]; then echo -n '{ @@ -134,12 +132,12 @@ exit 1 echo -n $@ &> {{.OutputFile}} ` -func installPluginUnderTest(t *testing.T, vendorName string, plugName string, execScriptTempl string, execTemplateData *map[string]interface{}) { +func installPluginUnderTest(t *testing.T, vendorName, plugName, tmpDir string, execScriptTempl string, execTemplateData *map[string]interface{}) { vendoredName := plugName if vendorName != "" { vendoredName = fmt.Sprintf("%s~%s", vendorName, plugName) } - pluginDir := path.Join(testPluginPath, vendoredName) + pluginDir := path.Join(tmpDir, vendoredName) err := os.MkdirAll(pluginDir, 0777) if err != nil { t.Errorf("Failed to create plugin: %v", err) @@ -174,9 +172,15 @@ func installPluginUnderTest(t *testing.T, vendorName string, plugName string, ex } func TestCanSupport(t *testing.T) { + tmpDir, err := utiltesting.MkTmpdir("flexvolume_test") + if err != nil { + t.Fatalf("error creating temp dir: %v", err) + } + defer os.RemoveAll(tmpDir) + plugMgr := volume.VolumePluginMgr{} - installPluginUnderTest(t, "kubernetes.io", "fakeAttacher", execScriptTempl1, nil) - plugMgr.InitPlugins(ProbeVolumePlugins(testPluginPath), volume.NewFakeVolumeHost("fake", nil, nil)) + installPluginUnderTest(t, "kubernetes.io", "fakeAttacher", tmpDir, execScriptTempl1, nil) + plugMgr.InitPlugins(ProbeVolumePlugins(tmpDir), volume.NewFakeVolumeHost("fake", nil, nil)) plugin, err := plugMgr.FindPluginByName("kubernetes.io/fakeAttacher") if err != nil { t.Errorf("Can't find the plugin by name") @@ -196,12 +200,19 @@ func TestCanSupport(t *testing.T) { } func TestGetAccessModes(t *testing.T) { + tmpDir, err := utiltesting.MkTmpdir("flexvolume_test") + if err != nil { + t.Fatalf("error creating temp dir: %v", err) + } + defer os.RemoveAll(tmpDir) + plugMgr := volume.VolumePluginMgr{} - plugMgr.InitPlugins(ProbeVolumePlugins(testPluginPath), volume.NewFakeVolumeHost("fake", nil, nil)) + installPluginUnderTest(t, "kubernetes.io", "fakeAttacher", tmpDir, execScriptTempl1, nil) + plugMgr.InitPlugins(ProbeVolumePlugins(tmpDir), volume.NewFakeVolumeHost(tmpDir, nil, nil)) plugin, err := plugMgr.FindPersistentPluginByName("kubernetes.io/fakeAttacher") if err != nil { - t.Errorf("Can't find the plugin by name") + t.Fatalf("Can't find the plugin by name") } if !contains(plugin.GetAccessModes(), api.ReadWriteOnce) || !contains(plugin.GetAccessModes(), api.ReadOnlyMany) { t.Errorf("Expected two AccessModeTypes: %s and %s", api.ReadWriteOnce, api.ReadOnlyMany) @@ -217,9 +228,10 @@ func contains(modes []api.PersistentVolumeAccessMode, mode api.PersistentVolumeA return false } -func doTestPluginAttachDetach(t *testing.T, spec *volume.Spec) { +func doTestPluginAttachDetach(t *testing.T, spec *volume.Spec, tmpDir string) { plugMgr := volume.VolumePluginMgr{} - plugMgr.InitPlugins(ProbeVolumePlugins(testPluginPath), volume.NewFakeVolumeHost("/tmp/fake", nil, nil)) + installPluginUnderTest(t, "kubernetes.io", "fakeAttacher", tmpDir, execScriptTempl1, nil) + plugMgr.InitPlugins(ProbeVolumePlugins(tmpDir), volume.NewFakeVolumeHost(tmpDir, nil, nil)) plugin, err := plugMgr.FindPluginByName("kubernetes.io/fakeAttacher") if err != nil { t.Errorf("Can't find the plugin by name") @@ -235,8 +247,9 @@ func doTestPluginAttachDetach(t *testing.T, spec *volume.Spec) { t.Errorf("Got a nil Builder") } path := builder.GetPath() - if path != "/tmp/fake/pods/poduid/volumes/kubernetes.io~fakeAttacher/vol1" { - t.Errorf("Got unexpected path: %s", path) + expectedPath := fmt.Sprintf("%s/pods/poduid/volumes/kubernetes.io~fakeAttacher/vol1", tmpDir) + if path != expectedPath { + t.Errorf("Unexpected path, expected %q, got: %q", expectedPath, path) } if err := builder.SetUp(nil); err != nil { t.Errorf("Expected success, got: %v", err) @@ -288,10 +301,16 @@ func doTestPluginAttachDetach(t *testing.T, spec *volume.Spec) { fake.ResetLog() } -func doTestPluginMountUnmount(t *testing.T, spec *volume.Spec) { +func doTestPluginMountUnmount(t *testing.T, spec *volume.Spec, tmpDir string) { + tmpDir, err := utiltesting.MkTmpdir("flexvolume_test") + if err != nil { + t.Fatalf("error creating temp dir: %v", err) + } + defer os.RemoveAll(tmpDir) + plugMgr := volume.VolumePluginMgr{} - installPluginUnderTest(t, "kubernetes.io", "fakeMounter", execScriptTempl2, nil) - plugMgr.InitPlugins(ProbeVolumePlugins(testPluginPath), volume.NewFakeVolumeHost("/tmp/fake", nil, nil)) + installPluginUnderTest(t, "kubernetes.io", "fakeMounter", tmpDir, execScriptTempl2, nil) + plugMgr.InitPlugins(ProbeVolumePlugins(tmpDir), volume.NewFakeVolumeHost(tmpDir, nil, nil)) plugin, err := plugMgr.FindPluginByName("kubernetes.io/fakeMounter") if err != nil { t.Errorf("Can't find the plugin by name") @@ -307,8 +326,9 @@ func doTestPluginMountUnmount(t *testing.T, spec *volume.Spec) { t.Errorf("Got a nil Builder") } path := builder.GetPath() - if path != "/tmp/fake/pods/poduid/volumes/kubernetes.io~fakeMounter/vol1" { - t.Errorf("Got unexpected path: %s", path) + expectedPath := fmt.Sprintf("%s/pods/poduid/volumes/kubernetes.io~fakeMounter/vol1", tmpDir) + if path != expectedPath { + t.Errorf("Unexpected path, expected %q, got: %q", expectedPath, path) } if err := builder.SetUp(nil); err != nil { t.Errorf("Expected success, got: %v", err) @@ -343,22 +363,40 @@ func doTestPluginMountUnmount(t *testing.T, spec *volume.Spec) { } func TestPluginVolumeAttacher(t *testing.T) { + tmpDir, err := utiltesting.MkTmpdir("flexvolume_test") + if err != nil { + t.Fatalf("error creating temp dir: %v", err) + } + defer os.RemoveAll(tmpDir) + vol := &api.Volume{ Name: "vol1", VolumeSource: api.VolumeSource{FlexVolume: &api.FlexVolumeSource{Driver: "kubernetes.io/fakeAttacher", ReadOnly: false}}, } - doTestPluginAttachDetach(t, volume.NewSpecFromVolume(vol)) + doTestPluginAttachDetach(t, volume.NewSpecFromVolume(vol), tmpDir) } func TestPluginVolumeMounter(t *testing.T) { + tmpDir, err := utiltesting.MkTmpdir("flexvolume_test") + if err != nil { + t.Fatalf("error creating temp dir: %v", err) + } + defer os.RemoveAll(tmpDir) + vol := &api.Volume{ Name: "vol1", VolumeSource: api.VolumeSource{FlexVolume: &api.FlexVolumeSource{Driver: "kubernetes.io/fakeMounter", ReadOnly: false}}, } - doTestPluginMountUnmount(t, volume.NewSpecFromVolume(vol)) + doTestPluginMountUnmount(t, volume.NewSpecFromVolume(vol), tmpDir) } func TestPluginPersistentVolume(t *testing.T) { + tmpDir, err := utiltesting.MkTmpdir("flexvolume_test") + if err != nil { + t.Fatalf("error creating temp dir: %v", err) + } + defer os.RemoveAll(tmpDir) + vol := &api.PersistentVolume{ ObjectMeta: api.ObjectMeta{ Name: "vol1", @@ -370,5 +408,5 @@ func TestPluginPersistentVolume(t *testing.T) { }, } - doTestPluginAttachDetach(t, volume.NewSpecFromPersistentVolume(vol, false)) + doTestPluginAttachDetach(t, volume.NewSpecFromPersistentVolume(vol, false), tmpDir) } diff --git a/pkg/volume/flocker/plugin_test.go b/pkg/volume/flocker/plugin_test.go index 10574d5a6a..f2db3544f6 100644 --- a/pkg/volume/flocker/plugin_test.go +++ b/pkg/volume/flocker/plugin_test.go @@ -17,7 +17,6 @@ limitations under the License. package flocker import ( - "io/ioutil" "os" "testing" @@ -25,6 +24,7 @@ import ( "github.com/stretchr/testify/assert" "k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/types" + utiltesting "k8s.io/kubernetes/pkg/util/testing" "k8s.io/kubernetes/pkg/volume" ) @@ -32,7 +32,7 @@ const pluginName = "kubernetes.io/flocker" func newInitializedVolumePlugMgr(t *testing.T) (volume.VolumePluginMgr, string) { plugMgr := volume.VolumePluginMgr{} - dir, err := ioutil.TempDir("", "flocker") + dir, err := utiltesting.MkTmpdir("flocker") assert.NoError(t, err) plugMgr.InitPlugins(ProbeVolumePlugins(), volume.NewFakeVolumeHost(dir, nil, nil)) return plugMgr, dir diff --git a/pkg/volume/gce_pd/gce_pd_test.go b/pkg/volume/gce_pd/gce_pd_test.go index af3f769de6..d1e64f47da 100644 --- a/pkg/volume/gce_pd/gce_pd_test.go +++ b/pkg/volume/gce_pd/gce_pd_test.go @@ -18,7 +18,6 @@ package gce_pd import ( "fmt" - "io/ioutil" "os" "path" "testing" @@ -28,11 +27,12 @@ import ( "k8s.io/kubernetes/pkg/client/unversioned/testclient" "k8s.io/kubernetes/pkg/types" "k8s.io/kubernetes/pkg/util/mount" + utiltesting "k8s.io/kubernetes/pkg/util/testing" "k8s.io/kubernetes/pkg/volume" ) func TestCanSupport(t *testing.T) { - tmpDir, err := ioutil.TempDir(os.TempDir(), "gcepdTest") + tmpDir, err := utiltesting.MkTmpdir("gcepdTest") if err != nil { t.Fatalf("can't make a temp dir: %v", err) } @@ -56,7 +56,7 @@ func TestCanSupport(t *testing.T) { } func TestGetAccessModes(t *testing.T) { - tmpDir, err := ioutil.TempDir(os.TempDir(), "gcepdTest") + tmpDir, err := utiltesting.MkTmpdir("gcepdTest") if err != nil { t.Fatalf("can't make a temp dir: %v", err) } @@ -124,7 +124,7 @@ func (fake *fakePDManager) DeleteVolume(cd *gcePersistentDiskDeleter) error { } func TestPlugin(t *testing.T) { - tmpDir, err := ioutil.TempDir(os.TempDir(), "gcepdTest") + tmpDir, err := utiltesting.MkTmpdir("gcepdTest") if err != nil { t.Fatalf("can't make a temp dir: %v", err) } @@ -275,7 +275,7 @@ func TestPersistentClaimReadOnlyFlag(t *testing.T) { client := testclient.NewSimpleFake(pv, claim) - tmpDir, err := ioutil.TempDir(os.TempDir(), "gcepdTest") + tmpDir, err := utiltesting.MkTmpdir("gcepdTest") if err != nil { t.Fatalf("can't make a temp dir: %v", err) } diff --git a/pkg/volume/glusterfs/glusterfs_test.go b/pkg/volume/glusterfs/glusterfs_test.go index 3987231631..336fffa6aa 100644 --- a/pkg/volume/glusterfs/glusterfs_test.go +++ b/pkg/volume/glusterfs/glusterfs_test.go @@ -17,6 +17,7 @@ limitations under the License. package glusterfs import ( + "fmt" "os" "testing" @@ -25,12 +26,19 @@ import ( "k8s.io/kubernetes/pkg/types" "k8s.io/kubernetes/pkg/util/exec" "k8s.io/kubernetes/pkg/util/mount" + utiltesting "k8s.io/kubernetes/pkg/util/testing" "k8s.io/kubernetes/pkg/volume" ) func TestCanSupport(t *testing.T) { + tmpDir, err := utiltesting.MkTmpdir("glusterfs_test") + if err != nil { + t.Fatalf("error creating 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/glusterfs") if err != nil { t.Errorf("Can't find the plugin by name") @@ -47,8 +55,14 @@ func TestCanSupport(t *testing.T) { } func TestGetAccessModes(t *testing.T) { + tmpDir, err := utiltesting.MkTmpdir("glusterfs_test") + if err != nil { + t.Fatalf("error creating 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/glusterfs") if err != nil { @@ -69,8 +83,14 @@ func contains(modes []api.PersistentVolumeAccessMode, mode api.PersistentVolumeA } func doTestPlugin(t *testing.T, spec *volume.Spec) { + tmpDir, err := utiltesting.MkTmpdir("glusterfs_test") + if err != nil { + t.Fatalf("error creating 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/glusterfs") if err != nil { t.Errorf("Can't find the plugin by name") @@ -101,8 +121,9 @@ func doTestPlugin(t *testing.T, spec *volume.Spec) { t.Error("Got a nil Builder") } path := builder.GetPath() - if path != "/tmp/fake/pods/poduid/volumes/kubernetes.io~glusterfs/vol1" { - t.Errorf("Got unexpected path: %s", path) + expectedPath := fmt.Sprintf("%s/pods/poduid/volumes/kubernetes.io~glusterfs/vol1", tmpDir) + if path != expectedPath { + t.Errorf("Unexpected path, expected %q, got: %q", expectedPath, path) } if err := builder.SetUp(nil); err != nil { t.Errorf("Expected success, got: %v", err) @@ -155,6 +176,12 @@ func TestPluginPersistentVolume(t *testing.T) { } func TestPersistentClaimReadOnlyFlag(t *testing.T) { + tmpDir, err := utiltesting.MkTmpdir("glusterfs_test") + if err != nil { + t.Fatalf("error creating temp dir: %v", err) + } + defer os.RemoveAll(tmpDir) + pv := &api.PersistentVolume{ ObjectMeta: api.ObjectMeta{ Name: "pvA", @@ -195,7 +222,7 @@ func TestPersistentClaimReadOnlyFlag(t *testing.T) { client := testclient.NewSimpleFake(pv, claim, ep) plugMgr := volume.VolumePluginMgr{} - plugMgr.InitPlugins(ProbeVolumePlugins(), volume.NewFakeVolumeHost("/tmp/fake", client, nil)) + plugMgr.InitPlugins(ProbeVolumePlugins(), volume.NewFakeVolumeHost(tmpDir, client, nil)) plug, _ := plugMgr.FindPluginByName(glusterfsPluginName) // readOnly bool is supplied by persistent-claim volume source when its builder creates other volumes diff --git a/pkg/volume/host_path/host_path_test.go b/pkg/volume/host_path/host_path_test.go index 33b1c62818..4c1ad12927 100644 --- a/pkg/volume/host_path/host_path_test.go +++ b/pkg/volume/host_path/host_path_test.go @@ -90,6 +90,7 @@ func TestRecycler(t *testing.T) { } func TestDeleter(t *testing.T) { + // Deleter has a hard-coded regex for "/tmp". tempPath := fmt.Sprintf("/tmp/hostpath/%s", util.NewUUID()) defer os.RemoveAll(tempPath) err := os.MkdirAll(tempPath, 0750) @@ -147,7 +148,7 @@ func TestDeleterTempDir(t *testing.T) { } func TestProvisioner(t *testing.T) { - tempPath := "/tmp/hostpath/" + tempPath := fmt.Sprintf("/tmp/hostpath/%s", util.NewUUID()) defer os.RemoveAll(tempPath) err := os.MkdirAll(tempPath, 0750) diff --git a/pkg/volume/iscsi/iscsi_test.go b/pkg/volume/iscsi/iscsi_test.go index 4cc82e5576..7bc1791595 100644 --- a/pkg/volume/iscsi/iscsi_test.go +++ b/pkg/volume/iscsi/iscsi_test.go @@ -17,6 +17,7 @@ limitations under the License. package iscsi import ( + "fmt" "os" "testing" @@ -24,12 +25,19 @@ import ( "k8s.io/kubernetes/pkg/client/unversioned/testclient" "k8s.io/kubernetes/pkg/types" "k8s.io/kubernetes/pkg/util/mount" + utiltesting "k8s.io/kubernetes/pkg/util/testing" "k8s.io/kubernetes/pkg/volume" ) func TestCanSupport(t *testing.T) { + tmpDir, err := utiltesting.MkTmpdir("iscsi_test") + if err != nil { + t.Fatalf("error creating 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/iscsi") if err != nil { @@ -44,8 +52,14 @@ func TestCanSupport(t *testing.T) { } func TestGetAccessModes(t *testing.T) { + tmpDir, err := utiltesting.MkTmpdir("iscsi_test") + if err != nil { + t.Fatalf("error creating 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/iscsi") if err != nil { @@ -66,12 +80,23 @@ func contains(modes []api.PersistentVolumeAccessMode, mode api.PersistentVolumeA } type fakeDiskManager struct { + tmpDir string attachCalled bool detachCalled bool } +func NewFakeDiskManager() *fakeDiskManager { + return &fakeDiskManager{ + tmpDir: utiltesting.MkTmpdirOrDie("fc_test"), + } +} + +func (fake *fakeDiskManager) Cleanup() { + os.RemoveAll(fake.tmpDir) +} + func (fake *fakeDiskManager) MakeGlobalPDName(disk iscsiDisk) string { - return "/tmp/fake_iscsi_path" + return fake.tmpDir } func (fake *fakeDiskManager) AttachDisk(b iscsiDiskBuilder) error { globalPath := b.manager.MakeGlobalPDName(*b.iscsiDisk) @@ -98,14 +123,21 @@ func (fake *fakeDiskManager) DetachDisk(c iscsiDiskCleaner, mntPath string) erro } func doTestPlugin(t *testing.T, spec *volume.Spec) { + tmpDir, err := utiltesting.MkTmpdir("iscsi_test") + if err != nil { + t.Fatalf("error creating 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/iscsi") if err != nil { t.Errorf("Can't find the plugin by name") } - fakeManager := &fakeDiskManager{} + fakeManager := NewFakeDiskManager() + defer fakeManager.Cleanup() fakeMounter := &mount.FakeMounter{} builder, err := plug.(*iscsiPlugin).newBuilderInternal(spec, types.UID("poduid"), fakeManager, fakeMounter) if err != nil { @@ -116,8 +148,9 @@ func doTestPlugin(t *testing.T, spec *volume.Spec) { } path := builder.GetPath() - if path != "/tmp/fake/pods/poduid/volumes/kubernetes.io~iscsi/vol1" { - t.Errorf("Got unexpected path: %s", path) + expectedPath := fmt.Sprintf("%s/pods/poduid/volumes/kubernetes.io~iscsi/vol1", tmpDir) + if path != expectedPath { + t.Errorf("Unexpected path, expected %q, got: %q", expectedPath, path) } if err := builder.SetUp(nil); err != nil { @@ -141,8 +174,9 @@ func doTestPlugin(t *testing.T, spec *volume.Spec) { t.Errorf("Attach was not called") } - fakeManager = &fakeDiskManager{} - cleaner, err := plug.(*iscsiPlugin).newCleanerInternal("vol1", types.UID("poduid"), fakeManager, fakeMounter) + fakeManager2 := NewFakeDiskManager() + defer fakeManager2.Cleanup() + cleaner, err := plug.(*iscsiPlugin).newCleanerInternal("vol1", types.UID("poduid"), fakeManager2, fakeMounter) if err != nil { t.Errorf("Failed to make a new Cleaner: %v", err) } @@ -158,7 +192,7 @@ func doTestPlugin(t *testing.T, spec *volume.Spec) { } else if !os.IsNotExist(err) { t.Errorf("SetUp() failed: %v", err) } - if !fakeManager.detachCalled { + if !fakeManager2.detachCalled { t.Errorf("Detach was not called") } } @@ -198,6 +232,12 @@ func TestPluginPersistentVolume(t *testing.T) { } func TestPersistentClaimReadOnlyFlag(t *testing.T) { + tmpDir, err := utiltesting.MkTmpdir("iscsi_test") + if err != nil { + t.Fatalf("error creating temp dir: %v", err) + } + defer os.RemoveAll(tmpDir) + pv := &api.PersistentVolume{ ObjectMeta: api.ObjectMeta{ Name: "pvA", @@ -233,7 +273,7 @@ func TestPersistentClaimReadOnlyFlag(t *testing.T) { client := testclient.NewSimpleFake(pv, claim) plugMgr := volume.VolumePluginMgr{} - plugMgr.InitPlugins(ProbeVolumePlugins(), volume.NewFakeVolumeHost("/tmp/fake", client, nil)) + plugMgr.InitPlugins(ProbeVolumePlugins(), volume.NewFakeVolumeHost(tmpDir, client, nil)) plug, _ := plugMgr.FindPluginByName(iscsiPluginName) // readOnly bool is supplied by persistent-claim volume source when its builder creates other volumes diff --git a/pkg/volume/metrics_du_test.go b/pkg/volume/metrics_du_test.go index 826bdcf127..3827f5dedb 100644 --- a/pkg/volume/metrics_du_test.go +++ b/pkg/volume/metrics_du_test.go @@ -23,6 +23,8 @@ import ( "os" "path/filepath" "testing" + + utiltesting "k8s.io/kubernetes/pkg/util/testing" ) const expectedBlockSize = 4096 @@ -30,7 +32,7 @@ const expectedBlockSize = 4096 // TestMetricsDuGetCapacity tests that MetricsDu can read disk usage // for path func TestMetricsDuGetCapacity(t *testing.T) { - tmpDir, err := ioutil.TempDir(os.TempDir(), "metrics_du_test") + tmpDir, err := utiltesting.MkTmpdir("metrics_du_test") if err != nil { t.Fatalf("Can't make a tmp dir: %v", err) } diff --git a/pkg/volume/nfs/nfs_test.go b/pkg/volume/nfs/nfs_test.go index 781b82c7ae..94ed824b8d 100644 --- a/pkg/volume/nfs/nfs_test.go +++ b/pkg/volume/nfs/nfs_test.go @@ -17,6 +17,7 @@ limitations under the License. package nfs import ( + "fmt" "os" "testing" @@ -24,12 +25,19 @@ import ( "k8s.io/kubernetes/pkg/client/unversioned/testclient" "k8s.io/kubernetes/pkg/types" "k8s.io/kubernetes/pkg/util/mount" + utiltesting "k8s.io/kubernetes/pkg/util/testing" "k8s.io/kubernetes/pkg/volume" ) func TestCanSupport(t *testing.T) { + tmpDir, err := utiltesting.MkTmpdir("nfs_test") + if err != nil { + t.Fatalf("error creating temp dir: %v", err) + } + defer os.RemoveAll(tmpDir) + plugMgr := volume.VolumePluginMgr{} - plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), volume.NewFakeVolumeHost("fake", nil, nil)) + plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), volume.NewFakeVolumeHost(tmpDir, nil, nil)) plug, err := plugMgr.FindPluginByName("kubernetes.io/nfs") if err != nil { t.Errorf("Can't find the plugin by name") @@ -49,8 +57,14 @@ func TestCanSupport(t *testing.T) { } func TestGetAccessModes(t *testing.T) { + tmpDir, err := utiltesting.MkTmpdir("nfs_test") + if err != nil { + t.Fatalf("error creating temp dir: %v", err) + } + defer os.RemoveAll(tmpDir) + plugMgr := volume.VolumePluginMgr{} - plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), volume.NewFakeVolumeHost("/tmp/fake", nil, nil)) + plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), volume.NewFakeVolumeHost(tmpDir, nil, nil)) plug, err := plugMgr.FindPersistentPluginByName("kubernetes.io/nfs") if err != nil { @@ -62,8 +76,14 @@ func TestGetAccessModes(t *testing.T) { } func TestRecycler(t *testing.T) { + tmpDir, err := utiltesting.MkTmpdir("nfs_test") + if err != nil { + t.Fatalf("error creating temp dir: %v", err) + } + defer os.RemoveAll(tmpDir) + plugMgr := volume.VolumePluginMgr{} - plugMgr.InitPlugins([]volume.VolumePlugin{&nfsPlugin{nil, newMockRecycler, volume.VolumeConfig{}}}, volume.NewFakeVolumeHost("/tmp/fake", nil, nil)) + plugMgr.InitPlugins([]volume.VolumePlugin{&nfsPlugin{nil, newMockRecycler, volume.VolumeConfig{}}}, volume.NewFakeVolumeHost(tmpDir, nil, nil)) spec := &volume.Spec{PersistentVolume: &api.PersistentVolume{Spec: api.PersistentVolumeSpec{PersistentVolumeSource: api.PersistentVolumeSource{NFS: &api.NFSVolumeSource{Path: "/foo"}}}}} plug, err := plugMgr.FindRecyclablePluginBySpec(spec) @@ -113,8 +133,14 @@ func contains(modes []api.PersistentVolumeAccessMode, mode api.PersistentVolumeA } func doTestPlugin(t *testing.T, spec *volume.Spec) { + tmpDir, err := utiltesting.MkTmpdir("nfs_test") + if err != nil { + t.Fatalf("error creating temp dir: %v", err) + } + defer os.RemoveAll(tmpDir) + plugMgr := volume.VolumePluginMgr{} - plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), volume.NewFakeVolumeHost("/tmp/fake", nil, nil)) + plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), volume.NewFakeVolumeHost(tmpDir, nil, nil)) plug, err := plugMgr.FindPluginByName("kubernetes.io/nfs") if err != nil { t.Errorf("Can't find the plugin by name") @@ -130,8 +156,9 @@ func doTestPlugin(t *testing.T, spec *volume.Spec) { t.Errorf("Got a nil Builder") } path := builder.GetPath() - if path != "/tmp/fake/pods/poduid/volumes/kubernetes.io~nfs/vol1" { - t.Errorf("Got unexpected path: %s", path) + expectedPath := fmt.Sprintf("%s/pods/poduid/volumes/kubernetes.io~nfs/vol1", tmpDir) + if path != expectedPath { + t.Errorf("Unexpected path, expected %q, got: %q", expectedPath, path) } if err := builder.SetUp(nil); err != nil { t.Errorf("Expected success, got: %v", err) @@ -184,7 +211,7 @@ func doTestPlugin(t *testing.T, spec *volume.Spec) { func TestPluginVolume(t *testing.T) { vol := &api.Volume{ Name: "vol1", - VolumeSource: api.VolumeSource{NFS: &api.NFSVolumeSource{Server: "localhost", Path: "/tmp", ReadOnly: false}}, + VolumeSource: api.VolumeSource{NFS: &api.NFSVolumeSource{Server: "localhost", Path: "/somepath", ReadOnly: false}}, } doTestPlugin(t, volume.NewSpecFromVolume(vol)) } @@ -196,7 +223,7 @@ func TestPluginPersistentVolume(t *testing.T) { }, Spec: api.PersistentVolumeSpec{ PersistentVolumeSource: api.PersistentVolumeSource{ - NFS: &api.NFSVolumeSource{Server: "localhost", Path: "/tmp", ReadOnly: false}, + NFS: &api.NFSVolumeSource{Server: "localhost", Path: "/somepath", ReadOnly: false}, }, }, } @@ -205,6 +232,12 @@ func TestPluginPersistentVolume(t *testing.T) { } func TestPersistentClaimReadOnlyFlag(t *testing.T) { + tmpDir, err := utiltesting.MkTmpdir("nfs_test") + if err != nil { + t.Fatalf("error creating temp dir: %v", err) + } + defer os.RemoveAll(tmpDir) + pv := &api.PersistentVolume{ ObjectMeta: api.ObjectMeta{ Name: "pvA", @@ -235,7 +268,7 @@ func TestPersistentClaimReadOnlyFlag(t *testing.T) { client := testclient.NewSimpleFake(pv, claim) plugMgr := volume.VolumePluginMgr{} - plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), volume.NewFakeVolumeHost("/tmp/fake", client, nil)) + plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), volume.NewFakeVolumeHost(tmpDir, client, nil)) plug, _ := plugMgr.FindPluginByName(nfsPluginName) // readOnly bool is supplied by persistent-claim volume source when its builder creates other volumes diff --git a/pkg/volume/persistent_claim/persistent_claim_test.go b/pkg/volume/persistent_claim/persistent_claim_test.go index 2a1c45cc3a..dd597ba77e 100644 --- a/pkg/volume/persistent_claim/persistent_claim_test.go +++ b/pkg/volume/persistent_claim/persistent_claim_test.go @@ -18,7 +18,7 @@ package persistent_claim import ( "fmt" - "io/ioutil" + "os" "strings" "testing" @@ -27,22 +27,25 @@ import ( "k8s.io/kubernetes/pkg/client/unversioned/testclient" "k8s.io/kubernetes/pkg/types" utilstrings "k8s.io/kubernetes/pkg/util/strings" + utiltesting "k8s.io/kubernetes/pkg/util/testing" "k8s.io/kubernetes/pkg/volume" "k8s.io/kubernetes/pkg/volume/gce_pd" "k8s.io/kubernetes/pkg/volume/host_path" ) -func newTestHost(t *testing.T, fakeKubeClient client.Interface) volume.VolumeHost { - tempDir, err := ioutil.TempDir("/tmp", "persistent_volume_test.") +// newTestHost returns the temp directory and the VolumeHost created. +// Please be sure to cleanup the temp directory once done! +func newTestHost(t *testing.T, fakeKubeClient client.Interface) (string, volume.VolumeHost) { + tempDir, err := utiltesting.MkTmpdir("persistent_volume_test.") if err != nil { t.Fatalf("can't make a temp rootdir: %v", err) } - return volume.NewFakeVolumeHost(tempDir, fakeKubeClient, testProbeVolumePlugins()) + return tempDir, volume.NewFakeVolumeHost(tempDir, fakeKubeClient, testProbeVolumePlugins()) } func TestCanSupport(t *testing.T) { plugMgr := volume.VolumePluginMgr{} - plugMgr.InitPlugins(ProbeVolumePlugins(), volume.NewFakeVolumeHost("/tmp/fake", nil, ProbeVolumePlugins())) + plugMgr.InitPlugins(ProbeVolumePlugins(), volume.NewFakeVolumeHost("/somepath/fake", nil, ProbeVolumePlugins())) plug, err := plugMgr.FindPluginByName("kubernetes.io/persistent-claim") if err != nil { @@ -119,7 +122,7 @@ func TestNewBuilder(t *testing.T) { }, Spec: api.PersistentVolumeSpec{ PersistentVolumeSource: api.PersistentVolumeSource{ - HostPath: &api.HostPathVolumeSource{Path: "/tmp"}, + HostPath: &api.HostPathVolumeSource{Path: "/somepath"}, }, ClaimRef: &api.ObjectReference{ Name: "claimB", @@ -143,8 +146,8 @@ func TestNewBuilder(t *testing.T) { }, plugin: host_path.ProbeVolumePlugins(volume.VolumeConfig{})[0], testFunc: func(builder volume.Builder, plugin volume.VolumePlugin) error { - if builder.GetPath() != "/tmp" { - return fmt.Errorf("Expected HostPath.Path /tmp, got: %s", builder.GetPath()) + if builder.GetPath() != "/somepath" { + return fmt.Errorf("Expected HostPath.Path /somepath, got: %s", builder.GetPath()) } return nil }, @@ -237,7 +240,9 @@ func TestNewBuilder(t *testing.T) { client := testclient.NewSimpleFake(item.pv, item.claim) plugMgr := volume.VolumePluginMgr{} - plugMgr.InitPlugins(testProbeVolumePlugins(), newTestHost(t, client)) + tempDir, vh := newTestHost(t, client) + defer os.RemoveAll(tempDir) + plugMgr.InitPlugins(testProbeVolumePlugins(), vh) plug, err := plugMgr.FindPluginByName("kubernetes.io/persistent-claim") if err != nil { @@ -288,7 +293,9 @@ func TestNewBuilderClaimNotBound(t *testing.T) { client := testclient.NewSimpleFake(pv, claim) plugMgr := volume.VolumePluginMgr{} - plugMgr.InitPlugins(testProbeVolumePlugins(), newTestHost(t, client)) + tempDir, vh := newTestHost(t, client) + defer os.RemoveAll(tempDir) + plugMgr.InitPlugins(testProbeVolumePlugins(), vh) plug, err := plugMgr.FindPluginByName("kubernetes.io/persistent-claim") if err != nil { diff --git a/pkg/volume/rbd/rbd_test.go b/pkg/volume/rbd/rbd_test.go index c71f29e4a6..fda920b13f 100644 --- a/pkg/volume/rbd/rbd_test.go +++ b/pkg/volume/rbd/rbd_test.go @@ -17,6 +17,7 @@ limitations under the License. package rbd import ( + "fmt" "os" "testing" @@ -24,12 +25,19 @@ import ( "k8s.io/kubernetes/pkg/client/unversioned/testclient" "k8s.io/kubernetes/pkg/types" "k8s.io/kubernetes/pkg/util/mount" + utiltesting "k8s.io/kubernetes/pkg/util/testing" "k8s.io/kubernetes/pkg/volume" ) func TestCanSupport(t *testing.T) { + tmpDir, err := utiltesting.MkTmpdir("rbd_test") + if err != nil { + t.Fatalf("error creating 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/rbd") if err != nil { @@ -43,10 +51,22 @@ func TestCanSupport(t *testing.T) { } } -type fakeDiskManager struct{} +type fakeDiskManager struct { + tmpDir string +} + +func NewFakeDiskManager() *fakeDiskManager { + return &fakeDiskManager{ + tmpDir: utiltesting.MkTmpdirOrDie("rbd_test"), + } +} + +func (fake *fakeDiskManager) Cleanup() { + os.RemoveAll(fake.tmpDir) +} func (fake *fakeDiskManager) MakeGlobalPDName(disk rbd) string { - return "/tmp/fake_rbd_path" + return fake.tmpDir } func (fake *fakeDiskManager) AttachDisk(b rbdBuilder) error { globalPath := b.manager.MakeGlobalPDName(*b.rbd) @@ -67,14 +87,22 @@ func (fake *fakeDiskManager) DetachDisk(c rbdCleaner, mntPath string) error { } func doTestPlugin(t *testing.T, spec *volume.Spec) { + tmpDir, err := utiltesting.MkTmpdir("rbd_test") + if err != nil { + t.Fatalf("error creating 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/rbd") if err != nil { t.Errorf("Can't find the plugin by name") } - builder, err := plug.(*rbdPlugin).newBuilderInternal(spec, types.UID("poduid"), &fakeDiskManager{}, &mount.FakeMounter{}, "secrets") + fdm := NewFakeDiskManager() + defer fdm.Cleanup() + builder, err := plug.(*rbdPlugin).newBuilderInternal(spec, types.UID("poduid"), fdm, &mount.FakeMounter{}, "secrets") if err != nil { t.Errorf("Failed to make a new Builder: %v", err) } @@ -83,8 +111,9 @@ func doTestPlugin(t *testing.T, spec *volume.Spec) { } path := builder.GetPath() - if path != "/tmp/fake/pods/poduid/volumes/kubernetes.io~rbd/vol1" { - t.Errorf("Got unexpected path: %s", path) + expectedPath := fmt.Sprintf("%s/pods/poduid/volumes/kubernetes.io~rbd/vol1", tmpDir) + if path != expectedPath { + t.Errorf("Unexpected path, expected %q, got: %q", expectedPath, path) } if err := builder.SetUp(nil); err != nil { @@ -105,7 +134,7 @@ func doTestPlugin(t *testing.T, spec *volume.Spec) { } } - cleaner, err := plug.(*rbdPlugin).newCleanerInternal("vol1", types.UID("poduid"), &fakeDiskManager{}, &mount.FakeMounter{}) + cleaner, err := plug.(*rbdPlugin).newCleanerInternal("vol1", types.UID("poduid"), fdm, &mount.FakeMounter{}) if err != nil { t.Errorf("Failed to make a new Cleaner: %v", err) } @@ -156,6 +185,12 @@ func TestPluginPersistentVolume(t *testing.T) { } func TestPersistentClaimReadOnlyFlag(t *testing.T) { + tmpDir, err := utiltesting.MkTmpdir("rbd_test") + if err != nil { + t.Fatalf("error creating temp dir: %v", err) + } + defer os.RemoveAll(tmpDir) + pv := &api.PersistentVolume{ ObjectMeta: api.ObjectMeta{ Name: "pvA", @@ -190,7 +225,7 @@ func TestPersistentClaimReadOnlyFlag(t *testing.T) { client := testclient.NewSimpleFake(pv, claim) plugMgr := volume.VolumePluginMgr{} - plugMgr.InitPlugins(ProbeVolumePlugins(), volume.NewFakeVolumeHost("/tmp/fake", client, nil)) + plugMgr.InitPlugins(ProbeVolumePlugins(), volume.NewFakeVolumeHost(tmpDir, client, nil)) plug, _ := plugMgr.FindPluginByName(rbdPluginName) // readOnly bool is supplied by persistent-claim volume source when its builder creates other volumes