2014-09-03 12:34:38 +03:00
|
|
|
# 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 six
|
2015-08-25 08:16:57 -07:00
|
|
|
from toscaparser.common import exception
|
|
|
|
from toscaparser import functions
|
|
|
|
from toscaparser.tests.base import TestCase
|
|
|
|
from toscaparser.tosca_template import ToscaTemplate
|
2015-11-03 15:09:39 -08:00
|
|
|
from toscaparser.utils.gettextutils import _
|
2014-09-03 12:34:38 +03:00
|
|
|
|
|
|
|
|
|
|
|
class IntrinsicFunctionsTest(TestCase):
|
|
|
|
|
|
|
|
tosca_tpl = os.path.join(
|
|
|
|
os.path.dirname(os.path.abspath(__file__)),
|
|
|
|
"data/tosca_single_instance_wordpress.yaml")
|
2015-09-24 10:23:24 -07:00
|
|
|
params = {'db_name': 'my_wordpress', 'db_user': 'my_db_user'}
|
|
|
|
tosca = ToscaTemplate(tosca_tpl, parsed_params=params)
|
2014-09-03 12:34:38 +03:00
|
|
|
|
2015-12-17 10:55:29 +01:00
|
|
|
def _get_node(self, node_name, tosca=None):
|
|
|
|
if tosca is None:
|
|
|
|
tosca = self.tosca
|
2014-09-03 12:34:38 +03:00
|
|
|
return [
|
2015-12-17 10:55:29 +01:00
|
|
|
node for node in tosca.nodetemplates
|
2014-09-03 12:34:38 +03:00
|
|
|
if node.name == node_name][0]
|
|
|
|
|
|
|
|
def _get_operation(self, interfaces, operation):
|
|
|
|
return [
|
|
|
|
interface for interface in interfaces
|
|
|
|
if interface.name == operation][0]
|
|
|
|
|
2015-03-25 15:10:25 +02:00
|
|
|
def _get_property(self, node_template, property_name):
|
2015-03-23 17:14:30 -07:00
|
|
|
return [prop.value for prop in node_template.get_properties_objects()
|
2015-03-25 15:10:25 +02:00
|
|
|
if prop.name == property_name][0]
|
|
|
|
|
2015-09-24 10:23:24 -07:00
|
|
|
def _get_inputs_dict(self):
|
|
|
|
inputs = {}
|
|
|
|
for input in self.tosca.inputs:
|
|
|
|
inputs[input.name] = input.default
|
|
|
|
return inputs
|
2014-09-03 12:34:38 +03:00
|
|
|
|
2015-09-24 10:23:24 -07:00
|
|
|
def _get_input(self, name):
|
|
|
|
self._get_inputs_dict()[name]
|
|
|
|
|
|
|
|
def test_get_property(self):
|
2014-09-03 12:34:38 +03:00
|
|
|
wordpress = self._get_node('wordpress')
|
|
|
|
operation = self._get_operation(wordpress.interfaces, 'configure')
|
2015-09-24 10:23:24 -07:00
|
|
|
wp_db_password = operation.inputs['wp_db_password']
|
|
|
|
self.assertTrue(isinstance(wp_db_password, functions.GetProperty))
|
|
|
|
result = wp_db_password.result()
|
|
|
|
self.assertEqual('wp_pass', result)
|
2014-09-03 12:34:38 +03:00
|
|
|
|
2015-09-24 10:23:24 -07:00
|
|
|
def test_get_property_with_input_param(self):
|
|
|
|
wordpress = self._get_node('wordpress')
|
|
|
|
operation = self._get_operation(wordpress.interfaces, 'configure')
|
|
|
|
wp_db_user = operation.inputs['wp_db_user']
|
|
|
|
self.assertTrue(isinstance(wp_db_user, functions.GetProperty))
|
|
|
|
result = wp_db_user.result()
|
|
|
|
self.assertEqual('my_db_user', result)
|
2014-09-03 12:34:38 +03:00
|
|
|
|
|
|
|
def test_unknown_capability_property(self):
|
2015-11-03 15:09:39 -08:00
|
|
|
self.assertRaises(exception.ValidationError, self._load_template,
|
|
|
|
'functions/test_unknown_capability_property.yaml')
|
|
|
|
exception.ExceptionCollector.assertExceptionMessage(
|
2014-09-24 09:52:33 +03:00
|
|
|
KeyError,
|
2015-11-18 16:25:16 -08:00
|
|
|
_('\'Property "unknown" was not found in capability '
|
2015-11-03 15:09:39 -08:00
|
|
|
'"database_endpoint" of node template "database" referenced '
|
|
|
|
'from node template "database".\''))
|
2014-09-03 12:34:38 +03:00
|
|
|
|
|
|
|
def test_get_input_in_properties(self):
|
|
|
|
mysql_dbms = self._get_node('mysql_dbms')
|
2015-03-23 17:14:30 -07:00
|
|
|
expected_inputs = ['db_root_pwd', 'db_port']
|
|
|
|
props = mysql_dbms.get_properties()
|
|
|
|
for key in props.keys():
|
|
|
|
prop = props[key]
|
|
|
|
self.assertTrue(isinstance(prop.value, functions.GetInput))
|
|
|
|
expected_inputs.remove(prop.value.input_name)
|
|
|
|
self.assertListEqual(expected_inputs, [])
|
2014-09-03 12:34:38 +03:00
|
|
|
|
|
|
|
def test_get_input_validation(self):
|
2015-11-03 15:09:39 -08:00
|
|
|
self.assertRaises(
|
|
|
|
exception.ValidationError, self._load_template,
|
|
|
|
'functions/test_unknown_input_in_property.yaml')
|
|
|
|
exception.ExceptionCollector.assertExceptionMessage(
|
|
|
|
exception.UnknownInputError,
|
2015-11-18 16:25:16 -08:00
|
|
|
_('Unknown input "objectstore_name".'))
|
2015-11-03 15:09:39 -08:00
|
|
|
|
|
|
|
self.assertRaises(
|
|
|
|
exception.ValidationError, self._load_template,
|
|
|
|
'functions/test_unknown_input_in_interface.yaml')
|
|
|
|
exception.ExceptionCollector.assertExceptionMessage(
|
|
|
|
exception.UnknownInputError,
|
2015-11-18 16:25:16 -08:00
|
|
|
_('Unknown input "image_id".'))
|
2015-11-03 15:09:39 -08:00
|
|
|
|
|
|
|
self.assertRaises(
|
|
|
|
exception.ValidationError, self._load_template,
|
|
|
|
'functions/test_invalid_function_signature.yaml')
|
|
|
|
exception.ExceptionCollector.assertExceptionMessage(
|
|
|
|
ValueError,
|
2015-11-18 16:25:16 -08:00
|
|
|
_('Expected one argument for function "get_input" but received '
|
|
|
|
'"[\'cpus\', \'cpus\']".'))
|
2014-09-25 13:28:48 +03:00
|
|
|
|
2015-03-25 15:10:25 +02:00
|
|
|
def test_get_input_default_value_result(self):
|
|
|
|
mysql_dbms = self._get_node('mysql_dbms')
|
2015-06-15 11:38:24 -07:00
|
|
|
dbms_port = self._get_property(mysql_dbms, 'port')
|
2015-03-25 15:10:25 +02:00
|
|
|
self.assertEqual(3306, dbms_port.result())
|
|
|
|
dbms_root_password = self._get_property(mysql_dbms,
|
2015-06-15 11:38:24 -07:00
|
|
|
'root_password')
|
2015-03-25 15:10:25 +02:00
|
|
|
self.assertIsNone(dbms_root_password.result())
|
|
|
|
|
2015-12-17 10:55:29 +01:00
|
|
|
def test_get_property_with_host(self):
|
|
|
|
tosca_tpl = os.path.join(
|
|
|
|
os.path.dirname(os.path.abspath(__file__)),
|
|
|
|
"data/functions/test_get_property_with_host.yaml")
|
|
|
|
mysql_database = self._get_node('mysql_database',
|
|
|
|
ToscaTemplate(tosca_tpl))
|
|
|
|
operation = self._get_operation(mysql_database.interfaces, 'configure')
|
|
|
|
db_port = operation.inputs['db_port']
|
|
|
|
self.assertTrue(isinstance(db_port, functions.GetProperty))
|
|
|
|
result = db_port.result()
|
|
|
|
self.assertEqual(3306, result)
|
|
|
|
test = operation.inputs['test']
|
|
|
|
self.assertTrue(isinstance(test, functions.GetProperty))
|
|
|
|
result = test.result()
|
|
|
|
self.assertEqual(1, result)
|
|
|
|
|
2016-01-11 09:35:45 +01:00
|
|
|
def test_get_property_with_nested_params(self):
|
|
|
|
tosca_tpl = os.path.join(
|
|
|
|
os.path.dirname(os.path.abspath(__file__)),
|
|
|
|
"data/functions/tosca_nested_property_names_indexes.yaml")
|
|
|
|
webserver = self._get_node('wordpress', ToscaTemplate(tosca_tpl))
|
|
|
|
operation = self._get_operation(webserver.interfaces, 'configure')
|
|
|
|
wp_endpoint_prot = operation.inputs['wp_endpoint_protocol']
|
|
|
|
self.assertTrue(isinstance(wp_endpoint_prot, functions.GetProperty))
|
|
|
|
self.assertEqual('tcp', wp_endpoint_prot.result())
|
|
|
|
wp_list_prop = operation.inputs['wp_list_prop']
|
|
|
|
self.assertTrue(isinstance(wp_list_prop, functions.GetProperty))
|
|
|
|
self.assertEqual(3, wp_list_prop.result())
|
|
|
|
|
2014-09-25 13:28:48 +03:00
|
|
|
|
|
|
|
class GetAttributeTest(TestCase):
|
|
|
|
|
|
|
|
def _load_template(self, filename):
|
|
|
|
return ToscaTemplate(os.path.join(
|
|
|
|
os.path.dirname(os.path.abspath(__file__)),
|
|
|
|
'data',
|
|
|
|
filename))
|
|
|
|
|
|
|
|
def test_get_attribute_in_outputs(self):
|
|
|
|
tpl = self._load_template('tosca_single_instance_wordpress.yaml')
|
|
|
|
website_url_output = [
|
|
|
|
x for x in tpl.outputs if x.name == 'website_url'][0]
|
|
|
|
self.assertIsInstance(website_url_output.value, functions.GetAttribute)
|
|
|
|
self.assertEqual('server', website_url_output.value.node_template_name)
|
2015-03-17 15:31:58 -07:00
|
|
|
self.assertEqual('private_address',
|
|
|
|
website_url_output.value.attribute_name)
|
2014-09-25 13:28:48 +03:00
|
|
|
|
|
|
|
def test_get_attribute_invalid_args(self):
|
2015-11-18 16:25:16 -08:00
|
|
|
expected_msg = _('Expected arguments: "node-template-name", '
|
|
|
|
'"attribute-name"')
|
2014-09-25 13:28:48 +03:00
|
|
|
err = self.assertRaises(ValueError,
|
|
|
|
functions.get_function, None, None,
|
|
|
|
{'get_attribute': []})
|
|
|
|
self.assertIn(expected_msg, six.text_type(err))
|
|
|
|
err = self.assertRaises(ValueError,
|
|
|
|
functions.get_function, None, None,
|
|
|
|
{'get_attribute': ['x']})
|
|
|
|
self.assertIn(expected_msg, six.text_type(err))
|
|
|
|
err = self.assertRaises(ValueError,
|
|
|
|
functions.get_function, None, None,
|
|
|
|
{'get_attribute': ['x', 'y', 'z']})
|
|
|
|
self.assertIn(expected_msg, six.text_type(err))
|
|
|
|
|
|
|
|
def test_get_attribute_unknown_node_template_name(self):
|
2015-11-03 15:09:39 -08:00
|
|
|
self.assertRaises(
|
|
|
|
exception.ValidationError, self._load_template,
|
2014-09-25 13:28:48 +03:00
|
|
|
'functions/test_get_attribute_unknown_node_template_name.yaml')
|
2015-11-03 15:09:39 -08:00
|
|
|
exception.ExceptionCollector.assertExceptionMessage(
|
|
|
|
KeyError,
|
2015-11-18 16:25:16 -08:00
|
|
|
_('\'Node template "unknown_node_template" was not found.\''))
|
2014-09-25 13:28:48 +03:00
|
|
|
|
|
|
|
def test_get_attribute_unknown_attribute(self):
|
2015-11-03 15:09:39 -08:00
|
|
|
self.assertRaises(
|
|
|
|
exception.ValidationError, self._load_template,
|
2014-09-25 13:28:48 +03:00
|
|
|
'functions/test_get_attribute_unknown_attribute_name.yaml')
|
2015-11-03 15:09:39 -08:00
|
|
|
exception.ExceptionCollector.assertExceptionMessage(
|
|
|
|
KeyError,
|
2015-11-18 16:25:16 -08:00
|
|
|
_('\'Attribute "unknown_attribute" was not found in node template '
|
|
|
|
'"server".\''))
|
2014-12-04 01:47:59 +02:00
|
|
|
|
|
|
|
def test_get_attribute_host_keyword(self):
|
|
|
|
tpl = self._load_template(
|
|
|
|
'functions/test_get_attribute_host_keyword.yaml')
|
|
|
|
|
|
|
|
def assert_get_attribute_host_functionality(node_template_name):
|
|
|
|
node = [x for x in tpl.nodetemplates
|
|
|
|
if x.name == node_template_name][0]
|
|
|
|
configure_op = [
|
|
|
|
x for x in node.interfaces if x.name == 'configure'][0]
|
2015-01-07 21:59:15 -08:00
|
|
|
ip_addr_input = configure_op.inputs['ip_address']
|
2014-12-04 01:47:59 +02:00
|
|
|
self.assertIsInstance(ip_addr_input, functions.GetAttribute)
|
|
|
|
self.assertEqual('server',
|
|
|
|
ip_addr_input.get_referenced_node_template().name)
|
|
|
|
|
|
|
|
assert_get_attribute_host_functionality('dbms')
|
|
|
|
assert_get_attribute_host_functionality('database')
|
|
|
|
|
|
|
|
def test_get_attribute_host_not_found(self):
|
2015-11-03 15:09:39 -08:00
|
|
|
self.assertRaises(
|
|
|
|
exception.ValidationError, self._load_template,
|
2014-12-04 01:47:59 +02:00
|
|
|
'functions/test_get_attribute_host_not_found.yaml')
|
2015-11-03 15:09:39 -08:00
|
|
|
exception.ExceptionCollector.assertExceptionMessage(
|
|
|
|
ValueError,
|
2015-11-18 16:25:16 -08:00
|
|
|
_('"get_attribute: [ HOST, ... ]" was used in node template '
|
|
|
|
'"server" but "tosca.relationships.HostedOn" was not found in '
|
|
|
|
'the relationship chain.'))
|
2014-12-04 01:47:59 +02:00
|
|
|
|
|
|
|
def test_get_attribute_illegal_host_in_outputs(self):
|
2015-11-03 15:09:39 -08:00
|
|
|
self.assertRaises(
|
|
|
|
exception.ValidationError, self._load_template,
|
2014-12-04 01:47:59 +02:00
|
|
|
'functions/test_get_attribute_illegal_host_in_outputs.yaml')
|
2015-11-03 15:09:39 -08:00
|
|
|
exception.ExceptionCollector.assertExceptionMessage(
|
|
|
|
ValueError,
|
2015-11-18 16:25:16 -08:00
|
|
|
_('"get_attribute: [ HOST, ... ]" is not allowed in "outputs" '
|
|
|
|
'section of the TOSCA template.'))
|