2015-08-28 16:31:20 +00:00
|
|
|
package fingerprint
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/client/config"
|
2018-06-13 22:33:25 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
2015-08-28 16:31:20 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2019-11-26 15:26:25 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2015-08-28 16:31:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestEnvAWSFingerprint_nonAws(t *testing.T) {
|
2018-09-16 00:48:59 +00:00
|
|
|
f := NewEnvAWSFingerprint(testlog.HCLogger(t))
|
2019-11-26 15:26:25 +00:00
|
|
|
f.(*EnvAWSFingerprint).endpoint = "http://127.0.0.1/latest"
|
|
|
|
|
2015-08-28 16:31:20 +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)
|
2019-11-26 15:26:25 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Empty(t, response.Attributes)
|
2015-08-28 16:31:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnvAWSFingerprint_aws(t *testing.T) {
|
2020-03-26 15:13:21 +00:00
|
|
|
endpoint, cleanup := startFakeEC2Metadata(t, awsStubs)
|
2019-11-26 15:26:25 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
2018-09-16 00:48:59 +00:00
|
|
|
f := NewEnvAWSFingerprint(testlog.HCLogger(t))
|
2019-11-26 15:26:25 +00:00
|
|
|
f.(*EnvAWSFingerprint).endpoint = endpoint
|
|
|
|
|
2015-08-28 16:31:20 +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)
|
2019-11-26 15:26:25 +00:00
|
|
|
require.NoError(t, err)
|
2015-08-28 16:31:20 +00:00
|
|
|
|
|
|
|
keys := []string{
|
2017-08-11 16:54:27 +00:00
|
|
|
"platform.aws.ami-id",
|
2016-01-23 02:12:16 +00:00
|
|
|
"unique.platform.aws.hostname",
|
|
|
|
"unique.platform.aws.instance-id",
|
2015-08-31 19:18:40 +00:00
|
|
|
"platform.aws.instance-type",
|
2016-01-23 02:12:16 +00:00
|
|
|
"unique.platform.aws.local-hostname",
|
|
|
|
"unique.platform.aws.local-ipv4",
|
|
|
|
"unique.platform.aws.public-hostname",
|
|
|
|
"unique.platform.aws.public-ipv4",
|
2015-08-31 19:18:40 +00:00
|
|
|
"platform.aws.placement.availability-zone",
|
2016-01-23 02:12:16 +00:00
|
|
|
"unique.network.ip-address",
|
2015-08-28 16:31:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, k := range keys {
|
2018-01-30 17:57:37 +00:00
|
|
|
assertNodeAttributeContains(t, response.Attributes, k)
|
2015-08-28 16:31:20 +00:00
|
|
|
}
|
2015-08-31 19:18:40 +00:00
|
|
|
|
2019-11-26 15:26:25 +00:00
|
|
|
require.NotEmpty(t, response.Links)
|
2015-08-31 19:18:40 +00:00
|
|
|
|
|
|
|
// confirm we have at least instance-id and ami-id
|
2015-08-31 20:02:31 +00:00
|
|
|
for _, k := range []string{"aws.ec2"} {
|
2018-01-30 17:57:37 +00:00
|
|
|
assertNodeLinksContains(t, response.Links, k)
|
2015-08-31 19:18:40 +00:00
|
|
|
}
|
2015-08-28 16:31:20 +00:00
|
|
|
}
|
|
|
|
|
2015-09-22 21:56:04 +00:00
|
|
|
func TestNetworkFingerprint_AWS(t *testing.T) {
|
2020-03-26 15:13:21 +00:00
|
|
|
endpoint, cleanup := startFakeEC2Metadata(t, awsStubs)
|
2019-11-26 15:26:25 +00:00
|
|
|
defer cleanup()
|
2015-09-22 21:56:04 +00:00
|
|
|
|
2018-09-16 00:48:59 +00:00
|
|
|
f := NewEnvAWSFingerprint(testlog.HCLogger(t))
|
2019-11-26 15:26:25 +00:00
|
|
|
f.(*EnvAWSFingerprint).endpoint = endpoint
|
|
|
|
|
2015-09-22 21:56:04 +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)
|
2019-11-26 15:26:25 +00:00
|
|
|
require.NoError(t, err)
|
2015-09-22 21:56:04 +00:00
|
|
|
|
2018-01-30 17:57:37 +00:00
|
|
|
assertNodeAttributeContains(t, response.Attributes, "unique.network.ip-address")
|
2015-09-23 04:22:23 +00:00
|
|
|
|
2019-11-26 15:26:25 +00:00
|
|
|
require.NotNil(t, response.NodeResources)
|
|
|
|
require.Len(t, response.NodeResources.Networks, 1)
|
2015-09-23 04:22:23 +00:00
|
|
|
|
|
|
|
// Test at least the first Network Resource
|
2018-10-03 16:47:18 +00:00
|
|
|
net := response.NodeResources.Networks[0]
|
2019-11-26 15:26:25 +00:00
|
|
|
require.NotEmpty(t, net.IP, "Expected Network Resource to have an IP")
|
|
|
|
require.NotEmpty(t, net.CIDR, "Expected Network Resource to have a CIDR")
|
|
|
|
require.NotEmpty(t, net.Device, "Expected Network Resource to have a Device Name")
|
2015-09-22 21:56:04 +00:00
|
|
|
}
|
|
|
|
|
2016-11-15 21:55:51 +00:00
|
|
|
func TestNetworkFingerprint_AWS_network(t *testing.T) {
|
2020-03-26 15:13:21 +00:00
|
|
|
endpoint, cleanup := startFakeEC2Metadata(t, awsStubs)
|
2019-11-26 15:26:25 +00:00
|
|
|
defer cleanup()
|
2016-11-15 21:55:51 +00:00
|
|
|
|
2018-09-16 00:48:59 +00:00
|
|
|
f := NewEnvAWSFingerprint(testlog.HCLogger(t))
|
2019-11-26 15:26:25 +00:00
|
|
|
f.(*EnvAWSFingerprint).endpoint = endpoint
|
|
|
|
|
2018-01-24 14:09:53 +00:00
|
|
|
{
|
|
|
|
node := &structs.Node{
|
|
|
|
Attributes: make(map[string]string),
|
|
|
|
}
|
2016-11-15 21:55:51 +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)
|
2019-11-26 15:26:25 +00:00
|
|
|
require.NoError(t, err)
|
2016-11-15 21:55:51 +00:00
|
|
|
|
2019-11-26 15:26:25 +00:00
|
|
|
require.True(t, response.Detected, "expected response to be applicable")
|
2018-01-30 17:57:37 +00:00
|
|
|
|
|
|
|
assertNodeAttributeContains(t, response.Attributes, "unique.network.ip-address")
|
2016-11-15 21:55:51 +00:00
|
|
|
|
2019-11-26 15:26:25 +00:00
|
|
|
require.NotNil(t, response.NodeResources)
|
|
|
|
require.Len(t, response.NodeResources.Networks, 1)
|
2018-01-24 14:09:53 +00:00
|
|
|
|
|
|
|
// Test at least the first Network Resource
|
2018-10-03 16:47:18 +00:00
|
|
|
net := response.NodeResources.Networks[0]
|
2019-11-26 15:26:25 +00:00
|
|
|
require.NotEmpty(t, net.IP, "Expected Network Resource to have an IP")
|
|
|
|
require.NotEmpty(t, net.CIDR, "Expected Network Resource to have a CIDR")
|
|
|
|
require.NotEmpty(t, net.Device, "Expected Network Resource to have a Device Name")
|
|
|
|
require.Equal(t, 1000, net.MBits)
|
2016-11-15 21:55:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try again this time setting a network speed in the config
|
2018-01-24 14:09:53 +00:00
|
|
|
{
|
|
|
|
node := &structs.Node{
|
|
|
|
Attributes: make(map[string]string),
|
|
|
|
}
|
2016-11-15 21:55:51 +00:00
|
|
|
|
2018-01-24 14:09:53 +00:00
|
|
|
cfg := &config.Config{
|
|
|
|
NetworkSpeed: 10,
|
|
|
|
}
|
2016-11-15 21:55:51 +00:00
|
|
|
|
2018-12-01 16:10:39 +00:00
|
|
|
request := &FingerprintRequest{Config: cfg, Node: node}
|
|
|
|
var response FingerprintResponse
|
2018-01-26 16:21:07 +00:00
|
|
|
err := f.Fingerprint(request, &response)
|
2019-11-26 15:26:25 +00:00
|
|
|
require.NoError(t, err)
|
2016-11-15 21:55:51 +00:00
|
|
|
|
2018-01-30 17:57:37 +00:00
|
|
|
assertNodeAttributeContains(t, response.Attributes, "unique.network.ip-address")
|
2016-11-15 21:55:51 +00:00
|
|
|
|
2019-11-26 15:26:25 +00:00
|
|
|
require.NotNil(t, response.NodeResources)
|
|
|
|
require.Len(t, response.NodeResources.Networks, 1)
|
2018-01-24 14:09:53 +00:00
|
|
|
|
|
|
|
// Test at least the first Network Resource
|
2018-10-03 16:47:18 +00:00
|
|
|
net := response.NodeResources.Networks[0]
|
2019-11-26 15:26:25 +00:00
|
|
|
require.NotEmpty(t, net.IP, "Expected Network Resource to have an IP")
|
|
|
|
require.NotEmpty(t, net.CIDR, "Expected Network Resource to have a CIDR")
|
|
|
|
require.NotEmpty(t, net.Device, "Expected Network Resource to have a Device Name")
|
|
|
|
require.Equal(t, 10, net.MBits)
|
2016-11-15 21:55:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 15:13:21 +00:00
|
|
|
func TestNetworkFingerprint_AWS_NoNetwork(t *testing.T) {
|
|
|
|
endpoint, cleanup := startFakeEC2Metadata(t, noNetworkAWSStubs)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
f := NewEnvAWSFingerprint(testlog.HCLogger(t))
|
|
|
|
f.(*EnvAWSFingerprint).endpoint = endpoint
|
2015-09-22 21:56:04 +00:00
|
|
|
|
2020-03-26 15:13:21 +00:00
|
|
|
node := &structs.Node{
|
|
|
|
Attributes: make(map[string]string),
|
2015-09-22 21:56:04 +00:00
|
|
|
}
|
2018-01-24 14:09:53 +00:00
|
|
|
|
2020-03-26 15:13:21 +00:00
|
|
|
request := &FingerprintRequest{Config: &config.Config{}, Node: node}
|
|
|
|
var response FingerprintResponse
|
|
|
|
err := f.Fingerprint(request, &response)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.True(t, response.Detected, "expected response to be applicable")
|
|
|
|
|
|
|
|
require.Equal(t, "ami-1234", response.Attributes["platform.aws.ami-id"])
|
|
|
|
|
|
|
|
require.Nil(t, response.NodeResources)
|
|
|
|
}
|
|
|
|
|
2020-03-26 15:33:44 +00:00
|
|
|
func TestNetworkFingerprint_AWS_IncompleteImitation(t *testing.T) {
|
|
|
|
endpoint, cleanup := startFakeEC2Metadata(t, incompleteAWSImitationStubs)
|
2020-03-26 15:13:21 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
f := NewEnvAWSFingerprint(testlog.HCLogger(t))
|
|
|
|
f.(*EnvAWSFingerprint).endpoint = endpoint
|
|
|
|
|
|
|
|
node := &structs.Node{
|
|
|
|
Attributes: make(map[string]string),
|
|
|
|
}
|
|
|
|
|
|
|
|
request := &FingerprintRequest{Config: &config.Config{}, Node: node}
|
|
|
|
var response FingerprintResponse
|
|
|
|
err := f.Fingerprint(request, &response)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.False(t, response.Detected, "expected response not to be applicable")
|
|
|
|
|
|
|
|
require.NotContains(t, response.Attributes, "platform.aws.ami-id")
|
|
|
|
require.Nil(t, response.NodeResources)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Utility functions for tests
|
|
|
|
|
|
|
|
func startFakeEC2Metadata(t *testing.T, endpoints []endpoint) (endpoint string, cleanup func()) {
|
2019-11-26 15:26:25 +00:00
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2020-03-26 15:13:21 +00:00
|
|
|
for _, e := range endpoints {
|
2019-11-26 15:26:25 +00:00
|
|
|
if r.RequestURI == e.Uri {
|
|
|
|
w.Header().Set("Content-Type", e.ContentType)
|
|
|
|
fmt.Fprintln(w, e.Body)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
|
|
|
return ts.URL + "/latest", ts.Close
|
|
|
|
}
|
|
|
|
|
|
|
|
type routes struct {
|
|
|
|
Endpoints []*endpoint `json:"endpoints"`
|
|
|
|
}
|
2020-03-26 15:13:21 +00:00
|
|
|
|
2019-11-26 15:26:25 +00:00
|
|
|
type endpoint struct {
|
2020-03-26 15:33:44 +00:00
|
|
|
Uri string `json:"uri"`
|
|
|
|
ContentType string `json:"content-type"`
|
|
|
|
Body string `json:"body"`
|
2020-03-26 15:13:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// awsStubs mimics normal EC2 instance metadata
|
|
|
|
var awsStubs = []endpoint{
|
|
|
|
{
|
|
|
|
Uri: "/latest/meta-data/ami-id",
|
|
|
|
ContentType: "text/plain",
|
|
|
|
Body: "ami-1234",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Uri: "/latest/meta-data/hostname",
|
|
|
|
ContentType: "text/plain",
|
|
|
|
Body: "ip-10-0-0-207.us-west-2.compute.internal",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Uri: "/latest/meta-data/placement/availability-zone",
|
|
|
|
ContentType: "text/plain",
|
|
|
|
Body: "us-west-2a",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Uri: "/latest/meta-data/instance-id",
|
|
|
|
ContentType: "text/plain",
|
|
|
|
Body: "i-b3ba3875",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Uri: "/latest/meta-data/instance-type",
|
|
|
|
ContentType: "text/plain",
|
|
|
|
Body: "m3.2xlarge",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Uri: "/latest/meta-data/local-hostname",
|
|
|
|
ContentType: "text/plain",
|
|
|
|
Body: "ip-10-0-0-207.us-west-2.compute.internal",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Uri: "/latest/meta-data/local-ipv4",
|
|
|
|
ContentType: "text/plain",
|
|
|
|
Body: "10.0.0.207",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Uri: "/latest/meta-data/public-hostname",
|
|
|
|
ContentType: "text/plain",
|
|
|
|
Body: "ec2-54-191-117-175.us-west-2.compute.amazonaws.com",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Uri: "/latest/meta-data/public-ipv4",
|
|
|
|
ContentType: "text/plain",
|
|
|
|
Body: "54.191.117.175",
|
|
|
|
},
|
2015-09-22 21:56:04 +00:00
|
|
|
}
|
2019-11-26 15:26:25 +00:00
|
|
|
|
2020-03-26 15:13:21 +00:00
|
|
|
// noNetworkAWSStubs mimics an EC2 instance but without local ip address
|
|
|
|
// may happen in environments with odd EC2 Metadata emulation
|
|
|
|
var noNetworkAWSStubs = []endpoint{
|
|
|
|
{
|
|
|
|
Uri: "/latest/meta-data/ami-id",
|
|
|
|
ContentType: "text/plain",
|
|
|
|
Body: "ami-1234",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Uri: "/latest/meta-data/hostname",
|
|
|
|
ContentType: "text/plain",
|
|
|
|
Body: "ip-10-0-0-207.us-west-2.compute.internal",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Uri: "/latest/meta-data/placement/availability-zone",
|
|
|
|
ContentType: "text/plain",
|
|
|
|
Body: "us-west-2a",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Uri: "/latest/meta-data/instance-id",
|
|
|
|
ContentType: "text/plain",
|
|
|
|
Body: "i-b3ba3875",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Uri: "/latest/meta-data/instance-type",
|
|
|
|
ContentType: "text/plain",
|
|
|
|
Body: "m3.2xlarge",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Uri: "/latest/meta-data/local-hostname",
|
|
|
|
ContentType: "text/plain",
|
|
|
|
Body: "ip-10-0-0-207.us-west-2.compute.internal",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Uri: "/latest/meta-data/local-ipv4",
|
|
|
|
ContentType: "text/plain",
|
|
|
|
Body: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Uri: "/latest/meta-data/public-hostname",
|
|
|
|
ContentType: "text/plain",
|
|
|
|
Body: "ec2-54-191-117-175.us-west-2.compute.amazonaws.com",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Uri: "/latest/meta-data/public-ipv4",
|
|
|
|
ContentType: "text/plain",
|
|
|
|
Body: "54.191.117.175",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-03-26 15:33:44 +00:00
|
|
|
// incompleteAWSImitationsStub mimics environments where some AWS endpoints
|
2020-03-26 15:13:21 +00:00
|
|
|
// return empty, namely Hetzner
|
2020-03-26 15:33:44 +00:00
|
|
|
var incompleteAWSImitationStubs = []endpoint{
|
2020-03-26 15:13:21 +00:00
|
|
|
{
|
|
|
|
Uri: "/latest/meta-data/hostname",
|
|
|
|
ContentType: "text/plain",
|
|
|
|
Body: "ip-10-0-0-207.us-west-2.compute.internal",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Uri: "/latest/meta-data/instance-id",
|
|
|
|
ContentType: "text/plain",
|
|
|
|
Body: "i-b3ba3875",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Uri: "/latest/meta-data/local-ipv4",
|
|
|
|
ContentType: "text/plain",
|
|
|
|
Body: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Uri: "/latest/meta-data/public-ipv4",
|
|
|
|
ContentType: "text/plain",
|
|
|
|
Body: "54.191.117.175",
|
|
|
|
},
|
2019-11-26 15:26:25 +00:00
|
|
|
}
|