Statistics
| Branch: | Tag: | Revision:

root / qa / qa_daemon.py @ 91104b80

History | View | Annotate | Download (4 kB)

1 c68d1f43 Michael Hanselmann
#
2 c68d1f43 Michael Hanselmann
#
3 c68d1f43 Michael Hanselmann
4 b998270c Iustin Pop
# Copyright (C) 2007, 2008, 2009, 2010, 2011 Google Inc.
5 cec9845c Michael Hanselmann
#
6 cec9845c Michael Hanselmann
# This program is free software; you can redistribute it and/or modify
7 cec9845c Michael Hanselmann
# it under the terms of the GNU General Public License as published by
8 cec9845c Michael Hanselmann
# the Free Software Foundation; either version 2 of the License, or
9 cec9845c Michael Hanselmann
# (at your option) any later version.
10 cec9845c Michael Hanselmann
#
11 cec9845c Michael Hanselmann
# This program is distributed in the hope that it will be useful, but
12 cec9845c Michael Hanselmann
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 cec9845c Michael Hanselmann
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 cec9845c Michael Hanselmann
# General Public License for more details.
15 cec9845c Michael Hanselmann
#
16 cec9845c Michael Hanselmann
# You should have received a copy of the GNU General Public License
17 cec9845c Michael Hanselmann
# along with this program; if not, write to the Free Software
18 cec9845c Michael Hanselmann
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 cec9845c Michael Hanselmann
# 02110-1301, USA.
20 cec9845c Michael Hanselmann
21 cec9845c Michael Hanselmann
22 cec9845c Michael Hanselmann
"""Daemon related QA tests.
23 cec9845c Michael Hanselmann

24 cec9845c Michael Hanselmann
"""
25 cec9845c Michael Hanselmann
26 cec9845c Michael Hanselmann
import time
27 cec9845c Michael Hanselmann
28 cec9845c Michael Hanselmann
from ganeti import utils
29 304d9f02 Michael Hanselmann
from ganeti import pathutils
30 cec9845c Michael Hanselmann
31 cec9845c Michael Hanselmann
import qa_config
32 cec9845c Michael Hanselmann
import qa_utils
33 cec9845c Michael Hanselmann
import qa_error
34 cec9845c Michael Hanselmann
35 2f4b4f78 Iustin Pop
from qa_utils import AssertMatch, AssertCommand, StartSSH, GetCommandOutput
36 cec9845c Michael Hanselmann
37 cec9845c Michael Hanselmann
38 b998270c Iustin Pop
def _InstanceRunning(name):
39 cec9845c Michael Hanselmann
  """Checks whether an instance is running.
40 cec9845c Michael Hanselmann

41 b998270c Iustin Pop
  @param name: full name of the instance
42 2f4b4f78 Iustin Pop

43 cec9845c Michael Hanselmann
  """
44 b998270c Iustin Pop
  master = qa_config.GetMasterNode()
45 b998270c Iustin Pop
46 b998270c Iustin Pop
  cmd = (utils.ShellQuoteArgs(["gnt-instance", "list", "-o", "status", name]) +
47 b998270c Iustin Pop
         ' | grep running')
48 b998270c Iustin Pop
  ret = StartSSH(master["primary"], cmd).wait()
49 cec9845c Michael Hanselmann
  return ret == 0
50 cec9845c Michael Hanselmann
51 cec9845c Michael Hanselmann
52 b998270c Iustin Pop
def _ShutdownInstance(name):
53 b998270c Iustin Pop
  """Shuts down instance without recording state and waits for completion.
54 cec9845c Michael Hanselmann

55 b998270c Iustin Pop
  @param name: full name of the instance
56 cec9845c Michael Hanselmann

57 2f4b4f78 Iustin Pop
  """
58 b998270c Iustin Pop
  AssertCommand(["gnt-instance", "shutdown", "--no-remember", name])
59 cec9845c Michael Hanselmann
60 b998270c Iustin Pop
  if _InstanceRunning(name):
61 b998270c Iustin Pop
    raise qa_error.Error("instance shutdown failed")
62 cec9845c Michael Hanselmann
63 cec9845c Michael Hanselmann
64 91104b80 Thomas Thrainer
def _StartInstance(name):
65 91104b80 Thomas Thrainer
  """Starts instance and waits for completion.
66 91104b80 Thomas Thrainer

67 91104b80 Thomas Thrainer
  @param name: full name of the instance
68 91104b80 Thomas Thrainer

69 91104b80 Thomas Thrainer
  """
70 91104b80 Thomas Thrainer
  AssertCommand(["gnt-instance", "start", name])
71 91104b80 Thomas Thrainer
72 91104b80 Thomas Thrainer
  if not bool(_InstanceRunning(name)):
73 91104b80 Thomas Thrainer
    raise qa_error.Error("instance start failed")
74 91104b80 Thomas Thrainer
75 91104b80 Thomas Thrainer
76 28a6fbc8 Michael Hanselmann
def _ResetWatcherDaemon():
77 cec9845c Michael Hanselmann
  """Removes the watcher daemon's state file.
78 cec9845c Michael Hanselmann

79 cec9845c Michael Hanselmann
  """
80 9d7b1e53 Michael Hanselmann
  AssertCommand([
81 9d7b1e53 Michael Hanselmann
    "bash", "-c",
82 3c286190 Dimitris Aragiorgis
    "rm -vf %s" % (pathutils.WATCHER_GROUP_STATE_FILE % "*-*-*-*"),
83 9d7b1e53 Michael Hanselmann
    ])
