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 pathutils
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.
69 qa_utils.MakeNodePath(qa_config.GetMasterNode(),
70 pathutils.WATCHER_GROUP_STATE_FILE % "*-*-*-*")
72 AssertCommand(["bash", "-c", "rm -vf %s" % path])
75 def _RunWatcherDaemon():
76 """Runs the ganeti-watcher daemon on the master node.
79 AssertCommand(["ganeti-watcher", "-d", "--ignore-pause", "--wait-children"])
82 def TestPauseWatcher():
83 """Tests and pauses the watcher.
86 master = qa_config.GetMasterNode()
88 AssertCommand(["gnt-cluster", "watcher", "pause", "4h"])
90 cmd = ["gnt-cluster", "watcher", "info"]
91 output = GetCommandOutput(master.primary,
92 utils.ShellQuoteArgs(cmd))
93 AssertMatch(output, r"^.*\bis paused\b.*")
96 def TestResumeWatcher():
97 """Tests and unpauses the watcher.
100 master = qa_config.GetMasterNode()
102 AssertCommand(["gnt-cluster", "watcher", "continue"])
104 cmd = ["gnt-cluster", "watcher", "info"]
105 output = GetCommandOutput(master.primary,
106 utils.ShellQuoteArgs(cmd))
107 AssertMatch(output, r"^.*\bis not paused\b.*")
110 def TestInstanceAutomaticRestart(instance):
111 """Test automatic restart of instance by ganeti-watcher.
114 inst_name = qa_utils.ResolveInstanceName(instance.name)
116 _ResetWatcherDaemon()
117 _ShutdownInstance(inst_name)
122 if not _InstanceRunning(inst_name):
123 raise qa_error.Error("Daemon didn't restart instance")
125 AssertCommand(["gnt-instance", "info", inst_name])
128 def TestInstanceConsecutiveFailures(instance):
129 """Test five consecutive instance failures.
132 inst_name = qa_utils.ResolveInstanceName(instance.name)
134 _ResetWatcherDaemon()
136 for should_start in ([True] * 5) + [False]:
137 _ShutdownInstance(inst_name)
141 if bool(_InstanceRunning(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 AssertCommand(["gnt-instance", "info", inst_name])