| 1 | package status |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "net/http/httptest" |
| 6 | "testing" |
| 7 | "time" |
| 8 | ) |
| 9 | |
| 10 | // A box that knows nothing but its own instance id still publishes a |
| 11 | // Metrics block: the id is the join the console's architecture page hangs |
| 12 | // the whole cloud half on, and the attach rule must not drop it for want of |
| 13 | // a memory reading. |
| 14 | func TestCollectBoxPublishesInstanceIDAlone(t *testing.T) { |
| 15 | box := CollectBox(nil, Probes{InstanceID: "i-0c8e41d9"}, time.Now()) |
| 16 | if box.Metrics == nil { |
| 17 | t.Fatal("a box that knows its instance id must publish a Metrics block") |
| 18 | } |
| 19 | if box.Metrics.InstanceID != "i-0c8e41d9" { |
| 20 | t.Errorf("InstanceID = %q, want i-0c8e41d9", box.Metrics.InstanceID) |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | // Off-cloud there is no instance id, and absence is the honest answer: it |
| 25 | // is what tells the console this is somebody else's hardware rather than a |
| 26 | // machine it failed to find. |
| 27 | func TestCollectBoxOmitsAnAbsentInstanceID(t *testing.T) { |
| 28 | box := CollectBox(nil, Probes{}, time.Now()) |
| 29 | if box.Metrics != nil && box.Metrics.InstanceID != "" { |
| 30 | t.Errorf("InstanceID = %q, want empty", box.Metrics.InstanceID) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | // InstanceID reads IMDSv2: a PUT for a token, then a GET carrying it. A |
| 35 | // server that refuses the token (IMDSv1-only, or the uid-gate) yields "", |
| 36 | // never a partial or a panic. |
| 37 | func TestInstanceIDReadsIMDSv2(t *testing.T) { |
| 38 | var sawToken bool |
| 39 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 40 | switch { |
| 41 | case r.Method == http.MethodPut && r.URL.Path == "/latest/api/token": |
| 42 | if r.Header.Get("X-aws-ec2-metadata-token-ttl-seconds") == "" { |
| 43 | t.Error("token request must carry a TTL header") |
| 44 | } |
| 45 | w.Write([]byte("tok123")) |
| 46 | case r.URL.Path == "/latest/meta-data/instance-id": |
| 47 | if r.Header.Get("X-aws-ec2-metadata-token") == "tok123" { |
| 48 | sawToken = true |
| 49 | } |
| 50 | w.Write([]byte("i-0abcdef")) |
| 51 | default: |
| 52 | http.NotFound(w, r) |
| 53 | } |
| 54 | })) |
| 55 | defer srv.Close() |
| 56 | |
| 57 | if got := instanceIDFrom(srv.URL, time.Second); got != "i-0abcdef" { |
| 58 | t.Errorf("instanceIDFrom = %q, want i-0abcdef", got) |
| 59 | } |
| 60 | if !sawToken { |
| 61 | t.Error("the metadata GET must present the IMDSv2 token") |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | func TestInstanceIDIsEmptyWhenIMDSIsUnreachable(t *testing.T) { |
| 66 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 67 | http.Error(w, "denied", http.StatusForbidden) |
| 68 | })) |
| 69 | defer srv.Close() |
| 70 | if got := instanceIDFrom(srv.URL, time.Second); got != "" { |
| 71 | t.Errorf("instanceIDFrom = %q, want empty when IMDS refuses", got) |
| 72 | } |
| 73 | if got := instanceIDFrom("http://127.0.0.1:1", 200*time.Millisecond); got != "" { |
| 74 | t.Errorf("instanceIDFrom = %q, want empty when nothing answers", got) |
| 75 | } |
| 76 | } |
| 77 | |