github.com/anuvu/nomad@v0.8.7-atom1/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/nomad/structs"
     9  )
    10  
    11  func TestCPUFingerprint(t *testing.T) {
    12  	f := NewCPUFingerprint(testLogger())
    13  	node := &structs.Node{
    14  		Attributes: make(map[string]string),
    15  	}
    16  
    17  	request := &cstructs.FingerprintRequest{Config: &config.Config{}, Node: node}
    18  	var response cstructs.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  	if response.Resources == nil || response.Resources.CPU == 0 {
    48  		t.Fatalf("Expected to find CPU Resources")
    49  	}
    50  }
    51  
    52  // TestCPUFingerprint_OverrideCompute asserts that setting cpu_total_compute in
    53  // the client config overrides the detected CPU freq (if any).
    54  func TestCPUFingerprint_OverrideCompute(t *testing.T) {
    55  	f := NewCPUFingerprint(testLogger())
    56  	node := &structs.Node{
    57  		Attributes: make(map[string]string),
    58  	}
    59  	cfg := &config.Config{}
    60  	var originalCPU int
    61  
    62  	{
    63  		request := &cstructs.FingerprintRequest{Config: cfg, Node: node}
    64  		var response cstructs.FingerprintResponse
    65  		err := f.Fingerprint(request, &response)
    66  		if err != nil {
    67  			t.Fatalf("err: %v", err)
    68  		}
    69  
    70  		if !response.Detected {
    71  			t.Fatalf("expected response to be applicable")
    72  		}
    73  
    74  		if response.Resources.CPU == 0 {
    75  			t.Fatalf("expected fingerprint of cpu of but found 0")
    76  		}
    77  
    78  		originalCPU = response.Resources.CPU
    79  	}
    80  
    81  	{
    82  		// Override it with a setting
    83  		cfg.CpuCompute = originalCPU + 123
    84  
    85  		// Make sure the Fingerprinter applies the override to the node resources
    86  		request := &cstructs.FingerprintRequest{Config: cfg, Node: node}
    87  		var response cstructs.FingerprintResponse
    88  		err := f.Fingerprint(request, &response)
    89  		if err != nil {
    90  			t.Fatalf("err: %v", err)
    91  		}
    92  
    93  		if response.Resources.CPU != cfg.CpuCompute {
    94  			t.Fatalf("expected override cpu of %d but found %d", cfg.CpuCompute, response.Resources.CPU)
    95  		}
    96  	}
    97  }