1 # Copyright (C) 2007 Google Inc.
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful, but
9 # WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 """Daemon related QA tests.
25 from ganeti import utils
26 from ganeti import constants
32 from qa_utils import AssertEqual, StartSSH
35 def _InstanceRunning(node, name):
36 """Checks whether an instance is running.
39 node: Node the instance runs on
40 name: Full name of Xen instance
42 cmd = utils.ShellQuoteArgs(['xm', 'list', name]) + ' >/dev/null'
43 ret = StartSSH(node['primary'], cmd).wait()
47 def _XmShutdownInstance(node, name):
48 """Shuts down instance using "xm" and waits for completion.
51 node: Node the instance runs on
52 name: Full name of Xen instance
54 master = qa_config.GetMasterNode()
56 cmd = ['xm', 'shutdown', name]
57 AssertEqual(StartSSH(node['primary'],
58 utils.ShellQuoteArgs(cmd)).wait(), 0)
61 end = time.time() + 60
62 while time.time() <= end:
63 if not _InstanceRunning(node, name):
67 raise qa_error.Error("xm shutdown failed")
70 def _ResetWatcherDaemon():
71 """Removes the watcher daemon's state file.
74 node: Node to be reset
76 master = qa_config.GetMasterNode()
78 cmd = ['rm', '-f', constants.WATCHER_STATEFILE]
79 AssertEqual(StartSSH(master['primary'],
80 utils.ShellQuoteArgs(cmd)).wait(), 0)
83 def _RunWatcherDaemon():
84 """Runs the ganeti-watcher daemon on the master node.
87 master = qa_config.GetMasterNode()
89 cmd = ['ganeti-watcher', '-d']
90 AssertEqual(StartSSH(master['primary'],
91 utils.ShellQuoteArgs(cmd)).wait(), 0)
94 def PrintCronWarning():
95 """Shows a warning about the cron job.
98 msg = ("For the following tests it's recommended to turn off the"
99 " ganeti-watcher cronjob.")
101 print qa_utils.FormatWarning(msg)
104 @qa_utils.DefineHook('daemon-automatic-restart')
105 def TestInstanceAutomaticRestart(node, instance):
106 """Test automatic restart of instance by ganeti-watcher.
109 master = qa_config.GetMasterNode()
110 inst_name = qa_utils.ResolveInstanceName(instance)
112 _ResetWatcherDaemon()
113 _XmShutdownInstance(node, inst_name)
118 if not _InstanceRunning(node, inst_name):
119 raise qa_error.Error("Daemon didn't restart instance")
121 cmd = ['gnt-instance', 'info', inst_name]
122 AssertEqual(StartSSH(master['primary'],
123 utils.ShellQuoteArgs(cmd)).wait(), 0)
126 @qa_utils.DefineHook('daemon-consecutive-failures')
127 def TestInstanceConsecutiveFailures(node, instance):
128 """Test five consecutive instance failures.
131 master = qa_config.GetMasterNode()
132 inst_name = qa_utils.ResolveInstanceName(instance)
134 _ResetWatcherDaemon()
136 for should_start in ([True] * 5) + [False]:
137 _XmShutdownInstance(node, inst_name)
141 if bool(_InstanceRunning(node, inst_name)) != should_start:
143 msg = "Instance not started when it should"
145 msg = "Instance started when it shouldn't"
146 raise qa_error.Error(msg)
148 cmd = ['gnt-instance', 'info', inst_name]
149 AssertEqual(StartSSH(master['primary'],
150 utils.ShellQuoteArgs(cmd)).wait(), 0)