github.com/aavshr/aws-sdk-go@v1.41.3/awstesting/integration/integration.go (about)

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