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"
|
2018-02-05 17:20:07 +00:00
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// test that the driver manager updates a node when its attributes change
|
|
|
|
func TestFingerprintManager_Fingerprint_MockDriver(t *testing.T) {
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
mockConfig := &config.Config{
|
|
|
|
Node: node,
|
|
|
|
}
|
2018-02-05 17:20:07 +00:00
|
|
|
|
|
|
|
var resp *cstructs.FingerprintResponse
|
|
|
|
updateNode := func(r *cstructs.FingerprintResponse) {
|
|
|
|
resp = r
|
2018-01-24 13:01:37 +00:00
|
|
|
}
|
2018-02-05 17:20:07 +00:00
|
|
|
|
2018-01-24 13:01:37 +00:00
|
|
|
getConfig := func() *config.Config {
|
|
|
|
return mockConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
fm := FingerprintManager{
|
2018-02-05 17:20:07 +00:00
|
|
|
getConfig: getConfig,
|
|
|
|
node: node,
|
|
|
|
updateNode: updateNode,
|
|
|
|
logger: testLogger(),
|
2018-01-24 13:01:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// test setting up a mock driver
|
|
|
|
drivers := []string{"mock_driver"}
|
|
|
|
err := fm.SetupDrivers(drivers)
|
|
|
|
require.Nil(err)
|
|
|
|
|
2018-02-05 17:20:07 +00:00
|
|
|
require.NotEqual("", resp.Attributes["driver.mock_driver"])
|
2018-01-24 13:01:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFingerprintManager_Fingerprint_RawExec(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
node := &structs.Node{
|
|
|
|
Attributes: make(map[string]string, 0),
|
|
|
|
}
|
|
|
|
mockConfig := &config.Config{
|
|
|
|
Node: node,
|
|
|
|
Options: map[string]string{
|
|
|
|
"driver.raw_exec.enable": "true",
|
|
|
|
},
|
|
|
|
}
|
2018-02-05 17:20:07 +00:00
|
|
|
var resp *cstructs.FingerprintResponse
|
|
|
|
updateNode := func(r *cstructs.FingerprintResponse) {
|
|
|
|
resp = r
|
2018-01-24 13:01:37 +00:00
|
|
|
}
|
2018-02-05 17:20:07 +00:00
|
|
|
|
2018-01-24 13:01:37 +00:00
|
|
|
getConfig := func() *config.Config {
|
|
|
|
return mockConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
fm := FingerprintManager{
|
2018-02-05 17:20:07 +00:00
|
|
|
getConfig: getConfig,
|
|
|
|
node: node,
|
|
|
|
updateNode: updateNode,
|
|
|
|
logger: testLogger(),
|
2018-01-24 13:01:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// test setting up a mock driver
|
|
|
|
drivers := []string{"raw_exec"}
|
|
|
|
err := fm.SetupDrivers(drivers)
|
|
|
|
require.Nil(err)
|
|
|
|
|
2018-02-05 17:20:07 +00:00
|
|
|
require.NotEqual("", resp.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 17:20:07 +00:00
|
|
|
var resp *cstructs.FingerprintResponse
|
|
|
|
updateNode := func(r *cstructs.FingerprintResponse) {
|
|
|
|
resp = r
|
|
|
|
}
|
2018-01-24 13:01:37 +00:00
|
|
|
mockConfig := &config.Config{
|
|
|
|
Node: node,
|
|
|
|
Options: map[string]string{
|
|
|
|
"test.shutdown_periodic_after": "true",
|
|
|
|
"test.shutdown_periodic_duration": "3",
|
|
|
|
},
|
|
|
|
}
|
2018-02-05 17:20:07 +00:00
|
|
|
|
2018-01-24 13:01:37 +00:00
|
|
|
getConfig := func() *config.Config {
|
|
|
|
return mockConfig
|
|
|
|
}
|
|
|
|
|
2018-02-05 17:20:07 +00:00
|
|
|
shutdownCh := make(chan struct{})
|
|
|
|
defer (func() {
|
|
|
|
close(shutdownCh)
|
|
|
|
})()
|
|
|
|
|
2018-01-24 13:01:37 +00:00
|
|
|
fm := FingerprintManager{
|
2018-02-05 17:20:07 +00:00
|
|
|
getConfig: getConfig,
|
|
|
|
node: node,
|
|
|
|
updateNode: updateNode,
|
|
|
|
shutdownCh: shutdownCh,
|
|
|
|
logger: testLogger(),
|
2018-01-24 13:01:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// test setting up a mock driver
|
|
|
|
drivers := []string{"mock_driver"}
|
|
|
|
err := fm.SetupDrivers(drivers)
|
|
|
|
require.Nil(err)
|
|
|
|
|
|
|
|
// Ensure the mock driver is registered on the client
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
2018-02-05 17:20:07 +00:00
|
|
|
mockDriverStatus := resp.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 17:20:07 +00:00
|
|
|
mockDriverStatus := resp.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)
|
|
|
|
})
|
|
|
|
}
|