Skip to content
This repository has been archived by the owner on Dec 17, 2021. It is now read-only.

Commit

Permalink
fix: lint (ADDON-39580)
Browse files Browse the repository at this point in the history
- added integration code: now, we can extract fields, based on the configuration file we have
  • Loading branch information
lstoppa committed Aug 3, 2021
1 parent 220f7e8 commit ae3647e
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,6 @@ def __extract_interface_indexes(self):

def __extract_interface_names(self):
return self.__extract_single_field_as_list(InterfaceMib.IF_MIB_IF_DESCR_BASE)

def extract_custom_field(self, snmp_field_name):
return self.__extract_single_field_as_list(snmp_field_name)
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
from splunk_connect_for_snmp_poller.manager.realtime.interface_mib import (
InterfaceMib,
)
from splunk_connect_for_snmp_poller.utilities import multi_key_lookup
from splunk_connect_for_snmp_poller.utilities import (
multi_key_lookup,
)

logger = logging.getLogger(__name__)

Expand All @@ -41,9 +43,23 @@ def extract_network_interface_data_from_config(config_as_dict):
for key in splunk_dimension.keys():
result.append(
{
"oid_name": f"{InterfaceMib.IF_MIB_METRIC_SUFFIX}{key}",
"oid_name": f"{InterfaceMib.IF_MIB_METRIC_SUFFIX}{key}_",
"splunk_dimension_name": splunk_dimension[key],
}
)
logger.info(f"IF-MIB additional attributes for Splunk: {result}")
return result


def extract_network_interface_data_from_walk(config_as_dict, if_mib_walk_data):
result = []
network_data = InterfaceMib(if_mib_walk_data)
if network_data.has_consistent_data():
enricher_fields = extract_network_interface_data_from_config(config_as_dict)
for data in enricher_fields:
splunk_dimension = data["splunk_dimension_name"]
current_result = network_data.extract_custom_field(data["oid_name"])
if len(current_result) > 0:
result.append({f"{splunk_dimension}": current_result})

return result
97 changes: 97 additions & 0 deletions tests/test_config_input_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#
# 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.
#

parsed_config_root_with_error = {
"enricher_with_error": {
"oidFamily": {
"IF-MIB": [
{"ifIndex": "interface_index"},
{"ifDescr": "interface_desc"},
]
}
}
}

parsed_config_family_with_error = {
"enricher": {
"oidFamily_with_error": {
"IF-MIB": [
{"ifIndex": "interface_index"},
{"ifDescr": "interface_desc"},
]
}
}
}

parsed_config_if_mib_with_error = {
"enricher": {
"oidFamily": {
"IF-MIB_with_error": [
{"ifIndex": "interface_index"},
{"ifDescr": "interface_desc"},
]
}
}
}

parsed_config_if_mib_without_elements = {"enricher": {"oidFamily": {"IF-MIB": []}}}

parsed_config_correct = {
"enricher": {
"oidFamily": {
"IF-MIB": [
{"ifIndex": "interface_index"},
{"ifDescr": "interface_desc"},
]
}
}
}

parsed_config_correct_three_fields = {
"enricher": {
"oidFamily": {
"IF-MIB": [
{"ifIndex": "interface_index"},
{"ifDescr": "interface_desc"},
{"ifInUcastPkts": "total_in_packets"},
]
}
}
}

parsed_config_correct_one_non_existing_field = {
"enricher": {
"oidFamily": {
"IF-MIB": [
{"ifIndex": "interface_index"},
{"ifDescr": "interface_desc"},
{"ifUknownField": "unknown_field"},
]
}
}
}

parsed_config_duplicate_keys = {
"enricher": {
"oidFamily": {
"IF-MIB": [
{"ifIndex": "interface_index"},
{"ifIndex": "interface_index"},
{"ifIndex": "interface_index_2"},
]
}
}
}
16 changes: 4 additions & 12 deletions tests/test_interface_mib.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,19 @@
#

import logging
import os
from unittest import TestCase

from splunk_connect_for_snmp_poller.manager.realtime.interface_mib import (
InterfaceMib,
)
from tests.test_utils import load_test_data
from tests.test_utils import (
file_data_path,
load_test_data,
)

logger = logging.getLogger(__name__)


def file_data_path(data_file_name):
current_dir = os.getcwd()
relative_data_file_path = os.path.join("mib_walk_data", data_file_name)
if current_dir.endswith("tests"):
file_path = os.path.join(current_dir, relative_data_file_path)
else:
file_path = os.path.join(current_dir, "tests", relative_data_file_path)
return file_path


class TestInterfaceMib(TestCase):
def test_loaded_walk_data_for_if_mib(self):
file_path = file_data_path("if_mib_walk.json")
Expand Down
120 changes: 73 additions & 47 deletions tests/test_static_config_data_for_network_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,60 +13,32 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
import logging
from unittest import TestCase

