From 07ba576a254b699cf088948a599c95e854537123 Mon Sep 17 00:00:00 2001 From: lstoppa Date: Mon, 7 Jun 2021 13:31:53 +0200 Subject: [PATCH 1/8] fix: broken MIB translation unit tests - config.yaml wasn't properly loaded (need to look to the parent's folder) --- tests/test_translator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_translator.py b/tests/test_translator.py index 4c3391ab..3ba73f95 100644 --- a/tests/test_translator.py +++ b/tests/test_translator.py @@ -78,7 +78,7 @@ def setUpClass(cls): @mongomock.patch() def setUp(self): - with open("./config.yaml", "r") as yamlfile: + with open("../config.yaml", "r") as yamlfile: server_config = yaml.load(yamlfile, Loader=yaml.FullLoader) server_config["snmp"]["mibs"]["dir"] = "../mibs/pysnmp" self.my_translator = Translator(server_config) From 5345f62d5e3633bc102ad16a98456566c0f3114a Mon Sep 17 00:00:00 2001 From: lstoppa Date: Mon, 7 Jun 2021 15:23:41 +0200 Subject: [PATCH 2/8] fix: broken MIB translation unit tests (added a copy of the config file) --- tests/config.yaml | 27 +++++++++++++++++++++++++++ tests/test_translator.py | 2 +- 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 tests/config.yaml diff --git a/tests/config.yaml b/tests/config.yaml new file mode 100644 index 00000000..3bbf1806 --- /dev/null +++ b/tests/config.yaml @@ -0,0 +1,27 @@ +# +# Copyright 2021 Splunk Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +snmp: + mibs: + dir: "/work/mibs/pysnmp" + load_list: "lookups/mibs_list.csv" + mibs_path: "/work/mibs" +mongo: + oid: + database: "mib_server" + collection: "oids" + mib: + database: "files" + collection: "mib_files" diff --git a/tests/test_translator.py b/tests/test_translator.py index 3ba73f95..4c3391ab 100644 --- a/tests/test_translator.py +++ b/tests/test_translator.py @@ -78,7 +78,7 @@ def setUpClass(cls): @mongomock.patch() def setUp(self): - with open("../config.yaml", "r") as yamlfile: + with open("./config.yaml", "r") as yamlfile: server_config = yaml.load(yamlfile, Loader=yaml.FullLoader) server_config["snmp"]["mibs"]["dir"] = "../mibs/pysnmp" self.my_translator = Translator(server_config) From 516f44955d008e0f55c6a6f3010ccaa732801244 Mon Sep 17 00:00:00 2001 From: lstoppa Date: Mon, 7 Jun 2021 16:20:01 +0200 Subject: [PATCH 3/8] fix: broken MIB translation unit tests (use relative paths) --- tests/config.yaml | 27 --------------------------- tests/test_translator.py | 10 ++++++++-- 2 files changed, 8 insertions(+), 29 deletions(-) delete mode 100644 tests/config.yaml diff --git a/tests/config.yaml b/tests/config.yaml deleted file mode 100644 index 3bbf1806..00000000 --- a/tests/config.yaml +++ /dev/null @@ -1,27 +0,0 @@ -# -# Copyright 2021 Splunk Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -snmp: - mibs: - dir: "/work/mibs/pysnmp" - load_list: "lookups/mibs_list.csv" - mibs_path: "/work/mibs" -mongo: - oid: - database: "mib_server" - collection: "oids" - mib: - database: "files" - collection: "mib_files" diff --git a/tests/test_translator.py b/tests/test_translator.py index 4c3391ab..2fa731f1 100644 --- a/tests/test_translator.py +++ b/tests/test_translator.py @@ -78,9 +78,15 @@ def setUpClass(cls): @mongomock.patch() def setUp(self): - with open("./config.yaml", "r") as yamlfile: + import os + + base_project_path = os.path.join(os.getcwd(), "..") + config_file = os.path.join(base_project_path, "config.yaml") + with open(config_file, "r") as yamlfile: server_config = yaml.load(yamlfile, Loader=yaml.FullLoader) - server_config["snmp"]["mibs"]["dir"] = "../mibs/pysnmp" + server_config["snmp"]["mibs"]["dir"] = os.path.join( + base_project_path, "mibs", "pysnmp" + ) self.my_translator = Translator(server_config) @mongomock.patch() From 45d1b73d75efe25c09d2d88544220be4cec64218 Mon Sep 17 00:00:00 2001 From: lstoppa Date: Tue, 8 Jun 2021 10:09:26 +0200 Subject: [PATCH 4/8] fix: broken MIB translation unit tests (use relative paths) - tested locally that now poetry run pytest works as expected --- tests/test_translator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_translator.py b/tests/test_translator.py index 2fa731f1..56239088 100644 --- a/tests/test_translator.py +++ b/tests/test_translator.py @@ -80,7 +80,7 @@ def setUpClass(cls): def setUp(self): import os - base_project_path = os.path.join(os.getcwd(), "..") + base_project_path = os.getcwd() config_file = os.path.join(base_project_path, "config.yaml") with open(config_file, "r") as yamlfile: server_config = yaml.load(yamlfile, Loader=yaml.FullLoader) From 227a018a6c196c26cebc6c589b21e699d8ab458e Mon Sep 17 00:00:00 2001 From: lstoppa Date: Tue, 8 Jun 2021 11:20:59 +0200 Subject: [PATCH 5/8] fix: fixed isort, pylint checker --- tests/test_translator.py | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/tests/test_translator.py b/tests/test_translator.py index 56239088..49ace47b 100644 --- a/tests/test_translator.py +++ b/tests/test_translator.py @@ -13,24 +13,34 @@ # See the License for the specific language governing permissions and # limitations under the License. # ######################################################################## +import json +import logging +import os from unittest import TestCase, mock import mongomock import yaml +from pysnmp.hlapi import ( + CommunityData, + ContextData, + ObjectIdentity, + ObjectType, + SnmpEngine, + UdpTransportTarget, + getCmd, +) from splunk_connect_for_snmp_mib_server.translator import Translator -import os -import logging -import json -from pysnmp.hlapi import * logger = logging.getLogger(__name__) -# This sample function was used to generate test data. -# I took the content of https://raw.githubusercontent.com/etingof/snmpsim/master/data/recorded/linux-full-walk.snmprec -# and sorted by data type. This should give us some real data we can use to test our MIB-server translation -# process. This was the input data we use in our test "test_translate_all_snmp_simulator_data_types" +# This sample function was used to generate test data. I took the content of +# https://raw.githubusercontent.com/etingof/snmpsim/master/data/recorded/linux +# -full-walk.snmprec +# and sorted by data type. This should give us some real data we can use to +# test our MIB-server translation process. This was the input data we use in +# our test "test_translate_all_snmp_simulator_data_types" def prepare_test_data(): for oid in ( "1.3.6.1.2.1.2.2.1.4.1", @@ -44,22 +54,21 @@ def prepare_test_data(): "1.3.6.1.4.1.2021.10.1.6.1", "1.3.6.1.2.1.31.1.1.1.10.1", ): - g = getCmd( + result = getCmd( SnmpEngine(), CommunityData("public"), UdpTransportTarget(("localhost", 1611)), ContextData(), ObjectType(ObjectIdentity(oid)), ) - error_indication, error_status, error_index, var_binds = next(g) + error_indication, error_status, error_index, var_binds = next(result) for name, value in var_binds: class_name = value.__class__.__name__ # normal_value = value str_value = str(value).replace("\n", "") pretty_value = value.prettyPrint() - print( - f"{str(name)}|{class_name}|{str_value}|{pretty_value}|{pretty_value == str_value}" - ) + same = pretty_value == str_value + print(f"{str(name)}|{class_name}|{str_value}|{pretty_value}|{same}") class TranslatorTest(TestCase): @@ -78,8 +87,6 @@ def setUpClass(cls): @mongomock.patch() def setUp(self): - import os - base_project_path = os.getcwd() config_file = os.path.join(base_project_path, "config.yaml") with open(config_file, "r") as yamlfile: From 3beb7f8604b907038950328463b1d1c7651b3b29 Mon Sep 17 00:00:00 2001 From: lstoppa Date: Tue, 8 Jun 2021 11:33:57 +0200 Subject: [PATCH 6/8] fix: fixed isort, flake8 --- tests/test_translator.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/tests/test_translator.py b/tests/test_translator.py index 49ace47b..2ce0436d 100644 --- a/tests/test_translator.py +++ b/tests/test_translator.py @@ -20,15 +20,8 @@ import mongomock import yaml -from pysnmp.hlapi import ( - CommunityData, - ContextData, - ObjectIdentity, - ObjectType, - SnmpEngine, - UdpTransportTarget, - getCmd, -) +from pysnmp.hlapi import (CommunityData, ContextData, ObjectIdentity, + ObjectType, SnmpEngine, UdpTransportTarget, getCmd) from splunk_connect_for_snmp_mib_server.translator import Translator @@ -147,7 +140,11 @@ def test_format_trap_non_existing_oid(self): value_type = input_var_binds_list[i]["val_type"] oid = input_var_binds_list[i]["oid"] value = input_var_binds_list[i]["val"] - current = f'oid-type{i + 1}="{oid_type}" value{i + 1}-type="{value_type}" {oid}="{value}" value{i + 1}="{value}"' + current = ( + f'oid-type{i + 1}="{oid_type}" ' + f'value{i + 1}-type="{value_type}" ' + f'{oid}="{value}" value{i + 1}="{value}"' + ) # these two additional spaces are not an error untranslated += f"{current} " if i < len(input_var_binds_list) - 1: From 94b34a1e3a8ecc7b0f1fd168353c3a3f5fc1229a Mon Sep 17 00:00:00 2001 From: lstoppa Date: Tue, 8 Jun 2021 11:41:42 +0200 Subject: [PATCH 7/8] fix: fixed isort, flake8 (part #2) --- splunk_connect_for_snmp_mib_server/translator.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/splunk_connect_for_snmp_mib_server/translator.py b/splunk_connect_for_snmp_mib_server/translator.py index 7baf8cb2..76151649 100644 --- a/splunk_connect_for_snmp_mib_server/translator.py +++ b/splunk_connect_for_snmp_mib_server/translator.py @@ -297,7 +297,7 @@ def format_trap_event(self, var_binds): custom_translated_value = self.custom_translator(value) offset += 1 - original_oid = '{oid}="{value}"'.format(offset=offset, oid=oid, value=value) + original_oid = '{oid}="{value}"'.format(oid=oid, value=value) oid_type_string = 'oid-type{offset}="{oid_type}"'.format( offset=offset, oid_type=name_type ) @@ -313,7 +313,6 @@ def format_trap_event(self, var_binds): if custom_translated_oid: custom_translated_mib_string = ( '{custom_translated_oid}="{custom_translated_value}"'.format( - offset=offset, custom_translated_oid=custom_translated_oid, custom_translated_value=custom_translated_value, ) From 81c8d4210c450a8cd24cfd484e6ccfb67ccdf4de Mon Sep 17 00:00:00 2001 From: lstoppa Date: Tue, 8 Jun 2021 11:46:29 +0200 Subject: [PATCH 8/8] fix: fixed isort, flake8 (part #3) --- splunk_connect_for_snmp_mib_server/translator.py | 3 ++- tests/test_translator.py | 11 +++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/splunk_connect_for_snmp_mib_server/translator.py b/splunk_connect_for_snmp_mib_server/translator.py index 76151649..7baf8cb2 100644 --- a/splunk_connect_for_snmp_mib_server/translator.py +++ b/splunk_connect_for_snmp_mib_server/translator.py @@ -297,7 +297,7 @@ def format_trap_event(self, var_binds): custom_translated_value = self.custom_translator(value) offset += 1 - original_oid = '{oid}="{value}"'.format(oid=oid, value=value) + original_oid = '{oid}="{value}"'.format(offset=offset, oid=oid, value=value) oid_type_string = 'oid-type{offset}="{oid_type}"'.format( offset=offset, oid_type=name_type ) @@ -313,6 +313,7 @@ def format_trap_event(self, var_binds): if custom_translated_oid: custom_translated_mib_string = ( '{custom_translated_oid}="{custom_translated_value}"'.format( + offset=offset, custom_translated_oid=custom_translated_oid, custom_translated_value=custom_translated_value, ) diff --git a/tests/test_translator.py b/tests/test_translator.py index 2ce0436d..5c006a71 100644 --- a/tests/test_translator.py +++ b/tests/test_translator.py @@ -20,8 +20,15 @@ import mongomock import yaml -from pysnmp.hlapi import (CommunityData, ContextData, ObjectIdentity, - ObjectType, SnmpEngine, UdpTransportTarget, getCmd) +from pysnmp.hlapi import ( + CommunityData, + ContextData, + ObjectIdentity, + ObjectType, + SnmpEngine, + UdpTransportTarget, + getCmd, +) from splunk_connect_for_snmp_mib_server.translator import Translator