2020-12-07 21:16:53 +00:00
|
|
|
package csi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-06-30 00:39:09 +00:00
|
|
|
"strings"
|
2020-12-07 21:16:53 +00:00
|
|
|
|
|
|
|
kansnapshot "github.com/kanisterio/kanister/pkg/kube/snapshot"
|
|
|
|
"github.com/kanisterio/kanister/pkg/kube/snapshot/apis/v1alpha1"
|
2020-12-16 22:23:06 +00:00
|
|
|
"github.com/kanisterio/kanister/pkg/kube/snapshot/apis/v1beta1"
|
2020-12-29 19:46:29 +00:00
|
|
|
"github.com/kastenhq/kubestr/pkg/common"
|
2020-12-07 21:16:53 +00:00
|
|
|
"github.com/kastenhq/kubestr/pkg/csi/types"
|
2021-01-13 02:48:51 +00:00
|
|
|
snapv1 "github.com/kubernetes-csi/external-snapshotter/client/v4/apis/volumesnapshot/v1"
|
2022-10-25 19:02:47 +00:00
|
|
|
pkgerrors "github.com/pkg/errors"
|
2020-12-07 21:16:53 +00:00
|
|
|
. "gopkg.in/check.v1"
|
|
|
|
v1 "k8s.io/api/core/v1"
|
|
|
|
sv1 "k8s.io/api/storage/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/api/resource"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
2024-08-05 08:39:36 +00:00
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
2020-12-07 21:16:53 +00:00
|
|
|
discoveryfake "k8s.io/client-go/discovery/fake"
|
|
|
|
"k8s.io/client-go/dynamic"
|
|
|
|
fakedynamic "k8s.io/client-go/dynamic/fake"
|
|
|
|
"k8s.io/client-go/kubernetes"
|
|
|
|
"k8s.io/client-go/kubernetes/fake"
|
|
|
|
k8stesting "k8s.io/client-go/testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *CSITestSuite) TestGetDriverNameFromUVSC(c *C) {
|
|
|
|
|
|
|
|
for _, tc := range []struct {
|
|
|
|
vsc unstructured.Unstructured
|
|
|
|
version string
|
|
|
|
expOut string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
vsc: unstructured.Unstructured{
|
|
|
|
Object: map[string]interface{}{
|
2020-12-29 19:46:29 +00:00
|
|
|
common.VolSnapClassAlphaDriverKey: "p2",
|
2020-12-07 21:16:53 +00:00
|
|
|
},
|
|
|
|
},
|
2020-12-29 19:46:29 +00:00
|
|
|
version: common.SnapshotAlphaVersion,
|
2020-12-07 21:16:53 +00:00
|
|
|
expOut: "p2",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
vsc: unstructured.Unstructured{
|
|
|
|
Object: map[string]interface{}{},
|
|
|
|
},
|
2020-12-29 19:46:29 +00:00
|
|
|
version: common.SnapshotAlphaVersion,
|
2020-12-07 21:16:53 +00:00
|
|
|
expOut: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
vsc: unstructured.Unstructured{
|
|
|
|
Object: map[string]interface{}{
|
2020-12-29 19:46:29 +00:00
|
|
|
common.VolSnapClassBetaDriverKey: "p2",
|
2020-12-07 21:16:53 +00:00
|
|
|
},
|
|
|
|
},
|
2020-12-29 19:46:29 +00:00
|
|
|
version: common.SnapshotBetaVersion,
|
2020-12-07 21:16:53 +00:00
|
|
|
expOut: "p2",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
vsc: unstructured.Unstructured{
|
|
|
|
Object: map[string]interface{}{},
|
|
|
|
},
|
2020-12-29 19:46:29 +00:00
|
|
|
version: common.SnapshotBetaVersion,
|
2020-12-07 21:16:53 +00:00
|
|
|
expOut: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
vsc: unstructured.Unstructured{
|
|
|
|
Object: map[string]interface{}{
|
2020-12-29 19:46:29 +00:00
|
|
|
common.VolSnapClassBetaDriverKey: map[string]string{},
|
2020-12-07 21:16:53 +00:00
|
|
|
},
|
|
|
|
},
|
2020-12-29 19:46:29 +00:00
|
|
|
version: common.SnapshotBetaVersion,
|
|
|
|
expOut: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
vsc: unstructured.Unstructured{
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
common.VolSnapClassStableDriverKey: "p2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
version: common.SnapshotStableVersion,
|
|
|
|
expOut: "p2",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
vsc: unstructured.Unstructured{
|
|
|
|
Object: map[string]interface{}{},
|
|
|
|
},
|
|
|
|
version: common.SnapshotStableVersion,
|
|
|
|
expOut: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
vsc: unstructured.Unstructured{
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
common.VolSnapClassStableDriverKey: map[string]string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
version: common.SnapshotStableVersion,
|
2020-12-07 21:16:53 +00:00
|
|
|
expOut: "",
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
driverName := getDriverNameFromUVSC(tc.vsc, tc.version)
|
|
|
|
c.Assert(driverName, Equals, tc.expOut)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CSITestSuite) TestGetCSISnapshotGroupVersion(c *C) {
|
|
|
|
for _, tc := range []struct {
|
|
|
|
cli kubernetes.Interface
|
|
|
|
resources []*metav1.APIResourceList
|
|
|
|
errChecker Checker
|
|
|
|
gvChecker Checker
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
cli: fake.NewSimpleClientset(),
|
|
|
|
resources: []*metav1.APIResourceList{
|
|
|
|
{
|
|
|
|
GroupVersion: "/////",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
errChecker: NotNil,
|
|
|
|
gvChecker: IsNil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cli: fake.NewSimpleClientset(),
|
|
|
|
resources: []*metav1.APIResourceList{
|
|
|
|
{
|
|
|
|
GroupVersion: "snapshot.storage.k8s.io/v1alpha1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
errChecker: IsNil,
|
|
|
|
gvChecker: NotNil,
|
|
|
|
},
|
2021-01-13 02:48:51 +00:00
|
|
|
{
|
|
|
|
cli: fake.NewSimpleClientset(),
|
|
|
|
resources: []*metav1.APIResourceList{
|
|
|
|
{
|
|
|
|
GroupVersion: "snapshot.storage.k8s.io/v1beta1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
errChecker: IsNil,
|
|
|
|
gvChecker: NotNil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cli: fake.NewSimpleClientset(),
|
|
|
|
resources: []*metav1.APIResourceList{
|
|
|
|
{
|
|
|
|
GroupVersion: "snapshot.storage.k8s.io/v1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
errChecker: IsNil,
|
|
|
|
gvChecker: NotNil,
|
|
|
|
},
|
2020-12-07 21:16:53 +00:00
|
|
|
{
|
|
|
|
cli: fake.NewSimpleClientset(),
|
|
|
|
resources: []*metav1.APIResourceList{
|
|
|
|
{
|
|
|
|
GroupVersion: "notrbac.authorization.k8s.io/v1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
errChecker: NotNil,
|
|
|
|
gvChecker: IsNil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cli: nil,
|
|
|
|
resources: nil,
|
|
|
|
errChecker: NotNil,
|
|
|
|
gvChecker: IsNil,
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
cli := tc.cli
|
|
|
|
if cli != nil {
|
|
|
|
cli.Discovery().(*discoveryfake.FakeDiscovery).Resources = tc.resources
|
|
|
|
}
|
|
|
|
p := &apiVersionFetch{kubeCli: cli}
|
|
|
|
gv, err := p.GetCSISnapshotGroupVersion()
|
|
|
|
c.Check(err, tc.errChecker)
|
|
|
|
c.Check(gv, tc.gvChecker)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-24 21:23:35 +00:00
|
|
|
func (s *CSITestSuite) TestValidatePVC(c *C) {
|
|
|
|
ctx := context.Background()
|
|
|
|
ops := NewArgumentValidator(fake.NewSimpleClientset(), nil)
|
|
|
|
pvc, err := ops.ValidatePVC(ctx, "pvc", "ns")
|
|
|
|
c.Check(err, NotNil)
|
|
|
|
c.Check(pvc, IsNil)
|
|
|
|
|
|
|
|
ops = NewArgumentValidator(fake.NewSimpleClientset(&v1.PersistentVolumeClaim{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Namespace: "ns",
|
|
|
|
Name: "pvc",
|
|
|
|
},
|
|
|
|
}), nil)
|
|
|
|
pvc, err = ops.ValidatePVC(ctx, "pvc", "ns")
|
|
|
|
c.Check(err, IsNil)
|
|
|
|
c.Check(pvc, NotNil)
|
|
|
|
|
|
|
|
ops = NewArgumentValidator(nil, nil)
|
|
|
|
pvc, err = ops.ValidatePVC(ctx, "pvc", "ns")
|
|
|
|
c.Check(err, NotNil)
|
|
|
|
c.Check(pvc, IsNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CSITestSuite) TestFetchPV(c *C) {
|
|
|
|
ctx := context.Background()
|
|
|
|
ops := NewArgumentValidator(fake.NewSimpleClientset(), nil)
|
|
|
|
pv, err := ops.FetchPV(ctx, "pv")
|
|
|
|
c.Check(err, NotNil)
|
|
|
|
c.Check(pv, IsNil)
|
|
|
|
|
|
|
|
ops = NewArgumentValidator(fake.NewSimpleClientset(&v1.PersistentVolume{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: "pv",
|
|
|
|
},
|
|
|
|
}), nil)
|
|
|
|
pv, err = ops.FetchPV(ctx, "pv")
|
|
|
|
c.Check(err, IsNil)
|
|
|
|
c.Check(pv, NotNil)
|
|
|
|
|
|
|
|
ops = NewArgumentValidator(nil, nil)
|
|
|
|
pv, err = ops.FetchPV(ctx, "pv")
|
|
|
|
c.Check(err, NotNil)
|
|
|
|
c.Check(pv, IsNil)
|
|
|
|
}
|
|
|
|
|
2020-12-07 21:16:53 +00:00
|
|
|
func (s *CSITestSuite) TestValidateNamespace(c *C) {
|
|
|
|
ctx := context.Background()
|
2023-07-24 21:23:35 +00:00
|
|
|
ops := NewArgumentValidator(fake.NewSimpleClientset(), nil)
|
2020-12-07 21:16:53 +00:00
|
|
|
err := ops.ValidateNamespace(ctx, "ns")
|
|
|
|
c.Check(err, NotNil)
|
|
|
|
|
2023-07-24 21:23:35 +00:00
|
|
|
ops = NewArgumentValidator(fake.NewSimpleClientset(&v1.Namespace{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: "ns",
|
|
|
|
},
|
|
|
|
}), nil)
|
2020-12-07 21:16:53 +00:00
|
|
|
err = ops.ValidateNamespace(ctx, "ns")
|
|
|
|
c.Check(err, IsNil)
|
|
|
|
|
2023-07-24 21:23:35 +00:00
|
|
|
ops = NewArgumentValidator(nil, nil)
|
2020-12-07 21:16:53 +00:00
|
|
|
err = ops.ValidateNamespace(ctx, "ns")
|
|
|
|
c.Check(err, NotNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CSITestSuite) TestValidateStorageClass(c *C) {
|
|
|
|
ctx := context.Background()
|
|
|
|
ops := &validateOperations{
|
|
|
|
kubeCli: fake.NewSimpleClientset(),
|
|
|
|
}
|
|
|
|
sc, err := ops.ValidateStorageClass(ctx, "sc")
|
|
|
|
c.Check(err, NotNil)
|
|
|
|
c.Check(sc, IsNil)
|
|
|
|
|
|
|
|
ops = &validateOperations{
|
|
|
|
kubeCli: fake.NewSimpleClientset(&sv1.StorageClass{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: "sc",
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
sc, err = ops.ValidateStorageClass(ctx, "sc")
|
|
|
|
c.Check(err, IsNil)
|
|
|
|
c.Check(sc, NotNil)
|
|
|
|
|
|
|
|
ops = &validateOperations{
|
|
|
|
kubeCli: nil,
|
|
|
|
}
|
|
|
|
sc, err = ops.ValidateStorageClass(ctx, "sc")
|
|
|
|
c.Check(err, NotNil)
|
|
|
|
c.Check(sc, IsNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CSITestSuite) TestValidateVolumeSnapshotClass(c *C) {
|
|
|
|
ctx := context.Background()
|
2021-01-13 02:48:51 +00:00
|
|
|
for _, tc := range []struct {
|
|
|
|
ops *validateOperations
|
|
|
|
groupVersion string
|
|
|
|
version string
|
|
|
|
errChecker Checker
|
|
|
|
uVCSChecker Checker
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
ops: &validateOperations{
|
|
|
|
dynCli: fakedynamic.NewSimpleDynamicClient(runtime.NewScheme()),
|
|
|
|
},
|
|
|
|
groupVersion: common.SnapshotAlphaVersion,
|
|
|
|
errChecker: NotNil,
|
|
|
|
uVCSChecker: IsNil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ops: &validateOperations{
|
|
|
|
dynCli: fakedynamic.NewSimpleDynamicClient(
|
|
|
|
runtime.NewScheme(),
|
|
|
|
&unstructured.Unstructured{
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
"apiVersion": fmt.Sprintf("%s/%s", v1alpha1.GroupName, v1alpha1.Version),
|
|
|
|
"kind": "VolumeSnapshotClass",
|
|
|
|
"metadata": map[string]interface{}{
|
|
|
|
"name": "vsc",
|
|
|
|
},
|
|
|
|
"snapshotter": "somesnapshotter",
|
|
|
|
"deletionPolicy": "Delete",
|
|
|
|
},
|
2020-12-07 21:16:53 +00:00
|
|
|
},
|
2021-01-13 02:48:51 +00:00
|
|
|
),
|
2020-12-07 21:16:53 +00:00
|
|
|
},
|
2021-01-13 02:48:51 +00:00
|
|
|
version: v1alpha1.Version,
|
|
|
|
errChecker: IsNil,
|
|
|
|
uVCSChecker: NotNil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ops: &validateOperations{
|
|
|
|
dynCli: nil,
|
|
|
|
},
|
|
|
|
version: v1alpha1.Version,
|
|
|
|
errChecker: NotNil,
|
|
|
|
uVCSChecker: IsNil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ops: &validateOperations{
|
|
|
|
dynCli: fakedynamic.NewSimpleDynamicClient(
|
|
|
|
runtime.NewScheme(),
|
|
|
|
&unstructured.Unstructured{
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
"apiVersion": fmt.Sprintf("%s/%s", v1beta1.GroupName, v1beta1.Version),
|
|
|
|
"kind": "VolumeSnapshotClass",
|
|
|
|
"metadata": map[string]interface{}{
|
|
|
|
"name": "vsc",
|
|
|
|
},
|
|
|
|
"driver": "somesnapshotter",
|
|
|
|
"deletionPolicy": "Delete",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
},
|
|
|
|
version: v1beta1.Version,
|
|
|
|
errChecker: IsNil,
|
|
|
|
uVCSChecker: NotNil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ops: &validateOperations{
|
|
|
|
dynCli: fakedynamic.NewSimpleDynamicClient(
|
|
|
|
runtime.NewScheme(),
|
|
|
|
&unstructured.Unstructured{
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
"apiVersion": fmt.Sprintf("%s/%s", kansnapshot.GroupName, kansnapshot.Version),
|
|
|
|
"kind": "VolumeSnapshotClass",
|
|
|
|
"metadata": map[string]interface{}{
|
|
|
|
"name": "vsc",
|
|
|
|
},
|
|
|
|
"driver": "somesnapshotter",
|
|
|
|
"deletionPolicy": "Delete",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
},
|
|
|
|
groupVersion: common.SnapshotStableVersion,
|
|
|
|
version: kansnapshot.Version,
|
|
|
|
errChecker: IsNil,
|
|
|
|
uVCSChecker: NotNil,
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
uVSC, err := tc.ops.ValidateVolumeSnapshotClass(ctx, "vsc", &metav1.GroupVersionForDiscovery{GroupVersion: tc.groupVersion, Version: tc.version})
|
|
|
|
c.Check(err, tc.errChecker)
|
|
|
|
c.Check(uVSC, tc.uVCSChecker)
|
2020-12-07 21:16:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CSITestSuite) TestCreatePVC(c *C) {
|
|
|
|
ctx := context.Background()
|
|
|
|
resourceQuantity := resource.MustParse("1Gi")
|
|
|
|
for _, tc := range []struct {
|
|
|
|
cli kubernetes.Interface
|
|
|
|
args *types.CreatePVCArgs
|
|
|
|
failCreates bool
|
|
|
|
errChecker Checker
|
|
|
|
pvcChecker Checker
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
cli: fake.NewSimpleClientset(),
|
|
|
|
args: &types.CreatePVCArgs{
|
|
|
|
GenerateName: "genName",
|
|
|
|
StorageClass: "sc",
|
|
|
|
Namespace: "ns",
|
|
|
|
DataSource: &v1.TypedLocalObjectReference{
|
|
|
|
Name: "ds",
|
|
|
|
},
|
|
|
|
RestoreSize: &resourceQuantity,
|
|
|
|
},
|
|
|
|
errChecker: IsNil,
|
|
|
|
pvcChecker: NotNil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cli: fake.NewSimpleClientset(),
|
|
|
|
args: &types.CreatePVCArgs{
|
|
|
|
GenerateName: "genName",
|
|
|
|
StorageClass: "sc",
|
|
|
|
Namespace: "ns",
|
|
|
|
DataSource: &v1.TypedLocalObjectReference{
|
|
|
|
Name: "ds",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
errChecker: IsNil,
|
|
|
|
pvcChecker: NotNil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cli: fake.NewSimpleClientset(),
|
|
|
|
args: &types.CreatePVCArgs{
|
|
|
|
GenerateName: "genName",
|
|
|
|
StorageClass: "sc",
|
|
|
|
Namespace: "ns",
|
|
|
|
},
|
|
|
|
errChecker: IsNil,
|
|
|
|
pvcChecker: NotNil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cli: fake.NewSimpleClientset(),
|
|
|
|
args: &types.CreatePVCArgs{
|
|
|
|
GenerateName: "genName",
|
|
|
|
StorageClass: "sc",
|
|
|
|
Namespace: "ns",
|
|
|
|
},
|
|
|
|
failCreates: true,
|
|
|
|
errChecker: NotNil,
|
|
|
|
pvcChecker: NotNil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cli: fake.NewSimpleClientset(),
|
|
|
|
args: &types.CreatePVCArgs{
|
|
|
|
GenerateName: "",
|
|
|
|
StorageClass: "sc",
|
|
|
|
Namespace: "ns",
|
|
|
|
},
|
|
|
|
errChecker: NotNil,
|
|
|
|
pvcChecker: IsNil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cli: fake.NewSimpleClientset(),
|
|
|
|
args: &types.CreatePVCArgs{
|
|
|
|
GenerateName: "something",
|
|
|
|
StorageClass: "",
|
|
|
|
Namespace: "ns",
|
|
|
|
},
|
|
|
|
errChecker: NotNil,
|
|
|
|
pvcChecker: IsNil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cli: fake.NewSimpleClientset(),
|
|
|
|
args: &types.CreatePVCArgs{
|
|
|
|
GenerateName: "Something",
|
|
|
|
StorageClass: "sc",
|
|
|
|
Namespace: "",
|
|
|
|
},
|
|
|
|
errChecker: NotNil,
|
|
|
|
pvcChecker: IsNil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cli: nil,
|
|
|
|
args: &types.CreatePVCArgs{},
|
|
|
|
errChecker: NotNil,
|
|
|
|
pvcChecker: IsNil,
|
|
|
|
},
|
|
|
|
} {
|
2023-07-24 21:23:35 +00:00
|
|
|
appCreator := NewApplicationCreator(tc.cli, 0)
|
|
|
|
creator := appCreator.(*applicationCreate)
|
2020-12-07 21:16:53 +00:00
|
|
|
if tc.failCreates {
|
|
|
|
creator.kubeCli.(*fake.Clientset).Fake.PrependReactor("create", "persistentvolumeclaims", func(action k8stesting.Action) (handled bool, ret runtime.Object, err error) {
|
|
|
|
return true, nil, errors.New("Error creating object")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
pvc, err := creator.CreatePVC(ctx, tc.args)
|
|
|
|
c.Check(pvc, tc.pvcChecker)
|
|
|
|
c.Check(err, tc.errChecker)
|
|
|
|
if pvc != nil && err == nil {
|
|
|
|
_, ok := pvc.Labels[createdByLabel]
|
|
|
|
c.Assert(ok, Equals, true)
|
|
|
|
c.Assert(pvc.GenerateName, Equals, tc.args.GenerateName)
|
|
|
|
c.Assert(pvc.Namespace, Equals, tc.args.Namespace)
|
|
|
|
c.Assert(pvc.Spec.AccessModes, DeepEquals, []v1.PersistentVolumeAccessMode{v1.ReadWriteOnce})
|
|
|
|
c.Assert(*pvc.Spec.StorageClassName, Equals, tc.args.StorageClass)
|
|
|
|
c.Assert(pvc.Spec.DataSource, DeepEquals, tc.args.DataSource)
|
|
|
|
if tc.args.RestoreSize != nil {
|
2024-02-02 07:09:32 +00:00
|
|
|
c.Assert(pvc.Spec.Resources, DeepEquals, v1.VolumeResourceRequirements{
|
2020-12-07 21:16:53 +00:00
|
|
|
Requests: v1.ResourceList{
|
|
|
|
v1.ResourceStorage: *tc.args.RestoreSize,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
} else {
|
2024-02-02 07:09:32 +00:00
|
|
|
c.Assert(pvc.Spec.Resources, DeepEquals, v1.VolumeResourceRequirements{
|
2020-12-07 21:16:53 +00:00
|
|
|
Requests: v1.ResourceList{
|
|
|
|
v1.ResourceStorage: resource.MustParse("1Gi"),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CSITestSuite) TestCreatePod(c *C) {
|
|
|
|
ctx := context.Background()
|
|
|
|
for _, tc := range []struct {
|
2022-06-30 00:39:09 +00:00
|
|
|
description string
|
2020-12-07 21:16:53 +00:00
|
|
|
cli kubernetes.Interface
|
|
|
|
args *types.CreatePodArgs
|
|
|
|
failCreates bool
|
|
|
|
errChecker Checker
|
|
|
|
podChecker Checker
|
|
|
|
}{
|
|
|
|
{
|
2022-06-30 00:39:09 +00:00
|
|
|
description: "pod with container image and runAsUser 1000 created",
|
2022-10-25 19:02:47 +00:00
|
|
|
cli: fake.NewSimpleClientset(),
|
2020-12-07 21:16:53 +00:00
|
|
|
args: &types.CreatePodArgs{
|
|
|
|
GenerateName: "name",
|
|
|
|
Namespace: "ns",
|
2021-10-08 19:53:57 +00:00
|
|
|
Command: []string{"somecommand"},
|
2020-12-07 21:16:53 +00:00
|
|
|
RunAsUser: 1000,
|
|
|
|
ContainerImage: "containerimage",
|
2024-08-07 20:06:53 +00:00
|
|
|
PVCMap: map[string]types.VolumePath{
|
|
|
|
"pvcname": {
|
|
|
|
MountPath: "/mnt/fs",
|
|
|
|
},
|
|
|
|
},
|
2020-12-07 21:16:53 +00:00
|
|
|
},
|
|
|
|
errChecker: IsNil,
|
|
|
|
podChecker: NotNil,
|
|
|
|
},
|
|
|
|
{
|
2022-06-30 00:39:09 +00:00
|
|
|
description: "Pod creation error on kubeCli",
|
2022-10-25 19:02:47 +00:00
|
|
|
cli: fake.NewSimpleClientset(),
|
2020-12-07 21:16:53 +00:00
|
|
|
args: &types.CreatePodArgs{
|
|
|
|
GenerateName: "name",
|
|
|
|
Namespace: "ns",
|
2021-10-08 19:53:57 +00:00
|
|
|
Command: []string{"somecommand"},
|
2024-08-07 20:06:53 +00:00
|
|
|
PVCMap: map[string]types.VolumePath{
|
|
|
|
"pvcname": {
|
|
|
|
MountPath: "/mnt/fs",
|
|
|
|
},
|
|
|
|
},
|
2020-12-07 21:16:53 +00:00
|
|
|
},
|
|
|
|
failCreates: true,
|
|
|
|
errChecker: NotNil,
|
|
|
|
podChecker: NotNil,
|
|
|
|
},
|
|
|
|
{
|
2023-07-24 21:23:35 +00:00
|
|
|
description: "Neither Name nor GenerateName set",
|
2022-10-25 19:02:47 +00:00
|
|
|
cli: fake.NewSimpleClientset(),
|
2020-12-07 21:16:53 +00:00
|
|
|
args: &types.CreatePodArgs{
|
|
|
|
GenerateName: "",
|
|
|
|
Namespace: "ns",
|
2021-10-08 19:53:57 +00:00
|
|
|
Command: []string{"somecommand"},
|
2024-08-07 20:06:53 +00:00
|
|
|
PVCMap: map[string]types.VolumePath{
|
|
|
|
"pvcname": {
|
|
|
|
MountPath: "/mnt/fs",
|
|
|
|
},
|
|
|
|
},
|
2023-07-24 21:23:35 +00:00
|
|
|
},
|
|
|
|
errChecker: NotNil,
|
|
|
|
podChecker: IsNil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Both Name and GenerateName set",
|
|
|
|
cli: fake.NewSimpleClientset(),
|
|
|
|
args: &types.CreatePodArgs{
|
|
|
|
GenerateName: "name",
|
|
|
|
Name: "name",
|
|
|
|
Namespace: "ns",
|
|
|
|
Command: []string{"somecommand"},
|
2024-08-07 20:06:53 +00:00
|
|
|
PVCMap: map[string]types.VolumePath{
|
|
|
|
"pvcname": {
|
|
|
|
MountPath: "/mnt/fs",
|
|
|
|
},
|
|
|
|
},
|
2023-07-24 21:23:35 +00:00
|
|
|
},
|
|
|
|
errChecker: NotNil,
|
|
|
|
podChecker: IsNil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Neither MountPath nor DevicePath set error",
|
|
|
|
cli: fake.NewSimpleClientset(),
|
|
|
|
args: &types.CreatePodArgs{
|
|
|
|
GenerateName: "name",
|
|
|
|
Namespace: "ns",
|
|
|
|
Command: []string{"somecommand"},
|
2024-08-07 20:06:53 +00:00
|
|
|
PVCMap: map[string]types.VolumePath{"pvcname": {}},
|
2023-07-24 21:23:35 +00:00
|
|
|
},
|
|
|
|
errChecker: NotNil,
|
|
|
|
podChecker: IsNil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Both MountPath and DevicePath set error",
|
|
|
|
cli: fake.NewSimpleClientset(),
|
|
|
|
args: &types.CreatePodArgs{
|
|
|
|
GenerateName: "name",
|
|
|
|
Namespace: "ns",
|
|
|
|
Command: []string{"somecommand"},
|
2024-08-07 20:06:53 +00:00
|
|
|
PVCMap: map[string]types.VolumePath{
|
|
|
|
"pvcname": {
|
|
|
|
MountPath: "/mnt/fs",
|
|
|
|
DevicePath: "/mnt/dev",
|
|
|
|
},
|
|
|
|
},
|
2020-12-07 21:16:53 +00:00
|
|
|
},
|
|
|
|
errChecker: NotNil,
|
|
|
|
podChecker: IsNil,
|
|
|
|
},
|
|
|
|
{
|
2022-06-30 00:39:09 +00:00
|
|
|
description: "PVC name not set error",
|
2022-10-25 19:02:47 +00:00
|
|
|
cli: fake.NewSimpleClientset(),
|
2020-12-07 21:16:53 +00:00
|
|
|
args: &types.CreatePodArgs{
|
|
|
|
GenerateName: "name",
|
|
|
|
Namespace: "ns",
|
2021-10-08 19:53:57 +00:00
|
|
|
Command: []string{"somecommand"},
|
2024-08-07 20:06:53 +00:00
|
|
|
PVCMap: map[string]types.VolumePath{"": {MountPath: "/mnt/fs"}},
|
2020-12-07 21:16:53 +00:00
|
|
|
},
|
|
|
|
errChecker: NotNil,
|
|
|
|
podChecker: IsNil,
|
|
|
|
},
|
|
|
|
{
|
2022-06-30 00:39:09 +00:00
|
|
|
description: "default namespace pod is created",
|
2022-10-25 19:02:47 +00:00
|
|
|
cli: fake.NewSimpleClientset(),
|
2020-12-07 21:16:53 +00:00
|
|
|
args: &types.CreatePodArgs{
|
|
|
|
GenerateName: "name",
|
|
|
|
Namespace: "",
|
2021-10-08 19:53:57 +00:00
|
|
|
Command: []string{"somecommand"},
|
2024-08-07 20:06:53 +00:00
|
|
|
PVCMap: map[string]types.VolumePath{
|
|
|
|
"pvcname": {
|
|
|
|
MountPath: "/mnt/fs",
|
|
|
|
},
|
|
|
|
},
|
2020-12-07 21:16:53 +00:00
|
|
|
},
|
|
|
|
errChecker: NotNil,
|
|
|
|
podChecker: IsNil,
|
|
|
|
},
|
|
|
|
{
|
2023-07-24 21:23:35 +00:00
|
|
|
description: "ns namespace pod is created (GenerateName/MountPath)",
|
2022-10-25 19:02:47 +00:00
|
|
|
cli: fake.NewSimpleClientset(),
|
2020-12-07 21:16:53 +00:00
|
|
|
args: &types.CreatePodArgs{
|
|
|
|
GenerateName: "name",
|
|
|
|
Namespace: "ns",
|
2021-10-08 19:53:57 +00:00
|
|
|
Command: []string{"somecommand"},
|
2024-08-07 20:06:53 +00:00
|
|
|
PVCMap: map[string]types.VolumePath{
|
|
|
|
"pvcname": {
|
|
|
|
MountPath: "/mnt/fs",
|
|
|
|
},
|
|
|
|
},
|
2023-07-24 21:23:35 +00:00
|
|
|
},
|
|
|
|
errChecker: IsNil,
|
|
|
|
podChecker: NotNil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "ns namespace pod is created (Name/DevicePath)",
|
|
|
|
cli: fake.NewSimpleClientset(),
|
|
|
|
args: &types.CreatePodArgs{
|
2024-08-07 20:06:53 +00:00
|
|
|
Name: "name",
|
|
|
|
Namespace: "ns",
|
|
|
|
Command: []string{"somecommand"},
|
|
|
|
PVCMap: map[string]types.VolumePath{
|
|
|
|
"pvcname": {
|
|
|
|
DevicePath: "/mnt/dev",
|
|
|
|
},
|
|
|
|
},
|
2020-12-07 21:16:53 +00:00
|
|
|
},
|
2021-10-08 19:53:57 +00:00
|
|
|
errChecker: IsNil,
|
|
|
|
podChecker: NotNil,
|
2020-12-07 21:16:53 +00:00
|
|
|
},
|
|
|
|
{
|
2022-06-30 00:39:09 +00:00
|
|
|
description: "kubeCli not initialized",
|
2022-10-25 19:02:47 +00:00
|
|
|
cli: nil,
|
|
|
|
args: &types.CreatePodArgs{},
|
|
|
|
errChecker: NotNil,
|
|
|
|
podChecker: IsNil,
|
2020-12-07 21:16:53 +00:00
|
|
|
},
|
|
|
|
} {
|
2022-06-30 00:39:09 +00:00
|
|
|
fmt.Println("test:", tc.description)
|
2020-12-07 21:16:53 +00:00
|
|
|
creator := &applicationCreate{kubeCli: tc.cli}
|
|
|
|
if tc.failCreates {
|
|
|
|
creator.kubeCli.(*fake.Clientset).Fake.PrependReactor("create", "pods", func(action k8stesting.Action) (handled bool, ret runtime.Object, err error) {
|
|
|
|
return true, nil, errors.New("Error creating object")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
pod, err := creator.CreatePod(ctx, tc.args)
|
|
|
|
c.Check(pod, tc.podChecker)
|
|
|
|
c.Check(err, tc.errChecker)
|
|
|
|
if pod != nil && err == nil {
|
|
|
|
_, ok := pod.Labels[createdByLabel]
|
|
|
|
c.Assert(ok, Equals, true)
|
2023-07-24 21:23:35 +00:00
|
|
|
if tc.args.GenerateName != "" {
|
|
|
|
c.Assert(pod.GenerateName, Equals, tc.args.GenerateName)
|
|
|
|
c.Assert(pod.Spec.Containers[0].Name, Equals, tc.args.GenerateName)
|
|
|
|
} else {
|
|
|
|
c.Assert(pod.Name, Equals, tc.args.Name)
|
|
|
|
c.Assert(pod.Spec.Containers[0].Name, Equals, tc.args.Name)
|
|
|
|
}
|
2020-12-07 21:16:53 +00:00
|
|
|
c.Assert(pod.Namespace, Equals, tc.args.Namespace)
|
|
|
|
c.Assert(len(pod.Spec.Containers), Equals, 1)
|
2021-10-08 19:53:57 +00:00
|
|
|
c.Assert(pod.Spec.Containers[0].Command, DeepEquals, tc.args.Command)
|
|
|
|
c.Assert(pod.Spec.Containers[0].Args, DeepEquals, tc.args.ContainerArgs)
|
2024-08-07 20:06:53 +00:00
|
|
|
index := 0
|
2024-08-23 21:33:44 +00:00
|
|
|
pvcCount := 1
|
2024-08-07 20:06:53 +00:00
|
|
|
for pvcName, path := range tc.args.PVCMap {
|
|
|
|
if len(path.MountPath) != 0 {
|
|
|
|
c.Assert(pod.Spec.Containers[0].VolumeMounts[index], DeepEquals, v1.VolumeMount{
|
2024-08-23 21:33:44 +00:00
|
|
|
Name: fmt.Sprintf("persistent-storage-%d", pvcCount),
|
2024-08-07 20:06:53 +00:00
|
|
|
MountPath: path.MountPath,
|
|
|
|
})
|
|
|
|
c.Assert(pod.Spec.Containers[0].VolumeDevices, IsNil)
|
|
|
|
} else {
|
|
|
|
c.Assert(pod.Spec.Containers[0].VolumeDevices[index], DeepEquals, v1.VolumeDevice{
|
2024-08-23 21:33:44 +00:00
|
|
|
Name: fmt.Sprintf("persistent-storage-%d", pvcCount),
|
2024-08-07 20:06:53 +00:00
|
|
|
DevicePath: path.DevicePath,
|
|
|
|
})
|
|
|
|
c.Assert(pod.Spec.Containers[0].VolumeMounts, IsNil)
|
|
|
|
}
|
|
|
|
c.Assert(pod.Spec.Volumes[index], DeepEquals, v1.Volume{
|
2024-08-23 21:33:44 +00:00
|
|
|
Name: fmt.Sprintf("persistent-storage-%d", pvcCount),
|
2024-08-07 20:06:53 +00:00
|
|
|
VolumeSource: v1.VolumeSource{
|
|
|
|
PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
|
|
|
|
ClaimName: pvcName,
|
|
|
|
},
|
2020-12-07 21:16:53 +00:00
|
|
|
},
|
2024-08-07 20:06:53 +00:00
|
|
|
})
|
|
|
|
index++
|
2024-08-23 21:33:44 +00:00
|
|
|
pvcCount++
|
2024-08-07 20:06:53 +00:00
|
|
|
}
|
2020-12-07 21:16:53 +00:00
|
|
|
if tc.args.ContainerImage == "" {
|
2020-12-29 19:46:29 +00:00
|
|
|
c.Assert(pod.Spec.Containers[0].Image, Equals, common.DefaultPodImage)
|
2020-12-07 21:16:53 +00:00
|
|
|
} else {
|
|
|
|
c.Assert(pod.Spec.Containers[0].Image, Equals, tc.args.ContainerImage)
|
|
|
|
}
|
|
|
|
if tc.args.RunAsUser > 0 {
|
|
|
|
c.Assert(pod.Spec.SecurityContext, DeepEquals, &v1.PodSecurityContext{
|
|
|
|
RunAsUser: &tc.args.RunAsUser,
|
|
|
|
FSGroup: &tc.args.RunAsUser,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
c.Check(pod.Spec.SecurityContext, IsNil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CSITestSuite) TestCreateSnapshot(c *C) {
|
|
|
|
ctx := context.Background()
|
|
|
|
for _, tc := range []struct {
|
|
|
|
snapshotter kansnapshot.Snapshotter
|
|
|
|
args *types.CreateSnapshotArgs
|
|
|
|
snapChecker Checker
|
|
|
|
errChecker Checker
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
snapshotter: &fakeSnapshotter{
|
2021-01-13 02:48:51 +00:00
|
|
|
getSnap: &snapv1.VolumeSnapshot{
|
2020-12-07 21:16:53 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: "createdName",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: &types.CreateSnapshotArgs{
|
|
|
|
Namespace: "ns",
|
|
|
|
PVCName: "pvc",
|
|
|
|
VolumeSnapshotClass: "vsc",
|
|
|
|
SnapshotName: "snap1",
|
|
|
|
},
|
|
|
|
snapChecker: NotNil,
|
|
|
|
errChecker: IsNil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
snapshotter: &fakeSnapshotter{
|
|
|
|
getErr: fmt.Errorf("get Error"),
|
|
|
|
},
|
|
|
|
args: &types.CreateSnapshotArgs{
|
|
|
|
Namespace: "ns",
|
|
|
|
PVCName: "pvc",
|
|
|
|
VolumeSnapshotClass: "vsc",
|
|
|
|
SnapshotName: "snap1",
|
|
|
|
},
|
|
|
|
snapChecker: IsNil,
|
|
|
|
errChecker: NotNil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
snapshotter: &fakeSnapshotter{
|
|
|
|
createErr: fmt.Errorf("create Error"),
|
|
|
|
},
|
|
|
|
args: &types.CreateSnapshotArgs{
|
|
|
|
Namespace: "ns",
|
|
|
|
PVCName: "pvc",
|
|
|
|
VolumeSnapshotClass: "vsc",
|
|
|
|
SnapshotName: "snap1",
|
|
|
|
},
|
|
|
|
snapChecker: IsNil,
|
|
|
|
errChecker: NotNil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
snapshotter: &fakeSnapshotter{
|
|
|
|
createErr: fmt.Errorf("create Error"),
|
|
|
|
},
|
|
|
|
args: &types.CreateSnapshotArgs{
|
|
|
|
Namespace: "",
|
|
|
|
PVCName: "pvc",
|
|
|
|
VolumeSnapshotClass: "vsc",
|
|
|
|
SnapshotName: "snap1",
|
|
|
|
},
|
|
|
|
snapChecker: IsNil,
|
|
|
|
errChecker: NotNil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
snapshotter: &fakeSnapshotter{
|
|
|
|
createErr: fmt.Errorf("create Error"),
|
|
|
|
},
|
|
|
|
args: &types.CreateSnapshotArgs{
|
|
|
|
Namespace: "ns",
|
|
|
|
PVCName: "",
|
|
|
|
VolumeSnapshotClass: "vsc",
|
|
|
|
SnapshotName: "snap1",
|
|
|
|
},
|
|
|
|
snapChecker: IsNil,
|
|
|
|
errChecker: NotNil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
snapshotter: &fakeSnapshotter{
|
|
|
|
createErr: fmt.Errorf("create Error"),
|
|
|
|
},
|
|
|
|
args: &types.CreateSnapshotArgs{
|
|
|
|
Namespace: "ns",
|
|
|
|
PVCName: "pvc",
|
|
|
|
VolumeSnapshotClass: "",
|
|
|
|
SnapshotName: "snap1",
|
|
|
|
},
|
|
|
|
snapChecker: IsNil,
|
|
|
|
errChecker: NotNil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
snapshotter: &fakeSnapshotter{
|
|
|
|
createErr: fmt.Errorf("create Error"),
|
|
|
|
},
|
|
|
|
args: &types.CreateSnapshotArgs{
|
|
|
|
Namespace: "ns",
|
|
|
|
PVCName: "pvc",
|
|
|
|
VolumeSnapshotClass: "vsc",
|
|
|
|
SnapshotName: "",
|
|
|
|
},
|
|
|
|
snapChecker: IsNil,
|
|
|
|
errChecker: NotNil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
snapshotter: &fakeSnapshotter{},
|
|
|
|
snapChecker: IsNil,
|
|
|
|
errChecker: NotNil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
snapChecker: IsNil,
|
|
|
|
errChecker: NotNil,
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
snapCreator := &snapshotCreate{}
|
|
|
|
snapshot, err := snapCreator.CreateSnapshot(ctx, tc.snapshotter, tc.args)
|
|
|
|
c.Check(snapshot, tc.snapChecker)
|
|
|
|
c.Check(err, tc.errChecker)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CSITestSuite) TestCreateFromSourceCheck(c *C) {
|
|
|
|
ctx := context.Background()
|
2020-12-16 22:23:06 +00:00
|
|
|
gv := &metav1.GroupVersionForDiscovery{Version: v1alpha1.Version}
|
2020-12-07 21:16:53 +00:00
|
|
|
for _, tc := range []struct {
|
2020-12-16 22:23:06 +00:00
|
|
|
dyncli dynamic.Interface
|
|
|
|
snapshotter kansnapshot.Snapshotter
|
|
|
|
args *types.CreateFromSourceCheckArgs
|
|
|
|
groupVersion *metav1.GroupVersionForDiscovery
|
|
|
|
errChecker Checker
|
2020-12-07 21:16:53 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
dyncli: fakedynamic.NewSimpleDynamicClient(runtime.NewScheme()),
|
|
|
|
snapshotter: &fakeSnapshotter{
|
|
|
|
gsSrc: &kansnapshot.Source{
|
|
|
|
Handle: "handle",
|
|
|
|
Driver: "driver",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: &types.CreateFromSourceCheckArgs{
|
|
|
|
VolumeSnapshotClass: "vsc",
|
|
|
|
SnapshotName: "snapshot",
|
|
|
|
Namespace: "ns",
|
|
|
|
},
|
2020-12-16 22:23:06 +00:00
|
|
|
groupVersion: gv,
|
|
|
|
errChecker: IsNil,
|
2020-12-07 21:16:53 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
dyncli: fakedynamic.NewSimpleDynamicClient(runtime.NewScheme()),
|
|
|
|
snapshotter: &fakeSnapshotter{
|
|
|
|
gsSrc: &kansnapshot.Source{
|
|
|
|
Handle: "handle",
|
|
|
|
Driver: "driver",
|
|
|
|
},
|
|
|
|
cfsErr: fmt.Errorf("cfs error"),
|
|
|
|
},
|
|
|
|
args: &types.CreateFromSourceCheckArgs{
|
|
|
|
VolumeSnapshotClass: "vsc",
|
|
|
|
SnapshotName: "snapshot",
|
|
|
|
Namespace: "ns",
|
|
|
|
},
|
2020-12-16 22:23:06 +00:00
|
|
|
groupVersion: gv,
|
|
|
|
errChecker: NotNil,
|
2020-12-07 21:16:53 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
dyncli: fakedynamic.NewSimpleDynamicClient(runtime.NewScheme()),
|
|
|
|
snapshotter: &fakeSnapshotter{
|
|
|
|
gsErr: fmt.Errorf("gs error"),
|
|
|
|
},
|
|
|
|
args: &types.CreateFromSourceCheckArgs{
|
|
|
|
VolumeSnapshotClass: "vsc",
|
|
|
|
SnapshotName: "snapshot",
|
|
|
|
Namespace: "ns",
|
|
|
|
},
|
2020-12-16 22:23:06 +00:00
|
|
|
groupVersion: gv,
|
|
|
|
errChecker: NotNil,
|
2020-12-07 21:16:53 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
dyncli: fakedynamic.NewSimpleDynamicClient(runtime.NewScheme()),
|
|
|
|
snapshotter: &fakeSnapshotter{
|
|
|
|
cvsErr: fmt.Errorf("cvs error"),
|
|
|
|
},
|
|
|
|
args: &types.CreateFromSourceCheckArgs{
|
|
|
|
VolumeSnapshotClass: "vsc",
|
|
|
|
SnapshotName: "snapshot",
|
|
|
|
Namespace: "ns",
|
|
|
|
},
|
2020-12-16 22:23:06 +00:00
|
|
|
groupVersion: gv,
|
|
|
|
errChecker: NotNil,
|
2020-12-07 21:16:53 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
dyncli: fakedynamic.NewSimpleDynamicClient(runtime.NewScheme()),
|
|
|
|
snapshotter: &fakeSnapshotter{},
|
|
|
|
args: &types.CreateFromSourceCheckArgs{
|
|
|
|
VolumeSnapshotClass: "",
|
|
|
|
SnapshotName: "snapshot",
|
|
|
|
Namespace: "ns",
|
|
|
|
},
|
2020-12-16 22:23:06 +00:00
|
|
|
groupVersion: gv,
|
|
|
|
errChecker: NotNil,
|
2020-12-07 21:16:53 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
dyncli: fakedynamic.NewSimpleDynamicClient(runtime.NewScheme()),
|
|
|
|
snapshotter: &fakeSnapshotter{},
|
|
|
|
args: &types.CreateFromSourceCheckArgs{
|
|
|
|
VolumeSnapshotClass: "vsc",
|
|
|
|
SnapshotName: "",
|
|
|
|
Namespace: "ns",
|
|
|
|
},
|
2020-12-16 22:23:06 +00:00
|
|
|
groupVersion: gv,
|
|
|
|
errChecker: NotNil,
|
2020-12-07 21:16:53 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
dyncli: fakedynamic.NewSimpleDynamicClient(runtime.NewScheme()),
|
|
|
|
snapshotter: &fakeSnapshotter{},
|
|
|
|
args: &types.CreateFromSourceCheckArgs{
|
|
|
|
VolumeSnapshotClass: "vsc",
|
|
|
|
SnapshotName: "snapshot",
|
|
|
|
Namespace: "",
|
|
|
|
},
|
2020-12-16 22:23:06 +00:00
|
|
|
groupVersion: gv,
|
|
|
|
errChecker: NotNil,
|
2020-12-07 21:16:53 +00:00
|
|
|
},
|
|
|
|
{
|
2020-12-16 22:23:06 +00:00
|
|
|
dyncli: fakedynamic.NewSimpleDynamicClient(runtime.NewScheme()),
|
|
|
|
snapshotter: &fakeSnapshotter{},
|
|
|
|
groupVersion: gv,
|
|
|
|
errChecker: NotNil,
|
2020-12-07 21:16:53 +00:00
|
|
|
},
|
|
|
|
{
|
2020-12-16 22:23:06 +00:00
|
|
|
dyncli: fakedynamic.NewSimpleDynamicClient(runtime.NewScheme()),
|
|
|
|
groupVersion: gv,
|
|
|
|
errChecker: NotNil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
dyncli: fakedynamic.NewSimpleDynamicClient(runtime.NewScheme()),
|
|
|
|
groupVersion: nil,
|
|
|
|
errChecker: NotNil,
|
2020-12-07 21:16:53 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
dyncli: nil,
|
|
|
|
errChecker: NotNil,
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
snapCreator := &snapshotCreate{
|
|
|
|
dynCli: tc.dyncli,
|
|
|
|
}
|
2020-12-16 22:23:06 +00:00
|
|
|
err := snapCreator.CreateFromSourceCheck(ctx, tc.snapshotter, tc.args, tc.groupVersion)
|
2020-12-07 21:16:53 +00:00
|
|
|
c.Check(err, tc.errChecker)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type fakeSnapshotter struct {
|
|
|
|
name string
|
|
|
|
|
|
|
|
createErr error
|
|
|
|
|
2021-01-13 02:48:51 +00:00
|
|
|
getSnap *snapv1.VolumeSnapshot
|
2020-12-07 21:16:53 +00:00
|
|
|
getErr error
|
|
|
|
|
|
|
|
cvsErr error
|
|
|
|
|
|
|
|
gsSrc *kansnapshot.Source
|
|
|
|
gsErr error
|
|
|
|
|
|
|
|
cfsErr error
|
|
|
|
}
|
|
|
|
|
2024-02-02 07:09:32 +00:00
|
|
|
func (f *fakeSnapshotter) GroupVersion(ctx context.Context) schema.GroupVersion {
|
|
|
|
return schema.GroupVersion{
|
|
|
|
Group: common.SnapGroupName,
|
|
|
|
Version: "v1",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-06 16:57:30 +00:00
|
|
|
func (f *fakeSnapshotter) GetVolumeSnapshotClass(ctx context.Context, annotationKey, annotationValue, storageClassName string) (string, error) {
|
2020-12-07 21:16:53 +00:00
|
|
|
return "", nil
|
|
|
|
}
|
2021-08-06 16:57:30 +00:00
|
|
|
func (f *fakeSnapshotter) CloneVolumeSnapshotClass(ctx context.Context, sourceClassName, targetClassName, newDeletionPolicy string, excludeAnnotations []string) error {
|
2020-12-07 21:16:53 +00:00
|
|
|
return f.cvsErr
|
|
|
|
}
|
2024-08-05 08:39:36 +00:00
|
|
|
func (f *fakeSnapshotter) Create(ctx context.Context, pvcName string, snapshotClass *string, waitForReady bool, snapshotMeta kansnapshot.ObjectMeta) error {
|
2020-12-07 21:16:53 +00:00
|
|
|
return f.createErr
|
|
|
|
}
|
2021-01-13 02:48:51 +00:00
|
|
|
func (f *fakeSnapshotter) Get(ctx context.Context, name, namespace string) (*snapv1.VolumeSnapshot, error) {
|
2020-12-07 21:16:53 +00:00
|
|
|
return f.getSnap, f.getErr
|
|
|
|
}
|
2021-01-13 02:48:51 +00:00
|
|
|
func (f *fakeSnapshotter) Delete(ctx context.Context, name, namespace string) (*snapv1.VolumeSnapshot, error) {
|
2020-12-07 21:16:53 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
func (f *fakeSnapshotter) DeleteContent(ctx context.Context, name string) error { return nil }
|
2024-08-05 08:39:36 +00:00
|
|
|
func (f *fakeSnapshotter) Clone(ctx context.Context, name, namespace string, waitForReady bool, snapshotMeta, contentMeta kansnapshot.ObjectMeta) error {
|
2020-12-07 21:16:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (f *fakeSnapshotter) GetSource(ctx context.Context, snapshotName, namespace string) (*kansnapshot.Source, error) {
|
|
|
|
return f.gsSrc, f.gsErr
|
|
|
|
}
|
2024-08-05 08:39:36 +00:00
|
|
|
func (f *fakeSnapshotter) CreateFromSource(ctx context.Context, source *kansnapshot.Source, waitForReady bool, snapshotMeta, contentMeta kansnapshot.ObjectMeta) error {
|
2020-12-07 21:16:53 +00:00
|
|
|
return f.cfsErr
|
|
|
|
}
|
2024-08-05 08:39:36 +00:00
|
|
|
func (f *fakeSnapshotter) CreateContentFromSource(ctx context.Context, source *kansnapshot.Source, snapshotName, snapshotNs, deletionPolicy string, contentMeta kansnapshot.ObjectMeta) error {
|
2020-12-07 21:16:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (f *fakeSnapshotter) WaitOnReadyToUse(ctx context.Context, snapshotName, namespace string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-24 00:36:54 +00:00
|
|
|
func (f *fakeSnapshotter) List(ctx context.Context, namespace string, labels map[string]string) (*snapv1.VolumeSnapshotList, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-12-07 21:16:53 +00:00
|
|
|
func (s *CSITestSuite) TestDeletePVC(c *C) {
|
|
|
|
ctx := context.Background()
|
|
|
|
for _, tc := range []struct {
|
|
|
|
cli kubernetes.Interface
|
|
|
|
pvcName string
|
|
|
|
namespace string
|
|
|
|
errChecker Checker
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
cli: fake.NewSimpleClientset(),
|
|
|
|
pvcName: "pvc",
|
|
|
|
namespace: "ns",
|
|
|
|
errChecker: NotNil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cli: fake.NewSimpleClientset(&v1.PersistentVolumeClaim{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: "pvc",
|
|
|
|
Namespace: "notns",
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
pvcName: "pvc",
|
|
|
|
namespace: "ns",
|
|
|
|
errChecker: NotNil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cli: fake.NewSimpleClientset(&v1.PersistentVolumeClaim{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: "pvc",
|
|
|
|
Namespace: "ns",
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
pvcName: "pvc",
|
|
|
|
namespace: "ns",
|
|
|
|
errChecker: IsNil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cli: nil,
|
|
|
|
pvcName: "pvc",
|
|
|
|
namespace: "ns",
|
|
|
|
errChecker: NotNil,
|
|
|
|
},
|
|
|
|
} {
|
2023-07-24 21:23:35 +00:00
|
|
|
cleaner := NewCleaner(tc.cli, nil)
|
2020-12-07 21:16:53 +00:00
|
|
|
err := cleaner.DeletePVC(ctx, tc.pvcName, tc.namespace)
|
|
|
|
c.Check(err, tc.errChecker)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CSITestSuite) TestDeletePod(c *C) {
|
|
|
|
ctx := context.Background()
|
|
|
|
for _, tc := range []struct {
|
|
|
|
cli kubernetes.Interface
|
|
|
|
podName string
|
|
|
|
namespace string
|
|
|
|
errChecker Checker
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
cli: fake.NewSimpleClientset(),
|
|
|
|
podName: "pod",
|
|
|
|
namespace: "ns",
|
|
|
|
errChecker: NotNil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cli: fake.NewSimpleClientset(&v1.Pod{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: "pod",
|
|
|
|
Namespace: "notns",
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
podName: "pod",
|
|
|
|
namespace: "ns",
|
|
|
|
errChecker: NotNil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cli: fake.NewSimpleClientset(&v1.Pod{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: "pod",
|
|
|
|
Namespace: "ns",
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
podName: "pod",
|
|
|
|
namespace: "ns",
|
|
|
|
errChecker: IsNil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cli: nil,
|
|
|
|
podName: "pod",
|
|
|
|
namespace: "ns",
|
|
|
|
errChecker: NotNil,
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
cleaner := &cleanse{
|
|
|
|
kubeCli: tc.cli,
|
|
|
|
}
|
|
|
|
err := cleaner.DeletePod(ctx, tc.podName, tc.namespace)
|
|
|
|
c.Check(err, tc.errChecker)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CSITestSuite) TestDeleteSnapshot(c *C) {
|
|
|
|
ctx := context.Background()
|
|
|
|
for _, tc := range []struct {
|
|
|
|
cli dynamic.Interface
|
|
|
|
snapshotName string
|
|
|
|
namespace string
|
2020-12-16 22:23:06 +00:00
|
|
|
groupVersion *metav1.GroupVersionForDiscovery
|
2020-12-07 21:16:53 +00:00
|
|
|
errChecker Checker
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
cli: fakedynamic.NewSimpleDynamicClient(runtime.NewScheme()),
|
|
|
|
snapshotName: "snap1",
|
|
|
|
namespace: "ns",
|
2020-12-16 22:23:06 +00:00
|
|
|
groupVersion: &metav1.GroupVersionForDiscovery{
|
|
|
|
Version: v1alpha1.Version,
|
|
|
|
},
|
|
|
|
errChecker: NotNil,
|
2020-12-07 21:16:53 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
cli: fakedynamic.NewSimpleDynamicClient(runtime.NewScheme(),
|
|
|
|
&unstructured.Unstructured{
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
"apiVersion": fmt.Sprintf("%s/%s", v1alpha1.GroupName, v1alpha1.Version),
|
|
|
|
"kind": "VolumeSnapshot",
|
|
|
|
"metadata": map[string]interface{}{
|
|
|
|
"name": "snap1",
|
|
|
|
"namespace": "notns",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
snapshotName: "pod",
|
|
|
|
namespace: "ns",
|
|
|
|
errChecker: NotNil,
|
2020-12-16 22:23:06 +00:00
|
|
|
groupVersion: &metav1.GroupVersionForDiscovery{
|
|
|
|
Version: v1alpha1.Version,
|
|
|
|
},
|
2020-12-07 21:16:53 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
cli: fakedynamic.NewSimpleDynamicClient(runtime.NewScheme(),
|
|
|
|
&unstructured.Unstructured{
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
"apiVersion": fmt.Sprintf("%s/%s", v1alpha1.GroupName, v1alpha1.Version),
|
|
|
|
"kind": "VolumeSnapshot",
|
|
|
|
"metadata": map[string]interface{}{
|
|
|
|
"name": "snap1",
|
|
|
|
"namespace": "ns",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
snapshotName: "snap1",
|
|
|
|
namespace: "ns",
|
|
|
|
errChecker: IsNil,
|
2020-12-16 22:23:06 +00:00
|
|
|
groupVersion: &metav1.GroupVersionForDiscovery{
|
|
|
|
Version: v1alpha1.Version,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cli: fakedynamic.NewSimpleDynamicClient(runtime.NewScheme(),
|
|
|
|
&unstructured.Unstructured{
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
"apiVersion": fmt.Sprintf("%s/%s", v1beta1.GroupName, v1beta1.Version),
|
|
|
|
"kind": "VolumeSnapshot",
|
|
|
|
"metadata": map[string]interface{}{
|
|
|
|
"name": "snap1",
|
|
|
|
"namespace": "ns",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
snapshotName: "snap1",
|
|
|
|
namespace: "ns",
|
|
|
|
errChecker: NotNil,
|
|
|
|
groupVersion: &metav1.GroupVersionForDiscovery{
|
|
|
|
Version: v1alpha1.Version,
|
|
|
|
},
|
|
|
|
},
|
2021-01-13 02:48:51 +00:00
|
|
|
{
|
|
|
|
cli: fakedynamic.NewSimpleDynamicClient(runtime.NewScheme(),
|
|
|
|
&unstructured.Unstructured{
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
"apiVersion": fmt.Sprintf("%s/%s", v1beta1.GroupName, v1beta1.Version),
|
|
|
|
"kind": "VolumeSnapshot",
|
|
|
|
"metadata": map[string]interface{}{
|
|
|
|
"name": "snap1",
|
|
|
|
"namespace": "ns",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
snapshotName: "snap1",
|
|
|
|
namespace: "ns",
|
|
|
|
errChecker: IsNil,
|
|
|
|
groupVersion: &metav1.GroupVersionForDiscovery{
|
|
|
|
Version: v1beta1.Version,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cli: fakedynamic.NewSimpleDynamicClient(runtime.NewScheme(),
|
|
|
|
&unstructured.Unstructured{
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
"apiVersion": fmt.Sprintf("%s/%s", kansnapshot.GroupName, kansnapshot.Version),
|
|
|
|
"kind": "VolumeSnapshot",
|
|
|
|
"metadata": map[string]interface{}{
|
|
|
|
"name": "snap1",
|
|
|
|
"namespace": "ns",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
snapshotName: "snap1",
|
|
|
|
namespace: "ns",
|
|
|
|
errChecker: NotNil,
|
|
|
|
groupVersion: &metav1.GroupVersionForDiscovery{
|
|
|
|
Version: v1alpha1.Version,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cli: fakedynamic.NewSimpleDynamicClient(runtime.NewScheme(),
|
|
|
|
&unstructured.Unstructured{
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
"apiVersion": fmt.Sprintf("%s/%s", kansnapshot.GroupName, kansnapshot.Version),
|
|
|
|
"kind": "VolumeSnapshot",
|
|
|
|
"metadata": map[string]interface{}{
|
|
|
|
"name": "snap1",
|
|
|
|
"namespace": "ns",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
snapshotName: "snap1",
|
|
|
|
namespace: "ns",
|
|
|
|
errChecker: IsNil,
|
|
|
|
groupVersion: &metav1.GroupVersionForDiscovery{
|
|
|
|
Version: kansnapshot.Version,
|
|
|
|
},
|
|
|
|
},
|
2020-12-16 22:23:06 +00:00
|
|
|
{
|
|
|
|
cli: fakedynamic.NewSimpleDynamicClient(runtime.NewScheme()),
|
|
|
|
snapshotName: "pod",
|
|
|
|
namespace: "ns",
|
|
|
|
errChecker: NotNil,
|
2020-12-07 21:16:53 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
cli: nil,
|
|
|
|
snapshotName: "pod",
|
|
|
|
namespace: "ns",
|
|
|
|
errChecker: NotNil,
|
|
|
|
},
|
|
|
|
} {
|
2023-07-24 21:23:35 +00:00
|
|
|
cleaner := NewCleaner(nil, tc.cli)
|
2020-12-16 22:23:06 +00:00
|
|
|
err := cleaner.DeleteSnapshot(ctx, tc.snapshotName, tc.namespace, tc.groupVersion)
|
2020-12-07 21:16:53 +00:00
|
|
|
c.Check(err, tc.errChecker)
|
|
|
|
}
|
|
|
|
}
|
2022-06-30 00:39:09 +00:00
|
|
|
|
|
|
|
func (s *CSITestSuite) TestWaitForPVCReady(c *C) {
|
|
|
|
ctx := context.Background()
|
|
|
|
const ns = "ns"
|
|
|
|
const pvc = "pvc"
|
|
|
|
boundPVC := s.getPVC(ns, pvc, v1.ClaimBound)
|
|
|
|
claimLostPVC := s.getPVC(ns, pvc, v1.ClaimLost)
|
|
|
|
stuckPVC := s.getPVC(ns, pvc, "")
|
2022-10-25 19:02:47 +00:00
|
|
|
normalGetFunc := func(action k8stesting.Action) (handled bool, ret runtime.Object, err error) {
|
2022-06-30 00:39:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
deadlineExceededGetFunc := func(action k8stesting.Action) (handled bool, ret runtime.Object, err error) {
|
|
|
|
return true, nil, pkgerrors.Wrapf(context.DeadlineExceeded, "some wrapped error")
|
|
|
|
}
|
|
|
|
|
|
|
|
warningEvent := v1.Event{
|
2022-10-25 19:02:47 +00:00
|
|
|
Type: v1.EventTypeWarning,
|
2022-06-30 00:39:09 +00:00
|
|
|
Message: "waiting for a volume to be created, either by external provisioner \"ceph.com/rbd\" or manually created by system administrator",
|
|
|
|
}
|
|
|
|
for _, tc := range []struct {
|
|
|
|
description string
|
2022-10-25 19:02:47 +00:00
|
|
|
cli kubernetes.Interface
|
|
|
|
pvcGetFunc func(action k8stesting.Action) (handled bool, ret runtime.Object, err error)
|
|
|
|
eventsList []v1.Event
|
|
|
|
errChecker Checker
|
|
|
|
errString string
|
2022-06-30 00:39:09 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
description: "Happy path",
|
2022-10-25 19:02:47 +00:00
|
|
|
cli: fake.NewSimpleClientset(boundPVC),
|
|
|
|
pvcGetFunc: normalGetFunc,
|
|
|
|
errChecker: IsNil,
|
2022-06-30 00:39:09 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Missing PVC",
|
2022-10-25 19:02:47 +00:00
|
|
|
cli: fake.NewSimpleClientset(),
|
|
|
|
pvcGetFunc: normalGetFunc,
|
|
|
|
errChecker: NotNil,
|
|
|
|
errString: "Could not find PVC",
|
2022-06-30 00:39:09 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "PVC ClaimLost",
|
|
|
|
cli: fake.NewSimpleClientset(claimLostPVC),
|
|
|
|
pvcGetFunc: normalGetFunc,
|
|
|
|
errChecker: NotNil,
|
|
|
|
errString: "ClaimLost",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "context.DeadlineExceeded but no event warnings",
|
|
|
|
cli: fake.NewSimpleClientset(stuckPVC),
|
|
|
|
pvcGetFunc: deadlineExceededGetFunc,
|
|
|
|
errChecker: NotNil,
|
|
|
|
errString: context.DeadlineExceeded.Error(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "context.DeadlineExceeded, unable to provision PVC",
|
|
|
|
cli: fake.NewSimpleClientset(stuckPVC),
|
|
|
|
pvcGetFunc: deadlineExceededGetFunc,
|
|
|
|
eventsList: []v1.Event{warningEvent},
|
|
|
|
errChecker: NotNil,
|
|
|
|
errString: warningEvent.Message,
|
|
|
|
},
|
2022-10-25 19:02:47 +00:00
|
|
|
} {
|
2022-06-30 00:39:09 +00:00
|
|
|
fmt.Println("test:", tc.description)
|
|
|
|
creator := &applicationCreate{kubeCli: tc.cli}
|
|
|
|
creator.kubeCli.(*fake.Clientset).PrependReactor("get", "persistentvolumeclaims", tc.pvcGetFunc)
|
|
|
|
creator.kubeCli.(*fake.Clientset).PrependReactor("list", "events", func(action k8stesting.Action) (handled bool, ret runtime.Object, err error) {
|
|
|
|
return true, &v1.EventList{Items: tc.eventsList}, nil
|
|
|
|
})
|
|
|
|
err := creator.WaitForPVCReady(ctx, ns, pvc)
|
|
|
|
c.Check(err, tc.errChecker)
|
|
|
|
if err != nil {
|
|
|
|
c.Assert(strings.Contains(err.Error(), tc.errString), Equals, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CSITestSuite) getPVC(ns, pvc string, phase v1.PersistentVolumeClaimPhase) *v1.PersistentVolumeClaim {
|
|
|
|
return &v1.PersistentVolumeClaim{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2022-10-25 19:02:47 +00:00
|
|
|
Name: pvc,
|
2022-06-30 00:39:09 +00:00
|
|
|
Namespace: ns,
|
|
|
|
},
|
2022-10-25 19:02:47 +00:00
|
|
|
Status: v1.PersistentVolumeClaimStatus{
|
2022-06-30 00:39:09 +00:00
|
|
|
Phase: phase,
|
|
|
|
},
|
|
|
|
}
|
2022-10-25 19:02:47 +00:00
|
|
|
}
|
2023-07-24 21:23:35 +00:00
|
|
|
|
|
|
|
func (s *CSITestSuite) TestWaitForPodReady(c *C) {
|
|
|
|
ctx := context.Background()
|
|
|
|
const ns = "ns"
|
|
|
|
const podName = "pod"
|
|
|
|
readyPod := &v1.Pod{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Namespace: ns,
|
|
|
|
Name: podName,
|
|
|
|
},
|
|
|
|
Spec: v1.PodSpec{
|
|
|
|
Containers: []v1.Container{
|
|
|
|
{Name: "container-0"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Status: v1.PodStatus{
|
|
|
|
Phase: v1.PodRunning,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
warningEvent := v1.Event{
|
|
|
|
Type: v1.EventTypeWarning,
|
|
|
|
Message: "warning event",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range []struct {
|
|
|
|
description string
|
|
|
|
cli kubernetes.Interface
|
|
|
|
eventsList []v1.Event
|
|
|
|
errChecker Checker
|
|
|
|
errString string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
description: "Happy path",
|
|
|
|
cli: fake.NewSimpleClientset(readyPod),
|
|
|
|
errChecker: IsNil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Not found",
|
|
|
|
cli: fake.NewSimpleClientset(),
|
|
|
|
errChecker: NotNil,
|
|
|
|
errString: "not found",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Pod events",
|
|
|
|
cli: fake.NewSimpleClientset(),
|
|
|
|
errChecker: NotNil,
|
|
|
|
errString: "had issues creating Pod",
|
|
|
|
eventsList: []v1.Event{warningEvent},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "No CLI",
|
|
|
|
errChecker: NotNil,
|
|
|
|
errString: "kubeCli not initialized",
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
fmt.Println("TestWaitForPodReady:", tc.description)
|
|
|
|
creator := &applicationCreate{kubeCli: tc.cli}
|
|
|
|
if len(tc.eventsList) > 0 {
|
|
|
|
creator.kubeCli.(*fake.Clientset).PrependReactor("list", "events", func(action k8stesting.Action) (handled bool, ret runtime.Object, err error) {
|
|
|
|
return true, &v1.EventList{Items: tc.eventsList}, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
err := creator.WaitForPodReady(ctx, ns, podName)
|
|
|
|
c.Check(err, tc.errChecker)
|
|
|
|
if err != nil {
|
|
|
|
c.Assert(strings.Contains(err.Error(), tc.errString), Equals, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|