github.com/ranjib/nomad@v0.1.1-0.20160225204057-97751b02f70b/client/fingerprint/cgroup_test.go (about)

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