4 # Copyright (C) 2007, 2008, 2009, 2010 Google Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 """Daemon related QA tests.
28 from ganeti import utils
29 from ganeti import constants
35 from qa_utils import AssertMatch, AssertCommand, StartSSH, GetCommandOutput
38 def _InstanceRunning(node, name):
39 """Checks whether an instance is running.
41 @param node: node the instance runs on
42 @param name: full name of the Xen instance
45 cmd = utils.ShellQuoteArgs(['xm', 'list', name]) + ' >/dev/null'
46 ret = StartSSH(node['primary'], cmd).wait()
50 def _XmShutdownInstance(node, name):
51 """Shuts down instance using "xm" and waits for completion.
53 @param node: node the instance runs on
54 @param name: full name of Xen instance
57 AssertCommand(["xm", "shutdown", name], node=node)
60 end = time.time() + 60
61 while time.time() <= end:
62 if not _InstanceRunning(node, name):
66 raise qa_error.Error("xm shutdown failed")
69 def _ResetWatcherDaemon():
70 """Removes the watcher daemon's state file.
73 AssertCommand(["rm", "-f", constants.WATCHER_STATEFILE])
76 def _RunWatcherDaemon():
77 """Runs the ganeti-watcher daemon on the master node.
80 AssertCommand(["ganeti-watcher", "-d", "--ignore-pause"])
83 def TestPauseWatcher():
84 """Tests and pauses the watcher.
87 master = qa_config.GetMasterNode()
89 AssertCommand(["gnt-cluster", "watcher", "pause", "4h"])
91 cmd = ["gnt-cluster", "watcher", "info"]
92 output = GetCommandOutput(master["primary"],
93 utils.ShellQuoteArgs(cmd))
94 AssertMatch(output, r"^.*\bis paused\b.*")
97 def TestResumeWatcher():
98 """Tests and unpauses the watcher.
101 master = qa_config.GetMasterNode()
103 AssertCommand(["gnt-cluster", "watcher", "continue"])
105 cmd = ["gnt-cluster", "watcher", "info"]
106 output = GetCommandOutput(master["primary"],
107 utils.ShellQuoteArgs(cmd))
108 AssertMatch(output, r"^.*\bis not paused\b.*")
111 def TestInstanceAutomaticRestart(node, instance):
112 """Test automatic restart of instance by ganeti-watcher.
115 inst_name = qa_utils.ResolveInstanceName(instance["name"])
117 _ResetWatcherDaemon()
118 _XmShutdownInstance(node, inst_name)
123 if not _InstanceRunning(node, inst_name):
124 raise qa_error.Error("Daemon didn't restart instance")
126 AssertCommand(["gnt-instance", "info", inst_name])
129 def TestInstanceConsecutiveFailures(node, instance):
130 """Test five consecutive instance failures.
133 inst_name = qa_utils.ResolveInstanceName(instance["name"])
135 _ResetWatcherDaemon()
137 for should_start in ([True] * 5) + [False]:
138 _XmShutdownInstance(node, inst_name)
142 if bool(_InstanceRunning(node, inst_name)) != should_start:
144 msg = "Instance not started when it should"
146 msg = "Instance started when it shouldn't"
147 raise qa_error.Error(msg)
149 AssertCommand(["gnt-instance", "info", inst_name])