2015-03-26 18:53:21 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors.
|
2015-03-26 18:53:21 +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 glusterfs
|
|
|
|
|
|
|
|
import (
|
2016-01-25 21:57:42 +00:00
|
|
|
"fmt"
|
2015-03-26 18:53:21 +00:00
|
|
|
"os"
|
2016-10-18 07:54:35 +00:00
|
|
|
"reflect"
|
2015-03-26 18:53:21 +00:00
|
|
|
"testing"
|
|
|
|
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2016-02-16 22:16:45 +00:00
|
|
|
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
|
2016-10-18 07:54:35 +00:00
|
|
|
"k8s.io/kubernetes/pkg/client/testing/core"
|
|
|
|
"k8s.io/kubernetes/pkg/runtime"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/types"
|
|
|
|
"k8s.io/kubernetes/pkg/util/exec"
|
|
|
|
"k8s.io/kubernetes/pkg/util/mount"
|
2016-01-25 21:57:42 +00:00
|
|
|
utiltesting "k8s.io/kubernetes/pkg/util/testing"
|
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"
|
2015-03-26 18:53:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCanSupport(t *testing.T) {
|
2016-01-25 21:57:42 +00:00
|
|
|
tmpDir, err := utiltesting.MkTmpdir("glusterfs_test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error creating temp dir: %v", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
2015-03-26 18:53:21 +00:00
|
|
|
plugMgr := volume.VolumePluginMgr{}
|
2016-05-30 02:22:22 +00:00
|
|
|
plugMgr.InitPlugins(ProbeVolumePlugins(), volumetest.NewFakeVolumeHost(tmpDir, nil, nil, "" /* rootContext */))
|
2015-03-26 18:53:21 +00:00
|
|
|
plug, err := plugMgr.FindPluginByName("kubernetes.io/glusterfs")
|
|
|
|
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/glusterfs" {
|
|
|
|
t.Errorf("Wrong name: %s", plug.GetPluginName())
|
2015-03-26 18:53:21 +00:00
|
|
|
}
|
2015-10-07 16:47:11 +00:00
|
|
|
if plug.CanSupport(&volume.Spec{PersistentVolume: &api.PersistentVolume{Spec: api.PersistentVolumeSpec{PersistentVolumeSource: api.PersistentVolumeSource{}}}}) {
|
|
|
|
t.Errorf("Expected false")
|
2015-05-23 13:53:33 +00:00
|
|
|
}
|
2015-08-12 19:11:03 +00:00
|
|
|
if plug.CanSupport(&volume.Spec{Volume: &api.Volume{VolumeSource: api.VolumeSource{}}}) {
|
2015-03-26 18:53:21 +00:00
|
|
|
t.Errorf("Expected false")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetAccessModes(t *testing.T) {
|
2016-01-25 21:57:42 +00:00
|
|
|
tmpDir, err := utiltesting.MkTmpdir("glusterfs_test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error creating temp dir: %v", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
2015-03-26 18:53:21 +00:00
|
|
|
plugMgr := volume.VolumePluginMgr{}
|
2016-05-30 02:22:22 +00:00
|
|
|
plugMgr.InitPlugins(ProbeVolumePlugins(), volumetest.NewFakeVolumeHost(tmpDir, nil, nil, "" /* rootContext */))
|
2015-03-26 18:53:21 +00:00
|
|
|
|
|
|
|
plug, err := plugMgr.FindPersistentPluginByName("kubernetes.io/glusterfs")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Can't find the plugin by name")
|
|
|
|
}
|
|
|
|
if !contains(plug.GetAccessModes(), api.ReadWriteOnce) || !contains(plug.GetAccessModes(), api.ReadOnlyMany) || !contains(plug.GetAccessModes(), api.ReadWriteMany) {
|
|
|
|
t.Errorf("Expected three AccessModeTypes: %s, %s, and %s", api.ReadWriteOnce, api.ReadOnlyMany, api.ReadWriteMany)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-18 20:22:30 +00:00
|
|
|
func contains(modes []api.PersistentVolumeAccessMode, mode api.PersistentVolumeAccessMode) bool {
|
2015-03-26 18:53:21 +00:00
|
|
|
for _, m := range modes {
|
|
|
|
if m == mode {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-06-01 14:34:40 +00:00
|
|
|
func doTestPlugin(t *testing.T, spec *volume.Spec) {
|
2016-01-25 21:57:42 +00:00
|
|
|
tmpDir, err := utiltesting.MkTmpdir("glusterfs_test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error creating temp dir: %v", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
2015-03-26 18:53:21 +00:00
|
|
|
plugMgr := volume.VolumePluginMgr{}
|
2016-05-30 02:22:22 +00:00
|
|
|
plugMgr.InitPlugins(ProbeVolumePlugins(), volumetest.NewFakeVolumeHost(tmpDir, nil, nil, "" /* rootContext */))
|
2015-03-26 18:53:21 +00:00
|
|
|
plug, err := plugMgr.FindPluginByName("kubernetes.io/glusterfs")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Can't find the plugin by name")
|
|
|
|
}
|
|
|
|
ep := &api.Endpoints{ObjectMeta: api.ObjectMeta{Name: "foo"}, Subsets: []api.EndpointSubset{{
|
|
|
|
Addresses: []api.EndpointAddress{{IP: "127.0.0.1"}}}}}
|
|
|
|
var fcmd exec.FakeCmd
|
|
|
|
fcmd = exec.FakeCmd{
|
|
|
|
CombinedOutputScript: []exec.FakeCombinedOutputAction{
|
|
|
|
// mount
|
|
|
|
func() ([]byte, error) {
|
|
|
|
return []byte{}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
fake := exec.FakeExec{
|
|
|
|
CommandScript: []exec.FakeCommandAction{
|
|
|
|
func(cmd string, args ...string) exec.Cmd { return exec.InitFakeCmd(&fcmd, cmd, args...) },
|
|
|
|
},
|
|
|
|
}
|
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.(*glusterfsPlugin).newMounterInternal(spec, ep, pod, &mount.FakeMounter{}, &fake)
|
|
|
|
volumePath := mounter.GetPath()
|
2015-03-26 18:53:21 +00:00
|
|
|
if err != nil {
|
2016-03-23 05:12:21 +00:00
|
|
|
t.Errorf("Failed to make a new Mounter: %v", err)
|
2015-03-26 18:53:21 +00:00
|
|
|
}
|
2016-03-23 05:12:21 +00:00
|
|
|
if mounter == nil {
|
|
|
|
t.Error("Got a nil Mounter")
|
2015-03-26 18:53:21 +00:00
|
|
|
}
|
2016-03-23 05:12:21 +00:00
|
|
|
path := mounter.GetPath()
|
2016-01-25 21:57:42 +00:00
|
|
|
expectedPath := fmt.Sprintf("%s/pods/poduid/volumes/kubernetes.io~glusterfs/vol1", tmpDir)
|
|
|
|
if path != expectedPath {
|
|
|
|
t.Errorf("Unexpected path, expected %q, got: %q", expectedPath, path)
|
2015-03-26 18:53:21 +00:00
|
|
|
}
|
2016-03-23 05:12:21 +00:00
|
|
|
if err := mounter.SetUp(nil); err != nil {
|
2015-03-26 18:53:21 +00:00
|
|
|
t.Errorf("Expected success, got: %v", err)
|
|
|
|
}
|
|
|
|
if _, err := os.Stat(volumePath); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
t.Errorf("SetUp() failed, volume path not created: %s", volumePath)
|
|
|
|
} else {
|
|
|
|
t.Errorf("SetUp() failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2016-03-23 05:12:21 +00:00
|
|
|
unmounter, err := plug.(*glusterfsPlugin).newUnmounterInternal("vol1", types.UID("poduid"), &mount.FakeMounter{})
|
2015-03-26 18:53:21 +00:00
|
|
|
if err != nil {
|
2016-03-23 05:12:21 +00:00
|
|
|
t.Errorf("Failed to make a new Unmounter: %v", err)
|
2015-03-26 18:53:21 +00:00
|
|
|
}
|
2016-03-23 05:12:21 +00:00
|
|
|
if unmounter == nil {
|
|
|
|
t.Error("Got a nil Unmounter")
|
2015-03-26 18:53:21 +00:00
|
|
|
}
|
2016-03-23 05:12:21 +00:00
|
|
|
if err := unmounter.TearDown(); err != nil {
|
2015-03-26 18:53:21 +00:00
|
|
|
t.Errorf("Expected success, got: %v", err)
|
|
|
|
}
|
|
|
|
if _, err := os.Stat(volumePath); err == nil {
|
|
|
|
t.Errorf("TearDown() failed, volume path still exists: %s", volumePath)
|
|
|
|
} else if !os.IsNotExist(err) {
|
|
|
|
t.Errorf("SetUp() failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2015-06-01 14:34:40 +00:00
|
|
|
|
|
|
|
func TestPluginVolume(t *testing.T) {
|
|
|
|
vol := &api.Volume{
|
|
|
|
Name: "vol1",
|
2015-08-08 01:52:23 +00:00
|
|
|
VolumeSource: api.VolumeSource{Glusterfs: &api.GlusterfsVolumeSource{EndpointsName: "ep", Path: "vol", ReadOnly: false}},
|
2015-06-01 14:34:40 +00:00
|
|
|
}
|
|
|
|
doTestPlugin(t, volume.NewSpecFromVolume(vol))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPluginPersistentVolume(t *testing.T) {
|
|
|
|
vol := &api.PersistentVolume{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "vol1",
|
|
|
|
},
|
|
|
|
Spec: api.PersistentVolumeSpec{
|
|
|
|
PersistentVolumeSource: api.PersistentVolumeSource{
|
2015-08-08 01:52:23 +00:00
|
|
|
Glusterfs: &api.GlusterfsVolumeSource{EndpointsName: "ep", Path: "vol", ReadOnly: false},
|
2015-06-01 14:34:40 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-07-01 14:50:39 +00:00
|
|
|
doTestPlugin(t, volume.NewSpecFromPersistentVolume(vol, false))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPersistentClaimReadOnlyFlag(t *testing.T) {
|
2016-01-25 21:57:42 +00:00
|
|
|
tmpDir, err := utiltesting.MkTmpdir("glusterfs_test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error creating temp dir: %v", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
2015-07-01 14:50:39 +00:00
|
|
|
pv := &api.PersistentVolume{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "pvA",
|
|
|
|
},
|
|
|
|
Spec: api.PersistentVolumeSpec{
|
|
|
|
PersistentVolumeSource: api.PersistentVolumeSource{
|
2015-08-08 01:52:23 +00:00
|
|
|
Glusterfs: &api.GlusterfsVolumeSource{EndpointsName: "ep", Path: "vol", ReadOnly: false},
|
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,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ep := &api.Endpoints{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
2016-06-02 01:47:36 +00:00
|
|
|
Namespace: "nsA",
|
|
|
|
Name: "ep",
|
2015-07-01 14:50:39 +00:00
|
|
|
},
|
|
|
|
Subsets: []api.EndpointSubset{{
|
|
|
|
Addresses: []api.EndpointAddress{{IP: "127.0.0.1"}},
|
2016-07-25 21:34:05 +00:00
|
|
|
Ports: []api.EndpointPort{{Name: "foo", Port: 80, Protocol: api.ProtocolTCP}},
|
2015-07-01 14:50:39 +00:00
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
2016-01-15 05:00:58 +00:00
|
|
|
client := fake.NewSimpleClientset(pv, claim, ep)
|
2015-07-01 14:50:39 +00:00
|
|
|
|
|
|
|
plugMgr := volume.VolumePluginMgr{}
|
2016-05-30 02:22:22 +00:00
|
|
|
plugMgr.InitPlugins(ProbeVolumePlugins(), volumetest.NewFakeVolumeHost(tmpDir, client, nil, "" /* rootContext */))
|
2015-07-01 14:50:39 +00:00
|
|
|
plug, _ := plugMgr.FindPluginByName(glusterfsPluginName)
|
|
|
|
|
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)
|
2016-06-02 01:47:36 +00:00
|
|
|
pod := &api.Pod{ObjectMeta: api.ObjectMeta{Namespace: "nsA", 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
|
|
|
}
|
2015-06-01 14:34:40 +00:00
|
|
|
}
|
2016-09-20 14:24:30 +00:00
|
|
|
|
2016-10-18 07:54:35 +00:00
|
|
|
func TestParseClassParameters(t *testing.T) {
|
|
|
|
secret := api.Secret{
|
2016-10-27 06:21:36 +00:00
|
|
|
Type: "kubernetes.io/glusterfs",
|
2016-10-18 07:54:35 +00:00
|
|
|
Data: map[string][]byte{
|
|
|
|
"data": []byte("mypassword"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
parameters map[string]string
|
|
|
|
secret *api.Secret
|
|
|
|
expectError bool
|
|
|
|
expectConfig *provisioningConfig
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"password",
|
|
|
|
map[string]string{
|
|
|
|
"resturl": "https://localhost:8080",
|
|
|
|
"restuser": "admin",
|
|
|
|
"restuserkey": "password",
|
|
|
|
},
|
|
|
|
nil, // secret
|
|
|
|
false, // expect error
|
|
|
|
&provisioningConfig{
|
|
|
|
url: "https://localhost:8080",
|
|
|
|
user: "admin",
|
|
|
|
userKey: "password",
|
|
|
|
secretValue: "password",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"secret",
|
|
|
|
map[string]string{
|
|
|
|
"resturl": "https://localhost:8080",
|
|
|
|
"restuser": "admin",
|
|
|
|
"secretname": "mysecret",
|
|
|
|
"secretnamespace": "default",
|
|
|
|
},
|
|
|
|
&secret,
|
|
|
|
false, // expect error
|
|
|
|
&provisioningConfig{
|
|
|
|
url: "https://localhost:8080",
|
|
|
|
user: "admin",
|
|
|
|
secretName: "mysecret",
|
|
|
|
secretNamespace: "default",
|
|
|
|
secretValue: "mypassword",
|
|
|
|
},
|
|
|
|
},
|
2016-09-20 14:24:30 +00:00
|
|
|
{
|
2016-10-18 07:54:35 +00:00
|
|
|
"no authentication",
|
|
|
|
map[string]string{
|
|
|
|
"resturl": "https://localhost:8080",
|
|
|
|
"restauthenabled": "false",
|
|
|
|
},
|
|
|
|
&secret,
|
|
|
|
false, // expect error
|
|
|
|
&provisioningConfig{
|
2016-10-19 16:23:17 +00:00
|
|
|
url: "https://localhost:8080",
|
2016-10-18 07:54:35 +00:00
|
|
|
},
|
2016-09-20 14:24:30 +00:00
|
|
|
},
|
|
|
|
{
|
2016-10-18 07:54:35 +00:00
|
|
|
"missing secret",
|
|
|
|
map[string]string{
|
|
|
|
"resturl": "https://localhost:8080",
|
|
|
|
"secretname": "mysecret",
|
|
|
|
"secretnamespace": "default",
|
|
|
|
},
|
|
|
|
nil, // secret
|
|
|
|
true, // expect error
|
|
|
|
nil,
|
2016-09-20 14:24:30 +00:00
|
|
|
},
|
|
|
|
{
|
2016-10-18 07:54:35 +00:00
|
|
|
"secret with no namespace",
|
|
|
|
map[string]string{
|
|
|
|
"resturl": "https://localhost:8080",
|
|
|
|
"secretname": "mysecret",
|
|
|
|
},
|
|
|
|
&secret,
|
|
|
|
true, // expect error
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"missing url",
|
|
|
|
map[string]string{
|
|
|
|
"restuser": "admin",
|
|
|
|
"restuserkey": "password",
|
|
|
|
},
|
|
|
|
nil, // secret
|
|
|
|
true, // expect error
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"unknown parameter",
|
|
|
|
map[string]string{
|
|
|
|
"unknown": "yes",
|
|
|
|
"resturl": "https://localhost:8080",
|
|
|
|
"restuser": "admin",
|
|
|
|
"restuserkey": "password",
|
|
|
|
},
|
|
|
|
nil, // secret
|
|
|
|
true, // expect error
|
|
|
|
nil,
|
2016-09-20 14:24:30 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-10-18 07:54:35 +00:00
|
|
|
for _, test := range tests {
|
2016-09-20 14:24:30 +00:00
|
|
|
|
2016-10-18 07:54:35 +00:00
|
|
|
client := &fake.Clientset{}
|
|
|
|
client.AddReactor("get", "secrets", func(action core.Action) (handled bool, ret runtime.Object, err error) {
|
|
|
|
if test.secret != nil {
|
|
|
|
return true, test.secret, nil
|
|
|
|
}
|
|
|
|
return true, nil, fmt.Errorf("Test %s did not set a secret", test.name)
|
|
|
|
})
|
2016-09-20 14:24:30 +00:00
|
|
|
|
2016-10-18 07:54:35 +00:00
|
|
|
cfg, err := parseClassParameters(test.parameters, client)
|
|
|
|
|
|
|
|
if err != nil && !test.expectError {
|
|
|
|
t.Errorf("Test %s got unexpected error %v", test.name, err)
|
2016-09-20 14:24:30 +00:00
|
|
|
}
|
2016-10-18 07:54:35 +00:00
|
|
|
if err == nil && test.expectError {
|
|
|
|
t.Errorf("test %s expected error and got none", test.name)
|
2016-09-20 14:24:30 +00:00
|
|
|
}
|
2016-10-18 07:54:35 +00:00
|
|
|
if test.expectConfig != nil {
|
|
|
|
if !reflect.DeepEqual(cfg, test.expectConfig) {
|
|
|
|
t.Errorf("Test %s returned unexpected data, expected: %+v, got: %+v", test.name, test.expectConfig, cfg)
|
|
|
|
}
|
2016-09-20 14:24:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|