github.com/hairyhenderson/templater@v3.5.0+incompatible/aws/testutils.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"net/http/httptest"
     7  	"net/url"
     8  
     9  	"github.com/aws/aws-sdk-go/service/ec2"
    10  )
    11  
    12  // MockServer -
    13  func MockServer(code int, body string) (*httptest.Server, *Ec2Meta) {
    14  	server, httpClient := MockHTTPServer(code, body)
    15  
    16  	client := &Ec2Meta{server.URL + "/", httpClient, false, make(map[string]string), ClientOptions{}}
    17  	return server, client
    18  }
    19  
    20  // MockHTTPServer -
    21  func MockHTTPServer(code int, body string) (*httptest.Server, *http.Client) {
    22  	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    23  		w.WriteHeader(code)
    24  		// nolint: errcheck
    25  		fmt.Fprintln(w, body)
    26  	}))
    27  
    28  	tr := &http.Transport{
    29  		Proxy: func(req *http.Request) (*url.URL, error) {
    30  			return url.Parse(server.URL)
    31  		},
    32  	}
    33  	httpClient := &http.Client{Transport: tr}
    34  	return server, httpClient
    35  }
    36  
    37  // NewDummyEc2Info -
    38  func NewDummyEc2Info(metaClient *Ec2Meta) *Ec2Info {
    39  	i := &Ec2Info{
    40  		metaClient: metaClient,
    41  		describer:  func() (InstanceDescriber, error) { return DummyInstanceDescriber{}, nil },
    42  	}
    43  	return i
    44  }
    45  
    46  // NewDummyEc2Meta -
    47  func NewDummyEc2Meta() *Ec2Meta {
    48  	return &Ec2Meta{nonAWS: true}
    49  }
    50  
    51  // DummyInstanceDescriber - test doubles
    52  type DummyInstanceDescriber struct {
    53  	tags []*ec2.Tag
    54  }
    55  
    56  // DescribeInstances -
    57  func (d DummyInstanceDescriber) DescribeInstances(*ec2.DescribeInstancesInput) (*ec2.DescribeInstancesOutput, error) {
    58  	output := &ec2.DescribeInstancesOutput{
    59  		Reservations: []*ec2.Reservation{
    60  			{
    61  				Instances: []*ec2.Instance{
    62  					{
    63  						Tags: d.tags,
    64  					},
    65  				},
    66  			},
    67  		},
    68  	}
    69  	return output, nil
    70  }