Files
nomad/command/helper_stats_test.go
Michael Schurter ca4e237d62 gofmt -s -w command/helper_stats_test.go
Fixes the static checks build
2018-11-15 14:14:05 -08:00

132 lines
2.9 KiB
Go

package command
import (
"sort"
"testing"
"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/helper"
"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) {
hostStats := &api.HostStats{
DeviceStats: []*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),
},
},
},
},
},
}
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),
},
}
require.EqualValues(t, expected, buildDeviceStatsSummaryMap(hostStats))
}
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{
"k0": statValue("v0"),
},
Nested: map[string]*api.StatObject{
"nested1": {
Attributes: map[string]*api.StatValue{
"k1_0": statValue("v1_0"),
"k1_1": statValue("v1_1"),
},
Nested: map[string]*api.StatObject{
"nested1_1": {
Attributes: map[string]*api.StatValue{
"k11_0": statValue("v11_0"),
"k11_1": statValue("v11_1"),
},
},
},
},
"nested2": {
Attributes: map[string]*api.StatValue{
"k2": statValue("v2"),
},
},
},
}
result := []string{"preseedkey|pressededvalue"}
formatDeviceStats(stat, "", &result)
// check array is appended only
require.Equal(t, "preseedkey|pressededvalue", result[0])
// check rest of values
sort.Strings(result)
expected := []string{
"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",
"preseedkey|pressededvalue",
}
require.Equal(t, expected, result)
}