From 3682e9b052498bbbac7cd805adaf7ad54212a64b Mon Sep 17 00:00:00 2001 From: Phil Hoffman Date: Thu, 4 Oct 2018 15:46:55 -0400 Subject: [PATCH] *Update ExecProvider to use safe_get() *Update unit tests to use ConfigNode() instead of dict() --- config/exec_provider.py | 9 +++++++-- config/exec_provider_test.py | 15 ++++++++++----- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/config/exec_provider.py b/config/exec_provider.py index 9b8b645c..436942f0 100644 --- a/config/exec_provider.py +++ b/config/exec_provider.py @@ -32,16 +32,21 @@ class ExecProvider(object): """ def __init__(self, exec_config): + """ + exec_config must be of type ConfigNode because we depend on + safe_get(self, key) to correctly handle optional exec provider + config parameters. + """ for key in ['command', 'apiVersion']: if key not in exec_config: raise ConfigException( 'exec: malformed request. missing key \'%s\'' % key) self.api_version = exec_config['apiVersion'] self.args = [exec_config['command']] - if 'args' in exec_config: + if exec_config.safe_get('args'): self.args.extend(exec_config['args']) self.env = os.environ.copy() - if 'env' in exec_config: + if exec_config.safe_get('env'): additional_vars = {} for item in exec_config['env']: name = item['name'] diff --git a/config/exec_provider_test.py b/config/exec_provider_test.py index a564e766..44579beb 100644 --- a/config/exec_provider_test.py +++ b/config/exec_provider_test.py @@ -19,15 +19,18 @@ from .config_exception import ConfigException from .exec_provider import ExecProvider +from .kube_config import ConfigNode class ExecProviderTest(unittest.TestCase): def setUp(self): - self.input_ok = { - 'command': 'aws-iam-authenticator token -i dummy', - 'apiVersion': 'client.authentication.k8s.io/v1beta1' - } + self.input_ok = ConfigNode('test', { + 'command': 'aws-iam-authenticator', + 'args': ['token', '-i', 'dummy'], + 'apiVersion': 'client.authentication.k8s.io/v1beta1', + 'env': None + }) self.output_ok = """ { "apiVersion": "client.authentication.k8s.io/v1beta1", @@ -39,7 +42,9 @@ def setUp(self): """ def test_missing_input_keys(self): - exec_configs = [{}, {'command': ''}, {'apiVersion': ''}] + exec_configs = [ConfigNode('test1', {}), + ConfigNode('test2', {'command': ''}), + ConfigNode('test3', {'apiVersion': ''})] for exec_config in exec_configs: with self.assertRaises(ConfigException) as context: ExecProvider(exec_config)