84 28a6fbc8 Michael Hanselmann
85 28a6fbc8 Michael Hanselmann
86 28a6fbc8 Michael Hanselmann
def _RunWatcherDaemon():
87 28a6fbc8 Michael Hanselmann
  """Runs the ganeti-watcher daemon on the master node.
88 28a6fbc8 Michael Hanselmann

89 28a6fbc8 Michael Hanselmann
  """
90 16e0b9c9 Michael Hanselmann
  AssertCommand(["ganeti-watcher", "-d", "--ignore-pause", "--wait-children"])
91 cec9845c Michael Hanselmann
92 cec9845c Michael Hanselmann
93 8201b996 Iustin Pop
def TestPauseWatcher():
94 8201b996 Iustin Pop
  """Tests and pauses the watcher.
95 23269c5b Michael Hanselmann

96 23269c5b Michael Hanselmann
  """
97 8201b996 Iustin Pop
  master = qa_config.GetMasterNode()
98 8201b996 Iustin Pop
99 2f4b4f78 Iustin Pop
  AssertCommand(["gnt-cluster", "watcher", "pause", "4h"])
100 8201b996 Iustin Pop
101 8201b996 Iustin Pop
  cmd = ["gnt-cluster", "watcher", "info"]
102 8201b996 Iustin Pop
  output = GetCommandOutput(master["primary"],
103 8201b996 Iustin Pop
                            utils.ShellQuoteArgs(cmd))
104 8201b996 Iustin Pop
  AssertMatch(output, r"^.*\bis paused\b.*")
105 8201b996 Iustin Pop
106 8201b996 Iustin Pop
107 8201b996 Iustin Pop
def TestResumeWatcher():
108 8201b996 Iustin Pop
  """Tests and unpauses the watcher.
109 8201b996 Iustin Pop

110 8201b996 Iustin Pop
  """
111 8201b996 Iustin Pop
  master = qa_config.GetMasterNode()
112 8201b996 Iustin Pop
113 2f4b4f78 Iustin Pop
  AssertCommand(["gnt-cluster", "watcher", "continue"])
114 8201b996 Iustin Pop
115 8201b996 Iustin Pop
  cmd = ["gnt-cluster", "watcher", "info"]
116 8201b996 Iustin Pop
  output = GetCommandOutput(master["primary"],
117 8201b996 Iustin Pop
                            utils.ShellQuoteArgs(cmd))
118 8201b996 Iustin Pop
  AssertMatch(output, r"^.*\bis not paused\b.*")
119 23269c5b Michael Hanselmann
120 23269c5b Michael Hanselmann
121 b998270c Iustin Pop
def TestInstanceAutomaticRestart(instance):
122 cec9845c Michael Hanselmann
  """Test automatic restart of instance by ganeti-watcher.
123 cec9845c Michael Hanselmann

124 cec9845c Michael Hanselmann
  """
125 46f9a948 Michael Hanselmann
  inst_name = qa_utils.ResolveInstanceName(instance["name"])
126 cec9845c Michael Hanselmann
127 28a6fbc8 Michael Hanselmann
  _ResetWatcherDaemon()
128 b998270c Iustin Pop
  _ShutdownInstance(inst_name)
129 cec9845c Michael Hanselmann
130 28a6fbc8 Michael Hanselmann
  _RunWatcherDaemon()
131 28a6fbc8 Michael Hanselmann
  time.sleep(5)
132 cec9845c Michael Hanselmann
133 b998270c Iustin Pop
  if not _InstanceRunning(inst_name):
134 28a6fbc8 Michael Hanselmann
    raise qa_error.Error("Daemon didn't restart instance")
135 cec9845c Michael Hanselmann
136 2f4b4f78 Iustin Pop
  AssertCommand(["gnt-instance", "info", inst_name])
137 cec9845c Michael Hanselmann
138 cec9845c Michael Hanselmann
139 b998270c Iustin Pop
def TestInstanceConsecutiveFailures(instance):
140 cec9845c Michael Hanselmann
  """Test five consecutive instance failures.
141 cec9845c Michael Hanselmann

142 cec9845c Michael Hanselmann
  """
143 46f9a948 Michael Hanselmann
  inst_name = qa_utils.ResolveInstanceName(instance["name"])
144 91104b80 Thomas Thrainer
  inst_was_running = bool(_InstanceRunning(inst_name))
145 cec9845c Michael Hanselmann
146 28a6fbc8 Michael Hanselmann
  _ResetWatcherDaemon()
147 cec9845c Michael Hanselmann
148 28a6fbc8 Michael Hanselmann
  for should_start in ([True] * 5) + [False]:
149 b998270c Iustin Pop
    _ShutdownInstance(inst_name)
150 28a6fbc8 Michael Hanselmann
    _RunWatcherDaemon()
151 28a6fbc8 Michael Hanselmann
    time.sleep(5)
152 cec9845c Michael Hanselmann
153 b998270c Iustin Pop
    if bool(_InstanceRunning(inst_name)) != should_start:
154 28a6fbc8 Michael Hanselmann
      if should_start:
155 28a6fbc8 Michael Hanselmann
        msg = "Instance not started when it should"
156 28a6fbc8 Michael Hanselmann
      else:
157 28a6fbc8 Michael Hanselmann
        msg = "Instance started when it shouldn't"
158 28a6fbc8 Michael Hanselmann
      raise qa_error.Error(msg)
159 cec9845c Michael Hanselmann
160 2f4b4f78 Iustin Pop
  AssertCommand(["gnt-instance", "info", inst_name])
161 91104b80 Thomas Thrainer
162 91104b80 Thomas Thrainer
  if inst_was_running:
163 91104b80 Thomas Thrainer
    _StartInstance(inst_name)