aboutsummaryrefslogtreecommitdiffstats
path: root/ice_validator/app_tests/preload_tests/test_grapi.py
blob: 10b090dc667cd03978c4a0d5d9ef5924d15d4685 (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
# -*- 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============================================
import json
import tempfile
from pathlib import Path
from shutil import rmtree

import pytest

from preload.environment import EnvironmentFileDataSource
from preload.model import Vnf, get_heat_templates
from preload_grapi import GrApiPreloadGenerator
from tests.helpers import first

THIS_DIR = Path(__file__).parent
SAMPLE_HEAT_DIR = THIS_DIR / "sample_heat"


def load_json(path):
    with path.open("r") as f:
        return json.load(f)


def load_module(base_dir, name):
    path = Path(str(base_dir / "grapi" / name))
    assert path.exists(), "{} does not exist".format(path)
    return load_json(path)


@pytest.fixture(scope="session")
def session_dir(request):
    # Temporary directory that gets deleted at the session
    # pytest tmpdir doesn't support a non-function scoped temporary directory
    session_dir = Path(tempfile.mkdtemp())
    request.addfinalizer(lambda: rmtree(session_dir))
    return session_dir


@pytest.fixture(scope="session")
def preload(pytestconfig, session_dir):
    # Generate the preloads for testing
    def fake_getoption(opt, default=None):
        return [SAMPLE_HEAT_DIR.as_posix()] if opt == "template_dir" else None

    pytestconfig.getoption = fake_getoption
    templates = get_heat_templates(pytestconfig)
    vnf = Vnf(templates)
    datasource = EnvironmentFileDataSource(THIS_DIR / "sample_env")
    generator = GrApiPreloadGenerator(vnf, session_dir, datasource)
    generator.generate()
    return session_dir


@pytest.fixture(scope="session")
def base(preload):
    return load_module(preload, "base.json")


@pytest.fixture(scope="session")
def incremental(preload):
    return load_module(preload, "incremental.json")


def test_incomplete_filenames(preload):
    base = THIS_DIR / "sample_env/preloads/grapi/base_incomplete.json"
    inc = THIS_DIR / "sample_env/preloads/grapi/incremental_incomplete.json"
    assert base.exists()
    assert inc.exists()


def test_base_fields(base):
    data = base["input"]["preload-vf-module-topology-information"][
        "vnf-topology-identifier-structure"
    ]
    assert data["vnf-name"] == "VALUE FOR: vnf_name"
    assert "<Service Name>/<VF Instance Name>" in data["vnf-type"]


def test_base_azs(base):
    az = base["input"]["preload-vf-module-topology-information"][
        "vnf-resource-assignments"
    ]["availability-zones"]["availability-zone"]
    assert isinstance(az, list)
    assert len(az) == 2
    assert az[0] == "VALUE FOR: availability_zone_0"


def test_base_networks(base):
    nets = base["input"]["preload-vf-module-topology-information"][
        "vnf-resource-assignments"
    ]["vnf-networks"]["vnf-network"]
    assert isinstance(nets, list)
    assert len(nets) == 3
    oam = first(nets, lambda n: n["network-role"] == "oam")
    assert oam == {
        "network-role": "oam",
        "network-name": "VALUE FOR: network name of oam_net_id",
        "subnets-data": {
            "subnet-data": [{"subnet-name": "VALUE FOR: name of oam_subnet_id"}]
        },
    }


def test_base_vm_types(base):
    vms = base["input"]["preload-vf-module-topology-information"]["vf-module-topology"][
        "vf-module-assignments"
    ]["vms"]["vm"]
    vm_types = {vm["vm-type"] for vm in vms}
    assert vm_types == {"db", "svc", "mgmt", "lb"}
    db = first(vms, lambda v: v["vm-type"] == "db")
    assert db == {
        "vm-type": "db",
        "vm-count": 2,
        "vm-names": {"vm-name": ["VALUE FOR: db_name_0", "VALUE FOR: db_name_1"]},
        "vm-networks": {
            "vm-network": [
                {
                    "network-role": "oam",
                    "network-role-tag": "oam",
                    "network-information-items": {
                        "network-information-item": [
                            {
                                "ip-version": "4",
                                "use-dhcp": "N",
                                "ip-count": 2,
                                "network-ips": {
                                    "network-ip": [
                                        "VALUE FOR: db_oam_ip_0",
                                        "VALUE FOR: db_oam_ip_1",
                                    ]
                                },
                            },
                            {
                                "ip-version": "6",
                                "use-dhcp": "N",
                                "ip-count": 0,
                                "network-ips": {"network-ip": []},
                            },
                        ]
                    },
                    "mac-addresses": {"mac-address": []},
                    "floating-ips": {"floating-ip-v4": [], "floating-ip-v6": []},
                    "interface-route-prefixes": {"interface-route-prefix": []},
                },
                {
                    "network-role": "ha",
                    "network-role-tag": "ha",
                    "network-information-items": {
                        "network-information-item": [
                            {
                                "ip-version": "4",
                                "use-dhcp": "Y",
                                "ip-count": 0,
                                "network-ips": {"network-ip": []},
                            },
                            {
                                "ip-version": "6",
                                "use-dhcp": "Y",
                                "ip-count": 0,
                                "network-ips": {"network-ip": []},
                            },
                        ]
                    },
                    "mac-addresses": {"mac-address": []},
                    "floating-ips": {
                        "floating-ip-v4": ["VALUE FOR: db_ha_floating_ip"],
                        "floating-ip-v6": ["VALUE FOR: db_ha_floating_v6_ip"],
                    },
                    "interface-route-prefixes": {"interface-route-prefix": []},
                },
            ]
        },
    }


def test_base_general(base):
    general = base["input"]["preload-vf-module-topology-information"][
        "vf-module-topology"
    ]["vf-module-topology-identifier"]
    assert (
        general["vf-module-type"] == "VALUE FOR: <vfModuleModelName> from CSAR or SDC"
    )
    assert general["vf-module-name"] == "VALUE FOR: vf_module_name"


def test_base_parameters(base):
    params = base["input"]["preload-vf-module-topology-information"][
        "vf-module-topology"
    ]["vf-module-parameters"]["param"]
    assert params == []


def test_incremental(incremental):
    az = incremental["input"]["preload-vf-module-topology-information"][
        "vnf-resource-assignments"
    ]["availability-zones"]["availability-zone"]
    assert isinstance(az, list)
    assert len(az) == 1
    assert az[0] == "VALUE FOR: availability_zone_0"


def test_incremental_networks(incremental):
    nets = incremental["input"]["preload-vf-module-topology-information"][
        "vnf-resource-assignments"
    ]["vnf-networks"]["vnf-network"]
    assert isinstance(nets, list)
    assert len(nets) == 1
    assert nets[0]["network-role"] == "ha"


def test_preload_env_population(preload):
    base_path = THIS_DIR / "sample_env/preloads/grapi/base_incomplete.json"
    data = load_json(base_path)
    azs = data["input"]["preload-vf-module-topology-information"][
        "vnf-resource-assignments"
    ]["availability-zones"]["availability-zone"]
    assert azs == ["az0", "az1"]


def test_preload_env_population_missing_value(preload):
    base_path = THIS_DIR / "sample_env/preloads/grapi/base_incomplete.json"
    data = load_json(base_path)
    vnf_name = data["input"]["preload-vf-module-topology-information"][
        "vnf-topology-identifier-structure"
    ]["vnf-name"]
    assert vnf_name == "VALUE FOR: vnf_name"