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