CI: Make a generic function for (un)deployment with script

Add simple unit tests for the new functions.

Signed-off-by: Jaroslava Fiedlerova <jaroslava.fiedlerova@openairinterface.org>
This commit is contained in:
Jaroslava Fiedlerova
2025-09-09 20:24:54 +02:00
committed by Jaroslava Fiedlerova
parent cb0e501293
commit c02c3daad9
9 changed files with 182 additions and 0 deletions

View File

@@ -276,6 +276,38 @@ def Deploy_Physim(ctx, HTML, node, workdir, script, options):
logging.error('\u001B[1m Physical Simulator Fail\u001B[0m')
return test_status
def DeployWithScript(HTML, node, script, options, tag):
logging.debug(f'Deploy with script {script} on node: {node}')
opt = options.replace('%%image_tag%%', tag)
with cls_cmd.getConnection(node) as c:
ret = c.exec_script(script, 600, opt)
logging.debug(f'"{script}" finished with code {ret.returncode}, output:\n{ret.stdout}')
HTML.CreateHtmlTestRowQueue(f'on node {node}', 'OK' if ret.returncode == 0 else 'KO', [f'{ret.stdout}'])
return ret.returncode == 0
def UndeployWithScript(HTML, ctx, node, script, options):
logging.debug(f'Undeploy with script {script} on node: {node}')
remote_dir = '/tmp/undeploy'
opt = options.replace('%%log_dir%%', remote_dir)
with cls_cmd.getConnection(node) as c:
# create a directory for log collection
c.run(f'rm -rf {remote_dir}')
ret = c.run(f'mkdir {remote_dir}')
if ret.returncode != 0:
logging.error("cannot create directory for log collection")
return False
ret = c.exec_script(script, 600, opt)
logging.debug(f'"{script}" finished with code {ret.returncode}, output:\n{ret.stdout}')
ret_ls = c.run(f'ls -1 {remote_dir}')
files = ret_ls.stdout.strip().splitlines()
log_files = []
for lf in files:
name = archiveArtifact(c, ctx, f'{remote_dir}/{lf}')
log_files.append(name)
msg = "Log files:\n" + "\n".join([os.path.basename(lf) for lf in log_files])
HTML.CreateHtmlTestRowQueue(f'on node {node}', 'OK' if ret.returncode == 0 else 'KO', [f'{ret.stdout}\n\n{msg}'])
return ret.returncode == 0
#-----------------------------------------------------------
# OaiCiTest Class Definition
#-----------------------------------------------------------

View File

@@ -165,6 +165,15 @@ def ExecuteActionWithParam(action, ctx, node):
core_op = getattr(cls_oaicitest.OaiCiTest, action)
success = core_op(cn_id, ctx, HTML)
elif action == 'DeployWithScript' or action == 'UndeployWithScript':
script = test.findtext('script')
options = test.findtext('options')
if action == 'DeployWithScript':
deploymentTag = RAN.branch
success = cls_oaicitest.DeployWithScript(HTML, node, script, options, deploymentTag)
elif action == 'UndeployWithScript':
success = cls_oaicitest.UndeployWithScript(HTML, ctx, node, script, options)
elif action == 'Deploy_Object' or action == 'Undeploy_Object' or action == "Create_Workspace" or action == "Stop_Object":
CONTAINERS.yamlPath = test.findtext('yaml_path')
CONTAINERS.services = test.findtext('services')

View File

