open-nomad/client/fingerprint_manager_test.go

688 lines
17 KiB
Go
Raw Normal View History

2018-01-24 13:01:37 +00:00
package client
import (
"fmt"
"testing"
"github.com/hashicorp/nomad/client/config"
"github.com/hashicorp/nomad/client/driver"
cstructs "github.com/hashicorp/nomad/client/structs"
2018-01-24 13:01:37 +00:00
"github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/testutil"
"github.com/stretchr/testify/require"
)
2018-02-05 23:02:52 +00:00
func TestFingerprintManager_Run_MockDriver(t *testing.T) {
driver.CheckForMockDriver(t)
2018-01-24 13:01:37 +00:00
t.Parallel()
require := require.New(t)
node := &structs.Node{
Attributes: make(map[string]string, 0),
Links: make(map[string]string, 0),
Resources: &structs.Resources{},
Drivers: make(map[string]*structs.DriverInfo, 0),
2018-01-24 13:01:37 +00:00
}
testConfig := config.Config{Node: node}
testClient := &Client{config: &testConfig}
2018-02-05 23:02:52 +00:00
conf := config.DefaultConfig()
2018-01-24 13:01:37 +00:00
getConfig := func() *config.Config {
2018-02-05 23:02:52 +00:00
return conf
2018-01-24 13:01:37 +00:00
}
2018-02-05 23:02:52 +00:00
fm := NewFingerprintManager(
getConfig,
node,
make(chan struct{}),
testClient.updateNodeFromFingerprint,
testClient.updateNodeFromHealthCheck,
2018-02-05 23:02:52 +00:00
testLogger(),
)
2018-01-24 13:01:37 +00:00
2018-02-05 23:02:52 +00:00
err := fm.Run()
2018-01-24 13:01:37 +00:00
require.Nil(err)
2018-02-05 23:02:52 +00:00
require.NotEqual("", node.Attributes["driver.mock_driver"])
2018-01-24 13:01:37 +00:00
}
func TestFingerprintManager_Run_ResourcesFingerprint(t *testing.T) {
driver.CheckForMockDriver(t)
t.Parallel()
require := require.New(t)
node := &structs.Node{
Attributes: make(map[string]string, 0),
Links: make(map[string]string, 0),
Resources: &structs.Resources{},
Drivers: make(map[string]*structs.DriverInfo, 0),
}
testConfig := config.Config{Node: node}
testClient := &Client{config: &testConfig}
conf := config.DefaultConfig()
getConfig := func() *config.Config {
return conf
}
fm := NewFingerprintManager(
getConfig,
node,
make(chan struct{}),
testClient.updateNodeFromFingerprint,
testClient.updateNodeFromHealthCheck,
testLogger(),
)
err := fm.Run()
require.Nil(err)
require.NotEqual(0, node.Resources.CPU)
require.NotEqual(0, node.Resources.MemoryMB)
2018-02-23 20:01:57 +00:00
require.NotZero(node.Resources.DiskMB)
}
2018-02-05 23:02:52 +00:00
func TestFingerprintManager_Fingerprint_Run(t *testing.T) {
2018-01-24 13:01:37 +00:00
t.Parallel()
require := require.New(t)
node := &structs.Node{
Attributes: make(map[string]string, 0),
Links: make(map[string]string, 0),
Resources: &structs.Resources{},
Drivers: make(map[string]*structs.DriverInfo, 0),
2018-01-24 13:01:37 +00:00
}
testConfig := config.Config{Node: node}
testClient := &Client{config: &testConfig}
2018-02-05 23:02:52 +00:00
conf := config.DefaultConfig()
conf.Options = map[string]string{"driver.raw_exec.enable": "true"}
2018-01-24 13:01:37 +00:00
getConfig := func() *config.Config {
2018-02-05 23:02:52 +00:00
return conf
2018-01-24 13:01:37 +00:00
}
2018-02-05 23:02:52 +00:00
fm := NewFingerprintManager(
getConfig,
node,
make(chan struct{}),
testClient.updateNodeFromFingerprint,
testClient.updateNodeFromHealthCheck,
2018-02-05 23:02:52 +00:00
testLogger(),
)
err := fm.Run()
2018-01-24 13:01:37 +00:00
require.Nil(err)
2018-02-05 23:02:52 +00:00
require.NotEqual("", node.Attributes["driver.raw_exec"])
require.True(node.Drivers["raw_exec"].Detected)
2018-03-07 16:58:14 +00:00
require.True(node.Drivers["raw_exec"].Healthy)
2018-01-24 13:01:37 +00:00
}
func TestFingerprintManager_Fingerprint_Periodic(t *testing.T) {
t.Parallel()
require := require.New(t)
node := &structs.Node{
Attributes: make(map[string]string, 0),
Links: make(map[string]string, 0),
Resources: &structs.Resources{},
Drivers: make(map[string]*structs.DriverInfo, 0),
2018-01-24 13:01:37 +00:00
}
testConfig := config.Config{Node: node}
testClient := &Client{config: &testConfig}
2018-02-05 23:02:52 +00:00
conf := config.DefaultConfig()
conf.Options = map[string]string{
"test.shutdown_periodic_after": "true",
"test.shutdown_periodic_duration": "3",
2018-01-24 13:01:37 +00:00
}
getConfig := func() *config.Config {
2018-02-05 23:02:52 +00:00
return conf
2018-01-24 13:01:37 +00:00
}
shutdownCh := make(chan struct{})
defer (func() {
close(shutdownCh)
})()
2018-02-05 23:02:52 +00:00
fm := NewFingerprintManager(
getConfig,
node,
shutdownCh,
testClient.updateNodeFromFingerprint,
testClient.updateNodeFromHealthCheck,
2018-02-05 23:02:52 +00:00
testLogger(),
)
2018-01-24 13:01:37 +00:00
2018-02-05 23:02:52 +00:00
err := fm.Run()
2018-01-24 13:01:37 +00:00
require.Nil(err)
2018-03-03 01:21:13 +00:00
{
// Ensure the mock driver is registered and healthy on the client
testutil.WaitForResult(func() (bool, error) {
fm.nodeLock.Lock()
mockDriverStatus := node.Attributes["driver.mock_driver"]
fm.nodeLock.Unlock()
if mockDriverStatus == "" {
return false, fmt.Errorf("mock driver attribute should be set on the client")
}
return true, nil
}, func(err error) {
t.Fatalf("err: %v", err)
})
}
// Ensure that the client fingerprinter eventually removes this attribute and
// marks the driver as unhealthy
2018-03-03 01:21:13 +00:00
{
testutil.WaitForResult(func() (bool, error) {
fm.nodeLock.Lock()
mockDriverStatus := node.Attributes["driver.mock_driver"]
fm.nodeLock.Unlock()
if mockDriverStatus != "" {
return false, fmt.Errorf("mock driver attribute should not be set on the client")
}
return true, nil
}, func(err error) {
t.Fatalf("err: %v", err)
})
}
2018-01-24 13:01:37 +00:00
}
2018-02-05 23:02:52 +00:00
// This is a temporary measure to check that a driver has both attributes on a
// node set as well as DriverInfo.
func TestFingerprintManager_HealthCheck_Driver(t *testing.T) {
t.Parallel()
require := require.New(t)
node := &structs.Node{
Attributes: make(map[string]string, 0),
Drivers: make(map[string]*structs.DriverInfo, 0),
}
updateNode := func(r *cstructs.FingerprintResponse) *structs.Node {
if r.Attributes != nil {
for k, v := range r.Attributes {
node.Attributes[k] = v
}
}
return node
}
updateHealthCheck := func(resp *cstructs.HealthCheckResponse) *structs.Node {
if resp.Drivers != nil {
for k, v := range resp.Drivers {
node.Drivers[k] = v
}
}
return node
}
conf := config.DefaultConfig()
conf.Options = map[string]string{
"driver.raw_exec.enable": "1",
"test.shutdown_periodic_after": "true",
"test.shutdown_periodic_duration": "2",
}
getConfig := func() *config.Config {
return conf
}
shutdownCh := make(chan struct{})
defer (func() {
close(shutdownCh)
})()
fm := NewFingerprintManager(
getConfig,
node,
shutdownCh,
updateNode,
updateHealthCheck,
testLogger(),
)
err := fm.Run()
require.Nil(err)
// Ensure the mock driver is registered and healthy on the client
testutil.WaitForResult(func() (bool, error) {
mockDriverAttribute := node.Attributes["driver.mock_driver"]
if mockDriverAttribute == "" {
return false, fmt.Errorf("mock driver info should be set on the client attributes")
}
mockDriverInfo := node.Drivers["mock_driver"]
if mockDriverInfo == nil {
return false, fmt.Errorf("mock driver info should be set on the client")
}
if !mockDriverInfo.Healthy {
return false, fmt.Errorf("mock driver info should be healthy")
}
return true, nil
}, func(err error) {
t.Fatalf("err: %v", err)
})
// Ensure that a default driver without health checks enabled is registered and healthy on the client
testutil.WaitForResult(func() (bool, error) {
rawExecAttribute := node.Attributes["driver.raw_exec"]
if rawExecAttribute == "" {
return false, fmt.Errorf("raw exec info should be set on the client attributes")
}
rawExecInfo := node.Drivers["raw_exec"]
if rawExecInfo == nil {
return false, fmt.Errorf("raw exec driver info should be set on the client")
}
if !rawExecInfo.Detected {
return false, fmt.Errorf("raw exec driver should be detected")
}
return true, nil
}, func(err error) {
t.Fatalf("err: %v", err)
})
2018-03-07 16:58:14 +00:00
// Ensure the mock driver is de-registered when it becomes unhealthy
testutil.WaitForResult(func() (bool, error) {
mockDriverAttribute := node.Attributes["driver.mock_driver"]
if mockDriverAttribute == "" {
return false, fmt.Errorf("mock driver info should be set on the client attributes")
}
mockDriverInfo := node.Drivers["mock_driver"]
if mockDriverInfo == nil {
return false, fmt.Errorf("mock driver info should be set on the client")
}
if !mockDriverInfo.Healthy {
return false, fmt.Errorf("mock driver info should not be healthy")
}
return true, nil
}, func(err error) {
t.Fatalf("err: %v", err)
})
}
func TestFingerprintManager_HealthCheck_Periodic(t *testing.T) {
t.Parallel()
require := require.New(t)
node := &structs.Node{
2018-03-03 01:21:13 +00:00
Attributes: make(map[string]string, 0),
Links: make(map[string]string, 0),
Resources: &structs.Resources{},
Drivers: make(map[string]*structs.DriverInfo, 0),
}
2018-03-03 01:21:13 +00:00
testConfig := config.Config{Node: node}
testClient := &Client{config: &testConfig}
conf := config.DefaultConfig()
conf.Options = map[string]string{
"test.shutdown_periodic_after": "true",
2018-03-03 01:21:13 +00:00
"test.shutdown_periodic_duration": "3",
}
getConfig := func() *config.Config {
return conf
}
shutdownCh := make(chan struct{})
defer (func() {
close(shutdownCh)
})()
fm := NewFingerprintManager(
getConfig,
node,
shutdownCh,
2018-03-03 01:21:13 +00:00
testClient.updateNodeFromFingerprint,
testClient.updateNodeFromHealthCheck,
testLogger(),
)
err := fm.Run()
require.Nil(err)
2018-03-03 01:21:13 +00:00
{
// Ensure the mock driver is registered and healthy on the client
testutil.WaitForResult(func() (bool, error) {
fm.nodeLock.Lock()
mockDriverInfo := node.Drivers["mock_driver"]
fm.nodeLock.Unlock()
if mockDriverInfo == nil {
return false, fmt.Errorf("mock driver info should be set on the client")
}
if !mockDriverInfo.Detected {
return false, fmt.Errorf("mock driver info should be detected")
}
if !mockDriverInfo.Healthy {
return false, fmt.Errorf("mock driver info should be healthy")
}
return true, nil
}, func(err error) {
t.Fatalf("err: %v", err)
})
}
{
// Ensure that the client health check eventually removes this attribute and
// marks the driver as unhealthy
testutil.WaitForResult(func() (bool, error) {
fm.nodeLock.Lock()
mockDriverInfo := node.Drivers["mock_driver"]
fm.nodeLock.Unlock()
if mockDriverInfo == nil {
return false, fmt.Errorf("mock driver info should be set on the client")
}
if !mockDriverInfo.Detected {
return false, fmt.Errorf("mock driver info should be detected")
}
if !mockDriverInfo.Healthy {
return false, fmt.Errorf("mock driver info should be healthy")
}
return true, nil
}, func(err error) {
t.Fatalf("err: %v", err)
})
}
{
// Ensure that the client health check eventually removes this attribute and
// marks the driver as unhealthy
testutil.WaitForResult(func() (bool, error) {
fm.nodeLock.Lock()
mockDriverInfo := node.Drivers["mock_driver"]
fm.nodeLock.Unlock()
if mockDriverInfo == nil {
return false, fmt.Errorf("mock driver info should be set on the client")
}
if !mockDriverInfo.Detected {
return false, fmt.Errorf("mock driver should be detected")
}
if mockDriverInfo.Healthy {
return false, fmt.Errorf("mock driver should not be healthy")
}
return true, nil
}, func(err error) {
t.Fatalf("err: %v", err)
})
}
}
func TestFimgerprintManager_Run_InWhitelist(t *testing.T) {
2018-02-05 23:02:52 +00:00
t.Parallel()
require := require.New(t)
node := &structs.Node{
Attributes: make(map[string]string, 0),
Links: make(map[string]string, 0),
Resources: &structs.Resources{},
Drivers: make(map[string]*structs.DriverInfo, 0),
2018-02-05 23:02:52 +00:00
}
testConfig := config.Config{Node: node}
testClient := &Client{config: &testConfig}
2018-02-05 23:02:52 +00:00
conf := config.DefaultConfig()
conf.Options = map[string]string{"fingerprint.whitelist": " arch,cpu,memory,network,storage,foo,bar "}
getConfig := func() *config.Config {
return conf
}
shutdownCh := make(chan struct{})
defer (func() {
close(shutdownCh)
})()
fm := NewFingerprintManager(
getConfig,
node,
shutdownCh,
testClient.updateNodeFromFingerprint,
testClient.updateNodeFromHealthCheck,
2018-02-05 23:02:52 +00:00
testLogger(),
)
err := fm.Run()
require.Nil(err)
require.NotEqual(node.Attributes["cpu.frequency"], "")
}
2018-03-11 18:07:37 +00:00
func TestFingerprintManager_Run_InBlacklist(t *testing.T) {
2018-02-05 23:02:52 +00:00
t.Parallel()
require := require.New(t)
node := &structs.Node{
Attributes: make(map[string]string, 0),
Links: make(map[string]string, 0),
Resources: &structs.Resources{},
Drivers: make(map[string]*structs.DriverInfo, 0),
2018-02-05 23:02:52 +00:00
}
testConfig := config.Config{Node: node}
testClient := &Client{config: &testConfig}
2018-02-05 23:02:52 +00:00
conf := config.DefaultConfig()
conf.Options = map[string]string{"fingerprint.whitelist": " arch,memory,foo,bar "}
conf.Options = map[string]string{"fingerprint.blacklist": " cpu "}
getConfig := func() *config.Config {
return conf
}
shutdownCh := make(chan struct{})
defer (func() {
close(shutdownCh)
})()
fm := NewFingerprintManager(
getConfig,
node,
shutdownCh,
testClient.updateNodeFromFingerprint,
testClient.updateNodeFromHealthCheck,
2018-02-05 23:02:52 +00:00
testLogger(),
)
err := fm.Run()
require.Nil(err)
require.Equal(node.Attributes["cpu.frequency"], "")
require.NotEqual(node.Attributes["memory.totalbytes"], "")
}
2018-03-11 18:07:37 +00:00
func TestFingerprintManager_Run_Combination(t *testing.T) {
2018-02-05 23:02:52 +00:00
t.Parallel()
require := require.New(t)
node := &structs.Node{
Attributes: make(map[string]string, 0),
Links: make(map[string]string, 0),
Resources: &structs.Resources{},
Drivers: make(map[string]*structs.DriverInfo, 0),
2018-02-05 23:02:52 +00:00
}
testConfig := config.Config{Node: node}
testClient := &Client{config: &testConfig}
2018-02-05 23:02:52 +00:00
conf := config.DefaultConfig()
conf.Options = map[string]string{"fingerprint.whitelist": " arch,cpu,memory,foo,bar "}
conf.Options = map[string]string{"fingerprint.blacklist": " memory,nomad "}
getConfig := func() *config.Config {
return conf
}
shutdownCh := make(chan struct{})
defer (func() {
close(shutdownCh)
})()
fm := NewFingerprintManager(
getConfig,
node,
shutdownCh,
testClient.updateNodeFromFingerprint,
testClient.updateNodeFromHealthCheck,
2018-02-05 23:02:52 +00:00
testLogger(),
)
err := fm.Run()
require.Nil(err)
require.NotEqual(node.Attributes["cpu.frequency"], "")
require.NotEqual(node.Attributes["cpu.arch"], "")
require.Equal(node.Attributes["memory.totalbytes"], "")
require.Equal(node.Attributes["nomad.version"], "")
}
2018-03-11 18:07:37 +00:00
func TestFingerprintManager_Run_WhitelistDrivers(t *testing.T) {
2018-02-05 23:02:52 +00:00
t.Parallel()
require := require.New(t)
node := &structs.Node{
Attributes: make(map[string]string, 0),
Links: make(map[string]string, 0),
Resources: &structs.Resources{},
Drivers: make(map[string]*structs.DriverInfo, 0),
2018-02-05 23:02:52 +00:00
}
testConfig := config.Config{Node: node}
testClient := &Client{config: &testConfig}
2018-02-05 23:02:52 +00:00
conf := config.DefaultConfig()
conf.Options = map[string]string{
"driver.raw_exec.enable": "1",
"driver.whitelist": " raw_exec , foo ",
}
getConfig := func() *config.Config {
return conf
}
shutdownCh := make(chan struct{})
defer (func() {
close(shutdownCh)
})()
fm := NewFingerprintManager(
getConfig,
node,
shutdownCh,
testClient.updateNodeFromFingerprint,
testClient.updateNodeFromHealthCheck,
2018-02-05 23:02:52 +00:00
testLogger(),
)
err := fm.Run()
require.Nil(err)
require.NotEqual(node.Attributes["driver.raw_exec"], "")
}
2018-03-11 18:07:37 +00:00
func TestFingerprintManager_Run_AllDriversBlacklisted(t *testing.T) {
2018-02-05 23:02:52 +00:00
t.Parallel()
require := require.New(t)
node := &structs.Node{
Attributes: make(map[string]string, 0),
Links: make(map[string]string, 0),
Resources: &structs.Resources{},
Drivers: make(map[string]*structs.DriverInfo, 0),
2018-02-05 23:02:52 +00:00
}
testConfig := config.Config{Node: node}
testClient := &Client{config: &testConfig}
2018-02-05 23:02:52 +00:00
conf := config.DefaultConfig()
conf.Options = map[string]string{
"driver.whitelist": " foo,bar,baz ",
}
getConfig := func() *config.Config {
return conf
}
shutdownCh := make(chan struct{})
defer (func() {
close(shutdownCh)
})()
fm := NewFingerprintManager(
getConfig,
node,
shutdownCh,
testClient.updateNodeFromFingerprint,
testClient.updateNodeFromHealthCheck,
2018-02-05 23:02:52 +00:00
testLogger(),
)
err := fm.Run()
require.Nil(err)
require.Equal(node.Attributes["driver.raw_exec"], "")
require.Equal(node.Attributes["driver.exec"], "")
require.Equal(node.Attributes["driver.docker"], "")
}
2018-03-11 18:07:37 +00:00
func TestFingerprintManager_Run_DriversWhiteListBlacklistCombination(t *testing.T) {
2018-02-05 23:02:52 +00:00
t.Parallel()
require := require.New(t)
node := &structs.Node{
Attributes: make(map[string]string, 0),
Links: make(map[string]string, 0),
Resources: &structs.Resources{},
Drivers: make(map[string]*structs.DriverInfo, 0),
2018-02-05 23:02:52 +00:00
}
testConfig := config.Config{Node: node}
testClient := &Client{config: &testConfig}
2018-02-05 23:02:52 +00:00
conf := config.DefaultConfig()
conf.Options = map[string]string{
"driver.raw_exec.enable": "1",
"driver.whitelist": " raw_exec,exec,foo,bar,baz ",
"driver.blacklist": " exec,foo,bar,baz ",
}
getConfig := func() *config.Config {
return conf
}
shutdownCh := make(chan struct{})
defer (func() {
close(shutdownCh)
})()
fm := NewFingerprintManager(
getConfig,
node,
shutdownCh,
testClient.updateNodeFromFingerprint,
testClient.updateNodeFromHealthCheck,
2018-02-05 23:02:52 +00:00
testLogger(),
)
err := fm.Run()
require.Nil(err)
require.NotEqual(node.Attributes["driver.raw_exec"], "")
require.Equal(node.Attributes["driver.exec"], "")
require.Equal(node.Attributes["foo"], "")
require.Equal(node.Attributes["bar"], "")
require.Equal(node.Attributes["baz"], "")
}
2018-03-11 18:07:37 +00:00
func TestFingerprintManager_Run_DriversInBlacklist(t *testing.T) {
2018-02-05 23:02:52 +00:00
t.Parallel()
require := require.New(t)
node := &structs.Node{
Attributes: make(map[string]string, 0),
Links: make(map[string]string, 0),
Resources: &structs.Resources{},
Drivers: make(map[string]*structs.DriverInfo, 0),
2018-02-05 23:02:52 +00:00
}
conf := config.DefaultConfig()
conf.Options = map[string]string{
"driver.raw_exec.enable": "1",
"driver.whitelist": " raw_exec,foo,bar,baz ",
"driver.blacklist": " exec,foo,bar,baz ",
}
conf.Node = node
testClient := &Client{config: conf}
2018-02-05 23:02:52 +00:00
shutdownCh := make(chan struct{})
defer (func() {
close(shutdownCh)
})()
fm := NewFingerprintManager(
testClient.GetConfig,
2018-02-05 23:02:52 +00:00
node,
shutdownCh,
testClient.updateNodeFromFingerprint,
testClient.updateNodeFromHealthCheck,
2018-02-05 23:02:52 +00:00
testLogger(),
)
err := fm.Run()
require.Nil(err)
require.NotEqual(node.Attributes["driver.raw_exec"], "")
require.Equal(node.Attributes["driver.exec"], "")
}