Add Onap Jakarta support
[it/dep.git] / smo-install / test / pythonsdk / src / orantests / disable_test_cl_k8s.py
1 #!/usr/bin/env python3
2 ###
3 # ============LICENSE_START=======================================================
4 # ORAN SMO PACKAGE - PYTHONSDK TESTS
5 # ================================================================================
6 # Copyright (C) 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 """Closed Loop Apex usecase tests module."""
25 # This usecase has limitations due to Clamp issue.
26 # 1. make sure using the policy-clamp-be version 6.2.0-snapshot-latest at this the moment
27 import time
28 import logging.config
29 import subprocess
30 import os
31 from subprocess import check_output
32 import pytest
33 from waiting import wait
34 from onapsdk.configuration import settings
35 from oransdk.utils.jinja import jinja_env
36 from oransdk.policy.clamp import ClampToscaTemplate
37 from smo.cl_usecase import ClCommissioningUtils
38
39 # Set working dir as python script location
40 abspath = os.path.abspath(__file__)
41 dname = os.path.dirname(abspath)
42 os.chdir(dname)
43
44 logging.config.dictConfig(settings.LOG_CONFIG)
45 logger = logging.getLogger("test Control Loops for O-RU Fronthaul Recovery usecase - Clamp K8S usecase")
46 clcommissioning_utils = ClCommissioningUtils()
47 clamp = ClampToscaTemplate(settings.CLAMP_BASICAUTH)
48
49 chartmuseum_ip = subprocess.run("kubectl get services -n test | grep test-chartmuseum | awk '{print $3}'", shell=True, check=True, stdout=subprocess.PIPE).stdout.decode('utf-8').strip()+":8080"
50 chartmuseum_port = "8080"
51 chart_version = "1.0.0"
52 chart_name = "oru-app"
53 release_name = "oru-app"
54
55 @pytest.fixture(scope="module", autouse=True)
56 def setup_simulators():
57     """Prepare the test environment before the executing the tests."""
58     logger.info("Test class setup for Closed Loop tests")
59
60     deploy_chartmuseum()
61
62     # Add the remote repo to Clamp k8s pod
63     logger.info("Add the remote repo to Clamp k8s pod")
64     k8s_pod = subprocess.run("kubectl get pods -n onap | grep k8s | awk '{print $1}'", shell=True, check=True, stdout=subprocess.PIPE).stdout.decode('utf-8').strip()
65
66     repo_url = subprocess.run("kubectl get services -n test | grep test-chartmuseum | awk '{print $3}'", shell=True, check=True, stdout=subprocess.PIPE).stdout.decode('utf-8').strip()+":8080"
67     logger.info("k8s: %s, repo_url:%s", k8s_pod, repo_url)
68     cmd = f"kubectl exec -it -n onap {k8s_pod} -- sh -c \"helm repo add chartmuseum http://{repo_url}\""
69     check_output(cmd, shell=True).decode('utf-8')
70     cmd = f"kubectl exec -it -n onap {k8s_pod} -- sh -c \"helm repo update\""
71     check_output(cmd, shell=True).decode('utf-8')
72     cmd = f"kubectl exec -it -n onap {k8s_pod} -- sh -c \"helm search repo -l oru-app\""
73     result = check_output(cmd, shell=True).decode('utf-8')
74     if result == '':
75         logger.info("Failed to update the K8s pod repo")
76     logger.info("Test Session setup completed successfully")
77
78     ### Cleanup code
79     yield
80     # Finish and delete the cl instance
81     clcommissioning_utils.clean_instance()
82     wait(lambda: is_oru_app_down(), sleep_seconds=5, timeout_seconds=60, waiting_for="Oru app is down")
83     # Remove the remote repo to Clamp k8s pod
84     cmd = f"kubectl exec -it -n onap {k8s_pod} -- sh -c \"helm repo remove chartmuseum\""
85     check_output(cmd, shell=True).decode('utf-8')
86     cmd = "kubectl delete namespace test"
87     check_output(cmd, shell=True).decode('utf-8')
88     cmd = "helm repo remove test"
89     check_output(cmd, shell=True).decode('utf-8')
90     time.sleep(10)
91     logger.info("Test Session cleanup done")
92
93
94 def deploy_chartmuseum():
95     """Start chartmuseum pod and populate with the nedded helm chart."""
96     logger.info("Start to deploy chartmuseum")
97     cmd = "helm repo add test https://chartmuseum.github.io/charts"
98     check_output(cmd, shell=True).decode('utf-8')
99     cmd = "kubectl create namespace test"
100     check_output(cmd, shell=True).decode('utf-8')
101
102     cmd = "helm install test test/chartmuseum --version 3.1.0 --namespace test --set env.open.DISABLE_API=false"
103     check_output(cmd, shell=True).decode('utf-8')
104     wait(lambda: is_chartmuseum_up(), sleep_seconds=10, timeout_seconds=60, waiting_for="chartmuseum to be ready")
105
106     chartmuseum_url = subprocess.run("kubectl get services -n test | grep test-chartmuseum | awk '{print $3}'", shell=True, check=True, stdout=subprocess.PIPE).stdout.decode('utf-8').strip()+":8080"
107     cmd = f"curl --noproxy '*' -X POST --data-binary @{dname}/resources/cl-test-helm-chart/oru-app-1.0.0.tgz http://{chartmuseum_url}/api/charts"
108     check_output(cmd, shell=True).decode('utf-8')
109
110
111 def is_chartmuseum_up() -> bool:
112     """Check if the chartmuseum is up."""
113     cmd = "kubectl get pods --field-selector status.phase=Running -n test"
114     result = check_output(cmd, shell=True).decode('utf-8')
115     logger.info("Checking if chartmuseum is UP: %s", result)
116     if result == '':
117         logger.info("chartmuseum is Down")
118         return False
119     logger.info("chartmuseum is Up")
120     return True
121
122
123 def is_oru_app_up() -> bool:
124     """Check if the oru-app is up."""
125     cmd = "kubectl get pods -n nonrtric | grep oru-app | wc -l"
126     result = check_output(cmd, shell=True).decode('utf-8')
127     logger.info("Checking if oru-app is up :%s", result)
128     if int(result) == 1:
129         logger.info("ORU-APP is Up")
130         return True
131     logger.info("ORU-APP is Down")
132     return False
133
134 def is_oru_app_down() -> bool:
135     """Check if the oru-app is down."""
136     cmd = "kubectl get pods -n nonrtric | grep oru-app | wc -l"
137     result = check_output(cmd, shell=True).decode('utf-8')
138     logger.info("Checking if oru-app is down :%s", result)
139     if int(result) == 0:
140         logger.info("ORU-APP is Down")
141         return True
142     logger.info("ORU-APP is Up")
143     return False
144
145 def test_cl_oru_app_deploy():
146     """The Closed Loop O-RU Fronthaul Recovery usecase Apex version."""
147     logger.info("Upload tosca to commissioning")
148     tosca_template = jinja_env().get_template("commission_k8s.json.j2").render(chartmuseumIp=chartmuseum_ip, chartmuseumPort=chartmuseum_port, chartVersion=chart_version, chartName=chart_name, releaseName=release_name)
149     assert clcommissioning_utils.create_instance(tosca_template) is True
150
151     logger.info("Check if oru-app is up")
152     wait(lambda: is_oru_app_up(), sleep_seconds=5, timeout_seconds=60, waiting_for="Oru app to be up")