4 # Copyright (C) 2007 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 AssertEqual, StartSSH
38 def _InstanceRunning(node, name):
39 """Checks whether an instance is running.
42 node: Node the instance runs on
43 name: Full name of 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.
54 node: Node the instance runs on
55 name: Full name of Xen instance
57 master = qa_config.GetMasterNode()
59 cmd = ['xm', 'shutdown', name]
60 AssertEqual(StartSSH(node['primary'],
61 utils.ShellQuoteArgs(cmd)).wait(), 0)
64 end = time.time() + 60
65 while time.time() <= end:
66 if not _InstanceRunning(node, name):
70 raise qa_error.Error("xm shutdown failed")
73 def _ResetWatcherDaemon():
74 """Removes the watcher daemon's state file.
77 node: Node to be reset
79 master = qa_config.GetMasterNode()
81 cmd = ['rm', '-f', constants.WATCHER_STATEFILE]
82 AssertEqual(StartSSH(master['primary'],
83 utils.ShellQuoteArgs(cmd)).wait(), 0)
86 def _RunWatcherDaemon():
87 """Runs the ganeti-watcher daemon on the master node.
90 master = qa_config.GetMasterNode()
92 cmd = ['ganeti-watcher', '-d']
93 AssertEqual(StartSSH(master['primary'],
94 utils.ShellQuoteArgs(cmd)).wait(), 0)
97 def PrintCronWarning():
98 """Shows a warning about the cron job.
101 msg = ("For the following tests it's recommended to turn off the"
102 " ganeti-watcher cronjob.")
104 print qa_utils.FormatWarning(msg)
107 def TestInstanceAutomaticRestart(node, instance):
108 """Test automatic restart of instance by ganeti-watcher.
111 master = qa_config.GetMasterNode()
112 inst_name = qa_utils.ResolveInstanceName(instance)
114 _ResetWatcherDaemon()
115 _XmShutdownInstance(node, inst_name)
120 if not _InstanceRunning(node, inst_name):
121 raise qa_error.Error("Daemon didn't restart instance")
123 cmd = ['gnt-instance', 'info', inst_name]
124 AssertEqual(StartSSH(master['primary'],
125 utils.ShellQuoteArgs(cmd)).wait(), 0)
128 def TestInstanceConsecutiveFailures(node, instance):
129 """Test five consecutive instance failures.
132 master = qa_config.GetMasterNode()
133 inst_name = qa_utils.ResolveInstanceName(instance)
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 cmd = ['gnt-instance', 'info', inst_name]
150 AssertEqual(StartSSH(master['primary'],
151 utils.ShellQuoteArgs(cmd)).wait(), 0)