open-nomad/client/fingerprint/cpu_test.go

116 lines
3 KiB
Go
Raw Normal View History

2015-08-26 21:27:44 +00:00
package fingerprint
import (
"strconv"
2015-08-26 21:27:44 +00:00
"testing"
"github.com/hashicorp/nomad/ci"
2015-08-26 21:27:44 +00:00
"github.com/hashicorp/nomad/client/config"
2018-06-13 22:33:25 +00:00
"github.com/hashicorp/nomad/helper/testlog"
2015-08-26 21:27:44 +00:00
"github.com/hashicorp/nomad/nomad/structs"
)
func TestCPUFingerprint(t *testing.T) {
ci.Parallel(t)
f := NewCPUFingerprint(testlog.HCLogger(t))
2015-08-26 21:27:44 +00:00
node := &structs.Node{
Attributes: make(map[string]string),
}
request := &FingerprintRequest{Config: &config.Config{}, Node: node}
var response FingerprintResponse
err := f.Fingerprint(request, &response)
2015-08-26 21:27:44 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
2018-01-31 22:03:55 +00:00
if !response.Detected {
t.Fatalf("expected response to be applicable")
}
2015-08-26 21:27:44 +00:00
// CPU info
attributes := response.Attributes
if attributes == nil {
t.Fatalf("expected attributes to be initialized")
}
if attributes["cpu.numcores"] == "" {
2015-08-26 21:27:44 +00:00
t.Fatalf("Missing Num Cores")
}
if attributes["cpu.modelname"] == "" {
2015-08-26 21:27:44 +00:00
t.Fatalf("Missing Model Name")
}
if attributes["cpu.frequency"] == "" {
2015-08-27 14:19:53 +00:00
t.Fatalf("Missing CPU Frequency")
}
if attributes["cpu.totalcompute"] == "" {
2015-08-27 14:19:53 +00:00
t.Fatalf("Missing CPU Total Compute")
}
2018-09-30 00:23:41 +00:00
// COMPAT(0.10): Remove in 0.10
if response.Resources == nil || response.Resources.CPU == 0 {
2015-11-07 14:14:25 +00:00
t.Fatalf("Expected to find CPU Resources")
2015-08-27 19:15:56 +00:00
}
2018-09-30 00:23:41 +00:00
2018-10-04 21:33:09 +00:00
if response.NodeResources == nil || response.NodeResources.Cpu.CpuShares == 0 {
2018-09-30 00:23:41 +00:00
t.Fatalf("Expected to find CPU Resources")
}
2015-08-26 21:27:44 +00:00
}
2017-06-27 18:56:52 +00:00
// TestCPUFingerprint_OverrideCompute asserts that setting cpu_total_compute in
// the client config overrides the detected CPU freq (if any).
func TestCPUFingerprint_OverrideCompute(t *testing.T) {
ci.Parallel(t)
f := NewCPUFingerprint(testlog.HCLogger(t))
2017-06-27 18:56:52 +00:00
node := &structs.Node{
Attributes: make(map[string]string),
}
cfg := &config.Config{}
var originalCPU int
2017-06-27 18:56:52 +00:00
{
request := &FingerprintRequest{Config: cfg, Node: node}
var response FingerprintResponse
err := f.Fingerprint(request, &response)
if err != nil {
t.Fatalf("err: %v", err)
}
2018-01-31 22:03:55 +00:00
if !response.Detected {
t.Fatalf("expected response to be applicable")
}
if response.Resources.CPU == 0 {
t.Fatalf("expected fingerprint of cpu of but found 0")
}
2017-06-27 18:56:52 +00:00
originalCPU = response.Resources.CPU
2017-06-27 18:56:52 +00:00
}
{
// Override it with a setting
cfg.CpuCompute = originalCPU + 123
// Make sure the Fingerprinter applies the override to the node resources
request := &FingerprintRequest{Config: cfg, Node: node}
var response FingerprintResponse
err := f.Fingerprint(request, &response)
if err != nil {
t.Fatalf("err: %v", err)
}
2018-09-30 00:23:41 +00:00
// COMPAT(0.10): Remove in 0.10
if response.Resources.CPU != cfg.CpuCompute {
t.Fatalf("expected override cpu of %d but found %d", cfg.CpuCompute, response.Resources.CPU)
}
2018-10-16 22:34:32 +00:00
if response.NodeResources.Cpu.CpuShares != int64(cfg.CpuCompute) {
2018-10-04 21:33:09 +00:00
t.Fatalf("expected override cpu of %d but found %d", cfg.CpuCompute, response.NodeResources.Cpu.CpuShares)
2018-09-30 00:23:41 +00:00
}
if response.Attributes["cpu.totalcompute"] != strconv.Itoa(cfg.CpuCompute) {
t.Fatalf("expected override cpu.totalcompute of %d but found %s", cfg.CpuCompute, response.Attributes["cpu.totalcompute"])
}
2017-06-27 18:56:52 +00:00
}
}