From 42837a4bfe8732986551c9518bb12d0f28ebb30e Mon Sep 17 00:00:00 2001 From: ahothan Date: Wed, 23 Aug 2017 12:15:14 -0700 Subject: [PATCH] NFVBENCH-8 config checking fails with Exception TypeError: string indices must be integers, not str Change-Id: Ic3263374efffc9ea2566e107b99f47fb3b0c6edf Signed-off-by: ahothan --- nfvbench/config.py | 20 +++++++++++--------- nfvbench/nfvbench.py | 8 -------- test/test_nfvbench.py | 40 ++++++++++++++++++++++++++-------------- 3 files changed, 37 insertions(+), 31 deletions(-) diff --git a/nfvbench/config.py b/nfvbench/config.py index a0587b6..df91454 100644 --- a/nfvbench/config.py +++ b/nfvbench/config.py @@ -14,6 +14,7 @@ # from attrdict import AttrDict +from log import LOG import yaml @@ -30,6 +31,7 @@ def config_load(file_name, from_cfg=None): .format(file_name)) if from_cfg: + _validate_config(cfg, from_cfg) cfg = from_cfg + cfg return cfg @@ -44,11 +46,12 @@ def config_loads(cfg_text, from_cfg=None): # empty string cfg = AttrDict() if from_cfg: + _validate_config(cfg, from_cfg) return from_cfg + cfg return cfg -def get_err_config(subset, superset): +def _get_err_config(subset, superset): result = {} for k, v in subset.items(): if k not in superset: @@ -58,17 +61,16 @@ def get_err_config(subset, superset): result.update({k: v}) continue if isinstance(v, dict): - res = get_err_config(v, superset[k]) + res = _get_err_config(v, superset[k]) if res: result.update({k: res}) if not result: return None return result - -def test_config(): - cfg = config_load('a1.yaml') - cfg = config_load('a2.yaml', cfg) - cfg = config_loads('color: 500', cfg) - config_loads('') - config_loads('#') +def _validate_config(subset, superset): + err_cfg = _get_err_config(subset, superset) + if err_cfg: + err_msg = 'Unknown options found in config file/string: ' + str(err_cfg) + LOG.error(err_msg) + raise Exception(err_msg) diff --git a/nfvbench/nfvbench.py b/nfvbench/nfvbench.py index dccd63c..e9594d5 100644 --- a/nfvbench/nfvbench.py +++ b/nfvbench/nfvbench.py @@ -21,7 +21,6 @@ from chain_runner import ChainRunner from collections import defaultdict from config import config_load from config import config_loads -from config import get_err_config import copy import credentials import datetime @@ -472,13 +471,6 @@ def main(): LOG.info('Loading configuration string: ' + opts.config) config = config_loads(opts.config, config) - # Making sure no unknown option is given - err_config = get_err_config(config, default_cfg) - if err_config: - err_msg = 'Unknown options found in config file/string: ' + err_config - LOG.error(err_msg) - raise Exception(err_msg) - # traffic profile override options override_custom_traffic(config, opts.frame_sizes, opts.unidir) diff --git a/test/test_nfvbench.py b/test/test_nfvbench.py index ad78a9e..1d985b8 100644 --- a/test/test_nfvbench.py +++ b/test/test_nfvbench.py @@ -16,7 +16,7 @@ from attrdict import AttrDict import logging -from nfvbench.config import get_err_config +from nfvbench.config import config_loads from nfvbench.connection import SSH from nfvbench.credentials import Credentials from nfvbench.fluentd import FluentLogHandler @@ -648,20 +648,32 @@ def test_no_credentials(): def test_config(): refcfg = {1: 100, 2: {21: 100, 22: 200}, 3: None} - assert(get_err_config({}, refcfg) is None) - assert(get_err_config({1: 10}, refcfg) is None) - assert(get_err_config({2: {21: 1000}}, refcfg) is None) - assert(get_err_config({3: "abc"}, refcfg) is None) + res1 = {1: 10, 2: {21: 100, 22: 200}, 3: None} + res2 = {1: 100, 2: {21: 1000, 22: 200}, 3: None} + res3 = {1: 100, 2: {21: 100, 22: 200}, 3: "abc"} + assert(config_loads("{}", refcfg) == refcfg) + assert(config_loads("{1: 10}", refcfg) == res1) + assert(config_loads("{2: {21: 1000}}", refcfg) == res2) + assert(config_loads('{3: "abc"}', refcfg) == res3) + # correctly fails - assert(get_err_config({4: 0}, refcfg) == {4: 0}) - assert(get_err_config({2: {21: 100, 30: 50}}, refcfg) == {2: {30: 50}}) - assert(get_err_config({2: {0: 1, 1: 2}}, refcfg) == {2: {0: 1, 1: 2}}) - assert(get_err_config({2: {0: 1, 1: 2}, 5: 5}, refcfg) == {2: {0: 1, 1: 2}, 5: 5}) - # invalid value type - assert(get_err_config({1: 'abc', 2: {21: 0}}, refcfg) == {1: 'abc'}) - assert(get_err_config({2: 100}, refcfg) == {2: 100}) - # both correctly fail and invalid value type - assert(get_err_config({2: 100, 5: 10}, refcfg) == {2: 100, 5: 10}) + # pairs of input string and expected subset (None if identical) + fail_pairs = [ + ["{4: 0}", None], + ["{2: {21: 100, 30: 50}}", "{2: {30: 50}}"], + ["{2: {0: 1, 1: 2}, 5: 5}", None], + ["{1: 'abc', 2: {21: 0}}", "{1: 'abc'}"], + ["{2: 100}", None] + ] + for fail_pair in fail_pairs: + with pytest.raises(Exception) as e_info: + config_loads(fail_pair[0], refcfg) + expected = fail_pair[1] + if expected is None: + expected = fail_pair[0] + assert expected in e_info.value.message + + def test_fluentd(): logger = logging.getLogger('fluent-logger') -- 2.16.6