@@ -0,0 +1,22 @@
#!/bin/bash
# SPDX-License-Identifier: MIT
export PS4='[\D{%Y-%m-%d %H:%M:%S}] '
function die() { echo $@; exit 1; }
[ $# -eq 3 ] || die "usage: $0 <path-to-dir> <namespace> <image-tag>"
OC_NS=${2}
IMAGE_TAG=${3}
OC_DIR=${1}
OC_RELEASE=$(basename "${1}")
cat /opt/oc-password | oc login -u oaicicd --server https://api.oai.cs.eurecom.fr:6443 > /dev/null
oc project ${OC_NS} > /dev/null
set -x
helm install --wait --timeout 120s ${OC_RELEASE} --set nfimage.version=${IMAGE_TAG} --hide-notes ${OC_DIR}/.
status=$?
set +x
oc logout > /dev/null
[ $status -eq 0 ] || die "OC chart deployment failed"

View File

@@ -0,0 +1,26 @@
#!/bin/bash
# SPDX-License-Identifier: MIT
function die() { echo $@; exit 1; }
[ $# -eq 3 ] || die "usage: $0 <path-to-dir> <namespace> <log-dir>"
export PS4='[\D{%Y-%m-%d %H:%M:%S}] '
OC_DIR=${1}
OC_NS=${2}
LOG_DIR=${3}
OC_RELEASE=$(basename "${OC_DIR}")
cat ${OC_DIR}/oc-password | oc login -u oaicicd --server https://api.oai.cs.eurecom.fr:6443 > /dev/null
oc project ${OC_NS} > /dev/null
oc describe pod > ${LOG_DIR}/describe-pods-post-test.log
oc get pods.metrics.k8s &> ${LOG_DIR}/nf-resource-consumption.log
oc logs -l app.kubernetes.io/name=${OC_RELEASE} --tail=-1 > ${LOG_DIR}/${OC_RELEASE}.log
set -x
helm uninstall ${OC_RELEASE} --wait
status=$?
set +x
oc logout > /dev/null
[ $status -eq 0 ] || die "OC chart undeployment failed"

View File

@@ -21,6 +21,7 @@ To run individual unit tests, start them like so:
python tests/log-analysis.py -v
python tests/ping-iperf.py -v
python tests/pull-clean-int-registry.py -v
python tests/script-deployment.py -v
The logs will indicate if all tests passed. `tests/deployment.py` requires
these images to be present:

View File

@@ -0,0 +1,65 @@
# SPDX-License-Identifier: LicenseRef-CSSL-1.0
import sys
import logging
logging.basicConfig(
level=logging.DEBUG,
stream=sys.stdout,
format="[%(asctime)s] %(levelname)8s: %(message)s"
)
import os
import tempfile
import unittest
sys.path.append('./') # to find OAI imports below
import cls_oaicitest
import cls_oai_html
import cls_cmd
from cls_ci_helper import TestCaseCtx
class TestScriptDeployment(unittest.TestCase):
def setUp(self):
self.tmpdir = tempfile.mkdtemp()
self.html = cls_oai_html.HTMLManagement()
self.node = 'localhost'
self.tag = 'develop-12345678'
self.ctx = TestCaseCtx.Default(self.tmpdir)
def tearDown(self):
with cls_cmd.LocalCmd() as c:
c.run(f'rm -rf {self.ctx.logPath}')
def test_simple_deployment(self):
script = 'tests/scripts/deploy-with-script.sh'
options = 'WILL PASS'
success = cls_oaicitest.DeployWithScript(self.html, self.node, script, options, self.tag)
self.assertTrue(success)
def test_simple_deployment_fail(self):
script = 'tests/scripts/deploy-with-script.sh'
options = 'WILLFAIL'
success = cls_oaicitest.DeployWithScript(self.html, self.node, script, options, self.tag)
self.assertFalse(success)
def test_simple_undeployment(self):
script = 'tests/scripts/undeploy-with-script.sh'
options = '%%log_dir%% WILL PASS'
success = cls_oaicitest.UndeployWithScript(self.html, self.ctx, self.node, script, options)
self.assertTrue(success)
# verify logs were created
files = os.listdir(self.tmpdir)
self.assertIn('112233-log1.txt', files)
self.assertIn('112233-log2.txt', files)
def test_simple_undeployment_fail(self):
script = 'tests/scripts/undeploy-with-script.sh'
options = '%%log_dir%% WILLFAILL'
success = cls_oaicitest.UndeployWithScript(self.html, self.ctx, self.node, script, options)
self.assertFalse(success)
# verify logs were created
files = os.listdir(self.tmpdir)
self.assertIn('112233-log1.txt', files)
self.assertIn('112233-log2.txt', files)
if __name__ == '__main__':
unittest.main()

View File

@@ -0,0 +1,10 @@
#!/bin/bash
# SPDX-License-Identifier: MIT
function die() { echo $@; exit 1; }
echo "Deploy script executed with options: $@"
[ $# -lt 2 ] && die "failing"
exit 0

View File

@@ -0,0 +1,15 @@
#!/bin/bash
# SPDX-License-Identifier: MIT
function die() { echo $@; exit 1; }
echo "Undeploy script executed with options: $@"
LOG_DIR="$1"
mkdir -p "$LOG_DIR"
echo "Undeployment started" > "$LOG_DIR/log1.txt"
echo "Undeployment finished successfully" > "$LOG_DIR/log2.txt"
[ $# -lt 3 ] && die "failing"
exit 0

View File

@@ -33,3 +33,5 @@
- Create_Workspace
- AnalyzeRTStats
- AnalyzeRTStats_Object
- DeployWithScript
- UndeployWithScript