Skip to content

Commit 0e070f7

Browse files
authored
Correct path to test-upgrade.py (#3411)
correct path to the test-upgrade.py file retries on the test--cluster.py rework of the test-cluster.py to have retries and timeouts so it is more reliable when run from our CI
1 parent 86e4920 commit 0e070f7

File tree

2 files changed

+83
-23
lines changed

2 files changed

+83
-23
lines changed

tests/test-cluster.py

Lines changed: 67 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import string
22
import random
33
import time
4-
54
import pytest
65
import os
6+
import signal
77
import subprocess
88
from os import path
99

@@ -79,10 +79,19 @@ def _setup_lxc(self, channel_or_snap):
7979
if channel_or_snap.startswith("/"):
8080
self._transfer_install_local_snap_lxc(channel_or_snap)
8181
else:
82-
cmd_prefix = "/snap/bin/lxc exec {} -- script -e -c".format(self.vm_name).split()
83-
cmd = ["snap install microk8s --classic --channel {}".format(channel_or_snap)]
82+
cmd = "snap install microk8s --classic --channel {}".format(channel_or_snap)
8483
time.sleep(20)
85-
subprocess.check_output(cmd_prefix + cmd)
84+
print("About to run {}".format(cmd))
85+
output = ""
86+
attempt = 0
87+
while attempt < 3:
88+
try:
89+
output = self.run(cmd)
90+
break
91+
except ChildProcessError:
92+
time.sleep(10)
93+
attempt += 1
94+
print(output.decode())
8695
else:
8796
if channel_or_snap.startswith("/"):
8897
self._transfer_install_local_snap_lxc(channel_or_snap)
@@ -153,8 +162,18 @@ def run(self, cmd):
153162
)
154163
return output
155164
elif self.backend == "lxc":
156-
cmd_prefix = "/snap/bin/lxc exec {} -- script -e -c ".format(self.vm_name).split()
157-
output = subprocess.check_output(cmd_prefix + [cmd])
165+
cmd_prefix = "/snap/bin/lxc exec {} -- ".format(self.vm_name)
166+
with subprocess.Popen(
167+
cmd_prefix + cmd, shell=True, stdout=subprocess.PIPE, preexec_fn=os.setsid
168+
) as process:
169+
try:
170+
output = process.communicate(timeout=300)[0]
171+
if process.returncode != 0:
172+
raise ChildProcessError("Failed to run command")
173+
except subprocess.TimeoutExpired:
174+
os.killpg(process.pid, signal.SIGKILL) # send signal to the process group
175+
print("Process timed out")
176+
output = process.communicate()[0]
158177
return output
159178
else:
160179
raise Exception("Not implemented for backend {}".format(self.backend))
@@ -211,11 +230,20 @@ def setup_cluster(self):
211230

212231
# Wait for nodes to be ready
213232
print("Waiting for nodes to register")
214-
connected_nodes = vm_master.run("/snap/bin/microk8s.kubectl get no")
215-
while "NotReady" in connected_nodes.decode():
216-
time.sleep(5)
217-
connected_nodes = vm_master.run("/snap/bin/microk8s.kubectl get no")
218-
print(connected_nodes.decode())
233+
attempt = 0
234+
while attempt < 10:
235+
try:
236+
connected_nodes = vm_master.run("/snap/bin/microk8s.kubectl get no")
237+
if "NotReady" in connected_nodes.decode():
238+
time.sleep(5)
239+
connected_nodes = vm_master.run("/snap/bin/microk8s.kubectl get no")
240+
print(connected_nodes.decode())
241+
break
242+
except ChildProcessError:
243+
time.sleep(10)
244+
attempt += 1
245+
if attempt == 10:
246+
raise
219247

220248
# Wait for CNI pods
221249
print("Waiting for cni")
@@ -339,10 +367,20 @@ def test_nodes_in_ha(self):
339367
self.VM[0].run("/snap/bin/microk8s.join {}".format(endpoint[0]))
340368

