github.com/hhrutter/nomad@v0.6.0-rc2.0.20170723054333-80c4b03f0705/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/nomad/structs" 11 ) 12 13 // A fake mount point detector that returns an empty path 14 type MountPointDetectorNoMountPoint struct{} 15 16 func (m *MountPointDetectorNoMountPoint) MountPoint() (string, error) { 17 return "", nil 18 } 19 20 // A fake mount point detector that returns an error 21 type MountPointDetectorMountPointFail struct{} 22 23 func (m *MountPointDetectorMountPointFail) MountPoint() (string, error) { 24 return "", fmt.Errorf("cgroup mountpoint discovery failed") 25 } 26 27 // A fake mount point detector that returns a valid path 28 type MountPointDetectorValidMountPoint struct{} 29 30 func (m *MountPointDetectorValidMountPoint) MountPoint() (string, error) { 31 return "/sys/fs/cgroup", nil 32 } 33 34 // A fake mount point detector that returns an empty path 35 type MountPointDetectorEmptyMountPoint struct{} 36 37 func (m *MountPointDetectorEmptyMountPoint) MountPoint() (string, error) { 38 return "", nil 39 } 40 41 func TestCGroupFingerprint(t *testing.T) { 42 f := &CGroupFingerprint{ 43 logger: testLogger(), 44 lastState: cgroupUnavailable, 45 mountPointDetector: &MountPointDetectorMountPointFail{}, 46 } 47 48 node := &structs.Node{ 49 Attributes: make(map[string]string), 50 } 51 52 ok, err := f.Fingerprint(&config.Config{}, node) 53 if err == nil { 54 t.Fatalf("expected an error") 55 } 56 if ok { 57 t.Fatalf("should not apply") 58 } 59 if a, ok := node.Attributes["unique.cgroup.mountpoint"]; ok { 60 t.Fatalf("unexpected attribute found, %s", a) 61 } 62 63 f = &CGroupFingerprint{ 64 logger: testLogger(), 65 lastState: cgroupUnavailable, 66 mountPointDetector: &MountPointDetectorValidMountPoint{}, 67 } 68 69 node = &structs.Node{ 70 Attributes: make(map[string]string), 71 } 72 73 ok, err = f.Fingerprint(&config.Config{}, node) 74 if err != nil { 75 t.Fatalf("unexpected error, %s", err) 76 } 77 if !ok { 78 t.Fatalf("should apply") 79 } 80 assertNodeAttributeContains(t, node, "unique.cgroup.mountpoint") 81 82 f = &CGroupFingerprint{ 83 logger: testLogger(), 84 lastState: cgroupUnavailable, 85 mountPointDetector: &MountPointDetectorEmptyMountPoint{}, 86 } 87 88 node = &structs.Node{ 89 Attributes: make(map[string]string), 90 } 91 92 ok, err = f.Fingerprint(&config.Config{}, node) 93 if err != nil { 94 t.Fatalf("unexpected error, %s", err) 95 } 96 if !ok { 97 t.Fatalf("should apply") 98 } 99 if a, ok := node.Attributes["unique.cgroup.mountpoint"]; ok { 100 t.Fatalf("unexpected attribute found, %s", a) 101 } 102 }