fix(internal.ethtool): avoid internal name conflict with aws (#11696)

This commit is contained in:
Mya 2022-09-20 09:36:29 -06:00 committed by GitHub
parent eabb66f7d7
commit 5f22bd17db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 99 additions and 55 deletions

View File

@ -108,6 +108,10 @@ func (e *Ethtool) normalizeKey(key string) string {
if inStringSlice(e.NormalizeKeys, "underscore") {
key = strings.ReplaceAll(key, " ", "_")
}
// aws has a conflicting name that needs to be renamed
if key == "interface_up" {
key = "interface_up_counter"
}
return key
}

View File

@ -305,6 +305,15 @@ func toStringMapInterface(in map[string]uint64) map[string]interface{} {
return m
}
func toStringMapUint(in map[string]interface{}) map[string]uint64 {
var m = map[string]uint64{}
for k, v := range in {
t := v.(uint64)
m[k] = t
}
return m
}
func TestGather(t *testing.T) {
setup()
var acc testutil.Accumulator
@ -314,15 +323,20 @@ func TestGather(t *testing.T) {
require.Len(t, acc.Metrics, 2)
expectedFieldsEth1 := toStringMapInterface(interfaceMap["eth1"].Stat)
expectedFieldsEth1["interface_up_counter"] = expectedFieldsEth1["interface_up"]
expectedFieldsEth1["interface_up"] = true
expectedTagsEth1 := map[string]string{
"interface": "eth1",
"driver": "driver1",
}
acc.AssertContainsTaggedFields(t, pluginName, expectedFieldsEth1, expectedTagsEth1)
expectedFieldsEth2 := toStringMapInterface(interfaceMap["eth2"].Stat)
expectedFieldsEth2["interface_up_counter"] = expectedFieldsEth2["interface_up"]
expectedFieldsEth2["interface_up"] = false
expectedTagsEth2 := map[string]string{
"interface": "eth2",
"driver": "driver1",
"interface": "eth2",
}
acc.AssertContainsTaggedFields(t, pluginName, expectedFieldsEth2, expectedTagsEth2)
}
@ -339,6 +353,8 @@ func TestGatherIncludeInterfaces(t *testing.T) {
// Should contain eth1
expectedFieldsEth1 := toStringMapInterface(interfaceMap["eth1"].Stat)
expectedFieldsEth1["interface_up_counter"] = expectedFieldsEth1["interface_up"]
expectedFieldsEth1["interface_up"] = true
expectedTagsEth1 := map[string]string{
"interface": "eth1",
"driver": "driver1",
@ -347,6 +363,8 @@ func TestGatherIncludeInterfaces(t *testing.T) {
// Should not contain eth2
expectedFieldsEth2 := toStringMapInterface(interfaceMap["eth2"].Stat)
expectedFieldsEth2["interface_up_counter"] = expectedFieldsEth2["interface_up"]
expectedFieldsEth2["interface_up"] = false
expectedTagsEth2 := map[string]string{
"interface": "eth2",
"driver": "driver1",
@ -366,6 +384,8 @@ func TestGatherIgnoreInterfaces(t *testing.T) {
// Should not contain eth1
expectedFieldsEth1 := toStringMapInterface(interfaceMap["eth1"].Stat)
expectedFieldsEth1["interface_up_counter"] = expectedFieldsEth1["interface_up"]
expectedFieldsEth1["interface_up"] = true
expectedTagsEth1 := map[string]string{
"interface": "eth1",
"driver": "driver1",
@ -374,6 +394,8 @@ func TestGatherIgnoreInterfaces(t *testing.T) {
// Should contain eth2
expectedFieldsEth2 := toStringMapInterface(interfaceMap["eth2"].Stat)
expectedFieldsEth2["interface_up_counter"] = expectedFieldsEth2["interface_up"]
expectedFieldsEth2["interface_up"] = false
expectedTagsEth2 := map[string]string{
"interface": "eth2",
"driver": "driver1",
@ -383,93 +405,111 @@ func TestGatherIgnoreInterfaces(t *testing.T) {
type TestCase struct {
normalization []string
stats map[string]uint64
expectedFields map[string]uint64
stats map[string]interface{}
expectedFields map[string]interface{}
}
func TestNormalizedKeys(t *testing.T) {
cases := []TestCase{
{
normalization: []string{"underscore"},
stats: map[string]uint64{
"port rx": 1,
" Port_tx": 0,
"interface_up": 0,
stats: map[string]interface{}{
"port rx": uint64(1),
" Port_tx": uint64(0),
"interface_up": uint64(0),
},
expectedFields: map[string]uint64{
"port_rx": 1,
"_Port_tx": 0,
"interface_up": 0,
expectedFields: map[string]interface{}{
"port_rx": uint64(1),
"_Port_tx": uint64(0),
"interface_up": true,
"interface_up_counter": uint64(0),
},
},
{
normalization: []string{"underscore", "lower"},
stats: map[string]uint64{
"Port rx": 1,
" Port_tx": 0,
"interface_up": 0,
stats: map[string]interface{}{
"Port rx": uint64(1),
" Port_tx": uint64(0),
"interface_up": uint64(0),
},
expectedFields: map[string]uint64{
"port_rx": 1,
"_port_tx": 0,
"interface_up": 0,
expectedFields: map[string]interface{}{
"port_rx": uint64(1),
"_port_tx": uint64(0),
"interface_up": true,
"interface_up_counter": uint64(0),
},
},
{
normalization: []string{"underscore", "lower", "trim"},
stats: map[string]uint64{
" Port RX ": 1,
" Port_tx": 0,
"interface_up": 0,
},
expectedFields: map[string]uint64{
"port_rx": 1,
"port_tx": 0,
"interface_up": 0,
stats: map[string]interface{}{
" Port RX ": uint64(1),
" Port_tx": uint64(0),
"interface_up": uint64(0),
},
expectedFields: map[string]interface{}{
"port_rx": uint64(1),
"port_tx": uint64(0),
"interface_up": true,
"interface_up_counter": uint64(0)},
},
{
normalization: []string{"underscore", "lower", "snakecase", "trim"},
stats: map[string]uint64{
" Port RX ": 1,
" Port_tx": 0,
"interface_up": 0,
stats: map[string]interface{}{
" Port RX ": uint64(1),
" Port_tx": uint64(0),
"interface_up": uint64(0),
},
expectedFields: map[string]uint64{
"port_rx": 1,
"port_tx": 0,
"interface_up": 0,
expectedFields: map[string]interface{}{
"port_rx": uint64(1),
"port_tx": uint64(0),
"interface_up": true,
"interface_up_counter": uint64(0),
},
},
{
normalization: []string{"snakecase"},
stats: map[string]uint64{
" PortRX ": 1,
" PortTX": 0,
"interface_up": 0,
stats: map[string]interface{}{
" PortRX ": uint64(1),
" PortTX": uint64(0),
"interface_up": uint64(0),
},
expectedFields: map[string]uint64{
"port_rx": 1,
"port_tx": 0,
"interface_up": 0,
expectedFields: map[string]interface{}{
"port_rx": uint64(1),
"port_tx": uint64(0),
"interface_up": true,
"interface_up_counter": uint64(0),
},
},
{
normalization: []string{},
stats: map[string]uint64{
" Port RX ": 1,
" Port_tx": 0,
"interface_up": 0,
stats: map[string]interface{}{
" Port RX ": uint64(1),
" Port_tx": uint64(0),
"interface_up": uint64(0),
},
expectedFields: map[string]uint64{
" Port RX ": 1,
" Port_tx": 0,
"interface_up": 0,
expectedFields: map[string]interface{}{
" Port RX ": uint64(1),
" Port_tx": uint64(0),
"interface_up": true,
"interface_up_counter": uint64(0),
},
},
{
normalization: []string{},
stats: map[string]interface{}{
" Port RX ": uint64(1),
" Port_tx": uint64(0),
},
expectedFields: map[string]interface{}{
" Port RX ": uint64(1),
" Port_tx": uint64(0),
"interface_up": true,
},
},
}
for _, c := range cases {
eth0 := &InterfaceMock{"eth0", "e1000e", c.stats, false, true}
eth0 := &InterfaceMock{"eth0", "e1000e", toStringMapUint(c.stats), false, true}
expectedTags := map[string]string{
"interface": eth0.Name,
"driver": eth0.DriverName,
@ -492,7 +532,7 @@ func TestNormalizedKeys(t *testing.T) {
require.NoError(t, err)
require.Len(t, acc.Metrics, 1)
acc.AssertContainsFields(t, pluginName, toStringMapInterface(c.expectedFields))
acc.AssertContainsTaggedFields(t, pluginName, toStringMapInterface(c.expectedFields), expectedTags)
acc.AssertContainsFields(t, pluginName, c.expectedFields)
acc.AssertContainsTaggedFields(t, pluginName, c.expectedFields, expectedTags)
}
}