From 86f5326877fb7d5c80691b852cf8735910de77c2 Mon Sep 17 00:00:00 2001 From: Alexandre Yang Date: Fri, 24 Nov 2023 14:28:44 +0100 Subject: [PATCH] improve GetNextColumnOidNaive --- .../corechecks/snmp/internal/session/utils.go | 5 +++++ .../corechecks/snmp/internal/session/utils_test.go | 14 ++++++++------ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/pkg/collector/corechecks/snmp/internal/session/utils.go b/pkg/collector/corechecks/snmp/internal/session/utils.go index c02be8eaea65c..e646948ee6f03 100644 --- a/pkg/collector/corechecks/snmp/internal/session/utils.go +++ b/pkg/collector/corechecks/snmp/internal/session/utils.go @@ -23,6 +23,11 @@ func GetNextColumnOidNaive(oid string) (string, error) { } tableOid := oid[0:idx] rowFullIndex := oid[idx+3:] // +3 to skip `.1.` + if !strings.Contains(rowFullIndex, ".") { + // TODO: test me + // entry `.1.` must be followed by a column + index, hence the index is expected to contain at least one `.` + return "", fmt.Errorf("the last `.1.` is not an table entry: %s", oid) + } rowFirstIndex := strings.Split(rowFullIndex, ".")[0] rowFirstIndexNum, err := strconv.Atoi(rowFirstIndex) if err != nil { diff --git a/pkg/collector/corechecks/snmp/internal/session/utils_test.go b/pkg/collector/corechecks/snmp/internal/session/utils_test.go index 610b422cb849c..289be0ad97d3d 100644 --- a/pkg/collector/corechecks/snmp/internal/session/utils_test.go +++ b/pkg/collector/corechecks/snmp/internal/session/utils_test.go @@ -25,12 +25,6 @@ func TestGetNextColumnOid(t *testing.T) { oid: "1.3.6.1.2.1.2.2.1.99.99", expectedOid: "1.3.6.1.2.1.2.2.1.100", }, - { - oid: "1.3.6.1.2.1.2.2.1.1.1", - // ideally it should return "1.3.6.1.2.1.2.2.1.2" instead of `1.3.6.1.2.1.2.2.1.1.2` - // but we can improve the algorithm later if needed - expectedOid: "1.3.6.1.2.1.2.2.1.1.2", - }, { oid: "1.3.6.2.2.2.2.2.99999", expectedErr: "the oid is not a column oid: 1.3.6.2.2.2.2.2.99999", @@ -39,6 +33,14 @@ func TestGetNextColumnOid(t *testing.T) { oid: ".1.3.6.1.2.1.2.2.1.2.99.", // trim trailing/ending `.` expectedOid: "1.3.6.1.2.1.2.2.1.3", }, + { + oid: "1.3.6.1.2.1.2.2.1.1.1", + expectedErr: "the last `.1.` is not an table entry: 1.3.6.1.2.1.2.2.1.1.1", + }, + { + oid: "1.3.6.2.2.2.2.2.1.9", + expectedErr: "the last `.1.` is not an table entry: 1.3.6.2.2.2.2.2.1.9", + }, } for _, tt := range tests { t.Run(tt.oid, func(t *testing.T) {