Statistics
| Branch: | Tag: | Revision:

root / qa / qa_daemon.py @ 9d7b1e53

History | View | Annotate | Download (3.7 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 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(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 _ResetWatcherDaemon():
65
  """Removes the watcher daemon's state file.
66

67
  """
68
  AssertCommand(["rm", "-f", constants.WATCHER_STATEFILE])
69
  AssertCommand([
70
    "bash", "-c",
71
    "rm -vf %s" % (constants.WATCHER_GROUP_STATE_FILE % "*-*-*-*")
72
    ])
73

    
74

    
75
def _RunWatcherDaemon():
76
  """Runs the ganeti-watcher daemon on the master node.
77

78
  """
79
  AssertCommand(["ganeti-watcher", "-d", "--ignore-pause"])
80

    
81

    
82
def TestPauseWatcher():
83
  """Tests and pauses the watcher.
84

85
  """
86
  master = qa_config.GetMasterNode()
87

    
88
  AssertCommand(["gnt-cluster", "watcher", "pause", "4h"])
89

    
90
  cmd = ["gnt-cluster", "watcher", "info"]
91
  output = GetCommandOutput(master["primary"],
92
                            utils.ShellQuoteArgs(cmd))
93
  AssertMatch(output, r"^.*\bis paused\b.*")
94

    
95

    
96
def TestResumeWatcher():
97
  """Tests and unpauses the watcher.
98

99
  """
100
  master = qa_config.GetMasterNode()
101

    
102
  AssertCommand(["gnt-cluster", "watcher", "continue"])
103

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

    
109

    
110
def TestInstanceAutomaticRestart(instance):
111
  """Test automatic restart of instance by ganeti-watcher.
112

113
  """
114
  inst_name = qa_utils.ResolveInstanceName(instance["name"])
115

    
116
  _ResetWatcherDaemon()
117
  _ShutdownInstance(inst_name)
118

    
119
  _RunWatcherDaemon()
120
  time.sleep(5)
121

    
122
  if not _InstanceRunning(inst_name):
123
    raise qa_error.Error("Daemon didn't restart instance")
124

    
125
  AssertCommand(["gnt-instance", "info", inst_name])
126

    
127

    
128
def TestInstanceConsecutiveFailures(instance):
129
  """Test five consecutive instance failures.
130

131
  """
132
  inst_name = qa_utils.ResolveInstanceName(instance["name"])
133

    
134
  _ResetWatcherDaemon()
135

    
136
  for should_start in ([True] * 5) + [False]:
137
    _ShutdownInstance(inst_name)
138
    _RunWatcherDaemon()
139
    time.sleep(5)
140

    
141
    if bool(_InstanceRunning(inst_name)) != should_start:
142
      if should_start:
143
        msg = "Instance not started when it should"
144
      else:
145
        msg = "Instance started when it shouldn't"
146
      raise qa_error.Error(msg)
147

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