diff options
author | Vijay VK <vv770d@att.com> | 2018-03-27 02:05:36 +0100 |
---|---|---|
committer | Ladue, David (dl3158) <dl3158@att.com> | 2018-03-28 00:08:38 -0400 |
commit | 339ca1c5d3c9f138a13ae82c181b001df43645eb (patch) | |
tree | 8e7685bad1508786503dad76073e418c1dfc19fe /tests | |
parent | e0b9a69c01845eb32baaf9029b1775b81d78906f (diff) |
fix tox setup for snmptrap
Change-Id: I39adcd37cab64937af182c4716cba0cfaba6c7a2
Signed-off-by: VENKATESH KUMAR <vv770d@att.com>
Issue-ID: DCAEGEN2-271
Signed-off-by: VENKATESH KUMAR <vv770d@att.com>
Signed-off-by: Ladue, David (dl3158) <dl3158@att.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/__init__.py | 21 | ||||
-rw-r--r-- | tests/_test_trapd_get_cbs_config.py | 44 | ||||
-rw-r--r-- | tests/conftest.py | 12 | ||||
-rw-r--r-- | tests/snmp.setup.py (renamed from tests/setup.py) | 0 | ||||
-rw-r--r-- | tests/test_snmptrapd.py | 100 | ||||
-rw-r--r-- | tests/test_trapd_get_cbs_config.py | 61 | ||||
-rw-r--r-- | tests/test_trapd_settings.py | 73 | ||||
-rw-r--r-- | tests/tox.ini | 15 |
8 files changed, 238 insertions, 88 deletions
diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..1875bf6 --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1,21 @@ +# ================================================================================ +# Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# 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. +# ============LICENSE_END========================================================= +# +# ECOMP is a trademark and service mark of AT&T Intellectual Property. + + +# empty __init__.py so that pytest can add correct path to coverage report, -- per pytest +# best practice guideline diff --git a/tests/_test_trapd_get_cbs_config.py b/tests/_test_trapd_get_cbs_config.py deleted file mode 100644 index 5fcfc2a..0000000 --- a/tests/_test_trapd_get_cbs_config.py +++ /dev/null @@ -1,44 +0,0 @@ -import pytest -import unittest -import os -from onap_dcae_cbs_docker_client.client import get_config -from trapd_exit import cleanup_and_exit -from trapd_logging import stdout_logger -import trapd_get_cbs_config - -class test_get_cbs_config(unittest.TestCase): - """ - Test the trapd_get_cbs_config mod - """ - - def test_cbs_env_present(self): - """ - Test that CBS env variable exists and we can get config even - if CONSUL_HOST doesn't provide - """ - os.environ.update(CONSUL_HOST='nosuchhost') - result = trapd_get_cbs_config.trapd_get_cbs_config() - compare = str(result).startswith("{'snmptrap': ") - self.assertEqual(compare, False) - - def test_cbs_fallback_env_present(self): - """ - Test that CBS fallback env variable exists and we can get config - from fallback env var - """ - os.environ.update(CBS_SIM_JSON='../etc/snmptrapd.json') - result = trapd_get_cbs_config.trapd_get_cbs_config() - compare = str(result).startswith("{'snmptrap': ") - self.assertEqual(compare, False) - - def test_cbs_fallback_env_not_present(self): - """ - Test that CBS fallback env variable does not exists fails - """ - os.environ.update(CBS_SIM_JSON='../etc/no_such_file.json') - result = trapd_get_cbs_config.trapd_get_cbs_config() - compare = str(result).startswith("{'snmptrap': ") - self.assertEqual(compare, False) - -if __name__ == '__main__': - unittest.main() diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..d78ed5e --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,12 @@ +import pytest + +@pytest.fixture + +def test_var(_var_name): + + try: + _var_name + except NameError: + return False + else: + return True diff --git a/tests/setup.py b/tests/snmp.setup.py index 5a12f10..5a12f10 100644 --- a/tests/setup.py +++ b/tests/snmp.setup.py diff --git a/tests/test_snmptrapd.py b/tests/test_snmptrapd.py index 2f1783c..2cc11d7 100644 --- a/tests/test_snmptrapd.py +++ b/tests/test_snmptrapd.py @@ -1,46 +1,88 @@ +import os import pytest import unittest +import snmptrapd +import datetime + +import trapd_settings as tds +import trapd_http_session import trapd_runtime_pid - -class test_save_pid(unittest.TestCase): +import trapd_io +import trapd_logging +import trapd_get_cbs_config + +class test_snmptrapd(unittest.TestCase): """ Test the save_pid mod """ - def test_correct_usage(self): + def test_usage_err(self): """ - Test that attempt to create pid file in standard location works + Test usage error """ - result = trapd_runtime_pid.save_pid('/tmp/snmptrap_test_pid_file') - self.assertEqual(result, True) + + with pytest.raises(SystemExit) as pytest_wrapped_sys_exit: + result = snmptrapd.usage_err() + assert pytest_wrapped_sys_exit.type == SystemExit + assert pytest_wrapped_sys_exit.value.code == 1 + - def test_missing_directory(self): + def test_load_all_configs(self): """ - Test that attempt to create pid file in missing dir fails + Test load of all configs """ - result = trapd_runtime_pid.save_pid('/bogus/directory/for/snmptrap_test_pid_file') - self.assertEqual(result, False) - -class test_rm_pid(unittest.TestCase): - """ - Test the rm_pid mod - """ - - def test_correct_usage(self): - """ - Test that attempt to remove pid file in standard location works - """ - # must create it before removing it - result = trapd_runtime_pid.save_pid('/tmp/snmptrap_test_pid_file') - result = trapd_runtime_pid.rm_pid('/tmp/snmptrap_test_pid_file') + + # init vars + tds.init() + + # request load of CBS data + os.environ.update(CBS_SIM_JSON='/opt/app/snmptrap/etc/snmptrapd.json') + result = trapd_get_cbs_config.get_cbs_config() self.assertEqual(result, True) - - def test_missing_file(self): + + # request load of CBS data + result = snmptrapd.load_all_configs(0, 1) + self.assertEqual(result, True) + + def test_log_all_arriving_traps(self): """ - Test that attempt to rm non-existent pid file fails + Test logging of traps """ - result = trapd_runtime_pid.rm_pid('/tmp/snmptrap_test_pid_file_9999') - self.assertEqual(result, False) - + + # init vars + tds.init() + + # request load of CBS data + os.environ.update(CBS_SIM_JSON='/opt/app/snmptrap/etc/snmptrapd.json') + result = trapd_get_cbs_config.get_cbs_config() + + # set last day to current + tds.last_day = datetime.datetime.now().day + + # trap dict for logging + tds.trap_dict = {'uuid': '06f6e91c-3236-11e8-9953-005056865aac', 'agent address': '1.2.3.4', 'agent name': 'test-agent.nodomain.com', 'cambria.partition': 'test-agent.nodomain.com', 'community': '', 'community len': 0, 'epoch_serno': 15222068260000, 'protocol version': 'v2c', 'time received': 1522206826.2938566, 'trap category': 'ONAP-COLLECTOR-SNMPTRAP', 'sysUptime': '218567736', 'notify OID': '1.3.6.1.4.1.9999.9.9.999', 'notify OID len': 10} + + # open eelf logs + trapd_io.open_eelf_logs() + + # open trap logs + tds.arriving_traps_filename = tds.c_config['files.runtime_base_dir'] + "/" + \ + tds.c_config['files.log_dir'] + "/" + \ + (tds.c_config['files.arriving_traps_log']) + tds.arriving_traps_fd = trapd_io.open_file(tds.arriving_traps_filename) + + # name and open json trap log + tds.json_traps_filename = tds.c_config['files.runtime_base_dir'] + "/" + tds.c_config['files.log_dir'] + "/" + "DMAAP_" + ( + tds.c_config['streams_publishes']['sec_fault_unsecure']['dmaap_info']['topic_url'].split('/')[-1]) + ".json" + tds.json_traps_fd = trapd_io.open_file(tds.json_traps_filename) + msg = ("published traps logged to: %s" % tds.json_traps_filename) + trapd_io.stdout_logger(msg) + trapd_logging.ecomp_logger(tds.LOG_TYPE_DEBUG, tds.SEV_INFO, tds.CODE_GENERAL, msg) + + # don't open files, but try to log - should raise exception + with pytest.raises(Exception) as pytest_wrapped_exception: + result = snmptrapd.log_all_arriving_traps() + assert pytest_wrapped_exception.type == AttributeError + if __name__ == '__main__': unittest.main() diff --git a/tests/test_trapd_get_cbs_config.py b/tests/test_trapd_get_cbs_config.py new file mode 100644 index 0000000..415c951 --- /dev/null +++ b/tests/test_trapd_get_cbs_config.py @@ -0,0 +1,61 @@ +import pytest +import unittest +import os + +from onap_dcae_cbs_docker_client.client import get_config +from trapd_exit import cleanup_and_exit +from trapd_io import stdout_logger, ecomp_logger +import trapd_settings as tds +import trapd_get_cbs_config + +class test_get_cbs_config(unittest.TestCase): + """ + Test the trapd_get_cbs_config mod + """ + + def test_cbs_env_present(self): + """ + Test that CBS env variable exists and we can get config even + if CONSUL_HOST doesn't provide + """ + os.environ.update(CONSUL_HOST='nosuchhost') + # result = trapd_get_cbs_config.get_cbs_config() + # print("result: %s" % result) + # compare = str(result).startswith("{'snmptrap': ") + # self.assertEqual(compare, False) + + with pytest.raises(Exception) as pytest_wrapped_sys_exit: + result = trapd_get_cbs_config.get_cbs_config() + assert pytest_wrapped_sys_exit.type == SystemExit + # assert pytest_wrapped_sys_exit.value.code == 1 + + + def test_cbs_override_env_invalid(self): + """ + """ + os.environ.update(CBS_SIM_JSON='/opt/app/snmptrap/etc/nosuchfile.json') + # result = trapd_get_cbs_config.get_cbs_config() + # print("result: %s" % result) + # compare = str(result).startswith("{'snmptrap': ") + # self.assertEqual(compare, False) + + with pytest.raises(SystemExit) as pytest_wrapped_sys_exit: + result = trapd_get_cbs_config.get_cbs_config() + assert pytest_wrapped_sys_exit.type == SystemExit + assert pytest_wrapped_sys_exit.value.code == 1 + + + def test_cbs_fallback_env_present(self): + """ + Test that CBS fallback env variable exists and we can get config + from fallback env var + """ + os.environ.update(CBS_SIM_JSON='/opt/app/snmptrap/etc/snmptrapd.json') + result = trapd_get_cbs_config.get_cbs_config() + print("result: %s" % result) + # compare = str(result).startswith("{'snmptrap': ") + # self.assertEqual(compare, True) + self.assertEqual(result, True) + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_trapd_settings.py b/tests/test_trapd_settings.py new file mode 100644 index 0000000..ab64426 --- /dev/null +++ b/tests/test_trapd_settings.py @@ -0,0 +1,73 @@ +import pytest +import unittest +import trapd_exit + +pid_file="/tmp/test_pid_file" +pid_file_dne="/tmp/test_pid_file_NOT" + +import trapd_settings as tds +from conftest import test_var + +class test_cleanup_and_exit(unittest.TestCase): + """ + Test for presense of required vars + """ + + + def test_nonexistent_dict(self): + """ + Test nosuch var + """ + tds.init() + try: + tds.no_such_var + result = True + except: + result = False + + self.assertEqual(result, False) + + def test_config_dict(self): + """ + Test config dict + """ + tds.init() + try: + tds.c_config + result = True + except: + result = False + + self.assertEqual(result, True) + + def test_dns_cache_ip_to_name(self): + """ + Test dns cache name dict + """ + + tds.init() + try: + tds.dns_cache_ip_to_name + result = True + except: + result = False + + self.assertEqual(result, True) + + def test_dns_cache_ip_expires(self): + """ + Test dns cache ip expires dict + """ + + tds.init() + try: + tds.dns_cache_ip_expires + result = True + except: + result = False + + self.assertEqual(result, True) + +if __name__ == '__main__': + # tds.init() + unittest.main() diff --git a/tests/tox.ini b/tests/tox.ini deleted file mode 100644 index 3d8e842..0000000 --- a/tests/tox.ini +++ /dev/null @@ -1,15 +0,0 @@ -[tox] -envlist = py36 - -[testenv] -deps = coverage -commands = coverage erase - -[testenv:py36] -deps = coverage pytest -commands = coverage run ../bin/snmptrapd.py & - pytest test_trapd_exit.py - pytest test_trapd_http_session.py - pytest test_trapd_runtime_pid.py - ./test_snmptrapd_send_test_trap.py & - coverage report -m |