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 host_path
|
|
|
|
|
|
|
|
import (
|
2015-09-07 16:11:37 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2014-11-23 15:47:25 +00:00
|
|
|
"testing"
|
|
|
|
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2015-09-18 13:15:48 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/resource"
|
2016-02-16 22:16:45 +00:00
|
|
|
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/types"
|
2015-09-07 16:11:37 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/volume"
|
2016-02-29 21:31:42 +00:00
|
|
|
volumetest "k8s.io/kubernetes/pkg/volume/testing"
|
2014-11-23 15:47:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCanSupport(t *testing.T) {
|
2015-03-19 05:18:31 +00:00
|
|
|
plugMgr := volume.VolumePluginMgr{}
|
2016-05-30 02:22:22 +00:00
|
|
|
plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), volumetest.NewFakeVolumeHost("fake", nil, nil, "" /* rootContext */))
|
2014-11-23 15:47:25 +00:00
|
|
|
|
|
|
|
plug, err := plugMgr.FindPluginByName("kubernetes.io/host-path")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Can't find the plugin by name")
|
|
|
|
}
|
2016-05-30 02:22:22 +00:00
|
|
|
if plug.GetPluginName() != "kubernetes.io/host-path" {
|
|
|
|
t.Errorf("Wrong name: %s", plug.GetPluginName())
|
2014-11-23 15:47:25 +00:00
|
|
|
}
|
2015-08-12 19:11:03 +00:00
|
|
|
if !plug.CanSupport(&volume.Spec{Volume: &api.Volume{VolumeSource: api.VolumeSource{HostPath: &api.HostPathVolumeSource{}}}}) {
|
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{PersistentVolume: &api.PersistentVolume{Spec: api.PersistentVolumeSpec{PersistentVolumeSource: api.PersistentVolumeSource{HostPath: &api.HostPathVolumeSource{}}}}}) {
|
2015-05-23 13:53:33 +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{}}}) {
|
2014-11-23 15:47:25 +00:00
|
|
|
t.Errorf("Expected false")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-12 19:37:02 +00:00
|
|
|
func TestGetAccessModes(t *testing.T) {
|
|
|
|
plugMgr := volume.VolumePluginMgr{}
|
2016-05-30 02:22:22 +00:00
|
|
|
plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), volumetest.NewFakeVolumeHost("/tmp/fake", nil, nil, "" /* rootContext */))
|
2015-03-12 19:37:02 +00:00
|
|
|
|
|
|
|
plug, err := plugMgr.FindPersistentPluginByName("kubernetes.io/host-path")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Can't find the plugin by name")
|
|
|
|
}
|
|
|
|
if len(plug.GetAccessModes()) != 1 || plug.GetAccessModes()[0] != api.ReadWriteOnce {
|
2015-05-18 20:22:30 +00:00
|
|
|
t.Errorf("Expected %s PersistentVolumeAccessMode", api.ReadWriteOnce)
|
2015-03-12 19:37:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-29 20:34:02 +00:00
|
|
|
func TestRecycler(t *testing.T) {
|
|
|
|
plugMgr := volume.VolumePluginMgr{}
|
2016-05-30 02:22:22 +00:00
|
|
|
pluginHost := volumetest.NewFakeVolumeHost("/tmp/fake", nil, nil, "" /* rootContext */)
|
2016-02-29 21:31:42 +00:00
|
|
|
plugMgr.InitPlugins([]volume.VolumePlugin{&hostPathPlugin{nil, volumetest.NewFakeRecycler, nil, nil, volume.VolumeConfig{}}}, pluginHost)
|
2015-05-29 20:34:02 +00:00
|
|
|
|
2015-08-12 19:11:03 +00:00
|
|
|
spec := &volume.Spec{PersistentVolume: &api.PersistentVolume{Spec: api.PersistentVolumeSpec{PersistentVolumeSource: api.PersistentVolumeSource{HostPath: &api.HostPathVolumeSource{Path: "/foo"}}}}}
|
2015-05-29 20:34:02 +00:00
|
|
|
plug, err := plugMgr.FindRecyclablePluginBySpec(spec)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Can't find the plugin by name")
|
|
|
|
}
|
2016-05-19 10:58:25 +00:00
|
|
|
recycler, err := plug.NewRecycler("pv-name", spec)
|
2015-05-29 20:34:02 +00:00
|
|
|
if err != nil {
|
2015-08-08 01:52:23 +00:00
|
|
|
t.Errorf("Failed to make a new Recyler: %v", err)
|
2015-05-29 20:34:02 +00:00
|
|
|
}
|
2015-08-12 19:11:03 +00:00
|
|
|
if recycler.GetPath() != spec.PersistentVolume.Spec.HostPath.Path {
|
|
|
|
t.Errorf("Expected %s but got %s", spec.PersistentVolume.Spec.HostPath.Path, recycler.GetPath())
|
2015-05-29 20:34:02 +00:00
|
|
|
}
|
|
|
|
if err := recycler.Recycle(); err != nil {
|
|
|
|
t.Errorf("Mock Recycler expected to return nil but got %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-07 16:11:37 +00:00
|
|
|
func TestDeleter(t *testing.T) {
|
2016-01-25 21:57:42 +00:00
|
|
|
// Deleter has a hard-coded regex for "/tmp".
|
2015-09-07 16:11:37 +00:00
|
|
|
tempPath := fmt.Sprintf("/tmp/hostpath/%s", util.NewUUID())
|
|
|
|
defer os.RemoveAll(tempPath)
|
|
|
|
err := os.MkdirAll(tempPath, 0750)
|
|
|
|
if err != nil {
|
2015-12-04 20:40:01 +00:00
|
|
|
t.Fatalf("Failed to create tmp directory for deleter: %v", err)
|
2015-09-07 16:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
plugMgr := volume.VolumePluginMgr{}
|
2016-05-30 02:22:22 +00:00
|
|
|
plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), volumetest.NewFakeVolumeHost("/tmp/fake", nil, nil, "" /* rootContext */))
|
2015-09-07 16:11:37 +00:00
|
|
|
|
|
|
|
spec := &volume.Spec{PersistentVolume: &api.PersistentVolume{Spec: api.PersistentVolumeSpec{PersistentVolumeSource: api.PersistentVolumeSource{HostPath: &api.HostPathVolumeSource{Path: tempPath}}}}}
|
|
|
|
plug, err := plugMgr.FindDeletablePluginBySpec(spec)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Can't find the plugin by name")
|
|
|
|
}
|
|
|
|
deleter, err := plug.NewDeleter(spec)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to make a new Deleter: %v", err)
|
|
|
|
}
|
|
|
|
if deleter.GetPath() != tempPath {
|
|
|
|
t.Errorf("Expected %s but got %s", tempPath, deleter.GetPath())
|
|
|
|
}
|
|
|
|
if err := deleter.Delete(); err != nil {
|
|
|
|
t.Errorf("Mock Recycler expected to return nil but got %s", err)
|
|
|
|
}
|
|
|
|
if exists, _ := util.FileExists("foo"); exists {
|
|
|
|
t.Errorf("Temp path expected to be deleted, but was found at %s", tempPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleterTempDir(t *testing.T) {
|
|
|
|
tests := map[string]struct {
|
|
|
|
expectedFailure bool
|
|
|
|
path string
|
|
|
|
}{
|
|
|
|
"just-tmp": {true, "/tmp"},
|
|
|
|
"not-tmp": {true, "/nottmp"},
|
|
|
|
"good-tmp": {false, "/tmp/scratch"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, test := range tests {
|
|
|
|
plugMgr := volume.VolumePluginMgr{}
|
2016-05-30 02:22:22 +00:00
|
|
|
plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), volumetest.NewFakeVolumeHost("/tmp/fake", nil, nil, "" /* rootContext */))
|
2015-09-07 16:11:37 +00:00
|
|
|
spec := &volume.Spec{PersistentVolume: &api.PersistentVolume{Spec: api.PersistentVolumeSpec{PersistentVolumeSource: api.PersistentVolumeSource{HostPath: &api.HostPathVolumeSource{Path: test.path}}}}}
|
|
|
|
plug, _ := plugMgr.FindDeletablePluginBySpec(spec)
|
|
|
|
deleter, _ := plug.NewDeleter(spec)
|
|
|
|
err := deleter.Delete()
|
|
|
|
if err == nil && test.expectedFailure {
|
|
|
|
t.Errorf("Expected failure for test '%s' but got nil err", name)
|
|
|
|
}
|
|
|
|
if err != nil && !test.expectedFailure {
|
|
|
|
t.Errorf("Unexpected failure for test '%s': %v", name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-12 18:27:49 +00:00
|
|
|
func TestProvisioner(t *testing.T) {
|
2016-01-25 21:57:42 +00:00
|
|
|
tempPath := fmt.Sprintf("/tmp/hostpath/%s", util.NewUUID())
|
2015-09-07 19:55:28 +00:00
|
|
|
defer os.RemoveAll(tempPath)
|
|
|
|
err := os.MkdirAll(tempPath, 0750)
|
|
|
|
|
|
|
|
plugMgr := volume.VolumePluginMgr{}
|
2016-05-30 02:22:22 +00:00
|
|
|
plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), volumetest.NewFakeVolumeHost("/tmp/fake", nil, nil, "" /* rootContext */))
|
2015-09-07 19:55:28 +00:00
|
|
|
spec := &volume.Spec{PersistentVolume: &api.PersistentVolume{Spec: api.PersistentVolumeSpec{PersistentVolumeSource: api.PersistentVolumeSource{HostPath: &api.HostPathVolumeSource{Path: tempPath}}}}}
|
|
|
|
plug, err := plugMgr.FindCreatablePluginBySpec(spec)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Can't find the plugin by name")
|
|
|
|
}
|
2015-10-12 18:27:49 +00:00
|
|
|
creater, err := plug.NewProvisioner(volume.VolumeOptions{Capacity: resource.MustParse("1Gi"), PersistentVolumeReclaimPolicy: api.PersistentVolumeReclaimDelete})
|
2015-09-07 19:55:28 +00:00
|
|
|
if err != nil {
|
2015-10-12 18:27:49 +00:00
|
|
|
t.Errorf("Failed to make a new Provisioner: %v", err)
|
2015-09-07 19:55:28 +00:00
|
|
|
}
|
2016-05-17 12:55:24 +00:00
|
|
|
pv, err := creater.Provision()
|
2015-09-07 19:55:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error creating volume: %v", err)
|
|
|
|
}
|
|
|
|
if pv.Spec.HostPath.Path == "" {
|
|
|
|
t.Errorf("Expected pv.Spec.HostPath.Path to not be empty: %#v", pv)
|
|
|
|
}
|
2015-10-12 18:27:49 +00:00
|
|
|
expectedCapacity := resource.NewQuantity(1*1024*1024*1024, resource.BinarySI)
|
2015-09-18 13:15:48 +00:00
|
|
|
actualCapacity := pv.Spec.Capacity[api.ResourceStorage]
|
|
|
|
expectedAmt := expectedCapacity.Value()
|
|
|
|
actualAmt := actualCapacity.Value()
|
|
|
|
if expectedAmt != actualAmt {
|
|
|
|
t.Errorf("Expected capacity %+v but got %+v", expectedAmt, actualAmt)
|
|
|
|
}
|
|
|
|
|
|
|
|
if pv.Spec.PersistentVolumeReclaimPolicy != api.PersistentVolumeReclaimDelete {
|
|
|
|
t.Errorf("Expected reclaim policy %+v but got %+v", api.PersistentVolumeReclaimDelete, pv.Spec.PersistentVolumeReclaimPolicy)
|
|
|
|
}
|
|
|
|
|
2015-09-07 19:55:28 +00:00
|
|
|
os.RemoveAll(pv.Spec.HostPath.Path)
|
|
|
|
}
|
|
|
|
|
2014-11-23 15:47:25 +00:00
|
|
|
func TestPlugin(t *testing.T) {
|
2015-03-19 05:18:31 +00:00
|
|
|
plugMgr := volume.VolumePluginMgr{}
|
2016-05-30 02:22:22 +00:00
|
|
|
plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), volumetest.NewFakeVolumeHost("fake", nil, nil, "" /* rootContext */))
|
2014-11-23 15:47:25 +00:00
|
|
|
|
|
|
|
plug, err := plugMgr.FindPluginByName("kubernetes.io/host-path")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Can't find the plugin by name")
|
|
|
|
}
|
|
|
|
spec := &api.Volume{
|
2015-03-03 22:48:55 +00:00
|
|
|
Name: "vol1",
|
2015-08-08 01:52:23 +00:00
|
|
|
VolumeSource: api.VolumeSource{HostPath: &api.HostPathVolumeSource{Path: "/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")}}
|
2016-03-23 05:12:21 +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 {
|
|
|
|
t.Errorf("Got a nil Mounter")
|
2014-11-23 15:47:25 +00:00
|
|
|
}
|
|
|
|
|
2016-03-23 05:12:21 +00:00
|
|
|
path := mounter.GetPath()
|
2014-11-23 15:47:25 +00:00
|
|
|
if path != "/vol1" {
|
|
|
|
t.Errorf("Got unexpected path: %s", path)
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-03-23 05:12:21 +00:00
|
|
|
unmounter, err := plug.NewUnmounter("vol1", types.UID("poduid"))
|
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
|
|
|
}
|
|
|
|
|
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-07-01 14:50:39 +00:00
|
|
|
|
|
|
|
func TestPersistentClaimReadOnlyFlag(t *testing.T) {
|
|
|
|
pv := &api.PersistentVolume{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "pvA",
|
|
|
|
},
|
|
|
|
Spec: api.PersistentVolumeSpec{
|
|
|
|
PersistentVolumeSource: api.PersistentVolumeSource{
|
2015-08-08 01:52:23 +00:00
|
|
|
HostPath: &api.HostPathVolumeSource{Path: "foo"},
|
2015-07-01 14:50:39 +00:00
|
|
|
},
|
|
|
|
ClaimRef: &api.ObjectReference{
|
|
|
|
Name: "claimA",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
claim := &api.PersistentVolumeClaim{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "claimA",
|
|
|
|
Namespace: "nsA",
|
|
|
|
},
|
|
|
|
Spec: api.PersistentVolumeClaimSpec{
|
|
|
|
VolumeName: "pvA",
|
|
|
|
},
|
|
|
|
Status: api.PersistentVolumeClaimStatus{
|
|
|
|
Phase: api.ClaimBound,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-01-15 05:00:58 +00:00
|
|
|
client := fake.NewSimpleClientset(pv, claim)
|
2015-07-01 14:50:39 +00:00
|
|
|
|
|
|
|
plugMgr := volume.VolumePluginMgr{}
|
2016-05-30 02:22:22 +00:00
|
|
|
plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), volumetest.NewFakeVolumeHost("/tmp/fake", client, nil, "" /* rootContext */))
|
2015-07-01 14:50:39 +00:00
|
|
|
plug, _ := plugMgr.FindPluginByName(hostPathPluginName)
|
|
|
|
|
2016-03-23 05:12:21 +00:00
|
|
|
// readOnly bool is supplied by persistent-claim volume source when its mounter creates other volumes
|
2015-07-01 14:50:39 +00:00
|
|
|
spec := volume.NewSpecFromPersistentVolume(pv, true)
|
|
|
|
pod := &api.Pod{ObjectMeta: api.ObjectMeta{UID: types.UID("poduid")}}
|
2016-03-23 05:12:21 +00:00
|
|
|
mounter, _ := plug.NewMounter(spec, pod, volume.VolumeOptions{})
|
2015-07-01 14:50:39 +00:00
|
|
|
|
2016-03-23 05:12:21 +00:00
|
|
|
if !mounter.GetAttributes().ReadOnly {
|
|
|
|
t.Errorf("Expected true for mounter.IsReadOnly")
|
2015-07-01 14:50:39 +00:00
|
|
|
}
|
|
|
|
}
|