Add tests for all supported storage classes

Signed-off-by: ddelange <14880945+ddelange@users.noreply.github.com>
This commit is contained in:
ddelange 2022-04-04 09:55:51 +02:00
parent fb937deabf
commit 966fae5463
No known key found for this signature in database
GPG key ID: AFC17B7BBC0301A6
2 changed files with 54 additions and 75 deletions

View file

@ -75,6 +75,18 @@ const listMax = 1000
// noStorageClass defines the value to be used if storage class is not supported by the S3 endpoint // noStorageClass defines the value to be used if storage class is not supported by the S3 endpoint
const noStorageClass = "NONE" const noStorageClass = "NONE"
// s3StorageClasses lists all compatible (instant retrieval) S3 storage classes
var s3StorageClasses = []string{
noStorageClass,
s3.StorageClassStandard,
s3.StorageClassReducedRedundancy,
s3.StorageClassStandardIa,
s3.StorageClassOnezoneIa,
s3.StorageClassIntelligentTiering,
s3.StorageClassOutposts,
s3.StorageClassGlacierIr,
}
// validRegions maps known s3 region identifiers to region descriptors // validRegions maps known s3 region identifiers to region descriptors
var validRegions = map[string]struct{}{} var validRegions = map[string]struct{}{}
@ -310,16 +322,7 @@ func FromParameters(parameters map[string]interface{}) (*Driver, error) {
if !ok { if !ok {
return nil, fmt.Errorf( return nil, fmt.Errorf(
"the storageclass parameter must be one of %v, %v invalid", "the storageclass parameter must be one of %v, %v invalid",
[]string{ s3StorageClasses,
noStorageClass,
s3.StorageClassStandard,
s3.StorageClassReducedRedundancy,
s3.StorageClassStandardIa,
s3.StorageClassOnezoneIa,
s3.StorageClassIntelligentTiering,
s3.StorageClassOutposts,
s3.StorageClassGlacierIr,
},
storageClassParam, storageClassParam,
) )
} }
@ -335,16 +338,7 @@ func FromParameters(parameters map[string]interface{}) (*Driver, error) {
storageClassString != s3.StorageClassGlacierIr { storageClassString != s3.StorageClassGlacierIr {
return nil, fmt.Errorf( return nil, fmt.Errorf(
"the storageclass parameter must be one of %v, %v invalid", "the storageclass parameter must be one of %v, %v invalid",
[]string{ s3StorageClasses,
noStorageClass,
s3.StorageClassStandard,
s3.StorageClassReducedRedundancy,
s3.StorageClassStandardIa,
s3.StorageClassOnezoneIa,
s3.StorageClassIntelligentTiering,
s3.StorageClassOutposts,
s3.StorageClassGlacierIr,
},
storageClassParam, storageClassParam,
) )
} }

View file

@ -228,66 +228,51 @@ func TestStorageClass(t *testing.T) {
} }
defer os.Remove(rootDir) defer os.Remove(rootDir)
standardDriver, err := s3DriverConstructor(rootDir, s3.StorageClassStandard)
if err != nil {
t.Fatalf("unexpected error creating driver with standard storage: %v", err)
}
rrDriver, err := s3DriverConstructor(rootDir, s3.StorageClassReducedRedundancy)
if err != nil {
t.Fatalf("unexpected error creating driver with reduced redundancy storage: %v", err)
}
if _, err = s3DriverConstructor(rootDir, noStorageClass); err != nil {
t.Fatalf("unexpected error creating driver without storage class: %v", err)
}
standardFilename := "/test-standard"
rrFilename := "/test-rr"
contents := []byte("contents") contents := []byte("contents")
ctx := context.Background() ctx := context.Background()
for _, storageClass := range s3StorageClasses {
err = standardDriver.PutContent(ctx, standardFilename, contents) filename := "/test-" + storageClass
s3Driver, err := s3DriverConstructor(rootDir, storageClass)
if err != nil { if err != nil {
t.Fatalf("unexpected error creating content: %v", err) t.Fatalf("unexpected error creating driver with storage class %v: %v", storageClass, err)
} }
defer standardDriver.Delete(ctx, standardFilename)
err = rrDriver.PutContent(ctx, rrFilename, contents) err = s3Driver.PutContent(ctx, filename, contents)
if err != nil { if err != nil {
t.Fatalf("unexpected error creating content: %v", err) t.Fatalf("unexpected error creating content with storage class %v: %v", storageClass, err)
} }
defer rrDriver.Delete(ctx, rrFilename) defer s3Driver.Delete(ctx, filename)
standardDriverUnwrapped := standardDriver.Base.StorageDriver.(*driver) driverUnwrapped := s3Driver.Base.StorageDriver.(*driver)
resp, err := standardDriverUnwrapped.S3.GetObject(&s3.GetObjectInput{ resp, err := driverUnwrapped.S3.GetObject(&s3.GetObjectInput{
Bucket: aws.String(standardDriverUnwrapped.Bucket), Bucket: aws.String(driverUnwrapped.Bucket),
Key: aws.String(standardDriverUnwrapped.s3Path(standardFilename)), Key: aws.String(driverUnwrapped.s3Path(filename)),
}) })
if err != nil { if err != nil {
t.Fatalf("unexpected error retrieving standard storage file: %v", err) t.Fatalf("unexpected error retrieving file with storage class %v: %v", storageClass, err)
} }
defer resp.Body.Close() defer resp.Body.Close()
// Amazon only populates this header value for non-standard storage classes // Amazon only populates this header value for non-standard storage classes
if resp.StorageClass != nil { if storageClass == s3.StorageClassStandard && resp.StorageClass != nil {
t.Fatalf("unexpected storage class for standard file: %v", resp.StorageClass) t.Fatalf(
"unexpected response storage class for file with storage class %v: %v",
storageClass,
*resp.StorageClass,
)
} else if storageClass != s3.StorageClassStandard && resp.StorageClass == nil {
t.Fatalf(
"unexpected response storage class for file with storage class %v: %v",
storageClass,
s3.StorageClassStandard,
)
} else if storageClass != s3.StorageClassStandard && storageClass != *resp.StorageClass {
t.Fatalf(
"unexpected response storage class for file with storage class %v: %v",
storageClass,
*resp.StorageClass,
)
} }
rrDriverUnwrapped := rrDriver.Base.StorageDriver.(*driver)
resp, err = rrDriverUnwrapped.S3.GetObject(&s3.GetObjectInput{
Bucket: aws.String(rrDriverUnwrapped.Bucket),
Key: aws.String(rrDriverUnwrapped.s3Path(rrFilename)),
})
if err != nil {
t.Fatalf("unexpected error retrieving reduced-redundancy storage file: %v", err)
} }
defer resp.Body.Close()
if resp.StorageClass == nil {
t.Fatalf("unexpected storage class for reduced-redundancy file: %v", s3.StorageClassStandard)
} else if *resp.StorageClass != s3.StorageClassReducedRedundancy {
t.Fatalf("unexpected storage class for reduced-redundancy file: %v", *resp.StorageClass)
}
} }
func TestDelete(t *testing.T) { func TestDelete(t *testing.T) {