[VVP] Generated completed preload from env files
[vvp/validation-scripts.git] / ice_validator / app_tests / preload_tests / test_environment.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 from pathlib import Path
38
39 import pytest
40
41 from preload.environment import CloudServiceArchive, PreloadEnvironment
42
43 THIS_DIR = Path(__file__).parent
44 PRELOAD_ENV_DIR = THIS_DIR / "preload_envs"
45
46
47 @pytest.fixture(scope="session")
48 def csar():
49     return CloudServiceArchive(PRELOAD_ENV_DIR / "test.csar")
50
51
52 @pytest.fixture(scope="session")
53 def env():
54     return PreloadEnvironment(PRELOAD_ENV_DIR)
55
56
57 def test_csar_service_name(csar):
58     assert csar.service_name == "stark_vccf_svc"
59
60
61 def test_csar_str_and_repr(csar):
62     assert str(csar) == "CSAR (path=test.csar, name=stark_vccf_svc)"
63     assert repr(csar) == "CSAR (path=test.csar, name=stark_vccf_svc)"
64
65
66 def test_csar_vf_module_model_name(csar):
67     assert (
68         csar.get_vf_module_model_name("base_vIECCF")
69         == "StarkVccfVf..base_vIECCF..module-0"
70     )
71
72
73 def test_csar_get_vf_module_resource_name(csar):
74     assert csar.get_vf_module_resource_name("base_vIECCF") == "stark_vccf_vf"
75
76
77 def test_csar_get_vf_module_resource_name_not_found(csar):
78     assert csar.get_vf_module_resource_name("unknown") is None
79
80
81 def test_preload_environment_global_csar(env):
82     assert env.csar.service_name == "stark_vccf_svc"
83
84
85 def test_preload_environment_nest_env_csar_inherit(env):
86     env_two = env.get_environment("env_two")
87     assert env_two.csar.service_name == "stark_vccf_svc"
88
89
90 def test_preload_environment_nest_env_csar_override(env):
91     sub_env = env.get_environment("env_three")
92     assert sub_env.csar.service_name == "StarkMultiModule2_43550"
93
94
95 def test_preload_environment_environments(env):
96     names = {e.name for e in env.environments}
97     assert names == {"env_two", "env_three", "env_one_a"}
98
99
100 def test_preload_environment_environments_nested(env):
101     env_one = env.get_environment("env_one")
102     names = {e.name for e in env_one.environments}
103     assert names == {"env_one_a"}
104
105
106 def test_preload_environment_get_module_global_base(env):
107     module = env.get_module("base")
108     assert module["my_ip"] == "default"
109
110
111 def test_preload_environment_get_module_global_not_found(env):
112     module = env.get_module("unknown")
113     assert module == {}
114
115
116 def test_preload_environment_get_module_sub_env(env):
117     env_two = env.get_environment("env_two")
118     module = env_two.get_module("base")
119     assert module["my_ip"] == "192.168.0.2"
120     assert module["common"] == "ABC"
121
122
123 def test_preload_environment_module_names(env):
124     expected = {"base.env", "incremental.env"}
125     assert env.module_names == expected
126     # check a nested env with inherits all modules
127     assert env.get_environment("env_three").module_names == expected
128
129
130 def test_preload_environment_modules(env):
131     modules = env.modules
132     assert isinstance(modules, dict)
133     assert modules.keys() == {"base.env", "incremental.env"}
134     assert all(isinstance(val, dict) for val in modules.values())
135
136
137 def test_preload_environment_is_base(env):
138     assert env.is_base
139     assert not env.get_environment("env_one").is_base
140
141
142 def test_preload_environment_is_leaf(env):
143     assert not env.is_leaf
144     assert env.get_environment("env_two").is_leaf
145     assert not env.get_environment("env_one").is_leaf
146     assert env.get_environment("env_one_a").is_leaf
147
148
149 def test_preload_environment_str_repr(env):
150     assert str(env) == "PreloadEnvironment(name=preload_envs)"
151     assert repr(env) == "PreloadEnvironment(name=preload_envs)"
152
153
154 def test_preload_environment_defaults(env):
155     expected = {"availability_zone_0": "az0"}
156     assert env.defaults == expected
157     assert env.get_environment("env_one_a").defaults == expected
158
159
160 def test_preload_environment_defaults_merging_and_override(env):
161     assert env.get_environment("env_three").defaults == {
162         "availability_zone_0": "az0-b",
163         "custom_env_3": "default",
164     }
165
166
167 def test_preload_environment_defaults_in_module_env(env):
168     mod = env.get_environment("env_three").get_module("base")
169     assert mod == {
170         "availability_zone_0": "az0-b",
171         "common": "ABC",
172         "custom_env_3": "default",
173         "my_ip": "default",
174     }
175     mod = env.get_environment("env_one").get_module("base")
176     assert mod == {
177         "availability_zone_0": "az0",
178         "common": "ABC",
179         "my_ip": "192.168.0.1",
180     }