-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Snapshot Volume for AWS EBS storage (#4361)
* Snapshot Volume for AWS EBS storage * address review suggestions * address review suggestions * Address review comments and add unit tests * Add function for profile validation * fix compilation error * add unit tests
- Loading branch information
1 parent
eb4a83a
commit 830c8d4
Showing
11 changed files
with
924 additions
and
104 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package function | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
|
||
. "gopkg.in/check.v1" | ||
"k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/client-go/kubernetes/fake" | ||
"k8s.io/client-go/testing" | ||
|
||
crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1" | ||
"github.com/kanisterio/kanister/pkg/blockstorage" | ||
"github.com/kanisterio/kanister/pkg/param" | ||
"github.com/kanisterio/kanister/pkg/testutil/mockblockstorage" | ||
) | ||
|
||
type CreateVolumeFromSnapshotTestSuite struct{} | ||
|
||
var _ = Suite(&CreateVolumeFromSnapshotTestSuite{}) | ||
|
||
func (s *CreateVolumeFromSnapshotTestSuite) TestCreateVolumeFromSnapshot(c *C) { | ||
ctx := context.Background() | ||
ns := "ns" | ||
mockGetter := mockblockstorage.NewGetter() | ||
profile := ¶m.Profile{ | ||
Location: crv1alpha1.Location{ | ||
Type: crv1alpha1.LocationTypeS3Compliant, | ||
S3Compliant: &crv1alpha1.S3CompliantLocation{ | ||
Region: "us-west-2"}, | ||
}, | ||
Credential: param.Credential{ | ||
Type: param.CredentialTypeKeyPair, | ||
KeyPair: ¶m.KeyPair{ | ||
ID: "foo", | ||
Secret: "bar", | ||
}, | ||
}, | ||
} | ||
cli := fake.NewSimpleClientset() | ||
// fake doesn't handle generated names for PVs, so ... | ||
var i int | ||
pvl := &v1.PersistentVolumeList{} | ||
// kube.CreatePV() calls create() and list() which is to be handled for fake client | ||
cli.PrependReactor("create", "persistentvolumes", | ||
func(action testing.Action) (handled bool, ret runtime.Object, err error) { | ||
ca := action.(testing.CreateAction) | ||
pv := ca.GetObject().(*v1.PersistentVolume) | ||
pvl.Items = append(pvl.Items, *pv) | ||
if pv.ObjectMeta.Name == "" && pv.ObjectMeta.GenerateName != "" { | ||
pv.ObjectMeta.Name = fmt.Sprintf("%s%d", pv.ObjectMeta.GenerateName, i) | ||
i++ | ||
return true, pv, nil | ||
} | ||
return false, nil, nil | ||
}) | ||
cli.PrependReactor("list", "persistentvolumes", | ||
func(action testing.Action) (handled bool, ret runtime.Object, err error) { | ||
return true, pvl, nil | ||
}) | ||
tags := []*blockstorage.KeyValue{ | ||
{Key: "testkey", Value: "testval"}, | ||
} | ||
volInfo1 := VolumeSnapshotInfo{SnapshotID: "snap-1", Type: blockstorage.TypeEBS, Region: "us-west-2", PVCName: "pvc-1", Az: "us-west-2a", Tags: tags, VolumeType: "ssd"} | ||
volInfo2 := VolumeSnapshotInfo{SnapshotID: "snap-2", Type: blockstorage.TypeEBS, Region: "us-west-2", PVCName: "pvc-2", Az: "us-west-2a", Tags: tags, VolumeType: "ssd"} | ||
var PVCData1 []VolumeSnapshotInfo | ||
PVCData1 = append(PVCData1, volInfo1) | ||
PVCData1 = append(PVCData1, volInfo2) | ||
info, err := json.Marshal(PVCData1) | ||
c.Assert(err, IsNil) | ||
snapinfo := string(info) | ||
for _, tc := range []struct { | ||
snapshotinfo string | ||
check Checker | ||
}{ | ||
{ | ||
snapshotinfo: snapinfo, | ||
check: IsNil, | ||
}, | ||
} { | ||
providerList, err := createVolumeFromSnapshot(ctx, cli, ns, tc.snapshotinfo, profile, mockGetter) | ||
c.Assert(providerList, Not(Equals), tc.check) | ||
c.Assert(err, tc.check) | ||
if err != nil { | ||
continue | ||
} | ||
c.Assert(len(providerList) == 2, Equals, true) | ||
provider, ok := providerList["pvc-1"] | ||
c.Assert(ok, Equals, true) | ||
c.Assert(len(provider.(*mockblockstorage.Provider).SnapIDList) == 1, Equals, true) | ||
c.Assert(mockblockstorage.CheckID("snap-1", provider.(*mockblockstorage.Provider).SnapIDList), Equals, true) | ||
c.Assert(len(provider.(*mockblockstorage.Provider).VolIDList) == 1, Equals, true) | ||
|
||
provider, ok = providerList["pvc-2"] | ||
c.Assert(ok, Equals, true) | ||
c.Assert(len(provider.(*mockblockstorage.Provider).SnapIDList) == 1, Equals, true) | ||
c.Assert(mockblockstorage.CheckID("snap-2", provider.(*mockblockstorage.Provider).SnapIDList), Equals, true) | ||
c.Assert(len(provider.(*mockblockstorage.Provider).VolIDList) == 1, Equals, true) | ||
|
||
_, err = cli.Core().PersistentVolumeClaims(ns).Get("pvc-1", metav1.GetOptions{}) | ||
c.Assert(err, IsNil) | ||
_, err = cli.Core().PersistentVolumeClaims(ns).Get("pvc-2", metav1.GetOptions{}) | ||
c.Assert(err, IsNil) | ||
} | ||
} |
Oops, something went wrong.