2018-11-14 16:51:50 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/api"
|
|
|
|
"github.com/hashicorp/nomad/helper"
|
2018-11-15 19:44:13 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-11-14 16:51:50 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDeviceQualifiedID(t *testing.T) {
|
|
|
|
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
require.Equal("vendor/type/name[id]", deviceQualifiedID("vendor", "type", "name", "id"))
|
|
|
|
require.Equal("vendor/type[id]", deviceQualifiedID("vendor", "type", "", "id"))
|
|
|
|
require.Equal("vendor[id]", deviceQualifiedID("vendor", "", "", "id"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuildDeviceStatsSummaryMap(t *testing.T) {
|
2018-11-15 19:44:13 +00:00
|
|
|
hostDeviceStats := []*api.DeviceGroupStats{
|
|
|
|
{
|
|
|
|
Vendor: "vendor1",
|
|
|
|
Type: "type1",
|
|
|
|
Name: "name1",
|
|
|
|
InstanceStats: map[string]*api.DeviceStats{
|
|
|
|
"id1": {
|
|
|
|
Summary: &api.StatValue{
|
|
|
|
StringVal: helper.StringToPtr("stat1"),
|
2018-11-14 16:51:50 +00:00
|
|
|
},
|
2018-11-15 19:44:13 +00:00
|
|
|
},
|
|
|
|
"id2": {
|
|
|
|
Summary: &api.StatValue{
|
|
|
|
IntNumeratorVal: helper.Int64ToPtr(2),
|
2018-11-14 16:51:50 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-11-15 19:44:13 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Vendor: "vendor2",
|
|
|
|
Type: "type2",
|
|
|
|
InstanceStats: map[string]*api.DeviceStats{
|
|
|
|
"id1": {
|
|
|
|
Summary: &api.StatValue{
|
|
|
|
StringVal: helper.StringToPtr("stat3"),
|
2018-11-14 16:51:50 +00:00
|
|
|
},
|
2018-11-15 19:44:13 +00:00
|
|
|
},
|
|
|
|
"id2": {
|
|
|
|
Summary: &api.StatValue{
|
|
|
|
IntNumeratorVal: helper.Int64ToPtr(4),
|
2018-11-14 16:51:50 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := map[string]*api.StatValue{
|
|
|
|
"vendor1/type1/name1[id1]": {
|
|
|
|
StringVal: helper.StringToPtr("stat1"),
|
|
|
|
},
|
|
|
|
"vendor1/type1/name1[id2]": {
|
|
|
|
IntNumeratorVal: helper.Int64ToPtr(2),
|
|
|
|
},
|
|
|
|
"vendor2/type2[id1]": {
|
|
|
|
StringVal: helper.StringToPtr("stat3"),
|
|
|
|
},
|
|
|
|
"vendor2/type2[id2]": {
|
|
|
|
IntNumeratorVal: helper.Int64ToPtr(4),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-11-15 19:44:13 +00:00
|
|
|
require.EqualValues(t, expected, buildDeviceStatsSummaryMap(hostDeviceStats))
|
2018-11-14 16:51:50 +00:00
|
|
|
}
|
2018-11-15 03:11:59 +00:00
|
|
|
|
|
|
|
func TestFormatDeviceStats(t *testing.T) {
|
|
|
|
statValue := func(v string) *api.StatValue {
|
|
|
|
return &api.StatValue{
|
|
|
|
StringVal: helper.StringToPtr(v),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stat := &api.StatObject{
|
|
|
|
Attributes: map[string]*api.StatValue{
|
2018-11-16 22:28:17 +00:00
|
|
|
"a0": statValue("va0"),
|
2018-11-15 03:11:59 +00:00
|
|
|
"k0": statValue("v0"),
|
|
|
|
},
|
|
|
|
Nested: map[string]*api.StatObject{
|
2018-11-15 22:14:05 +00:00
|
|
|
"nested1": {
|
2018-11-15 03:11:59 +00:00
|
|
|
Attributes: map[string]*api.StatValue{
|
|
|
|
"k1_0": statValue("v1_0"),
|
|
|
|
"k1_1": statValue("v1_1"),
|
|
|
|
},
|
|
|
|
Nested: map[string]*api.StatObject{
|
2018-11-15 22:14:05 +00:00
|
|
|
"nested1_1": {
|
2018-11-15 03:11:59 +00:00
|
|
|
Attributes: map[string]*api.StatValue{
|
|
|
|
"k11_0": statValue("v11_0"),
|
|
|
|
"k11_1": statValue("v11_1"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-11-15 22:14:05 +00:00
|
|
|
"nested2": {
|
2018-11-15 03:11:59 +00:00
|
|
|
Attributes: map[string]*api.StatValue{
|
|
|
|
"k2": statValue("v2"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-11-16 22:28:17 +00:00
|
|
|
result := formatDeviceStats("TestDeviceID", stat)
|
2018-11-15 03:11:59 +00:00
|
|
|
|
2018-11-16 22:28:17 +00:00
|
|
|
// check that device id always appears first
|
|
|
|
require.Equal(t, "Device|TestDeviceID", result[0])
|
2018-11-15 03:11:59 +00:00
|
|
|
|
|
|
|
// check rest of values
|
|
|
|
expected := []string{
|
2018-11-16 22:28:17 +00:00
|
|
|
"Device|TestDeviceID",
|
|
|
|
"a0|va0",
|
2018-11-15 03:11:59 +00:00
|
|
|
"k0|v0",
|
|
|
|
"nested1.k1_0|v1_0",
|
|
|
|
"nested1.k1_1|v1_1",
|
|
|
|
"nested1.nested1_1.k11_0|v11_0",
|
|
|
|
"nested1.nested1_1.k11_1|v11_1",
|
|
|
|
"nested2.k2|v2",
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(t, expected, result)
|
|
|
|
}
|
2018-11-15 19:44:13 +00:00
|
|
|
|
|
|
|
func TestNodeStatusCommand_GetDeviceResourcesForNode(t *testing.T) {
|
|
|
|
hostDeviceStats := []*api.DeviceGroupStats{
|
|
|
|
{
|
|
|
|
Vendor: "vendor1",
|
|
|
|
Type: "type1",
|
|
|
|
Name: "name1",
|
|
|
|
InstanceStats: map[string]*api.DeviceStats{
|
|
|
|
"id1": {
|
|
|
|
Summary: &api.StatValue{
|
|
|
|
StringVal: helper.StringToPtr("stat1"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"id2": {
|
|
|
|
Summary: &api.StatValue{
|
|
|
|
IntNumeratorVal: helper.Int64ToPtr(2),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Vendor: "vendor2",
|
|
|
|
Type: "type2",
|
|
|
|
InstanceStats: map[string]*api.DeviceStats{
|
|
|
|
"id1": {
|
|
|
|
Summary: &api.StatValue{
|
|
|
|
StringVal: helper.StringToPtr("stat3"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"id2": {
|
|
|
|
Summary: &api.StatValue{
|
|
|
|
IntNumeratorVal: helper.Int64ToPtr(4),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
node := &api.Node{
|
|
|
|
NodeResources: &api.NodeResources{
|
|
|
|
Devices: []*api.NodeDeviceResource{
|
|
|
|
{
|
|
|
|
Vendor: "vendor2",
|
|
|
|
Type: "type2",
|
|
|
|
Instances: []*api.NodeDevice{
|
|
|
|
{ID: "id1"},
|
|
|
|
{ID: "id2"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Vendor: "vendor1",
|
|
|
|
Type: "type1",
|
|
|
|
Name: "name1",
|
|
|
|
Instances: []*api.NodeDevice{
|
|
|
|
{ID: "id1"},
|
|
|
|
{ID: "id2"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
formattedDevices := getDeviceResourcesForNode(hostDeviceStats, node)
|
|
|
|
expected := []string{
|
|
|
|
"vendor1/type1/name1[id1]|stat1",
|
|
|
|
"vendor1/type1/name1[id2]|2",
|
|
|
|
"vendor2/type2[id1]|stat3",
|
|
|
|
"vendor2/type2[id2]|4",
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, expected, formattedDevices)
|
|
|
|
}
|
2018-11-15 19:57:15 +00:00
|
|
|
|
|
|
|
func TestNodeStatusCommand_GetDeviceResources(t *testing.T) {
|
|
|
|
hostDeviceStats := []*api.DeviceGroupStats{
|
|
|
|
{
|
|
|
|
Vendor: "vendor1",
|
|
|
|
Type: "type1",
|
|
|
|
Name: "name1",
|
|
|
|
InstanceStats: map[string]*api.DeviceStats{
|
|
|
|
"id1": {
|
|
|
|
Summary: &api.StatValue{
|
|
|
|
StringVal: helper.StringToPtr("stat1"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"id2": {
|
|
|
|
Summary: &api.StatValue{
|
|
|
|
IntNumeratorVal: helper.Int64ToPtr(2),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Vendor: "vendor2",
|
|
|
|
Type: "type2",
|
|
|
|
InstanceStats: map[string]*api.DeviceStats{
|
|
|
|
"id1": {
|
|
|
|
Summary: &api.StatValue{
|
|
|
|
StringVal: helper.StringToPtr("stat3"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"id2": {
|
|
|
|
Summary: &api.StatValue{
|
|
|
|
IntNumeratorVal: helper.Int64ToPtr(4),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
formattedDevices := getDeviceResources(hostDeviceStats)
|
|
|
|
expected := []string{
|
|
|
|
"vendor1/type1/name1[id1]|stat1",
|
|
|
|
"vendor1/type1/name1[id2]|2",
|
|
|
|
"vendor2/type2[id1]|stat3",
|
|
|
|
"vendor2/type2[id2]|4",
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, expected, formattedDevices)
|
|
|
|
}
|