Statistics
| Branch: | Tag: | Revision:

root / qa / qa_daemon.py @ b780c231

History | View | Annotate | Download (4.1 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 aecba21e Michael Hanselmann
  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 ea0d8b70 Michael Hanselmann
  path = \
81 ea0d8b70 Michael Hanselmann
    qa_utils.MakeNodePath(qa_config.GetMasterNode(),
82 ea0d8b70 Michael Hanselmann
                          pathutils.WATCHER_GROUP_STATE_FILE % "*-*-*-*")
83 ea0d8b70 Michael Hanselmann
84 ea0d8b70 Michael Hanselmann
  AssertCommand(["bash", "-c", "rm -vf %s" % path])
85 28a6fbc8 Michael Hanselmann
86 28a6fbc8 Michael Hanselmann
87 28a6fbc8 Michael Hanselmann
def _RunWatcherDaemon():
88 28a6fbc8 Michael Hanselmann
  """Runs the ganeti-watcher daemon on the master node.
89 28a6fbc8 Michael Hanselmann

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

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

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

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

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