Skip to content

Commit

Permalink
Fix aws-sdk-go test behavior (#229)
Browse files Browse the repository at this point in the history
- Added missing endpoint entry in the case of secure endpoint.
- Added bucket creation and deletion code, as the error code is different 
  between AWS S3 and Minio server, if test is run with a non-existent bucket.

Fixes #228
  • Loading branch information
kannappanr authored and harshavardhana committed Nov 18, 2017
1 parent 5c42d21 commit 734b41f
Showing 1 changed file with 36 additions and 14 deletions.
50 changes: 36 additions & 14 deletions run/core/aws-sdk-go/quick-tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"encoding/xml"
"errors"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"os"
Expand Down Expand Up @@ -127,10 +126,29 @@ func randString(n int, src rand.Source, prefix string) string {
return prefix + string(b[0:30-len(prefix)])
}

func testPresignedPut(s3Client *s3.S3) {
func cleanup(s3Client *s3.S3, bucket string, object string, function string,
args map[string]interface{}, startTime time.Time) {

// Deleting the object, just in case it was created. Will not check for errors.
s3Client.DeleteObject(&s3.DeleteObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(object),
})

_, err := s3Client.DeleteBucket(&s3.DeleteBucketInput{
Bucket: aws.String(bucket),
})
if err != nil {
failureLog(function, args, startTime, "", "AWS SDK Go DeleteBucket Failed", err).Fatal()
return
}

}

func testPresignedPutInvalidHash(s3Client *s3.S3) {
startTime := time.Now()
function := "PresignedPut"
bucket := randString(60, rand.NewSource(time.Now().UnixNano()), "aws-sdk-go-test")
bucket := randString(60, rand.NewSource(time.Now().UnixNano()), "aws-sdk-go-test-")
object := "presignedTest"
expiry := 1 * time.Minute
args := map[string]interface{}{
Expand All @@ -139,6 +157,15 @@ func testPresignedPut(s3Client *s3.S3) {
"expiry": expiry,
}

_, err := s3Client.CreateBucket(&s3.CreateBucketInput{
Bucket: aws.String(bucket),
})
if err != nil {
failureLog(function, args, startTime, "", "AWS SDK Go CreateBucket Failed", err).Fatal()
return
}
defer cleanup(s3Client, bucket, object, function, args, startTime)

req, _ := s3Client.PutObjectRequest(&s3.PutObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(object),
Expand All @@ -161,15 +188,11 @@ func testPresignedPut(s3Client *s3.S3) {
failureLog(function, args, startTime, "", "AWS SDK Go presigned put request failed", err).Fatal()
return
}
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
failureLog(function, args, startTime, "", "AWS SDK Go reading response body failed", err).Fatal()
return
}
defer resp.Body.Close()

dec := xml.NewDecoder(resp.Body)
errResp := ErrorResponse{}
err = xml.Unmarshal(body, &errResp)
err = dec.Decode(&errResp)
if err != nil {
failureLog(function, args, startTime, "", "AWS SDK Go unmarshalling xml failed", err).Fatal()
return
Expand All @@ -179,6 +202,7 @@ func testPresignedPut(s3Client *s3.S3) {
failureLog(function, args, startTime, "", fmt.Sprintf("AWS SDK Go presigned PUT expected to fail with XAmzContentSHA256Mismatch but got %v", errResp.Code), errors.New("AWS S3 error code mismatch")).Fatal()
return
}

successLogger(function, args, startTime).Info()
}

Expand All @@ -189,7 +213,7 @@ func main() {
secure := os.Getenv("ENABLE_HTTPS")
sdkEndpoint := "http://" + endpoint
if secure == "1" {
sdkEndpoint = "https://"
sdkEndpoint = "https://" + endpoint
}

creds := credentials.NewStaticCredentials(accessKey, secretKey, "")
Expand All @@ -201,8 +225,6 @@ func main() {
S3ForcePathStyle: aws.Bool(true),
}

s3Config = s3Config.WithLogLevel(aws.LogDebug)

// Create an S3 service object in the default region.
s3Client := s3.New(newSession, s3Config)

Expand All @@ -215,5 +237,5 @@ func main() {
// log Info or above -- success cases are Info level, failures are Fatal level
log.SetLevel(log.InfoLevel)
// execute tests
testPresignedPut(s3Client)
testPresignedPutInvalidHash(s3Client)
}

0 comments on commit 734b41f

Please sign in to comment.