3 # ============LICENSE_START=======================================================
4 # ORAN SMO PACKAGE - PYTHONSDK TESTS
5 # ================================================================================
6 # Copyright (C) 2021-2022 AT&T Intellectual Property. All rights
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
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.
20 # ============LICENSE_END============================================
21 # ===================================================================
24 """Module called by pytest."""
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
40 # Set working dir as python script location
41 abspath = os.path.abspath(__file__)
42 dname = os.path.dirname(abspath)
45 logging.config.dictConfig(settings.LOG_CONFIG)
46 logger = logging.getLogger("Test Session setup")
48 network_sims = NetworkSimulators("./resources")
50 clamp = ClampToscaTemplate(settings.CLAMP_BASICAUTH)
55 def policy_component_ready():
56 """Check if components are ready."""
57 logger.info("Verify policy components are ready")
59 policy_ready = {"api_ready": False, "pap_ready": False, "apex_ready": False}
60 except (RequestException, NewConnectionError, ConnectionFailed, APIError) as e:
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"]
75 def sdnc_component_ready():
76 """Check if SDNC component is ready."""
77 logger.info("Verify sdnc component is ready")
80 response = OranSdnc.get_events(settings.SDNC_BASICAUTH, "test")
81 except (RequestException, NewConnectionError, ConnectionFailed, APIError) as e:
84 return response.status_code == 200
86 def clamp_component_ready():
87 """Check if Clamp component is ready."""
88 logger.info("Verify Clamp component is ready")
90 response = clamp.get_template_instance()
91 except (RequestException, NewConnectionError, ConnectionFailed, APIError) as e:
94 return response["automationCompositionList"] is not None
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")
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")