From 3a10532067963e67def8619fbb0929affc176a55 Mon Sep 17 00:00:00 2001 From: spzala Date: Thu, 16 Apr 2015 21:14:34 -0700 Subject: [PATCH] TOSCA: Create a test CLI program This program provide a good usage to test the Heat-Translator tool on the command line with expection of similar usage via Heat python client in near future. The TOSCA hello world use case is implemented for quick testing as well. implements blueprint tosca-translator-cli-test-utility Change-Id: Ia4ddf65aafcbe440ea9551ccc2648de4a7c88c97 --- doc/source/usage.rst | 29 ++++- heat_translator.py | 101 ++++++++++++++++++ translator/hot/tosca/tosca_compute.py | 8 +- .../tests/test_tosca_hot_translation.py | 11 ++ .../data/hot_output/hot_tosca_helloworld.yaml | 15 +++ .../toscalib/tests/data/tosca_helloworld.yaml | 23 ++++ 6 files changed, 180 insertions(+), 7 deletions(-) create mode 100644 heat_translator.py create mode 100644 translator/toscalib/tests/data/hot_output/hot_tosca_helloworld.yaml create mode 100644 translator/toscalib/tests/data/tosca_helloworld.yaml diff --git a/doc/source/usage.rst b/doc/source/usage.rst index f25160cd..14ad376f 100644 --- a/doc/source/usage.rst +++ b/doc/source/usage.rst @@ -2,7 +2,30 @@ Usage ===== -Heat-Translator project is planned to be made available via Heat Client an -which then can be invoked on command line as, +Heat-Translator project is planned to be made available via python-heatclient. + +For now, it can be tested on the command line as below:: + + git clone https://github.com/openstack/heat-translator + python heat_translator.py --template-file== --template-type= --parameters="purpose=test" + +The heat_translator.py test program is at the root level of the project. The program has currently tested with TOSCA templates. +It requires two arguments:: + +1. Path to the file that needs to be translated +2. Type of translation (e.g. tosca) + +An optional argument can be provided to handle user inputs parameters. + +For example, a TOSCA hello world template can be translated by running the following command from the directory where you have cloned the project:: + + python heat_translator.py --template-file=translator/toscalib/tests/data/tosca_helloworld.yaml --template-type=tosca + +This should produce a translated Heat Orchestration Template on the command line. In the near future, new options will be added to save the output +to a file. When deploy the translated template with Heat, please keep in mind that you have image registered in the Glance. The Heat-Translator +project sets flavor and image from a pre-defined set of values (as listed in /home/openstack/heat-translator/translator/hot/tosca/tosca_compute.py) +with the best possible match to the constraints defined in the TOSCA template. If there is no possible match found, a null value is set for now. +Per the future plan, an image and flavor will be provided from an online repository. + +Once the project is made available in python-heatclient, it will be enabled to deploy translated output template seamlessly via Heat. -heat-translator \ No newline at end of file diff --git a/heat_translator.py b/heat_translator.py new file mode 100644 index 00000000..a085691f --- /dev/null +++ b/heat_translator.py @@ -0,0 +1,101 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + + +import os +import sys + +from translator.hot.tosca_translator import TOSCATranslator +from translator.toscalib.tosca_template import ToscaTemplate +from translator.toscalib.utils.gettextutils import _ + +""" +Test the heat-translator from command line as: +#python heat_translator.py + --template-file= + --template-type= + --parameters="purpose=test" +Takes three user arguments, +1. type of translation (e.g. tosca) (required) +2. Path to the file that needs to be translated (required) +3. Input parameters (optional) + +This should be only used for testing purpose. The proper use of +Heat-Translator tool is via python-heatclient once it made available there. +""" + + +def main(): + if len(sys.argv) < 3: + msg = _("The program requires minimum two arguments. " + "Please refer to the usage documentation.") + raise ValueError(msg) + if "--template-file=" not in sys.argv[1]: + msg = _("The program expects --template-file as first argument. " + "Please refer to the usage documentation.") + raise ValueError(msg) + if "--template-type=" not in sys.argv[2]: + msg = _("The program expects --template-type as second argument. " + "Please refer to the usage documentation.") + raise ValueError(msg) + path = sys.argv[1].split('--template-file=')[1] + # e.g. --template_file=translator/toscalib/tests/data/tosca_helloworld.yaml + template_type = sys.argv[2].split('--template-type=')[1] + # e.g. --template_type=tosca + supported_types = ['tosca'] + if not template_type: + raise ValueError(_("Template type is needed. For example, 'tosca'")) + elif template_type not in supported_types: + raise ValueError(_("%(value)s is not a valid template type.") + % {'value': template_type}) + parsed_params = {} + if len(sys.argv) > 3: + parsed_params = parse_parameters(sys.argv[3]) + if os.path.isfile(path): + heat_tpl = translate(template_type, path, parsed_params) + if heat_tpl: + write_output(heat_tpl) + else: + raise ValueError(_("%(path)s is not a valid file.") % {'path': path}) + + +def parse_parameters(parameter_list): + parsed_inputs = {} + if parameter_list.startswith('--parameters'): + inputs = parameter_list.split('--parameters=')[1].\ + replace('"', '').split(';') + for param in inputs: + keyvalue = param.split('=') + parsed_inputs[keyvalue[0]] = keyvalue[1] + else: + raise ValueError(_("%(param) is not a valid parameter.") + % parameter_list) + return parsed_inputs + + +def translate(sourcetype, path, parsed_params): + output = None + if sourcetype == "tosca": + tosca = ToscaTemplate(path) + translator = TOSCATranslator(tosca, parsed_params) + output = translator.translate() + return output + + +def write_output(output): + print(output) + +if __name__ == '__main__': + main() diff --git a/translator/hot/tosca/tosca_compute.py b/translator/hot/tosca/tosca_compute.py index d75933ba..a0f5e2ea 100755 --- a/translator/hot/tosca/tosca_compute.py +++ b/translator/hot/tosca/tosca_compute.py @@ -49,10 +49,10 @@ IMAGES = {'ubuntu-software-config-os-init': {'architecture': 'x86_64', 'type': 'Linux', 'distribution': 'CirrOS', 'version': '0.3.1'}, - 'cirros-0.3.2-x86_64-uec': {'architecture': 'x86_64', - 'type': 'Linux', - 'distribution': 'CirrOS', - 'version': '0.3.2'}} + 'rhel-6.5-test-image': {'architecture': 'x86_64', + 'type': 'Linux', + 'distribution': 'RHEL', + 'version': '6.5'}} class ToscaCompute(HotResource): diff --git a/translator/tests/test_tosca_hot_translation.py b/translator/tests/test_tosca_hot_translation.py index 45f799ee..530e5dc6 100644 --- a/translator/tests/test_tosca_hot_translation.py +++ b/translator/tests/test_tosca_hot_translation.py @@ -33,3 +33,14 @@ class ToscaHotTranslationTest(TestCase): params) self.assertEqual({}, diff, ' : ' + json.dumps(diff, indent=4, separators=(', ', ': '))) + + def test_hot_translate_helloworld(self): + tosca_file = \ + '../toscalib/tests/data/tosca_helloworld.yaml' + hot_file = '../toscalib/tests/data/hot_output/' \ + 'hot_tosca_helloworld.yaml' + diff = TranslationUtils.compare_tosca_translation_with_hot(tosca_file, + hot_file, + {}) + self.assertEqual({}, diff, ' : ' + + json.dumps(diff, indent=4, separators=(', ', ': '))) diff --git a/translator/toscalib/tests/data/hot_output/hot_tosca_helloworld.yaml b/translator/toscalib/tests/data/hot_output/hot_tosca_helloworld.yaml new file mode 100644 index 00000000..60e39dc8 --- /dev/null +++ b/translator/toscalib/tests/data/hot_output/hot_tosca_helloworld.yaml @@ -0,0 +1,15 @@ +heat_template_version: 2013-05-23 + +description: > + Template for deploying a single server with predefined properties. + +parameters: {} +resources: + my_server: + type: OS::Nova::Server + properties: + flavor: m1.medium + image: rhel-6.5-test-image + key_name: userkey + user_data_format: SOFTWARE_CONFIG +outputs: {} \ No newline at end of file diff --git a/translator/toscalib/tests/data/tosca_helloworld.yaml b/translator/toscalib/tests/data/tosca_helloworld.yaml new file mode 100644 index 00000000..2247b6cc --- /dev/null +++ b/translator/toscalib/tests/data/tosca_helloworld.yaml @@ -0,0 +1,23 @@ +tosca_definitions_version: tosca_simple_yaml_1_0_0 + +description: Template for deploying a single server with predefined properties. + +topology_template: + node_templates: + my_server: + type: tosca.nodes.Compute + capabilities: + # Host container properties + host: + properties: + num_cpus: 2 + disk_size: 10 GB + mem_size: 4 MB + # Guest Operating System properties + os: + properties: + # host Operating System image properties + architecture: x86_64 + type: Linux + distribution: RHEL + version: 6.5