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.
70 "rm -vf %s" % (pathutils.WATCHER_GROUP_STATE_FILE % "*-*-*-*"),
74 def _RunWatcherDaemon():
75 """Runs the ganeti-watcher daemon on the master node.
78 AssertCommand(["ganeti-watcher", "-d", "--ignore-pause", "--wait-children"])
81 def TestPauseWatcher():
82 """Tests and pauses the watcher.
85 master = qa_config.GetMasterNode()
87 AssertCommand(["gnt-cluster", "watcher", "pause", "4h"])
89 cmd = ["gnt-cluster", "watcher", "info"]
90 output = GetCommandOutput(master["primary"],
91 utils.ShellQuoteArgs(cmd))
92 AssertMatch(output, r"^.*\bis paused\b.*")
95 def TestResumeWatcher():
96 """Tests and unpauses the watcher.
99 master = qa_config.GetMasterNode()
101 AssertCommand(["gnt-cluster", "watcher", "continue"])
103 cmd = ["gnt-cluster", "watcher", "info"]
104 output = GetCommandOutput(master["primary"],
105 utils.ShellQuoteArgs(cmd))
106 AssertMatch(output, r"^.*\bis not paused\b.*")
109 def TestInstanceAutomaticRestart(instance):
110 """Test automatic restart of instance by ganeti-watcher.
113 inst_name = qa_utils.ResolveInstanceName(instance["name"])
115 _ResetWatcherDaemon()
116 _ShutdownInstance(inst_name)
121 if not _InstanceRunning(inst_name):
122 raise qa_error.Error("Daemon didn't restart instance")
124 AssertCommand(["gnt-instance", "info", inst_name])
127 def TestInstanceConsecutiveFailures(instance):
128 """Test five consecutive instance failures.
131 inst_name = qa_utils.ResolveInstanceName(instance["name"])
133 _ResetWatcherDaemon()
135 for should_start in ([True] * 5) + [False]:
136 _ShutdownInstance(inst_name)
140 if bool(_InstanceRunning(inst_name)) != should_start:
142 msg = "Instance not started when it should"
144 msg = "Instance started when it shouldn't"
145 raise qa_error.Error(msg)
147 AssertCommand(["gnt-instance", "info", inst_name])