2015-10-05 05:37:45 +00:00
|
|
|
package fingerprint
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"os"
|
2018-04-10 14:36:46 +00:00
|
|
|
"strings"
|
2015-10-05 05:37:45 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/client/config"
|
2018-06-13 22:33:25 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
2015-10-05 05:37:45 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGCEFingerprint_nonGCE(t *testing.T) {
|
2015-10-07 02:16:28 +00:00
|
|
|
os.Setenv("GCE_ENV_URL", "http://127.0.0.1/computeMetadata/v1/instance/")
|
2018-09-16 00:48:59 +00:00
|
|
|
f := NewEnvGCEFingerprint(testlog.HCLogger(t))
|
2015-10-05 05:37:45 +00:00
|
|
|
node := &structs.Node{
|
|
|
|
Attributes: make(map[string]string),
|
|
|
|
}
|
|
|
|
|
2018-12-01 16:10:39 +00:00
|
|
|
request := &FingerprintRequest{Config: &config.Config{}, Node: node}
|
|
|
|
var response FingerprintResponse
|
2018-01-26 16:21:07 +00:00
|
|
|
err := f.Fingerprint(request, &response)
|
2015-10-05 05:37:45 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-01-31 22:03:55 +00:00
|
|
|
if response.Detected {
|
2018-01-30 17:57:37 +00:00
|
|
|
t.Fatalf("expected response to not be applicable")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(response.Attributes) > 0 {
|
2018-01-24 14:09:53 +00:00
|
|
|
t.Fatalf("Should have zero attributes without test server")
|
2015-10-05 05:37:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testFingerprint_GCE(t *testing.T, withExternalIp bool) {
|
|
|
|
node := &structs.Node{
|
|
|
|
Attributes: make(map[string]string),
|
|
|
|
}
|
|
|
|
|
|
|
|
// configure mock server with fixture routes, data
|
|
|
|
routes := routes{}
|
|
|
|
if err := json.Unmarshal([]byte(GCE_routes), &routes); err != nil {
|
|
|
|
t.Fatalf("Failed to unmarshal JSON in GCE ENV test: %s", err)
|
|
|
|
}
|
2015-10-07 18:35:00 +00:00
|
|
|
networkEndpoint := &endpoint{
|
|
|
|
Uri: "/computeMetadata/v1/instance/network-interfaces/?recursive=true",
|
|
|
|
ContentType: "application/json",
|
|
|
|
}
|
2015-10-05 05:37:45 +00:00
|
|
|
if withExternalIp {
|
2015-10-07 18:35:00 +00:00
|
|
|
networkEndpoint.Body = `[{"accessConfigs":[{"externalIp":"104.44.55.66","type":"ONE_TO_ONE_NAT"},{"externalIp":"104.44.55.67","type":"ONE_TO_ONE_NAT"}],"forwardedIps":[],"ip":"10.240.0.5","network":"projects/555555/networks/default"}]`
|
|
|
|
} else {
|
|
|
|
networkEndpoint.Body = `[{"accessConfigs":[],"forwardedIps":[],"ip":"10.240.0.5","network":"projects/555555/networks/default"}]`
|
2015-10-05 05:37:45 +00:00
|
|
|
}
|
2015-10-07 18:35:00 +00:00
|
|
|
routes.Endpoints = append(routes.Endpoints, networkEndpoint)
|
2015-10-05 05:37:45 +00:00
|
|
|
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
value, ok := r.Header["Metadata-Flavor"]
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("Metadata-Flavor not present in HTTP request header")
|
|
|
|
}
|
|
|
|
if value[0] != "Google" {
|
|
|
|
t.Fatalf("Expected Metadata-Flavor Google, saw %s", value[0])
|
|
|
|
}
|
|
|
|
|
2018-04-10 14:36:46 +00:00
|
|
|
uavalue, ok := r.Header["User-Agent"]
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("User-Agent not present in HTTP request header")
|
|
|
|
}
|
|
|
|
if !strings.Contains(uavalue[0], "Nomad/") {
|
|
|
|
t.Fatalf("Expected User-Agent to contain Nomad/, got %s", uavalue[0])
|
|
|
|
}
|
|
|
|
|
2015-10-05 05:37:45 +00:00
|
|
|
found := false
|
|
|
|
for _, e := range routes.Endpoints {
|
|
|
|
if r.RequestURI == e.Uri {
|
|
|
|
w.Header().Set("Content-Type", e.ContentType)
|
|
|
|
fmt.Fprintln(w, e.Body)
|
|
|
|
}
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
w.WriteHeader(404)
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
os.Setenv("GCE_ENV_URL", ts.URL+"/computeMetadata/v1/instance/")
|
2018-09-16 00:48:59 +00:00
|
|
|
f := NewEnvGCEFingerprint(testlog.HCLogger(t))
|
2015-10-05 05:37:45 +00:00
|
|
|
|
2018-12-01 16:10:39 +00:00
|
|
|
request := &FingerprintRequest{Config: &config.Config{}, Node: node}
|
|
|
|
var response FingerprintResponse
|
2018-01-26 16:21:07 +00:00
|
|
|
err := f.Fingerprint(request, &response)
|
2018-01-24 14:09:53 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
2015-10-05 05:37:45 +00:00
|
|
|
}
|
|
|
|
|
2018-01-31 22:03:55 +00:00
|
|
|
if !response.Detected {
|
2018-01-30 17:57:37 +00:00
|
|
|
t.Fatalf("expected response to be applicable")
|
|
|
|
}
|
|
|
|
|
2015-10-05 05:37:45 +00:00
|
|
|
keys := []string{
|
2016-01-23 02:12:16 +00:00
|
|
|
"unique.platform.gce.id",
|
|
|
|
"unique.platform.gce.hostname",
|
2015-10-05 05:37:45 +00:00
|
|
|
"platform.gce.zone",
|
|
|
|
"platform.gce.machine-type",
|
|
|
|
"platform.gce.zone",
|
|
|
|
"platform.gce.tag.abc",
|
|
|
|
"platform.gce.tag.def",
|
2016-01-23 02:12:16 +00:00
|
|
|
"unique.platform.gce.tag.foo",
|
2015-10-05 05:37:45 +00:00
|
|
|
"platform.gce.attr.ghi",
|
|
|
|
"platform.gce.attr.jkl",
|
2016-01-23 02:12:16 +00:00
|
|
|
"unique.platform.gce.attr.bar",
|
2015-10-05 05:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, k := range keys {
|
2018-01-30 17:57:37 +00:00
|
|
|
assertNodeAttributeContains(t, response.Attributes, k)
|
2015-10-05 05:37:45 +00:00
|
|
|
}
|
|
|
|
|
2018-01-30 17:57:37 +00:00
|
|
|
if len(response.Links) == 0 {
|
2015-10-05 05:37:45 +00:00
|
|
|
t.Fatalf("Empty links for Node in GCE Fingerprint test")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure Links contains the GCE ID.
|
|
|
|
for _, k := range []string{"gce"} {
|
2018-01-30 17:57:37 +00:00
|
|
|
assertNodeLinksContains(t, response.Links, k)
|
2015-10-05 05:37:45 +00:00
|
|
|
}
|
|
|
|
|
2018-01-30 17:57:37 +00:00
|
|
|
assertNodeAttributeEquals(t, response.Attributes, "unique.platform.gce.id", "12345")
|
|
|
|
assertNodeAttributeEquals(t, response.Attributes, "unique.platform.gce.hostname", "instance-1.c.project.internal")
|
|
|
|
assertNodeAttributeEquals(t, response.Attributes, "platform.gce.zone", "us-central1-f")
|
|
|
|
assertNodeAttributeEquals(t, response.Attributes, "platform.gce.machine-type", "n1-standard-1")
|
|
|
|
assertNodeAttributeEquals(t, response.Attributes, "platform.gce.network.default", "true")
|
|
|
|
assertNodeAttributeEquals(t, response.Attributes, "unique.platform.gce.network.default.ip", "10.240.0.5")
|
2015-10-05 05:37:45 +00:00
|
|
|
if withExternalIp {
|
2018-01-30 17:57:37 +00:00
|
|
|
assertNodeAttributeEquals(t, response.Attributes, "unique.platform.gce.network.default.external-ip.0", "104.44.55.66")
|
|
|
|
assertNodeAttributeEquals(t, response.Attributes, "unique.platform.gce.network.default.external-ip.1", "104.44.55.67")
|
|
|
|
} else if _, ok := response.Attributes["unique.platform.gce.network.default.external-ip.0"]; ok {
|
2016-01-23 02:12:16 +00:00
|
|
|
t.Fatal("unique.platform.gce.network.default.external-ip is set without an external IP")
|
2015-10-05 05:37:45 +00:00
|
|
|
}
|
|
|
|
|
2018-01-30 17:57:37 +00:00
|
|
|
assertNodeAttributeEquals(t, response.Attributes, "platform.gce.scheduling.automatic-restart", "TRUE")
|
|
|
|
assertNodeAttributeEquals(t, response.Attributes, "platform.gce.scheduling.on-host-maintenance", "MIGRATE")
|
|
|
|
assertNodeAttributeEquals(t, response.Attributes, "platform.gce.cpu-platform", "Intel Ivy Bridge")
|
|
|
|
assertNodeAttributeEquals(t, response.Attributes, "platform.gce.tag.abc", "true")
|
|
|
|
assertNodeAttributeEquals(t, response.Attributes, "platform.gce.tag.def", "true")
|
|
|
|
assertNodeAttributeEquals(t, response.Attributes, "unique.platform.gce.tag.foo", "true")
|
|
|
|
assertNodeAttributeEquals(t, response.Attributes, "platform.gce.attr.ghi", "111")
|
|
|
|
assertNodeAttributeEquals(t, response.Attributes, "platform.gce.attr.jkl", "222")
|
|
|
|
assertNodeAttributeEquals(t, response.Attributes, "unique.platform.gce.attr.bar", "333")
|
2015-10-05 05:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const GCE_routes = `
|
|
|
|
{
|
|
|
|
"endpoints": [
|
|
|
|
{
|
|
|
|
"uri": "/computeMetadata/v1/instance/id",
|
|
|
|
"content-type": "text/plain",
|
|
|
|
"body": "12345"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"uri": "/computeMetadata/v1/instance/hostname",
|
|
|
|
"content-type": "text/plain",
|
|
|
|
"body": "instance-1.c.project.internal"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"uri": "/computeMetadata/v1/instance/zone",
|
|
|
|
"content-type": "text/plain",
|
|
|
|
"body": "projects/555555/zones/us-central1-f"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"uri": "/computeMetadata/v1/instance/machine-type",
|
|
|
|
"content-type": "text/plain",
|
|
|
|
"body": "projects/555555/machineTypes/n1-standard-1"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"uri": "/computeMetadata/v1/instance/tags",
|
|
|
|
"content-type": "application/json",
|
2016-01-23 02:12:16 +00:00
|
|
|
"body": "[\"abc\", \"def\", \"unique.foo\"]"
|
2015-10-05 05:37:45 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"uri": "/computeMetadata/v1/instance/attributes/?recursive=true",
|
|
|
|
"content-type": "application/json",
|
2016-01-23 02:12:16 +00:00
|
|
|
"body": "{\"ghi\":\"111\",\"jkl\":\"222\",\"unique.bar\":\"333\"}"
|
2015-10-09 14:10:40 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"uri": "/computeMetadata/v1/instance/scheduling/automatic-restart",
|
|
|
|
"content-type": "text/plain",
|
|
|
|
"body": "TRUE"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"uri": "/computeMetadata/v1/instance/scheduling/on-host-maintenance",
|
|
|
|
"content-type": "text/plain",
|
|
|
|
"body": "MIGRATE"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"uri": "/computeMetadata/v1/instance/cpu-platform",
|
|
|
|
"content-type": "text/plain",
|
|
|
|
"body": "Intel Ivy Bridge"
|
2015-10-05 05:37:45 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
func TestFingerprint_GCEWithExternalIp(t *testing.T) {
|
|
|
|
testFingerprint_GCE(t, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFingerprint_GCEWithoutExternalIp(t *testing.T) {
|
|
|
|
testFingerprint_GCE(t, false)
|
|
|
|
}
|