[VVP] Misc tweaks and fixes to preload generation
[vvp/validation-scripts.git] / ice_validator / app_tests / preload_tests / test_grapi.py
1 # -*- coding: utf8 -*-
2 # ============LICENSE_START====================================================
3 # org.onap.vvp/validation-scripts
4 # ===================================================================
5 # Copyright © 2019 AT&T Intellectual Property. All rights reserved.
6 # ===================================================================
7 #
8 # Unless otherwise specified, all software contained herein is licensed
9 # under the Apache License, Version 2.0 (the "License");
10 # you may not use this software except in compliance with the License.
11 # You may obtain a copy of the License at
12 #
13 #             http://www.apache.org/licenses/LICENSE-2.0
14 #
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS,
17 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 # See the License for the specific language governing permissions and
19 # limitations under the License.
20 #
21 #
22 #
23 # Unless otherwise specified, all documentation contained herein is licensed
24 # under the Creative Commons License, Attribution 4.0 Intl. (the "License");
25 # you may not use this documentation except in compliance with the License.
26 # You may obtain a copy of the License at
27 #
28 #             https://creativecommons.org/licenses/by/4.0/
29 #
30 # Unless required by applicable law or agreed to in writing, documentation
31 # distributed under the License is distributed on an "AS IS" BASIS,
32 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33 # See the License for the specific language governing permissions and
34 # limitations under the License.
35 #
36 # ============LICENSE_END============================================
37 import json
38 import tempfile
39 from pathlib import Path
40 from shutil import rmtree
41
42 import pytest
43
44 from preload.environment import PreloadEnvironment
45 from preload.model import Vnf, get_heat_templates
46 from preload_grapi import GrApiPreloadGenerator
47 from tests.helpers import first
48
49 THIS_DIR = Path(__file__).parent
50 SAMPLE_HEAT_DIR = THIS_DIR / "sample_heat"
51
52
53 def load_json(path):
54     with path.open("r") as f:
55         return json.load(f)
56
57
58 def load_module(base_dir, name):
59     path = Path(str(base_dir / "grapi" / name))
60     assert path.exists(), "{} does not exist".format(path)
61     return load_json(path)
62
63
64 @pytest.fixture(scope="session")
65 def session_dir(request):
66     # Temporary directory that gets deleted at the session
67     # pytest tmpdir doesn't support a non-function scoped temporary directory
68     session_dir = Path(tempfile.mkdtemp())
69     request.addfinalizer(lambda: rmtree(session_dir))
70     return session_dir
71
72
73 @pytest.fixture(scope="session")
74 def preload(pytestconfig, session_dir):
75     # Generate the preloads for testing
76     def fake_getoption(opt, default=None):
77         return [SAMPLE_HEAT_DIR.as_posix()] if opt == "template_dir" else None
78
79     pytestconfig.getoption = fake_getoption
80     templates = get_heat_templates(pytestconfig)
81     env = PreloadEnvironment(THIS_DIR / "sample_env")
82     vnf = Vnf(templates)
83     generator = GrApiPreloadGenerator(vnf, session_dir, env)
84     generator.generate()
85     return session_dir
86
87
88 @pytest.fixture(scope="session")
89 def base(preload):
90     return load_module(preload, "base_incomplete.json")
91
92
93 @pytest.fixture(scope="session")
94 def incremental(preload):
95     return load_module(preload, "incremental_incomplete.json")
96
97
98 def test_base_fields(base):
99     data = base["input"]["preload-vf-module-topology-information"][
100         "vnf-topology-identifier-structure"
101     ]
102     assert data["vnf-name"] == "VALUE FOR: vnf_name"
103     assert "<Service Name>/<VF Instance Name>" in data["vnf-type"]
104
105
106 def test_base_azs(base):
107     az = base["input"]["preload-vf-module-topology-information"][
108         "vnf-resource-assignments"
109     ]["availability-zones"]["availability-zone"]
110     assert isinstance(az, list)
111     assert len(az) == 2
112     assert az[0] == "VALUE FOR: availability_zone_0"
113
114
115 def test_base_networks(base):
116     nets = base["input"]["preload-vf-module-topology-information"][
117         "vnf-resource-assignments"
118     ]["vnf-networks"]["vnf-network"]
119     assert isinstance(nets, list)
120     assert len(nets) == 3
121     oam = first(nets, lambda n: n["network-role"] == "oam")
122     assert oam == {
123         "network-role": "oam",
124         "network-name": "VALUE FOR: network name of oam_net_id",
125         "subnets-data": {"subnet-data": [{"subnet-id": "VALUE FOR: oam_subnet_id"}]},
126     }
127
128
129 def test_base_vm_types(base):
130     vms = base["input"]["preload-vf-module-topology-information"]["vf-module-topology"][
131         "vf-module-assignments"
132     ]["vms"]["vm"]
133     vm_types = {vm["vm-type"] for vm in vms}
134     assert vm_types == {"db", "svc", "mgmt", "lb"}
135     db = first(vms, lambda v: v["vm-type"] == "db")
136     assert db == {
137         "vm-type": "db",
138         "vm-count": 2,
139         "vm-names": {"vm-name": ["VALUE FOR: db_name_0", "VALUE FOR: db_name_1"]},
140         "vm-networks": {
141             "vm-network": [
142                 {
143                     "network-role": "oam",
144                     "network-information-items": {
145                         "network-information-item": [
146                             {
147                                 "ip-version": "4",
148                                 "use-dhcp": "N",
149                                 "ip-count": 2,
150                                 "network-ips": {
151                                     "network-ip": [
152                                         "VALUE FOR: db_oam_ip_0",
153                                         "VALUE FOR: db_oam_ip_1",
154                                     ]
155                                 },
156                             },
157                             {
158                                 "ip-version": "6",
159                                 "use-dhcp": "N",
160                                 "ip-count": 0,
161                                 "network-ips": {"network-ip": []},
162                             },
163                         ]
164                     },
165                     "mac-addresses": {"mac-address": []},
166                     "floating-ips": {"floating-ip-v4": [], "floating-ip-v6": []},
167                     "interface-route-prefixes": {"interface-route-prefix": []},
168                 },
169                 {
170                     "network-role": "ha",
171                     "network-information-items": {
172                         "network-information-item": [
173                             {
174                                 "ip-version": "4",
175                                 "use-dhcp": "N",
176                                 "ip-count": 0,
177                                 "network-ips": {"network-ip": []},
178                             },
179                             {
180                                 "ip-version": "6",
181                                 "use-dhcp": "N",
182                                 "ip-count": 0,
183                                 "network-ips": {"network-ip": []},
184                             },
185                         ]
186                     },
187                     "mac-addresses": {"mac-address": []},
188                     "floating-ips": {
189                         "floating-ip-v4": ["VALUE FOR: db_ha_floating_ip"],
190                         "floating-ip-v6": ["VALUE FOR: db_ha_floating_v6_ip"],
191                     },
192                     "interface-route-prefixes": {"interface-route-prefix": []},
193                 },
194             ]
195         },
196     }
197
198
199 def test_base_general(base):
200     general = base["input"]["preload-vf-module-topology-information"][
201         "vf-module-topology"
202     ]["vf-module-topology-identifier"]
203     assert (
204         general["vf-module-type"] == "VALUE FOR: <vfModuleModelName> from CSAR or SDC"
205     )
206     assert general["vf-module-name"] == "VALUE FOR: vf_module_name"
207
208
209 def test_base_parameters(base):
210     params = base["input"]["preload-vf-module-topology-information"][
211         "vf-module-topology"
212     ]["vf-module-parameters"]["param"]
213     assert params == [
214         {"name": "db_vol0_id", "value": "VALUE FOR: db_vol0_id"},
215         {"name": "db_vol1_id", "value": "VALUE FOR: db_vol1_id"},
216     ]
217
218
219 def test_incremental(incremental):
220     az = incremental["input"]["preload-vf-module-topology-information"][
221         "vnf-resource-assignments"
222     ]["availability-zones"]["availability-zone"]
223     assert isinstance(az, list)
224     assert len(az) == 1
225     assert az[0] == "VALUE FOR: availability_zone_0"
226
227
228 def test_incremental_networks(incremental):
229     nets = incremental["input"]["preload-vf-module-topology-information"][
230         "vnf-resource-assignments"
231     ]["vnf-networks"]["vnf-network"]
232     assert isinstance(nets, list)
233     assert len(nets) == 1
234     assert nets[0]["network-role"] == "ha"
235
236
237 def test_preload_env_population(preload):
238     base_path = THIS_DIR / "sample_env/preloads/grapi/base_incomplete.json"
239     data = load_json(base_path)
240     azs = data["input"]["preload-vf-module-topology-information"][
241         "vnf-resource-assignments"
242     ]["availability-zones"]["availability-zone"]
243     assert azs == ["az0", "az1"]
244
245
246 def test_preload_env_population_missing_value(preload):
247     base_path = THIS_DIR / "sample_env/preloads/grapi/base_incomplete.json"
248     data = load_json(base_path)
249     vnf_name = data["input"]["preload-vf-module-topology-information"][
250         "vnf-topology-identifier-structure"
251     ]["vnf-name"]
252     assert vnf_name == "VALUE FOR: vnf_name"