Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automated cherry pick of #15475 #20534

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 16 additions & 12 deletions pkg/kubelet/network/cni/cni_test.go
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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))
Expand Down
10 changes: 8 additions & 2 deletions pkg/kubelet/runonce_test.go
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package kubelet

import (
"io/ioutil"
"os"
"testing"
"time"

Expand All @@ -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{},
Expand Down
41 changes: 34 additions & 7 deletions pkg/volume/aws_ebs/aws_ebs_test.go
Expand Up @@ -17,7 +17,9 @@ limitations under the License.
package aws_ebs

import (
"io/ioutil"
"os"
"path"
"testing"

"k8s.io/kubernetes/pkg/api"
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}

Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
19 changes: 16 additions & 3 deletions pkg/volume/cephfs/cephfs_test.go
Expand Up @@ -17,7 +17,9 @@ limitations under the License.
package cephfs

import (
"io/ioutil"
"os"
"path"
"testing"

"k8s.io/kubernetes/pkg/api"
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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 {
Expand Down
20 changes: 16 additions & 4 deletions pkg/volume/cinder/cinder_test.go
Expand Up @@ -17,7 +17,9 @@ limitations under the License.
package cinder

import (
"io/ioutil"
"os"
"path"
"testing"

"k8s.io/kubernetes/pkg/api"
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}

Expand Down