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 EnvironmentFileDataSource
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)
82 datasource = EnvironmentFileDataSource(THIS_DIR / "sample_env")
83 generator = GrApiPreloadGenerator(vnf, session_dir, datasource)
88 @pytest.fixture(scope="session")
90 return load_module(preload, "base.json")
93 @pytest.fixture(scope="session")
94 def incremental(preload):
95 return load_module(preload, "incremental.json")
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"
105 def test_base_fields(base):
106 data = base["input"]["preload-vf-module-topology-information"][
107 "vnf-topology-identifier-structure"
109 assert data["vnf-name"] == "VALUE FOR: vnf_name"
110 assert "<Service Name>/<VF Instance Name>" in data["vnf-type"]
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)
119 assert az[0] == "VALUE FOR: availability_zone_0"
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")
130 "network-role": "oam",
131 "network-name": "VALUE FOR: network name of oam_net_id",
133 "subnet-data": [{"subnet-name": "VALUE FOR: name of oam_subnet_id"}]
138 def test_base_vm_types(base):
139 vms = base["input"]["preload-vf-module-topology-information"]["vf-module-topology"][
140 "vf-module-assignments"
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")
148 "vm-names": {"vm-name": ["VALUE FOR: db_name_0", "VALUE FOR: db_name_1"]},
152 "network-role": "oam",
153 "network-role-tag": "oam",
154 "network-information-items": {
155 "network-information-item": [
162 "VALUE FOR: db_oam_ip_0",
163 "VALUE FOR: db_oam_ip_1",
171 "network-ips": {"network-ip": []},
175 "mac-addresses": {"mac-address": []},
176 "floating-ips": {"floating-ip-v4": [], "floating-ip-v6": []},
177 "interface-route-prefixes": {"interface-route-prefix": []},
180 "network-role": "ha",
181 "network-role-tag": "ha",
182 "network-information-items": {
183 "network-information-item": [
188 "network-ips": {"network-ip": []},
194 "network-ips": {"network-ip": []},
198 "mac-addresses": {"mac-address": []},
200 "floating-ip-v4": ["VALUE FOR: db_ha_floating_ip"],
201 "floating-ip-v6": ["VALUE FOR: db_ha_floating_v6_ip"],
203 "interface-route-prefixes": {"interface-route-prefix": []},
210 def test_base_general(base):
211 general = base["input"]["preload-vf-module-topology-information"][
213 ]["vf-module-topology-identifier"]
215 general["vf-module-type"] == "VALUE FOR: <vfModuleModelName> from CSAR or SDC"
217 assert general["vf-module-name"] == "VALUE FOR: vf_module_name"
220 def test_base_parameters(base):
221 params = base["input"]["preload-vf-module-topology-information"][
223 ]["vf-module-parameters"]["param"]
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)
233 assert az[0] == "VALUE FOR: availability_zone_0"
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"
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"]
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"
260 assert vnf_name == "VALUE FOR: vnf_name"