@@ -380,3 +380,119 @@ func TestGatherIgnoreInterfaces(t *testing.T) {
380380 }
381381 acc .AssertContainsTaggedFields (t , pluginName , expectedFieldsEth2 , expectedTagsEth2 )
382382}
383+
384+ type TestCase struct {
385+ normalization []string
386+ stats map [string ]uint64
387+ expectedFields map [string ]uint64
388+ }
389+
390+ func TestNormalizedKeys (t * testing.T ) {
391+ cases := []TestCase {
392+ {
393+ normalization : []string {"underscore" },
394+ stats : map [string ]uint64 {
395+ "port rx" : 1 ,
396+ " Port_tx" : 0 ,
397+ "interface_up" : 0 ,
398+ },
399+ expectedFields : map [string ]uint64 {
400+ "port_rx" : 1 ,
401+ "_Port_tx" : 0 ,
402+ "interface_up" : 0 ,
403+ },
404+ },
405+ {
406+ normalization : []string {"underscore" , "lower" },
407+ stats : map [string ]uint64 {
408+ "Port rx" : 1 ,
409+ " Port_tx" : 0 ,
410+ "interface_up" : 0 ,
411+ },
412+ expectedFields : map [string ]uint64 {
413+ "port_rx" : 1 ,
414+ "_port_tx" : 0 ,
415+ "interface_up" : 0 ,
416+ },
417+ },
418+ {
419+ normalization : []string {"underscore" , "lower" , "trim" },
420+ stats : map [string ]uint64 {
421+ " Port RX " : 1 ,
422+ " Port_tx" : 0 ,
423+ "interface_up" : 0 ,
424+ },
425+ expectedFields : map [string ]uint64 {
426+ "port_rx" : 1 ,
427+ "port_tx" : 0 ,
428+ "interface_up" : 0 ,
429+ },
430+ },
431+ {
432+ normalization : []string {"underscore" , "lower" , "snakecase" , "trim" },
433+ stats : map [string ]uint64 {
434+ " Port RX " : 1 ,
435+ " Port_tx" : 0 ,
436+ "interface_up" : 0 ,
437+ },
438+ expectedFields : map [string ]uint64 {
439+ "port_rx" : 1 ,
440+ "port_tx" : 0 ,
441+ "interface_up" : 0 ,
442+ },
443+ },
444+ {
445+ normalization : []string {"snakecase" },
446+ stats : map [string ]uint64 {
447+ " PortRX " : 1 ,
448+ " PortTX" : 0 ,
449+ "interface_up" : 0 ,
450+ },
451+ expectedFields : map [string ]uint64 {
452+ "port_rx" : 1 ,
453+ "port_tx" : 0 ,
454+ "interface_up" : 0 ,
455+ },
456+ },
457+ {
458+ normalization : []string {},
459+ stats : map [string ]uint64 {
460+ " Port RX " : 1 ,
461+ " Port_tx" : 0 ,
462+ "interface_up" : 0 ,
463+ },
464+ expectedFields : map [string ]uint64 {
465+ " Port RX " : 1 ,
466+ " Port_tx" : 0 ,
467+ "interface_up" : 0 ,
468+ },
469+ },
470+ }
471+ for _ , c := range cases {
472+ eth0 := & InterfaceMock {"eth0" , "e1000e" , c .stats , false , true }
473+ expectedTags := map [string ]string {
474+ "interface" : eth0 .Name ,
475+ "driver" : eth0 .DriverName ,
476+ }
477+
478+ interfaceMap = make (map [string ]* InterfaceMock )
479+ interfaceMap [eth0 .Name ] = eth0
480+
481+ cmd := & CommandEthtoolMock {interfaceMap }
482+ command = & Ethtool {
483+ InterfaceInclude : []string {},
484+ InterfaceExclude : []string {},
485+ NormalizeKeys : c .normalization ,
486+ command : cmd ,
487+ }
488+
489+ var acc testutil.Accumulator
490+ err := command .Gather (& acc )
491+
492+ assert .NoError (t , err )
493+ assert .Len (t , acc .Metrics , 1 )
494+
495+ acc .AssertContainsFields (t , pluginName , toStringMapInterface (c .expectedFields ))
496+ acc .AssertContainsTaggedFields (t , pluginName , toStringMapInterface (c .expectedFields ), expectedTags )
497+ }
498+ }
0 commit comments