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 _StartInstance(name):
65 """Starts instance and waits for completion.
67 @param name: full name of the instance
70 AssertCommand(["gnt-instance", "start", name])
72 if not bool(_InstanceRunning(name)):
73 raise qa_error.Error("instance start failed")
76 def _ResetWatcherDaemon():
77 """Removes the watcher daemon's state file.
81 qa_utils.MakeNodePath(qa_config.GetMasterNode(),
82 pathutils.WATCHER_GROUP_STATE_FILE % "*-*-*-*")
84 AssertCommand(["bash", "-c", "rm -vf %s" % path])
87 def _RunWatcherDaemon():
88 """Runs the ganeti-watcher daemon on the master node.
91 AssertCommand(["ganeti-watcher", "-d", "--ignore-pause", "--wait-children"])
94 def TestPauseWatcher():
95 """Tests and pauses the watcher.
98 master = qa_config.GetMasterNode()
100 AssertCommand(["gnt-cluster", "watcher", "pause", "4h"])
102 cmd = ["gnt-cluster", "watcher", "info"]
103 output = GetCommandOutput(master.primary,
104 utils.ShellQuoteArgs(cmd))
105 AssertMatch(output, r"^.*\bis paused\b.*")
108 def TestResumeWatcher():
109 """Tests and unpauses the watcher.
112 master = qa_config.GetMasterNode()
114 AssertCommand(["gnt-cluster", "watcher", "continue"])
116 cmd = ["gnt-cluster", "watcher", "info"]
117 output = GetCommandOutput(master.primary,
118 utils.ShellQuoteArgs(cmd))
119 AssertMatch(output, r"^.*\bis not paused\b.*")
122 def TestInstanceAutomaticRestart(instance):
123 """Test automatic restart of instance by ganeti-watcher.
126 inst_name = qa_utils.ResolveInstanceName(instance.name)
128 _ResetWatcherDaemon()
129 _ShutdownInstance(inst_name)
134 if not _InstanceRunning(inst_name):
135 raise qa_error.Error("Daemon didn't restart instance")
137 AssertCommand(["gnt-instance", "info", inst_name])
140 def TestInstanceConsecutiveFailures(instance):
141 """Test five consecutive instance failures.
144 inst_name = qa_utils.ResolveInstanceName(instance.name)
145 inst_was_running = bool(_InstanceRunning(inst_name))
147 _ResetWatcherDaemon()
149 for should_start in ([True] * 5) + [False]:
150 _ShutdownInstance(inst_name)
154 if bool(_InstanceRunning(inst_name)) != should_start:
156 msg = "Instance not started when it should"
158 msg = "Instance started when it shouldn't"
159 raise qa_error.Error(msg)
161 AssertCommand(["gnt-instance", "info", inst_name])
164 _StartInstance(inst_name)