Skip to content
This repository has been archived by the owner on Mar 13, 2022. It is now read-only.

Commit

Permalink
*Update ExecProvider to use safe_get()
Browse files Browse the repository at this point in the history
*Update unit tests to use ConfigNode() instead of dict()
  • Loading branch information
fillbit committed Oct 11, 2018
1 parent 7d1e449 commit 3682e9b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
9 changes: 7 additions & 2 deletions config/exec_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
15 changes: 10 additions & 5 deletions config/exec_provider_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
Expand Down

0 comments on commit 3682e9b

Please sign in to comment.