open-nomad/client/fingerprint_manager_test.go

430 lines
9.3 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) {
2018-01-24 13:01:37 +00:00
if _, ok := driver.BuiltinDrivers["mock_driver"]; !ok {
t.Skip(`test requires mock_driver; run with "-tags nomad_test"`)
}
t.Parallel()
require := require.New(t)
node := &structs.Node{
Attributes: make(map[string]string, 0),
}
2018-02-05 23:02:52 +00:00
updateNode := func(r *cstructs.FingerprintResponse) *structs.Node {
for k, v := range r.Attributes {
node.Attributes[k] = v
}
return node
2018-01-24 13:01:37 +00:00
}
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{}),
updateNode,
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
}
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),
}
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
updateNode := func(r *cstructs.FingerprintResponse) *structs.Node {
for k, v := range r.Attributes {
node.Attributes[k] = v
}
return node
2018-01-24 13:01:37 +00:00
}
2018-02-05 23:02:52 +00:00
fm := NewFingerprintManager(
getConfig,
node,
make(chan struct{}),
updateNode,
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"])
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),
}
2018-02-05 23:02:52 +00:00
updateNode := func(r *cstructs.FingerprintResponse) *structs.Node {
for k, v := range r.Attributes {
node.Attributes[k] = v
}
return node
}
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,
updateNode,
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)
// Ensure the mock driver is registered on the client
testutil.WaitForResult(func() (bool, error) {
2018-02-05 23:02:52 +00:00
mockDriverStatus := node.Attributes["driver.mock_driver"]
2018-01-24 13:01:37 +00:00
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
testutil.WaitForResult(func() (bool, error) {
2018-02-05 23:02:52 +00:00
mockDriverStatus := node.Attributes["driver.mock_driver"]
2018-01-24 13:01:37 +00:00
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-02-05 23:02:52 +00:00
func TestFimgerprintManager_Run_InWhitelist(t *testing.T) {
t.Parallel()
require := require.New(t)
node := &structs.Node{
Attributes: make(map[string]string, 0),
}
updateNode := func(r *cstructs.FingerprintResponse) *structs.Node {
for k, v := range r.Attributes {
node.Attributes[k] = v
}
return node
}
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,
updateNode,
testLogger(),
)
err := fm.Run()
require.Nil(err)
require.NotEqual(node.Attributes["cpu.frequency"], "")
}
func TestFimgerprintManager_Run_InBlacklist(t *testing.T) {
t.Parallel()
require := require.New(t)
node := &structs.Node{
Attributes: make(map[string]string, 0),
}
updateNode := func(r *cstructs.FingerprintResponse) *structs.Node {
for k, v := range r.Attributes {
node.Attributes[k] = v
}
return node
}
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,
updateNode,
testLogger(),
)
err := fm.Run()
require.Nil(err)
require.Equal(node.Attributes["cpu.frequency"], "")
require.NotEqual(node.Attributes["memory.totalbytes"], "")
}
func TestFimgerprintManager_Run_Combination(t *testing.T) {
t.Parallel()
require := require.New(t)
node := &structs.Node{
Attributes: make(map[string]string, 0),
}
updateNode := func(r *cstructs.FingerprintResponse) *structs.Node {
for k, v := range r.Attributes {
node.Attributes[k] = v
}
return node
}
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,
updateNode,
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"], "")
}
func TestFimgerprintManager_Run_WhitelistDrivers(t *testing.T) {
t.Parallel()
require := require.New(t)
node := &structs.Node{
Attributes: make(map[string]string, 0),
}
updateNode := func(r *cstructs.FingerprintResponse) *structs.Node {
for k, v := range r.Attributes {
node.Attributes[k] = v
}
return node
}
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,
updateNode,
testLogger(),
)
err := fm.Run()
require.Nil(err)
require.NotEqual(node.Attributes["driver.raw_exec"], "")
}
func TestFimgerprintManager_Run_AllDriversBlacklisted(t *testing.T) {
t.Parallel()
require := require.New(t)
node := &structs.Node{
Attributes: make(map[string]string, 0),
}
updateNode := func(r *cstructs.FingerprintResponse) *structs.Node {
for k, v := range r.Attributes {
node.Attributes[k] = v
}
return node
}
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,
updateNode,
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"], "")
}
func TestFimgerprintManager_Run_DriversWhiteListBlacklistCombination(t *testing.T) {
t.Parallel()
require := require.New(t)
node := &structs.Node{
Attributes: make(map[string]string, 0),
}
updateNode := func(r *cstructs.FingerprintResponse) *structs.Node {
for k, v := range r.Attributes {
node.Attributes[k] = v
}
return node
}
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,
updateNode,
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"], "")
}
func TestFimgerprintManager_Run_DriversInBlacklist(t *testing.T) {
t.Parallel()
require := require.New(t)
node := &structs.Node{
Attributes: make(map[string]string, 0),
}
updateNode := func(r *cstructs.FingerprintResponse) *structs.Node {
for k, v := range r.Attributes {
node.Attributes[k] = v
}
return node
}
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 ",
}
getConfig := func() *config.Config {
return conf
}
shutdownCh := make(chan struct{})
defer (func() {
close(shutdownCh)
})()
fm := NewFingerprintManager(
getConfig,
node,
shutdownCh,
updateNode,
testLogger(),
)
err := fm.Run()
require.Nil(err)
require.NotEqual(node.Attributes["driver.raw_exec"], "")
require.Equal(node.Attributes["driver.exec"], "")
}