summaryrefslogtreecommitdiffstats
path: root/dashboard/opnfv_yardstick_tc063.json
blob: 7284a909e219d49797c1b384cb162d28f67fad6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
{
  "__inputs": [
    {
      "name": "DS_YARDSTICK",
      "label": "yardstick",
      "description": "",
      "type": "datasource",
      "pluginId": "influxdb",
      "pluginName": "InfluxDB"
    }
  ],
  "__requires": [
    {
      "type": "grafana",
      "id": "grafana",
      "name": "Grafana",
      "version": "4.4.3"
    },
    {
      "type": "datasource",
      "id": "influxdb",
      "name": "InfluxDB",
      "version": "1.0.0"
    },
    {
      "type": "panel",
      "id": "singlestat",
      "name": "Singlestat",
      "version": ""
    }
  ],
  "annotations": {
    "list": []
  },
  "editable": true,
  "gnetId": null,
  "graphTooltip": 0,
  "hideControls": false,
  "id": null,
  "links": [],
  "refresh": false,
  "rows": [
    {
      "collapse": false,
      "height": 175,
      "panels": [
        {
          "cacheTimeout": null,
          "colorBackground": false,
          "colorValue": false,
          "colors": [
            "rgba(50, 172, 45, 0.97)",
            "rgba(237, 129, 40, 0.89)",
            "rgba(245, 54, 54, 0.9)"
          ],
          "datasource": "yardstick",
          "decimals": null,
          "format": "short",
          "gauge": {
            "maxValue": 100000,
            "minValue": 0,
            "show": false,
            "thresholdLabels": false,
            "thresholdMarkers": true
          },
          "id": 4,
          "interval": null,
          "links": [],
          "mappingType": 1,
          "mappingTypes": [
            {
              "name": "value to text",
              "value": 1
            },
            {
              "name": "range to text",
              "value": 2
            }
          ],
          "maxDataPoints": 100,
          "nullPointMode": "connected",
          "nullText": null,
          "postfix": "",
          "postfixFontSize": "50%",
          "prefix": "",
          "prefixFontSize": "50%",
          "rangeMaps": [
            {
              "from": "null",
              "text": "N/A",
              "to": "null"
            }
          ],
          "span": 3,
          "sparkline": {
            "fillColor": "rgba(84, 216, 27, 0.18)",
            "full": false,
            "lineColor": "rgb(31, 120, 193)",
            "show": false
          },
          "tableColumn": "",
          "targets": [
            {
              "alias": "",
              "dsType": "influxdb",
              "groupBy": [],
              "measurement": "opnfv_yardstick_tc063",
              "orderByTime": "ASC",
              "policy": "default",
              "refId": "A",
              "resultFormat": "time_series",
              "select": [
                [
                  {
                    "params": [
                      "Number of devices"
                    ],
                    "type": "field"
                  }
                ]
              ],
              "tags": []
            }
          ],
          "thresholds": "",
          "title": "Disk number",
          "type": "singlestat",
          "valueFontSize": "80%",
          "valueMaps": [
            {
              "op": "=",
              "text": "N/A",
              "value": "null"
            }
          ],
          "valueName": "current"
        },
        {
          "cacheTimeout": null,
          "colorBackground": false,
          "colorValue": false,
          "colors": [
            "rgba(50, 172, 45, 0.97)",
            "rgba(237, 129, 40, 0.89)",
            "rgba(245, 54, 54, 0.9)"
          ],
          "datasource": "yardstick",
          "decimals": null,
          "format": "decbytes",
          "gauge": {
            "maxValue": 21629144711168,
            "minValue": 0,
            "show": false,
            "thresholdLabels": false,
            "thresholdMarkers": true
          },
          "id": 3,
          "interval": null,
          "links": [],
          "mappingType": 1,
          "mappingTypes": [
            {
              "name": "value to text",
              "value": 1
            },
            {
              "name": "range to text",
              "value": 2
            }
          ],
          "maxDataPoints": 100,
          "nullPointMode": "connected",
          "nullText": null,
          "postfix": "",
          "postfixFontSize": "50%",
          "prefix": "",
          "prefixFontSize": "50%",
          "rangeMaps": [
            {
              "from": "null",
              "text": "N/A",
              "to": "null"
            }
          ],
          "span": 9,
          "sparkline": {
            "fillColor": "rgba(84, 216, 27, 0.18)",
            "full": false,
            "lineColor": "rgb(31, 120, 193)",
            "show": false
          },
          "tableColumn": "",
          "targets": [
            {
              "alias": "",
              "dsType": "influxdb",
              "groupBy": [],
              "measurement": "opnfv_yardstick_tc063",
              "orderByTime": "ASC",
              "policy": "default",
              "refId": "A",
              "resultFormat": "time_series",
              "select": [
                [
                  {
                    "params": [
                      "Total disk size in bytes"
                    ],
                    "type": "field"
                  }
                ]
              ],
              "tags": []
            }
          ],
          "thresholds": "",
          "title": "Total disk size (bytes)",
          "type": "singlestat",
          "valueFontSize": "80%",
          "valueMaps": [
            {
              "op": "=",
              "text": "N/A",
              "value": "null"
            }
          ],
          "valueName": "current"
        }
      ],
      "repeat": null,
      "repeatIteration": null,
      "repeatRowId": null,
      "showTitle": false,
      "title": "Dashboard Row",
      "titleSize": "h6"
    }
  ],
  "schemaVersion": 14,
  "style": "dark",
  "tags": [
    "Storage"
  ],
  "templating": {
    "list": []
  },
  "time": {
    "from": "now/d",
    "to": "now/d"
  },
  "timepicker": {
    "refresh_intervals": [
      "5s",
      "10s",
      "30s",
      "1m",
      "5m",
      "15m",
      "30m",
      "1h",
      "2h",
      "1d"
    ],
    "time_options": [
      "5m",
      "15m",
      "1h",
      "6h",
      "12h",
      "24h",
      "2d",
      "7d",
      "30d"
    ]
  },
  "timezone": "browser",
  "title": "opnfv_yardstick_tc063",
  "version": 5
}
'>1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833
#    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

from toscaparser.common import exception
from toscaparser.imports import ImportsLoader
from toscaparser.nodetemplate import NodeTemplate
from toscaparser.parameters import Input
from toscaparser.parameters import Output
from toscaparser.policy import Policy
from toscaparser.relationship_template import RelationshipTemplate
from toscaparser.repositories import Repository
from toscaparser.tests.base import TestCase
from toscaparser.topology_template import TopologyTemplate
from toscaparser.tosca_template import ToscaTemplate
from toscaparser.triggers import Triggers
from toscaparser.utils.gettextutils import _
import toscaparser.utils.yamlparser


