217c7d0723f639e75def7aa598e2de1765109794
[it/dep.git] / smo-install / test / pythonsdk / src / orantests / conftest.py
1 #!/usr/bin/env python3
2 ###
3 # ============LICENSE_START=======================================================
4 # ORAN SMO PACKAGE - PYTHONSDK TESTS
5 # ================================================================================
6 # Copyright (C) 2021-2022 AT&T Intellectual Property. All rights
7 #                             reserved.
8 # ================================================================================
9 # Licensed under the Apache License, Version 2.0 (the "License");
10 # you may not use this file 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 # ============LICENSE_END============================================
21 # ===================================================================
22 #
23 ###
24 """Module called by pytest."""
25 import logging
26 import logging.config
27 import os
28 from oransdk.dmaap.dmaap import OranDmaap
29 from oransdk.policy.policy import OranPolicy, PolicyType
30 from oransdk.sdnc.sdnc import OranSdnc
31 from onapsdk.configuration import settings
32 from waiting import wait
33 from smo.smo import Smo
34 from smo.network_simulators import NetworkSimulators
35
36 # Set working dir as python script location
37 abspath = os.path.abspath(__file__)
38 dname = os.path.dirname(abspath)
39 os.chdir(dname)
40
41 logging.config.dictConfig(settings.LOG_CONFIG)
42 logger = logging.getLogger("Test Session setup")
43
44 network_sims = NetworkSimulators("./resources")
45 smo = Smo()
46 dmaap = OranDmaap()
47 sdnc = OranSdnc()
48 policy = OranPolicy()
49
50 def policy_component_ready():
51     """Check if components are ready."""
52     logger.info("Verify policy components are ready")
53     policy_ready = {"api_ready": False, "pap_ready": False, "apex_ready": False}
54     policy_status = policy.get_components_status(settings.POLICY_BASICAUTH)
55     if (policy_status["api"]["healthy"] and not policy_ready["api_ready"]):
56         logger.info("Policy Api is ready")
57         policy_ready["api_ready"] = True
58     if (policy_status["pap"]["healthy"] and not policy_ready["pap_ready"]):
59         logger.info("Policy Pap is ready")
60         policy_ready["pap_ready"] = True
61     if (policy_status["pdps"]["apex"][0]["healthy"] == "HEALTHY" and not policy_ready["apex_ready"]):
62         logger.info("Policy Apex is ready")
63         policy_ready["apex_ready"] = True
64     return policy_ready["api_ready"] and policy_ready["pap_ready"] and policy_ready["apex_ready"]  
65
66
67 ###### Entry points of PYTEST Session
68 def pytest_sessionstart():
69     """Pytest calls it when starting a test session."""
70     logger.info("Check and wait for SMO to be running")
71     smo.wait_for_smo_to_be_running()
72     logger.info("Check and for for SDNC to be running")
73     wait(lambda: OranSdnc.get_events(settings.SDNC_BASICAUTH, "test").status_code == 200, sleep_seconds=10, timeout_seconds=300, waiting_for="SDNC to be ready")
74     wait(lambda: policy_component_ready(), sleep_seconds=10, timeout_seconds=300, waiting_for="Policy to be ready")
75     ## Just kill any simulators that could already be runnin
76     network_sims.stop_network_simulators()
77     ###### END of FIRST start, now we can start the sims for the real tests.
78     logger.info("Tests session setup is ready")