Statistics
| Branch: | Tag: | Revision:

root / qa / qa_daemon.py @ 91104b80

History | View | Annotate | Download (4 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
  AssertCommand([
81
    "bash", "-c",
82
    "rm -vf %s" % (pathutils.WATCHER_GROUP_STATE_FILE % "*-*-*-*"),
83
    ])
84

    
85

    
86
def _RunWatcherDaemon():
87
  """Runs the ganeti-watcher daemon on the master node.
88

89
  """
90
  AssertCommand(["ganeti-watcher", "-d", "--ignore-pause", "--wait-children"])
91

    
92

    
93
def TestPauseWatcher():
94
  """Tests and pauses the watcher.
95

96
  """
97
  master = qa_config.GetMasterNode()
98

    
99
  AssertCommand(["gnt-cluster", "watcher", "pause", "4h"])
100

    
101
  cmd = ["gnt-cluster", "watcher", "info"]
102
  output = GetCommandOutput(master["primary"],
103
                            utils.ShellQuoteArgs(cmd))
104
  AssertMatch(output, r"^.*\bis paused\b.*")
105

    
106

    
107
def TestResumeWatcher():
108
  """Tests and unpauses the watcher.
109

110
  """
111
  master = qa_config.GetMasterNode()
112

    
113
  AssertCommand(["gnt-cluster", "watcher", "continue"])
114

    
115
  cmd = ["gnt-cluster", "watcher", "info"]
116
  output = GetCommandOutput(master["primary"],
117
                            utils.ShellQuoteArgs(cmd))
118
  AssertMatch(output, r"^.*\bis not paused\b.*")
119

    
120

    
121
def TestInstanceAutomaticRestart(instance):
122
  """Test automatic restart of instance by ganeti-watcher.
123

124
  """
125
  inst_name = qa_utils.ResolveInstanceName(instance["name"])
126

    
127
  _ResetWatcherDaemon()
128
  _ShutdownInstance(inst_name)
129

    
130
  _RunWatcherDaemon()
131
  time.sleep(5)
132

    
133
  if not _InstanceRunning(inst_name):
134
    raise qa_error.Error("Daemon didn't restart instance")
135

    
136
  AssertCommand(["gnt-instance", "info", inst_name])
137

    
138

    
139
def TestInstanceConsecutiveFailures(instance):
140
  """Test five consecutive instance failures.
141

142
  """
143
  inst_name = qa_utils.ResolveInstanceName(instance["name"])
144
  inst_was_running = bool(_InstanceRunning(inst_name))
145

    
146
  _ResetWatcherDaemon()
147

    
148
  for should_start in ([True] * 5) + [False]:
149
    _ShutdownInstance(inst_name)
150
    _RunWatcherDaemon()
151
    time.sleep(5)
152

    
153
    if bool(_InstanceRunning(inst_name)) != should_start:
154
      if should_start:
155
        msg = "Instance not started when it should"
156
      else:
157
        msg = "Instance started when it shouldn't"
158
      raise qa_error.Error(msg)
159

    
160
  AssertCommand(["gnt-instance", "info", inst_name])
161

    
162
  if inst_was_running:
163
    _StartInstance(inst_name)