class ToscaTemplateValidationTest(TestCase):

    def test_well_defined_template(self):
        tpl_path = os.path.join(
            os.path.dirname(os.path.abspath(__file__)),
            "data/tosca_single_instance_wordpress.yaml")
        params = {'db_name': 'my_wordpress', 'db_user': 'my_db_user',
                  'db_root_pwd': '12345678'}
        self.assertIsNotNone(ToscaTemplate(tpl_path, params))

    def test_custom_interface_allowed(self):
        tpl_path = os.path.join(
            os.path.dirname(os.path.abspath(__file__)),
            "data/interfaces/test_custom_interface_in_template.yaml")
        self.assertIsNotNone(ToscaTemplate(tpl_path))

    def test_custom_interface_invalid_operation(self):
        tpl_path = os.path.join(
            os.path.dirname(os.path.abspath(__file__)),
            "data/interfaces/test_custom_interface_invalid_operation.yaml")
        self.assertRaises(exception.ValidationError,
                          ToscaTemplate, tpl_path)
        exception.ExceptionCollector.assertExceptionMessage(
            exception.UnknownFieldError,
            _('"interfaces" of template "customInterfaceTest" '
              'contains unknown field "CustomOp4". '
              'Refer to the definition to verify valid values.'))

    def test_first_level_sections(self):
        tpl_path = os.path.join(
            os.path.dirname(os.path.abspath(__file__)),
            "data/test_tosca_top_level_error1.yaml")
        self.assertRaises(exception.ValidationError, ToscaTemplate, tpl_path)
        exception.ExceptionCollector.assertExceptionMessage(
            exception.MissingRequiredFieldError,
            _('Template is missing required field '
              '"tosca_definitions_version".'))

        tpl_path = os.path.join(
            os.path.dirname(os.path.abspath(__file__)),
            "data/test_tosca_top_level_error2.yaml")
        self.assertRaises(exception.ValidationError, ToscaTemplate, tpl_path)
        exception.ExceptionCollector.assertExceptionMessage(
            exception.UnknownFieldError,
            _('Template contains unknown field "node_template". Refer to the '
              'definition to verify valid values.'))

    def test_template_with_imports_validation(self):
        tpl_path = os.path.join(
            os.path.dirname(os.path.abspath(__file__)),
            "data/tosca_imports_validation.yaml")
        self.assertRaises(exception.ValidationError, ToscaTemplate, tpl_path)
        exception.ExceptionCollector.assertExceptionMessage(
            exception.UnknownFieldError,
            _('Template custom_types/imported_sample.yaml contains unknown '
              'field "descriptions". Refer to the definition'
              ' to verify valid values.'))
        exception.ExceptionCollector.assertExceptionMessage(
            exception.UnknownFieldError,
            _('Template custom_types/imported_sample.yaml contains unknown '
              'field "node_typess". Refer to the definition to '
              'verify valid values.'))
        exception.ExceptionCollector.assertExceptionMessage(
            exception.UnknownFieldError,
            _('Template custom_types/imported_sample.yaml contains unknown '
              'field "tosca1_definitions_version". Refer to the definition'
              ' to verify valid values.'))
        exception.ExceptionCollector.assertExceptionMessage(
            exception.InvalidTemplateVersion,
            _('The template version "tosca_simple_yaml_1_10 in '
              'custom_types/imported_sample.yaml" is invalid. '
              'Valid versions are "tosca_simple_yaml_1_0, '
              'tosca_simple_profile_for_nfv_1_0_0".'))
        exception.ExceptionCollector.assertExceptionMessage(
            exception.UnknownFieldError,
            _('Template custom_types/imported_sample.yaml contains unknown '
              'field "policy_types1". Refer to the definition to '
              'verify valid values.'))
        exception.ExceptionCollector.assertExceptionMessage(
            exception.UnknownFieldError,
            _('Nodetype"tosca.nodes.SoftwareComponent.Logstash" contains '
              'unknown field "capabilities1". Refer to the definition '
              'to verify valid values.'))
        exception.ExceptionCollector.assertExceptionMessage(
            exception.UnknownFieldError,
            _('Policy "mycompany.mytypes.myScalingPolicy" contains unknown '
              'field "derived1_from". Refer to the definition to '
              'verify valid values.'))
        exception.ExceptionCollector.assertExceptionMessage(
            exception.UnknownFieldError,
            _('Relationshiptype "test.relation.connects" contains unknown '
              'field "derived_from4". Refer to the definition to '
              'verify valid values.'))

    def test_getoperation_IncorrectValue(self):
        # test case 1
        tpl_snippet = '''
        node_templates:
             front_end:
               type: tosca.nodes.Compute
               interfaces:
                 Standard:
                   create:
                     implementation: scripts/frontend/create.sh
                   configure:
                     implementation: scripts/frontend/configure.sh
                     inputs:
                       data_dir: {get_operation_output: [front_end,Standard1,
                                                            create,data_dir]}
        '''
        tpl = (toscaparser.utils.yamlparser.simple_parse(tpl_snippet))
        err = self.assertRaises(ValueError,
                                TopologyTemplate, tpl, None)
        expectedmessage = _('Enter a valid interface name')
        self.assertEqual(expectedmessage, err.__str__())
        # test case 2
        tpl_snippet2 = '''
        node_templates:
             front_end:
               type: tosca.nodes.Compute
               interfaces:
                 Standard:
                   create:
                     implementation: scripts/frontend/create.sh
                   configure:
                     implementation: scripts/frontend/configure.sh
                     inputs:
                       data_dir: {get_operation_output: [front_end1,Standard,
                                                            create,data_dir]}
        '''
        tpl2 = (toscaparser.utils.yamlparser.simple_parse(tpl_snippet2))
        err2 = self.assertRaises(KeyError,
                                 TopologyTemplate, tpl2, None)
        expectedmessage2 = _('\'Node template "front_end1" was not found.\'')
        self.assertEqual(expectedmessage2, err2.__str__())
        # test case 3
        tpl_snippet3 = '''
        node_templates:
             front_end:
               type: tosca.nodes.Compute
               interfaces:
                 Standard:
                   create:
                     implementation: scripts/frontend/create.sh
                   configure:
                     implementation: scripts/frontend/configure.sh
                     inputs:
                       data_dir: {get_operation_output: [front_end,Standard,
                                                      get_target,data_dir]}
        '''
        tpl3 = (toscaparser.utils.yamlparser.simple_parse(tpl_snippet3))
        err3 = self.assertRaises(ValueError,
                                 TopologyTemplate, tpl3, None)
        expectedmessage3 = _('Enter an operation of Standard interface')
        self.assertEqual(expectedmessage3, err3.__str__())
        # test case 4
        tpl_snippet4 = '''
        node_templates:
             front_end:
               type: tosca.nodes.Compute
               interfaces:
                 Standard:
                   create:
                     implementation: scripts/frontend/create.sh
                   configure:
                     implementation: scripts/frontend/configure.sh
                     inputs:
                       data_dir: {get_operation_output: [front_end,Configure,
                                                        create,data_dir]}
        '''
        tpl4 = (toscaparser.utils.yamlparser.simple_parse(tpl_snippet4))
        err4 = self.assertRaises(ValueError,
                                 TopologyTemplate, tpl4, None)
        expectedmessage4 = _('Enter an operation of Configure interface')
        self.assertEqual(expectedmessage4, err4.__str__())
        # test case 5
        tpl_snippet5 = '''
        node_templates:
             front_end:
               type: tosca.nodes.Compute
               interfaces:
                 Standard:
                   create:
                     implementation: scripts/frontend/create.sh
                   configure:
                     implementation: scripts/frontend/configure.sh
                     inputs:
                       data_dir: {get_operation_output: [front_end,Standard,
                                                                    create]}
        '''
        tpl5 = (toscaparser.utils.yamlparser.simple_parse(tpl_snippet5))
        err5 = self.assertRaises(ValueError,
                                 TopologyTemplate, tpl5, None)
        expectedmessage5 = _('Illegal arguments for function'
                             ' "get_operation_output".'
                             ' Expected arguments: "template_name",'
                             '"interface_name",'
                             '"operation_name","output_variable_name"')
        self.assertEqual(expectedmessage5, err5.__str__())

    def test_unsupported_type(self):
        tpl_snippet = '''
        node_templates:
          invalid_type:
            type: tosca.test.invalidtype
            properties:
              size: { get_input: storage_size }
              snapshot_id: { get_input: storage_snapshot_id }
        '''
        tpl = (toscaparser.utils.yamlparser.simple_parse(tpl_snippet))
        err = self.assertRaises(exception.UnsupportedTypeError,
                                TopologyTemplate, tpl, None)
        expectedmessage = _('Type "tosca.test.invalidtype" is valid'
                            ' TOSCA type but not supported at this time.')
        self.assertEqual(expectedmessage, err.__str__())

    def test_inputs(self):
        tpl_snippet1 = '''
        inputs:
          cpus:
            type: integer
            description: Number of CPUs for the server.
            constraint:
              - valid_values: [ 1, 2, 4 ]
            required: yes
            status: supported
        '''
        tpl_snippet2 = '''
        inputs:
          cpus:
            type: integer
            description: Number of CPUs for the server.
            constraints:
              - valid_values: [ 1, 2, 4 ]
            required: yes
            status: supported
        '''
        tpl_snippet3 = '''
        inputs:
          some_list:
            type: list
            description: List of items
            entry_schema:
              type: string
            default: []
        '''
        inputs1 = (toscaparser.utils.yamlparser.
                   simple_parse(tpl_snippet1)['inputs'])
        name1, attrs1 = list(inputs1.items())[0]
        inputs2 = (toscaparser.utils.yamlparser.
                   simple_parse(tpl_snippet2)['inputs'])
        name2, attrs2 = list(inputs2.items())[0]
        try:
            Input(name1, attrs1)
        except Exception as err:
            self.assertEqual(_('Input "cpus" contains unknown field '
                               '"constraint". Refer to the definition to '
                               'verify valid values.'),
                             err.__str__())
        input2 = Input(name2, attrs2)
        self.assertTrue(input2.required)
        toscaparser.utils.yamlparser.simple_parse(tpl_snippet3)['inputs']

    def _imports_content_test(self, tpl_snippet, path, custom_type_def):
        imports = (toscaparser.utils.yamlparser.
                   simple_parse(tpl_snippet)['imports'])
        loader = ImportsLoader(imports, path, custom_type_def)
        return loader.get_custom_defs()

    def test_imports_without_templates(self):
        tpl_snippet = '''
        imports:
          # omitted here for brevity
        '''
        path = 'toscaparser/tests/data/tosca_elk.yaml'
        errormsg = _('"imports" keyname is defined without including '
                     'templates.')
        err = self.assertRaises(exception.ValidationError,
                                self._imports_content_test,
                                tpl_snippet,
                                path,
                                "node_types")
        self.assertEqual(errormsg, err.__str__())

    def test_imports_with_name_without_templates(self):
        tpl_snippet = '''
        imports:
          - some_definitions:
        '''
        path = 'toscaparser/tests/data/tosca_elk.yaml'
        errormsg = _('A template file name is not provided with import '
                     'definition "some_definitions".')
        err = self.assertRaises(exception.ValidationError,
                                self._imports_content_test,
                                tpl_snippet, path, None)
        self.assertEqual(errormsg, err.__str__())

    def test_imports_without_import_name(self):
        tpl_snippet = '''
        imports:
          - custom_types/paypalpizzastore_nodejs_app.yaml
          - https://raw.githubusercontent.com/openstack/\
tosca-parser/master/toscaparser/tests/data/custom_types/wordpress.yaml
        '''
        path = 'toscaparser/tests/data/tosca_elk.yaml'
        custom_defs = self._imports_content_test(tpl_snippet,
                                                 path,
                                                 "node_types")
        self.assertTrue(custom_defs)

    def test_imports_wth_import_name(self):
        tpl_snippet = '''
        imports:
          - some_definitions: custom_types/paypalpizzastore_nodejs_app.yaml
          - more_definitions:
              file: 'https://raw.githubusercontent.com/openstack/tosca-parser\
/master/toscaparser/tests/data/custom_types/wordpress.yaml'
              namespace_prefix: single_instance_wordpress
        '''
        path = 'toscaparser/tests/data/tosca_elk.yaml'
        custom_defs = self._imports_content_test(tpl_snippet,
                                                 path,
                                                 "node_types")
        self.assertTrue(custom_defs.get("single_instance_wordpress.tosca."
                                        "nodes.WebApplication.WordPress"))

    def test_imports_wth_namespace_prefix(self):
        tpl_snippet = '''
        imports:
          - more_definitions:
              file: custom_types/nested_rsyslog.yaml
              namespace_prefix: testprefix
        '''
        path = 'toscaparser/tests/data/tosca_elk.yaml'
        custom_defs = self._imports_content_test(tpl_snippet,
                                                 path,
                                                 "node_types")
        self.assertTrue(custom_defs.get("testprefix.Rsyslog"))

    def test_imports_with_no_main_template(self):
        tpl_snippet = '''
        imports:
          - some_definitions: https://raw.githubusercontent.com/openstack/\
tosca-parser/master/toscaparser/tests/data/custom_types/wordpress.yaml
          - some_definitions:
              file: my_defns/my_typesdefs_n.yaml
        '''
        errormsg = _('Input tosca template is not provided.')
        err = self.assertRaises(exception.ValidationError,
                                self._imports_content_test,
                                tpl_snippet, None, None)
        self.assertEqual(errormsg, err.__str__())

    def test_imports_duplicate_name(self):
        tpl_snippet = '''
        imports:
          - some_definitions: https://raw.githubusercontent.com/openstack/\
tosca-parser/master/toscaparser/tests/data/custom_types/wordpress.yaml
          - some_definitions:
              file: my_defns/my_typesdefs_n.yaml
        '''
        errormsg = _('Duplicate import name "some_definitions" was found.')
        path = 'toscaparser/tests/data/tosca_elk.yaml'
        err = self.assertRaises(exception.ValidationError,
                                self._imports_content_test,
                                tpl_snippet, path, None)
        self.assertEqual(errormsg, err.__str__())

    def test_imports_missing_req_field_in_def(self):
        tpl_snippet = '''
        imports:
          - more_definitions:
              file1: my_defns/my_typesdefs_n.yaml
              repository: my_company_repo
              namespace_uri: http://mycompany.com/ns/tosca/2.0
              namespace_prefix: mycompany
        '''
        errormsg = _('Import of template "more_definitions" is missing '
                     'required field "file".')
        path = 'toscaparser/tests/data/tosca_elk.yaml'
        err = self.assertRaises(exception.MissingRequiredFieldError,
                                self._imports_content_test,
                                tpl_snippet, path, None)
        self.assertEqual(errormsg, err.__str__())

    def test_imports_file_with_uri(self):
        tpl_snippet = '''
        imports:
          - more_definitions:
              file: https://raw.githubusercontent.com/openstack/\
tosca-parser/master/toscaparser/tests/data/custom_types/wordpress.yaml
        '''
        path = 'https://raw.githubusercontent.com/openstack/\
tosca-parser/master/toscaparser/tests/data/\
tosca_single_instance_wordpress_with_url_import.yaml'
        custom_defs = self._imports_content_test(tpl_snippet,
                                                 path,
                                                 "node_types")
        self.assertTrue(custom_defs.get("tosca.nodes."
                                        "WebApplication.WordPress"))

    def test_imports_file_namespace_fields(self):
        tpl_snippet = '''
        imports:
          - more_definitions:
             file: https://raw.githubusercontent.com/openstack/\
heat-translator/master/translator/tests/data/custom_types/wordpress.yaml
             namespace_prefix: mycompany
             namespace_uri: http://docs.oasis-open.org/tosca/ns/simple/yaml/1.0
        '''
        path = 'toscaparser/tests/data/tosca_elk.yaml'
        custom_defs = self._imports_content_test(tpl_snippet,
                                                 path,
                                                 "node_types")
        self.assertTrue(custom_defs.get("mycompany.tosca.nodes."
                                        "WebApplication.WordPress"))

    def test_import_error_file_uri(self):
        tpl_snippet = '''
        imports:
          - more_definitions:
             file: mycompany.com/ns/tosca/2.0/toscaparser/tests/data\
/tosca_elk.yaml
             namespace_prefix: mycompany
             namespace_uri: http://docs.oasis-open.org/tosca/ns/simple/yaml/1.0
        '''
        path = 'toscaparser/tests/data/tosca_elk.yaml'
        self.assertRaises(ImportError,
                          self._imports_content_test,
                          tpl_snippet, path, None)

    def test_import_single_line_error(self):
        tpl_snippet = '''
        imports:
          - some_definitions: abc.com/tests/data/tosca_elk.yaml
        '''
        errormsg = _('Import "abc.com/tests/data/tosca_elk.yaml" is not '
                     'valid.')
        path = 'toscaparser/tests/data/tosca_elk.yaml'
        err = self.assertRaises(ImportError,
                                self._imports_content_test,
                                tpl_snippet, path, None)
        self.assertEqual(errormsg, err.__str__())

    def test_outputs(self):
        tpl_snippet = '''
        outputs:
          server_address:
            description: IP address of server instance.
            values: { get_property: [server, private_address] }
        '''
        outputs = (toscaparser.utils.yamlparser.
                   simple_parse(tpl_snippet)['outputs'])
        name, attrs = list(outputs.items())[0]
        output = Output(name, attrs)
        try:
            output.validate()
        except Exception as err:
            self.assertTrue(
                isinstance(err, exception.MissingRequiredFieldError))
            self.assertEqual(_('Output "server_address" is missing required '
                               'field "value".'), err.__str__())

        tpl_snippet = '''
        outputs:
          server_address:
            descriptions: IP address of server instance.
            value: { get_property: [server, private_address] }
        '''
        outputs = (toscaparser.utils.yamlparser.
                   simple_parse(tpl_snippet)['outputs'])
        name, attrs = list(outputs.items())[0]
        output = Output(name, attrs)
        try:
            output.validate()
        except Exception as err:
            self.assertIsInstance(err, exception.UnknownFieldError)
            self.assertEqual(_('Output "server_address" contains unknown '
                               'field "descriptions". Refer to the definition '
                               'to verify valid values.'),
                             err.__str__())

    def _repo_content(self, path):
        repositories = path['repositories']
        reposit = []
        for name, val in repositories.items():
            reposits = Repository(name, val)
            reposit.append(reposits)
        return reposit

    def test_repositories(self):
        tpl_snippet = '''
        repositories:
           repo_code0: https://raw.githubusercontent.com/nandinivemula/intern
           repo_code1:
              description: My project's code Repository in github usercontent.
              url: https://github.com/nandinivemula/intern
              credential:
                 user: nandini
                 password: tcs@12345
           repo_code2:
              description: My Project's code Repository in github.
              url: https://github.com/nandinivemula/intern
              credential:
                 user: xyzw
                 password: xyz@123
        '''
        tpl = (toscaparser.utils.yamlparser.simple_parse(tpl_snippet))
        repoobject = self._repo_content(tpl)
        actualrepo_names = []
        for repo in repoobject:
            repos = repo.name
            actualrepo_names.append(repos)
        reposname = list(tpl.values())
        reposnames = reposname[0]
        expected_reponames = list(reposnames.keys())
        self.assertEqual(expected_reponames, actualrepo_names)

    def test_repositories_with_missing_required_field(self):
        tpl_snippet = '''
        repositories:
           repo_code0: https://raw.githubusercontent.com/nandinivemula/intern
           repo_code1:
              description: My project's code Repository in github usercontent.
              credential:
                 user: nandini
                 password: tcs@12345
           repo_code2:
              description: My Project's code Repository in github.
              url: https://github.com/nandinivemula/intern
              credential:
                 user: xyzw
                 password: xyz@123
        '''
        tpl = (toscaparser.utils.yamlparser.simple_parse(tpl_snippet))
        err = self.assertRaises(exception.MissingRequiredFieldError,
                                self._repo_content, tpl)
        expectedmessage = _('Repository "repo_code1" is missing '
                            'required field "url".')
        self.assertEqual(expectedmessage, err.__str__())

    def test_repositories_with_unknown_field(self):
        tpl_snippet = '''
        repositories:
           repo_code0: https://raw.githubusercontent.com/nandinivemula/intern
           repo_code1:
              description: My project's code Repository in github usercontent.
              url: https://github.com/nandinivemula/intern
              credential:
                 user: nandini
                 password: tcs@12345
           repo_code2:
              descripton: My Project's code Repository in github.
              url: https://github.com/nandinivemula/intern
              credential:
                 user: xyzw
                 password: xyz@123
        '''
        tpl = (toscaparser.utils.yamlparser.simple_parse(tpl_snippet))
        err = self.assertRaises(exception.UnknownFieldError,
                                self._repo_content, tpl)
        expectedmessage = _('repositories "repo_code2" contains unknown field'
                            ' "descripton". Refer to the definition to verify'
                            ' valid values.')
        self.assertEqual(expectedmessage, err.__str__())

    def test_repositories_with_invalid_url(self):
        tpl_snippet = '''
        repositories:
           repo_code0: https://raw.githubusercontent.com/nandinivemula/intern
           repo_code1:
              description: My project's code Repository in github usercontent.
              url: h
              credential:
                 user: nandini
                 password: tcs@12345
           repo_code2:
              description: My Project's code Repository in github.
              url: https://github.com/nandinivemula/intern
              credential:
                 user: xyzw
                 password: xyz@123
        '''
        tpl = (toscaparser.utils.yamlparser.simple_parse(tpl_snippet))
        err = self.assertRaises(exception.URLException,
                                self._repo_content, tpl)
        expectedmessage = _('repsositories "repo_code1" Invalid Url')
        self.assertEqual(expectedmessage, err.__str__())

    def test_groups(self):
        tpl_snippet = '''
        node_templates:
          server:
            type: tosca.nodes.Compute
            requirements:
              - log_endpoint:
                  capability: log_endpoint

          mysql_dbms:
            type: tosca.nodes.DBMS
            properties:
              root_password: aaa
              port: 3376

        groups:
          webserver_group:
            type: tosca.groups.Root
            members: [ server, mysql_dbms ]
        '''
        tpl = (toscaparser.utils.yamlparser.simple_parse(tpl_snippet))
        TopologyTemplate(tpl, None)

    def test_groups_with_missing_required_field(self):
        tpl_snippet = '''
        node_templates:
          server:
            type: tosca.nodes.Compute
            requirements:
              - log_endpoint:
                  capability: log_endpoint

          mysql_dbms:
            type: tosca.nodes.DBMS
            properties:
              root_password: aaa
              port: 3376

        groups:
          webserver_group:
              members: ['server', 'mysql_dbms']
        '''
        tpl = (toscaparser.utils.yamlparser.simple_parse(tpl_snippet))
        err = self.assertRaises(exception.MissingRequiredFieldError,
                                TopologyTemplate, tpl, None)
        expectedmessage = _('Template "webserver_group" is missing '
                            'required field "type".')
        self.assertEqual(expectedmessage, err.__str__())

    def test_groups_with_unknown_target(self):
        tpl_snippet = '''
        node_templates:
          server:
            type: tosca.nodes.Compute
            requirements:
              - log_endpoint:
                  capability: log_endpoint

          mysql_dbms:
            type: tosca.nodes.DBMS
            properties:
              root_password: aaa
              port: 3376

        groups:
          webserver_group:
            type: tosca.groups.Root
            members: [ serv, mysql_dbms ]
        '''
        tpl = (toscaparser.utils.yamlparser.simple_parse(tpl_snippet))
        expectedmessage = _('"Target member "serv" is not found in '
                            'node_templates"')
        err = self.assertRaises(exception.InvalidGroupTargetException,
                                TopologyTemplate, tpl, None)
        self.assertEqual(expectedmessage, err.__str__())

    def test_groups_with_repeated_targets(self):
        tpl_snippet = '''
        node_templates:
          server:
            type: tosca.nodes.Compute
            requirements:
              - log_endpoint:
                  capability: log_endpoint

          mysql_dbms:
            type: tosca.nodes.DBMS
            properties:
              root_password: aaa
              port: 3376

        groups:
          webserver_group:
            type: tosca.groups.Root
            members: [ server, server, mysql_dbms ]
        '''
        tpl = (toscaparser.utils.yamlparser.simple_parse(tpl_snippet))
        expectedmessage = _('"Member nodes '
                            '"[\'server\', \'server\', \'mysql_dbms\']" '
                            'should be >= 1 and not repeated"')
        err = self.assertRaises(exception.InvalidGroupTargetException,
                                TopologyTemplate, tpl, None)
        self.assertEqual(expectedmessage, err.__str__())

    def test_groups_with_only_one_target(self):
        tpl_snippet = '''
        node_templates:
          server:
            type: tosca.nodes.Compute
            requirements:
              - log_endpoint:
                  capability: log_endpoint

          mysql_dbms:
            type: tosca.nodes.DBMS
            properties:
              root_password: aaa
              port: 3376

        groups:
          webserver_group:
            type: tosca.groups.Root
            members: []
        '''
        tpl = (toscaparser.utils.yamlparser.simple_parse(tpl_snippet))
        expectedmessage = _('"Member nodes "[]" should be >= 1 '
                            'and not repeated"')
        err = self.assertRaises(exception.InvalidGroupTargetException,
                                TopologyTemplate, tpl, None)
        self.assertEqual(expectedmessage, err.__str__())

    def _custom_types(self):
        custom_types = {}
        def_file = os.path.join(
            os.path.dirname(os.path.abspath(__file__)),
            "data/custom_types/wordpress.yaml")
        custom_type = toscaparser.utils.yamlparser.load_yaml(def_file)
        node_types = custom_type['node_types']
        for name in node_types:
            defintion = node_types[name]
            custom_types[name] = defintion
        return custom_types

    def _single_node_template_content_test(self, tpl_snippet):
        nodetemplates = (toscaparser.utils.yamlparser.
                         simple_ordered_parse(tpl_snippet))['node_templates']
        name = list(nodetemplates.keys())[0]
        nodetemplate = NodeTemplate(name, nodetemplates,
                                    self._custom_types())
        nodetemplate.validate()
        nodetemplate.requirements
        nodetemplate.get_capabilities_objects()
        nodetemplate.get_properties_objects()
        nodetemplate.interfaces

    def test_node_templates(self):
        tpl_snippet = '''
        node_templates:
          server:
            capabilities:
              host:
                properties:
                  disk_size: 10
                  num_cpus: 4
                  mem_size: 4096
              os:
                properties:
                  architecture: x86_64
                  type: Linux
                  distribution: Fedora
                  version: 18.0
        '''
        expectedmessage = _('Template "server" is missing required field '
                            '"type".')
        err = self.assertRaises(
            exception.MissingRequiredFieldError,
            lambda: self._single_node_template_content_test(tpl_snippet))
        self.assertEqual(expectedmessage, err.__str__())

    def test_node_template_with_wrong_properties_keyname(self):
        """Node template keyname 'properties' given as 'propertiessss'."""
        tpl_snippet = '''
        node_templates:
          mysql_dbms:
            type: tosca.nodes.DBMS
            propertiessss:
              root_password: aaa
              port: 3376
        '''
        expectedmessage = _('Node template "mysql_dbms" contains unknown '
                            'field "propertiessss". Refer to the definition '
                            'to verify valid values.')
        err = self.assertRaises(
            exception.UnknownFieldError,
            lambda: self._single_node_template_content_test(tpl_snippet))
        self.assertEqual(expectedmessage, err.__str__())

    def test_node_template_with_wrong_requirements_keyname(self):
        """Node template keyname 'requirements' given as 'requirement'."""
        tpl_snippet = '''
        node_templates:
          mysql_dbms:
            type: tosca.nodes.DBMS
            properties:
              root_password: aaa
              port: 3376
            requirement:
              - host: server
        '''
        expectedmessage = _('Node template "mysql_dbms" contains unknown '
                            'field "requirement". Refer to the definition to '
                            'verify valid values.')
        err = self.assertRaises(
            exception.UnknownFieldError,
            lambda: self._single_node_template_content_test(tpl_snippet))
        self.assertEqual(expectedmessage, err.__str__())

    def test_node_template_with_wrong_interfaces_keyname(self):
        """Node template keyname 'interfaces' given as 'interfac'."""
        tpl_snippet = '''
        node_templates:
          mysql_dbms:
            type: tosca.nodes.DBMS
            properties:
              root_password: aaa
              port: 3376
            requirements:
              - host: server
            interfac:
              Standard:
                configure: mysql_database_configure.sh
        '''
        expectedmessage = _('Node template "mysql_dbms" contains unknown '
                            'field "interfac". Refer to the definition to '
                            'verify valid values.')
        err = self.assertRaises(
            exception.UnknownFieldError,
            lambda: self._single_node_template_content_test(tpl_snippet))
        self.assertEqual(expectedmessage, err.__str__())

    def test_node_template_with_wrong_capabilities_keyname(self):
        """Node template keyname 'capabilities' given as 'capabilitiis'."""
        tpl_snippet = '''
        node_templates:
          mysql_database:
            type: tosca.nodes.Database
            properties:
              db_name: { get_input: db_name }
              db_user: { get_input: db_user }
              db_password: { get_input: db_pwd }
            capabilitiis:
              database_endpoint:
                properties:
                  port: { get_input: db_port }
        '''
        expectedmessage = _('Node template "mysql_database" contains unknown '
                            'field "capabilitiis". Refer to the definition to '
                            'verify valid values.')
        err = self.assertRaises(
            exception.UnknownFieldError,
            lambda: self._single_node_template_content_test(tpl_snippet))
        self.assertEqual(expectedmessage, err.__str__())

    def test_node_template_with_wrong_artifacts_keyname(self):
        """Node template keyname 'artifacts' given as 'artifactsss'."""
        tpl_snippet = '''
        node_templates:
          mysql_database:
            type: tosca.nodes.Database
            artifactsss:
              db_content:
                implementation: files/my_db_content.txt
                type: tosca.artifacts.File
        '''
        expectedmessage = _('Node template "mysql_database" contains unknown '
                            'field "artifactsss". Refer to the definition to '
                            'verify valid values.')
        err = self.assertRaises(
            exception.UnknownFieldError,
            lambda: self._single_node_template_content_test(tpl_snippet))
        self.assertEqual(expectedmessage, err.__str__())

    def test_node_template_with_multiple_wrong_keynames(self):
        """Node templates given with multiple wrong keynames."""
        tpl_snippet = '''
        node_templates:
          mysql_dbms:
            type: tosca.nodes.DBMS
            propertieees:
              root_password: aaa
              port: 3376
            requirements:
              - host: server
            interfacs:
              Standard:
                configure: mysql_database_configure.sh
        '''
        expectedmessage = _('Node template "mysql_dbms" contains unknown '
                            'field "propertieees". Refer to the definition to '
                            'verify valid values.')
        err = self.assertRaises(
            exception.UnknownFieldError,
            lambda: self._single_node_template_content_test(tpl_snippet))
        self.assertEqual(expectedmessage, err.__str__())

        tpl_snippet = '''
        node_templates:
          mysql_database:
            type: tosca.nodes.Database
            properties:
              name: { get_input: db_name }
              user: { get_input: db_user }
              password: { get_input: db_pwd }
            capabilitiiiies:
              database_endpoint:
              properties:
                port: { get_input: db_port }
            requirementsss:
              - host:
                  node: mysql_dbms
            interfac:
              Standard:
                 configure: mysql_database_configure.sh

        '''
        expectedmessage = _('Node template "mysql_database" contains unknown '
                            'field "capabilitiiiies". Refer to the definition '
                            'to verify valid values.')
        err = self.assertRaises(
            exception.UnknownFieldError,
            lambda: self._single_node_template_content_test(tpl_snippet))
        self.assertEqual(expectedmessage, err.__str__())

    def test_node_template_type(self):
        tpl_snippet = '''
        node_templates:
          mysql_database:
            type: tosca.nodes.Databases
            properties:
              db_name: { get_input: db_name }
              db_user: { get_input: db_user }
              db_password: { get_input: db_pwd }
            capabilities:
              database_endpoint:
                properties:
                  port: { get_input: db_port }
            requirements:
              - host: mysql_dbms
            interfaces:
              Standard:
                 configure: mysql_database_configure.sh
        '''
        expectedmessage = _('Type "tosca.nodes.Databases" is not '
                            'a valid type.')
        err = self.assertRaises(
            exception.InvalidTypeError,
            lambda: self._single_node_template_content_test(tpl_snippet))
        self.assertEqual(expectedmessage, err.__str__())

    def test_node_template_requirements(self):
        tpl_snippet = '''
        node_templates:
          webserver:
            type: tosca.nodes.WebServer
            requirements:
              host: server
            interfaces:
              Standard:
                create: webserver_install.sh
                start: d.sh
        '''
        expectedmessage = _('"requirements" of template "webserver" must be '
                            'of type "list".')
        err = self.assertRaises(
            exception.TypeMismatchError,
            lambda: self._single_node_template_content_test(tpl_snippet))
        self.assertEqual(expectedmessage, err.__str__())

        tpl_snippet = '''
        node_templates:
          mysql_database:
            type: tosca.nodes.Database
            properties:
              db_name: { get_input: db_name }
              db_user: { get_input: db_user }
              db_password: { get_input: db_pwd }
            capabilities:
              database_endpoint:
                properties:
                  port: { get_input: db_port }
            requirements:
              - host: mysql_dbms
              - database_endpoint: mysql_database
            interfaces:
              Standard:
                 configure: mysql_database_configure.sh
        '''
        expectedmessage = _('"requirements" of template "mysql_database" '
                            'contains unknown field "database_endpoint". '
                            'Refer to the definition to verify valid values.')
        err = self.assertRaises(
            exception.UnknownFieldError,
            lambda: self._single_node_template_content_test(tpl_snippet))
        self.assertEqual(expectedmessage, err.__str__())

    def test_node_template_requirements_with_wrong_node_keyname(self):
        """Node template requirements keyname 'node' given as 'nodes'."""
        tpl_snippet = '''
        node_templates:
          mysql_database:
            type: tosca.nodes.Database
            requirements:
              - host:
                  nodes: mysql_dbms

        '''
        expectedmessage = _('"requirements" of template "mysql_database" '
                            'contains unknown field "nodes". Refer to the '
                            'definition to verify valid values.')
        err = self.assertRaises(
            exception.UnknownFieldError,
            lambda: self._single_node_template_content_test(tpl_snippet))
        self.assertEqual(expectedmessage, err.__str__())

    def test_node_template_requirements_with_wrong_capability_keyname(self):
        """Incorrect node template requirements keyname

        Node template requirements keyname 'capability' given as
        'capabilityy'.
        """
        tpl_snippet = '''
        node_templates:
          mysql_database:
            type: tosca.nodes.Database
            requirements:
              - host:
                  node: mysql_dbms
              - log_endpoint:
                  node: logstash
                  capabilityy: log_endpoint
                  relationship:
                    type: tosca.relationships.ConnectsTo

        '''
        expectedmessage = _('"requirements" of template "mysql_database" '
                            'contains unknown field "capabilityy". Refer to '
                            'the definition to verify valid values.')
        err = self.assertRaises(
            exception.UnknownFieldError,
            lambda: self._single_node_template_content_test(tpl_snippet))
        self.assertEqual(expectedmessage, err.__str__())

    def test_node_template_requirements_with_wrong_relationship_keyname(self):
        """Incorrect node template requirements keyname

        Node template requirements keyname 'relationship' given as
        'relationshipppp'.
        """
        tpl_snippet = '''
        node_templates:
          mysql_database:
            type: tosca.nodes.Database
            requirements:
              - host:
                  node: mysql_dbms
              - log_endpoint:
                  node: logstash
                  capability: log_endpoint
                  relationshipppp:
                    type: tosca.relationships.ConnectsTo

        '''
        expectedmessage = _('"requirements" of template "mysql_database" '
                            'contains unknown field "relationshipppp". Refer '
                            'to the definition to verify valid values.')
        err = self.assertRaises(
            exception.UnknownFieldError,
            lambda: self._single_node_template_content_test(tpl_snippet))
        self.assertEqual(expectedmessage, err.__str__())

    def test_node_template_requirements_with_wrong_occurrences_keyname(self):
        """Incorrect node template requirements keyname

        Node template requirements keyname 'occurrences' given as
        'occurences'.
        """
        tpl_snippet = '''
        node_templates:
          mysql_database:
            type: tosca.nodes.Database
            requirements:
              - host:
                  node: mysql_dbms
              - log_endpoint:
                  node: logstash
                  capability: log_endpoint
                  relationship:
                    type: tosca.relationships.ConnectsTo
                  occurences: [0, UNBOUNDED]
        '''
        expectedmessage = _('"requirements" of template "mysql_database" '
                            'contains unknown field "occurences". Refer to '
                            'the definition to verify valid values.')
        err = self.assertRaises(
            exception.UnknownFieldError,
            lambda: self._single_node_template_content_test(tpl_snippet))
        self.assertEqual(expectedmessage, err.__str__())

    def test_node_template_requirements_with_multiple_wrong_keynames(self):
        """Node templates given with multiple wrong requirements keynames."""
        tpl_snippet = '''
        node_templates:
          mysql_database:
            type: tosca.nodes.Database
            requirements:
              - host:
                  node: mysql_dbms
              - log_endpoint:
                  nod: logstash
                  capabilit: log_endpoint
                  relationshipppp:
                    type: tosca.relationships.ConnectsTo

        '''
        expectedmessage = _('"requirements" of template "mysql_database" '
                            'contains unknown field "nod". Refer to the '
                            'definition to verify valid values.')
        err = self.assertRaises(
            exception.UnknownFieldError,
            lambda: self._single_node_template_content_test(tpl_snippet))
        self.assertEqual(expectedmessage, err.__str__())

        tpl_snippet = '''
        node_templates:
          mysql_database:
            type: tosca.nodes.Database
            requirements:
              - host:
                  node: mysql_dbms
              - log_endpoint:
                  node: logstash
                  capabilit: log_endpoint
                  relationshipppp:
                    type: tosca.relationships.ConnectsTo

        '''
        expectedmessage = _('"requirements" of template "mysql_database" '
                            'contains unknown field "capabilit". Refer to the '
                            'definition to verify valid values.')
        err = self.assertRaises(
            exception.UnknownFieldError,
            lambda: self._single_node_template_content_test(tpl_snippet))
        self.assertEqual(expectedmessage, err.__str__())

    def test_node_template_requirements_invalid_occurrences(self):
        tpl_snippet = '''
        node_templates:
          server:
            type: tosca.nodes.Compute
            requirements:
              - log_endpoint:
                  capability: log_endpoint
                  occurrences: [0, -1]
        '''
        expectedmessage = _('Value of property "[0, -1]" is invalid.')
        err = self.assertRaises(
            exception.InvalidPropertyValueError,
            lambda: self._single_node_template_content_test(tpl_snippet))
        self.assertEqual(expectedmessage, err.__str__())

        tpl_snippet = '''
        node_templates:
          server:
            type: tosca.nodes.Compute
            requirements:
              - log_endpoint:
                  capability: log_endpoint
                  occurrences: [a, w]
        '''
        expectedmessage = _('"a" is not an integer.')
        err = self.assertRaises(
            ValueError,
            lambda: self._single_node_template_content_test(tpl_snippet))
        self.assertEqual(expectedmessage, err.__str__())

        tpl_snippet = '''
        node_templates:
          server:
            type: tosca.nodes.Compute
            requirements:
              - log_endpoint:
                  capability: log_endpoint
                  occurrences: -1
        '''
        expectedmessage = _('"-1" is not a list.')
        err = self.assertRaises(
            ValueError,
            lambda: self._single_node_template_content_test(tpl_snippet))
        self.assertEqual(expectedmessage, err.__str__())

        tpl_snippet = '''
        node_templates:
          server:
            type: tosca.nodes.Compute
            requirements:
              - log_endpoint:
                  capability: log_endpoint
                  occurrences: [5, 1]
        '''
        expectedmessage = _('Value of property "[5, 1]" is invalid.')
        err = self.assertRaises(
            exception.InvalidPropertyValueError,
            lambda: self._single_node_template_content_test(tpl_snippet))
        self.assertEqual(expectedmessage, err.__str__())

        tpl_snippet = '''
        node_templates:
          server:
            type: tosca.nodes.Compute
            requirements:
              - log_endpoint:
                  capability: log_endpoint
                  occurrences: [0, 0]
        '''
        expectedmessage = _('Value of property "[0, 0]" is invalid.')
        err = self.assertRaises(
            exception.InvalidPropertyValueError,
            lambda: self._single_node_template_content_test(tpl_snippet))
        self.assertEqual(expectedmessage, err.__str__())

    def test_node_template_requirements_valid_occurrences(self):
        tpl_snippet = '''
        node_templates:
          server:
            type: tosca.nodes.Compute
            requirements:
              - log_endpoint:
                  capability: log_endpoint
                  occurrences: [2, 2]
        '''
        self._single_node_template_content_test(tpl_snippet)

    def test_node_template_capabilities(self):
        tpl_snippet = '''
        node_templates:
          mysql_database:
            type: tosca.nodes.Database
            properties:
              db_name: { get_input: db_name }
              db_user: { get_input: db_user }
              db_password: { get_input: db_pwd }
            capabilities:
              http_endpoint:
                properties:
                  port: { get_input: db_port }
            requirements:
              - host: mysql_dbms
            interfaces:
              Standard:
                 configure: mysql_database_configure.sh
        '''
        expectedmessage = _('"capabilities" of template "mysql_database" '
                            'contains unknown field "http_endpoint". Refer to '
                            'the definition to verify valid values.')
        err = self.assertRaises(
            exception.UnknownFieldError,
            lambda: self._single_node_template_content_test(tpl_snippet))
        self.assertEqual(expectedmessage, err.__str__())

    def test_node_template_properties(self):
        tpl_snippet = '''
        node_templates:
          server:
            type: tosca.nodes.Compute
            properties:
              os_image: F18_x86_64
            capabilities:
              host:
                properties:
                  disk_size: 10 GB
                  num_cpus: { get_input: cpus }
                  mem_size: 4096 MB
              os:
                properties:
                  architecture: x86_64
                  type: Linux
                  distribution: Fedora
                  version: 18.0
        '''
        expectedmessage = _('"properties" of template "server" contains '
                            'unknown field "os_image". Refer to the '
                            'definition to verify valid values.')
        err = self.assertRaises(
            exception.UnknownFieldError,
            lambda: self._single_node_template_content_test(tpl_snippet))
        self.assertEqual(expectedmessage, err.__str__())

    def test_node_template_interfaces(self):
        tpl_snippet = '''
        node_templates:
          wordpress:
            type: tosca.nodes.WebApplication.WordPress
            requirements:
              - host: webserver
              - database_endpoint: mysql_database
            interfaces:
              Standards:
                 create: wordpress_install.sh
                 configure:
                   implementation: wordpress_configure.sh
                   inputs:
                     wp_db_name: { get_property: [ mysql_database, db_name ] }
                     wp_db_user: { get_property: [ mysql_database, db_user ] }
                     wp_db_password: { get_property: [ mysql_database, \
                     db_password ] }
                     wp_db_port: { get_property: [ SELF, \
                     database_endpoint, port ] }
        '''
        expectedmessage = _('"interfaces" of template "wordpress" contains '
                            'unknown field "Standards". Refer to the '
                            'definition to verify valid values.')
        err = self.assertRaises(
            exception.UnknownFieldError,
            lambda: self._single_node_template_content_test(tpl_snippet))
        self.assertEqual(expectedmessage, err.__str__())

        tpl_snippet = '''
        node_templates:
          wordpress:
            type: tosca.nodes.WebApplication.WordPress
            requirements:
              - host: webserver
              - database_endpoint: mysql_database
            interfaces:
              Standard:
                 create: wordpress_install.sh
                 config:
                   implementation: wordpress_configure.sh
                   inputs:
                     wp_db_name: { get_property: [ mysql_database, db_name ] }
                     wp_db_user: { get_property: [ mysql_database, db_user ] }
                     wp_db_password: { get_property: [ mysql_database, \
                     db_password ] }
                     wp_db_port: { get_property: [ SELF, \
                     database_endpoint, port ] }
        '''
        expectedmessage = _('"interfaces" of template "wordpress" contains '
                            'unknown field "config". Refer to the definition '
                            'to verify valid values.')
        err = self.assertRaises(
            exception.UnknownFieldError,
            lambda: self._single_node_template_content_test(tpl_snippet))
        self.assertEqual(expectedmessage, err.__str__())

        tpl_snippet = '''
        node_templates:
          wordpress:
            type: tosca.nodes.WebApplication.WordPress
            requirements:
              - host: webserver
              - database_endpoint: mysql_database
            interfaces:
              Standard:
                 create: wordpress_install.sh
                 configure:
                   implementation: wordpress_configure.sh
                   input:
                     wp_db_name: { get_property: [ mysql_database, db_name ] }
                     wp_db_user: { get_property: [ mysql_database, db_user ] }
                     wp_db_password: { get_property: [ mysql_database, \
                     db_password ] }
                     wp_db_port: { get_ref_property: [ database_endpoint, \
                     database_endpoint, port ] }
        '''
        expectedmessage = _('"interfaces" of template "wordpress" contains '
                            'unknown field "input". Refer to the definition '
                            'to verify valid values.')
        err = self.assertRaises(
            exception.UnknownFieldError,
            lambda: self._single_node_template_content_test(tpl_snippet))
        self.assertEqual(expectedmessage, err.__str__())

    def test_relationship_template_properties(self):
        tpl_snippet = '''
        relationship_templates:
            storage_attachto:
                type: AttachesTo
                properties:
                  device: test_device
        '''
        expectedmessage = _('"properties" of template "storage_attachto" is '
                            'missing required field "[\'location\']".')
        rel_template = (toscaparser.utils.yamlparser.
                        simple_parse(tpl_snippet))['relationship_templates']
        name = list(rel_template.keys())[0]
        rel_template = RelationshipTemplate(rel_template[name], name)
        err = self.assertRaises(exception.MissingRequiredFieldError,
                                rel_template.validate)
        self.assertEqual(expectedmessage, six.text_type(err))

    def test_invalid_template_version(self):
        tosca_tpl = os.path.join(
            os.path.dirname(os.path.abspath(__file__)),
            "data/test_invalid_template_version.yaml")
        self.assertRaises(exception.ValidationError, ToscaTemplate, tosca_tpl)
        valid_versions = ', '.join(ToscaTemplate.VALID_TEMPLATE_VERSIONS)
        exception.ExceptionCollector.assertExceptionMessage(
            exception.InvalidTemplateVersion,
            (_('The template version "tosca_xyz" is invalid. Valid versions '
               'are "%s".') % valid_versions))

    def test_node_template_capabilities_properties(self):
        # validating capability property values
        tpl_snippet = '''
        node_templates:
          server:
            type: tosca.nodes.WebServer
            capabilities:
              data_endpoint:
                properties:
                  initiator: test
        '''
        expectedmessage = _('The value "test" of property "initiator" is '
                            'not valid. Expected a value from "[source, '
                            'target, peer]".')

        err = self.assertRaises(
            exception.ValidationError,
            lambda: self._single_node_template_content_test(tpl_snippet))
        self.assertEqual(expectedmessage, err.__str__())

        tpl_snippet = '''
        node_templates:
          server:
            type: tosca.nodes.Compute
            capabilities:
              host:
                properties:
                  disk_size: 10 GB
                  num_cpus: { get_input: cpus }
                  mem_size: 4096 MB
              os:
                properties:
                  architecture: x86_64
                  type: Linux
                  distribution: Fedora
                  version: 18.0
              scalable:
                properties:
                  min_instances: 1
                  max_instances: 3
                  default_instances: 5
        '''
        expectedmessage = _('"properties" of template "server": '
                            '"default_instances" value is not between '
                            '"min_instances" and "max_instances".')
        err = self.assertRaises(
            exception.ValidationError,
            lambda: self._single_node_template_content_test(tpl_snippet))
        self.assertEqual(expectedmessage, err.__str__())

    def test_node_template_objectstorage_without_required_property(self):
        tpl_snippet = '''
        node_templates:
          server:
            type: tosca.nodes.ObjectStorage
            properties:
              maxsize: 1 GB
        '''
        expectedmessage = _('"properties" of template "server" is missing '
                            'required field "[\'name\']".')
        err = self.assertRaises(
            exception.MissingRequiredFieldError,
            lambda: self._single_node_template_content_test(tpl_snippet))
        self.assertEqual(expectedmessage, err.__str__())

    def test_node_template_objectstorage_with_invalid_scalar_unit(self):
        tpl_snippet = '''
        node_templates:
          server:
            type: tosca.nodes.ObjectStorage
            properties:
              name: test
              maxsize: -1
        '''
        expectedmessage = _('"-1" is not a valid scalar-unit.')
        err = self.assertRaises(
            ValueError,
            lambda: self._single_node_template_content_test(tpl_snippet))
        self.assertEqual(expectedmessage, err.__str__())

    def test_node_template_objectstorage_with_invalid_scalar_type(self):
        tpl_snippet = '''
        node_templates:
          server:
            type: tosca.nodes.ObjectStorage
            properties:
              name: test
              maxsize: 1 XB
        '''
        expectedmessage = _('"1 XB" is not a valid scalar-unit.')
        err = self.assertRaises(
            ValueError,
            lambda: self._single_node_template_content_test(tpl_snippet))
        self.assertEqual(expectedmessage, err.__str__())

    def test_special_keywords(self):
        """Test special keywords

           Test that special keywords, e.g. metadata, which are not part
           of specification do not throw any validation error.
        """
        tpl_snippet_metadata_map = '''
        node_templates:
          server:
            type: tosca.nodes.Compute
            metadata:
              name: server A
              role: master
        '''
        self._single_node_template_content_test(tpl_snippet_metadata_map)

        tpl_snippet_metadata_inline = '''
        node_templates:
          server:
            type: tosca.nodes.Compute
            metadata: none
        '''
        self._single_node_template_content_test(tpl_snippet_metadata_inline)

    def test_policy_valid_keynames(self):
        tpl_snippet = '''
        policies:
          - servers_placement:
              type: tosca.policies.Placement
              description: Apply placement policy to servers
              metadata: { user1: 1001, user2: 1002 }
              targets: [ serv1, serv2 ]
        '''
        policies = (toscaparser.utils.yamlparser.
                    simple_parse(tpl_snippet))['policies'][0]
        name = list(policies.keys())[0]
        Policy(name, policies[name], None, None)

    def test_policy_invalid_keyname(self):
        tpl_snippet = '''
        policies:
          - servers_placement:
              type: tosca.policies.Placement
              testkey: testvalue
        '''
        policies = (toscaparser.utils.yamlparser.
                    simple_parse(tpl_snippet))['policies'][0]
        name = list(policies.keys())[0]

        expectedmessage = _('Policy "servers_placement" contains '
                            'unknown field "testkey". Refer to the '
                            'definition to verify valid values.')
        err = self.assertRaises(
            exception.UnknownFieldError,
            lambda: Policy(name, policies[name], None, None))
        self.assertEqual(expectedmessage, err.__str__())

    def test_policy_trigger_valid_keyname_senlin_resources(self):
        tpl_snippet = '''
        triggers:
         - resize_compute:
             description: trigger
             event_type: tosca.events.resource.utilization
             schedule:
               start_time: "2015-05-07T07:00:00Z"
               end_time: "2015-06-07T07:00:00Z"
             target_filter:
               node: master-container
               requirement: host
               capability: Container
             condition:
               constraint: { greater_than: 50 }
               period: 60
               evaluations: 1
               method : average
             action:
               resize: # Operation name
                inputs:
                 strategy: LEAST_USED
                 implementation: Senlin.webhook()
        '''
        triggers = (toscaparser.utils.yamlparser.
                    simple_parse(tpl_snippet))['triggers'][0]
        name = list(triggers.keys())[0]
        Triggers(name, triggers[name])

    def test_policy_trigger_valid_keyname_heat_resources(self):
        tpl_snippet = '''
        triggers:
         - high_cpu_usage:
             description: trigger
             meter_name: cpu_util
             condition:
               constraint: utilization greater_than 60%
               threshold: 60
               period: 600
               evaluations: 1
               method: average
               comparison_operator: gt
             metadata: SG1
             action: [SP1]
        '''
        triggers = (toscaparser.utils.yamlparser.
                    simple_parse(tpl_snippet))['triggers'][0]
        name = list(triggers.keys())[0]
        Triggers(name, triggers[name])

    def test_policy_trigger_invalid_keyname_senlin_resources(self):
        tpl_snippet = '''
        triggers:
         - resize_compute:
             description: trigger
             event_type: tosca.events.resource.utilization
             schedule:
               start_time: "2015-05-07T07:00:00Z"
               end_time: "2015-06-07T07:00:00Z"
             target_filter1:
               node: master-container
               requirement: host
               capability: Container
             condition:
               constraint: utilization greater_than 50%
               period1: 60
               evaluations: 1
               method: average
             action:
               resize: # Operation name
                inputs:
                 strategy: LEAST_USED
                 implementation: Senlin.webhook()
        '''
        triggers = (toscaparser.utils.yamlparser.
                    simple_parse(tpl_snippet))['triggers'][0]
        name = list(triggers.keys())[0]
        expectedmessage = _(
            'Triggers "resize_compute" contains unknown field '
            '"target_filter1". Refer to the definition '
            'to verify valid values.')
        err = self.assertRaises(
            exception.UnknownFieldError,
            lambda: Triggers(name, triggers[name]))
        self.assertEqual(expectedmessage, err.__str__())

    def test_policy_trigger_invalid_keyname_heat_resources(self):
        tpl_snippet = '''
        triggers:
         - high_cpu_usage:
             description: trigger
             meter_name: cpu_util
             condition:
               constraint: utilization greater_than 60%
               threshold: 60
               period: 600
               evaluations: 1
               method: average
               comparison_operator: gt
             metadata1: SG1
             action: [SP1]
        '''
        triggers = (toscaparser.utils.yamlparser.
                    simple_parse(tpl_snippet))['triggers'][0]
        name = list(triggers.keys())[0]
        expectedmessage = _(
            'Triggers "high_cpu_usage" contains unknown field '
            '"metadata1". Refer to the definition '
            'to verify valid values.')
        err = self.assertRaises(
            exception.UnknownFieldError,
            lambda: Triggers(name, triggers[name]))
        self.assertEqual(expectedmessage, err.__str__())

    def test_policy_missing_required_keyname(self):
        tpl_snippet = '''
        policies:
          - servers_placement:
              description: test description
        '''
        policies = (toscaparser.utils.yamlparser.
                    simple_parse(tpl_snippet))['policies'][0]
        name = list(policies.keys())[0]

        expectedmessage = _('Template "servers_placement" is missing '
                            'required field "type".')
        err = self.assertRaises(
            exception.MissingRequiredFieldError,
            lambda: Policy(name, policies[name], None, None))
        self.assertEqual(expectedmessage, err.__str__())

    def test_credential_datatype(self):
        tosca_tpl = os.path.join(
            os.path.dirname(os.path.abspath(__file__)),
            "data/test_credential_datatype.yaml")
        self.assertIsNotNone(ToscaTemplate(tosca_tpl))

    def test_invalid_default_value(self):
        tpl_path = os.path.join(
            os.path.dirname(os.path.abspath(__file__)),
            "data/test_invalid_input_defaults.yaml")
        self.assertRaises(exception.ValidationError, ToscaTemplate, tpl_path)
        exception.ExceptionCollector.assertExceptionMessage(
            ValueError, _('"two" is not an integer.'))

    def test_invalid_capability(self):
        tpl_snippet = '''
        node_templates:
          server:
            type: tosca.nodes.Compute
            capabilities:
                oss:
                    properties:
                        architecture: x86_64
        '''
        tpl = (toscaparser.utils.yamlparser.simple_parse(tpl_snippet))
        err = self.assertRaises(exception.UnknownFieldError,
                                TopologyTemplate, tpl, None)
        expectedmessage = _('"capabilities" of template "server" contains '
                            'unknown field "oss". Refer to the definition '
                            'to verify valid values.')
        self.assertEqual(expectedmessage, err.__str__())

    def test_qualified_name(self):
        tpl_snippet_full_name = '''
        node_templates:
          supported_type:
            type: tosca.nodes.Compute
        '''
        tpl = (
            toscaparser.utils.yamlparser.simple_parse(
                tpl_snippet_full_name))
        TopologyTemplate(tpl, None)

        tpl_snippet_short_name = '''
        node_templates:
          supported_type:
            type: Compute
        '''
        tpl = (
            toscaparser.utils.yamlparser.simple_parse(
                tpl_snippet_short_name))
        TopologyTemplate(tpl, None)

        tpl_snippet_qualified_name = '''
        node_templates:
          supported_type:
            type: tosca:Compute
        '''
        tpl = (
            toscaparser.utils.yamlparser.simple_parse(
                tpl_snippet_qualified_name))
        TopologyTemplate(tpl, None)

    def test_requirements_as_list(self):
        """Node template with requirements provided with or without list

        Node template requirements are required to be provided as list.
        """

        expectedmessage = _('"requirements" of template "my_webserver"'
                            ' must be of type "list".')

        # requirements provided as dictionary
        tpl_snippet1 = '''
        node_templates:
          my_webserver:
            type: tosca.nodes.WebServer
            requirements:
              host: server
          server:
            type: tosca.nodes.Compute
        '''
        err1 = self.assertRaises(
            exception.TypeMismatchError,
            lambda: self._single_node_template_content_test(tpl_snippet1))
        self.assertEqual(expectedmessage, err1.__str__())

        # requirements provided as string
        tpl_snippet2 = '''
        node_templates:
          my_webserver:
            type: tosca.nodes.WebServer
            requirements: server
          server:
            type: tosca.nodes.Compute
        '''
        err2 = self.assertRaises(
            exception.TypeMismatchError,
            lambda: self._single_node_template_content_test(tpl_snippet2))
        self.assertEqual(expectedmessage, err2.__str__())

        # requirements provided as list
        tpl_snippet3 = '''
        node_templates:
          my_webserver:
            type: tosca.nodes.WebServer
            requirements:
              - host: server
          server:
            type: tosca.nodes.Compute
        '''
        self.assertIsNone(
            self._single_node_template_content_test(tpl_snippet3))

    def test_properties_override_with_flavor_and_image(self):
        tpl_path = os.path.join(
            os.path.dirname(os.path.abspath(__file__)),
            "data/test_normative_type_properties_override.yaml")
        self.assertIsNotNone(ToscaTemplate(tpl_path))