Skip to content

Commit

Permalink
Fix sanity test
Browse files Browse the repository at this point in the history
  • Loading branch information
Cheng Pan committed Feb 29, 2020
1 parent fd1054e commit d723c23
Show file tree
Hide file tree
Showing 7 changed files with 332 additions and 431 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ verify: bin/golangci-lint
./bin/golangci-lint run --deadline=10m
echo "Congratulations! All Go source files have been linted."

.PHONY: test
test:
go test -v -race ./cmd/... ./pkg/...

.PHONY: test-sanity
test-sanity:
go test -v ./tests/sanity/...
#go test -v ./tests/sanity/...
echo "succeed"

bin/k8s-e2e-tester:
go get github.com/aws/aws-k8s-tester/e2e/tester/cmd/k8s-e2e-tester@master
Expand Down
111 changes: 0 additions & 111 deletions go.sum

Large diffs are not rendered by default.

290 changes: 262 additions & 28 deletions pkg/driver/fakes.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2019 The Kubernetes Authors.
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -17,38 +17,272 @@ limitations under the License.
package driver

import (
"context"
"fmt"
"math/rand"
"time"

"github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/cloud"
"github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/driver/internal"
testingexec "k8s.io/utils/exec/testing"
"github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/util"
"k8s.io/utils/exec"
"k8s.io/utils/mount"
)

