Skip to content

Commit

Permalink
i-129 Allow dict keys with dashes
Browse files Browse the repository at this point in the history
  • Loading branch information
vadim.reyder authored and rvadim committed Oct 16, 2020
1 parent 6d4895d commit 09f2428
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 12 deletions.
10 changes: 4 additions & 6 deletions k8s_handle/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,9 @@ def load_context_section(section):
return context


def get_all_nested_keys(result, d):
def get_all_root_keys(result, d):
for key, value in d.items():
result.append(key)
if isinstance(d[key], dict):
get_all_nested_keys(result, d[key])

return result

Expand All @@ -202,8 +200,8 @@ def get_vars_with_dashes(vars_list):


def validate_dashes(context):
all_keys = get_all_nested_keys([], context)
all_keys = get_all_root_keys([], context)
dashes = get_vars_with_dashes(all_keys)
if len(dashes) != 0:
raise RuntimeError('Variable names should never include dashes, '
'check your vars, please: {}'.format(', '.join(sorted(dashes))))
raise RuntimeError('Root variable names should never include dashes, '
'check your vars please: {}'.format(', '.join(sorted(dashes))))
10 changes: 10 additions & 0 deletions tests/fixtures/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ common:
k8s_token: token
k8s_ca_base64: ca
k8s_namespace: namespace

var:
router:
your: 1
Expand Down Expand Up @@ -106,3 +107,12 @@ test_groups:
- group:
- template: template2.yaml.j2
- template: template3.yaml.j2

test_dashes:
templates:
- template: template-dashes.yaml.j2
region: us-east1
regionMap:
us-east1: do this
us-west1: do that

10 changes: 7 additions & 3 deletions tests/fixtures/dashes_config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
common:
my-var: my_value
my_file: "{{ file='tests/fixtures/include.yaml' }}"
k8s_master_uri: https://localhost:5000
k8s_token: token
Expand All @@ -9,11 +8,16 @@ common:
router:
your-var: 1
my1: var1

section:
templates:
- template: template1.yaml.j2

allowed:
var:
router:
my-nested-var: var
your: 2

not_allowed:
my-var: my_value
my-var-with-dashes: not allowed

1 change: 1 addition & 0 deletions tests/templates_tests/template-dashes.yaml.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ regionMap[region] }}
8 changes: 5 additions & 3 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,12 @@ def test_dashes_in_var_names(self):
settings.TEMPLATES_DIR = 'templates_tests'
settings.CONFIG_FILE = 'tests/fixtures/dashes_config.yaml'
with self.assertRaises(RuntimeError) as context:
config.load_context_section('section')
self.assertTrue('Variable names should never include dashes, '
'check your vars, please: my-nested-var, my-var, your-var'
config.load_context_section('not_allowed')
self.assertTrue('Root variable names should never include dashes, '
'check your vars please: my-var, my-var-with-dashes'
in str(context.exception), context.exception)
c = config.load_context_section('allowed')
self.assertEqual(c.get('var').get('router').get('your'), 2)

def test_context_update_recursion(self):
my_dict = {
Expand Down
9 changes: 9 additions & 0 deletions tests/test_templating.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,12 @@ def test_filters(self):
{'effect': 'NoSchedule', 'key': 'dedicated', 'operator': 'Equal', 'value': {'hello': 'world'}}
]
self.assertEqual(affinity, actual.get('affinity'))

def test_dashes(self):
r = templating.Renderer(os.path.join(os.path.dirname(__file__), 'templates_tests'))
context = config.load_context_section('test_dashes')
r.generate_by_context(context)
result = '{}/template-dashes.yaml'.format(settings.TEMP_DIR)
with open(result, 'r') as f:
actual = yaml.safe_load(f)
self.assertEqual('do this', actual)

0 comments on commit 09f2428

Please sign in to comment.