-
Notifications
You must be signed in to change notification settings - Fork 24
/
testdriver.go
179 lines (161 loc) · 5.48 KB
/
testdriver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package e2e
import (
"context"
"fmt"
"strings"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
custom_testsuites "github.com/awslabs/aws-s3-csi-driver/tests/e2e-kubernetes/testsuites"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apiserver/pkg/storage/names"
f "k8s.io/kubernetes/test/e2e/framework"
e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper"
"k8s.io/kubernetes/test/e2e/storage/framework"
)
const (
maxS3ExpressBucketNameLength = 63
)
var (
CommitId string
BucketRegion string // assumed to be the same as k8s cluster's region
BucketPrefix string
Performance bool
)
type s3Driver struct {
driverInfo framework.DriverInfo
}
type s3Volume struct {
bucketName string
}
var _ framework.TestDriver = &s3Driver{}
var _ framework.PreprovisionedVolumeTestDriver = &s3Driver{}
var _ framework.PreprovisionedPVTestDriver = &s3Driver{}
func initS3Driver() *s3Driver {
return &s3Driver{
driverInfo: framework.DriverInfo{
Name: "s3.csi.aws.com",
MaxFileSize: framework.FileSizeLarge,
SupportedFsType: sets.NewString(
"", // Default fsType
),
Capabilities: map[framework.Capability]bool{
framework.CapPersistence: true,
},
RequiredAccessModes: []v1.PersistentVolumeAccessMode{
v1.ReadWriteMany,
v1.ReadOnlyMany,
},
},
}
}
func (d *s3Driver) GetDriverInfo() *framework.DriverInfo {
return &d.driverInfo
}
func (d *s3Driver) SkipUnsupportedTest(pattern framework.TestPattern) {
if pattern.VolType != framework.PreprovisionedPV {
e2eskipper.Skipf("S3 Driver only supports static provisioning -- skipping")
}
}
func (d *s3Driver) PrepareTest(ctx context.Context, f *f.Framework) *framework.PerTestConfig {
config := &framework.PerTestConfig{
Driver: d,
Prefix: "s3",
Framework: f,
}
return config
}
func (d *s3Driver) CreateVolume(ctx context.Context, config *framework.PerTestConfig, volumeType framework.TestVolType) framework.TestVolume {
if volumeType != framework.PreprovisionedPV {
f.Failf("Unsupported volType: %v is specified", volumeType)
}
bucketName := names.SimpleNameGenerator.GenerateName(fmt.Sprintf("%s-e2e-kubernetes-%s-", BucketPrefix, CommitId))
input := &s3.CreateBucketInput{
Bucket: aws.String(bucketName),
// note: you need this if testing in non us-east-1 regions
// CreateBucketConfiguration: &types.CreateBucketConfiguration{
// LocationConstraint: types.BucketLocationConstraint(BucketRegion),
// },
}
if config.Prefix == custom_testsuites.S3ExpressTestIdentifier {
// assume us-east-1 since that's where our integration tests currently do their work
// https://docs.aws.amazon.com/AmazonS3/latest/userguide/s3-express-networking.html
regionAz := "use1-az4"
if BucketRegion == "us-west-2" {
regionAz = "usw2-az1"
}
// refer to s3 express bucket naming conventions
// https://docs.aws.amazon.com/AmazonS3/latest/userguide/directory-bucket-naming-rules.html
suffix := fmt.Sprintf("--%s--x-s3", regionAz)
// s3 express doesn't allow non-virtually routable names
bucketName = strings.Replace(bucketName, ".", "", -1)
if len(bucketName)+len(suffix) > maxS3ExpressBucketNameLength {
bucketName = strings.TrimRight(bucketName[:maxS3ExpressBucketNameLength-len(suffix)], "-")
}
bucketName = fmt.Sprintf("%s%s", bucketName, suffix)
input = &s3.CreateBucketInput{
Bucket: aws.String(bucketName),
CreateBucketConfiguration: &types.CreateBucketConfiguration{
Location: &types.LocationInfo{
Name: aws.String(regionAz),
Type: types.LocationTypeAvailabilityZone,
},
Bucket: &types.BucketInfo{
DataRedundancy: types.DataRedundancySingleAvailabilityZone,
Type: types.BucketTypeDirectory,
},
},
}
}
f.Logf("Attempting to create bucket: %s", bucketName)
_, err := newS3Client().CreateBucket(context.TODO(), input)
f.ExpectNoError(err)
f.Logf("Created bucket: %s", bucketName)
return &s3Volume{bucketName: bucketName}
}
func (d *s3Driver) GetPersistentVolumeSource(readOnly bool, fsType string, testVolume framework.TestVolume) (*v1.PersistentVolumeSource, *v1.VolumeNodeAffinity) {
volume, _ := testVolume.(*s3Volume)
return &v1.PersistentVolumeSource{
CSI: &v1.CSIPersistentVolumeSource{
Driver: d.driverInfo.Name,
VolumeHandle: volume.bucketName,
VolumeAttributes: map[string]string{"bucketName": volume.bucketName},
},
}, nil
}
func (v *s3Volume) DeleteVolume(ctx context.Context) {
s3Client := newS3Client()
objects, err := s3Client.ListObjectsV2(context.TODO(), &s3.ListObjectsV2Input{
Bucket: aws.String(v.bucketName),
})
f.ExpectNoError(err)
var objectIds []types.ObjectIdentifier
// get all object keys in the s3 bucket
for _, obj := range objects.Contents {
objectIds = append(objectIds, types.ObjectIdentifier{Key: obj.Key})
}
// delete all objects from the bucket
if len(objectIds) > 0 {
_, err = s3Client.DeleteObjects(context.TODO(), &s3.DeleteObjectsInput{
Bucket: aws.String(v.bucketName),
Delete: &types.Delete{Objects: objectIds},
})
f.ExpectNoError(err)
}
// finally delete the bucket
input := &s3.DeleteBucketInput{
Bucket: aws.String(v.bucketName),
}
_, err = s3Client.DeleteBucket(context.TODO(), input)
f.ExpectNoError(err)
f.Logf("Deleted bucket: %s", v.bucketName)
}
func newS3Client() *s3.Client {
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithRegion(BucketRegion),
)
f.ExpectNoError(err)
return s3.NewFromConfig(cfg)
}