2015-09-18 21:56:27 +00:00
|
|
|
package fingerprint
|
|
|
|
|
|
|
|
import (
|
2015-10-29 18:01:15 +00:00
|
|
|
"fmt"
|
2015-09-23 03:56:31 +00:00
|
|
|
"net"
|
2016-07-11 03:16:06 +00:00
|
|
|
"os"
|
2015-09-18 21:56:27 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/client/config"
|
2018-01-24 14:09:53 +00:00
|
|
|
cstructs "github.com/hashicorp/nomad/client/structs"
|
2018-06-13 22:33:25 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
2015-09-18 21:56:27 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
2016-07-11 03:16:06 +00:00
|
|
|
// Set skipOnlineTestEnvVar to a non-empty value to skip network tests. Useful
|
|
|
|
// when working offline (e.g. an airplane).
|
|
|
|
const skipOnlineTestsEnvVar = "TEST_NOMAD_SKIP_ONLINE_NET"
|
|
|
|
|
2015-10-29 18:01:15 +00:00
|
|
|
var (
|
|
|
|
lo = net.Interface{
|
|
|
|
Index: 2,
|
|
|
|
MTU: 65536,
|
|
|
|
Name: "lo",
|
|
|
|
HardwareAddr: []byte{23, 43, 54, 54},
|
|
|
|
Flags: net.FlagUp | net.FlagLoopback,
|
|
|
|
}
|
|
|
|
|
2015-10-29 22:14:13 +00:00
|
|
|
eth0 = net.Interface{
|
|
|
|
Index: 3,
|
|
|
|
MTU: 1500,
|
|
|
|
Name: "eth0",
|
|
|
|
HardwareAddr: []byte{23, 44, 54, 67},
|
|
|
|
Flags: net.FlagUp | net.FlagMulticast | net.FlagBroadcast,
|
|
|
|
}
|
2015-10-29 18:01:15 +00:00
|
|
|
|
2015-10-29 22:14:13 +00:00
|
|
|
eth1 = net.Interface{
|
|
|
|
Index: 4,
|
|
|
|
MTU: 1500,
|
|
|
|
Name: "eth1",
|
|
|
|
HardwareAddr: []byte{23, 44, 54, 69},
|
|
|
|
Flags: net.FlagMulticast | net.FlagBroadcast,
|
|
|
|
}
|
2015-10-29 18:01:15 +00:00
|
|
|
|
2015-10-29 22:14:13 +00:00
|
|
|
eth2 = net.Interface{
|
|
|
|
Index: 4,
|
|
|
|
MTU: 1500,
|
|
|
|
Name: "eth2",
|
|
|
|
HardwareAddr: []byte{23, 44, 54, 70},
|
|
|
|
Flags: net.FlagUp | net.FlagBroadcast | net.FlagMulticast,
|
|
|
|
}
|
2017-08-23 22:32:22 +00:00
|
|
|
|
|
|
|
// One link local address
|
|
|
|
eth3 = net.Interface{
|
|
|
|
Index: 4,
|
|
|
|
MTU: 1500,
|
|
|
|
Name: "eth3",
|
|
|
|
HardwareAddr: []byte{23, 44, 54, 71},
|
|
|
|
Flags: net.FlagUp | net.FlagBroadcast | net.FlagMulticast,
|
|
|
|
}
|
|
|
|
|
|
|
|
// One link local address and one globally routable address
|
|
|
|
eth4 = net.Interface{
|
|
|
|
Index: 4,
|
|
|
|
MTU: 1500,
|
|
|
|
Name: "eth4",
|
|
|
|
HardwareAddr: []byte{23, 44, 54, 72},
|
|
|
|
Flags: net.FlagUp | net.FlagBroadcast | net.FlagMulticast,
|
|
|
|
}
|
2015-10-29 22:14:13 +00:00
|
|
|
)
|
2015-10-29 18:01:15 +00:00
|
|
|
|
|
|
|
// A fake network detector which returns no devices
|
2018-03-11 18:15:37 +00:00
|
|
|
type NetworkInterfaceDetectorNoDevices struct {
|
2015-10-29 18:01:15 +00:00
|
|
|
}
|
|
|
|
|
2018-03-11 18:15:37 +00:00
|
|
|
func (f *NetworkInterfaceDetectorNoDevices) Interfaces() ([]net.Interface, error) {
|
2015-10-29 18:01:15 +00:00
|
|
|
return make([]net.Interface, 0), nil
|
|
|
|
}
|
|
|
|
|
2018-03-11 18:15:37 +00:00
|
|
|
func (f *NetworkInterfaceDetectorNoDevices) InterfaceByName(name string) (*net.Interface, error) {
|
2015-10-29 18:01:15 +00:00
|
|
|
return nil, fmt.Errorf("Device with name %s doesn't exist", name)
|
|
|
|
}
|
|
|
|
|
2018-03-11 18:15:37 +00:00
|
|
|
func (f *NetworkInterfaceDetectorNoDevices) Addrs(intf *net.Interface) ([]net.Addr, error) {
|
2015-10-29 18:01:15 +00:00
|
|
|
return nil, fmt.Errorf("No interfaces found for device %v", intf.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// A fake network detector which returns only loopback
|
|
|
|
type NetworkInterfaceDetectorOnlyLo struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NetworkInterfaceDetectorOnlyLo) Interfaces() ([]net.Interface, error) {
|
|
|
|
return []net.Interface{lo}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NetworkInterfaceDetectorOnlyLo) InterfaceByName(name string) (*net.Interface, error) {
|
|
|
|
if name == "lo" {
|
|
|
|
return &lo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("No device with name %v found", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NetworkInterfaceDetectorOnlyLo) Addrs(intf *net.Interface) ([]net.Addr, error) {
|
|
|
|
if intf.Name == "lo" {
|
|
|
|
_, ipnet1, _ := net.ParseCIDR("127.0.0.1/8")
|
|
|
|
_, ipnet2, _ := net.ParseCIDR("2001:DB8::/48")
|
|
|
|
return []net.Addr{ipnet1, ipnet2}, nil
|
|
|
|
}
|
2015-10-29 22:14:13 +00:00
|
|
|
|
|
|
|
return nil, fmt.Errorf("Can't find addresses for device: %v", intf.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// A fake network detector which simulates the presence of multiple interfaces
|
|
|
|
type NetworkInterfaceDetectorMultipleInterfaces struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NetworkInterfaceDetectorMultipleInterfaces) Interfaces() ([]net.Interface, error) {
|
2017-11-13 23:10:02 +00:00
|
|
|
// Return link local first to test we don't prefer it
|
2017-11-15 19:32:32 +00:00
|
|
|
return []net.Interface{lo, eth0, eth1, eth2, eth3, eth4}, nil
|
2015-10-29 22:14:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NetworkInterfaceDetectorMultipleInterfaces) InterfaceByName(name string) (*net.Interface, error) {
|
|
|
|
var intf *net.Interface
|
|
|
|
switch name {
|
|
|
|
case "lo":
|
|
|
|
intf = &lo
|
|
|
|
case "eth0":
|
|
|
|
intf = ð0
|
|
|
|
case "eth1":
|
|
|
|
intf = ð1
|
|
|
|
case "eth2":
|
|
|
|
intf = ð2
|
2017-08-23 22:32:22 +00:00
|
|
|
case "eth3":
|
|
|
|
intf = ð3
|
|
|
|
case "eth4":
|
|
|
|
intf = ð4
|
2015-10-29 22:14:13 +00:00
|
|
|
}
|
|
|
|
if intf != nil {
|
|
|
|
return intf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("No device with name %v found", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NetworkInterfaceDetectorMultipleInterfaces) Addrs(intf *net.Interface) ([]net.Addr, error) {
|
|
|
|
if intf.Name == "lo" {
|
|
|
|
_, ipnet1, _ := net.ParseCIDR("127.0.0.1/8")
|
|
|
|
_, ipnet2, _ := net.ParseCIDR("2001:DB8::/48")
|
|
|
|
return []net.Addr{ipnet1, ipnet2}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if intf.Name == "eth0" {
|
|
|
|
_, ipnet1, _ := net.ParseCIDR("100.64.0.11/10")
|
2017-04-08 20:43:29 +00:00
|
|
|
_, ipnet2, _ := net.ParseCIDR("2001:0db8:85a3:0000:0000:8a2e:0370:7334/64")
|
|
|
|
ipAddr, _ := net.ResolveIPAddr("ip6", "fe80::140c:9579:8037:f565")
|
|
|
|
return []net.Addr{ipnet1, ipnet2, ipAddr}, nil
|
2015-10-29 22:14:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if intf.Name == "eth1" {
|
|
|
|
_, ipnet1, _ := net.ParseCIDR("100.64.0.10/10")
|
|
|
|
_, ipnet2, _ := net.ParseCIDR("2003:DB8::/48")
|
|
|
|
return []net.Addr{ipnet1, ipnet2}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if intf.Name == "eth2" {
|
|
|
|
return []net.Addr{}, nil
|
|
|
|
}
|
2017-08-23 22:32:22 +00:00
|
|
|
|
|
|
|
if intf.Name == "eth3" {
|
|
|
|
_, ipnet1, _ := net.ParseCIDR("169.254.155.20/32")
|
|
|
|
return []net.Addr{ipnet1}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if intf.Name == "eth4" {
|
|
|
|
_, ipnet1, _ := net.ParseCIDR("169.254.155.20/32")
|
|
|
|
_, ipnet2, _ := net.ParseCIDR("100.64.0.10/10")
|
|
|
|
return []net.Addr{ipnet1, ipnet2}, nil
|
|
|
|
}
|
|
|
|
|
2015-10-29 18:01:15 +00:00
|
|
|
return nil, fmt.Errorf("Can't find addresses for device: %v", intf.Name)
|
|
|
|
}
|
|
|
|
|
2015-09-18 21:56:27 +00:00
|
|
|
func TestNetworkFingerprint_basic(t *testing.T) {
|
2016-07-11 03:16:06 +00:00
|
|
|
if v := os.Getenv(skipOnlineTestsEnvVar); v != "" {
|
|
|
|
t.Skipf("Environment variable %+q not empty, skipping test", skipOnlineTestsEnvVar)
|
|
|
|
}
|
|
|
|
|
2018-09-16 00:48:59 +00:00
|
|
|
f := &NetworkFingerprint{logger: testlog.HCLogger(t), interfaceDetector: &DefaultNetworkInterfaceDetector{}}
|
2015-09-18 21:56:27 +00:00
|
|
|
node := &structs.Node{
|
|
|
|
Attributes: make(map[string]string),
|
|
|
|
}
|
2016-11-15 21:55:51 +00:00
|
|
|
cfg := &config.Config{NetworkSpeed: 101}
|
2015-09-18 21:56:27 +00:00
|
|
|
|
2018-01-24 14:09:53 +00:00
|
|
|
request := &cstructs.FingerprintRequest{Config: cfg, Node: node}
|
2018-01-26 16:21:07 +00:00
|
|
|
var response cstructs.FingerprintResponse
|
|
|
|
err := f.Fingerprint(request, &response)
|
2015-09-18 21:56:27 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2018-01-26 16:21:07 +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")
|
|
|
|
}
|
|
|
|
|
|
|
|
attributes := response.Attributes
|
2018-01-26 16:21:07 +00:00
|
|
|
if len(attributes) == 0 {
|
2016-07-11 03:16:06 +00:00
|
|
|
t.Fatalf("should apply (HINT: working offline? Set env %q=y", skipOnlineTestsEnvVar)
|
2015-09-18 21:56:27 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 16:21:07 +00:00
|
|
|
assertNodeAttributeContains(t, attributes, "unique.network.ip-address")
|
2015-09-23 03:56:31 +00:00
|
|
|
|
2018-01-26 16:21:07 +00:00
|
|
|
ip := attributes["unique.network.ip-address"]
|
2015-09-23 03:56:31 +00:00
|
|
|
match := net.ParseIP(ip)
|
|
|
|
if match == nil {
|
|
|
|
t.Fatalf("Bad IP match: %s", ip)
|
2015-09-18 21:56:27 +00:00
|
|
|
}
|
2015-09-23 04:22:23 +00:00
|
|
|
|
2018-01-30 17:57:37 +00:00
|
|
|
if response.Resources == nil || len(response.Resources.Networks) == 0 {
|
2015-09-23 04:22:23 +00:00
|
|
|
t.Fatal("Expected to find Network Resources")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test at least the first Network Resource
|
2018-01-30 17:57:37 +00:00
|
|
|
net := response.Resources.Networks[0]
|
2015-09-23 04:22:23 +00:00
|
|
|
if net.IP == "" {
|
|
|
|
t.Fatal("Expected Network Resource to not be empty")
|
|
|
|
}
|
2015-09-23 04:51:56 +00:00
|
|
|
if net.CIDR == "" {
|
|
|
|
t.Fatal("Expected Network Resource to have a CIDR")
|
|
|
|
}
|
|
|
|
if net.Device == "" {
|
|
|
|
t.Fatal("Expected Network Resource to have a Device Name")
|
|
|
|
}
|
2016-11-15 21:55:51 +00:00
|
|
|
if net.MBits != 101 {
|
2017-08-07 21:13:05 +00:00
|
|
|
t.Fatalf("Expected Network Resource to have bandwidth %d; got %d", 101, net.MBits)
|
2015-10-03 00:32:11 +00:00
|
|
|
}
|
2015-09-18 21:56:27 +00:00
|
|
|
}
|
2015-10-29 18:01:15 +00:00
|
|
|
|
|
|
|
func TestNetworkFingerprint_default_device_absent(t *testing.T) {
|
2018-09-16 00:48:59 +00:00
|
|
|
f := &NetworkFingerprint{logger: testlog.HCLogger(t), interfaceDetector: &NetworkInterfaceDetectorOnlyLo{}}
|
2015-10-29 18:01:15 +00:00
|
|
|
node := &structs.Node{
|
|
|
|
Attributes: make(map[string]string),
|
|
|
|
}
|
|
|
|
cfg := &config.Config{NetworkSpeed: 100, NetworkInterface: "eth0"}
|
|
|
|
|
2018-01-24 14:09:53 +00:00
|
|
|
request := &cstructs.FingerprintRequest{Config: cfg, Node: node}
|
2018-01-26 16:21:07 +00:00
|
|
|
var response cstructs.FingerprintResponse
|
|
|
|
err := f.Fingerprint(request, &response)
|
2015-10-29 18:01:15 +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 {
|
|
|
|
t.Fatalf("attributes should be zero but instead are: %v", response.Attributes)
|
2015-10-29 18:01:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetworkFingerPrint_default_device(t *testing.T) {
|
2018-09-16 00:48:59 +00:00
|
|
|
f := &NetworkFingerprint{logger: testlog.HCLogger(t), interfaceDetector: &NetworkInterfaceDetectorOnlyLo{}}
|
2015-10-29 18:01:15 +00:00
|
|
|
node := &structs.Node{
|
|
|
|
Attributes: make(map[string]string),
|
|
|
|
}
|
|
|
|
cfg := &config.Config{NetworkSpeed: 100, NetworkInterface: "lo"}
|
|
|
|
|
2018-01-24 14:09:53 +00:00
|
|
|
request := &cstructs.FingerprintRequest{Config: cfg, Node: node}
|
2018-01-26 16:21:07 +00:00
|
|
|
var response cstructs.FingerprintResponse
|
|
|
|
err := f.Fingerprint(request, &response)
|
2015-10-29 18:01:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2018-01-26 16:21:07 +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")
|
|
|
|
}
|
|
|
|
|
|
|
|
attributes := response.Attributes
|
2018-01-26 16:21:07 +00:00
|
|
|
if len(attributes) == 0 {
|
2015-10-29 18:01:15 +00:00
|
|
|
t.Fatalf("should apply")
|
|
|
|
}
|
|
|
|
|
2018-01-26 16:21:07 +00:00
|
|
|
assertNodeAttributeContains(t, attributes, "unique.network.ip-address")
|
2015-10-29 18:01:15 +00:00
|
|
|
|
2018-01-26 16:21:07 +00:00
|
|
|
ip := attributes["unique.network.ip-address"]
|
2015-10-29 18:01:15 +00:00
|
|
|
match := net.ParseIP(ip)
|
|
|
|
if match == nil {
|
|
|
|
t.Fatalf("Bad IP match: %s", ip)
|
|
|
|
}
|
|
|
|
|
2018-01-30 17:57:37 +00:00
|
|
|
if response.Resources == nil || len(response.Resources.Networks) == 0 {
|
2015-10-29 18:01:15 +00:00
|
|
|
t.Fatal("Expected to find Network Resources")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test at least the first Network Resource
|
2018-01-30 17:57:37 +00:00
|
|
|
net := response.Resources.Networks[0]
|
2015-10-29 18:01:15 +00:00
|
|
|
if net.IP == "" {
|
|
|
|
t.Fatal("Expected Network Resource to not be empty")
|
|
|
|
}
|
|
|
|
if net.CIDR == "" {
|
|
|
|
t.Fatal("Expected Network Resource to have a CIDR")
|
|
|
|
}
|
|
|
|
if net.Device == "" {
|
|
|
|
t.Fatal("Expected Network Resource to have a Device Name")
|
|
|
|
}
|
|
|
|
if net.MBits == 0 {
|
2017-08-07 21:13:05 +00:00
|
|
|
t.Fatal("Expected Network Resource to have a non-zero bandwidth")
|
2015-10-29 18:01:15 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-29 22:14:13 +00:00
|
|
|
|
2017-08-23 22:32:22 +00:00
|
|
|
func TestNetworkFingerPrint_LinkLocal_Allowed(t *testing.T) {
|
2018-09-16 00:48:59 +00:00
|
|
|
f := &NetworkFingerprint{logger: testlog.HCLogger(t), interfaceDetector: &NetworkInterfaceDetectorMultipleInterfaces{}}
|
2017-08-23 22:32:22 +00:00
|
|
|
node := &structs.Node{
|
|
|
|
Attributes: make(map[string]string),
|
|
|
|
}
|
|
|
|
cfg := &config.Config{NetworkSpeed: 100, NetworkInterface: "eth3"}
|
|
|
|
|
2018-01-24 14:09:53 +00:00
|
|
|
request := &cstructs.FingerprintRequest{Config: cfg, Node: node}
|
2018-01-26 16:21:07 +00:00
|
|
|
var response cstructs.FingerprintResponse
|
|
|
|
err := f.Fingerprint(request, &response)
|
2017-08-23 22:32:22 +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 be applicable")
|
|
|
|
}
|
|
|
|
|
|
|
|
attributes := response.Attributes
|
2018-01-26 16:21:07 +00:00
|
|
|
assertNodeAttributeContains(t, attributes, "unique.network.ip-address")
|
2017-08-23 22:32:22 +00:00
|
|
|
|
2018-01-26 16:21:07 +00:00
|
|
|
ip := attributes["unique.network.ip-address"]
|
2017-08-23 22:32:22 +00:00
|
|
|
match := net.ParseIP(ip)
|
|
|
|
if match == nil {
|
|
|
|
t.Fatalf("Bad IP match: %s", ip)
|
|
|
|
}
|
|
|
|
|
2018-01-30 17:57:37 +00:00
|
|
|
if response.Resources == nil || len(response.Resources.Networks) == 0 {
|
2017-08-23 22:32:22 +00:00
|
|
|
t.Fatal("Expected to find Network Resources")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test at least the first Network Resource
|
2018-01-30 17:57:37 +00:00
|
|
|
net := response.Resources.Networks[0]
|
2017-08-23 22:32:22 +00:00
|
|
|
if net.IP == "" {
|
|
|
|
t.Fatal("Expected Network Resource to not be empty")
|
|
|
|
}
|
|
|
|
if net.CIDR == "" {
|
|
|
|
t.Fatal("Expected Network Resource to have a CIDR")
|
|
|
|
}
|
|
|
|
if net.Device == "" {
|
|
|
|
t.Fatal("Expected Network Resource to have a Device Name")
|
|
|
|
}
|
|
|
|
if net.MBits == 0 {
|
|
|
|
t.Fatal("Expected Network Resource to have a non-zero bandwidth")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetworkFingerPrint_LinkLocal_Allowed_MixedIntf(t *testing.T) {
|
2018-09-16 00:48:59 +00:00
|
|
|
f := &NetworkFingerprint{logger: testlog.HCLogger(t), interfaceDetector: &NetworkInterfaceDetectorMultipleInterfaces{}}
|
2017-08-23 22:32:22 +00:00
|
|
|
node := &structs.Node{
|
|
|
|
Attributes: make(map[string]string),
|
|
|
|
}
|
|
|
|
cfg := &config.Config{NetworkSpeed: 100, NetworkInterface: "eth4"}
|
|
|
|
|
2018-01-24 14:09:53 +00:00
|
|
|
request := &cstructs.FingerprintRequest{Config: cfg, Node: node}
|
2018-01-26 16:21:07 +00:00
|
|
|
var response cstructs.FingerprintResponse
|
|
|
|
err := f.Fingerprint(request, &response)
|
2017-08-23 22:32:22 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2018-01-26 16:21:07 +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")
|
|
|
|
}
|
|
|
|
|
|
|
|
attributes := response.Attributes
|
2018-01-26 16:21:07 +00:00
|
|
|
if len(attributes) == 0 {
|
|
|
|
t.Fatalf("should apply attributes")
|
2017-08-23 22:32:22 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 16:21:07 +00:00
|
|
|
assertNodeAttributeContains(t, attributes, "unique.network.ip-address")
|
2017-08-23 22:32:22 +00:00
|
|
|
|
2018-01-26 16:21:07 +00:00
|
|
|
ip := attributes["unique.network.ip-address"]
|
2017-08-23 22:32:22 +00:00
|
|
|
match := net.ParseIP(ip)
|
|
|
|
if match == nil {
|
|
|
|
t.Fatalf("Bad IP match: %s", ip)
|
|
|
|
}
|
|
|
|
|
2018-01-30 17:57:37 +00:00
|
|
|
if response.Resources == nil || len(response.Resources.Networks) == 0 {
|
2017-08-23 22:32:22 +00:00
|
|
|
t.Fatal("Expected to find Network Resources")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test at least the first Network Resource
|
2018-01-30 17:57:37 +00:00
|
|
|
net := response.Resources.Networks[0]
|
2017-08-23 22:32:22 +00:00
|
|
|
if net.IP == "" {
|
|
|
|
t.Fatal("Expected Network Resource to not be empty")
|
|
|
|
}
|
|
|
|
if net.IP == "169.254.155.20" {
|
|
|
|
t.Fatalf("expected non-link local address; got %v", net.IP)
|
|
|
|
}
|
|
|
|
if net.CIDR == "" {
|
|
|
|
t.Fatal("Expected Network Resource to have a CIDR")
|
|
|
|
}
|
|
|
|
if net.Device == "" {
|
|
|
|
t.Fatal("Expected Network Resource to have a Device Name")
|
|
|
|
}
|
|
|
|
if net.MBits == 0 {
|
|
|
|
t.Fatal("Expected Network Resource to have a non-zero bandwidth")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetworkFingerPrint_LinkLocal_Disallowed(t *testing.T) {
|
2018-09-16 00:48:59 +00:00
|
|
|
f := &NetworkFingerprint{logger: testlog.HCLogger(t), interfaceDetector: &NetworkInterfaceDetectorMultipleInterfaces{}}
|
2017-08-23 22:32:22 +00:00
|
|
|
node := &structs.Node{
|
|
|
|
Attributes: make(map[string]string),
|
|
|
|
}
|
|
|
|
cfg := &config.Config{
|
|
|
|
NetworkSpeed: 100,
|
|
|
|
NetworkInterface: "eth3",
|
|
|
|
Options: map[string]string{
|
|
|
|
networkDisallowLinkLocalOption: "true",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-01-24 14:09:53 +00:00
|
|
|
request := &cstructs.FingerprintRequest{Config: cfg, Node: node}
|
2018-01-26 16:21:07 +00:00
|
|
|
var response cstructs.FingerprintResponse
|
|
|
|
err := f.Fingerprint(request, &response)
|
2017-08-23 22:32:22 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2018-01-30 17:57:37 +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")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(response.Attributes) != 0 {
|
2018-01-26 16:21:07 +00:00
|
|
|
t.Fatalf("should not apply attributes")
|
2017-08-23 22:32:22 +00:00
|
|
|
}
|
|
|
|
}
|