Skip to content

Commit

Permalink
format python test code
Browse files Browse the repository at this point in the history
  • Loading branch information
kate-goldenring committed Apr 19, 2021
1 parent cd2523a commit 8d7682b
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 62 deletions.
108 changes: 69 additions & 39 deletions test/run-end-to-end.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
#!/usr/bin/env python3
import shared_test_code
import json, os, subprocess, time, yaml
import json
import os
import subprocess
import time
import yaml
from kubernetes import client, config
from kubernetes.client.rest import ApiException


def main():
print("End-to-end test main start")

# If this is a PUSH, the test needs to wait for the new containers to be
# built/pushed. In this case, the workflow will set /tmp/sleep_duration.txt to
# the number of seconds to sleep.
# If this is a MANUALLY triggerd or a PULL-REQUEST, no new containers will
# If this is a MANUALLY triggerd or a PULL-REQUEST, no new containers will
# be built/pushed, the workflows will not set /tmp/sleep_duration.txt and
# this test will execute immediately.
shared_test_code.initial_sleep()
Expand All @@ -21,7 +26,7 @@ def main():
# Get version of akri to test
test_version = shared_test_code.get_test_version()
print("Testing version: {}".format(test_version))

shared_test_code.major_version = "v" + test_version.split(".")[0]
print("Testing major version: {}".format(shared_test_code.major_version))

Expand All @@ -48,7 +53,7 @@ def main():
".format(location=helm_chart_location, config_name=shared_test_code.DEBUG_ECHO_NAME, description_prefix=shared_test_code.DEBUG_ECHO_DESCRIPTIONS_PREFIX, res_width_key=shared_test_code.PROPERTIES_RESOLUTION_WIDTH_KEY, res_width_val=shared_test_code.PROPERTIES_RESOLUTION_WIDTH_VALUE, res_height_key=shared_test_code.PROPERTIES_RESOLUTION_HEIGHT_KEY, res_height_val=shared_test_code.PROPERTIES_RESOLUTION_HEIGHT_VALUE, cri_args=cri_args, helm_args=extra_helm_args)
print("Helm command: {}".format(helm_install_command))
os.system(helm_install_command)

try:
res = do_test()
except Exception as e:
Expand All @@ -58,7 +63,7 @@ def main():
# Best effort cleanup work
try:
# Save Agent and controller logs
shared_test_code.save_agent_and_controller_logs()
shared_test_code.save_agent_and_controller_logs()
finally:
# Delete akri and check that controller and Agent pods deleted
os.system("helm delete akri")
Expand All @@ -67,10 +72,9 @@ def main():
if not shared_test_code.check_akri_state(0, 0, 0, 0, 0, 0):
print("Akri not running in expected state after helm delete")
raise RuntimeError("Scenario Failed")

if not res:
raise RuntimeError("Scenario Failed")



def do_test():
Expand All @@ -86,72 +90,88 @@ def do_test():
if not shared_test_code.crds_applied():
print("CRDs not applied by helm chart")
return False

print("Checking for initial Akri state")
if not shared_test_code.check_akri_state(1, 1, 2, 2, 1, 2):
print("Akri not running in expected state")
os.system('sudo {} get pods,services,akric,akrii --show-labels'.format(kubectl_cmd))
os.system(
'sudo {} get pods,services,akric,akrii --show-labels'.format(kubectl_cmd))
return False

#
# Check agent responds to dynamic offline/online resource
#
print("Writing to Agent pod {} that device offline".format(shared_test_code.agent_pod_name))
os.system('sudo {} exec -i {} -- /bin/bash -c "echo "OFFLINE" > /tmp/debug-echo-availability.txt"'.format(kubectl_cmd, shared_test_code.agent_pod_name))
#
print("Writing to Agent pod {} that device offline".format(
shared_test_code.agent_pod_name))
os.system('sudo {} exec -i {} -- /bin/bash -c "echo "OFFLINE" > /tmp/debug-echo-availability.txt"'.format(
kubectl_cmd, shared_test_code.agent_pod_name))

