Fix/add use cases under SMO package
[it/dep.git] / smo-install / test / pythonsdk / src / orantests / oran_tests / 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 requests import RequestException
29 from onapsdk.configuration import settings
30 from onapsdk.exceptions import ConnectionFailed, APIError
31 from waiting import wait
32 from urllib3.exceptions import NewConnectionError
33 from oransdk.dmaap.dmaap import OranDmaap
34 from oransdk.policy.clamp import ClampToscaTemplate
35 from oransdk.policy.policy import OranPolicy
36 from oransdk.sdnc.sdnc import OranSdnc
37 from smo.smo import Smo
38 from smo.network_simulators import NetworkSimulators
39
40 # Set working dir as python script location
41 abspath = os.path.abspath(__file__)
42 dname = os.path.dirname(abspath)
43 os.chdir(dname)
44
45 logging.config.dictConfig(settings.LOG_CONFIG)
46 logger = logging.getLogger("Test Session setup")
47
48 network_sims = NetworkSimulators("./resources")
49 smo = Smo()
50 clamp = ClampToscaTemplate(settings.CLAMP_BASICAUTH)
51 dmaap = OranDmaap()
52 sdnc = OranSdnc()
53 policy = OranPolicy()
54
55 def policy_component_ready():
56     """Check if components are ready."""
57     logger.info("Verify policy components are ready")
58     try:
59         policy_ready = {"api_ready": False, "pap_ready": False, "apex_ready": False}
60     except (RequestException, NewConnectionError, ConnectionFailed, APIError) as e:
61         logger.error(e)
62         return False
63     policy_status = policy.get_components_status(settings.POLICY_BASICAUTH)
64     if (policy_status["api"]["healthy"] and not policy_ready["api_ready"]):
65         logger.info("Policy Api is ready")
66         policy_ready["api_ready"] = True
67     if (policy_status["pap"]["healthy"] and not policy_ready["pap_ready"]):
68         logger.info("Policy Pap is ready")
69         policy_ready["pap_ready"] = True
70     if (len(policy_status["pdps"]["apex"]) > 0 and policy_status["pdps"]["apex"][0]["healthy"] == "HEALTHY" and not policy_ready["apex_ready"]):
71         logger.info("Policy Apex is ready")
72         policy_ready["apex_ready"] = True
73     return policy_ready["api_ready"] and policy_ready["pap_ready"] and policy_ready["apex_ready"]
74
75 def sdnc_component_ready():
76     """Check if SDNC component is ready."""
77     logger.info("Verify sdnc component is ready")
78
79     try:
80         response = OranSdnc.get_events(settings.SDNC_BASICAUTH, "test")
81     except (RequestException, NewConnectionError, ConnectionFailed, APIError) as e:
82         logger.error(e)
83         return False
84     return response.status_code == 200
85
86 def clamp_component_ready():
87     """Check if Clamp component is ready."""
88     logger.info("Verify Clamp component is ready")
89     try:
90         response = clamp.get_template_instance()
91     except (RequestException, NewConnectionError, ConnectionFailed, APIError) as e:
92         logger.error(e)
93         return False
94     return response["automationCompositionList"] is not None
95
96 ###### Entry points of PYTEST Session
97 def pytest_sessionstart():
98     """Pytest calls it when starting a test session."""
99     logger.info("Check and wait for SMO to be running")
100     smo.wait_for_smo_to_be_running()
101     logger.info("Check and for for SDNC to be running")
102     wait(lambda: policy_component_ready(), sleep_seconds=settings.POLICY_CHECK_RETRY, timeout_seconds=settings.POLICY_CHECK_TIMEOUT, waiting_for="Policy to be ready")
103     wait(lambda: sdnc_component_ready(), sleep_seconds=settings.SDNC_CHECK_RETRY, timeout_seconds=settings.SDNC_CHECK_TIMEOUT, waiting_for="SDNC to be ready")
104     # disable for now, until policy/clamp issue has been fixed
105     wait(lambda: clamp_component_ready(), sleep_seconds=settings.CLAMP_CHECK_RETRY, timeout_seconds=settings.CLAMP_CHECK_TIMEOUT, waiting_for="Clamp to be ready")
106
107     ## Just kill any simulators that could already be runnin
108     network_sims.stop_network_simulators()
109     ###### END of FIRST start, now we can start the sims for the real tests.
110     logger.info("Tests session setup is ready")