Simplify QA commands
[ganeti-local] / qa / qa_daemon.py
1 #
2 #
3
4 # Copyright (C) 2007, 2008, 2009, 2010 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(node, name):
39   """Checks whether an instance is running.
40
41   @param node: node the instance runs on
42   @param name: full name of the Xen instance
43
44   """
45   cmd = utils.ShellQuoteArgs(['xm', 'list', name]) + ' >/dev/null'
46   ret = StartSSH(node['primary'], cmd).wait()
47   return ret == 0
48
49
50 def _XmShutdownInstance(node, name):
51   """Shuts down instance using "xm" and waits for completion.
52
53   @param node: node the instance runs on
54   @param name: full name of Xen instance
55
56   """
57   AssertCommand(["xm", "shutdown", name], node=node)
58
59   # Wait up to a minute
60   end = time.time() + 60
61   while time.time() <= end:
62     if not _InstanceRunning(node, name):
63       break
64     time.sleep(5)
65   else:
66     raise qa_error.Error("xm shutdown failed")
67
68
69 def _ResetWatcherDaemon():
70   """Removes the watcher daemon's state file.
71
72   """
73   AssertCommand(["rm", "-f", constants.WATCHER_STATEFILE])
74
75
76 def _RunWatcherDaemon():
77   """Runs the ganeti-watcher daemon on the master node.
78
79   """
80   AssertCommand(["ganeti-watcher", "-d", "--ignore-pause"])
81
82
83 def TestPauseWatcher():
84   """Tests and pauses the watcher.
85
86   """
87   master = qa_config.GetMasterNode()
88
89   AssertCommand(["gnt-cluster", "watcher", "pause", "4h"])
90
91   cmd = ["gnt-cluster", "watcher", "info"]
92   output = GetCommandOutput(master["primary"],
93                             utils.ShellQuoteArgs(cmd))
94   AssertMatch(output, r"^.*\bis paused\b.*")
95
96
97 def TestResumeWatcher():
98   """Tests and unpauses the watcher.
99
100   """
101   master = qa_config.GetMasterNode()
102
103   AssertCommand(["gnt-cluster", "watcher", "continue"])
104
105   cmd = ["gnt-cluster", "watcher", "info"]
106   output = GetCommandOutput(master["primary"],
107                             utils.ShellQuoteArgs(cmd))
108   AssertMatch(output, r"^.*\bis not paused\b.*")
109
110
111 def TestInstanceAutomaticRestart(node, instance):
112   """Test automatic restart of instance by ganeti-watcher.
113
114   """
115   inst_name = qa_utils.ResolveInstanceName(instance["name"])
116
117   _ResetWatcherDaemon()
118   _XmShutdownInstance(node, inst_name)
119
120   _RunWatcherDaemon()
121   time.sleep(5)
122
123   if not _InstanceRunning(node, inst_name):
124     raise qa_error.Error("Daemon didn't restart instance")
125
126   AssertCommand(["gnt-instance", "info", inst_name])
127
128
129 def TestInstanceConsecutiveFailures(node, instance):
130   """Test five consecutive instance failures.
131
132   """
133   inst_name = qa_utils.ResolveInstanceName(instance["name"])
134
135   _ResetWatcherDaemon()
136
137   for should_start in ([True] * 5) + [False]:
138     _XmShutdownInstance(node, inst_name)
139     _RunWatcherDaemon()
140     time.sleep(5)
141
142     if bool(_InstanceRunning(node, inst_name)) != should_start:
143       if should_start:
144         msg = "Instance not started when it should"
145       else:
146         msg = "Instance started when it shouldn't"
147       raise qa_error.Error(msg)
148
149   AssertCommand(["gnt-instance", "info", inst_name])