2015-12-16 22:16:30 +00:00
|
|
|
|
// +build linux
|
|
|
|
|
|
2014-11-23 15:47:25 +00:00
|
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
|
Copyright 2014 The Kubernetes Authors.
|
2014-11-23 15:47:25 +00:00
|
|
|
|
|
|
|
|
|
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 empty_dir
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"os"
|
2015-03-07 20:35:00 +00:00
|
|
|
|
"path"
|
2014-11-23 15:47:25 +00:00
|
|
|
|
"testing"
|
|
|
|
|
|
2017-06-22 18:24:23 +00:00
|
|
|
|
"k8s.io/api/core/v1"
|
2017-01-17 03:38:19 +00:00
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2017-01-11 14:09:48 +00:00
|
|
|
|
"k8s.io/apimachinery/pkg/types"
|
2017-01-23 18:37:22 +00:00
|
|
|
|
utiltesting "k8s.io/client-go/util/testing"
|
2015-08-05 22:03:47 +00:00
|
|
|
|
"k8s.io/kubernetes/pkg/util/mount"
|
|
|
|
|
"k8s.io/kubernetes/pkg/volume"
|
2016-02-29 21:31:42 +00:00
|
|
|
|
volumetest "k8s.io/kubernetes/pkg/volume/testing"
|
2015-08-05 22:03:47 +00:00
|
|
|
|
"k8s.io/kubernetes/pkg/volume/util"
|
2014-11-23 15:47:25 +00:00
|
|
|
|
)
|
|
|
|
|
|
2015-03-07 20:35:00 +00:00
|
|
|
|
// Construct an instance of a plugin, by name.
|
2016-10-24 22:45:42 +00:00
|
|
|
|
func makePluginUnderTest(t *testing.T, plugName, basePath string) volume.VolumePlugin {
|
2015-03-19 05:18:31 +00:00
|
|
|
|
plugMgr := volume.VolumePluginMgr{}
|
2016-10-25 00:29:41 +00:00
|
|
|
|
plugMgr.InitPlugins(ProbeVolumePlugins(), volumetest.NewFakeVolumeHost(basePath, nil, nil))
|
2014-11-23 15:47:25 +00:00
|
|
|
|
|
2015-03-07 20:35:00 +00:00
|
|
|
|
plug, err := plugMgr.FindPluginByName(plugName)
|
2014-11-23 15:47:25 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
t.Errorf("Can't find the plugin by name")
|
|
|
|
|
}
|
2015-03-07 20:35:00 +00:00
|
|
|
|
return plug
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCanSupport(t *testing.T) {
|
2016-01-25 21:57:42 +00:00
|
|
|
|
tmpDir, err := utiltesting.MkTmpdir("emptydirTest")
|
2015-10-12 12:28:03 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("can't make a temp dir: %v", err)
|
|
|
|
|
}
|
|
|
|
|
defer os.RemoveAll(tmpDir)
|
2016-10-24 22:45:42 +00:00
|
|
|
|
plug := makePluginUnderTest(t, "kubernetes.io/empty-dir", tmpDir)
|
2015-03-07 20:35:00 +00:00
|
|
|
|
|
2016-05-30 02:22:22 +00:00
|
|
|
|
if plug.GetPluginName() != "kubernetes.io/empty-dir" {
|
|
|
|
|
t.Errorf("Wrong name: %s", plug.GetPluginName())
|
2014-11-23 15:47:25 +00:00
|
|
|
|
}
|
2016-11-18 20:58:56 +00:00
|
|
|
|
if !plug.CanSupport(&volume.Spec{Volume: &v1.Volume{VolumeSource: v1.VolumeSource{EmptyDir: &v1.EmptyDirVolumeSource{}}}}) {
|
2014-11-23 15:47:25 +00:00
|
|
|
|
t.Errorf("Expected true")
|
|
|
|
|
}
|
2016-11-18 20:58:56 +00:00
|
|
|
|
if plug.CanSupport(&volume.Spec{Volume: &v1.Volume{VolumeSource: v1.VolumeSource{}}}) {
|
2015-04-14 16:29:33 +00:00
|
|
|
|
t.Errorf("Expected false")
|
2014-11-23 15:47:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-16 23:17:47 +00:00
|
|
|
|
type fakeMountDetector struct {
|
|
|
|
|
medium storageMedium
|
|
|
|
|
isMount bool
|
2015-03-07 20:35:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-16 23:17:47 +00:00
|
|
|
|
func (fake *fakeMountDetector) GetMountMedium(path string) (storageMedium, bool, error) {
|
|
|
|
|
return fake.medium, fake.isMount, nil
|
2015-03-07 20:35:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-07 16:40:55 +00:00
|
|
|
|
func TestPluginEmptyRootContext(t *testing.T) {
|
|
|
|
|
doTestPlugin(t, pluginTestConfig{
|
2016-11-18 20:58:56 +00:00
|
|
|
|
medium: v1.StorageMediumDefault,
|
2015-07-07 16:40:55 +00:00
|
|
|
|
expectedSetupMounts: 0,
|
|
|
|
|
expectedTeardownMounts: 0})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type pluginTestConfig struct {
|
2016-11-18 20:58:56 +00:00
|
|
|
|
medium v1.StorageMedium
|
2015-07-07 16:40:55 +00:00
|
|
|
|
idempotent bool
|
|
|
|
|
expectedSetupMounts int
|
|
|
|
|
shouldBeMountedBeforeTeardown bool
|
|
|
|
|
expectedTeardownMounts int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// doTestPlugin sets up a volume and tears it back down.
|
|
|
|
|
func doTestPlugin(t *testing.T, config pluginTestConfig) {
|
2016-01-25 21:57:42 +00:00
|
|
|
|
basePath, err := utiltesting.MkTmpdir("emptydir_volume_test")
|
2015-03-07 20:35:00 +00:00
|
|
|
|
if err != nil {
|
2015-10-12 12:28:03 +00:00
|
|
|
|
t.Fatalf("can't make a temp rootdir: %v", err)
|
2015-03-07 20:35:00 +00:00
|
|
|
|
}
|
2015-10-12 12:28:03 +00:00
|
|
|
|
defer os.RemoveAll(basePath)
|
2015-03-07 20:35:00 +00:00
|
|
|
|
|
2015-07-07 16:40:55 +00:00
|
|
|
|
var (
|
|
|
|
|
volumePath = path.Join(basePath, "pods/poduid/volumes/kubernetes.io~empty-dir/test-volume")
|
|
|
|
|
metadataDir = path.Join(basePath, "pods/poduid/plugins/kubernetes.io~empty-dir/test-volume")
|
2015-03-07 20:35:00 +00:00
|
|
|
|
|
2016-10-24 22:45:42 +00:00
|
|
|
|
plug = makePluginUnderTest(t, "kubernetes.io/empty-dir", basePath)
|
2015-07-07 16:40:55 +00:00
|
|
|
|
volumeName = "test-volume"
|
2016-11-18 20:58:56 +00:00
|
|
|
|
spec = &v1.Volume{
|
2015-07-07 16:40:55 +00:00
|
|
|
|
Name: volumeName,
|
2016-11-18 20:58:56 +00:00
|
|
|
|
VolumeSource: v1.VolumeSource{EmptyDir: &v1.EmptyDirVolumeSource{Medium: config.medium}},
|
2015-07-07 16:40:55 +00:00
|
|
|
|
}
|
2015-03-07 20:35:00 +00:00
|
|
|
|
|
2016-03-23 05:12:21 +00:00
|
|
|
|
physicalMounter = mount.FakeMounter{}
|
|
|
|
|
mountDetector = fakeMountDetector{}
|
2017-01-17 03:38:19 +00:00
|
|
|
|
pod = &v1.Pod{ObjectMeta: metav1.ObjectMeta{UID: types.UID("poduid")}}
|
2015-07-07 16:40:55 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if config.idempotent {
|
2016-03-23 05:12:21 +00:00
|
|
|
|
physicalMounter.MountPoints = []mount.MountPoint{
|
2015-07-07 16:40:55 +00:00
|
|
|
|
{
|
|
|
|
|
Path: volumePath,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
util.SetReady(metadataDir)
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-23 05:12:21 +00:00
|
|
|
|
mounter, err := plug.(*emptyDirPlugin).newMounterInternal(volume.NewSpecFromVolume(spec),
|
2015-07-07 16:40:55 +00:00
|
|
|
|
pod,
|
2016-03-23 05:12:21 +00:00
|
|
|
|
&physicalMounter,
|
2015-07-07 16:40:55 +00:00
|
|
|
|
&mountDetector,
|
2016-05-30 02:22:22 +00:00
|
|
|
|
volume.VolumeOptions{})
|
2014-11-23 15:47:25 +00:00
|
|
|
|
if err != nil {
|
2016-03-23 05:12:21 +00:00
|
|
|
|
t.Errorf("Failed to make a new Mounter: %v", err)
|
2014-11-23 15:47:25 +00:00
|
|
|
|
}
|
2016-03-23 05:12:21 +00:00
|
|
|
|
if mounter == nil {
|
|
|
|
|
t.Errorf("Got a nil Mounter")
|
2014-11-23 15:47:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-23 05:12:21 +00:00
|
|
|
|
volPath := mounter.GetPath()
|
2015-07-07 16:40:55 +00:00
|
|
|
|
if volPath != volumePath {
|
2015-03-07 20:35:00 +00:00
|
|
|
|
t.Errorf("Got unexpected path: %s", volPath)
|
2014-11-23 15:47:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-23 05:12:21 +00:00
|
|
|
|
if err := mounter.SetUp(nil); err != nil {
|
2014-11-23 15:47:25 +00:00
|
|
|
|
t.Errorf("Expected success, got: %v", err)
|
|
|
|
|
}
|
2015-07-07 16:40:55 +00:00
|
|
|
|
|
|
|
|
|
// Stat the directory and check the permission bits
|
|
|
|
|
fileinfo, err := os.Stat(volPath)
|
|
|
|
|
if !config.idempotent {
|
|
|
|
|
if err != nil {
|
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
|
t.Errorf("SetUp() failed, volume path not created: %s", volPath)
|
|
|
|
|
} else {
|
|
|
|
|
t.Errorf("SetUp() failed: %v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if e, a := perm, fileinfo.Mode().Perm(); e != a {
|
|
|
|
|
t.Errorf("Unexpected file mode for %v: expected: %v, got: %v", volPath, e, a)
|
2014-11-23 15:47:25 +00:00
|
|
|
|
}
|
2015-07-07 16:40:55 +00:00
|
|
|
|
} else if err == nil {
|
|
|
|
|
// If this test is for idempotency and we were able
|
|
|
|
|
// to stat the volume path, it's an error.
|
|
|
|
|
t.Errorf("Volume directory was created unexpectedly")
|
2014-11-23 15:47:25 +00:00
|
|
|
|
}
|
2015-07-07 16:40:55 +00:00
|
|
|
|
|
|
|
|
|
// Check the number of mounts performed during setup
|
2016-03-23 05:12:21 +00:00
|
|
|
|
if e, a := config.expectedSetupMounts, len(physicalMounter.Log); e != a {
|
|
|
|
|
t.Errorf("Expected %v physicalMounter calls during setup, got %v", e, a)
|
2015-07-07 16:40:55 +00:00
|
|
|
|
} else if config.expectedSetupMounts == 1 &&
|
2016-03-23 05:12:21 +00:00
|
|
|
|
(physicalMounter.Log[0].Action != mount.FakeActionMount || physicalMounter.Log[0].FSType != "tmpfs") {
|
|
|
|
|
t.Errorf("Unexpected physicalMounter action during setup: %#v", physicalMounter.Log[0])
|
2015-03-07 20:35:00 +00:00
|
|
|
|
}
|
2016-03-23 05:12:21 +00:00
|
|
|
|
physicalMounter.ResetLog()
|
2014-11-23 15:47:25 +00:00
|
|
|
|
|
2016-10-03 09:29:50 +00:00
|
|
|
|
// Make an unmounter for the volume
|
2015-07-07 16:40:55 +00:00
|
|
|
|
teardownMedium := mediumUnknown
|
2016-11-18 20:58:56 +00:00
|
|
|
|
if config.medium == v1.StorageMediumMemory {
|
2015-07-07 16:40:55 +00:00
|
|
|
|
teardownMedium = mediumMemory
|
|
|
|
|
}
|
2016-03-23 05:12:21 +00:00
|
|
|
|
unmounterMountDetector := &fakeMountDetector{medium: teardownMedium, isMount: config.shouldBeMountedBeforeTeardown}
|
|
|
|
|
unmounter, err := plug.(*emptyDirPlugin).newUnmounterInternal(volumeName, types.UID("poduid"), &physicalMounter, unmounterMountDetector)
|
2014-11-23 15:47:25 +00:00
|
|
|
|
if err != nil {
|
2016-03-23 05:12:21 +00:00
|
|
|
|
t.Errorf("Failed to make a new Unmounter: %v", err)
|
2014-11-23 15:47:25 +00:00
|
|
|
|
}
|
2016-03-23 05:12:21 +00:00
|
|
|
|
if unmounter == nil {
|
|
|
|
|
t.Errorf("Got a nil Unmounter")
|
2014-11-23 15:47:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-07 16:40:55 +00:00
|
|
|
|
// Tear down the volume
|
2016-03-23 05:12:21 +00:00
|
|
|
|
if err := unmounter.TearDown(); err != nil {
|
2014-11-23 15:47:25 +00:00
|
|
|
|
t.Errorf("Expected success, got: %v", err)
|
|
|
|
|
}
|
2015-03-07 20:35:00 +00:00
|
|
|
|
if _, err := os.Stat(volPath); err == nil {
|
|
|
|
|
t.Errorf("TearDown() failed, volume path still exists: %s", volPath)
|
2014-11-23 15:47:25 +00:00
|
|
|
|
} else if !os.IsNotExist(err) {
|
|
|
|
|
t.Errorf("SetUp() failed: %v", err)
|
|
|
|
|
}
|
2015-07-07 16:40:55 +00:00
|
|
|
|
|
2016-03-23 05:12:21 +00:00
|
|
|
|
// Check the number of physicalMounter calls during tardown
|
|
|
|
|
if e, a := config.expectedTeardownMounts, len(physicalMounter.Log); e != a {
|
|
|
|
|
t.Errorf("Expected %v physicalMounter calls during teardown, got %v", e, a)
|
|
|
|
|
} else if config.expectedTeardownMounts == 1 && physicalMounter.Log[0].Action != mount.FakeActionUnmount {
|
|
|
|
|
t.Errorf("Unexpected physicalMounter action during teardown: %#v", physicalMounter.Log[0])
|
2015-03-07 20:35:00 +00:00
|
|
|
|
}
|
2016-03-23 05:12:21 +00:00
|
|
|
|
physicalMounter.ResetLog()
|
2014-11-23 15:47:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestPluginBackCompat(t *testing.T) {
|
2016-01-25 21:57:42 +00:00
|
|
|
|
basePath, err := utiltesting.MkTmpdir("emptydirTest")
|
2015-10-12 12:28:03 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("can't make a temp dir: %v", err)
|
|
|
|
|
}
|
|
|
|
|
defer os.RemoveAll(basePath)
|
|
|
|
|
|
2016-10-24 22:45:42 +00:00
|
|
|
|
plug := makePluginUnderTest(t, "kubernetes.io/empty-dir", basePath)
|
2014-11-23 15:47:25 +00:00
|
|
|
|
|
2016-11-18 20:58:56 +00:00
|
|
|
|
spec := &v1.Volume{
|
2015-01-21 01:40:43 +00:00
|
|
|
|
Name: "vol1",
|
2014-11-23 15:47:25 +00:00
|
|
|
|
}
|
2017-01-17 03:38:19 +00:00
|
|
|
|
pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{UID: types.UID("poduid")}}
|
2016-05-30 02:22:22 +00:00
|
|
|
|
mounter, err := plug.NewMounter(volume.NewSpecFromVolume(spec), pod, volume.VolumeOptions{})
|
2014-11-23 15:47:25 +00:00
|
|
|
|
if err != nil {
|
2016-03-23 05:12:21 +00:00
|
|
|
|
t.Errorf("Failed to make a new Mounter: %v", err)
|
2014-11-23 15:47:25 +00:00
|
|
|
|
}
|
2016-03-23 05:12:21 +00:00
|
|
|
|
if mounter == nil {
|
2016-12-19 07:42:14 +00:00
|
|
|
|
t.Fatalf("Got a nil Mounter")
|
2014-11-23 15:47:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-23 05:12:21 +00:00
|
|
|
|
volPath := mounter.GetPath()
|
2015-03-07 20:35:00 +00:00
|
|
|
|
if volPath != path.Join(basePath, "pods/poduid/volumes/kubernetes.io~empty-dir/vol1") {
|
|
|
|
|
t.Errorf("Got unexpected path: %s", volPath)
|
2014-11-23 15:47:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-12-04 20:40:01 +00:00
|
|
|
|
|
|
|
|
|
// TestMetrics tests that MetricProvider methods return sane values.
|
|
|
|
|
func TestMetrics(t *testing.T) {
|
|
|
|
|
// Create an empty temp directory for the volume
|
2016-01-25 21:57:42 +00:00
|
|
|
|
tmpDir, err := utiltesting.MkTmpdir("empty_dir_test")
|
2015-12-04 20:40:01 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Can't make a tmp dir: %v", err)
|
|
|
|
|
}
|
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
|
2016-10-24 22:45:42 +00:00
|
|
|
|
plug := makePluginUnderTest(t, "kubernetes.io/empty-dir", tmpDir)
|
2015-12-04 20:40:01 +00:00
|
|
|
|
|
2016-11-18 20:58:56 +00:00
|
|
|
|
spec := &v1.Volume{
|
2015-12-04 20:40:01 +00:00
|
|
|
|
Name: "vol1",
|
|
|
|
|
}
|
2017-01-17 03:38:19 +00:00
|
|
|
|
pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{UID: types.UID("poduid")}}
|
2016-05-30 02:22:22 +00:00
|
|
|
|
mounter, err := plug.NewMounter(volume.NewSpecFromVolume(spec), pod, volume.VolumeOptions{})
|
2015-12-04 20:40:01 +00:00
|
|
|
|
if err != nil {
|
2016-03-23 05:12:21 +00:00
|
|
|
|
t.Errorf("Failed to make a new Mounter: %v", err)
|
2015-12-04 20:40:01 +00:00
|
|
|
|
}
|
2016-12-19 07:42:14 +00:00
|
|
|
|
if mounter == nil {
|
|
|
|
|
t.Fatalf("Got a nil Mounter")
|
|
|
|
|
}
|
2015-12-04 20:40:01 +00:00
|
|
|
|
|
|
|
|
|
// Need to create the subdirectory
|
2016-03-23 05:12:21 +00:00
|
|
|
|
os.MkdirAll(mounter.GetPath(), 0755)
|
2015-12-04 20:40:01 +00:00
|
|
|
|
|
2016-02-29 21:31:42 +00:00
|
|
|
|
expectedEmptyDirUsage, err := volumetest.FindEmptyDirectoryUsageOnTmpfs()
|
2015-12-11 17:55:17 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
t.Errorf("Unexpected error finding expected empty directory usage on tmpfs: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-04 20:40:01 +00:00
|
|
|
|
// TODO(pwittroc): Move this into a reusable testing utility
|
2016-03-23 05:12:21 +00:00
|
|
|
|
metrics, err := mounter.GetMetrics()
|
2015-12-04 20:40:01 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
t.Errorf("Unexpected error when calling GetMetrics %v", err)
|
|
|
|
|
}
|
2015-12-11 17:55:17 +00:00
|
|
|
|
if e, a := expectedEmptyDirUsage.Value(), metrics.Used.Value(); e != a {
|
|
|
|
|
t.Errorf("Unexpected value for empty directory; expected %v, got %v", e, a)
|
2015-12-04 20:40:01 +00:00
|
|
|
|
}
|
|
|
|
|
if metrics.Capacity.Value() <= 0 {
|
|
|
|
|
t.Errorf("Expected Capacity to be greater than 0")
|
|
|
|
|
}
|
|
|
|
|
if metrics.Available.Value() <= 0 {
|
|
|
|
|
t.Errorf("Expected Available to be greater than 0")
|
|
|
|
|
}
|
|
|
|
|
}
|