2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2023-03-28 13:27:58 +00:00
|
|
|
//go:build !darwin || !arm64 || !cgo
|
|
|
|
|
2015-08-26 21:27:44 +00:00
|
|
|
package fingerprint
|
|
|
|
|
|
|
|
import (
|
2020-12-05 01:46:51 +00:00
|
|
|
"strconv"
|
2015-08-26 21:27:44 +00:00
|
|
|
"testing"
|
|
|
|
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2015-08-26 21:27:44 +00:00
|
|
|
"github.com/hashicorp/nomad/client/config"
|
2023-06-05 19:15:00 +00:00
|
|
|
"github.com/hashicorp/nomad/client/lib/cgutil"
|
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"
|
2023-03-28 13:27:58 +00:00
|
|
|
"github.com/shoenig/test/must"
|
2015-08-26 21:27:44 +00:00
|
|
|
)
|
|
|
|
|
2023-03-28 13:27:58 +00:00
|
|
|
func TestCPUFingerprint_Classic(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
|
2023-06-05 19:15:00 +00:00
|
|
|
logger := testlog.HCLogger(t)
|
|
|
|
|
|
|
|
// create cpuset manager so we can ensure cgroup tree is correct
|
|
|
|
mgr := cgutil.CreateCPUSetManager("", nil, logger)
|
|
|
|
mgr.Init()
|
|
|
|
|
|
|
|
// create the fingerprinter
|
|
|
|
f := NewCPUFingerprint(logger)
|
2023-03-28 13:27:58 +00:00
|
|
|
node := &structs.Node{Attributes: make(map[string]string)}
|
2018-01-24 14:09:53 +00:00
|
|
|
|
2018-12-01 16:10:39 +00:00
|
|
|
request := &FingerprintRequest{Config: &config.Config{}, Node: node}
|
|
|
|
var response FingerprintResponse
|
2015-08-26 21:27:44 +00:00
|
|
|
|
2023-06-05 19:15:00 +00:00
|
|
|
// run the fingerprinter
|
2023-03-28 13:27:58 +00:00
|
|
|
err := f.Fingerprint(request, &response)
|
|
|
|
must.NoError(t, err)
|
2018-01-30 17:57:37 +00:00
|
|
|
|
2023-03-28 13:27:58 +00:00
|
|
|
must.True(t, response.Detected)
|
2018-01-30 17:57:37 +00:00
|
|
|
attributes := response.Attributes
|
2023-03-28 13:27:58 +00:00
|
|
|
must.NotNil(t, attributes)
|
|
|
|
must.MapContainsKey(t, attributes, "cpu.numcores")
|
|
|
|
must.MapContainsKey(t, attributes, "cpu.modelname")
|
|
|
|
must.MapContainsKey(t, attributes, "cpu.frequency")
|
|
|
|
must.MapContainsKey(t, attributes, "cpu.totalcompute")
|
|
|
|
must.Positive(t, response.Resources.CPU)
|
|
|
|
must.Positive(t, response.NodeResources.Cpu.CpuShares)
|
|
|
|
must.Positive(t, response.NodeResources.Cpu.SharesPerCore())
|
|
|
|
must.SliceNotEmpty(t, response.NodeResources.Cpu.ReservableCpuCores)
|
|
|
|
|
|
|
|
// asymetric core detection currently only works with apple silicon
|
|
|
|
must.MapNotContainsKey(t, attributes, "cpu.numcores.power")
|
|
|
|
must.MapNotContainsKey(t, attributes, "cpu.numcores.efficiency")
|
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) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
|
2018-09-16 00:48:59 +00:00
|
|
|
f := NewCPUFingerprint(testlog.HCLogger(t))
|
2017-06-27 18:56:52 +00:00
|
|
|
node := &structs.Node{
|
|
|
|
Attributes: make(map[string]string),
|
|
|
|
}
|
2022-09-26 20:03:03 +00:00
|
|
|
cfg := &config.Config{
|
|
|
|
ReservableCores: []uint16{0, 1, 2},
|
|
|
|
}
|
2018-01-24 14:09:53 +00:00
|
|
|
var originalCPU int
|
2017-06-27 18:56:52 +00:00
|
|
|
|
2018-01-24 14:09:53 +00:00
|
|
|
{
|
2018-12-01 16:10:39 +00:00
|
|
|
request := &FingerprintRequest{Config: cfg, Node: node}
|
|
|
|
var response FingerprintResponse
|
2018-01-26 16:21:07 +00:00
|
|
|
err := f.Fingerprint(request, &response)
|
2018-01-24 14:09:53 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2018-01-26 16:21:07 +00:00
|
|
|
|
2018-01-31 22:03:55 +00:00
|
|
|
if !response.Detected {
|
2018-01-30 17:57:37 +00:00
|
|
|
t.Fatalf("expected response to be applicable")
|
|
|
|
}
|
|
|
|
|
2022-09-26 20:03:03 +00:00
|
|
|
if attr := response.Attributes["cpu.reservablecores"]; attr != "3" {
|
|
|
|
t.Fatalf("expected cpu.reservablecores == 3 but found %s", attr)
|
|
|
|
}
|
|
|
|
|
2018-01-30 17:57:37 +00:00
|
|
|
if response.Resources.CPU == 0 {
|
2018-01-24 14:09:53 +00:00
|
|
|
t.Fatalf("expected fingerprint of cpu of but found 0")
|
|
|
|
}
|
2017-06-27 18:56:52 +00:00
|
|
|
|
2018-01-30 17:57:37 +00:00
|
|
|
originalCPU = response.Resources.CPU
|
2017-06-27 18:56:52 +00:00
|
|
|
}
|
|
|
|
|
2018-01-24 14:09:53 +00:00
|
|
|
{
|
|
|
|
// Override it with a setting
|
|
|
|
cfg.CpuCompute = originalCPU + 123
|
|
|
|
|
|
|
|
// Make sure the Fingerprinter applies the override to the node resources
|
2018-12-01 16:10:39 +00:00
|
|
|
request := &FingerprintRequest{Config: cfg, Node: node}
|
|
|
|
var response FingerprintResponse
|
2018-01-26 16:21:07 +00:00
|
|
|
err := f.Fingerprint(request, &response)
|
2018-01-24 14:09:53 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-09-30 00:23:41 +00:00
|
|
|
// COMPAT(0.10): Remove in 0.10
|
2018-01-30 17:57:37 +00:00
|
|
|
if response.Resources.CPU != cfg.CpuCompute {
|
|
|
|
t.Fatalf("expected override cpu of %d but found %d", cfg.CpuCompute, response.Resources.CPU)
|
2018-01-24 14:09:53 +00:00
|
|
|
}
|
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
|
|
|
}
|
2020-12-05 01:46:51 +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"])
|
|
|
|
}
|
2022-09-26 20:03:03 +00:00
|
|
|
|
|
|
|
if attr := response.Attributes["cpu.reservablecores"]; attr != "3" {
|
|
|
|
t.Fatalf("expected cpu.reservablecores == 3 but found %s", attr)
|
|
|
|
}
|
2017-06-27 18:56:52 +00:00
|
|
|
}
|
|
|
|
}
|