4 # Copyright (C) 2007, 2008, 2009, 2010, 2011 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(name):
39 """Checks whether an instance is running.
41 @param name: full name of the instance
44 master = qa_config.GetMasterNode()
46 cmd = (utils.ShellQuoteArgs(["gnt-instance", "list", "-o", "status", name]) +
48 ret = StartSSH(master["primary"], cmd).wait()
52 def _ShutdownInstance(name):
53 """Shuts down instance without recording state and waits for completion.
55 @param name: full name of the instance
58 AssertCommand(["gnt-instance", "shutdown", "--no-remember", name])
60 if _InstanceRunning(name):
61 raise qa_error.Error("instance shutdown failed")
64 def _ResetWatcherDaemon():
65 """Removes the watcher daemon's state file.
68 AssertCommand(["rm", "-f", constants.WATCHER_STATEFILE])
71 def _RunWatcherDaemon():
72 """Runs the ganeti-watcher daemon on the master node.
75 AssertCommand(["ganeti-watcher", "-d", "--ignore-pause"])
78 def TestPauseWatcher():
79 """Tests and pauses the watcher.
82 master = qa_config.GetMasterNode()
84 AssertCommand(["gnt-cluster", "watcher", "pause", "4h"])
86 cmd = ["gnt-cluster", "watcher", "info"]
87 output = GetCommandOutput(master["primary"],
88 utils.ShellQuoteArgs(cmd))
89 AssertMatch(output, r"^.*\bis paused\b.*")
92 def TestResumeWatcher():
93 """Tests and unpauses the watcher.
96 master = qa_config.GetMasterNode()
98 AssertCommand(["gnt-cluster", "watcher", "continue"])
100 cmd = ["gnt-cluster", "watcher", "info"]
101 output = GetCommandOutput(master["primary"],
102 utils.ShellQuoteArgs(cmd))
103 AssertMatch(output, r"^.*\bis not paused\b.*")
106 def TestInstanceAutomaticRestart(instance):
107 """Test automatic restart of instance by ganeti-watcher.
110 inst_name = qa_utils.ResolveInstanceName(instance["name"])
112 _ResetWatcherDaemon()
113 _ShutdownInstance(inst_name)
118 if not _InstanceRunning(inst_name):
119 raise qa_error.Error("Daemon didn't restart instance")
121 AssertCommand(["gnt-instance", "info", inst_name])
124 def TestInstanceConsecutiveFailures(instance):
125 """Test five consecutive instance failures.
128 inst_name = qa_utils.ResolveInstanceName(instance["name"])
130 _ResetWatcherDaemon()
132 for should_start in ([True] * 5) + [False]:
133 _ShutdownInstance(inst_name)
137 if bool(_InstanceRunning(inst_name)) != should_start:
139 msg = "Instance not started when it should"
141 msg = "Instance started when it shouldn't"
142 raise qa_error.Error(msg)
144 AssertCommand(["gnt-instance", "info", inst_name])