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