Skip to content

Commit

Permalink
Reduce backup test duration by removing sleep().
Browse files Browse the repository at this point in the history
Instead of waiting for 70 seconds, the test now uses "watch" API
(instead of polling). This lets the test proceed as soon as backup pod appears.
  • Loading branch information
draghuram committed Jan 30, 2020
1 parent acc860c commit 1a364f7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 20 deletions.
8 changes: 7 additions & 1 deletion tests/src/common/kubeclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import time
import urllib3

from kubernetes import client
from kubernetes import client, watch
from kubernetes import config as k8sconfig

import conftest
Expand Down Expand Up @@ -80,6 +80,12 @@ def list(self, label_selector="", timeout_seconds=30):
return self.v1api.list_namespaced_pod(self.namespace, label_selector=label_selector,
timeout_seconds=timeout_seconds)

def get_by_watch(self, label_selector="", timeout_seconds=30):
w = watch.Watch()
for event in w.stream(self.v1api.list_namespaced_pod, self.namespace,
label_selector=label_selector, timeout_seconds=timeout_seconds):
return event['object']

def read(self, name):
return self.v1api.read_namespaced_pod(name, self.namespace)

Expand Down
23 changes: 4 additions & 19 deletions tests/src/test_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,29 +119,14 @@ def test_backup(globalconfig, resources):
# Wait for a backup pod to appear and then check its status.
# Since the backup schedule is every minute, wait for slightly
# longer than a minute before checking.
time.sleep(70)

pods = kubeclient.wait_for_pod_to_appear(label_selector)

# Since the backup runs every minute and we are waiting for more than a
# a minute, there are several possibilities:
# - No backups started which is extremely unlikely.
# - A single backup started which either finished or is still running.
# - More than one backup started
# To take care of all these scenarios, we first look for completed backup
# and if we don't find one, we look for a running backup.

backup_pod = next((x for x in pods.items if x.status.phase != "Running"), None)
if not backup_pod:
backup_pod = next((x for x in pods.items if x.status.phase == "Running"), None)
if not backup_pod:
raise Exception("Could not find a completed or running backup")
backup_pod = globalconfig.pod_api.get_by_watch(label_selector, timeout_seconds=75)

pod_name = backup_pod.metadata.name
resources["backup_pod_name"] = pod_name

if backup_pod.status.phase == "Running":
phase = backup_pod.status.phase
if phase == "Running" or phase == "Pending":
pod = kubeclient.wait_for_pod_to_be_done(pod_name)
backup_pod = globalconfig.pod_api.get(pod_name)
backup_pod = globalconfig.pod_api.read(pod_name)

assert backup_pod.status.phase == "Succeeded"

0 comments on commit 1a364f7

Please # to comment.