github.com/smithx10/nomad@v0.9.1-rc1/client/fingerprint/cgroup_test.go (about) 1 // +build linux 2 3 package fingerprint 4 5 import ( 6 "fmt" 7 "testing" 8 9 "github.com/hashicorp/nomad/client/config" 10 "github.com/hashicorp/nomad/helper/testlog" 11 "github.com/hashicorp/nomad/nomad/structs" 12 ) 13 14 // A fake mount point detector that returns an empty path 15 type MountPointDetectorNoMountPoint struct{} 16 17 func (m *MountPointDetectorNoMountPoint) MountPoint() (string, error) { 18 return "", nil 19 } 20 21 // A fake mount point detector that returns an error 22 type MountPointDetectorMountPointFail struct{} 23 24 func (m *MountPointDetectorMountPointFail) MountPoint() (string, error) { 25 return "", fmt.Errorf("cgroup mountpoint discovery failed") 26 } 27 28 // A fake mount point detector that returns a valid path 29 type MountPointDetectorValidMountPoint struct{} 30 31 func (m *MountPointDetectorValidMountPoint) MountPoint() (string, error) { 32 return "/sys/fs/cgroup", nil 33 } 34 35 // A fake mount point detector that returns an empty path 36 type MountPointDetectorEmptyMountPoint struct{} 37 38 func (m *MountPointDetectorEmptyMountPoint) MountPoint() (string, error) { 39 return "", nil 40 } 41 42 func TestCGroupFingerprint(t *testing.T) { 43 { 44 f := &CGroupFingerprint{ 45 logger: testlog.HCLogger(t), 46 lastState: cgroupUnavailable, 47 mountPointDetector: &MountPointDetectorMountPointFail{}, 48 } 49 50 node := &structs.Node{ 51 Attributes: make(map[string]string), 52 } 53 54 request := &FingerprintRequest{Config: &config.Config{}, Node: node} 55 var response FingerprintResponse 56 err := f.Fingerprint(request, &response) 57 if err == nil { 58 t.Fatalf("expected an error") 59 } 60 61 if a, _ := response.Attributes["unique.cgroup.mountpoint"]; a != "" { 62 t.Fatalf("unexpected attribute found, %s", a) 63 } 64 } 65 66 { 67 f := &CGroupFingerprint{ 68 logger: testlog.HCLogger(t), 69 lastState: cgroupUnavailable, 70 mountPointDetector: &MountPointDetectorValidMountPoint{}, 71 } 72 73 node := &structs.Node{ 74 Attributes: make(map[string]string), 75 } 76 77 request := &FingerprintRequest{Config: &config.Config{}, Node: node} 78 var response FingerprintResponse 79 err := f.Fingerprint(request, &response) 80 if err != nil { 81 t.Fatalf("unexpected error, %s", err) 82 } 83 if a, ok := response.Attributes["unique.cgroup.mountpoint"]; !ok { 84 t.Fatalf("unable to find attribute: %s", a) 85 } 86 } 87 88 { 89 f := &CGroupFingerprint{ 90 logger: testlog.HCLogger(t), 91 lastState: cgroupUnavailable, 92 mountPointDetector: &MountPointDetectorEmptyMountPoint{}, 93 } 94 95 node := &structs.Node{ 96 Attributes: make(map[string]string), 97 } 98 99 request := &FingerprintRequest{Config: &config.Config{}, Node: node} 100 var response FingerprintResponse 101 err := f.Fingerprint(request, &response) 102 if err != nil { 103 t.Fatalf("unexpected error, %s", err) 104 } 105 if a, _ := response.Attributes["unique.cgroup.mountpoint"]; a != "" { 106 t.Fatalf("unexpected attribute found, %s", a) 107 } 108 } 109 { 110 f := &CGroupFingerprint{ 111 logger: testlog.HCLogger(t), 112 lastState: cgroupAvailable, 113 mountPointDetector: &MountPointDetectorValidMountPoint{}, 114 } 115 116 node := &structs.Node{ 117 Attributes: make(map[string]string), 118 } 119 120 request := &FingerprintRequest{Config: &config.Config{}, Node: node} 121 var response FingerprintResponse 122 err := f.Fingerprint(request, &response) 123 if err != nil { 124 t.Fatalf("unexpected error, %s", err) 125 } 126 if a, _ := response.Attributes["unique.cgroup.mountpoint"]; a == "" { 127 t.Fatalf("expected attribute to be found, %s", a) 128 } 129 } 130 }