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"
|
|
|
|
|
2017-06-22 18:24:23 +00:00
|
|
|
"k8s.io/api/core/v1"
|
2017-01-25 13:13:07 +00:00
|
|
|
"k8s.io/apimachinery/pkg/api/resource"
|
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-24 14:35:22 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/uuid"
|
2017-06-23 20:56:37 +00:00
|
|
|
"k8s.io/client-go/kubernetes/fake"
|
2017-06-29 08:39:26 +00:00
|
|
|
utilfile "k8s.io/kubernetes/pkg/util/file"
|
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
|
|
|
)
|
|
|
|
|
2017-06-07 05:10:09 +00:00
|
|
|
func newHostPathType(pathType string) *v1.HostPathType {
|
|
|
|
hostPathType := new(v1.HostPathType)
|
|
|
|
*hostPathType = v1.HostPathType(pathType)
|
|
|
|
return hostPathType
|
|
|
|
}
|
|
|
|
|
|
|
|
func newHostPathTypeList(pathType ...string) []*v1.HostPathType {
|
|
|
|
typeList := []*v1.HostPathType{}
|
|
|
|
for _, ele := range pathType {
|
|
|
|
typeList = append(typeList, newHostPathType(ele))
|
|
|
|
}
|
|
|
|
|
|
|
|
return typeList
|
|
|
|
}
|
|
|
|
|
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-10-25 00:29:41 +00:00
|
|
|
plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), volumetest.NewFakeVolumeHost("fake", nil, nil))
|
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
|
|
|
}
|
2016-11-18 20:58:56 +00:00
|
|
|
if !plug.CanSupport(&volume.Spec{Volume: &v1.Volume{VolumeSource: v1.VolumeSource{HostPath: &v1.HostPathVolumeSource{}}}}) {
|
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{PersistentVolume: &v1.PersistentVolume{Spec: v1.PersistentVolumeSpec{PersistentVolumeSource: v1.PersistentVolumeSource{HostPath: &v1.HostPathVolumeSource{}}}}}) {
|
2015-05-23 13:53:33 +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{}}}) {
|
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-10-25 00:29:41 +00:00
|
|
|
plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), volumetest.NewFakeVolumeHost("/tmp/fake", nil, nil))
|
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")
|
|
|
|
}
|
2016-11-18 20:58:56 +00:00
|
|
|
if len(plug.GetAccessModes()) != 1 || plug.GetAccessModes()[0] != v1.ReadWriteOnce {
|
|
|
|
t.Errorf("Expected %s PersistentVolumeAccessMode", v1.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-10-25 00:29:41 +00:00
|
|
|
pluginHost := volumetest.NewFakeVolumeHost("/tmp/fake", nil, nil)
|
Simplifies NFS and Host Path Plugin - Removed newRecyclerFunc, newDeleterFunc and newProvisionerFunc
struct hostPathPlugin contains newRecyclerFunc, newDeleterFunc and newProvisionerFunc items that have only one instance, i.e. newRecycler, newDeleter or newProvisioner function.
That's why the newRecyclerFunc, newDeleterFunc and newProvisionerFunc items are removed and the newRecycler, newDeleter or newProvisioner functions are called directly.
In addition, the TestRecycler tests whether NewFakeRecycler function is called and returns nil. This is no longer needed so this particular part of the test is removed. In addition, the no longer used NewFakeRecycler function is removed also.
Similarly for the NFS plugin, struct nfsPlugin contains newRecyclerFunc item that has only one instance, i.e. newRecycler function. That's why the newRecyclerFunc item is removed and the newRecycler function is called directly. In addition, the TestRecycler tests whether newMockRecycler function is called and returns nil. This is no longer needed so this particular part of the test is removed. In addition, the no longer used newMockRecycler function is removed also.
2016-10-25 10:36:49 +00:00
|
|
|
plugMgr.InitPlugins([]volume.VolumePlugin{&hostPathPlugin{nil, volume.VolumeConfig{}}}, pluginHost)
|
2015-05-29 20:34:02 +00:00
|
|
|
|
2016-11-18 20:58:56 +00:00
|
|
|
spec := &volume.Spec{PersistentVolume: &v1.PersistentVolume{Spec: v1.PersistentVolumeSpec{PersistentVolumeSource: v1.PersistentVolumeSource{HostPath: &v1.HostPathVolumeSource{Path: "/foo"}}}}}
|
2017-01-23 19:49:00 +00:00
|
|
|
_, err := plugMgr.FindRecyclablePluginBySpec(spec)
|
2015-05-29 20:34:02 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Can't find the plugin by name")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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".
|
2016-07-26 15:13:18 +00:00
|
|
|
tempPath := fmt.Sprintf("/tmp/hostpath/%s", uuid.NewUUID())
|
2015-09-07 16:11:37 +00:00
|
|
|
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-10-25 00:29:41 +00:00
|
|
|
plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), volumetest.NewFakeVolumeHost("/tmp/fake", nil, nil))
|
2015-09-07 16:11:37 +00:00
|
|
|
|
2016-11-18 20:58:56 +00:00
|
|
|
spec := &volume.Spec{PersistentVolume: &v1.PersistentVolume{Spec: v1.PersistentVolumeSpec{PersistentVolumeSource: v1.PersistentVolumeSource{HostPath: &v1.HostPathVolumeSource{Path: tempPath}}}}}
|
2015-09-07 16:11:37 +00:00
|
|
|
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)
|
|
|
|
}
|
2017-06-07 05:10:09 +00:00
|
|
|
if exists, _ := utilfile.FileExists(tempPath); exists {
|
2015-09-07 16:11:37 +00:00
|
|
|
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-10-25 00:29:41 +00:00
|
|
|
plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), volumetest.NewFakeVolumeHost("/tmp/fake", nil, nil))
|
2016-11-18 20:58:56 +00:00
|
|
|
spec := &volume.Spec{PersistentVolume: &v1.PersistentVolume{Spec: v1.PersistentVolumeSpec{PersistentVolumeSource: v1.PersistentVolumeSource{HostPath: &v1.HostPathVolumeSource{Path: test.path}}}}}
|
2015-09-07 16:11:37 +00:00
|
|
|
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-07-26 15:13:18 +00:00
|
|
|
tempPath := fmt.Sprintf("/tmp/hostpath/%s", uuid.NewUUID())
|
2015-09-07 19:55:28 +00:00
|
|
|
defer os.RemoveAll(tempPath)
|
|
|
|
err := os.MkdirAll(tempPath, 0750)
|
|
|
|
|
|
|
|
plugMgr := volume.VolumePluginMgr{}
|
2016-08-18 08:36:49 +00:00
|
|
|
plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{ProvisioningEnabled: true}),
|
2016-10-25 00:29:41 +00:00
|
|
|
volumetest.NewFakeVolumeHost("/tmp/fake", nil, nil))
|
2016-11-18 20:58:56 +00:00
|
|
|
spec := &volume.Spec{PersistentVolume: &v1.PersistentVolume{Spec: v1.PersistentVolumeSpec{PersistentVolumeSource: v1.PersistentVolumeSource{HostPath: &v1.HostPathVolumeSource{Path: tempPath}}}}}
|
2015-09-07 19:55:28 +00:00
|
|
|
plug, err := plugMgr.FindCreatablePluginBySpec(spec)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Can't find the plugin by name")
|
|
|
|
}
|
2016-10-12 10:22:01 +00:00
|
|
|
options := volume.VolumeOptions{
|
2016-11-18 20:58:56 +00:00
|
|
|
PVC: volumetest.CreateTestPVC("1Gi", []v1.PersistentVolumeAccessMode{v1.ReadWriteOnce}),
|
|
|
|
PersistentVolumeReclaimPolicy: v1.PersistentVolumeReclaimDelete,
|
2016-10-12 10:22:01 +00:00
|
|
|
}
|
|
|
|
creater, err := plug.NewProvisioner(options)
|
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)
|
2016-11-18 20:58:56 +00:00
|
|
|
actualCapacity := pv.Spec.Capacity[v1.ResourceStorage]
|
2015-09-18 13:15:48 +00:00
|
|
|
expectedAmt := expectedCapacity.Value()
|
|
|
|
actualAmt := actualCapacity.Value()
|
|
|
|
if expectedAmt != actualAmt {
|
|
|
|
t.Errorf("Expected capacity %+v but got %+v", expectedAmt, actualAmt)
|
|
|
|
}
|
|
|
|
|
2016-11-18 20:58:56 +00:00
|
|
|
if pv.Spec.PersistentVolumeReclaimPolicy != v1.PersistentVolumeReclaimDelete {
|
|
|
|
t.Errorf("Expected reclaim policy %+v but got %+v", v1.PersistentVolumeReclaimDelete, pv.Spec.PersistentVolumeReclaimPolicy)
|
2015-09-18 13:15:48 +00:00
|
|
|
}
|
|
|
|
|
2015-09-07 19:55:28 +00:00
|
|
|
os.RemoveAll(pv.Spec.HostPath.Path)
|
|
|
|
}
|
|
|
|
|
2017-06-10 13:48:42 +00:00
|
|
|
func TestInvalidHostPath(t *testing.T) {
|
|
|
|
plugMgr := volume.VolumePluginMgr{}
|
|
|
|
plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), volumetest.NewFakeVolumeHost("fake", nil, nil))
|
|
|
|
|
|
|
|
plug, err := plugMgr.FindPluginByName(hostPathPluginName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to find plugin %s by name: %v", hostPathPluginName, err)
|
|
|
|
}
|
|
|
|
spec := &v1.Volume{
|
|
|
|
Name: "vol1",
|
|
|
|
VolumeSource: v1.VolumeSource{HostPath: &v1.HostPathVolumeSource{Path: "/no/backsteps/allowed/.."}},
|
|
|
|
}
|
|
|
|
pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{UID: types.UID("poduid")}}
|
|
|
|
mounter, err := plug.NewMounter(volume.NewSpecFromVolume(spec), pod, volume.VolumeOptions{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = mounter.SetUp(nil)
|
|
|
|
expectedMsg := "invalid HostPath `/no/backsteps/allowed/..`: must not contain '..'"
|
|
|
|
if err.Error() != expectedMsg {
|
|
|
|
t.Fatalf("expected error `%s` but got `%s`", expectedMsg, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-10-25 00:29:41 +00:00
|
|
|
plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), volumetest.NewFakeVolumeHost("fake", nil, nil))
|
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")
|
|
|
|
}
|
2017-06-07 05:10:09 +00:00
|
|
|
|
|
|
|
volPath := "/tmp/vol1"
|
2016-11-18 20:58:56 +00:00
|
|
|
spec := &v1.Volume{
|
2015-03-03 22:48:55 +00:00
|
|
|
Name: "vol1",
|
2017-06-07 05:10:09 +00:00
|
|
|
VolumeSource: v1.VolumeSource{HostPath: &v1.HostPathVolumeSource{Path: volPath, Type: newHostPathType(string(v1.HostPathDirectoryOrCreate))}},
|
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")}}
|
2017-06-07 05:10:09 +00:00
|
|
|
defer os.RemoveAll(volPath)
|
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 {
|
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
|
|
|
path := mounter.GetPath()
|
2017-06-07 05:10:09 +00:00
|
|
|
if path != volPath {
|
2014-11-23 15:47:25 +00:00
|
|
|
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 {
|
2016-12-19 07:42:14 +00:00
|
|
|
t.Fatalf("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) {
|
2016-11-18 20:58:56 +00:00
|
|
|
pv := &v1.PersistentVolume{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-07-01 14:50:39 +00:00
|
|
|
Name: "pvA",
|
|
|
|
},
|
2016-11-18 20:58:56 +00:00
|
|
|
Spec: v1.PersistentVolumeSpec{
|
|
|
|
PersistentVolumeSource: v1.PersistentVolumeSource{
|
2017-06-07 05:10:09 +00:00
|
|
|
HostPath: &v1.HostPathVolumeSource{Path: "foo", Type: newHostPathType(string(v1.HostPathDirectoryOrCreate))},
|
2015-07-01 14:50:39 +00:00
|
|
|
},
|
2016-11-18 20:58:56 +00:00
|
|
|
ClaimRef: &v1.ObjectReference{
|
2015-07-01 14:50:39 +00:00
|
|
|
Name: "claimA",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2017-06-07 05:10:09 +00:00
|
|
|
defer os.RemoveAll("foo")
|
2015-07-01 14:50:39 +00:00
|
|
|
|
2016-11-18 20:58:56 +00:00
|
|
|
claim := &v1.PersistentVolumeClaim{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-07-01 14:50:39 +00:00
|
|
|
Name: "claimA",
|
|
|
|
Namespace: "nsA",
|
|
|
|
},
|
2016-11-18 20:58:56 +00:00
|
|
|
Spec: v1.PersistentVolumeClaimSpec{
|
2015-07-01 14:50:39 +00:00
|
|
|
VolumeName: "pvA",
|
|
|
|
},
|
2016-11-18 20:58:56 +00:00
|
|
|
Status: v1.PersistentVolumeClaimStatus{
|
|
|
|
Phase: v1.ClaimBound,
|
2015-07-01 14:50:39 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
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-10-25 00:29:41 +00:00
|
|
|
plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), volumetest.NewFakeVolumeHost("/tmp/fake", client, nil))
|
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)
|
2017-01-17 03:38:19 +00:00
|
|
|
pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{UID: types.UID("poduid")}}
|
2016-03-23 05:12:21 +00:00
|
|
|
mounter, _ := plug.NewMounter(spec, pod, volume.VolumeOptions{})
|
2016-12-19 07:42:14 +00:00
|
|
|
if mounter == nil {
|
|
|
|
t.Fatalf("Got a nil Mounter")
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
2017-06-07 05:10:09 +00:00
|
|
|
|
|
|
|
type fakeFileTypeChecker struct {
|
|
|
|
desiredType string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fftc *fakeFileTypeChecker) getFileType(_ os.FileInfo) (v1.HostPathType, error) {
|
|
|
|
return *newHostPathType(fftc.desiredType), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setUp() error {
|
|
|
|
err := os.MkdirAll("/tmp/ExistingFolder", os.FileMode(0755))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.OpenFile("/tmp/ExistingFolder/foo", os.O_CREATE, os.FileMode(0644))
|
|
|
|
defer f.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func tearDown() {
|
|
|
|
os.RemoveAll("/tmp/ExistingFolder")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOSFileTypeChecker(t *testing.T) {
|
|
|
|
err := setUp()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
defer tearDown()
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
path string
|
|
|
|
desiredType string
|
|
|
|
isDir bool
|
|
|
|
isFile bool
|
|
|
|
isSocket bool
|
|
|
|
isBlock bool
|
|
|
|
isChar bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Existing Folder",
|
|
|
|
path: "/tmp/ExistingFolder",
|
|
|
|
isDir: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Existing File",
|
|
|
|
path: "/tmp/ExistingFolder/foo",
|
|
|
|
isFile: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Existing Socket File",
|
|
|
|
path: "/tmp/ExistingFolder/foo",
|
|
|
|
desiredType: string(v1.HostPathSocket),
|
|
|
|
isSocket: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Existing Character Device",
|
|
|
|
path: "/tmp/ExistingFolder/foo",
|
|
|
|
desiredType: string(v1.HostPathCharDev),
|
|
|
|
isChar: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Existing Block Device",
|
|
|
|
path: "/tmp/ExistingFolder/foo",
|
|
|
|
desiredType: string(v1.HostPathBlockDev),
|
|
|
|
isBlock: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range testCases {
|
|
|
|
oftc, err := newOSFileTypeChecker(tc.path,
|
|
|
|
&fakeFileTypeChecker{desiredType: tc.desiredType})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d: %q] expect nil, but got %v", i, tc.name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
path := oftc.GetPath()
|
|
|
|
if path != tc.path {
|
|
|
|
t.Errorf("[%d: %q] got unexpected path: %s", i, tc.name, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
exist := oftc.Exists()
|
|
|
|
if !exist {
|
|
|
|
t.Errorf("[%d: %q] path: %s does not exist", i, tc.name, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
if tc.isDir {
|
|
|
|
if !oftc.IsDir() {
|
|
|
|
t.Errorf("[%d: %q] expected folder, got unexpected: %s", i, tc.name, path)
|
|
|
|
}
|
|
|
|
if oftc.IsFile() {
|
|
|
|
t.Errorf("[%d: %q] expected folder, got unexpected file: %s", i, tc.name, path)
|
|
|
|
}
|
|
|
|
if oftc.IsSocket() {
|
|
|
|
t.Errorf("[%d: %q] expected folder, got unexpected socket file: %s", i, tc.name, path)
|
|
|
|
}
|
|
|
|
if oftc.IsBlock() {
|
|
|
|
t.Errorf("[%d: %q] expected folder, got unexpected block device: %s", i, tc.name, path)
|
|
|
|
}
|
|
|
|
if oftc.IsChar() {
|
|
|
|
t.Errorf("[%d: %q] expected folder, got unexpected character device: %s", i, tc.name, path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if tc.isFile {
|
|
|
|
if !oftc.IsFile() {
|
|
|
|
t.Errorf("[%d: %q] expected file, got unexpected: %s", i, tc.name, path)
|
|
|
|
}
|
|
|
|
if oftc.IsDir() {
|
|
|
|
t.Errorf("[%d: %q] expected file, got unexpected folder: %s", i, tc.name, path)
|
|
|
|
}
|
|
|
|
if oftc.IsSocket() {
|
|
|
|
t.Errorf("[%d: %q] expected file, got unexpected socket file: %s", i, tc.name, path)
|
|
|
|
}
|
|
|
|
if oftc.IsBlock() {
|
|
|
|
t.Errorf("[%d: %q] expected file, got unexpected block device: %s", i, tc.name, path)
|
|
|
|
}
|
|
|
|
if oftc.IsChar() {
|
|
|
|
t.Errorf("[%d: %q] expected file, got unexpected character device: %s", i, tc.name, path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if tc.isSocket {
|
|
|
|
if !oftc.IsSocket() {
|
|
|
|
t.Errorf("[%d: %q] expected socket file, got unexpected: %s", i, tc.name, path)
|
|
|
|
}
|
|
|
|
if oftc.IsDir() {
|
|
|
|
t.Errorf("[%d: %q] expected socket file, got unexpected folder: %s", i, tc.name, path)
|
|
|
|
}
|
|
|
|
if !oftc.IsFile() {
|
|
|
|
t.Errorf("[%d: %q] expected socket file, got unexpected file: %s", i, tc.name, path)
|
|
|
|
}
|
|
|
|
if oftc.IsBlock() {
|
|
|
|
t.Errorf("[%d: %q] expected socket file, got unexpected block device: %s", i, tc.name, path)
|
|
|
|
}
|
|
|
|
if oftc.IsChar() {
|
|
|
|
t.Errorf("[%d: %q] expected socket file, got unexpected character device: %s", i, tc.name, path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if tc.isChar {
|
|
|
|
if !oftc.IsChar() {
|
|
|
|
t.Errorf("[%d: %q] expected character device, got unexpected: %s", i, tc.name, path)
|
|
|
|
}
|
|
|
|
if oftc.IsDir() {
|
|
|
|
t.Errorf("[%d: %q] expected character device, got unexpected folder: %s", i, tc.name, path)
|
|
|
|
}
|
|
|
|
if !oftc.IsFile() {
|
|
|
|
t.Errorf("[%d: %q] expected character device, got unexpected file: %s", i, tc.name, path)
|
|
|
|
}
|
|
|
|
if oftc.IsSocket() {
|
|
|
|
t.Errorf("[%d: %q] expected character device, got unexpected socket file: %s", i, tc.name, path)
|
|
|
|
}
|
|
|
|
if oftc.IsBlock() {
|
|
|
|
t.Errorf("[%d: %q] expected character device, got unexpected block device: %s", i, tc.name, path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if tc.isBlock {
|
|
|
|
if !oftc.IsBlock() {
|
|
|
|
t.Errorf("[%d: %q] expected block device, got unexpected: %s", i, tc.name, path)
|
|
|
|
}
|
|
|
|
if oftc.IsDir() {
|
|
|
|
t.Errorf("[%d: %q] expected block device, got unexpected folder: %s", i, tc.name, path)
|
|
|
|
}
|
|
|
|
if !oftc.IsFile() {
|
|
|
|
t.Errorf("[%d: %q] expected block device, got unexpected file: %s", i, tc.name, path)
|
|
|
|
}
|
|
|
|
if oftc.IsSocket() {
|
|
|
|
t.Errorf("[%d: %q] expected block device, got unexpected socket file: %s", i, tc.name, path)
|
|
|
|
}
|
|
|
|
if oftc.IsChar() {
|
|
|
|
t.Errorf("[%d: %q] expected block device, got unexpected character device: %s", i, tc.name, path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
type fakeHostPathTypeChecker struct {
|
|
|
|
name string
|
|
|
|
path string
|
|
|
|
exists bool
|
|
|
|
isDir bool
|
|
|
|
isFile bool
|
|
|
|
isSocket bool
|
|
|
|
isBlock bool
|
|
|
|
isChar bool
|
|
|
|
validpathType []*v1.HostPathType
|
|
|
|
invalidpathType []*v1.HostPathType
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ftc *fakeHostPathTypeChecker) MakeFile() error { return nil }
|
|
|
|
func (ftc *fakeHostPathTypeChecker) MakeDir() error { return nil }
|
|
|
|
func (ftc *fakeHostPathTypeChecker) Exists() bool { return ftc.exists }
|
|
|
|
func (ftc *fakeHostPathTypeChecker) IsFile() bool { return ftc.isFile }
|
|
|
|
func (ftc *fakeHostPathTypeChecker) IsDir() bool { return ftc.isDir }
|
|
|
|
func (ftc *fakeHostPathTypeChecker) IsBlock() bool { return ftc.isBlock }
|
|
|
|
func (ftc *fakeHostPathTypeChecker) IsChar() bool { return ftc.isChar }
|
|
|
|
func (ftc *fakeHostPathTypeChecker) IsSocket() bool { return ftc.isSocket }
|
|
|
|
func (ftc *fakeHostPathTypeChecker) GetPath() string { return ftc.path }
|
|
|
|
|
|
|
|
func TestHostPathTypeCheckerInternal(t *testing.T) {
|
|
|
|
testCases := []fakeHostPathTypeChecker{
|
|
|
|
{
|
|
|
|
name: "Existing Folder",
|
|
|
|
path: "/existingFolder",
|
|
|
|
isDir: true,
|
|
|
|
exists: true,
|
|
|
|
validpathType: newHostPathTypeList(string(v1.HostPathDirectoryOrCreate), string(v1.HostPathDirectory)),
|
|
|
|
invalidpathType: newHostPathTypeList(string(v1.HostPathFileOrCreate), string(v1.HostPathFile),
|
|
|
|
string(v1.HostPathSocket), string(v1.HostPathCharDev), string(v1.HostPathBlockDev)),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "New Folder",
|
|
|
|
path: "/newFolder",
|
|
|
|
isDir: false,
|
|
|
|
exists: false,
|
|
|
|
validpathType: newHostPathTypeList(string(v1.HostPathDirectoryOrCreate)),
|
|
|
|
invalidpathType: newHostPathTypeList(string(v1.HostPathDirectory), string(v1.HostPathFile),
|
|
|
|
string(v1.HostPathSocket), string(v1.HostPathCharDev), string(v1.HostPathBlockDev)),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Existing File",
|
|
|
|
path: "/existingFile",
|
|
|
|
isFile: true,
|
|
|
|
exists: true,
|
|
|
|
validpathType: newHostPathTypeList(string(v1.HostPathFileOrCreate), string(v1.HostPathFile)),
|
|
|
|
invalidpathType: newHostPathTypeList(string(v1.HostPathDirectoryOrCreate), string(v1.HostPathDirectory),
|
|
|
|
string(v1.HostPathSocket), string(v1.HostPathCharDev), string(v1.HostPathBlockDev)),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "New File",
|
|
|
|
path: "/newFile",
|
|
|
|
isFile: false,
|
|
|
|
exists: false,
|
|
|
|
validpathType: newHostPathTypeList(string(v1.HostPathFileOrCreate)),
|
2017-07-19 07:48:36 +00:00
|
|
|
invalidpathType: newHostPathTypeList(string(v1.HostPathDirectory),
|
2017-06-07 05:10:09 +00:00
|
|
|
string(v1.HostPathSocket), string(v1.HostPathCharDev), string(v1.HostPathBlockDev)),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Existing Socket",
|
|
|
|
path: "/existing.socket",
|
|
|
|
isSocket: true,
|
|
|
|
isFile: true,
|
|
|
|
exists: true,
|
|
|
|
validpathType: newHostPathTypeList(string(v1.HostPathSocket), string(v1.HostPathFileOrCreate), string(v1.HostPathFile)),
|
|
|
|
invalidpathType: newHostPathTypeList(string(v1.HostPathDirectoryOrCreate), string(v1.HostPathDirectory),
|
|
|
|
string(v1.HostPathCharDev), string(v1.HostPathBlockDev)),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Existing Character Device",
|
|
|
|
path: "/existing.char",
|
|
|
|
isChar: true,
|
|
|
|
isFile: true,
|
|
|
|
exists: true,
|
|
|
|
validpathType: newHostPathTypeList(string(v1.HostPathCharDev), string(v1.HostPathFileOrCreate), string(v1.HostPathFile)),
|
|
|
|
invalidpathType: newHostPathTypeList(string(v1.HostPathDirectoryOrCreate), string(v1.HostPathDirectory),
|
|
|
|
string(v1.HostPathSocket), string(v1.HostPathBlockDev)),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Existing Block Device",
|
|
|
|
path: "/existing.block",
|
|
|
|
isBlock: true,
|
|
|
|
isFile: true,
|
|
|
|
exists: true,
|
|
|
|
validpathType: newHostPathTypeList(string(v1.HostPathBlockDev), string(v1.HostPathFileOrCreate), string(v1.HostPathFile)),
|
|
|
|
invalidpathType: newHostPathTypeList(string(v1.HostPathDirectoryOrCreate), string(v1.HostPathDirectory),
|
|
|
|
string(v1.HostPathSocket), string(v1.HostPathCharDev)),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range testCases {
|
|
|
|
for _, pathType := range tc.validpathType {
|
|
|
|
err := checkTypeInternal(&tc, pathType)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d: %q] [%q] expected nil, got %v", i, tc.name, string(*pathType), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pathType := range tc.invalidpathType {
|
|
|
|
checkResult := checkTypeInternal(&tc, pathType)
|
|
|
|
if checkResult == nil {
|
|
|
|
t.Errorf("[%d: %q] [%q] expected error, got nil", i, tc.name, string(*pathType))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|