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 AssertEqual, AssertMatch, StartSSH, GetCommandOutput
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", "--ignore-pause"]
93 AssertEqual(StartSSH(master["primary"],
94 utils.ShellQuoteArgs(cmd)).wait(), 0)
97 def TestPauseWatcher():
98 """Tests and pauses the watcher.
101 master = qa_config.GetMasterNode()
103 cmd = ["gnt-cluster", "watcher", "pause", "4h"]
104 AssertEqual(StartSSH(master["primary"],
105 utils.ShellQuoteArgs(cmd)).wait(), 0)
107 cmd = ["gnt-cluster", "watcher", "info"]
108 output = GetCommandOutput(master["primary"],
109 utils.ShellQuoteArgs(cmd))
110 AssertMatch(output, r"^.*\bis paused\b.*")
113 def TestResumeWatcher():
114 """Tests and unpauses the watcher.
117 master = qa_config.GetMasterNode()
119 cmd = ["gnt-cluster", "watcher", "continue"]
120 AssertEqual(StartSSH(master["primary"],
121 utils.ShellQuoteArgs(cmd)).wait(), 0)
123 cmd = ["gnt-cluster", "watcher", "info"]
124 output = GetCommandOutput(master["primary"],
125 utils.ShellQuoteArgs(cmd))
126 AssertMatch(output, r"^.*\bis not paused\b.*")
129 def TestInstanceAutomaticRestart(node, instance):
130 """Test automatic restart of instance by ganeti-watcher.
133 master = qa_config.GetMasterNode()
134 inst_name = qa_utils.ResolveInstanceName(instance["name"])
136 _ResetWatcherDaemon()
137 _XmShutdownInstance(node, inst_name)
142 if not _InstanceRunning(node, inst_name):
143 raise qa_error.Error("Daemon didn't restart instance")
145 cmd = ['gnt-instance', 'info', inst_name]
146 AssertEqual(StartSSH(master['primary'],
147 utils.ShellQuoteArgs(cmd)).wait(), 0)
150 def TestInstanceConsecutiveFailures(node, instance):
151 """Test five consecutive instance failures.
154 master = qa_config.GetMasterNode()
155 inst_name = qa_utils.ResolveInstanceName(instance["name"])
157 _ResetWatcherDaemon()
159 for should_start in ([True] * 5) + [False]:
160 _XmShutdownInstance(node, inst_name)
164 if bool(_InstanceRunning(node, inst_name)) != should_start:
166 msg = "Instance not started when it should"
168 msg = "Instance started when it shouldn't"
169 raise qa_error.Error(msg)
171 cmd = ['gnt-instance', 'info', inst_name]
172 AssertEqual(StartSSH(master['primary'],
173 utils.ShellQuoteArgs(cmd)).wait(), 0)