[VVP] Preload Generation Enhancements and Fixes
[vvp/validation-scripts.git] / ice_validator / app_tests / preload_tests / test_vnfapi.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 tempfile
38 from pathlib import Path
39 from shutil import rmtree
40
41 import pytest
42
43 from app_tests.preload_tests.test_grapi import load_json
44 from preload.environment import PreloadEnvironment
45 from preload.model import Vnf, get_heat_templates
46 from preload_vnfapi import VnfApiPreloadGenerator
47 from tests.helpers import load_yaml, first
48
49 THIS_DIR = Path(__file__).parent
50 SAMPLE_HEAT_DIR = THIS_DIR / "sample_heat"
51
52
53 def load_module(base_dir, name):
54     path = Path(str(base_dir / "vnfapi" / name))
55     assert path.exists(), "{} does not exist".format(path)
56     return load_yaml(str(path))
57
58
59 @pytest.fixture(scope="session")
60 def session_dir(request):
61     # Temporary directory that gets deleted at the session
62     # pytest tmpdir doesn't support a non-function scoped temporary directory
63     session_dir = Path(tempfile.mkdtemp())
64     request.addfinalizer(lambda: rmtree(session_dir))
65     return session_dir
66
67
68 @pytest.fixture(scope="session")
69 def preload(pytestconfig, session_dir):
70     # Generate the preloads for testing
71     def fake_getoption(opt, default=None):
72         return [SAMPLE_HEAT_DIR.as_posix()] if opt == "template_dir" else None
73
74     pytestconfig.getoption = fake_getoption
75     templates = get_heat_templates(pytestconfig)
76     vnf = Vnf(templates)
77     preload_env = PreloadEnvironment(THIS_DIR / "sample_env")
78     generator = VnfApiPreloadGenerator(vnf, session_dir, preload_env)
79     generator.generate()
80     return session_dir
81
82
83 @pytest.fixture(scope="session")
84 def base(preload):
85     return load_module(preload, "base_incomplete.json")
86
87
88 @pytest.fixture(scope="session")
89 def incremental(preload):
90     return load_module(preload, "incremental_incomplete.json")
91
92
93 def test_base_azs(base):
94     az = base["input"]["vnf-topology-information"]["vnf-assignments"][
95         "availability-zones"
96     ]
97     assert az == [
98         {"availability-zone": "VALUE FOR: availability_zone_0"},
99         {"availability-zone": "VALUE FOR: availability_zone_1"},
100     ]
101
102
103 def test_base_networks(base):
104     nets = base["input"]["vnf-topology-information"]["vnf-assignments"]["vnf-networks"]
105     assert nets == [
106         {
107             "network-role": "oam",
108             "network-name": "VALUE FOR: network name for oam_net_id",
109             "subnet-id": "oam_subnet_id",
110         },
111         {"network-role": "ha", "network-name": "VALUE FOR: network name for ha_net_id"},
112         {
113             "network-role": "ctrl",
114             "network-name": "VALUE FOR: network name for ctrl_net_id",
115             "subnet-id": "ctrl_subnet_id",
116         },
117     ]
118
119
120 def test_base_vm_types(base):
121     vms = base["input"]["vnf-topology-information"]["vnf-assignments"]["vnf-vms"]
122     vm_types = {vm["vm-type"] for vm in vms}
123     assert vm_types == {"db", "svc", "mgmt", "lb"}
124     db = first(vms, lambda v: v["vm-type"] == "db")
125     assert db == {
126         "vm-type": "db",
127         "vm-count": 2,
128         "vm-names": {"vm-name": ["VALUE FOR: db_name_0", "VALUE FOR: db_name_1"]},
129         "vm-networks": [
130             {
131                 "network-role": "oam",
132                 "network-role-tag": "oam",
133                 "ip-count": 2,
134                 "ip-count-ipv6": 0,
135                 "floating-ip": "",
136                 "floating-ip-v6": "",
137                 "network-ips": [
138                     {"ip-address": "VALUE FOR: db_oam_ip_0"},
139                     {"ip-address": "VALUE FOR: db_oam_ip_1"},
140                 ],
141                 "network-ips-v6": [],
142                 "network-macs": [],
143                 "interface-route-prefixes": [],
144                 "use-dhcp": "N",
145             },
146             {
147                 "network-role": "ha",
148                 "network-role-tag": "ha",
149                 "ip-count": 0,
150                 "ip-count-ipv6": 0,
151                 "floating-ip": "VALUE FOR: db_ha_floating_ip",
152                 "floating-ip-v6": "VALUE FOR: db_ha_floating_v6_ip",
153                 "network-ips": [],
154                 "network-ips-v6": [],
155                 "network-macs": [],
156                 "interface-route-prefixes": [],
157                 "use-dhcp": "N",
158             },
159         ],
160     }
161
162
163 def test_base_parameters(base):
164     params = base["input"]["vnf-topology-information"]["vnf-parameters"]
165     assert params == [
166         {"vnf-parameter-name": "svc_image_name", "vnf-parameter-value": "svc_image"},
167         {"vnf-parameter-name": "svc_flavor_name", "vnf-parameter-value": "svc_flavor"},
168     ]
169
170
171 def test_incremental(incremental):
172     az = incremental["input"]["vnf-topology-information"]["vnf-assignments"][
173         "availability-zones"
174     ]
175     assert isinstance(az, list)
176     assert len(az) == 1
177     assert az[0] == {"availability-zone": "VALUE FOR: availability_zone_0"}
178
179
180 def test_incremental_networks(incremental):
181     nets = incremental["input"]["vnf-topology-information"]["vnf-assignments"][
182         "vnf-networks"
183     ]
184     assert isinstance(nets, list)
185     assert len(nets) == 1
186     assert nets[0]["network-role"] == "ha"
187
188
189 def test_preload_env_population(preload):
190     base_path = THIS_DIR / "sample_env/preloads/vnfapi/base_incomplete.json"
191     data = load_json(base_path)
192     azs = data["input"]["vnf-topology-information"]["vnf-assignments"][
193         "availability-zones"
194     ]
195     assert azs == [{"availability-zone": "az0"}, {"availability-zone": "az1"}]