2017-04-14 15:43:30 -07:00
|
|
|
/*
|
|
|
|
|
Copyright 2017 The Rook Authors. All rights reserved.
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
*/
|
2017-07-24 07:43:11 -07:00
|
|
|
package provisioner
|
2017-04-14 15:43:30 -07:00
|
|
|
|
|
|
|
|
import (
|
2017-09-14 09:40:34 -07:00
|
|
|
"io/ioutil"
|
|
|
|
|
"os"
|
|
|
|
|
"path"
|
|
|
|
|
"strings"
|
2017-04-14 15:43:30 -07:00
|
|
|
"testing"
|
|
|
|
|
|
2017-07-28 16:19:34 -07:00
|
|
|
"github.com/rook/rook/pkg/clusterd"
|
2017-12-04 08:33:19 -08:00
|
|
|
cephtest "github.com/rook/rook/pkg/daemon/ceph/test"
|
2017-10-27 14:51:50 -07:00
|
|
|
"github.com/rook/rook/pkg/operator/provisioner/controller"
|
2017-07-28 16:19:34 -07:00
|
|
|
"github.com/rook/rook/pkg/operator/test"
|
2017-09-14 09:40:34 -07:00
|
|
|
exectest "github.com/rook/rook/pkg/util/exec/test"
|
2017-04-14 15:43:30 -07:00
|
|
|
"github.com/stretchr/testify/assert"
|
2017-09-14 09:40:34 -07:00
|
|
|
"k8s.io/api/core/v1"
|
|
|
|
|
storagebeta "k8s.io/api/storage/v1beta1"
|
|
|
|
|
"k8s.io/apimachinery/pkg/api/resource"
|
2017-07-28 16:19:34 -07:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2017-09-14 09:40:34 -07:00
|
|
|
"k8s.io/apimachinery/pkg/types"
|
2017-04-14 15:43:30 -07:00
|
|
|
)
|
|
|
|
|
|
2017-09-14 09:40:34 -07:00
|
|
|
func TestProvisionImage(t *testing.T) {
|
2017-07-28 16:19:34 -07:00
|
|
|
clientset := test.New(3)
|
2017-09-14 09:40:34 -07:00
|
|
|
namespace := "ns"
|
|
|
|
|
configDir, _ := ioutil.TempDir("", "")
|
|
|
|
|
defer os.RemoveAll(configDir)
|
|
|
|
|
executor := &exectest.MockExecutor{
|
|
|
|
|
MockExecuteCommandWithOutput: func(debug bool, actionName string, command string, args ...string) (string, error) {
|
|
|
|
|
if strings.Contains(command, "ceph-authtool") {
|
2017-10-17 09:33:37 -07:00
|
|
|
cephtest.CreateConfigDir(path.Join(configDir, namespace))
|
2017-09-14 09:40:34 -07:00
|
|
|
}
|
2017-04-14 15:43:30 -07:00
|
|
|
|
2017-09-14 09:40:34 -07:00
|
|
|
if command == "rbd" && args[0] == "create" {
|
|
|
|
|
return `[{"image":"pvc-uid-1-1","size":1048576,"format":2}]`, nil
|
|
|
|
|
}
|
2017-07-28 16:19:34 -07:00
|
|
|
|
2017-09-14 09:40:34 -07:00
|
|
|
if command == "rbd" && args[0] == "ls" && args[1] == "-l" {
|
|
|
|
|
return `[{"image":"pvc-uid-1-1","size":1048576,"format":2}]`, nil
|
|
|
|
|
}
|
|
|
|
|
return "", nil
|
|
|
|
|
},
|
2017-07-28 16:19:34 -07:00
|
|
|
}
|
2017-09-14 09:40:34 -07:00
|
|
|
|
|
|
|
|
context := &clusterd.Context{
|
|
|
|
|
Clientset: clientset,
|
|
|
|
|
Executor: executor,
|
|
|
|
|
ConfigDir: configDir,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
provisioner := New(context)
|
|
|
|
|
volume := newVolumeOptions(newStorageClass("class-1", "rook.io/block", map[string]string{"pool": "testpool", "clusterName": "testCluster", "fsType": "ext3"}), newClaim("claim-1", "uid-1-1", "class-1", "", "class-1", nil))
|
|
|
|
|
|
|
|
|
|
pv, err := provisioner.Provision(volume)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
|
|
assert.Equal(t, "pvc-uid-1-1", pv.Name)
|
|
|
|
|
assert.NotNil(t, pv.Spec.PersistentVolumeSource.FlexVolume)
|
|
|
|
|
assert.Equal(t, "rook.io/rook", pv.Spec.PersistentVolumeSource.FlexVolume.Driver)
|
|
|
|
|
assert.Equal(t, "ext3", pv.Spec.PersistentVolumeSource.FlexVolume.FSType)
|
|
|
|
|
assert.Equal(t, "class-1", pv.Spec.PersistentVolumeSource.FlexVolume.Options["storageClass"])
|
|
|
|
|
assert.Equal(t, "testpool", pv.Spec.PersistentVolumeSource.FlexVolume.Options["pool"])
|
|
|
|
|
assert.Equal(t, "pvc-uid-1-1", pv.Spec.PersistentVolumeSource.FlexVolume.Options["image"])
|
2017-04-14 15:43:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestParseClassParameters(t *testing.T) {
|
|
|
|
|
cfg := make(map[string]string)
|
|
|
|
|
cfg["pool"] = "testPool"
|
|
|
|
|
cfg["clustername"] = "myname"
|
2017-09-14 09:40:34 -07:00
|
|
|
cfg["fstype"] = "ext4"
|
2017-04-14 15:43:30 -07:00
|
|
|
|
|
|
|
|
provConfig, err := parseClassParameters(cfg)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
|
|
assert.Equal(t, "testPool", provConfig.pool)
|
|
|
|
|
assert.Equal(t, "myname", provConfig.clusterName)
|
2017-09-14 09:40:34 -07:00
|
|
|
assert.Equal(t, "ext4", provConfig.fstype)
|
2017-04-14 15:43:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestParseClassParametersDefault(t *testing.T) {
|
|
|
|
|
cfg := make(map[string]string)
|
|
|
|
|
cfg["pool"] = "testPool"
|
|
|
|
|
|
|
|
|
|
provConfig, err := parseClassParameters(cfg)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
|
|
assert.Equal(t, "testPool", provConfig.pool)
|
|
|
|
|
assert.Equal(t, "rook", provConfig.clusterName)
|
2017-09-14 09:40:34 -07:00
|
|
|
assert.Equal(t, "", provConfig.fstype)
|
2017-04-14 15:43:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestParseClassParametersNoPool(t *testing.T) {
|
|
|
|
|
cfg := make(map[string]string)
|
|
|
|
|
cfg["clustername"] = "myname"
|
|
|
|
|
|
|
|
|
|
_, err := parseClassParameters(cfg)
|
|
|
|
|
assert.EqualError(t, err, "StorageClass for provisioner rookVolumeProvisioner must contain 'pool' parameter")
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestParseClassParametersInvalidOption(t *testing.T) {
|
|
|
|
|
cfg := make(map[string]string)
|
|
|
|
|
cfg["pool"] = "testPool"
|
|
|
|
|
cfg["foo"] = "bar"
|
|
|
|
|
|
|
|
|
|
_, err := parseClassParameters(cfg)
|
|
|
|
|
assert.EqualError(t, err, "invalid option \"foo\" for volume plugin rookVolumeProvisioner")
|
|
|
|
|
}
|
2017-04-20 16:28:29 -07:00
|
|
|
|
2017-09-14 09:40:34 -07:00
|
|
|
func newVolumeOptions(storageClass *storagebeta.StorageClass, claim *v1.PersistentVolumeClaim) controller.VolumeOptions {
|
|
|
|
|
return controller.VolumeOptions{
|
|
|
|
|
PersistentVolumeReclaimPolicy: v1.PersistentVolumeReclaimDelete,
|
|
|
|
|
PVName: "pvc-" + string(claim.ObjectMeta.UID),
|
|
|
|
|
PVC: claim,
|
|
|
|
|
Parameters: storageClass.Parameters,
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-04-20 16:28:29 -07:00
|
|
|
|
2017-09-14 09:40:34 -07:00
|
|
|
func newStorageClass(name, provisioner string, parameters map[string]string) *storagebeta.StorageClass {
|
|
|
|
|
return &storagebeta.StorageClass{
|
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
|
Name: name,
|
|
|
|
|
},
|
|
|
|
|
Provisioner: provisioner,
|
|
|
|
|
Parameters: parameters,
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-04-20 16:28:29 -07:00
|
|
|
|
2017-09-14 09:40:34 -07:00
|
|
|
func newClaim(name, claimUID, provisioner, volumeName, storageclassName string, annotations map[string]string) *v1.PersistentVolumeClaim {
|
|
|
|
|
claim := &v1.PersistentVolumeClaim{
|
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
|
Name: name,
|
|
|
|
|
Namespace: v1.NamespaceDefault,
|
|
|
|
|
UID: types.UID(claimUID),
|
|
|
|
|
ResourceVersion: "0",
|
|
|
|
|
SelfLink: "/api/v1/namespaces/" + v1.NamespaceDefault + "/persistentvolumeclaims/" + name,
|
|
|
|
|
},
|
|
|
|
|
Spec: v1.PersistentVolumeClaimSpec{
|
|
|
|
|
AccessModes: []v1.PersistentVolumeAccessMode{v1.ReadWriteOnce, v1.ReadOnlyMany},
|
|
|
|
|
Resources: v1.ResourceRequirements{
|
|
|
|
|
Requests: v1.ResourceList{
|
|
|
|
|
v1.ResourceName(v1.ResourceStorage): resource.MustParse("1Mi"),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
VolumeName: volumeName,
|
|
|
|
|
StorageClassName: &storageclassName,
|
|
|
|
|
},
|
|
|
|
|
Status: v1.PersistentVolumeClaimStatus{
|
|
|
|
|
Phase: v1.ClaimPending,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
for k, v := range annotations {
|
|
|
|
|
claim.Annotations[k] = v
|
|
|
|
|
}
|
|
|
|
|
return claim
|
2017-04-20 16:28:29 -07:00
|
|
|
}
|