|
1 | 1 | import string
|
2 | 2 | import random
|
3 | 3 | import time
|
4 |
| - |
5 | 4 | import pytest
|
6 | 5 | import os
|
| 6 | +import signal |
7 | 7 | import subprocess
|
8 | 8 | from os import path
|
9 | 9 |
|
@@ -79,10 +79,19 @@ def _setup_lxc(self, channel_or_snap):
|
79 | 79 | if channel_or_snap.startswith("/"):
|
80 | 80 | self._transfer_install_local_snap_lxc(channel_or_snap)
|
81 | 81 | 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) |
84 | 83 | 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()) |
86 | 95 | else:
|
87 | 96 | if channel_or_snap.startswith("/"):
|
88 | 97 | self._transfer_install_local_snap_lxc(channel_or_snap)
|
@@ -153,8 +162,18 @@ def run(self, cmd):
|
153 | 162 | )
|
154 | 163 | return output
|
155 | 164 | 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] |
158 | 177 | return output
|
159 | 178 | else:
|
160 | 179 | raise Exception("Not implemented for backend {}".format(self.backend))
|
@@ -211,11 +230,20 @@ def setup_cluster(self):
|
211 | 230 |
|
212 | 231 | # Wait for nodes to be ready
|
213 | 232 | 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 |
219 | 247 |
|
220 | 248 | # Wait for CNI pods
|
221 | 249 | print("Waiting for cni")
|
@@ -339,10 +367,20 @@ def test_nodes_in_ha(self):
|
339 | 367 | self.VM[0].run("/snap/bin/microk8s.join {}".format(endpoint[0]))
|
340 | 368 |
|
341 | 369 | 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 |
346 | 384 |
|
347 | 385 | attempt = 100
|
348 | 386 | while True:
|
@@ -375,11 +413,20 @@ def test_worker_noode(self):
|
375 | 413 |
|
376 | 414 | # Wait for nodes to be ready
|
377 | 415 | 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 |
383 | 430 |
|
384 | 431 | # Check that kubelet talks to the control plane node via the local proxy
|
385 | 432 | print("Checking the worker's configuration")
|
|
0 commit comments