aboutsummaryrefslogtreecommitdiffstats
path: root/ice_validator/tests/test_non_server_name.py
blob: 9361389defc21ea6e18d5300e49b5c791e525cc8 (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
# -*- coding: utf8 -*-
# ============LICENSE_START====================================================
# org.onap.vvp/validation-scripts
# ===================================================================
# Copyright © 2019 AT&T Intellectual Property. All rights reserved.
# ===================================================================
#
# Unless otherwise specified, all software contained herein is licensed
# under the Apache License, Version 2.0 (the "License");
# you may not use this software 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.
#
#
#
# Unless otherwise specified, all documentation contained herein is licensed
# under the Creative Commons License, Attribution 4.0 Intl. (the "License");
# you may not use this documentation except in compliance with the License.
# You may obtain a copy of the License at
#
#             https://creativecommons.org/licenses/by/4.0/
#
# Unless required by applicable law or agreed to in writing, documentation
# 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.
#
# ============LICENSE_END============================================
#
#

"""
resource property name
"""
import os
import collections
import pytest

from .structures import Heat
from .structures import HeatProcessor
from .helpers import validates
from tests.utils import nested_files

VERSION = "1.2.0"


def get_non_servers(heat):
    """
    Return a dict of non servers.  key is rid, value is resource
    """
    non_servers = {
        rid: resource
        for rid, resource in heat.resources.items()
        if heat.nested_get(resource, "type") != "OS::Nova::Server"
    }
    return non_servers


@validates("R-85734")
def test_non_server_name(yaml_file):
    """
    If a VNF's Heat Orchestration Template contains the property ``name``
    for a non ``OS::Nova::Server`` resource, the intrinsic function
    ``str_replace`` **MUST** be used in conjunction with the ONAP
    supplied metadata parameter ``vnf_name`` to generate a unique value.

    """
    h = Heat(filepath=yaml_file)
    if not h.resources:
        pytest.skip("No resources in this template")

    non_servers = get_non_servers(h)
    if not non_servers:
        pytest.skip("No non-server resources in this template")

    bad = []
    for rid, resource in non_servers.items():
        name = h.nested_get(resource, "properties", "name")
        if not name:
            continue

        # Make sure it uses str_replace
        str_replace = name.get("str_replace") if hasattr(name, "get") else None
        if not str_replace:
            bad.append("{}'s name property does not use str_replace".format(rid))
            continue

        # Make sure str_replace is properly formatted
        if not all(key in str_replace for key in ("template", "params")):
            bad.append(
                (
                    "{}'s name property use of str_replace is "
                    + "missing template, params, or both"
                ).format(rid)
            )
            continue
        params = str_replace["params"]
        if not isinstance(params, dict):
            bad.append(
                (
                    "{}'s name property's use of str_replace.params is "
                    + "missing or invalid"
                ).format(rid)
            )
            continue

        # Find the param that uses vnf_name
        vnf_name_param = None
        for key, value in params.items():
            if not isinstance(value, dict):
                continue
            if value.get("get_param", "") == "vnf_name":
                vnf_name_param = key
                break
        if not vnf_name_param:
            bad.append(
                (
                    "{}'s name property's use str_replace does not "
                    + "use have a params that maps to the parameter "
                    "via {{get_param: vnf_name}}"
                ).format(rid)
            )
            continue

        # make sure the VNF name is used in the template string
        template = str_replace.get("template") or ""
        if vnf_name_param not in template:
            bad.append(
                (
                    "{}'s name property's str_replace template does "
                    + "not incorporate vnf_name; expected {} in "
                    + "template ({})"
                ).format(rid, vnf_name_param, template)
            )
    msg = (
        "Improper name property for" " non-OS::Nova::Server resources. "
    ) + ". ".join(bad)

    assert not bad, msg


@validates("R-85734")
def test_non_server_name_unique(heat_template):
    """Test name has unique value
    """
    list_nest = nested_files.get_list_of_nested_files(
        heat_template, os.path.dirname(heat_template)
    )
    list_nest.append(heat_template)
    non_servers = {}
    for yaml_file in list_nest:
        h = Heat(filepath=yaml_file)
        non_servers.update(get_non_servers(h))
    names = collections.defaultdict(set)
    for rid, resource in non_servers.items():
        name = HeatProcessor.get_str_replace_name(resource)
        if name:
            names[name].add(rid)
    bad = {key: value for key, value in names.items() if len(value) > 1}
    delim = "\n" + 4 * " "
    assert not bad, "Names must be unique," " not shared across resource ids.%s%s" % (
        delim,
        delim.join("%s: %s" % (name, list(value)) for name, value in bad.items()),
    )