Merge "[VVP] Support pluggable data sources for preload data"
[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 EnvironmentFileDataSource
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     vnf = Vnf(templates)
82     datasource = EnvironmentFileDataSource(THIS_DIR / "sample_env")
83     generator = GrApiPreloadGenerator(vnf, session_dir, datasource)
84     generator.generate()
85     return session_dir
86
87
88 @pytest.fixture(scope="session")
89 def base(preload):
90     return load_module(preload, "base.json")
91
92
93 @pytest.fixture(scope="session")
94 def incremental(preload):
95     return load_module(preload, "incremental.json")
96
97
98 def test_incomplete_filenames(preload):
99     base = THIS_DIR / "sample_env/preloads/grapi/base_incomplete.json"
100     inc = THIS_DIR / "sample_env/preloads/grapi/incremental_incomplete.json"
101     assert base.exists()
102     assert inc.exists()
103
104
105 def test_base_fields(base):
106     data = base["input"]["preload-vf-module-topology-information"][
107         "vnf-topology-identifier-structure"
108     ]
109     assert data["vnf-name"] == "VALUE FOR: vnf_name"
110     assert "<Service Name>/<VF Instance Name>" in data["vnf-type"]
111
112
113 def test_base_azs(base):
114     az = base["input"]["preload-vf-module-topology-information"][
115         "vnf-resource-assignments"
116     ]["availability-zones"]["availability-zone"]
117     assert isinstance(az, list)
118     assert len(az) == 2
119     assert az[0] == "VALUE FOR: availability_zone_0"
120
121
122 def test_base_networks(base):
123     nets = base["input"]["preload-vf-module-topology-information"][
124         "vnf-resource-assignments"
125     ]["vnf-networks"]["vnf-network"]
126     assert isinstance(nets, list)
127     assert len(nets) == 3
128     oam = first(nets, lambda n: n["network-role"] == "oam")
129     assert oam == {
130         "network-role": "oam",
131         "network-name": "VALUE FOR: network name of oam_net_id",
132         "subnets-data": {
133             "subnet-data": [{"subnet-name": "VALUE FOR: name of oam_subnet_id"}]
134         },
135     }
136
137
138 def test_base_vm_types(base):
139     vms = base["input"]["preload-vf-module-topology-information"]["vf-module-topology"][
140         "vf-module-assignments"
141     ]["vms"]["vm"]
142     vm_types = {vm["vm-type"] for vm in vms}
143     assert vm_types == {"db", "svc", "mgmt", "lb"}
144     db = first(vms, lambda v: v["vm-type"] == "db")
145     assert db == {
146         "vm-type": "db",
147         "vm-count": 2,
148         "vm-names": {"vm-name": ["VALUE FOR: db_name_0", "VALUE FOR: db_name_1"]},
149         "vm-networks": {
150             "vm-network": [
151                 {
152                     "network-role": "oam",
153                     "network-role-tag": "oam",
154                     "network-information-items": {
155                         "network-information-item": [
156                             {
157                                 "ip-version": "4",
158                                 "use-dhcp": "N",
159                                 "ip-count": 2,
160                                 "network-ips": {
161                                     "network-ip": [
162                                         "VALUE FOR: db_oam_ip_0",
163                                         "VALUE FOR: db_oam_ip_1",
164                                     ]
165                                 },
166                             },
167                             {
168                                 "ip-version": "6",
169                                 "use-dhcp": "N",
170                                 "ip-count": 0,
171                                 "network-ips": {"network-ip": []},
172                             },
173                         ]
174                     },
175                     "mac-addresses": {"mac-address": []},
176                     "floating-ips": {"floating-ip-v4": [], "floating-ip-v6": []},
177                     "interface-route-prefixes": {"interface-route-prefix": []},
178                 },
179                 {
180                     "network-role": "ha",
181                     "network-role-tag": "ha",
182                     "network-information-items": {
183                         "network-information-item": [
184                             {
185                                 "ip-version": "4",
186                                 "use-dhcp": "Y",
187                                 "ip-count": 0,
188                                 "network-ips": {"network-ip": []},
189                             },
190                             {
191                                 "ip-version": "6",
192                                 "use-dhcp": "Y",
193                                 "ip-count": 0,
194                                 "network-ips": {"network-ip": []},
195                             },
196                         ]
197                     },
198                     "mac-addresses": {"mac-address": []},
199                     "floating-ips": {
200                         "floating-ip-v4": ["VALUE FOR: db_ha_floating_ip"],
201                         "floating-ip-v6": ["VALUE FOR: db_ha_floating_v6_ip"],
202                     },
203                     "interface-route-prefixes": {"interface-route-prefix": []},
204                 },
205             ]
206         },
207     }
208
209
210 def test_base_general(base):
211     general = base["input"]["preload-vf-module-topology-information"][
212         "vf-module-topology"
213     ]["vf-module-topology-identifier"]
214     assert (
215         general["vf-module-type"] == "VALUE FOR: <vfModuleModelName> from CSAR or SDC"
216     )
217     assert general["vf-module-name"] == "VALUE FOR: vf_module_name"
218
219
220 def test_base_parameters(base):
221     params = base["input"]["preload-vf-module-topology-information"][
222         "vf-module-topology"
223     ]["vf-module-parameters"]["param"]
224     assert params == []
225
226
227 def test_incremental(incremental):
228     az = incremental["input"]["preload-vf-module-topology-information"][
229         "vnf-resource-assignments"
230     ]["availability-zones"]["availability-zone"]
231     assert isinstance(az, list)
232     assert len(az) == 1
233     assert az[0] == "VALUE FOR: availability_zone_0"
234
235
236 def test_incremental_networks(incremental):
237     nets = incremental["input"]["preload-vf-module-topology-information"][
238         "vnf-resource-assignments"
239     ]["vnf-networks"]["vnf-network"]
240     assert isinstance(nets, list)
241     assert len(nets) == 1
242     assert nets[0]["network-role"] == "ha"
243
244
245 def test_preload_env_population(preload):
246     base_path = THIS_DIR / "sample_env/preloads/grapi/base_incomplete.json"
247     data = load_json(base_path)
248     azs = data["input"]["preload-vf-module-topology-information"][
249         "vnf-resource-assignments"
250     ]["availability-zones"]["availability-zone"]
251     assert azs == ["az0", "az1"]
252
253
254 def test_preload_env_population_missing_value(preload):
255     base_path = THIS_DIR / "sample_env/preloads/grapi/base_incomplete.json"
256     data = load_json(base_path)
257     vnf_name = data["input"]["preload-vf-module-topology-information"][
258         "vnf-topology-identifier-structure"
259     ]["vnf-name"]
260     assert vnf_name == "VALUE FOR: vnf_name"