2015-09-30 18:31:53 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors.
|
2015-09-30 18:31:53 +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 flexvolume
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"testing"
|
|
|
|
"text/template"
|
|
|
|
|
2017-06-22 17:25:57 +00:00
|
|
|
"k8s.io/api/core/v1"
|
2017-06-22 18:24:23 +00:00
|
|
|
utiltesting "k8s.io/client-go/util/testing"
|
2015-09-30 18:31:53 +00:00
|
|
|
"k8s.io/kubernetes/pkg/volume"
|
2016-02-29 21:31:42 +00:00
|
|
|
volumetest "k8s.io/kubernetes/pkg/volume/testing"
|
2018-06-27 20:06:35 +00:00
|
|
|
"k8s.io/utils/exec"
|
2015-09-30 18:31:53 +00:00
|
|
|
)
|
|
|
|
|
2018-04-16 16:31:44 +00:00
|
|
|
const execScriptTempl1 = `#!/usr/bin/env bash
|
2015-09-30 18:31:53 +00:00
|
|
|
if [ "$1" == "init" -a $# -eq 1 ]; then
|
|
|
|
echo -n '{
|
|
|
|
"status": "Success"
|
|
|
|
}'
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
PATH=$2
|
|
|
|
if [ "$1" == "attach" -a $# -eq 2 ]; then
|
|
|
|
echo -n '{
|
|
|
|
"device": "{{.DevicePath}}",
|
|
|
|
"status": "Success"
|
|
|
|
}'
|
|
|
|
exit 0
|
|
|
|
elif [ "$1" == "detach" -a $# -eq 2 ]; then
|
|
|
|
echo -n '{
|
|
|
|
"status": "Success"
|
|
|
|
}'
|
|
|
|
exit 0
|
2017-02-21 12:49:36 +00:00
|
|
|
elif [ "$1" == "getvolumename" -a $# -eq 4 ]; then
|
2015-09-30 18:31:53 +00:00
|
|
|
echo -n '{
|
2017-02-21 12:49:36 +00:00
|
|
|
"status": "Success",
|
|
|
|
"volume": "fakevolume"
|
2015-09-30 18:31:53 +00:00
|
|
|
}'
|
|
|
|
exit 0
|
2017-02-21 12:49:36 +00:00
|
|
|
elif [ "$1" == "isattached" -a $# -eq 2 ]; then
|
2015-09-30 18:31:53 +00:00
|
|
|
echo -n '{
|
2017-02-21 12:49:36 +00:00
|
|
|
"status": "Success",
|
|
|
|
"attached": true
|
2015-09-30 18:31:53 +00:00
|
|
|
}'
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo -n '{
|
2017-02-21 12:49:36 +00:00
|
|
|
"status": "Not supported"
|
2015-09-30 18:31:53 +00:00
|
|
|
}'
|
|
|
|
exit 1
|
|
|
|
|
|
|
|
# Direct the arguments to a file to be tested against later
|
|
|
|
echo -n $@ &> {{.OutputFile}}
|
|
|
|
`
|
|
|
|
|
2018-04-16 16:31:44 +00:00
|
|
|
const execScriptTempl2 = `#!/usr/bin/env bash
|
2015-09-30 18:31:53 +00:00
|
|
|
if [ "$1" == "init" -a $# -eq 1 ]; then
|
|
|
|
echo -n '{
|
|
|
|
"status": "Success"
|
|
|
|
}'
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2017-02-21 12:49:36 +00:00
|
|
|
if [ "$1" == "getvolumename" -a $# -eq 2 ]; then
|
2015-09-30 18:31:53 +00:00
|
|
|
echo -n '{
|
2017-02-21 12:49:36 +00:00
|
|
|
"status": "Success",
|
|
|
|
"volumeName": "fakevolume"
|
2015-09-30 18:31:53 +00:00
|
|
|
}'
|
|
|
|
exit 0
|
|
|
|
elif [ "$1" == "mount" -a $# -eq 4 ]; then
|
|
|
|
PATH=$2
|
|
|
|
/bin/mkdir -p $PATH
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
echo -n '{
|
|
|
|
"status": "Failure",
|
|
|
|
"reason": "Failed to create $PATH"
|
|
|
|
}'
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
echo -n '{
|
|
|
|
"status": "Success"
|
|
|
|
}'
|
|
|
|
exit 0
|
|
|
|
elif [ "$1" == "unmount" -a $# -eq 2 ]; then
|
|
|
|
PATH=$2
|
|
|
|
/bin/rm -r $PATH
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
echo -n '{
|
|
|
|
"status": "Failure",
|
|
|
|
"reason": "Failed to cleanup $PATH"
|
|
|
|
}'
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
echo -n '{
|
|
|
|
"status": "Success"
|
|
|
|
}'
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo -n '{
|
2017-02-21 12:49:36 +00:00
|
|
|
"status": "Not Supported"
|
2015-09-30 18:31:53 +00:00
|
|
|
}'
|
|
|
|
exit 1
|
|
|
|
|
|
|
|
# Direct the arguments to a file to be tested against later
|
|
|
|
echo -n $@ &> {{.OutputFile}}
|
|
|
|
`
|
|
|
|
|
2016-01-25 21:57:42 +00:00
|
|
|
func installPluginUnderTest(t *testing.T, vendorName, plugName, tmpDir string, execScriptTempl string, execTemplateData *map[string]interface{}) {
|
2015-09-30 18:31:53 +00:00
|
|
|
vendoredName := plugName
|
|
|
|
if vendorName != "" {
|
|
|
|
vendoredName = fmt.Sprintf("%s~%s", vendorName, plugName)
|
|
|
|
}
|
2016-01-25 21:57:42 +00:00
|
|
|
pluginDir := path.Join(tmpDir, vendoredName)
|
2015-09-30 18:31:53 +00:00
|
|
|
err := os.MkdirAll(pluginDir, 0777)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to create plugin: %v", err)
|
|
|
|
}
|
|
|
|
pluginExec := path.Join(pluginDir, plugName)
|
|
|
|
f, err := os.Create(pluginExec)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to install plugin")
|
|
|
|
}
|
|
|
|
err = f.Chmod(0777)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to set exec perms on plugin")
|
|
|
|
}
|
|
|
|
if execTemplateData == nil {
|
|
|
|
execTemplateData = &map[string]interface{}{
|
|
|
|
"DevicePath": "/dev/sdx",
|
|
|
|
"OutputFile": path.Join(pluginDir, plugName+".out"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tObj := template.Must(template.New("test").Parse(execScriptTempl))
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
if err := tObj.Execute(buf, *execTemplateData); err != nil {
|
|
|
|
t.Errorf("Error in executing script template - %v", err)
|
|
|
|
}
|
|
|
|
execScript := buf.String()
|
|
|
|
_, err = f.WriteString(execScript)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to write plugin exec")
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCanSupport(t *testing.T) {
|
2016-01-25 21:57:42 +00:00
|
|
|
tmpDir, err := utiltesting.MkTmpdir("flexvolume_test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error creating temp dir: %v", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
2015-09-30 18:31:53 +00:00
|
|
|
plugMgr := volume.VolumePluginMgr{}
|
2018-06-27 20:06:35 +00:00
|
|
|
runner := exec.New()
|
2016-01-25 21:57:42 +00:00
|
|
|
installPluginUnderTest(t, "kubernetes.io", "fakeAttacher", tmpDir, execScriptTempl1, nil)
|
2018-06-27 20:06:35 +00:00
|
|
|
plugMgr.InitPlugins(nil, GetDynamicPluginProber(tmpDir, runner), volumetest.NewFakeVolumeHost("fake", nil, nil))
|
2017-05-24 20:25:05 +00:00
|
|
|
plugin, err := plugMgr.FindPluginByName("flexvolume-kubernetes.io/fakeAttacher")
|
2015-09-30 18:31:53 +00:00
|
|
|
if err != nil {
|
2017-07-19 14:06:41 +00:00
|
|
|
t.Fatalf("Can't find the plugin by name")
|
2015-09-30 18:31:53 +00:00
|
|
|
}
|
2017-05-24 20:25:05 +00:00
|
|
|
if plugin.GetPluginName() != "flexvolume-kubernetes.io/fakeAttacher" {
|
2016-05-30 02:22:22 +00:00
|
|
|
t.Errorf("Wrong name: %s", plugin.GetPluginName())
|
2015-09-30 18:31:53 +00:00
|
|
|
}
|
2016-11-18 20:58:56 +00:00
|
|
|
if !plugin.CanSupport(&volume.Spec{Volume: &v1.Volume{VolumeSource: v1.VolumeSource{FlexVolume: &v1.FlexVolumeSource{Driver: "kubernetes.io/fakeAttacher"}}}}) {
|
2015-09-30 18:31:53 +00:00
|
|
|
t.Errorf("Expected true")
|
|
|
|
}
|
2017-11-28 04:19:38 +00:00
|
|
|
if !plugin.CanSupport(&volume.Spec{PersistentVolume: &v1.PersistentVolume{Spec: v1.PersistentVolumeSpec{PersistentVolumeSource: v1.PersistentVolumeSource{FlexVolume: &v1.FlexPersistentVolumeSource{Driver: "kubernetes.io/fakeAttacher"}}}}}) {
|
2015-09-30 18:31:53 +00:00
|
|
|
t.Errorf("Expected true")
|
|
|
|
}
|
2016-11-18 20:58:56 +00:00
|
|
|
if plugin.CanSupport(&volume.Spec{Volume: &v1.Volume{VolumeSource: v1.VolumeSource{}}}) {
|
2015-09-30 18:31:53 +00:00
|
|
|
t.Errorf("Expected false")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetAccessModes(t *testing.T) {
|
2016-01-25 21:57:42 +00:00
|
|
|
tmpDir, err := utiltesting.MkTmpdir("flexvolume_test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error creating temp dir: %v", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
2015-09-30 18:31:53 +00:00
|
|
|
plugMgr := volume.VolumePluginMgr{}
|
2018-06-27 20:06:35 +00:00
|
|
|
runner := exec.New()
|
2016-01-25 21:57:42 +00:00
|
|
|
installPluginUnderTest(t, "kubernetes.io", "fakeAttacher", tmpDir, execScriptTempl1, nil)
|
2018-06-27 20:06:35 +00:00
|
|
|
plugMgr.InitPlugins(nil, GetDynamicPluginProber(tmpDir, runner), volumetest.NewFakeVolumeHost(tmpDir, nil, nil))
|
2015-09-30 18:31:53 +00:00
|
|
|
|
2017-05-24 20:25:05 +00:00
|
|
|
plugin, err := plugMgr.FindPersistentPluginByName("flexvolume-kubernetes.io/fakeAttacher")
|
2015-09-30 18:31:53 +00:00
|
|
|
if err != nil {
|
2016-01-25 21:57:42 +00:00
|
|
|
t.Fatalf("Can't find the plugin by name")
|
2015-09-30 18:31:53 +00:00
|
|
|
}
|
2017-10-20 16:35:47 +00:00
|
|
|
if !volumetest.ContainsAccessMode(plugin.GetAccessModes(), v1.ReadWriteOnce) || !volumetest.ContainsAccessMode(plugin.GetAccessModes(), v1.ReadOnlyMany) {
|
2016-11-18 20:58:56 +00:00
|
|
|
t.Errorf("Expected two AccessModeTypes: %s and %s", v1.ReadWriteOnce, v1.ReadOnlyMany)
|
2015-09-30 18:31:53 +00:00
|
|
|
}
|
|
|
|
}
|