2 # ============LICENSE_START====================================================
3 # org.onap.vvp/validation-scripts
4 # ===================================================================
5 # Copyright © 2019 AT&T Intellectual Property. All rights reserved.
6 # ===================================================================
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
13 # http://www.apache.org/licenses/LICENSE-2.0
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.
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
28 # https://creativecommons.org/licenses/by/4.0/
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.
36 # ============LICENSE_END============================================
39 from pathlib import Path
40 from shutil import rmtree
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
49 THIS_DIR = Path(__file__).parent
50 SAMPLE_HEAT_DIR = THIS_DIR / "sample_heat"
54 with path.open("r") as f:
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)
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))
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
79 pytestconfig.getoption = fake_getoption
80 templates = get_heat_templates(pytestconfig)
81 env = PreloadEnvironment(THIS_DIR / "sample_env")
83 generator = GrApiPreloadGenerator(vnf, session_dir, env)
88 @pytest.fixture(scope="session")
90 return load_module(preload, "base_incomplete.json")
93 @pytest.fixture(scope="session")
94 def incremental(preload):
95 return load_module(preload, "incremental_incomplete.json")
98 def test_base_fields(base):
99 data = base["input"]["preload-vf-module-topology-information"][
100 "vnf-topology-identifier-structure"
102 assert data["vnf-name"] == "VALUE FOR: vnf_name"
103 assert "<Service Name>/<VF Instance Name>" in data["vnf-type"]
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)
112 assert az[0] == "VALUE FOR: availability_zone_0"
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")
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"}]},
129 def test_base_vm_types(base):
130 vms = base["input"]["preload-vf-module-topology-information"]["vf-module-topology"][
131 "vf-module-assignments"
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")
139 "vm-names": {"vm-name": ["VALUE FOR: db_name_0", "VALUE FOR: db_name_1"]},
143 "network-role": "oam",
144 "network-information-items": {
145 "network-information-item": [
152 "VALUE FOR: db_oam_ip_0",
153 "VALUE FOR: db_oam_ip_1",
161 "network-ips": {"network-ip": []},
165 "mac-addresses": {"mac-address": []},
166 "floating-ips": {"floating-ip-v4": [], "floating-ip-v6": []},
167 "interface-route-prefixes": {"interface-route-prefix": []},
170 "network-role": "ha",
171 "network-information-items": {
172 "network-information-item": [
177 "network-ips": {"network-ip": []},
183 "network-ips": {"network-ip": []},
187 "mac-addresses": {"mac-address": []},
189 "floating-ip-v4": ["VALUE FOR: db_ha_floating_ip"],
190 "floating-ip-v6": ["VALUE FOR: db_ha_floating_v6_ip"],
192 "interface-route-prefixes": {"interface-route-prefix": []},
199 def test_base_general(base):
200 general = base["input"]["preload-vf-module-topology-information"][
202 ]["vf-module-topology-identifier"]
204 general["vf-module-type"] == "VALUE FOR: <vfModuleModelName> from CSAR or SDC"
206 assert general["vf-module-name"] == "VALUE FOR: vf_module_name"
209 def test_base_parameters(base):
210 params = base["input"]["preload-vf-module-topology-information"][
212 ]["vf-module-parameters"]["param"]
214 {"name": "svc_image_name", "value": "svc_image"},
215 {"name": "svc_flavor_name", "value": "svc_flavor"},
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)
225 assert az[0] == "VALUE FOR: availability_zone_0"
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"
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"]
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"
252 assert vnf_name == "VALUE FOR: vnf_name"