From 45390b6d68d1f21366689d900201f636a4460d7b Mon Sep 17 00:00:00 2001 From: Alberto Donato Date: Wed, 5 Aug 2015 17:34:49 +0200 Subject: [PATCH] Add test. --- actions/pause | 4 +- actions/resume | 4 +- tests/053-basic-pause-and-resume | 67 ++++++++++++++++++++++++++++++++ tests/basic_deployment.py | 5 +++ 4 files changed, 78 insertions(+), 2 deletions(-) create mode 100755 tests/053-basic-pause-and-resume diff --git a/actions/pause b/actions/pause index 88666ec8..5e1d8d09 100755 --- a/actions/pause +++ b/actions/pause @@ -2,9 +2,11 @@ import sys +sys.path.append("hooks") + from charmhelpers.core import hookenv, host if __name__ == "__main__": - if not host.service_pause("mysql"): + if not host.service_pause("keystone"): hookenv.action_fail("Failed to pause service keystone.") sys.exit(-1) diff --git a/actions/resume b/actions/resume index 5f1caa4a..499ec822 100755 --- a/actions/resume +++ b/actions/resume @@ -2,9 +2,11 @@ import sys +sys.path.append("hooks") + from charmhelpers.core import hookenv, host if __name__ == "__main__": - if not host.service_resume("mysql"): + if not host.service_resume("keystone"): hookenv.action_fail("Failed to resume service keystone.") sys.exit(-1) diff --git a/tests/053-basic-pause-and-resume b/tests/053-basic-pause-and-resume new file mode 100755 index 00000000..910ba553 --- /dev/null +++ b/tests/053-basic-pause-and-resume @@ -0,0 +1,67 @@ +#!/usr/bin/python + +"""Test for keystone pause and resume actions.""" + +import subprocess +import time +import yaml + +import basic_deployment + + +class PauseResume(basic_deployment.KeystoneBasicDeployment): + + def _run_action(self, unit_id, action, *args): + command = ["juju", "action", "do", unit_id, action] + command.extend(args) + print("Running command: %s\n" % " ".join(command)) + try: + output = subprocess.check_output(command) + except Exception as e: + print("Failed: %s, %s\n" % (e, output)) + parts = output.strip().split() + action_id = parts[-1] + return action_id + + def _wait_on_action(self, action_id): + command = ["juju", "action", "fetch", action_id] + while True: + try: + output = subprocess.check_output(command) + except Exception as e: + print(e) + return False + + data = yaml.safe_load(output) + if data["status"] == "completed": + return True + elif data["status"] == "failed": + return False + time.sleep(2) + + def run(self): + super(PauseResume, self).run() + unit_name = "keystone/0" + unit = self.d.sentry.unit[unit_name] + assert self.is_keystone_running(unit), \ + "keystone not running in initial state." + action_id = self._run_action(unit_name, "pause") + assert self._wait_on_action(action_id), "Pause action failed." + + assert not self.is_keystone_running(unit), "keystone is still running!" + init_contents = unit.directory_contents("/etc/init/") + assert "keystone.override" in init_contents["files"], \ + "Override file not created." + + action_id = self._run_action(unit_name, "resume") + assert self._wait_on_action(action_id), "Resume action failed" + init_contents = unit.directory_contents("/etc/init/") + assert "keystone.override" not in init_contents["files"], \ + "Override file not removed." + assert self.is_keystone_running(unit), \ + "keystone not running after resume." + + +if __name__ == "__main__": + p = PauseResume() + p.run() diff --git a/tests/basic_deployment.py b/tests/basic_deployment.py index bf5dd7c1..3a5752ad 100644 --- a/tests/basic_deployment.py +++ b/tests/basic_deployment.py @@ -38,6 +38,11 @@ class KeystoneBasicDeployment(OpenStackAmuletDeployment): self._deploy() self._initialize_tests() + def _is_keystone_running(self, unit): + """Return whether the keystone unit is running.""" + _, code = unit.run("pidof keystone") + return code == 0 + def _add_services(self): """Add services