from splunk_connect_for_snmp_poller.manager.realtime.interface_mib import (
InterfaceMib,
)
from splunk_connect_for_snmp_poller.manager.static.interface_mib_utililities import (
extract_network_interface_data_from_config,
extract_network_interface_data_from_walk,
)
from tests.test_config_input_data import (
parsed_config_correct,
parsed_config_correct_one_non_existing_field,
parsed_config_correct_three_fields,
parsed_config_duplicate_keys,
parsed_config_family_with_error,
parsed_config_if_mib_with_error,
parsed_config_if_mib_without_elements,
parsed_config_root_with_error,
)
from tests.test_utils import (
file_data_path,
load_test_data,
)

parsed_config_root_with_error = {
"enricher_with_error": {
"oidFamily": {
"IF-MIB": [
{"ifIndex": "interface_index"},
{"ifDescr": "interface_desc"},
]
}
}
}

parsed_config_family_with_error = {
"enricher": {
"oidFamily_with_error": {
"IF-MIB": [
{"ifIndex": "interface_index"},
{"ifDescr": "interface_desc"},
]
}
}
}

parsed_config_if_mib_with_error = {
"enricher": {
"oidFamily": {
"IF-MIB_with_error": [
{"ifIndex": "interface_index"},
{"ifDescr": "interface_desc"},
]
}
}
}

parsed_config_if_mib_without_elements = {"enricher": {"oidFamily": {"IF-MIB": []}}}

parsed_config_correct = {
"enricher": {
"oidFamily": {
"IF-MIB": [
{"ifIndex": "interface_index"},
{"ifDescr": "interface_desc"},
]
}
}
}
logger = logging.getLogger(__name__)


class ExtractEnricherDataFromConfigTest(TestCase):
Expand All @@ -90,12 +62,66 @@ def test_correct_config(self):
self.assertTrue(len(result) == 2)
expected_result = [
{
"oid_name": f"{InterfaceMib.IF_MIB_METRIC_SUFFIX}ifIndex",
"oid_name": f"{InterfaceMib.IF_MIB_METRIC_SUFFIX}ifIndex_",
"splunk_dimension_name": "interface_index",
},
{
"oid_name": f"{InterfaceMib.IF_MIB_METRIC_SUFFIX}ifDescr",
"oid_name": f"{InterfaceMib.IF_MIB_METRIC_SUFFIX}ifDescr_",
"splunk_dimension_name": "interface_desc",
},
]
self.assertEqual(result, expected_result)

def test_duplicate_keys(self):
result = extract_network_interface_data_from_config(
parsed_config_duplicate_keys
)
self.assertTrue(len(result) == 3)
expected_result = [
{
"oid_name": "sc4snmp.IF-MIB.ifIndex_",
"splunk_dimension_name": "interface_index",
},
{
"oid_name": "sc4snmp.IF-MIB.ifIndex_",
"splunk_dimension_name": "interface_index",
},
{
"oid_name": "sc4snmp.IF-MIB.ifIndex_",
"splunk_dimension_name": "interface_index_2",
},
]
self.assertEqual(result, expected_result)


class ExtractEnricherDataFromSNMPWalkTest(TestCase):
def test_basic_integration(self):
file_path = file_data_path("if_mib_walk.json")
if_mibs = load_test_data(file_path)
self.assertIsNotNone(if_mibs)

result = extract_network_interface_data_from_walk(
parsed_config_correct_three_fields, if_mibs
)
self.assertTrue(len(result) == 3)
expected_result = [
{"interface_index": ["1", "2"]},
{"interface_desc": ["lo", "eth0"]},
{"total_in_packets": ["51491148", "108703537"]},
]
self.assertEqual(result, expected_result)

def test_basic_integration_one_non_existing_field(self):
file_path = file_data_path("if_mib_walk.json")
if_mibs = load_test_data(file_path)
self.assertIsNotNone(if_mibs)

result = extract_network_interface_data_from_walk(
parsed_config_correct_one_non_existing_field, if_mibs
)
self.assertTrue(len(result) == 2)
expected_result = [
{"interface_index": ["1", "2"]},
{"interface_desc": ["lo", "eth0"]},
]
self.assertEqual(result, expected_result)
15 changes: 14 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
# limitations under the License.
#

from jsoncomment import JsonComment
import os

from jsoncomment import (
JsonComment,
)


def load_test_data(data_file_path):
if data_file_path and os.path.exists(data_file_path):
Expand All @@ -31,6 +34,16 @@ def fake_walk_handler(simulator_ifmib_walk_data):
yield translated_metric


def file_data_path(data_file_name):
current_dir = os.getcwd()
relative_data_file_path = os.path.join("mib_walk_data", data_file_name)
if current_dir.endswith("tests"):
file_path = os.path.join(current_dir, relative_data_file_path)
else:
file_path = os.path.join(current_dir, "tests", relative_data_file_path)
return file_path


if __name__ == "__main__":
file_path = "mib_walk_data/if_mib_walk.json"

Expand Down

0 comments on commit ae3647e

Please sign in to comment.