2016-01-21 01:30:02 +00:00
|
|
|
package structs
|
|
|
|
|
2016-01-26 18:07:33 +00:00
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2017-09-29 16:58:48 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
2018-11-16 23:29:59 +00:00
|
|
|
psstructs "github.com/hashicorp/nomad/plugins/shared/structs"
|
|
|
|
"github.com/stretchr/testify/require"
|
2016-01-26 18:07:33 +00:00
|
|
|
)
|
2016-01-21 01:30:02 +00:00
|
|
|
|
2018-11-16 23:29:59 +00:00
|
|
|
// TODO Test
|
2016-01-21 01:30:02 +00:00
|
|
|
func testNode() *Node {
|
|
|
|
return &Node{
|
2017-09-29 16:58:48 +00:00
|
|
|
ID: uuid.Generate(),
|
2016-01-21 01:30:02 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
Name: "foobar",
|
|
|
|
Attributes: map[string]string{
|
|
|
|
"kernel.name": "linux",
|
|
|
|
"arch": "x86",
|
|
|
|
"version": "0.1.0",
|
|
|
|
"driver.exec": "1",
|
|
|
|
},
|
2018-10-03 16:47:18 +00:00
|
|
|
NodeResources: &NodeResources{
|
|
|
|
Cpu: NodeCpuResources{
|
2018-10-04 21:33:09 +00:00
|
|
|
CpuShares: 4000,
|
2018-10-03 16:47:18 +00:00
|
|
|
},
|
|
|
|
Memory: NodeMemoryResources{
|
|
|
|
MemoryMB: 8192,
|
|
|
|
},
|
|
|
|
Disk: NodeDiskResources{
|
|
|
|
DiskMB: 100 * 1024,
|
|
|
|
},
|
2016-01-21 01:30:02 +00:00
|
|
|
Networks: []*NetworkResource{
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2016-01-21 01:30:02 +00:00
|
|
|
Device: "eth0",
|
|
|
|
CIDR: "192.168.0.100/32",
|
2018-10-03 16:47:18 +00:00
|
|
|
IP: "192.168.0.100",
|
2016-01-21 01:30:02 +00:00
|
|
|
MBits: 1000,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Links: map[string]string{
|
|
|
|
"consul": "foobar.dc1",
|
|
|
|
},
|
|
|
|
Meta: map[string]string{
|
|
|
|
"pci-dss": "true",
|
|
|
|
},
|
|
|
|
NodeClass: "linux-medium-pci",
|
|
|
|
Status: NodeStatusReady,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNode_ComputedClass(t *testing.T) {
|
2018-11-16 23:29:59 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
2016-01-21 01:30:02 +00:00
|
|
|
// Create a node and gets it computed class
|
|
|
|
n := testNode()
|
2018-11-16 23:29:59 +00:00
|
|
|
require.NoError(n.ComputeClass())
|
|
|
|
require.NotEmpty(n.ComputedClass)
|
2016-01-21 01:30:02 +00:00
|
|
|
old := n.ComputedClass
|
|
|
|
|
|
|
|
// Compute again to ensure determinism
|
2018-11-16 23:29:59 +00:00
|
|
|
require.NoError(n.ComputeClass())
|
|
|
|
require.Equal(n.ComputedClass, old)
|
2016-01-21 01:30:02 +00:00
|
|
|
|
|
|
|
// Modify a field and compute the class again.
|
|
|
|
n.Datacenter = "New DC"
|
2018-11-16 23:29:59 +00:00
|
|
|
require.NoError(n.ComputeClass())
|
|
|
|
require.NotEqual(n.ComputedClass, old)
|
|
|
|
old = n.ComputedClass
|
2016-01-21 01:30:02 +00:00
|
|
|
|
2018-11-16 23:29:59 +00:00
|
|
|
// Add a device
|
|
|
|
n.NodeResources.Devices = append(n.NodeResources.Devices, &NodeDeviceResource{
|
|
|
|
Vendor: "foo",
|
|
|
|
Type: "gpu",
|
|
|
|
Name: "bam",
|
|
|
|
})
|
|
|
|
require.NoError(n.ComputeClass())
|
|
|
|
require.NotEqual(n.ComputedClass, old)
|
2016-01-21 01:30:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNode_ComputedClass_Ignore(t *testing.T) {
|
2018-11-16 23:29:59 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
2016-01-21 01:30:02 +00:00
|
|
|
// Create a node and gets it computed class
|
|
|
|
n := testNode()
|
2018-11-16 23:29:59 +00:00
|
|
|
require.NoError(n.ComputeClass())
|
|
|
|
require.NotEmpty(n.ComputedClass)
|
2016-01-21 01:30:02 +00:00
|
|
|
old := n.ComputedClass
|
|
|
|
|
|
|
|
// Modify an ignored field and compute the class again.
|
|
|
|
n.ID = "New ID"
|
2018-11-16 23:29:59 +00:00
|
|
|
require.NoError(n.ComputeClass())
|
|
|
|
require.NotEmpty(n.ComputedClass)
|
|
|
|
require.Equal(n.ComputedClass, old)
|
2016-01-21 01:30:02 +00:00
|
|
|
|
2018-11-16 23:29:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNode_ComputedClass_Device_Attr(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
// Create a node and gets it computed class
|
|
|
|
n := testNode()
|
|
|
|
d := &NodeDeviceResource{
|
|
|
|
Vendor: "foo",
|
|
|
|
Type: "gpu",
|
|
|
|
Name: "bam",
|
|
|
|
Attributes: map[string]*psstructs.Attribute{
|
|
|
|
"foo": psstructs.NewBoolAttribute(true),
|
|
|
|
},
|
2016-01-21 01:30:02 +00:00
|
|
|
}
|
2018-11-16 23:29:59 +00:00
|
|
|
n.NodeResources.Devices = append(n.NodeResources.Devices, d)
|
|
|
|
require.NoError(n.ComputeClass())
|
|
|
|
require.NotEmpty(n.ComputedClass)
|
|
|
|
old := n.ComputedClass
|
|
|
|
|
|
|
|
// Update the attributes to be have a unique value
|
|
|
|
d.Attributes["unique.bar"] = psstructs.NewBoolAttribute(false)
|
|
|
|
require.NoError(n.ComputeClass())
|
|
|
|
require.Equal(n.ComputedClass, old)
|
2016-01-21 01:30:02 +00:00
|
|
|
}
|
|
|
|
|
2016-01-21 19:25:14 +00:00
|
|
|
func TestNode_ComputedClass_Attr(t *testing.T) {
|
|
|
|
// Create a node and gets it computed class
|
|
|
|
n := testNode()
|
2016-01-21 01:30:02 +00:00
|
|
|
if err := n.ComputeClass(); err != nil {
|
|
|
|
t.Fatalf("ComputeClass() failed: %v", err)
|
|
|
|
}
|
2016-01-30 01:46:44 +00:00
|
|
|
if n.ComputedClass == "" {
|
2016-01-21 01:30:02 +00:00
|
|
|
t.Fatal("ComputeClass() didn't set computed class")
|
|
|
|
}
|
|
|
|
old := n.ComputedClass
|
|
|
|
|
2016-01-21 19:25:14 +00:00
|
|
|
// Add a unique addr and compute the class again
|
2016-01-23 02:12:16 +00:00
|
|
|
n.Attributes["unique.foo"] = "bar"
|
2016-01-21 01:30:02 +00:00
|
|
|
if err := n.ComputeClass(); err != nil {
|
|
|
|
t.Fatalf("ComputeClass() failed: %v", err)
|
|
|
|
}
|
|
|
|
if old != n.ComputedClass {
|
2016-01-21 19:25:14 +00:00
|
|
|
t.Fatal("ComputeClass() didn't ignore unique attr suffix")
|
2016-01-21 01:30:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Modify an attribute and compute the class again.
|
|
|
|
n.Attributes["version"] = "New Version"
|
|
|
|
if err := n.ComputeClass(); err != nil {
|
|
|
|
t.Fatalf("ComputeClass() failed: %v", err)
|
|
|
|
}
|
2016-01-30 01:46:44 +00:00
|
|
|
if n.ComputedClass == "" {
|
2016-01-21 01:30:02 +00:00
|
|
|
t.Fatal("ComputeClass() didn't set computed class")
|
|
|
|
}
|
|
|
|
if old == n.ComputedClass {
|
|
|
|
t.Fatal("ComputeClass() ignored attribute change")
|
|
|
|
}
|
2016-04-01 01:11:27 +00:00
|
|
|
|
|
|
|
// Remove and attribute and compute the class again.
|
2016-01-21 01:30:02 +00:00
|
|
|
old = n.ComputedClass
|
2016-04-01 01:11:27 +00:00
|
|
|
delete(n.Attributes, "driver.exec")
|
|
|
|
if err := n.ComputeClass(); err != nil {
|
|
|
|
t.Fatalf("ComputedClass() failed: %v", err)
|
|
|
|
}
|
|
|
|
if n.ComputedClass == "" {
|
|
|
|
t.Fatal("ComputeClass() didn't set computed class")
|
|
|
|
}
|
|
|
|
if old == n.ComputedClass {
|
|
|
|
t.Fatalf("ComputedClass() ignored removal of attribute key")
|
|
|
|
}
|
2016-01-21 01:30:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNode_ComputedClass_Meta(t *testing.T) {
|
|
|
|
// Create a node and gets it computed class
|
|
|
|
n := testNode()
|
|
|
|
if err := n.ComputeClass(); err != nil {
|
|
|
|
t.Fatalf("ComputeClass() failed: %v", err)
|
|
|
|
}
|
2016-01-30 01:46:44 +00:00
|
|
|
if n.ComputedClass == "" {
|
2016-01-21 01:30:02 +00:00
|
|
|
t.Fatal("ComputeClass() didn't set computed class")
|
|
|
|
}
|
|
|
|
old := n.ComputedClass
|
|
|
|
|
|
|
|
// Modify a meta key and compute the class again.
|
|
|
|
n.Meta["pci-dss"] = "false"
|
|
|
|
if err := n.ComputeClass(); err != nil {
|
|
|
|
t.Fatalf("ComputeClass() failed: %v", err)
|
|
|
|
}
|
2016-01-30 01:46:44 +00:00
|
|
|
if n.ComputedClass == "" {
|
2016-01-21 01:30:02 +00:00
|
|
|
t.Fatal("ComputeClass() didn't set computed class")
|
|
|
|
}
|
|
|
|
if old == n.ComputedClass {
|
|
|
|
t.Fatal("ComputeClass() ignored meta change")
|
|
|
|
}
|
|
|
|
old = n.ComputedClass
|
|
|
|
|
|
|
|
// Add a unique meta key and compute the class again.
|
2016-01-23 02:12:16 +00:00
|
|
|
n.Meta["unique.foo"] = "ignore"
|
2016-01-21 01:30:02 +00:00
|
|
|
if err := n.ComputeClass(); err != nil {
|
|
|
|
t.Fatalf("ComputeClass() failed: %v", err)
|
|
|
|
}
|
2016-01-30 01:46:44 +00:00
|
|
|
if n.ComputedClass == "" {
|
2016-01-21 01:30:02 +00:00
|
|
|
t.Fatal("ComputeClass() didn't set computed class")
|
|
|
|
}
|
|
|
|
if old != n.ComputedClass {
|
|
|
|
t.Fatal("ComputeClass() didn't ignore unique meta key")
|
|
|
|
}
|
|
|
|
}
|
2016-01-26 18:07:33 +00:00
|
|
|
|
|
|
|
func TestNode_EscapedConstraints(t *testing.T) {
|
|
|
|
// Non-escaped constraints
|
|
|
|
ne1 := &Constraint{
|
2016-02-05 00:50:20 +00:00
|
|
|
LTarget: "${attr.kernel.name}",
|
2016-01-26 18:07:33 +00:00
|
|
|
RTarget: "linux",
|
|
|
|
Operand: "=",
|
|
|
|
}
|
|
|
|
ne2 := &Constraint{
|
2016-02-05 00:50:20 +00:00
|
|
|
LTarget: "${meta.key_foo}",
|
2016-01-26 18:07:33 +00:00
|
|
|
RTarget: "linux",
|
|
|
|
Operand: "<",
|
|
|
|
}
|
|
|
|
ne3 := &Constraint{
|
2016-02-05 00:50:20 +00:00
|
|
|
LTarget: "${node.dc}",
|
2016-01-26 18:07:33 +00:00
|
|
|
RTarget: "test",
|
|
|
|
Operand: "!=",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Escaped constraints
|
|
|
|
e1 := &Constraint{
|
2016-02-05 00:50:20 +00:00
|
|
|
LTarget: "${attr.unique.kernel.name}",
|
2016-01-26 18:07:33 +00:00
|
|
|
RTarget: "linux",
|
|
|
|
Operand: "=",
|
|
|
|
}
|
|
|
|
e2 := &Constraint{
|
2016-02-05 00:50:20 +00:00
|
|
|
LTarget: "${meta.unique.key_foo}",
|
2016-01-26 18:07:33 +00:00
|
|
|
RTarget: "linux",
|
|
|
|
Operand: "<",
|
|
|
|
}
|
|
|
|
e3 := &Constraint{
|
2016-02-05 00:50:20 +00:00
|
|
|
LTarget: "${unique.node.id}",
|
2016-01-26 18:07:33 +00:00
|
|
|
RTarget: "test",
|
|
|
|
Operand: "!=",
|
|
|
|
}
|
|
|
|
constraints := []*Constraint{ne1, ne2, ne3, e1, e2, e3}
|
|
|
|
expected := []*Constraint{ne1, ne2, ne3}
|
|
|
|
if act := EscapedConstraints(constraints); reflect.DeepEqual(act, expected) {
|
|
|
|
t.Fatalf("EscapedConstraints(%v) returned %v; want %v", constraints, act, expected)
|
|
|
|
}
|
|
|
|
}
|