Provide option to only validate template

The heat-translator users may only want to validate template without an actual
translation. Create a new optional arg that will only validate template
without running translation.

Change-Id: Ibd17042b365c4c24940e693b87f908a38c681391
Partially Implements: blueprint template-validation-only
This commit is contained in:
spzala 2015-12-14 06:27:35 -08:00
parent e627d8b5c6
commit de2f25b6cf
3 changed files with 66 additions and 8 deletions

View File

@ -21,7 +21,7 @@ from toscaparser.utils.urlutils import UrlUtils
from translator.hot.tosca_translator import TOSCATranslator
"""
Test the heat-translator from command line as:
Test the heat-translator translation from command line as:
#heat-translator
--template-file=<path to the YAML template>
--template-type=<type of template e.g. tosca>
@ -31,7 +31,10 @@ Takes three user arguments,
2. Path to the file that needs to be translated (required)
3. Input parameters (optional)
This is an entry point for testing purpose on CLI.
In order to use heat-translator to only validate template,
without actual translation, pass --validate-only=true along with
other required arguments.
"""
log = logging.getLogger("heat-translator")
@ -68,16 +71,31 @@ class TranslatorShell(object):
raise ValueError(_("%(value)s is not a valid template type.")
% {'value': template_type})
parsed_params = {}
validate_only = None
if len(args) > 2:
parsed_params = self._parse_parameters(args[2])
parameters = None
for arg in args:
if "--validate-only=" in arg:
validate_only = arg
if "--parameters=" in arg:
parameters = arg
if parameters:
parsed_params = self._parse_parameters(parameters)
a_file = os.path.isfile(path)
a_url = UrlUtils.validate_url(path) if not a_file else False
if a_file or a_url:
heat_tpl = self._translate(template_type, path, parsed_params,
a_file)
if heat_tpl:
self._write_output(heat_tpl)
run_only_validation = False
if validate_only:
value = validate_only.split('-validate-only=')[1].lower()
if template_type == 'tosca' and value == 'true':
run_only_validation = True
if run_only_validation:
ToscaTemplate(path, parsed_params, a_file)
else:
heat_tpl = self._translate(template_type, path, parsed_params,
a_file)
if heat_tpl:
self._write_output(heat_tpl)
else:
raise ValueError(_("The path %(path)s is not a valid file"
" or URL.") % {'path': path})

View File

@ -0,0 +1,23 @@
tosca_definitions: tosca_simple_yaml_1_0
description: Template with invalid version and topology_template section.
topology_template:
node_temp:
my_server:
type: tosca.nodes.Compute
capabilities:
# Host container properties
host:
properties:
num_cpus: 2
disk_size: 10 GB
mem_size: 512 MB
# Guest Operating System properties
os:
properties:
# host Operating System image properties
architecture: x86_64
type: Linux
distribution: RHEL
version: 6.5

View File

@ -12,6 +12,7 @@
import os
from toscaparser.common import exception
from toscaparser.utils.gettextutils import _
import translator.shell as shell
from translator.tests.base import TestCase
@ -23,6 +24,7 @@ class ShellTest(TestCase):
"data/tosca_helloworld.yaml")
template_file = '--template-file=' + tosca_helloworld
template_type = '--template-type=tosca'
template_validation = "--validate-only=true"
def test_missing_arg(self):
error = self.assertRaises(ValueError, shell.main, '')
@ -80,3 +82,18 @@ class ShellTest(TestCase):
shell.main([template, self.template_type, parameters])
except Exception:
self.fail(_('The program raised an exception unexpectedly.'))
def test_validate_only(self):
try:
shell.main([self.template_file, self.template_type,
self.template_validation])
except Exception:
self.fail(_('The program raised an exception unexpectedly.'))
template = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"data/tosca_helloworld_invalid.yaml")
invalid_template = '--template-file=' + template
self.assertRaises(exception.ValidationError, shell.main,
[invalid_template, self.template_type,
self.template_validation])