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

Commit

Permalink
get_with_name raises exception on name duplication in kubeconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
roycaihw authored and ltamaster committed Mar 19, 2018
1 parent 8fb6c20 commit 2baecce
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
11 changes: 10 additions & 1 deletion config/kube_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,14 +399,23 @@ def get_with_name(self, name, safe=False):
raise ConfigException(
'Invalid kube-config file. Expected %s to be a list'
% self.name)
result = None
for v in self.value:
if 'name' not in v:
raise ConfigException(
'Invalid kube-config file. '
'Expected all values in %s list to have \'name\' key'
% self.name)
if v['name'] == name:
return ConfigNode('%s[name=%s]' % (self.name, name), v)
if result is None:
result = v
else:
raise ConfigException(
'Invalid kube-config file. '
'Expected only one object with name %s in %s list'
% (name, self.name))
if result is not None:
return ConfigNode('%s[name=%s]' % (self.name, name), result)
if safe:
return None
raise ConfigException(
Expand Down
13 changes: 11 additions & 2 deletions config/kube_config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,15 +193,19 @@ class TestConfigNode(BaseTestCase):
"with_names": [{"name": "test_name", "value": "test_value"},
{"name": "test_name2",
"value": {"key1", "test"}},
{"name": "test_name3", "value": [1, 2, 3]}]}
{"name": "test_name3", "value": [1, 2, 3]}],
"with_names_dup": [{"name": "test_name", "value": "test_value"},
{"name": "test_name",
"value": {"key1", "test"}},
{"name": "test_name3", "value": [1, 2, 3]}]}

def setUp(self):
super(TestConfigNode, self).setUp()
self.node = ConfigNode("test_obj", self.test_obj)

def test_normal_map_array_operations(self):
self.assertEqual("test", self.node['key1'])
self.assertEqual(4, len(self.node))
self.assertEqual(5, len(self.node))

self.assertEqual("test_obj/key2", self.node['key2'].name)
self.assertEqual(["a", "b", "c"], self.node['key2'].value)
Expand Down Expand Up @@ -248,6 +252,11 @@ def test_get_with_name_on_name_does_not_exists(self):
lambda: self.node['with_names'].get_with_name('no-name'),
"Expected object with name no-name in test_obj/with_names list")

def test_get_with_name_on_duplicate_name(self):
self.expect_exception(
lambda: self.node['with_names_dup'].get_with_name('test_name'),
"Expected only one object with name test_name in test_obj/with_names_dup list")


class FakeConfig:

Expand Down

0 comments on commit 2baecce

Please sign in to comment.