github.com/webdestroya/awsmocker@v0.2.6/main.go (about)

     1  package awsmocker
     2  
     3  import (
     4  	"os"
     5  	"time"
     6  
     7  	"github.com/aws/aws-sdk-go-v2/aws"
     8  )
     9  
    10  // Returned when you start the server, provides you some information if needed
    11  type MockerInfo struct {
    12  	// URL of the proxy server
    13  	ProxyURL string
    14  
    15  	// Aws configuration to use
    16  	// This is only provided if you gave ReturnAwsConfig in the options
    17  	AwsConfig *aws.Config
    18  }
    19  
    20  func Start(t TestingT, options *MockerOptions) *MockerInfo {
    21  
    22  	if h, ok := t.(tHelper); ok {
    23  		h.Helper()
    24  	}
    25  
    26  	if options == nil {
    27  		options = &MockerOptions{}
    28  	}
    29  
    30  	if options.Timeout == 0 {
    31  		options.Timeout = 5 * time.Second
    32  	}
    33  
    34  	if !options.SkipDefaultMocks {
    35  		options.Mocks = append(options.Mocks, MockStsGetCallerIdentityValid)
    36  	}
    37  
    38  	if options.MockEc2Metadata {
    39  		options.Mocks = append(options.Mocks, Mock_IMDS_Common()...)
    40  	}
    41  
    42  	// proxy bypass configuration
    43  	if options.DoNotProxy != "" {
    44  		noProxyStr := os.Getenv("NO_PROXY")
    45  		if noProxyStr == "" {
    46  			noProxyStr = os.Getenv("no_proxy")
    47  		}
    48  		if noProxyStr != "" {
    49  			noProxyStr += ","
    50  		}
    51  		noProxyStr += options.DoNotProxy
    52  
    53  		t.Setenv("NO_PROXY", noProxyStr)
    54  		t.Setenv("no_proxy", noProxyStr)
    55  	}
    56  
    57  	mocks := make([]*MockedEndpoint, 0, len(options.Mocks))
    58  	for i := range options.Mocks {
    59  		if options.Mocks[i] == nil {
    60  			continue
    61  		}
    62  		mocks = append(mocks, options.Mocks[i])
    63  	}
    64  
    65  	server := &mocker{
    66  		t:                  t,
    67  		timeout:            options.Timeout,
    68  		verbose:            options.Verbose,
    69  		debugTraffic:       getDebugMode(), // options.DebugTraffic,
    70  		doNotOverrideCreds: options.DoNotOverrideCreds,
    71  		doNotFailUnhandled: options.DoNotFailUnhandledRequests,
    72  		mocks:              mocks,
    73  		usingAwsConfig:     options.ReturnAwsConfig,
    74  	}
    75  	server.Start()
    76  
    77  	info := &MockerInfo{
    78  		ProxyURL: server.httpServer.URL,
    79  	}
    80  
    81  	if options.ReturnAwsConfig {
    82  		cfg := server.buildAwsConfig()
    83  		info.AwsConfig = &cfg
    84  	}
    85  
    86  	return info
    87  }