github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/client/fingerprint/cpu_test.go (about) 1 package fingerprint 2 3 import ( 4 "strconv" 5 "testing" 6 7 "github.com/hashicorp/nomad/client/config" 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.HCLogger(t)) 14 node := &structs.Node{ 15 Attributes: make(map[string]string), 16 } 17 18 request := &FingerprintRequest{Config: &config.Config{}, Node: node} 19 var response 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 // COMPAT(0.10): Remove in 0.10 49 if response.Resources == nil || response.Resources.CPU == 0 { 50 t.Fatalf("Expected to find CPU Resources") 51 } 52 53 if response.NodeResources == nil || response.NodeResources.Cpu.CpuShares == 0 { 54 t.Fatalf("Expected to find CPU Resources") 55 } 56 } 57 58 // TestCPUFingerprint_OverrideCompute asserts that setting cpu_total_compute in 59 // the client config overrides the detected CPU freq (if any). 60 func TestCPUFingerprint_OverrideCompute(t *testing.T) { 61 f := NewCPUFingerprint(testlog.HCLogger(t)) 62 node := &structs.Node{ 63 Attributes: make(map[string]string), 64 } 65 cfg := &config.Config{} 66 var originalCPU int 67 68 { 69 request := &FingerprintRequest{Config: cfg, Node: node} 70 var response FingerprintResponse 71 err := f.Fingerprint(request, &response) 72 if err != nil { 73 t.Fatalf("err: %v", err) 74 } 75 76 if !response.Detected { 77 t.Fatalf("expected response to be applicable") 78 } 79 80 if response.Resources.CPU == 0 { 81 t.Fatalf("expected fingerprint of cpu of but found 0") 82 } 83 84 originalCPU = response.Resources.CPU 85 } 86 87 { 88 // Override it with a setting 89 cfg.CpuCompute = originalCPU + 123 90 91 // Make sure the Fingerprinter applies the override to the node resources 92 request := &FingerprintRequest{Config: cfg, Node: node} 93 var response FingerprintResponse 94 err := f.Fingerprint(request, &response) 95 if err != nil { 96 t.Fatalf("err: %v", err) 97 } 98 99 // COMPAT(0.10): Remove in 0.10 100 if response.Resources.CPU != cfg.CpuCompute { 101 t.Fatalf("expected override cpu of %d but found %d", cfg.CpuCompute, response.Resources.CPU) 102 } 103 if response.NodeResources.Cpu.CpuShares != int64(cfg.CpuCompute) { 104 t.Fatalf("expected override cpu of %d but found %d", cfg.CpuCompute, response.NodeResources.Cpu.CpuShares) 105 } 106 if response.Attributes["cpu.totalcompute"] != strconv.Itoa(cfg.CpuCompute) { 107 t.Fatalf("expected override cpu.totalcompute of %d but found %s", cfg.CpuCompute, response.Attributes["cpu.totalcompute"]) 108 } 109 } 110 }