github.com/zhizhiboom/nomad@v0.8.5-0.20180907175415-f28fd3a1a056/client/fingerprint/cpu_test.go (about)

     1  package fingerprint
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/nomad/client/config"
     7  	cstructs "github.com/hashicorp/nomad/client/structs"
     8  	"github.com/hashicorp/nomad/helper/testlog"
     9  	"github.com/hashicorp/nomad/nomad/structs"
    10  )
    11  
    12  func TestCPUFingerprint(t *testing.T) {
    13  	f := NewCPUFingerprint(testlog.Logger(t))
    14  	node := &structs.Node{
    15  		Attributes: make(map[string]string),
    16  	}
    17  
    18  	request := &cstructs.FingerprintRequest{Config: &config.Config{}, Node: node}
    19  	var response cstructs.FingerprintResponse
    20  	err := f.Fingerprint(request, &response)
    21  	if err != nil {
    22  		t.Fatalf("err: %v", err)
    23  	}
    24  
    25  	if !response.Detected {
    26  		t.Fatalf("expected response to be applicable")
    27  	}
    28  
    29  	// CPU info
    30  	attributes := response.Attributes
    31  	if attributes == nil {
    32  		t.Fatalf("expected attributes to be initialized")
    33  	}
    34  	if attributes["cpu.numcores"] == "" {
    35  		t.Fatalf("Missing Num Cores")
    36  	}
    37  	if attributes["cpu.modelname"] == "" {
    38  		t.Fatalf("Missing Model Name")
    39  	}
    40  
    41  	if attributes["cpu.frequency"] == "" {
    42  		t.Fatalf("Missing CPU Frequency")
    43  	}
    44  	if attributes["cpu.totalcompute"] == "" {
    45  		t.Fatalf("Missing CPU Total Compute")
    46  	}
    47  
    48  	if response.Resources == nil || response.Resources.CPU == 0 {
    49  		t.Fatalf("Expected to find CPU Resources")
    50  	}
    51  }
    52  
    53  // TestCPUFingerprint_OverrideCompute asserts that setting cpu_total_compute in
    54  // the client config overrides the detected CPU freq (if any).
    55  func TestCPUFingerprint_OverrideCompute(t *testing.T) {
    56  	f := NewCPUFingerprint(testlog.Logger(t))
    57  	node := &structs.Node{
    58  		Attributes: make(map[string]string),
    59  	}
    60  	cfg := &config.Config{}
    61  	var originalCPU int
    62  
    63  	{
    64  		request := &cstructs.FingerprintRequest{Config: cfg, Node: node}
    65  		var response cstructs.FingerprintResponse
    66  		err := f.Fingerprint(request, &response)
    67  		if err != nil {
    68  			t.Fatalf("err: %v", err)
    69  		}
    70  
    71  		if !response.Detected {
    72  			t.Fatalf("expected response to be applicable")
    73  		}
    74  
    75  		if response.Resources.CPU == 0 {
    76  			t.Fatalf("expected fingerprint of cpu of but found 0")
    77  		}
    78  
    79  		originalCPU = response.Resources.CPU
    80  	}
    81  
    82  	{
    83  		// Override it with a setting
    84  		cfg.CpuCompute = originalCPU + 123
    85  
    86  		// Make sure the Fingerprinter applies the override to the node resources
    87  		request := &cstructs.FingerprintRequest{Config: cfg, Node: node}
    88  		var response cstructs.FingerprintResponse
    89  		err := f.Fingerprint(request, &response)
    90  		if err != nil {
    91  			t.Fatalf("err: %v", err)
    92  		}
    93  
    94  		if response.Resources.CPU != cfg.CpuCompute {
    95  			t.Fatalf("expected override cpu of %d but found %d", cfg.CpuCompute, response.Resources.CPU)
    96  		}
    97  	}
    98  }