Merge branch 'stable-2.6-hotplug' into stable-2.6-ippool-hotplug-esi
[ganeti-local] / qa / qa_daemon.py
1 #
2 #
3
4 # Copyright (C) 2007, 2008, 2009, 2010, 2011 Google Inc.
5 #
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.
10 #
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.
15 #
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
19 # 02110-1301, USA.
20
21
22 """Daemon related QA tests.
23
24 """
25
26 import time
27
28 from ganeti import utils
29 from ganeti import constants
30
31 import qa_config
32 import qa_utils
33 import qa_error
34
35 from qa_utils import AssertMatch, AssertCommand, StartSSH, GetCommandOutput
36
37
38 def _InstanceRunning(name):
39   """Checks whether an instance is running.
40
41   @param name: full name of the instance
42
43   """
44   master = qa_config.GetMasterNode()
45
46   cmd = (utils.ShellQuoteArgs(["gnt-instance", "list", "-o", "status", name]) +
47          ' | grep running')
48   ret = StartSSH(master["primary"], cmd).wait()
49   return ret == 0
50
51
52 def _ShutdownInstance(name):
53   """Shuts down instance without recording state and waits for completion.
54
55   @param name: full name of the instance
56
57   """
58   AssertCommand(["gnt-instance", "shutdown", "--no-remember", name])
59
60   if _InstanceRunning(name):
61     raise qa_error.Error("instance shutdown failed")
62
63
64 def _ResetWatcherDaemon():
65   """Removes the watcher daemon's state file.
66
67   """
68   AssertCommand([
69     "bash", "-c",
70     "rm -vf %s" % (constants.WATCHER_GROUP_STATE_FILE % "*-*-*-*")
71     ])
72
73
74 def _RunWatcherDaemon():
75   """Runs the ganeti-watcher daemon on the master node.
76
77   """
78   AssertCommand(["ganeti-watcher", "-d", "--ignore-pause", "--wait-children"])
79
80
81 def TestPauseWatcher():
82   """Tests and pauses the watcher.
83
84   """
85   master = qa_config.GetMasterNode()
86
87   AssertCommand(["gnt-cluster", "watcher", "pause", "4h"])
88
89   cmd = ["gnt-cluster", "watcher", "info"]
90   output = GetCommandOutput(master["primary"],
91                             utils.ShellQuoteArgs(cmd))
92   AssertMatch(output, r"^.*\bis paused\b.*")
93
94
95 def TestResumeWatcher():
96   """Tests and unpauses the watcher.
97
98   """
99   master = qa_config.GetMasterNode()
100
101   AssertCommand(["gnt-cluster", "watcher", "continue"])
102
103   cmd = ["gnt-cluster", "watcher", "info"]
104   output = GetCommandOutput(master["primary"],
105                             utils.ShellQuoteArgs(cmd))
106   AssertMatch(output, r"^.*\bis not paused\b.*")
107
108
109 def TestInstanceAutomaticRestart(instance):
110   """Test automatic restart of instance by ganeti-watcher.
111
112   """
113   inst_name = qa_utils.ResolveInstanceName(instance["name"])
114
115   _ResetWatcherDaemon()
116   _ShutdownInstance(inst_name)
117
118   _RunWatcherDaemon()
119   time.sleep(5)
120
121   if not _InstanceRunning(inst_name):
122     raise qa_error.Error("Daemon didn't restart instance")
123
124   AssertCommand(["gnt-instance", "info", inst_name])
125
126
127 def TestInstanceConsecutiveFailures(instance):
128   """Test five consecutive instance failures.
129
130   """
131   inst_name = qa_utils.ResolveInstanceName(instance["name"])
132
133   _ResetWatcherDaemon()
134
135   for should_start in ([True] * 5) + [False]:
136     _ShutdownInstance(inst_name)
137     _RunWatcherDaemon()
138     time.sleep(5)
139
140     if bool(_InstanceRunning(inst_name)) != should_start:
141       if should_start:
142         msg = "Instance not started when it should"
143       else:
144         msg = "Instance started when it shouldn't"
145       raise qa_error.Error(msg)
146
147   AssertCommand(["gnt-instance", "info", inst_name])