github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/client/fingerprint/env_azure_test.go (about) 1 package fingerprint 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "net/http" 7 "net/http/httptest" 8 "strings" 9 "testing" 10 11 "github.com/hashicorp/nomad/client/config" 12 "github.com/hashicorp/nomad/helper/testlog" 13 "github.com/hashicorp/nomad/nomad/structs" 14 ) 15 16 func TestAzureFingerprint_nonAzure(t *testing.T) { 17 18 t.Setenv("AZURE_ENV_URL", "http://127.0.0.1/metadata/instance/") 19 f := NewEnvAzureFingerprint(testlog.HCLogger(t)) 20 node := &structs.Node{ 21 Attributes: make(map[string]string), 22 } 23 24 request := &FingerprintRequest{Config: &config.Config{}, Node: node} 25 var response FingerprintResponse 26 err := f.Fingerprint(request, &response) 27 if err != nil { 28 t.Fatalf("err: %v", err) 29 } 30 31 if response.Detected { 32 t.Fatalf("expected response to not be applicable") 33 } 34 35 if len(response.Attributes) > 0 { 36 t.Fatalf("Should have zero attributes without test server") 37 } 38 } 39 40 func testFingerprint_Azure(t *testing.T, withExternalIp bool) { 41 node := &structs.Node{ 42 Attributes: make(map[string]string), 43 } 44 45 // configure mock server with fixture routes, data 46 routes := routes{} 47 if err := json.Unmarshal([]byte(AZURE_routes), &routes); err != nil { 48 t.Fatalf("Failed to unmarshal JSON in GCE ENV test: %s", err) 49 } 50 if withExternalIp { 51 networkEndpoint := &endpoint{ 52 Uri: "/metadata/instance/network/interface/0/ipv4/ipAddress/0/publicIpAddress", 53 ContentType: "text/plain", 54 Body: "104.44.55.66", 55 } 56 routes.Endpoints = append(routes.Endpoints, networkEndpoint) 57 } 58 59 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 60 value, ok := r.Header["Metadata"] 61 if !ok { 62 t.Fatal("Metadata not present in HTTP request header") 63 } 64 if value[0] != "true" { 65 t.Fatalf("Expected Metadata true, saw %s", value[0]) 66 } 67 68 uavalue, ok := r.Header["User-Agent"] 69 if !ok { 70 t.Fatal("User-Agent not present in HTTP request header") 71 } 72 if !strings.Contains(uavalue[0], "Nomad/") { 73 t.Fatalf("Expected User-Agent to contain Nomad/, got %s", uavalue[0]) 74 } 75 76 uri := r.RequestURI 77 if r.URL.RawQuery != "" { 78 uri = strings.Replace(uri, "?"+r.URL.RawQuery, "", 1) 79 } 80 81 found := false 82 for _, e := range routes.Endpoints { 83 if uri == e.Uri { 84 w.Header().Set("Content-Type", e.ContentType) 85 fmt.Fprintln(w, e.Body) 86 found = true 87 } 88 } 89 90 if !found { 91 w.WriteHeader(404) 92 } 93 })) 94 defer ts.Close() 95 t.Setenv("AZURE_ENV_URL", ts.URL+"/metadata/instance/") 96 f := NewEnvAzureFingerprint(testlog.HCLogger(t)) 97 98 request := &FingerprintRequest{Config: &config.Config{}, Node: node} 99 var response FingerprintResponse 100 err := f.Fingerprint(request, &response) 101 if err != nil { 102 t.Fatalf("err: %v", err) 103 } 104 105 if !response.Detected { 106 t.Fatalf("expected response to be applicable") 107 } 108 109 keys := []string{ 110 "unique.platform.azure.id", 111 "unique.platform.azure.name", 112 "platform.azure.location", 113 "platform.azure.resource-group", 114 "platform.azure.scale-set", 115 "platform.azure.vm-size", 116 "unique.platform.azure.local-ipv4", 117 "unique.platform.azure.mac", 118 "platform.azure.tag.Environment", 119 "platform.azure.tag.abc", 120 "unique.platform.azure.tag.foo", 121 } 122 123 for _, k := range keys { 124 assertNodeAttributeContains(t, response.Attributes, k) 125 } 126 127 if len(response.Links) == 0 { 128 t.Fatalf("Empty links for Node in GCE Fingerprint test") 129 } 130 131 // Make sure Links contains the GCE ID. 132 for _, k := range []string{"azure"} { 133 assertNodeLinksContains(t, response.Links, k) 134 } 135 136 assertNodeAttributeEquals(t, response.Attributes, "unique.platform.azure.id", "13f56399-bd52-4150-9748-7190aae1ff21") 137 assertNodeAttributeEquals(t, response.Attributes, "unique.platform.azure.name", "demo01.internal") 138 assertNodeAttributeEquals(t, response.Attributes, "platform.azure.location", "eastus") 139 assertNodeAttributeEquals(t, response.Attributes, "platform.azure.resource-group", "myrg") 140 assertNodeAttributeEquals(t, response.Attributes, "platform.azure.scale-set", "nomad-clients") 141 assertNodeAttributeEquals(t, response.Attributes, "unique.platform.azure.local-ipv4", "10.1.0.4") 142 assertNodeAttributeEquals(t, response.Attributes, "unique.platform.azure.mac", "000D3AF806EC") 143 assertNodeAttributeEquals(t, response.Attributes, "platform.azure.tag.Environment", "Test") 144 assertNodeAttributeEquals(t, response.Attributes, "platform.azure.tag.abc", "def") 145 assertNodeAttributeEquals(t, response.Attributes, "unique.platform.azure.tag.foo", "true") 146 147 if withExternalIp { 148 assertNodeAttributeEquals(t, response.Attributes, "unique.platform.azure.public-ipv4", "104.44.55.66") 149 } else if _, ok := response.Attributes["unique.platform.azure.public-ipv4"]; ok { 150 t.Fatal("unique.platform.azure.public-ipv4 is set without an external IP") 151 } 152 153 } 154 155 const AZURE_routes = ` 156 { 157 "endpoints": [ 158 { 159 "uri": "/metadata/instance/compute/azEnvironment", 160 "content-type": "text/plain", 161 "body": "AzurePublicCloud" 162 }, 163 164 { 165 "uri": "/metadata/instance/compute/location", 166 "content-type": "text/plain", 167 "body": "eastus" 168 }, 169 { 170 "uri": "/metadata/instance/compute/name", 171 "content-type": "text/plain", 172 "body": "demo01.internal" 173 }, 174 { 175 "uri": "/metadata/instance/compute/resourceGroupName", 176 "content-type": "text/plain", 177 "body": "myrg" 178 }, 179 { 180 "uri": "/metadata/instance/compute/vmId", 181 "content-type": "text/plain", 182 "body": "13f56399-bd52-4150-9748-7190aae1ff21" 183 }, 184 { 185 "uri": "/metadata/instance/compute/vmScaleSetName", 186 "content-type": "text/plain", 187 "body": "nomad-clients" 188 }, 189 { 190 "uri": "/metadata/instance/compute/vmSize", 191 "content-type": "text/plain", 192 "body": "Standard_A1_v2" 193 }, 194 { 195 "uri": "/metadata/instance/compute/tagsList", 196 "content-type": "application/json", 197 "body": "[{ \"name\":\"Environment\", \"value\":\"Test\"}, { \"name\":\"abc\", \"value\":\"def\"}, { \"name\":\"unique.foo\", \"value\":\"true\"}]" 198 }, 199 { 200 "uri": "/metadata/instance/network/interface/0/ipv4/ipAddress/0/privateIpAddress", 201 "content-type": "text/plain", 202 "body": "10.1.0.4" 203 }, 204 { 205 "uri": "/metadata/instance/network/interface/0/macAddress", 206 "content-type": "text/plain", 207 "body": "000D3AF806EC" 208 } 209 ] 210 } 211 ` 212 213 func TestFingerprint_AzureWithExternalIp(t *testing.T) { 214 testFingerprint_Azure(t, true) 215 } 216 217 func TestFingerprint_AzureWithoutExternalIp(t *testing.T) { 218 testFingerprint_Azure(t, false) 219 }