341369
print("Waiting for nodes to be ready")
342-
connected_nodes = self.VM[0].run("/snap/bin/microk8s.kubectl get no")
343-
while "NotReady" in connected_nodes.decode():
344-
time.sleep(5)
345-
connected_nodes = self.VM[0].run("/snap/bin/microk8s.kubectl get no")
370+
attempt = 0
371+
while attempt < 10:
372+
try:
373+
connected_nodes = self.VM[0].run("/snap/bin/microk8s.kubectl get no")
374+
if "NotReady" in connected_nodes.decode():
375+
time.sleep(5)
376+
continue
377+
print(connected_nodes.decode())
378+
break
379+
except ChildProcessError:
380+
time.sleep(10)
381+
attempt += 1
382+
if attempt == 10:
383+
raise
346384

347385
attempt = 100
348386
while True:
@@ -375,11 +413,20 @@ def test_worker_noode(self):
375413

376414
# Wait for nodes to be ready
377415
print("Waiting for node to register")
378-
connected_nodes = vm_master.run("/snap/bin/microk8s.kubectl get no")
379-
while "NotReady" in connected_nodes.decode():
380-
time.sleep(5)
381-
connected_nodes = vm_master.run("/snap/bin/microk8s.kubectl get no")
382-
print(connected_nodes.decode())
416+
attempt = 0
417+
while attempt < 10:
418+
try:
419+
connected_nodes = vm_master.run("/snap/bin/microk8s.kubectl get no")
420+
if "NotReady" in connected_nodes.decode():
421+
time.sleep(5)
422+
continue
423+
print(connected_nodes.decode())
424+
break
425+
except ChildProcessError:
426+
time.sleep(10)
427+
attempt += 1
428+
if attempt == 10:
429+
raise
383430

384431
# Check that kubelet talks to the control plane node via the local proxy
385432
print("Checking the worker's configuration")

tests/test-distro.sh

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,27 @@ fi
5757
# therefore we do not need to run it inside a VM/container
5858
apt-get install python3-pip -y
5959
pip3 install -U pytest requests pyyaml sh
60-
LXC_PROFILE="tests/lxc/microk8s.profile" BACKEND="lxc" CHANNEL_TO_TEST=${TO_CHANNEL} pytest -s tests/test-cluster.py
60+
export LXC_PROFILE="tests/lxc/microk8s.profile"
61+
export BACKEND="lxc"
62+
export CHANNEL_TO_TEST=${TO_CHANNEL}
63+
TRY_ATTEMPT=0
64+
while ! (timeout 3600 pytest -s tests/test-cluster.py) &&
65+
! [ ${TRY_ATTEMPT} -eq 3 ]
66+
do
67+
TRY_ATTEMPT=$((TRY_ATTEMPT+1))
68+
sleep 1
69+
done
70+
if [ ${TRY_ATTEMPT} -eq 3 ]
71+
then
72+
echo "Test clusterring took longer than expected"
73+
exit 1
74+
fi
6175

6276
# Test addons upgrade
63-
6477
NAME=machine-$RANDOM
6578
create_machine $NAME $PROXY
6679
# use 'script' for required tty: https://github.com/lxc/lxd/issues/1724#issuecomment-194416774
67-
lxc exec $NAME -- script -e -c "UPGRADE_MICROK8S_FROM=${FROM_CHANNEL} UPGRADE_MICROK8S_TO=${TO_CHANNEL} pytest -s /var/snap/microk8s/common/addons/core/tests/test-upgrade.py"
80+
lxc exec $NAME -- script -e -c "UPGRADE_MICROK8S_FROM=${FROM_CHANNEL} UPGRADE_MICROK8S_TO=${TO_CHANNEL} pytest -s /var/tmp/tests/test-upgrade.py"
6881
lxc delete $NAME --force
6982

7083
# Test upgrade-path

0 commit comments

Comments
 (0)