github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/config_test.go (about) 1 package aws 2 3 import ( 4 "bytes" 5 "fmt" 6 "log" 7 "net/http" 8 "net/http/httptest" 9 "reflect" 10 "testing" 11 "time" 12 13 "github.com/aws/aws-sdk-go/aws" 14 awsCredentials "github.com/aws/aws-sdk-go/aws/credentials" 15 "github.com/aws/aws-sdk-go/aws/session" 16 "github.com/aws/aws-sdk-go/service/ec2" 17 ) 18 19 func TestGetSupportedEC2Platforms(t *testing.T) { 20 ec2Endpoints := []*awsMockEndpoint{ 21 &awsMockEndpoint{ 22 Request: &awsMockRequest{"POST", "/", "Action=DescribeAccountAttributes&" + 23 "AttributeName.1=supported-platforms&Version=2016-11-15"}, 24 Response: &awsMockResponse{200, test_ec2_describeAccountAttributes_response, "text/xml"}, 25 }, 26 } 27 closeFunc, sess, err := getMockedAwsApiSession("EC2", ec2Endpoints) 28 if err != nil { 29 t.Fatal(err) 30 } 31 defer closeFunc() 32 conn := ec2.New(sess) 33 34 platforms, err := GetSupportedEC2Platforms(conn) 35 if err != nil { 36 t.Fatalf("Expected no error, received: %s", err) 37 } 38 expectedPlatforms := []string{"VPC", "EC2"} 39 if !reflect.DeepEqual(platforms, expectedPlatforms) { 40 t.Fatalf("Received platforms: %q\nExpected: %q\n", platforms, expectedPlatforms) 41 } 42 } 43 44 // getMockedAwsApiSession establishes a httptest server to simulate behaviour 45 // of a real AWS API server 46 func getMockedAwsApiSession(svcName string, endpoints []*awsMockEndpoint) (func(), *session.Session, error) { 47 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 48 buf := new(bytes.Buffer) 49 buf.ReadFrom(r.Body) 50 requestBody := buf.String() 51 52 log.Printf("[DEBUG] Received %s API %q request to %q: %s", 53 svcName, r.Method, r.RequestURI, requestBody) 54 55 for _, e := range endpoints { 56 if r.Method == e.Request.Method && r.RequestURI == e.Request.Uri && requestBody == e.Request.Body { 57 log.Printf("[DEBUG] Mocked %s API responding with %d: %s", 58 svcName, e.Response.StatusCode, e.Response.Body) 59 60 w.WriteHeader(e.Response.StatusCode) 61 w.Header().Set("Content-Type", e.Response.ContentType) 62 w.Header().Set("X-Amzn-Requestid", "1b206dd1-f9a8-11e5-becf-051c60f11c4a") 63 w.Header().Set("Date", time.Now().Format(time.RFC1123)) 64 65 fmt.Fprintln(w, e.Response.Body) 66 return 67 } 68 } 69 70 w.WriteHeader(400) 71 return 72 })) 73 74 sc := awsCredentials.NewStaticCredentials("accessKey", "secretKey", "") 75 76 sess, err := session.NewSession(&aws.Config{ 77 Credentials: sc, 78 Region: aws.String("us-east-1"), 79 Endpoint: aws.String(ts.URL), 80 CredentialsChainVerboseErrors: aws.Bool(true), 81 }) 82 83 return ts.Close, sess, err 84 } 85 86 type awsMockEndpoint struct { 87 Request *awsMockRequest 88 Response *awsMockResponse 89 } 90 91 type awsMockRequest struct { 92 Method string 93 Uri string 94 Body string 95 } 96 97 type awsMockResponse struct { 98 StatusCode int 99 Body string 100 ContentType string 101 } 102 103 var test_ec2_describeAccountAttributes_response = `<DescribeAccountAttributesResponse xmlns="http://ec2.amazonaws.com/doc/2016-11-15/"> 104 <requestId>7a62c49f-347e-4fc4-9331-6e8eEXAMPLE</requestId> 105 <accountAttributeSet> 106 <item> 107 <attributeName>supported-platforms</attributeName> 108 <attributeValueSet> 109 <item> 110 <attributeValue>VPC</attributeValue> 111 </item> 112 <item> 113 <attributeValue>EC2</attributeValue> 114 </item> 115 </attributeValueSet> 116 </item> 117 </accountAttributeSet> 118 </DescribeAccountAttributesResponse>`