print("Checking Akri state after taking device offline")
if not shared_test_code.check_akri_state(1, 1, 0, 0, 0, 0):
print("Akri not running in expected state after taking device offline")
os.system('sudo {} get pods,services,akric,akrii --show-labels'.format(kubectl_cmd))
os.system(
'sudo {} get pods,services,akric,akrii --show-labels'.format(kubectl_cmd))
return False

# Do back online scenario
print("Writing to Agent pod {} that device online".format(shared_test_code.agent_pod_name))
os.system('sudo {} exec -i {} -- /bin/bash -c "echo "ONLINE" > /tmp/debug-echo-availability.txt"'.format(kubectl_cmd, shared_test_code.agent_pod_name))

print("Writing to Agent pod {} that device online".format(
shared_test_code.agent_pod_name))
os.system('sudo {} exec -i {} -- /bin/bash -c "echo "ONLINE" > /tmp/debug-echo-availability.txt"'.format(
kubectl_cmd, shared_test_code.agent_pod_name))

print("Checking Akri state after bringing device back online")
if not shared_test_code.check_akri_state(1, 1, 2, 2, 1, 2):
print("Akri not running in expected state after bringing device back online")
os.system('sudo {} get pods,services,akric,akrii --show-labels'.format(kubectl_cmd))
os.system(
'sudo {} get pods,services,akric,akrii --show-labels'.format(kubectl_cmd))
return False

#
# Check that slot reconciliation is working on agent
#
#
print("Check logs for Agent slot-reconciliation for pod {}".format(shared_test_code.agent_pod_name))
temporary_agent_log_path = "/tmp/agent_log.txt"
for x in range(3):
log_result = subprocess.run('sudo {} logs {} > {}'.format(kubectl_cmd, shared_test_code.agent_pod_name, temporary_agent_log_path), shell=True)
log_result = subprocess.run('sudo {} logs {} > {}'.format(
kubectl_cmd, shared_test_code.agent_pod_name, temporary_agent_log_path), shell=True)
if log_result.returncode == 0:
print("Successfully stored Agent logs in {}".format(temporary_agent_log_path))
print("Successfully stored Agent logs in {}".format(
temporary_agent_log_path))
break
print("Failed to get logs from {} pod with result {} on attempt {} of 3".format(shared_test_code.agent_pod_name, log_result, x))
print("Failed to get logs from {} pod with result {} on attempt {} of 3".format(
shared_test_code.agent_pod_name, log_result, x))
if x == 2:
return False
grep_result = subprocess.run(['grep', "get_node_slots - crictl called successfully", temporary_agent_log_path])
grep_result = subprocess.run(
['grep', "get_node_slots - crictl called successfully", temporary_agent_log_path])
if grep_result.returncode != 0:
print("Akri failed to successfully connect to crictl via the CRI socket with return value of {}", grep_result)
# Log information to understand why error occurred
os.system('sudo {} get pods,services,akric,akrii --show-labels'.format(kubectl_cmd))
os.system(
'sudo {} get pods,services,akric,akrii --show-labels'.format(kubectl_cmd))
os.system('grep get_node_slots {}'.format(temporary_agent_log_path))
return False

#
# Check that broker is recreated if it is deleted
#
broker_pod_selector = "{}={}".format(shared_test_code.CONFIGURATION_LABEL_NAME, shared_test_code.DEBUG_ECHO_NAME)
brokers_info = shared_test_code.get_running_pod_names_and_uids(broker_pod_selector)
#
broker_pod_selector = "{}={}".format(
shared_test_code.CONFIGURATION_LABEL_NAME, shared_test_code.DEBUG_ECHO_NAME)
brokers_info = shared_test_code.get_running_pod_names_and_uids(
broker_pod_selector)
if len(brokers_info) != 2:
print("Expected to find 2 broker pods but found: {}", len(brokers_info))
os.system('sudo {} get pods,services,akric,akrii --show-labels'.format(kubectl_cmd))
os.system(
'sudo {} get pods,services,akric,akrii --show-labels'.format(kubectl_cmd))
return False

# There is a possible race condition here between when the `kubectl delete pod` returns,
# when check_broker_pod_state validates that the pod is gone, and when the check_akri_state
# validates that the broker pod has been restarted

