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"
|
|
|
|
"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,
|
|
|
|
}
|
|
|
|
)
|
2015-10-29 18:01:15 +00:00
|
|
|
|
|
|
|
// A fake network detector which returns no devices
|
|
|
|
type NetworkIntefaceDetectorNoDevices struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *NetworkIntefaceDetectorNoDevices) Interfaces() ([]net.Interface, error) {
|
|
|
|
return make([]net.Interface, 0), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *NetworkIntefaceDetectorNoDevices) InterfaceByName(name string) (*net.Interface, error) {
|
|
|
|
return nil, fmt.Errorf("Device with name %s doesn't exist", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *NetworkIntefaceDetectorNoDevices) Addrs(intf *net.Interface) ([]net.Addr, error) {
|
|
|
|
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) {
|
|
|
|
return []net.Interface{lo, eth0, eth1, eth2}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
|
2015-10-29 23:16:10 +00:00
|
|
|
f := &NetworkFingerprint{logger: testLogger(), 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
|
|
|
|
2015-10-03 00:32:11 +00:00
|
|
|
ok, err := f.Fingerprint(cfg, node)
|
2015-09-18 21:56:27 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !ok {
|
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
|
|
|
}
|
|
|
|
|
2016-01-23 02:12:16 +00:00
|
|
|
assertNodeAttributeContains(t, node, "unique.network.ip-address")
|
2015-09-23 03:56:31 +00:00
|
|
|
|
2016-01-23 02:12:16 +00:00
|
|
|
ip := node.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
|
|
|
|
|
|
|
if node.Resources == nil || len(node.Resources.Networks) == 0 {
|
|
|
|
t.Fatal("Expected to find Network Resources")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test at least the first Network Resource
|
|
|
|
net := node.Resources.Networks[0]
|
|
|
|
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 {
|
|
|
|
t.Fatalf("Expected Network Resource to have bandwith %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_no_devices(t *testing.T) {
|
|
|
|
f := &NetworkFingerprint{logger: testLogger(), interfaceDetector: &NetworkIntefaceDetectorNoDevices{}}
|
|
|
|
node := &structs.Node{
|
|
|
|
Attributes: make(map[string]string),
|
|
|
|
}
|
|
|
|
cfg := &config.Config{NetworkSpeed: 100}
|
|
|
|
|
|
|
|
ok, err := f.Fingerprint(cfg, node)
|
2015-11-19 17:22:32 +00:00
|
|
|
if err != nil {
|
2015-10-29 18:01:15 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
t.Fatalf("ok: %v", ok)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetworkFingerprint_default_device_absent(t *testing.T) {
|
|
|
|
f := &NetworkFingerprint{logger: testLogger(), interfaceDetector: &NetworkInterfaceDetectorOnlyLo{}}
|
|
|
|
node := &structs.Node{
|
|
|
|
Attributes: make(map[string]string),
|
|
|
|
}
|
|
|
|
cfg := &config.Config{NetworkSpeed: 100, NetworkInterface: "eth0"}
|
|
|
|
|
|
|
|
ok, err := f.Fingerprint(cfg, node)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
t.Fatalf("ok: %v", ok)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetworkFingerPrint_default_device(t *testing.T) {
|
|
|
|
f := &NetworkFingerprint{logger: testLogger(), interfaceDetector: &NetworkInterfaceDetectorOnlyLo{}}
|
|
|
|
node := &structs.Node{
|
|
|
|
Attributes: make(map[string]string),
|
|
|
|
}
|
|
|
|
cfg := &config.Config{NetworkSpeed: 100, NetworkInterface: "lo"}
|
|
|
|
|
|
|
|
ok, err := f.Fingerprint(cfg, node)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("should apply")
|
|
|
|
}
|
|
|
|
|
2016-01-23 02:12:16 +00:00
|
|
|
assertNodeAttributeContains(t, node, "unique.network.ip-address")
|
2015-10-29 18:01:15 +00:00
|
|
|
|
2016-01-23 02:12:16 +00:00
|
|
|
ip := node.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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if node.Resources == nil || len(node.Resources.Networks) == 0 {
|
|
|
|
t.Fatal("Expected to find Network Resources")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test at least the first Network Resource
|
|
|
|
net := node.Resources.Networks[0]
|
|
|
|
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 bandwith")
|
|
|
|
}
|
|
|
|
}
|
2015-10-29 22:14:13 +00:00
|
|
|
|
|
|
|
func TestNetworkFingerPrint_excludelo_down_interfaces(t *testing.T) {
|
|
|
|
f := &NetworkFingerprint{logger: testLogger(), interfaceDetector: &NetworkInterfaceDetectorMultipleInterfaces{}}
|
|
|
|
node := &structs.Node{
|
|
|
|
Attributes: make(map[string]string),
|
|
|
|
}
|
|
|
|
cfg := &config.Config{NetworkSpeed: 100}
|
|
|
|
|
|
|
|
ok, err := f.Fingerprint(cfg, node)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("should apply")
|
|
|
|
}
|
|
|
|
|
2016-01-23 02:12:16 +00:00
|
|
|
assertNodeAttributeContains(t, node, "unique.network.ip-address")
|
2015-10-29 22:14:13 +00:00
|
|
|
|
2016-01-23 02:12:16 +00:00
|
|
|
ip := node.Attributes["unique.network.ip-address"]
|
2015-10-29 22:14:13 +00:00
|
|
|
match := net.ParseIP(ip)
|
|
|
|
if match == nil {
|
|
|
|
t.Fatalf("Bad IP match: %s", ip)
|
|
|
|
}
|
|
|
|
|
|
|
|
if node.Resources == nil || len(node.Resources.Networks) == 0 {
|
|
|
|
t.Fatal("Expected to find Network Resources")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test at least the first Network Resource
|
|
|
|
net := node.Resources.Networks[0]
|
|
|
|
if net.IP == "" {
|
|
|
|
t.Fatal("Expected Network Resource to have an IP")
|
|
|
|
}
|
|
|
|
if net.CIDR == "" {
|
|
|
|
t.Fatal("Expected Network Resource to have a CIDR")
|
|
|
|
}
|
|
|
|
if net.Device != "eth0" {
|
|
|
|
t.Fatal("Expected Network Resource to be eth0. Actual: ", net.Device)
|
|
|
|
}
|
|
|
|
if net.MBits == 0 {
|
|
|
|
t.Fatal("Expected Network Resource to have a non-zero bandwith")
|
|
|
|
}
|
2017-04-08 20:43:29 +00:00
|
|
|
|
|
|
|
// Test the CIDR of the IPs
|
|
|
|
if node.Resources.Networks[0].CIDR != "100.64.0.0/32" {
|
|
|
|
t.Fatalf("bad CIDR: %v", node.Resources.Networks[0].CIDR)
|
|
|
|
}
|
|
|
|
if node.Resources.Networks[1].CIDR != "2001:db8:85a3::/128" {
|
|
|
|
t.Fatalf("bad CIDR: %v", node.Resources.Networks[1].CIDR)
|
|
|
|
}
|
|
|
|
// Ensure that the link local address isn't fingerprinted
|
|
|
|
if len(node.Resources.Networks) != 2 {
|
|
|
|
t.Fatalf("bad number of IPs %v", len(node.Resources.Networks))
|
|
|
|
}
|
2015-10-29 22:14:13 +00:00
|
|
|
}
|