2015-08-26 21:27:44 +00:00
|
|
|
package fingerprint
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/client/config"
|
2018-01-24 14:09:53 +00:00
|
|
|
cstructs "github.com/hashicorp/nomad/client/structs"
|
2015-08-26 21:27:44 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCPUFingerprint(t *testing.T) {
|
|
|
|
f := NewCPUFingerprint(testLogger())
|
|
|
|
node := &structs.Node{
|
|
|
|
Attributes: make(map[string]string),
|
|
|
|
}
|
2018-01-24 14:09:53 +00:00
|
|
|
|
|
|
|
request := &cstructs.FingerprintRequest{Config: &config.Config{}, Node: node}
|
2018-01-26 16:21:07 +00:00
|
|
|
var response cstructs.FingerprintResponse
|
|
|
|
err := f.Fingerprint(request, &response)
|
2015-08-26 21:27:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CPU info
|
2018-01-26 16:21:07 +00:00
|
|
|
attributes := response.GetAttributes()
|
|
|
|
if attributes["cpu.numcores"] == "" {
|
2015-08-26 21:27:44 +00:00
|
|
|
t.Fatalf("Missing Num Cores")
|
|
|
|
}
|
2018-01-26 16:21:07 +00:00
|
|
|
if attributes["cpu.modelname"] == "" {
|
2015-08-26 21:27:44 +00:00
|
|
|
t.Fatalf("Missing Model Name")
|
|
|
|
}
|
|
|
|
|
2018-01-26 16:21:07 +00:00
|
|
|
if attributes["cpu.frequency"] == "" {
|
2015-08-27 14:19:53 +00:00
|
|
|
t.Fatalf("Missing CPU Frequency")
|
|
|
|
}
|
2018-01-26 16:21:07 +00:00
|
|
|
if attributes["cpu.totalcompute"] == "" {
|
2015-08-27 14:19:53 +00:00
|
|
|
t.Fatalf("Missing CPU Total Compute")
|
|
|
|
}
|
|
|
|
|
2018-01-26 16:21:07 +00:00
|
|
|
resources := response.GetResources()
|
|
|
|
if 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
|
|
|
}
|
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) {
|
|
|
|
f := NewCPUFingerprint(testLogger())
|
|
|
|
node := &structs.Node{
|
|
|
|
Attributes: make(map[string]string),
|
|
|
|
}
|
|
|
|
cfg := &config.Config{}
|
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
|
|
|
{
|
|
|
|
request := &cstructs.FingerprintRequest{Config: cfg, Node: node}
|
2018-01-26 16:21:07 +00:00
|
|
|
var response cstructs.FingerprintResponse
|
|
|
|
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
|
|
|
|
|
|
|
resources := response.GetResources()
|
|
|
|
if 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-26 16:21:07 +00:00
|
|
|
originalCPU = 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
|
|
|
|
request := &cstructs.FingerprintRequest{Config: cfg, Node: node}
|
2018-01-26 16:21:07 +00:00
|
|
|
var response cstructs.FingerprintResponse
|
|
|
|
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
|
|
|
resources := response.GetResources()
|
|
|
|
if resources.CPU != cfg.CpuCompute {
|
|
|
|
t.Fatalf("expected override cpu of %d but found %d", cfg.CpuCompute, resources.CPU)
|
2018-01-24 14:09:53 +00:00
|
|
|
}
|
2017-06-27 18:56:52 +00:00
|
|
|
}
|
|
|
|
}
|