Files
nomad/client/fingerprint/nomad_test.go
Tim Gross 1788bfb42e remove addresses from node class hash (#24942)
When a node is fingerprinted, we calculate a "computed class" from a hash over a
subset of its fields and attributes. In the scheduler, when a given node fails
feasibility checking (before fit checking) we know that no other node of that
same class will be feasible, and we add the hash to a map so we can reject them
early. This hash cannot include any values that are unique to a given node,
otherwise no other node will have the same hash and we'll never save ourselves
the work of feasibility checking those nodes.

In #4390 we introduce the `nomad.advertise.address` attribute and in #19969 we
introduced `consul.dns.addr` attribute. Both of these are unique per node and
break the hash.

Additionally, we were not correctly filtering attributes out when checking if a
node escaped the class by not filtering for attributes that start with
`unique.`. The test for this introduced in #708 had an inverted assertion, which
allowed this to pass unnoticed since the early days of Nomad.

Ref: https://github.com/hashicorp/nomad/pull/708
Ref: https://github.com/hashicorp/nomad/pull/4390
Ref: https://github.com/hashicorp/nomad/pull/19969
2025-03-03 09:28:32 -05:00

67 lines
1.4 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package fingerprint
import (
"testing"
"github.com/hashicorp/nomad/ci"
"github.com/hashicorp/nomad/client/config"
"github.com/hashicorp/nomad/helper/testlog"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/version"
"github.com/stretchr/testify/require"
)
func TestNomadFingerprint(t *testing.T) {
ci.Parallel(t)
f := NewNomadFingerprint(testlog.HCLogger(t))
v := "foo"
r := "123"
h := "8.8.8.8:4646"
c := &config.Config{
Version: &version.VersionInfo{
Revision: r,
Version: v,
},
NomadServiceDiscovery: true,
}
node := &structs.Node{
Attributes: make(map[string]string),
HTTPAddr: h,
}
request := &FingerprintRequest{Config: c, Node: node}
var response FingerprintResponse
err := f.Fingerprint(request, &response)
if err != nil {
t.Fatalf("err: %v", err)
}
if !response.Detected {
t.Fatalf("expected response to be applicable")
}
if len(response.Attributes) == 0 {
t.Fatalf("should apply")
}
if response.Attributes["nomad.version"] != v {
t.Fatalf("incorrect version")
}
if response.Attributes["nomad.revision"] != r {
t.Fatalf("incorrect revision")
}
if response.Attributes["unique.advertise.address"] != h {
t.Fatalf("incorrect advertise address")
}
serviceDisco := response.Attributes["nomad.service_discovery"]
require.Equal(t, "true", serviceDisco, "service_discovery attr incorrect")
}