open-nomad/client/fingerprint/env_aws_test.go

353 lines
9.6 KiB
Go
Raw Normal View History

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"
"github.com/stretchr/testify/require"
2015-08-28 16:31:20 +00:00
)
func TestEnvAWSFingerprint_nonAws(t *testing.T) {
f := NewEnvAWSFingerprint(testlog.HCLogger(t))
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),
}
request := &FingerprintRequest{Config: &config.Config{}, Node: node}
var response FingerprintResponse
err := f.Fingerprint(request, &response)
require.NoError(t, err)
require.Empty(t, response.Attributes)
2015-08-28 16:31:20 +00:00
}
func TestEnvAWSFingerprint_aws(t *testing.T) {
endpoint, cleanup := startFakeEC2Metadata(t, awsStubs)
defer cleanup()
f := NewEnvAWSFingerprint(testlog.HCLogger(t))
f.(*EnvAWSFingerprint).endpoint = endpoint
2015-08-28 16:31:20 +00:00
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)
2015-08-28 16:31:20 +00:00
keys := []string{
"platform.aws.ami-id",
2016-01-23 02:12:16 +00:00
"unique.platform.aws.hostname",
"unique.platform.aws.instance-id",
"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",
"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 {
assertNodeAttributeContains(t, response.Attributes, k)
2015-08-28 16:31:20 +00:00
}
require.NotEmpty(t, response.Links)
// confirm we have at least instance-id and ami-id
for _, k := range []string{"aws.ec2"} {
assertNodeLinksContains(t, response.Links, k)
}
2015-08-28 16:31:20 +00:00
}
2015-09-22 21:56:04 +00:00
func TestNetworkFingerprint_AWS(t *testing.T) {
endpoint, cleanup := startFakeEC2Metadata(t, awsStubs)
defer cleanup()
2015-09-22 21:56:04 +00:00
f := NewEnvAWSFingerprint(testlog.HCLogger(t))
f.(*EnvAWSFingerprint).endpoint = endpoint
2015-09-22 21:56:04 +00:00
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)
2015-09-22 21:56:04 +00:00
assertNodeAttributeContains(t, response.Attributes, "unique.network.ip-address")
2015-09-23 04:22:23 +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]
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
}
func TestNetworkFingerprint_AWS_network(t *testing.T) {
endpoint, cleanup := startFakeEC2Metadata(t, awsStubs)
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.True(t, response.Detected, "expected response to be applicable")
assertNodeAttributeContains(t, response.Attributes, "unique.network.ip-address")
require.NotNil(t, response.NodeResources)
require.Len(t, response.NodeResources.Networks, 1)
// Test at least the first Network Resource
2018-10-03 16:47:18 +00:00
net := response.NodeResources.Networks[0]
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)
}
// Try again this time setting a network speed in the config
{
node := &structs.Node{
Attributes: make(map[string]string),
}
cfg := &config.Config{
NetworkSpeed: 10,
}
request := &FingerprintRequest{Config: cfg, Node: node}
var response FingerprintResponse
err := f.Fingerprint(request, &response)
require.NoError(t, err)
assertNodeAttributeContains(t, response.Attributes, "unique.network.ip-address")
require.NotNil(t, response.NodeResources)
require.Len(t, response.NodeResources.Networks, 1)
// Test at least the first Network Resource
2018-10-03 16:47:18 +00:00
net := response.NodeResources.Networks[0]
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)
}
}
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
node := &structs.Node{
Attributes: make(map[string]string),
2015-09-22 21:56:04 +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)
}
func TestNetworkFingerprint_AWS_IncompleteImitation(t *testing.T) {
endpoint, cleanup := startFakeEC2Metadata(t, incompleteAWSImitationStubs)
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()) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
for _, e := range endpoints {
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"`
}
type endpoint struct {
Uri string `json:"uri"`
ContentType string `json:"content-type"`
Body string `json:"body"`
}
// 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
}
// 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",
},
}
// incompleteAWSImitationsStub mimics environments where some AWS endpoints
// return empty, namely Hetzner
var incompleteAWSImitationStubs = []endpoint{
{
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",
},
}