broker_pod_name = sorted(brokers_info.keys())[0]
delete_pod_command = 'sudo {} delete pod {}'.format(kubectl_cmd, broker_pod_name)
delete_pod_command = 'sudo {} delete pod {}'.format(
kubectl_cmd, broker_pod_name)
print("Deleting broker pod: {}".format(delete_pod_command))
os.system(delete_pod_command)

Expand All @@ -161,33 +181,43 @@ def do_test():
# Wait for there to be 2 brokers pods again
if not shared_test_code.check_broker_pods_state(v1, 2):
print("Akri not running in expected state after broker pod restoration should have happened")
os.system('sudo {} get pods,services,akric,akrii --show-labels'.format(kubectl_cmd))
os.system(
'sudo {} get pods,services,akric,akrii --show-labels'.format(kubectl_cmd))
return False

restored_brokers_info = shared_test_code.get_running_pod_names_and_uids(broker_pod_selector)
restored_brokers_info = shared_test_code.get_running_pod_names_and_uids(
broker_pod_selector)
if len(restored_brokers_info) != 2:
print("Expected to find 2 broker pods but found: {}", len(restored_brokers_info))
os.system('sudo {} get pods,services,akric,akrii --show-labels'.format(kubectl_cmd))
print("Expected to find 2 broker pods but found: {}",
len(restored_brokers_info))
os.system(
'sudo {} get pods,services,akric,akrii --show-labels'.format(kubectl_cmd))
return False

# Make sure that the deleted broker uid is different from the restored broker pod uid ... signifying
# that the Pod was restarted
print("Restored broker pod uid should differ from original broker pod uid")
if brokers_info[broker_pod_name] == restored_brokers_info[broker_pod_name]:
print("Restored broker pod uid [{}] should differ from original broker pod uid [{}]".format(brokers_info[broker_pod_name], restored_brokers_info[broker_pod_name]))
os.system('sudo {} get pods,services,akric,akrii --show-labels'.format(kubectl_cmd))
print("Restored broker pod uid [{}] should differ from original broker pod uid [{}]".format(
brokers_info[broker_pod_name], restored_brokers_info[broker_pod_name]))
os.system(
'sudo {} get pods,services,akric,akrii --show-labels'.format(kubectl_cmd))
return False

# Do cleanup scenario
print("Deleting Akri configuration: {}".format(shared_test_code.DEBUG_ECHO_NAME))
os.system("sudo {} delete akric {}".format(kubectl_cmd, shared_test_code.DEBUG_ECHO_NAME))
print("Deleting Akri configuration: {}".format(
shared_test_code.DEBUG_ECHO_NAME))
os.system("sudo {} delete akric {}".format(
kubectl_cmd, shared_test_code.DEBUG_ECHO_NAME))

print("Checking Akri state after deleting configuration")
if not shared_test_code.check_akri_state(1, 1, 0, 0, 0, 0):
print("Akri not running in expected state after deleting configuration")
os.system('sudo {} get pods,services,akric,akrii --show-labels'.format(kubectl_cmd))
os.system(
'sudo {} get pods,services,akric,akrii --show-labels'.format(kubectl_cmd))
return False

return True
return True


main()
46 changes: 23 additions & 23 deletions test/shared_test_code.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#mod shared_test_code
# mod shared_test_code

import json, os, time, yaml
import json
import os
import time
import yaml
from kubernetes import client, config
from kubernetes.client.rest import ApiException

Expand Down Expand Up @@ -92,17 +95,14 @@ def get_test_version():
def save_agent_and_controller_logs(namespace="default"):
kubectl_cmd = get_kubectl_command()
for x in range(5):
if os.system("sudo {} logs {} --namespace={} >> {}".format(kubectl_cmd,
agent_pod_name,
namespace,
AGENT_LOG_PATH)):
continue

