Skip to content

Commit e695023

Browse files
authored
Fail config validation if templates are missing. (DataDog#20832)
* Fail config validation if templates are missing. Along the way refactor how we search for the templates * changelog
1 parent 670447e commit e695023

2 files changed

Lines changed: 10 additions & 9 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fail config validation if templates are missing.

datadog_checks_dev/datadog_checks/dev/tooling/commands/validate/config.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Licensed under a 3-clause BSD style license (see LICENSE)
44
import difflib
55
import os
6+
import re
67

78
import click
89
import yaml
@@ -35,8 +36,6 @@
3536

3637
IGNORE_DEFAULT_INSTANCE = {'ceph', 'dotnetclr', 'gunicorn', 'marathon', 'pgbouncer', 'process', 'supervisord'}
3738

38-
TEMPLATES = ['default', 'openmetrics_legacy', 'openmetrics', 'jmx']
39-
4039

4140
@click.command(context_settings=CONTEXT_SETTINGS, short_help='Validate default configuration files')
4241
@click.argument('check', shell_complete=complete_valid_checks, required=False)
@@ -113,6 +112,7 @@ def config(ctx, check, sync, verbose):
113112

114113
if not validate_default_template(spec_file_content):
115114
message = "Missing default template in init_config or instances section"
115+
files_failed[spec_file_path] = True
116116
check_display_queue.append(lambda message=message, **kwargs: echo_failure(message, **kwargs))
117117
annotate_error(spec_file_path, message)
118118

@@ -186,13 +186,13 @@ def validate_default_template(spec_file):
186186
if 'template: init_config' not in spec_file or 'template: instances' not in spec_file:
187187
# This config spec does not have init_config or instances
188188
return True
189-
for line in spec_file.split('\n'):
190-
has_default_templates = any("init_config/{}".format(template) in line for template in TEMPLATES) and any(
191-
"instances/{}".format(template) in line for template in TEMPLATES
192-
)
193-
if has_default_templates:
194-
return True
195-
return False
189+
190+
templates = {
191+
'intances': [f'template: init_config/{t}' for t in ['default', 'openmetrics_legacy', 'openmetrics', 'jmx']],
192+
'init_config': [f'template: init_config/{t}' for t in ['default', 'openmetrics_legacy', 'openmetrics', 'jmx']],
193+
}
194+
# We want both instances and init_config to have at least one template present.
195+
return all(any(re.search(t, spec_file) for t in tpls) for tpls in templates)
196196

197197

198198
def validate_config_legacy(check, check_display_queue, files_failed, files_warned, file_counter):

0 commit comments

Comments
 (0)