From 42a217228b452f2928c586a8489159517f453009 Mon Sep 17 00:00:00 2001
From: Raphael Defosseux <raphael.defosseux@eurecom.fr>
Date: Tue, 27 Apr 2021 15:19:49 +0200
Subject: [PATCH] CI: fixes for OpenShift deployment of physical-simulators  
 -- Missing finished tag in one deployment   -- Deep copy of the array of pod
 names   -- Hack for the out-of-sync on pexpect for `oc logs` and `oc get
 pods` commands

Signed-off-by: Raphael Defosseux <raphael.defosseux@eurecom.fr>
---
 .../nr-dlschsim/templates/deployment.yaml       |  1 +
 ci-scripts/cls_physim1.py                       | 17 +++++++++--------
 ci-scripts/sshconnection.py                     | 15 ++++++++++++---
 3 files changed, 22 insertions(+), 11 deletions(-)

diff --git a/charts/physims/charts/nr-dlschsim/templates/deployment.yaml b/charts/physims/charts/nr-dlschsim/templates/deployment.yaml
index 23c3699c25..06f01a0f0b 100644
--- a/charts/physims/charts/nr-dlschsim/templates/deployment.yaml
+++ b/charts/physims/charts/nr-dlschsim/templates/deployment.yaml
@@ -40,6 +40,7 @@ spec:
           export OPENAIR_DIR=/opt/oai-physim &&
           cd cmake_targets/autotests &&
           ./run_exec_autotests.bash -g "015106" -q -np -b &&
+          echo "FINISHED" &&
           sleep infinity
       dnsPolicy: ClusterFirst
       restartPolicy: Always
diff --git a/ci-scripts/cls_physim1.py b/ci-scripts/cls_physim1.py
index 9386ed03e1..5cb36629e7 100644
--- a/ci-scripts/cls_physim1.py
+++ b/ci-scripts/cls_physim1.py
@@ -189,7 +189,7 @@ class PhySim:
 			if mySSH.getBefore().count('Running') == 12:
 				logging.debug('\u001B[1m Running the physim test Scenarios\u001B[0m')
 				isRunning = True
-				podNames = re.findall('oai[\S\d\w]+', mySSH.getBefore())
+				podNames = re.findall('oai-[\S\d\w]+', mySSH.getBefore())
 			count +=1
 		if isRunning == False:
 			logging.error('\u001B[1m Some PODS Running FAILED \u001B[0m')
@@ -200,14 +200,15 @@ class PhySim:
 		# Waiting to complete the running test
 		count = 0
 		isFinished = False
-		tmpPodNames = podNames
-		while(count < 25 and isFinished == False):
-			time.sleep(58)
+		# doing a deep copy!
+		tmpPodNames = podNames.copy()
+		while(count < 28 and isFinished == False):
+			time.sleep(60)
 			for podName in tmpPodNames:
 				mySSH.command(f'oc logs --tail=1 {podName} 2>&1', '\$', 6)
-				if mySSH.getBefore().count('Finished') != 0:
+				if mySSH.getBefore().count('FINISHED') != 0:
+					logging.debug(podName + ' is finished')
 					tmpPodNames.remove(podName)
-				time.sleep(2)
 			if not tmpPodNames:
 				isFinished = True
 			count += 1
@@ -216,14 +217,14 @@ class PhySim:
         
 		# Getting the logs of each executables running in individual pods
 		for podName in podNames:
-			mySSH.command(f'oc logs {podName} >> cmake_targets/log/physim_test.txt 2>&1', '\$', 6)
+			mySSH.command(f'oc logs {podName} >> cmake_targets/log/physim_test.txt 2>&1', '\$', 15)
 		time.sleep(30)
 
 		# UnDeploy the physical simulator pods
 		mySSH.command('helm uninstall physim | tee -a cmake_targets/log/physim_helm_summary.txt 2>&1', '\$', 6)
 		isFinished1 = False
 		while(isFinished1 == False):
-			time.sleep(10)
+			time.sleep(20)
 			mySSH.command('oc get pods -l app.kubernetes.io/instance=physim', '\$', 6)
 			if re.search('No resources found', mySSH.getBefore()):
 				isFinished1 = True
diff --git a/ci-scripts/sshconnection.py b/ci-scripts/sshconnection.py
index 5bfe5da70f..087e920fe1 100644
--- a/ci-scripts/sshconnection.py
+++ b/ci-scripts/sshconnection.py
@@ -112,10 +112,19 @@ class SSHConnection():
 		return self.sshresponse
 
 	def command(self, commandline, expectedline, timeout):
-		logging.debug(commandline)
+		if commandline.count('oc logs') == 0:
+			logging.debug(commandline)
 		self.ssh.timeout = timeout
-		self.ssh.sendline(commandline)
-		self.sshresponse = self.ssh.expect([expectedline, pexpect.EOF, pexpect.TIMEOUT])
+		# Nasty patch when pexpect output is out of sync.
+		# Much pronounced when running back-to-back-back oc commands
+		if (commandline.count('oc logs') > 0) or (commandline.count('oc get') > 0):
+			self.ssh.send(commandline)
+			self.ssh.expect([commandline, pexpect.TIMEOUT])
+			self.ssh.send('\r\n')
+			self.sshresponse = self.ssh.expect([expectedline, pexpect.EOF, pexpect.TIMEOUT])
+		else:
+			self.ssh.sendline(commandline)
+			self.sshresponse = self.ssh.expect([expectedline, pexpect.EOF, pexpect.TIMEOUT])
 		if self.sshresponse == 0:
 			return 0
 		elif self.sshresponse == 1:
-- 
2.26.2