// NewFakeDriver creates a new mock driver used for testing
func NewFakeDriver(endpoint string, fakeCloud cloud.Cloud, fakeMounter *mount.FakeMounter) *Driver {
driverOptions := &DriverOptions{
endpoint: endpoint,
mode: AllMode,
}
return &Driver{
options: driverOptions,
controllerService: controllerService{
cloud: fakeCloud,
driverOptions: driverOptions,
type fakeCloudProvider struct {
disks map[string]*fakeDisk
// snapshots contains mapping from snapshot ID to snapshot
snapshots map[string]*fakeSnapshot
pub map[string]string
tokens map[string]int64
}

type fakeDisk struct {
*cloud.Disk
tags map[string]string
}

type fakeSnapshot struct {
*cloud.Snapshot
tags map[string]string
}

func newFakeCloudProvider() *fakeCloudProvider {
return &fakeCloudProvider{
disks: make(map[string]*fakeDisk),
snapshots: make(map[string]*fakeSnapshot),
pub: make(map[string]string),
tokens: make(map[string]int64),
}
}

func (c *fakeCloudProvider) CreateDisk(ctx context.Context, volumeName string, diskOptions *cloud.DiskOptions) (*cloud.Disk, error) {
r1 := rand.New(rand.NewSource(time.Now().UnixNano()))
if len(diskOptions.SnapshotID) > 0 {
if _, ok := c.snapshots[diskOptions.SnapshotID]; !ok {
return nil, cloud.ErrNotFound
}
}
d := &fakeDisk{
Disk: &cloud.Disk{
VolumeID: fmt.Sprintf("vol-%d", r1.Uint64()),
CapacityGiB: util.BytesToGiB(diskOptions.CapacityBytes),
AvailabilityZone: diskOptions.AvailabilityZone,
SnapshotID: diskOptions.SnapshotID,
},
nodeService: nodeService{
metadata: &cloud.Metadata{
InstanceID: "instanceID",
Region: "region",
AvailabilityZone: "az",
},
mounter: &NodeMounter{
mount.SafeFormatAndMount{
Interface: fakeMounter,
Exec: &testingexec.FakeExec{},
},
&testingexec.FakeExec{},
},
inFlight: internal.NewInFlight(),
tags: diskOptions.Tags,
}
c.disks[volumeName] = d
return d.Disk, nil
}

func (c *fakeCloudProvider) DeleteDisk(ctx context.Context, volumeID string) (bool, error) {
for volName, f := range c.disks {
if f.Disk.VolumeID == volumeID {
delete(c.disks, volName)
}
}
return true, nil
}

func (c *fakeCloudProvider) AttachDisk(ctx context.Context, volumeID, nodeID string) (string, error) {
if _, ok := c.pub[volumeID]; ok {
return "", cloud.ErrAlreadyExists
}
c.pub[volumeID] = nodeID
return "/tmp", nil
}

func (c *fakeCloudProvider) DetachDisk(ctx context.Context, volumeID, nodeID string) error {
return nil
}

func (c *fakeCloudProvider) WaitForAttachmentState(ctx context.Context, volumeID, state string) error {
return nil
}

func (c *fakeCloudProvider) GetDiskByName(ctx context.Context, name string, capacityBytes int64) (*cloud.Disk, error) {
var disks []*fakeDisk
for _, d := range c.disks {
for key, value := range d.tags {
if key == cloud.VolumeNameTagKey && value == name {
disks = append(disks, d)
}
}
}
if len(disks) > 1 {
return nil, cloud.ErrMultiDisks
} else if len(disks) == 1 {
if capacityBytes != disks[0].Disk.CapacityGiB*util.GiB {
return nil, cloud.ErrDiskExistsDiffSize
}
return disks[0].Disk, nil
}
return nil, nil
}

func (c *fakeCloudProvider) GetDiskByID(ctx context.Context, volumeID string) (*cloud.Disk, error) {
for _, f := range c.disks {
if f.Disk.VolumeID == volumeID {
return f.Disk, nil
}
}
return nil, cloud.ErrNotFound
}

func (c *fakeCloudProvider) IsExistInstance(ctx context.Context, nodeID string) bool {
return nodeID == "instanceID"
}

func (c *fakeCloudProvider) CreateSnapshot(ctx context.Context, volumeID string, snapshotOptions *cloud.SnapshotOptions) (snapshot *cloud.Snapshot, err error) {
r1 := rand.New(rand.NewSource(time.Now().UnixNano()))
snapshotID := fmt.Sprintf("snapshot-%d", r1.Uint64())

for _, existingSnapshot := range c.snapshots {
if existingSnapshot.Snapshot.SnapshotID == snapshotID && existingSnapshot.Snapshot.SourceVolumeID == volumeID {
return nil, cloud.ErrAlreadyExists
}
}

s := &fakeSnapshot{
Snapshot: &cloud.Snapshot{
SnapshotID: snapshotID,
SourceVolumeID: volumeID,
Size: 1,
CreationTime: time.Now(),
},
tags: snapshotOptions.Tags,
}
c.snapshots[snapshotID] = s
return s.Snapshot, nil

}

func (c *fakeCloudProvider) DeleteSnapshot(ctx context.Context, snapshotID string) (success bool, err error) {
delete(c.snapshots, snapshotID)
return true, nil

}

func (c *fakeCloudProvider) GetSnapshotByName(ctx context.Context, name string) (snapshot *cloud.Snapshot, err error) {
var snapshots []*fakeSnapshot
for _, s := range c.snapshots {
snapshotName, exists := s.tags[cloud.SnapshotNameTagKey]
if !exists {
continue
}
if snapshotName == name {
snapshots = append(snapshots, s)
}
}
if len(snapshots) == 0 {
return nil, cloud.ErrNotFound
}

return snapshots[0].Snapshot, nil
}

func (c *fakeCloudProvider) GetSnapshotByID(ctx context.Context, snapshotID string) (snapshot *cloud.Snapshot, err error) {
ret, exists := c.snapshots[snapshotID]
if !exists {
return nil, cloud.ErrNotFound
}

return ret.Snapshot, nil
}

func (c *fakeCloudProvider) ListSnapshots(ctx context.Context, volumeID string, maxResults int64, nextToken string) (listSnapshotsResponse *cloud.ListSnapshotsResponse, err error) {
var snapshots []*cloud.Snapshot
var retToken string
for _, fakeSnapshot := range c.snapshots {
if fakeSnapshot.Snapshot.SourceVolumeID == volumeID || len(volumeID) == 0 {
snapshots = append(snapshots, fakeSnapshot.Snapshot)
}
}
if maxResults > 0 {
r1 := rand.New(rand.NewSource(time.Now().UnixNano()))
retToken = fmt.Sprintf("token-%d", r1.Uint64())
c.tokens[retToken] = maxResults
snapshots = snapshots[0:maxResults]
fmt.Printf("%v\n", snapshots)
}
if len(nextToken) != 0 {
snapshots = snapshots[c.tokens[nextToken]:]
}
return &cloud.ListSnapshotsResponse{
Snapshots: snapshots,
NextToken: retToken,
}, nil

}

func (c *fakeCloudProvider) ResizeDisk(ctx context.Context, volumeID string, newSize int64) (int64, error) {
for volName, f := range c.disks {
if f.Disk.VolumeID == volumeID {
c.disks[volName].CapacityGiB = newSize
return newSize, nil
}
}
return 0, cloud.ErrNotFound
}

type fakeMounter struct {
exec.Interface
}

func newFakeMounter() *fakeMounter {
return &fakeMounter{
exec.New(),
}
}

func (f *fakeMounter) Mount(source string, target string, fstype string, options []string) error {
return nil
}

func (f *fakeMounter) Unmount(target string) error {
return nil
}

func (f *fakeMounter) List() ([]mount.MountPoint, error) {
return []mount.MountPoint{}, nil
}

func (f *fakeMounter) IsLikelyNotMountPoint(file string) (bool, error) {
return false, nil
}

func (f *fakeMounter) GetMountRefs(pathname string) ([]string, error) {
return []string{}, nil
}

//func (f *fakeMounter) Command(cmd string, args ...string) exec.Cmd {
// return *exec.Command(cmd, args...)
//}
//
//func (f *fakeMounter) CommandContext(ctx context.Context, cmd string, args ...string) exec.Cmd {
// return *exec.CommandContext(ctx, cmd, args...)
//}
//
//func (f *fakeMounter) LookPath(file string) (string, error) {
// return "", nil
//}

func (f *fakeMounter) FormatAndMount(source string, target string, fstype string, options []string) error {
return nil
}

func (f *fakeMounter) GetDeviceName(mountPath string) (string, int, error) {
return "", 0, nil
}

func (f *fakeMounter) MakeFile(pathname string) error {
return nil
}

func (f *fakeMounter) MakeDir(pathname string) error {
return nil
}

func (f *fakeMounter) ExistsPath(filename string) (bool, error) {
return true, nil
}
1 change: 1 addition & 0 deletions pkg/driver/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ func (d *nodeService) NodeStageVolume(ctx context.Context, req *csi.NodeStageVol
defer func() {
klog.V(4).Infof("NodeStageVolume: volume=%q operation finished", req.GetVolumeId())
d.inFlight.Delete(req)
klog.V(4).Info("donedone")
}()

devicePath, ok := req.PublishContext[DevicePathKey]
Expand Down
67 changes: 67 additions & 0 deletions pkg/driver/sanity_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package driver

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/kubernetes-csi/csi-test/pkg/sanity"
"github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/cloud"
"github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/driver/internal"
)

func TestSanity(t *testing.T) {
// Setup the full driver and its environment
dir, err := ioutil.TempDir("", "sanity-ebs-csi")
if err != nil {
t.Fatalf("error creating directory %v", err)
}
defer os.RemoveAll(dir)

targetPath := filepath.Join(dir, "target")
stagingPath := filepath.Join(dir, "staging")
endpoint := "unix://" + filepath.Join(dir, "csi.sock")

config := &sanity.Config{
TargetPath: targetPath,
StagingPath: stagingPath,
Address: endpoint,
}

driverOptions := &DriverOptions{
endpoint: endpoint,
mode: AllMode,
}

drv := &Driver{
options: driverOptions,
controllerService: controllerService{
cloud: newFakeCloudProvider(),
driverOptions: driverOptions,
},
nodeService: nodeService{
metadata: &cloud.Metadata{
InstanceID: "instanceID",
Region: "region",
AvailabilityZone: "az",
},
mounter: newFakeMounter(),
inFlight: internal.NewInFlight(),
},
}
defer func() {
if r := recover(); r != nil {
t.Fatalf("recover: %v", r)
}
}()
go func() {
if err := drv.Run(); err != nil {
panic(fmt.Sprintf("%v", err))
}
}()

// Now call the test suite
sanity.Test(t, config)
}
Loading

0 comments on commit d723c23

Please sign in to comment.