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 host_path
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2015-09-11 00:20:53 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/testapi"
|
2015-08-13 19:01:50 +00:00
|
|
|
"k8s.io/kubernetes/pkg/client/unversioned/testclient"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/types"
|
|
|
|
"k8s.io/kubernetes/pkg/volume"
|
2014-11-23 15:47:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCanSupport(t *testing.T) {
|
2015-03-19 05:18:31 +00:00
|
|
|
plugMgr := volume.VolumePluginMgr{}
|
2015-08-31 13:44:37 +00:00
|
|
|
plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), volume.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")
|
|
|
|
}
|
|
|
|
if plug.Name() != "kubernetes.io/host-path" {
|
|
|
|
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{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{}
|
2015-08-31 13:44:37 +00:00
|
|
|
plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), volume.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")
|
|
|
|
}
|
|
|
|
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{}
|
2015-09-03 03:14:26 +00:00
|
|
|
pluginHost := volume.NewFakeVolumeHost("/tmp/fake", nil, nil)
|
|
|
|
plugMgr.InitPlugins([]volume.VolumePlugin{&hostPathPlugin{nil, volume.NewFakeRecycler, 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")
|
|
|
|
}
|
|
|
|
recycler, err := plug.NewRecycler(spec)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-23 15:47:25 +00:00
|
|
|
func TestPlugin(t *testing.T) {
|
2015-03-19 05:18:31 +00:00
|
|
|
plugMgr := volume.VolumePluginMgr{}
|
2015-08-31 13:44:37 +00:00
|
|
|
plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), volume.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")
|
|
|
|
}
|
|
|
|
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")}}
|
|
|
|
builder, err := plug.NewBuilder(volume.NewSpecFromVolume(spec), pod, volume.VolumeOptions{}, nil)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
path := builder.GetPath()
|
|
|
|
if path != "/vol1" {
|
|
|
|
t.Errorf("Got unexpected path: %s", path)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := builder.SetUp(); err != nil {
|
|
|
|
t.Errorf("Expected success, got: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-05-04 14:43:10 +00:00
|
|
|
cleaner, err := plug.NewCleaner("vol1", types.UID("poduid"), nil)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if err := cleaner.TearDown(); err != nil {
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
o := testclient.NewObjects(api.Scheme, api.Scheme)
|
|
|
|
o.Add(pv)
|
|
|
|
o.Add(claim)
|
2015-08-03 17:30:35 +00:00
|
|
|
client := &testclient.Fake{}
|
2015-09-11 00:20:53 +00:00
|
|
|
client.AddReactor("*", "*", testclient.ObjectReaction(o, testapi.Default.RESTMapper()))
|
2015-07-01 14:50:39 +00:00
|
|
|
|
|
|
|
plugMgr := volume.VolumePluginMgr{}
|
2015-08-31 13:44:37 +00:00
|
|
|
plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), volume.NewFakeVolumeHost("/tmp/fake", client, nil))
|
2015-07-01 14:50:39 +00:00
|
|
|
plug, _ := plugMgr.FindPluginByName(hostPathPluginName)
|
|
|
|
|
|
|
|
// readOnly bool is supplied by persistent-claim volume source when its builder creates other volumes
|
|
|
|
spec := volume.NewSpecFromPersistentVolume(pv, true)
|
|
|
|
pod := &api.Pod{ObjectMeta: api.ObjectMeta{UID: types.UID("poduid")}}
|
|
|
|
builder, _ := plug.NewBuilder(spec, pod, volume.VolumeOptions{}, nil)
|
|
|
|
|
|
|
|
if !builder.IsReadOnly() {
|
|
|
|
t.Errorf("Expected true for builder.IsReadOnly")
|
|
|
|
}
|
|
|
|
}
|