2015-12-16 22:16:30 +00:00
|
|
|
|
// +build linux
|
|
|
|
|
|
2014-11-23 15:47:25 +00:00
|
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
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 (
|
2015-07-07 16:40:55 +00:00
|
|
|
|
"io/ioutil"
|
2014-11-23 15:47:25 +00:00
|
|
|
|
"os"
|
2015-03-07 20:35:00 +00:00
|
|
|
|
"path"
|
2014-11-23 15:47:25 +00:00
|
|
|
|
"testing"
|
|
|
|
|
|
2015-08-05 22:03:47 +00:00
|
|
|
|
"k8s.io/kubernetes/pkg/api"
|
|
|
|
|
"k8s.io/kubernetes/pkg/types"
|
|
|
|
|
"k8s.io/kubernetes/pkg/util/mount"
|
|
|
|
|
"k8s.io/kubernetes/pkg/volume"
|
|
|
|
|
"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.
|
2015-07-07 16:40:55 +00:00
|
|
|
|
func makePluginUnderTest(t *testing.T, plugName, basePath string) volume.VolumePlugin {
|
2015-03-19 05:18:31 +00:00
|
|
|
|
plugMgr := volume.VolumePluginMgr{}
|
|
|
|
|
plugMgr.InitPlugins(ProbeVolumePlugins(), volume.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) {
|
2015-10-12 12:28:03 +00:00
|
|
|
|
tmpDir, err := ioutil.TempDir(os.TempDir(), "emptydirTest")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("can't make a temp dir: %v", err)
|
|
|
|
|
}
|
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
plug := makePluginUnderTest(t, "kubernetes.io/empty-dir", tmpDir)
|
2015-03-07 20:35:00 +00:00
|
|
|
|
|
2014-11-23 15:47:25 +00:00
|
|
|
|
if plug.Name() != "kubernetes.io/empty-dir" {
|
|
|
|
|
t.Errorf("Wrong name: %s", plug.Name())
|
|
|
|
|
}
|
2015-08-12 19:11:03 +00:00
|
|
|
|
if !plug.CanSupport(&volume.Spec{Volume: &api.Volume{VolumeSource: api.VolumeSource{EmptyDir: &api.EmptyDirVolumeSource{}}}}) {
|
2014-11-23 15:47:25 +00:00
|
|
|
|
t.Errorf("Expected true")
|
|
|
|
|
}
|
2015-08-12 19:11:03 +00:00
|
|
|
|
if plug.CanSupport(&volume.Spec{Volume: &api.Volume{VolumeSource: api.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{
|
|
|
|
|
medium: api.StorageMediumDefault,
|
|
|
|
|
rootContext: "",
|
|
|
|
|
expectedSetupMounts: 0,
|
|
|
|
|
expectedTeardownMounts: 0})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestPluginRootContextSet(t *testing.T) {
|
|
|
|
|
if !selinuxEnabled() {
|
|
|
|
|
return
|
2015-03-07 20:35:00 +00:00
|
|
|
|
}
|
2015-07-07 16:40:55 +00:00
|
|
|
|
|
|
|
|
|
doTestPlugin(t, pluginTestConfig{
|
|
|
|
|
medium: api.StorageMediumDefault,
|
|
|
|
|
rootContext: "user:role:type:range",
|
|
|
|
|
expectedSELinuxContext: "user:role:type:range",
|
|
|
|
|
expectedSetupMounts: 0,
|
|
|
|
|
expectedTeardownMounts: 0})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestPluginTmpfs(t *testing.T) {
|
|
|
|
|
if !selinuxEnabled() {
|
|
|
|
|
return
|
2014-11-23 15:47:25 +00:00
|
|
|
|
}
|
2015-03-07 20:35:00 +00:00
|
|
|
|
|
2015-07-07 16:40:55 +00:00
|
|
|
|
doTestPlugin(t, pluginTestConfig{
|
|
|
|
|
medium: api.StorageMediumMemory,
|
|
|
|
|
rootContext: "user:role:type:range",
|
|
|
|
|
expectedSELinuxContext: "user:role:type:range",
|
|
|
|
|
expectedSetupMounts: 1,
|
|
|
|
|
shouldBeMountedBeforeTeardown: true,
|
|
|
|
|
expectedTeardownMounts: 1})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type pluginTestConfig struct {
|
|
|
|
|
medium api.StorageMedium
|
|
|
|
|
rootContext string
|
|
|
|
|
SELinuxOptions *api.SELinuxOptions
|
|
|
|
|
idempotent bool
|
|
|
|
|
expectedSELinuxContext string
|
|
|
|
|
expectedSetupMounts int
|
|
|
|
|
shouldBeMountedBeforeTeardown bool
|
|
|
|
|
expectedTeardownMounts int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// doTestPlugin sets up a volume and tears it back down.
|
|
|
|
|
func doTestPlugin(t *testing.T, config pluginTestConfig) {
|
2015-10-12 12:28:03 +00:00
|
|
|
|
basePath, err := ioutil.TempDir(os.TempDir(), "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
|
|
|
|
|
2015-07-07 16:40:55 +00:00
|
|
|
|
plug = makePluginUnderTest(t, "kubernetes.io/empty-dir", basePath)
|
|
|
|
|
volumeName = "test-volume"
|
|
|
|
|
spec = &api.Volume{
|
|
|
|
|
Name: volumeName,
|
|
|
|
|
VolumeSource: api.VolumeSource{EmptyDir: &api.EmptyDirVolumeSource{Medium: config.medium}},
|
|
|
|
|
}
|
2015-03-07 20:35:00 +00:00
|
|
|
|
|
2015-07-07 16:40:55 +00:00
|
|
|
|
mounter = mount.FakeMounter{}
|
|
|
|
|
mountDetector = fakeMountDetector{}
|
|
|
|
|
pod = &api.Pod{ObjectMeta: api.ObjectMeta{UID: types.UID("poduid")}}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Set up the SELinux options on the pod
|
|
|
|
|
if config.SELinuxOptions != nil {
|
|
|
|
|
pod.Spec = api.PodSpec{
|
|
|
|
|
Containers: []api.Container{
|
|
|
|
|
{
|
|
|
|
|
SecurityContext: &api.SecurityContext{
|
|
|
|
|
SELinuxOptions: config.SELinuxOptions,
|
|
|
|
|
},
|
|
|
|
|
VolumeMounts: []api.VolumeMount{
|
|
|
|
|
{
|
|
|
|
|
Name: volumeName,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
2014-11-23 15:47:25 +00:00
|
|
|
|
}
|
2015-07-07 16:40:55 +00:00
|
|
|
|
|
|
|
|
|
if config.idempotent {
|
|
|
|
|
mounter.MountPoints = []mount.MountPoint{
|
|
|
|
|
{
|
|
|
|
|
Path: volumePath,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
util.SetReady(metadataDir)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
builder, err := plug.(*emptyDirPlugin).newBuilderInternal(volume.NewSpecFromVolume(spec),
|
|
|
|
|
pod,
|
|
|
|
|
&mounter,
|
|
|
|
|
&mountDetector,
|
2015-10-07 19:19:06 +00:00
|
|
|
|
volume.VolumeOptions{RootContext: config.rootContext})
|
2014-11-23 15:47:25 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
t.Errorf("Failed to make a new Builder: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if builder == nil {
|
2015-03-31 22:32:02 +00:00
|
|
|
|
t.Errorf("Got a nil Builder")
|
2014-11-23 15:47:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-07 20:35:00 +00:00
|
|
|
|
volPath := builder.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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := builder.SetUp(); err != nil {
|
|
|
|
|
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
|
|
|
|
|
if e, a := config.expectedSetupMounts, len(mounter.Log); e != a {
|
|
|
|
|
t.Errorf("Expected %v mounter calls during setup, got %v", e, a)
|
|
|
|
|
} else if config.expectedSetupMounts == 1 &&
|
|
|
|
|
(mounter.Log[0].Action != mount.FakeActionMount || mounter.Log[0].FSType != "tmpfs") {
|
|
|
|
|
t.Errorf("Unexpected mounter action during setup: %#v", mounter.Log[0])
|
2015-03-07 20:35:00 +00:00
|
|
|
|
}
|
|
|
|
|
mounter.ResetLog()
|
2014-11-23 15:47:25 +00:00
|
|
|
|
|
2015-07-07 16:40:55 +00:00
|
|
|
|
// Make a cleaner for the volume
|
|
|
|
|
teardownMedium := mediumUnknown
|
|
|
|
|
if config.medium == api.StorageMediumMemory {
|
|
|
|
|
teardownMedium = mediumMemory
|
|
|
|
|
}
|
|
|
|
|
cleanerMountDetector := &fakeMountDetector{medium: teardownMedium, isMount: config.shouldBeMountedBeforeTeardown}
|
|
|
|
|
cleaner, err := plug.(*emptyDirPlugin).newCleanerInternal(volumeName, types.UID("poduid"), &mounter, cleanerMountDetector)
|
2014-11-23 15:47:25 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
t.Errorf("Failed to make a new Cleaner: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if cleaner == nil {
|
2015-03-31 22:32:02 +00:00
|
|
|
|
t.Errorf("Got a nil Cleaner")
|
2014-11-23 15:47:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-07 16:40:55 +00:00
|
|
|
|
// Tear down the volume
|
2014-11-23 15:47:25 +00:00
|
|
|
|
if err := cleaner.TearDown(); err != nil {
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
// Check the number of mounter calls during tardown
|
|
|
|
|
if e, a := config.expectedTeardownMounts, len(mounter.Log); e != a {
|
|
|
|
|
t.Errorf("Expected %v mounter calls during teardown, got %v", e, a)
|
|
|
|
|
} else if config.expectedTeardownMounts == 1 && mounter.Log[0].Action != mount.FakeActionUnmount {
|
|
|
|
|
t.Errorf("Unexpected mounter action during teardown: %#v", mounter.Log[0])
|
2015-03-07 20:35:00 +00:00
|
|
|
|
}
|
|
|
|
|
mounter.ResetLog()
|
2014-11-23 15:47:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestPluginBackCompat(t *testing.T) {
|
2015-10-12 12:28:03 +00:00
|
|
|
|
basePath, err := ioutil.TempDir(os.TempDir(), "emptydirTest")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("can't make a temp dir: %v", err)
|
|
|
|
|
}
|
|
|
|
|
defer os.RemoveAll(basePath)
|
|
|
|
|
|
2015-07-07 16:40:55 +00:00
|
|
|
|
plug := makePluginUnderTest(t, "kubernetes.io/empty-dir", basePath)
|
2014-11-23 15:47:25 +00:00
|
|
|
|
|
|
|
|
|
spec := &api.Volume{
|
2015-01-21 01:40:43 +00:00
|
|
|
|
Name: "vol1",
|
2014-11-23 15:47:25 +00:00
|
|
|
|
}
|
2015-05-11 00:12:57 +00:00
|
|
|
|
pod := &api.Pod{ObjectMeta: api.ObjectMeta{UID: types.UID("poduid")}}
|
2015-09-14 09:51:40 +00:00
|
|
|
|
builder, err := plug.NewBuilder(volume.NewSpecFromVolume(spec), pod, volume.VolumeOptions{RootContext: ""})
|
2014-11-23 15:47:25 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
t.Errorf("Failed to make a new Builder: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if builder == nil {
|
2015-03-31 22:32:02 +00:00
|
|
|
|
t.Errorf("Got a nil Builder")
|
2014-11-23 15:47:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-07 20:35:00 +00:00
|
|
|
|
volPath := builder.GetPath()
|
|
|
|
|
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
|
|
|
|
|
tmpDir, err := ioutil.TempDir(os.TempDir(), "empty_dir_test")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Can't make a tmp dir: %v", err)
|
|
|
|
|
}
|
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
|
|
|
|
|
plug := makePluginUnderTest(t, "kubernetes.io/empty-dir", tmpDir)
|
|
|
|
|
|
|
|
|
|
spec := &api.Volume{
|
|
|
|
|
Name: "vol1",
|
|
|
|
|
}
|
|
|
|
|
pod := &api.Pod{ObjectMeta: api.ObjectMeta{UID: types.UID("poduid")}}
|
|
|
|
|
builder, err := plug.NewBuilder(volume.NewSpecFromVolume(spec), pod, volume.VolumeOptions{RootContext: ""})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Errorf("Failed to make a new Builder: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Need to create the subdirectory
|
|
|
|
|
os.MkdirAll(builder.GetPath(), 0755)
|
|
|
|
|
|
2015-12-11 17:55:17 +00:00
|
|
|
|
expectedEmptyDirUsage, err := volume.FindEmptyDirectoryUsageOnTmpfs()
|
|
|
|
|
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
|
|
|
|
|
metrics, err := builder.GetMetrics()
|
|
|
|
|
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")
|
|
|
|
|
}
|
|
|
|
|
}
|