if os.system("sudo {} logs {} --namespace={} >> {}".format(kubectl_cmd,
controller_pod_name,
namespace,
CONTROLLER_LOG_PATH)):
continue
if not os.system("sudo {} logs {} --namespace={} >> {}".format(kubectl_cmd,
agent_pod_name,
namespace,
AGENT_LOG_PATH)) and not os.system("sudo {} logs {} --namespace={} >> {}".format(kubectl_cmd,
controller_pod_name,
namespace,
CONTROLLER_LOG_PATH)):
break
if x == 4:
print("Could not successfully save agent and controller logs after 5 tries")

Expand Down Expand Up @@ -145,39 +145,39 @@ def check_pods_running(v1, pod_label_selector, count):
if pod.status.phase != "Terminating":
all_terminating = False
break
if all_terminating: return True
if all_terminating:
return True
else:
# Expectation is that `count` pods are running
all_running = True
if len(pods) == count:
for pod in pods:
if pod.status.phase != "Running":
all_running = False
break
break
if all_running:
if pod_label_selector == BROKER_POD_LABEL_SELECTOR:
return check_broker_pods_env_var(pods)
return check_broker_pods_env_var(pods)
else:
return True
print("Wrong number of pods [{}] found ... expected {}".format(
pod_label_selector, count))
return False


def check_broker_pods_env_var(pods):
kubectl_cmd = get_kubectl_command()
for pod in pods:
for x in range(5):
if os.system('sudo {} exec -i {} -- /bin/bash -c "printenv | grep ^DEBUG_ECHO_DESCRIPTION={} | wc -l | grep -v 0"'.format(kubectl_cmd, pod.metadata.name, DEBUG_ECHO_DESCRIPTIONS_PREFIX)):
print("Could not find a DEBUG_ECHO_DESCRIPTION environment variable in broker Pod {} on try {}".format(pod.metadata.name, x + 1))
if os.system('sudo {} exec -i {} -- /bin/bash -c "printenv | grep ^{}={}$ | wc -l | grep -v 0"'.format(kubectl_cmd, pod.metadata.name, PROPERTIES_RESOLUTION_WIDTH_KEY, PROPERTIES_RESOLUTION_WIDTH_VALUE)):
print("Could not find a {} environment variable in broker Pod {} on try {}".format(PROPERTIES_RESOLUTION_WIDTH_KEY, pod.metadata.name, x + 1))
if os.system('sudo {} exec -i {} -- /bin/bash -c "printenv | grep ^{}={}$ | wc -l | grep -v 0"'.format(kubectl_cmd, pod.metadata.name, PROPERTIES_RESOLUTION_HEIGHT_KEY, PROPERTIES_RESOLUTION_HEIGHT_VALUE)):
print("Could not find a {} environment variable in broker Pod {} on try {}".format(PROPERTIES_RESOLUTION_HEIGHT_KEY, pod.metadata.name, x + 1))
if not os.system('sudo {} exec -i {} -- /bin/bash -c "printenv | grep ^DEBUG_ECHO_DESCRIPTION={} | wc -l | grep -v 0"'.format(kubectl_cmd, pod.metadata.name, DEBUG_ECHO_DESCRIPTIONS_PREFIX)) and not os.system('sudo {} exec -i {} -- /bin/bash -c "printenv | grep ^{}={}$ | wc -l | grep -v 0"'.format(kubectl_cmd, pod.metadata.name, PROPERTIES_RESOLUTION_WIDTH_KEY, PROPERTIES_RESOLUTION_WIDTH_VALUE)) and not os.system('sudo {} exec -i {} -- /bin/bash -c "printenv | grep ^{}={}$ | wc -l | grep -v 0"'.format(kubectl_cmd, pod.metadata.name, PROPERTIES_RESOLUTION_HEIGHT_KEY, PROPERTIES_RESOLUTION_HEIGHT_VALUE)):
break
if x == 4:
print("Could not find expected environment variables in broker Pod {} after 5 tries".format(pod.metadata.name, x))
print("Could not find expected environment variables in broker Pod {} after 5 tries".format(
pod.metadata.name, x))
return False
return True


def check_svcs_running(v1, svc_label_selector, count):
print("Checking number of svcs [{}] ... expected {}".format(
svc_label_selector, count))
Expand Down

0 comments on commit 8d7682b

Please sign in to comment.