github.com/bigcommerce/nomad@v0.9.3-bc/client/fingerprint/cpu_test.go (about)

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