1// +build integration 2 3// Package integration performs initialization and validation for integration 4// tests. 5package integration 6 7import ( 8 "crypto/rand" 9 "fmt" 10 "io" 11 "io/ioutil" 12 "os" 13 14 "github.com/aws/aws-sdk-go/aws" 15 "github.com/aws/aws-sdk-go/aws/session" 16) 17 18// Session is a shared session for all integration tests to use. 19var Session = session.Must(session.NewSession(&aws.Config{ 20 CredentialsChainVerboseErrors: aws.Bool(true), 21})) 22 23func init() { 24 logLevel := Session.Config.LogLevel 25 if os.Getenv("DEBUG") != "" { 26 logLevel = aws.LogLevel(aws.LogDebug) 27 } 28 if os.Getenv("DEBUG_SIGNING") != "" { 29 logLevel = aws.LogLevel(aws.LogDebugWithSigning) 30 } 31 if os.Getenv("DEBUG_BODY") != "" { 32 logLevel = aws.LogLevel(aws.LogDebugWithSigning | aws.LogDebugWithHTTPBody) 33 } 34 Session.Config.LogLevel = logLevel 35} 36 37// UniqueID returns a unique UUID-like identifier for use in generating 38// resources for integration tests. 39func UniqueID() string { 40 uuid := make([]byte, 16) 41 io.ReadFull(rand.Reader, uuid) 42 return fmt.Sprintf("%x", uuid) 43} 44 45// CreateFileOfSize will return an *os.File that is of size bytes 46func CreateFileOfSize(dir string, size int64) (*os.File, error) { 47 file, err := ioutil.TempFile(dir, "s3Bench") 48 if err != nil { 49 return nil, err 50 } 51 52 err = file.Truncate(size) 53 if err != nil { 54 file.Close() 55 os.Remove(file.Name()) 56 return nil, err 57 } 58 59 return file, nil 60} 61 62// SizeToName returns a human-readable string for the given size bytes 63func SizeToName(size int) string { 64 units := []string{"B", "KB", "MB", "GB"} 65 i := 0 66 for size >= 1024 { 67 size /= 1024 68 i++ 69 } 70 71 if i > len(units)-1 { 72 i = len(units) - 1 73 } 74 75 return fmt.Sprintf("%d%s", size, units[i]) 76} 77 78// SessionWithDefaultRegion returns a copy of the integration session with the 79// region set if one was not already provided. 80func SessionWithDefaultRegion(region string) *session.Session { 81 sess := Session.Copy() 82 if v := aws.StringValue(sess.Config.Region); len(v) == 0 { 83 sess.Config.Region = aws.String(region) 84 } 85 86 return sess 87} 88