From d38eee5b3ae77ffdd6fbe076231c9b86f09e9a9c Mon Sep 17 00:00:00 2001 From: Eli Qiao Date: Tue, 1 Dec 2015 21:32:49 +0800 Subject: [PATCH] Functional: only copy logs on exception This patch adds a copy_logs as the handler with unittest's addOnException, by doing this, we can get logs for all failed cases instead of copy them before teardown. If without any failed case, don't copy logs for saving time. Partially implements: blueprint swarm-functional-testing Change-Id: I47725d0d16735f650adf325adcf4194eb70c384d --- magnum/tests/contrib/copy_instance_logs.sh | 6 ++++- magnum/tests/functional/python_client_base.py | 27 ++++++++++--------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/magnum/tests/contrib/copy_instance_logs.sh b/magnum/tests/contrib/copy_instance_logs.sh index b021b5ab83..86b8aa74fc 100755 --- a/magnum/tests/contrib/copy_instance_logs.sh +++ b/magnum/tests/contrib/copy_instance_logs.sh @@ -47,9 +47,11 @@ if [[ "$COE" == "kubernetes" ]]; then remote_exec $SSH_USER "sudo journalctl -u kube-controller-manager --no-pager" kube-controller-manager.log remote_exec $SSH_USER "sudo journalctl -u docker --no-pager" docker.log remote_exec $SSH_USER "sudo journalctl -u flanneld --no-pager" flanneld.log + remote_exec $SSH_USER "sudo ip a" ipa.log + remote_exec $SSH_USER "sudo netstat -an" netstat.log elif [[ "$COE" == "swarm" ]]; then SSH_USER=fedora - remote_exec $SSH_USER "sudo systemctl list-units" systemctl_list_units.log + remote_exec $SSH_USER "sudo systemctl --full list-units" systemctl_list_units.log remote_exec $SSH_USER "sudo journalctl -u cloud-config --no-pager" cloud-config.log remote_exec $SSH_USER "sudo journalctl -u cloud-final --no-pager" cloud-final.log remote_exec $SSH_USER "sudo journalctl -u cloud-init-local --no-pager" cloud-init-local.log @@ -59,6 +61,8 @@ elif [[ "$COE" == "swarm" ]]; then remote_exec $SSH_USER "sudo journalctl -u swarm-agent --no-pager" swarm-agent.log remote_exec $SSH_USER "sudo journalctl -u docker --no-pager" docker.log remote_exec $SSH_USER "sudo journalctl -u flanneld --no-pager" flanneld.log + remote_exec $SSH_USER "sudo ip a" ipa.log + remote_exec $SSH_USER "sudo netstat -an" netstat.log else echo "ERROR: Unknown COE '${COE}'" EXIT_CODE=1 diff --git a/magnum/tests/functional/python_client_base.py b/magnum/tests/functional/python_client_base.py index 1c8b845862..6ec2b5def6 100644 --- a/magnum/tests/functional/python_client_base.py +++ b/magnum/tests/functional/python_client_base.py @@ -153,18 +153,21 @@ class BaseMagnumClient(base.TestCase): def _delete_bay(cls, bay_uuid): cls.cs.bays.delete(bay_uuid) - @classmethod - def _copy_logs(cls): - if not cls.copy_logs: + def _copy_logs(self, exec_info): + if not self.copy_logs: return + fn = exec_info[2].tb_frame.f_locals['fn'] + func_name = fn.im_self._get_test_method().__name__ - cls.bay = cls._show_bay(cls.bay.uuid) - for node_addr in cls.bay.node_addresses: + bay = self._show_bay(self.bay.uuid) + for node_addr in bay.node_addresses: subprocess.call(["magnum/tests/contrib/copy_instance_logs.sh", - node_addr, cls.baymodel.coe, "worker"]) - for node_addr in getattr(cls.bay, 'master_addresses', []): + node_addr, self.baymodel.coe, + "worker-" + func_name]) + for node_addr in getattr(bay, 'master_addresses', []): subprocess.call(["magnum/tests/contrib/copy_instance_logs.sh", - node_addr, cls.baymodel.coe, "master"]) + node_addr, self.baymodel.coe, + "master-" + func_name]) class BayTest(BaseMagnumClient): @@ -229,12 +232,12 @@ class BayTest(BaseMagnumClient): class BayAPITLSTest(BaseMagnumClient): """Base class of TLS enabled test case.""" + def setUp(self): + super(BayAPITLSTest, self).setUp() + self.addOnException(self._copy_logs) + @classmethod def tearDownClass(cls): - try: - cls._copy_logs() - except Exception as e: - print("WARNING: Failed to copy logs. Error: " + repr(e)) if cls.ca_dir: rmtree_without_raise(cls.ca_dir)