root / qa / qa_daemon.py @ 11e90588
History | View | Annotate | Download (4.1 kB)
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 pathutils |
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 _StartInstance(name): |
65 |
"""Starts instance and waits for completion.
|
66 |
|
67 |
@param name: full name of the instance
|
68 |
|
69 |
"""
|
70 |
AssertCommand(["gnt-instance", "start", name]) |
71 |
|
72 |
if not bool(_InstanceRunning(name)): |
73 |
raise qa_error.Error("instance start failed") |
74 |
|
75 |
|
76 |
def _ResetWatcherDaemon(): |
77 |
"""Removes the watcher daemon's state file.
|
78 |
|
79 |
"""
|
80 |
path = \ |
81 |
qa_utils.MakeNodePath(qa_config.GetMasterNode(), |
82 |
pathutils.WATCHER_GROUP_STATE_FILE % "*-*-*-*")
|
83 |
|
84 |
AssertCommand(["bash", "-c", "rm -vf %s" % path]) |
85 |
|
86 |
|
87 |
def _RunWatcherDaemon(): |
88 |
"""Runs the ganeti-watcher daemon on the master node.
|
89 |
|
90 |
"""
|
91 |
AssertCommand(["ganeti-watcher", "-d", "--ignore-pause", "--wait-children"]) |
92 |
|
93 |
|
94 |
def TestPauseWatcher(): |
95 |
"""Tests and pauses the watcher.
|
96 |
|
97 |
"""
|
98 |
master = qa_config.GetMasterNode() |
99 |
|
100 |
AssertCommand(["gnt-cluster", "watcher", "pause", "4h"]) |
101 |
|
102 |
cmd = ["gnt-cluster", "watcher", "info"] |
103 |
output = GetCommandOutput(master.primary, |
104 |
utils.ShellQuoteArgs(cmd)) |
105 |
AssertMatch(output, r"^.*\bis paused\b.*")
|
106 |
|
107 |
|
108 |
def TestResumeWatcher(): |
109 |
"""Tests and unpauses the watcher.
|
110 |
|
111 |
"""
|
112 |
master = qa_config.GetMasterNode() |
113 |
|
114 |
AssertCommand(["gnt-cluster", "watcher", "continue"]) |
115 |
|
116 |
cmd = ["gnt-cluster", "watcher", "info"] |
117 |
output = GetCommandOutput(master.primary, |
118 |
utils.ShellQuoteArgs(cmd)) |
119 |
AssertMatch(output, r"^.*\bis not paused\b.*")
|
120 |
|
121 |
|
122 |
def TestInstanceAutomaticRestart(instance): |
123 |
"""Test automatic restart of instance by ganeti-watcher.
|
124 |
|
125 |
"""
|
126 |
inst_name = qa_utils.ResolveInstanceName(instance.name) |
127 |
|
128 |
_ResetWatcherDaemon() |
129 |
_ShutdownInstance(inst_name) |
130 |
|
131 |
_RunWatcherDaemon() |
132 |
time.sleep(5)
|
133 |
|
134 |
if not _InstanceRunning(inst_name): |
135 |
raise qa_error.Error("Daemon didn't restart instance") |
136 |
|
137 |
AssertCommand(["gnt-instance", "info", inst_name]) |
138 |
|
139 |
|
140 |
def TestInstanceConsecutiveFailures(instance): |
141 |
"""Test five consecutive instance failures.
|
142 |
|
143 |
"""
|
144 |
inst_name = qa_utils.ResolveInstanceName(instance.name) |
145 |
inst_was_running = bool(_InstanceRunning(inst_name))
|
146 |
|
147 |
_ResetWatcherDaemon() |
148 |
|
149 |
for should_start in ([True] * 5) + [False]: |
150 |
_ShutdownInstance(inst_name) |
151 |
_RunWatcherDaemon() |
152 |
time.sleep(5)
|
153 |
|
154 |
if bool(_InstanceRunning(inst_name)) != should_start: |
155 |
if should_start:
|
156 |
msg = "Instance not started when it should"
|
157 |
else:
|
158 |
msg = "Instance started when it shouldn't"
|
159 |
raise qa_error.Error(msg)
|
160 |
|
161 |
AssertCommand(["gnt-instance", "info", inst_name]) |
162 |
|
163 |
if inst_was_running:
|
164 |
_StartInstance(inst_name) |