2021-10-01 13:59:55 +00:00
|
|
|
//go:build linux
|
2016-07-10 20:55:06 +00:00
|
|
|
// +build linux
|
|
|
|
|
2016-01-27 06:22:25 +00:00
|
|
|
package fingerprint
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/client/config"
|
2018-06-13 22:33:25 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
2016-01-27 06:22:25 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// A fake mount point detector that returns an empty path
|
|
|
|
type MountPointDetectorNoMountPoint struct{}
|
|
|
|
|
|
|
|
func (m *MountPointDetectorNoMountPoint) MountPoint() (string, error) {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// A fake mount point detector that returns an error
|
|
|
|
type MountPointDetectorMountPointFail struct{}
|
|
|
|
|
|
|
|
func (m *MountPointDetectorMountPointFail) MountPoint() (string, error) {
|
|
|
|
return "", fmt.Errorf("cgroup mountpoint discovery failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// A fake mount point detector that returns a valid path
|
|
|
|
type MountPointDetectorValidMountPoint struct{}
|
|
|
|
|
|
|
|
func (m *MountPointDetectorValidMountPoint) MountPoint() (string, error) {
|
|
|
|
return "/sys/fs/cgroup", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// A fake mount point detector that returns an empty path
|
|
|
|
type MountPointDetectorEmptyMountPoint struct{}
|
|
|
|
|
|
|
|
func (m *MountPointDetectorEmptyMountPoint) MountPoint() (string, error) {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCGroupFingerprint(t *testing.T) {
|
2018-01-24 14:09:53 +00:00
|
|
|
{
|
|
|
|
f := &CGroupFingerprint{
|
2018-10-05 02:36:40 +00:00
|
|
|
logger: testlog.HCLogger(t),
|
2018-01-24 14:09:53 +00:00
|
|
|
lastState: cgroupUnavailable,
|
|
|
|
mountPointDetector: &MountPointDetectorMountPointFail{},
|
|
|
|
}
|
|
|
|
|
|
|
|
node := &structs.Node{
|
|
|
|
Attributes: make(map[string]string),
|
|
|
|
}
|
|
|
|
|
2018-12-01 16:10:39 +00:00
|
|
|
request := &FingerprintRequest{Config: &config.Config{}, Node: node}
|
|
|
|
var response FingerprintResponse
|
2018-01-26 16:21:07 +00:00
|
|
|
err := f.Fingerprint(request, &response)
|
2018-01-24 14:09:53 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected an error")
|
|
|
|
}
|
|
|
|
|
2018-01-30 17:57:37 +00:00
|
|
|
if a, _ := response.Attributes["unique.cgroup.mountpoint"]; a != "" {
|
2018-01-24 14:09:53 +00:00
|
|
|
t.Fatalf("unexpected attribute found, %s", a)
|
|
|
|
}
|
2016-01-27 06:22:25 +00:00
|
|
|
}
|
|
|
|
|
2018-01-24 14:09:53 +00:00
|
|
|
{
|
|
|
|
f := &CGroupFingerprint{
|
2018-10-05 02:36:40 +00:00
|
|
|
logger: testlog.HCLogger(t),
|
2018-01-24 14:09:53 +00:00
|
|
|
lastState: cgroupUnavailable,
|
|
|
|
mountPointDetector: &MountPointDetectorValidMountPoint{},
|
|
|
|
}
|
|
|
|
|
|
|
|
node := &structs.Node{
|
|
|
|
Attributes: make(map[string]string),
|
|
|
|
}
|
|
|
|
|
2018-12-01 16:10:39 +00:00
|
|
|
request := &FingerprintRequest{Config: &config.Config{}, Node: node}
|
|
|
|
var response FingerprintResponse
|
2018-01-26 16:21:07 +00:00
|
|
|
err := f.Fingerprint(request, &response)
|
2018-01-24 14:09:53 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error, %s", err)
|
|
|
|
}
|
2018-01-30 17:57:37 +00:00
|
|
|
if a, ok := response.Attributes["unique.cgroup.mountpoint"]; !ok {
|
2018-01-24 14:09:53 +00:00
|
|
|
t.Fatalf("unable to find attribute: %s", a)
|
|
|
|
}
|
2016-01-27 06:22:25 +00:00
|
|
|
}
|
|
|
|
|
2018-01-24 14:09:53 +00:00
|
|
|
{
|
|
|
|
f := &CGroupFingerprint{
|
2018-10-05 02:36:40 +00:00
|
|
|
logger: testlog.HCLogger(t),
|
2018-01-24 14:09:53 +00:00
|
|
|
lastState: cgroupUnavailable,
|
|
|
|
mountPointDetector: &MountPointDetectorEmptyMountPoint{},
|
|
|
|
}
|
|
|
|
|
|
|
|
node := &structs.Node{
|
|
|
|
Attributes: make(map[string]string),
|
|
|
|
}
|
|
|
|
|
2018-12-01 16:10:39 +00:00
|
|
|
request := &FingerprintRequest{Config: &config.Config{}, Node: node}
|
|
|
|
var response FingerprintResponse
|
2018-01-26 16:21:07 +00:00
|
|
|
err := f.Fingerprint(request, &response)
|
2018-01-24 14:09:53 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error, %s", err)
|
|
|
|
}
|
2018-01-30 17:57:37 +00:00
|
|
|
if a, _ := response.Attributes["unique.cgroup.mountpoint"]; a != "" {
|
2018-01-24 14:09:53 +00:00
|
|
|
t.Fatalf("unexpected attribute found, %s", a)
|
|
|
|
}
|
2016-01-27 06:22:25 +00:00
|
|
|
}
|
2018-01-25 11:02:40 +00:00
|
|
|
{
|
|
|
|
f := &CGroupFingerprint{
|
2018-10-05 02:36:40 +00:00
|
|
|
logger: testlog.HCLogger(t),
|
2018-01-25 11:02:40 +00:00
|
|
|
lastState: cgroupAvailable,
|
|
|
|
mountPointDetector: &MountPointDetectorValidMountPoint{},
|
|
|
|
}
|
|
|
|
|
|
|
|
node := &structs.Node{
|
|
|
|
Attributes: make(map[string]string),
|
|
|
|
}
|
|
|
|
|
2018-12-01 16:10:39 +00:00
|
|
|
request := &FingerprintRequest{Config: &config.Config{}, Node: node}
|
|
|
|
var response FingerprintResponse
|
2018-01-26 16:21:07 +00:00
|
|
|
err := f.Fingerprint(request, &response)
|
2018-01-25 11:02:40 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error, %s", err)
|
|
|
|
}
|
2018-01-30 17:57:37 +00:00
|
|
|
if a, _ := response.Attributes["unique.cgroup.mountpoint"]; a == "" {
|
2018-01-25 11:02:40 +00:00
|
|
|
t.Fatalf("expected attribute to be found, %s", a)
|
|
|
|
}
|
|
|
|
}
|
2016-01-27 06:22:25 +00:00
|
|
|
}
|