1package imds
2
3import (
4	"context"
5	"net/http/httptest"
6	"strings"
7	"testing"
8	"time"
9
10	"github.com/google/go-cmp/cmp"
11)
12
13const instanceIdentityDocument = `{
14	"devpayProductCodes" : ["abc","123"],
15	"marketplaceProductCodes" : [ "1a2bc3" ],
16	"availabilityZone" : "us-east-1d",
17	"privateIp" : "10.158.112.84",
18	"version" : "2010-08-31",
19	"region" : "us-east-1",
20	"instanceId" : "i-1234567890abcdef0",
21	"billingProducts" : ["123"],
22	"instanceType" : "t1.micro",
23	"accountId" : "123456789012",
24	"pendingTime" : "2015-11-19T16:32:11Z",
25	"imageId" : "ami-5fb8c835",
26	"kernelId" : "aki-919dcaf8",
27	"ramdiskId" : "abc123",
28	"architecture" : "x86_64"
29}`
30
31func TestGetInstanceIdentityDocument(t *testing.T) {
32
33	cases := map[string]struct {
34		Body         []byte
35		ExpectResult InstanceIdentityDocument
36		ExpectTrace  []string
37		ExpectErr    string
38	}{
39		"success": {
40			Body: []byte(instanceIdentityDocument),
41			ExpectResult: InstanceIdentityDocument{
42				DevpayProductCodes:      []string{"abc", "123"},
43				MarketplaceProductCodes: []string{"1a2bc3"},
44				AvailabilityZone:        "us-east-1d",
45				PrivateIP:               "10.158.112.84",
46				Version:                 "2010-08-31",
47				Region:                  "us-east-1",
48				InstanceID:              "i-1234567890abcdef0",
49				BillingProducts:         []string{"123"},
50				InstanceType:            "t1.micro",
51				AccountID:               "123456789012",
52				PendingTime:             time.Date(2015, 11, 19, 16, 32, 11, 0, time.UTC),
53				ImageID:                 "ami-5fb8c835",
54				KernelID:                "aki-919dcaf8",
55				RamdiskID:               "abc123",
56				Architecture:            "x86_64",
57			},
58			ExpectTrace: []string{
59				getTokenPath,
60				getInstanceIdentityDocumentPath,
61			},
62		},
63	}
64
65	ctx := context.Background()
66
67	for name, c := range cases {
68		t.Run(name, func(t *testing.T) {
69			trace := newRequestTrace()
70			server := httptest.NewServer(trace.WrapHandler(
71				newTestServeMux(t,
72					newSecureAPIHandler(t,
73						[]string{"tokenA"},
74						5*time.Minute,
75						&successAPIResponseHandler{t: t,
76							path:   getInstanceIdentityDocumentPath,
77							method: "GET",
78							body:   append([]byte{}, c.Body...),
79						},
80					))))
81			defer server.Close()
82
83			// Asserts
84			client := New(Options{
85				Endpoint: server.URL,
86			})
87
88			resp, err := client.GetInstanceIdentityDocument(ctx, nil)
89			if len(c.ExpectErr) != 0 {
90				if err == nil {
91					t.Fatalf("expect error, got none")
92				}
93				if e, a := c.ExpectErr, err.Error(); !strings.Contains(a, e) {
94					t.Fatalf("expect error to contain %v, got %v", e, a)
95				}
96				return
97			}
98			if err != nil {
99				t.Fatalf("expect no error, got %v", err)
100			}
101
102			if resp == nil {
103				t.Fatalf("expect resp, got none")
104			}
105
106			if diff := cmp.Diff(c.ExpectResult, resp.InstanceIdentityDocument); len(diff) != 0 {
107				t.Errorf("expect result to match\n%s", diff)
108			}
109
110			if diff := cmp.Diff(c.ExpectTrace, trace.requests); len(diff) != 0 {
111				t.Errorf("expect trace to match\n%s", diff)
112			}
113		})
114	}
115}
116