-
Notifications
You must be signed in to change notification settings - Fork 808
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add csi-sanity tests #2254
Open
ElijahQuinones
wants to merge
1
commit into
kubernetes-sigs:master
Choose a base branch
from
ElijahQuinones:sanity
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add csi-sanity tests #2254
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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,219 @@ | ||
// Copyright 2024 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. | ||
// 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 sanity | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"math/rand" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go-v2/service/ec2" | ||
"github.com/aws/aws-sdk-go-v2/service/ec2/types" | ||
"github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/cloud" | ||
"github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/cloud/metadata" | ||
"github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/util" | ||
) | ||
|
||
var ( | ||
disks = make(map[string]*cloud.Disk) | ||
snapshots = make(map[string]*cloud.Snapshot) | ||
snapshotNameToID = make(map[string]string) | ||
) | ||
|
||
type FakeCloud struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. np: can make this package-internal |
||
fakeMetadata metadata.Metadata | ||
mountPath string | ||
} | ||
|
||
func newFakeCloud(fmd metadata.Metadata, mp string) *FakeCloud { | ||
return &FakeCloud{ | ||
fakeMetadata: fmd, | ||
mountPath: mp, | ||
} | ||
} | ||
|
||
func (d *FakeCloud) CreateDisk(ctx context.Context, volumeID string, diskOptions *cloud.DiskOptions) (*cloud.Disk, error) { | ||
for _, existingDisk := range disks { | ||
if existingDisk.VolumeID == volumeID && existingDisk.CapacityGiB != util.BytesToGiB(diskOptions.CapacityBytes) { | ||
return nil, cloud.ErrAlreadyExists | ||
} | ||
} | ||
|
||
if diskOptions.SnapshotID != "" { | ||
if _, exists := snapshots[diskOptions.SnapshotID]; !exists { | ||
return nil, cloud.ErrNotFound | ||
} | ||
newDisk := &cloud.Disk{ | ||
SnapshotID: diskOptions.SnapshotID, | ||
VolumeID: volumeID, | ||
AvailabilityZone: diskOptions.AvailabilityZone, | ||
CapacityGiB: util.BytesToGiB(diskOptions.CapacityBytes), | ||
} | ||
disks[volumeID] = newDisk | ||
return newDisk, nil | ||
} | ||
|
||
newDisk := &cloud.Disk{ | ||
VolumeID: volumeID, | ||
AvailabilityZone: diskOptions.AvailabilityZone, | ||
CapacityGiB: util.BytesToGiB(diskOptions.CapacityBytes), | ||
} | ||
disks[volumeID] = newDisk | ||
return newDisk, nil | ||
} | ||
func (d *FakeCloud) DeleteDisk(ctx context.Context, volumeID string) (bool, error) { | ||
_, exists := disks[volumeID] | ||
if !exists { | ||
return false, cloud.ErrNotFound | ||
} | ||
delete(disks, volumeID) | ||
return true, nil | ||
} | ||
|
||
func (d *FakeCloud) GetDiskByID(ctx context.Context, volumeID string) (*cloud.Disk, error) { | ||
disk, exists := disks[volumeID] | ||
if !exists { | ||
return nil, cloud.ErrNotFound | ||
} | ||
return disk, nil | ||
} | ||
|
||
func (d *FakeCloud) CreateSnapshot(ctx context.Context, volumeID string, opts *cloud.SnapshotOptions) (*cloud.Snapshot, error) { | ||
snapshotID := fmt.Sprintf("snapshot-%d", rand.New(rand.NewSource(time.Now().UnixNano())).Uint64()) | ||
|
||
_, exists := snapshots[snapshotID] | ||
if exists { | ||
return nil, cloud.ErrAlreadyExists | ||
} | ||
newSnapshot := &cloud.Snapshot{ | ||
SnapshotID: snapshotID, | ||
SourceVolumeID: volumeID, | ||
CreationTime: time.Now(), | ||
ReadyToUse: true, | ||
} | ||
snapshots[snapshotID] = newSnapshot | ||
snapshotNameToID[opts.Tags["CSIVolumeSnapshotName"]] = snapshotID | ||
return newSnapshot, nil | ||
} | ||
|
||
func (d *FakeCloud) DeleteSnapshot(ctx context.Context, snapshotID string) (bool, error) { | ||
if _, exists := snapshots[snapshotID]; !exists { | ||
return false, cloud.ErrNotFound | ||
} | ||
for name, id := range snapshotNameToID { | ||
if id == snapshotID { | ||
delete(snapshotNameToID, name) | ||
break | ||
} | ||
} | ||
delete(snapshots, snapshotID) | ||
return true, nil | ||
} | ||
func (d *FakeCloud) GetSnapshotByID(ctx context.Context, snapshotID string) (*cloud.Snapshot, error) { | ||
snapshot, exists := snapshots[snapshotID] | ||
if !exists { | ||
return nil, cloud.ErrNotFound | ||
} | ||
return snapshot, nil | ||
} | ||
|
||
func (d *FakeCloud) GetSnapshotByName(ctx context.Context, name string) (*cloud.Snapshot, error) { | ||
if snapshotID, exists := snapshotNameToID[name]; exists { | ||
return snapshots[snapshotID], nil | ||
} | ||
return nil, cloud.ErrNotFound | ||
} | ||
|
||
func (d *FakeCloud) ListSnapshots(ctx context.Context, sourceVolumeID string, maxResults int32, nextToken string) (*cloud.ListSnapshotsResponse, error) { | ||
var s []*cloud.Snapshot | ||
startIndex := 0 | ||
var err error | ||
|
||
if nextToken != "" { | ||
startIndex, err = strconv.Atoi(nextToken) | ||
if err != nil { | ||
return nil, fmt.Errorf("invalid next token %s", nextToken) | ||
} | ||
} | ||
var nextTokenStr string | ||
count := 0 | ||
for _, snap := range snapshots { | ||
if snap.SourceVolumeID == sourceVolumeID || sourceVolumeID == "" { | ||
if startIndex <= count { | ||
s = append(s, snap) | ||
if maxResults > 0 && int32(len(s)) >= maxResults { | ||
nextTokenStr = strconv.Itoa(startIndex + int(maxResults)) | ||
break | ||
} | ||
} | ||
count++ | ||
} | ||
} | ||
|
||
return &cloud.ListSnapshotsResponse{ | ||
Snapshots: s, | ||
NextToken: nextTokenStr, | ||
}, nil | ||
} | ||
|
||
func (d *FakeCloud) AttachDisk(ctx context.Context, volumeID string, instanceID string) (string, error) { | ||
_, diskExists := disks[volumeID] | ||
if !diskExists || instanceID != d.fakeMetadata.InstanceID { | ||
return "", cloud.ErrNotFound | ||
} | ||
return d.mountPath, nil | ||
} | ||
|
||
func (d *FakeCloud) DetachDisk(ctx context.Context, volumeID string, instanceID string) error { | ||
_, diskExists := disks[volumeID] | ||
if !diskExists || instanceID != d.fakeMetadata.InstanceID { | ||
return cloud.ErrNotFound | ||
} | ||
return nil | ||
} | ||
|
||
func (d *FakeCloud) ResizeOrModifyDisk(ctx context.Context, volumeID string, newSizeBytes int64, modifyOptions *cloud.ModifyDiskOptions) (int32, error) { | ||
disk, exists := disks[volumeID] | ||
if !exists { | ||
return 0, cloud.ErrNotFound | ||
} | ||
newSizeGiB := util.BytesToGiB(newSizeBytes) | ||
disk.CapacityGiB = newSizeGiB | ||
disks[volumeID] = disk | ||
realSizeGiB := newSizeGiB | ||
return realSizeGiB, nil | ||
} | ||
|
||
func (d *FakeCloud) AvailabilityZones(ctx context.Context) (map[string]struct{}, error) { | ||
return map[string]struct{}{}, nil | ||
} | ||
|
||
func (d *FakeCloud) EnableFastSnapshotRestores(ctx context.Context, availabilityZones []string, snapshotID string) (*ec2.EnableFastSnapshotRestoresOutput, error) { | ||
return &ec2.EnableFastSnapshotRestoresOutput{}, nil | ||
} | ||
|
||
func (d *FakeCloud) GetDiskByName(ctx context.Context, name string, capacityBytes int64) (*cloud.Disk, error) { | ||
return &cloud.Disk{}, nil | ||
} | ||
|
||
func (d *FakeCloud) ModifyTags(ctx context.Context, volumeID string, tagOptions cloud.ModifyTagsOptions) error { | ||
return nil | ||
} | ||
|
||
func (d *FakeCloud) WaitForAttachmentState(ctx context.Context, volumeID, expectedState, expectedInstance, expectedDevice string, alreadyAssigned bool) (*types.VolumeAttachment, error) { | ||
return &types.VolumeAttachment{}, nil | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
np: Can make these members of
FakeCloud
, instead of package level fields.