github.com/hernad/nomad@v1.6.112/command/helper_devices_test.go (about)

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