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(node):
71 """Removes the watcher daemon's state file.
74 node: Node to be reset
76 cmd = ['rm', '-f', constants.WATCHER_STATEFILE]
77 AssertEqual(StartSSH(node['primary'],
78 utils.ShellQuoteArgs(cmd)).wait(), 0)
81 def PrintCronWarning():
82 """Shows a warning about the required cron job.
86 print qa_utils.FormatWarning("The following tests require the cron script "
87 "for ganeti-watcher to be set up.")
90 def TestInstanceAutomaticRestart(node, instance):
91 """Test automatic restart of instance by ganeti-watcher.
93 Note: takes up to 6 minutes to complete.
95 master = qa_config.GetMasterNode()
96 inst_name = qa_utils.ResolveInstanceName(instance)
98 _ResetWatcherDaemon(node)
99 _XmShutdownInstance(node, inst_name)
101 # Give it a bit more than five minutes to start again
102 restart_at = time.time() + 330
104 # Wait until it's running again
105 while time.time() <= restart_at:
106 if _InstanceRunning(node, inst_name):
110 raise qa_error.Error("Daemon didn't restart instance in time")
112 cmd = ['gnt-instance', 'info', inst_name]
113 AssertEqual(StartSSH(master['primary'],
114 utils.ShellQuoteArgs(cmd)).wait(), 0)
117 def TestInstanceConsecutiveFailures(node, instance):
118 """Test five consecutive instance failures.
120 Note: takes at least 35 minutes to complete.
122 master = qa_config.GetMasterNode()
123 inst_name = qa_utils.ResolveInstanceName(instance)
125 _ResetWatcherDaemon(node)
126 _XmShutdownInstance(node, inst_name)
128 # Do shutdowns for 30 minutes
129 finished_at = time.time() + (35 * 60)
131 while time.time() <= finished_at:
132 if _InstanceRunning(node, inst_name):
133 _XmShutdownInstance(node, inst_name)
136 # Check for some time whether the instance doesn't start again
137 check_until = time.time() + 330
138 while time.time() <= check_until:
139 if _InstanceRunning(node, inst_name):
140 raise qa_error.Error("Instance started when it shouldn't")
143 cmd = ['gnt-instance', 'info', inst_name]
144 AssertEqual(StartSSH(master['primary'],
145 utils.ShellQuoteArgs(cmd)).wait(), 0)