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============================================
37 from pathlib import Path
42 from preload.environment import CloudServiceArchive, PreloadEnvironment
44 THIS_DIR = Path(__file__).parent
45 PRELOAD_ENV_DIR = THIS_DIR / "preload_envs"
48 @pytest.fixture(scope="session")
50 return CloudServiceArchive(PRELOAD_ENV_DIR / "test.csar")
53 @pytest.fixture(scope="session")
55 return PreloadEnvironment(PRELOAD_ENV_DIR)
58 def test_csar_service_name(csar):
59 assert csar.service_name == "stark_0917_vlb_svc"
62 def test_csar_str_and_repr(csar):
63 assert str(csar) == "CSAR (path=test.csar, name=stark_0917_vlb_svc)"
64 assert repr(csar) == "CSAR (path=test.csar, name=stark_0917_vlb_svc)"
67 def test_csar_vf_module_model_name(csar):
68 assert csar.get_vf_module_model_name("vdns") == "Stark0917VlbVf..vdns..module-3"
71 def test_csar_get_vf_module_resource_name(csar):
72 assert csar.get_vf_module_resource_name("vdns") == "stark_0917_vlb_vf"
75 def test_csar_get_vnf_type(csar):
76 assert csar.get_vnf_type("vdns") == "stark_0917_vlb_svc/stark_0917_vlb_vf 0"
79 def test_csar_get_vf_module_resource_name_not_found(csar):
80 assert csar.get_vf_module_resource_name("unknown") is None
83 def test_preload_environment_global_csar(env):
84 assert env.csar.service_name == "stark_0917_vlb_svc"
87 def test_preload_environment_nest_env_csar_inherit(env):
88 env_two = env.get_environment("env_two")
89 assert env_two.csar.service_name == "stark_0917_vlb_svc"
92 def test_preload_environment_nest_env_csar_override(env):
93 sub_env = env.get_environment("env_three")
94 assert sub_env.csar.service_name == "StarkMultiModule2_43550"
97 def test_preload_environment_environments(env):
98 names = {e.name for e in env.environments}
99 assert names == {"env_two", "env_three", "env_one_a"}
102 def test_preload_environment_environments_nested(env):
103 env_one = env.get_environment("env_one")
104 names = {e.name for e in env_one.environments}
105 assert names == {"env_one_a"}
108 def test_preload_environment_get_module_global_base(env):
109 module = env.get_module("base")
110 assert module["my_ip"] == "default"
113 def test_preload_environment_get_module_global_not_found(env):
114 module = env.get_module("unknown")
118 def test_preload_environment_get_module_sub_env(env):
119 env_two = env.get_environment("env_two")
120 module = env_two.get_module("base")
121 assert module["my_ip"] == "192.168.0.2"
122 assert module["common"] == "ABC"
125 def test_preload_environment_module_names(env):
126 expected = {"base.env", "incremental.env"}
127 assert env.module_names == expected
128 # check a nested env with inherits all modules
129 assert env.get_environment("env_three").module_names == expected
132 def test_preload_environment_modules(env):
133 modules = env.modules
134 assert isinstance(modules, dict)
135 assert modules.keys() == {"base.env", "incremental.env"}
136 assert all(isinstance(val, dict) for val in modules.values())
139 def test_preload_environment_is_base(env):
141 assert not env.get_environment("env_one").is_base
144 def test_preload_environment_is_leaf(env):
145 assert not env.is_leaf
146 assert env.get_environment("env_two").is_leaf
147 assert not env.get_environment("env_one").is_leaf
148 assert env.get_environment("env_one_a").is_leaf
151 def test_preload_environment_str_repr(env):
152 assert str(env) == "PreloadEnvironment(name=preload_envs)"
153 assert repr(env) == "PreloadEnvironment(name=preload_envs)"
156 def test_preload_environment_defaults(env):
157 expected = {"availability_zone_0": "az0"}
158 assert env.defaults == expected
159 assert env.get_environment("env_one_a").defaults == expected
162 def test_preload_environment_defaults_merging_and_override(env):
163 assert env.get_environment("env_three").defaults == {
164 "availability_zone_0": "az0-b",
165 "custom_env_3": "default",
169 def test_preload_environment_defaults_in_module_env(env):
170 mod = env.get_environment("env_three").get_module("base")
172 "availability_zone_0": "az0-b",
174 "custom_env_3": "default",
176 "vf-module-model-name": "Starkmultimodule2A9463fd82915..base..module-0",
177 "vnf-type": "StarkMultiModule2_43550/StarkMultiModule2 a9463fd8-2915 0",
179 mod = env.get_environment("env_one").get_module("base")
181 "availability_zone_0": "az0",
183 "my_ip": "192.168.0.1",
187 def test_preload_environment_uses_csar(env, monkeypatch):
188 csar = mock.MagicMock(spec=CloudServiceArchive)
189 csar.get_vnf_type = mock.Mock(return_value="stark_vccf_svc/stark_vccf_vf")
190 csar.get_vf_module_model_name = mock.Mock(return_value="model_name")
191 env = env.get_environment("env_three")
192 monkeypatch.setattr(env, "csar", csar)
193 mod = env.get_module("base")
194 assert mod["vnf-type"] == "stark_vccf_svc/stark_vccf_vf"
195 assert mod["vf-module-model-name"] == "model_name"