github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/command/helper_devices_test.go (about) 1 package command 2 3 import ( 4 "testing" 5 6 "github.com/hashicorp/nomad/api" 7 "github.com/hashicorp/nomad/ci" 8 "github.com/hashicorp/nomad/helper/pointer" 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 ) 12 13 func TestDeviceQualifiedID(t *testing.T) { 14 ci.Parallel(t) 15 16 require := require.New(t) 17 18 require.Equal("vendor/type/name[id]", deviceQualifiedID("vendor", "type", "name", "id")) 19 require.Equal("vendor/type[id]", deviceQualifiedID("vendor", "type", "", "id")) 20 require.Equal("vendor[id]", deviceQualifiedID("vendor", "", "", "id")) 21 } 22 23 func TestBuildDeviceStatsSummaryMap(t *testing.T) { 24 ci.Parallel(t) 25 26 hostDeviceStats := []*api.DeviceGroupStats{ 27 { 28 Vendor: "vendor1", 29 Type: "type1", 30 Name: "name1", 31 InstanceStats: map[string]*api.DeviceStats{ 32 "id1": { 33 Summary: &api.StatValue{ 34 StringVal: pointer.Of("stat1"), 35 }, 36 }, 37 "id2": { 38 Summary: &api.StatValue{ 39 IntNumeratorVal: pointer.Of(int64(2)), 40 }, 41 }, 42 }, 43 }, 44 { 45 Vendor: "vendor2", 46 Type: "type2", 47 InstanceStats: map[string]*api.DeviceStats{ 48 "id1": { 49 Summary: &api.StatValue{ 50 StringVal: pointer.Of("stat3"), 51 }, 52 }, 53 "id2": { 54 Summary: &api.StatValue{ 55 IntNumeratorVal: pointer.Of(int64(4)), 56 }, 57 }, 58 }, 59 }, 60 } 61 62 expected := map[string]*api.StatValue{ 63 "vendor1/type1/name1[id1]": { 64 StringVal: pointer.Of("stat1"), 65 }, 66 "vendor1/type1/name1[id2]": { 67 IntNumeratorVal: pointer.Of(int64(2)), 68 }, 69 "vendor2/type2[id1]": { 70 StringVal: pointer.Of("stat3"), 71 }, 72 "vendor2/type2[id2]": { 73 IntNumeratorVal: pointer.Of(int64(4)), 74 }, 75 } 76 77 require.EqualValues(t, expected, buildDeviceStatsSummaryMap(hostDeviceStats)) 78 } 79 80 func TestFormatDeviceStats(t *testing.T) { 81 ci.Parallel(t) 82 83 statValue := func(v string) *api.StatValue { 84 return &api.StatValue{ 85 StringVal: pointer.Of(v), 86 } 87 } 88 89 stat := &api.StatObject{ 90 Attributes: map[string]*api.StatValue{ 91 "a0": statValue("va0"), 92 "k0": statValue("v0"), 93 }, 94 Nested: map[string]*api.StatObject{ 95 "nested1": { 96 Attributes: map[string]*api.StatValue{ 97 "k1_0": statValue("v1_0"), 98 "k1_1": statValue("v1_1"), 99 }, 100 Nested: map[string]*api.StatObject{ 101 "nested1_1": { 102 Attributes: map[string]*api.StatValue{ 103 "k11_0": statValue("v11_0"), 104 "k11_1": statValue("v11_1"), 105 }, 106 }, 107 }, 108 }, 109 "nested2": { 110 Attributes: map[string]*api.StatValue{ 111 "k2": statValue("v2"), 112 }, 113 }, 114 }, 115 } 116 117 result := formatDeviceStats("TestDeviceID", stat) 118 119 // check that device id always appears first 120 require.Equal(t, "Device|TestDeviceID", result[0]) 121 122 // check rest of values 123 expected := []string{ 124 "Device|TestDeviceID", 125 "a0|va0", 126 "k0|v0", 127 "nested1.k1_0|v1_0", 128 "nested1.k1_1|v1_1", 129 "nested1.nested1_1.k11_0|v11_0", 130 "nested1.nested1_1.k11_1|v11_1", 131 "nested2.k2|v2", 132 } 133 134 require.Equal(t, expected, result) 135 } 136 137 func TestNodeStatusCommand_GetDeviceResourcesForNode(t *testing.T) { 138 ci.Parallel(t) 139 140 hostDeviceStats := []*api.DeviceGroupStats{ 141 { 142 Vendor: "vendor1", 143 Type: "type1", 144 Name: "name1", 145 InstanceStats: map[string]*api.DeviceStats{ 146 "id1": { 147 Summary: &api.StatValue{ 148 StringVal: pointer.Of("stat1"), 149 }, 150 }, 151 "id2": { 152 Summary: &api.StatValue{ 153 IntNumeratorVal: pointer.Of(int64(2)), 154 }, 155 }, 156 }, 157 }, 158 { 159 Vendor: "vendor2", 160 Type: "type2", 161 InstanceStats: map[string]*api.DeviceStats{ 162 "id1": { 163 Summary: &api.StatValue{ 164 StringVal: pointer.Of("stat3"), 165 }, 166 }, 167 "id2": { 168 Summary: &api.StatValue{ 169 IntNumeratorVal: pointer.Of(int64(4)), 170 }, 171 }, 172 }, 173 }, 174 } 175 176 node := &api.Node{ 177 NodeResources: &api.NodeResources{ 178 Devices: []*api.NodeDeviceResource{ 179 { 180 Vendor: "vendor2", 181 Type: "type2", 182 Instances: []*api.NodeDevice{ 183 {ID: "id1"}, 184 {ID: "id2"}, 185 }, 186 }, 187 { 188 Vendor: "vendor1", 189 Type: "type1", 190 Name: "name1", 191 Instances: []*api.NodeDevice{ 192 {ID: "id1"}, 193 {ID: "id2"}, 194 }, 195 }, 196 }, 197 }, 198 } 199 200 formattedDevices := getDeviceResourcesForNode(hostDeviceStats, node) 201 expected := []string{ 202 "vendor1/type1/name1[id1]|stat1", 203 "vendor1/type1/name1[id2]|2", 204 "vendor2/type2[id1]|stat3", 205 "vendor2/type2[id2]|4", 206 } 207 208 assert.Equal(t, expected, formattedDevices) 209 } 210 211 func TestNodeStatusCommand_GetDeviceResources(t *testing.T) { 212 ci.Parallel(t) 213 214 hostDeviceStats := []*api.DeviceGroupStats{ 215 { 216 Vendor: "vendor1", 217 Type: "type1", 218 Name: "name1", 219 InstanceStats: map[string]*api.DeviceStats{ 220 "id1": { 221 Summary: &api.StatValue{ 222 StringVal: pointer.Of("stat1"), 223 }, 224 }, 225 "id2": { 226 Summary: &api.StatValue{ 227 IntNumeratorVal: pointer.Of(int64(2)), 228 }, 229 }, 230 }, 231 }, 232 { 233 Vendor: "vendor2", 234 Type: "type2", 235 InstanceStats: map[string]*api.DeviceStats{ 236 "id1": { 237 Summary: &api.StatValue{ 238 StringVal: pointer.Of("stat3"), 239 }, 240 }, 241 "id2": { 242 Summary: &api.StatValue{ 243 IntNumeratorVal: pointer.Of(int64(4)), 244 }, 245 }, 246 }, 247 }, 248 } 249 250 formattedDevices := getDeviceResources(hostDeviceStats) 251 expected := []string{ 252 "vendor1/type1/name1[id1]|stat1", 253 "vendor1/type1/name1[id2]|2", 254 "vendor2/type2[id1]|stat3", 255 "vendor2/type2[id2]|4", 256 } 257 258 assert.Equal(t, expected, formattedDevices) 259 } 260 func TestGetDeviceAttributes(t *testing.T) { 261 ci.Parallel(t) 262 263 d := &api.NodeDeviceResource{ 264 Vendor: "Vendor", 265 Type: "Type", 266 Name: "Name", 267 268 Attributes: map[string]*api.Attribute{ 269 "utilization": { 270 FloatVal: pointer.Of(float64(0.78)), 271 Unit: "%", 272 }, 273 "filesystem": { 274 StringVal: pointer.Of("ext4"), 275 }, 276 }, 277 } 278 279 formattedDevices := getDeviceAttributes(d) 280 expected := []string{ 281 "Device Group|Vendor/Type/Name", 282 "filesystem|ext4", 283 "utilization|0.78 %", 284 } 285 286 assert.Equal(t, expected, formattedDevices) 287 }