...

Source file src/github.com/aws/aws-sdk-go-v2/feature/ec2/imds/api_op_GetIAMInfo_test.go

Documentation: github.com/aws/aws-sdk-go-v2/feature/ec2/imds

     1  package imds
     2  
     3  import (
     4  	"context"
     5  	"net/http/httptest"
     6  	"strings"
     7  	"testing"
     8  	"time"
     9  )
    10  
    11  func TestGetIAMInfo(t *testing.T) {
    12  	const validIamInfo = `{
    13  		"Code" : "Success",
    14  		"LastUpdated" : "2016-03-17T12:27:32Z",
    15  		"InstanceProfileArn" : "arn:aws:iam::123456789012:instance-profile/my-instance-profile",
    16  		"InstanceProfileId" : "AIPAABCDEFGHIJKLMN123"
    17  	}`
    18  
    19  	const unsuccessfulIamInfo = `{
    20  		"Code" : "Failed"
    21  	}`
    22  
    23  	cases := map[string]struct {
    24  		Body         []byte
    25  		ExpectResult IAMInfo
    26  		ExpectTrace  []string
    27  		ExpectErr    string
    28  	}{
    29  		"success": {
    30  			Body: []byte(validIamInfo),
    31  			ExpectResult: IAMInfo{
    32  				Code:               "Success",
    33  				LastUpdated:        time.Date(2016, 3, 17, 12, 27, 32, 0, time.UTC),
    34  				InstanceProfileArn: "arn:aws:iam::123456789012:instance-profile/my-instance-profile",
    35  				InstanceProfileID:  "AIPAABCDEFGHIJKLMN123",
    36  			},
    37  			ExpectTrace: []string{
    38  				getTokenPath,
    39  				getIAMInfoPath,
    40  			},
    41  		},
    42  		"not success code": {
    43  			Body:      []byte(unsuccessfulIamInfo),
    44  			ExpectErr: "Failed",
    45  			ExpectTrace: []string{
    46  				getTokenPath,
    47  				getIAMInfoPath,
    48  			},
    49  		},
    50  	}
    51  
    52  	ctx := context.Background()
    53  
    54  	for name, c := range cases {
    55  		t.Run(name, func(t *testing.T) {
    56  			trace := newRequestTrace()
    57  			server := httptest.NewServer(trace.WrapHandler(
    58  				newTestServeMux(t,
    59  					newSecureAPIHandler(t,
    60  						[]string{"tokenA"},
    61  						5*time.Minute,
    62  						&successAPIResponseHandler{t: t,
    63  							path:   getIAMInfoPath,
    64  							method: "GET",
    65  							body:   append([]byte{}, c.Body...),
    66  						},
    67  					))))
    68  			defer server.Close()
    69  
    70  			// Asserts
    71  			client := New(Options{
    72  				Endpoint: server.URL,
    73  			})
    74  
    75  			resp, err := client.GetIAMInfo(ctx, nil)
    76  			if len(c.ExpectErr) != 0 {
    77  				if err == nil {
    78  					t.Fatalf("expect error, got none")
    79  				}
    80  				if e, a := c.ExpectErr, err.Error(); !strings.Contains(a, e) {
    81  					t.Fatalf("expect error to contain %v, got %v", e, a)
    82  				}
    83  				return
    84  			}
    85  			if err != nil {
    86  				t.Fatalf("expect no error, got %v", err)
    87  			}
    88  
    89  			if resp == nil {
    90  				t.Fatalf("expect resp, got none")
    91  			}
    92  
    93  			if diff := cmpDiff(c.ExpectResult, resp.IAMInfo); len(diff) != 0 {
    94  				t.Errorf("expect result to match\n%s", diff)
    95  			}
    96  
    97  			if diff := cmpDiff(c.ExpectTrace, trace.requests); len(diff) != 0 {
    98  				t.Errorf("expect trace to match\n%s", diff)
    99  			}
   100  		})
   101  	}
   102  }
   103  

View as plain text