Statistics
| Branch: | Tag: | Revision:

root / qa / qa_daemon.py @ 7910e7a5

History | View | Annotate | Download (3.9 kB)

1
#
2
#
3

    
4
# Copyright (C) 2007 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 AssertEqual, StartSSH
36

    
37

    
38
def _InstanceRunning(node, name):
39
  """Checks whether an instance is running.
40

41
  Args:
42
    node: Node the instance runs on
43
    name: Full name of Xen instance
44
  """
45
  cmd = utils.ShellQuoteArgs(['xm', 'list', name]) + ' >/dev/null'
46
  ret = StartSSH(node['primary'], cmd).wait()
47
  return ret == 0
48

    
49

    
50
def _XmShutdownInstance(node, name):
51
  """Shuts down instance using "xm" and waits for completion.
52

53
  Args:
54
    node: Node the instance runs on
55
    name: Full name of Xen instance
56
  """
57
  master = qa_config.GetMasterNode()
58

    
59
  cmd = ['xm', 'shutdown', name]
60
  AssertEqual(StartSSH(node['primary'],
61
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
62

    
63
  # Wait up to a minute
64
  end = time.time() + 60
65
  while time.time() <= end:
66
    if not _InstanceRunning(node, name):
67
      break
68
    time.sleep(5)
69
  else:
70
    raise qa_error.Error("xm shutdown failed")
71

    
72

    
73
def _ResetWatcherDaemon():
74
  """Removes the watcher daemon's state file.
75

76
  Args:
77
    node: Node to be reset
78
  """
79
  master = qa_config.GetMasterNode()
80

    
81
  cmd = ['rm', '-f', constants.WATCHER_STATEFILE]
82
  AssertEqual(StartSSH(master['primary'],
83
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
84

    
85

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

89
  """
90
  master = qa_config.GetMasterNode()
91

    
92
  cmd = ['ganeti-watcher', '-d']
93
  AssertEqual(StartSSH(master['primary'],
94
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
95

    
96

    
97
def PrintCronWarning():
98
  """Shows a warning about the cron job.
99

100
  """
101
  msg = ("For the following tests it's recommended to turn off the"
102
         " ganeti-watcher cronjob.")
103
  print
104
  print qa_utils.FormatWarning(msg)
105

    
106

    
107
@qa_utils.DefineHook('daemon-automatic-restart')
108
def TestInstanceAutomaticRestart(node, instance):
109
  """Test automatic restart of instance by ganeti-watcher.
110

111
  """
112
  master = qa_config.GetMasterNode()
113
  inst_name = qa_utils.ResolveInstanceName(instance)
114

    
115
  _ResetWatcherDaemon()
116
  _XmShutdownInstance(node, inst_name)
117

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

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

    
124
  cmd = ['gnt-instance', 'info', inst_name]
125
  AssertEqual(StartSSH(master['primary'],
126
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
127

    
128

    
129
@qa_utils.DefineHook('daemon-consecutive-failures')
130
def TestInstanceConsecutiveFailures(node, instance):
131
  """Test five consecutive instance failures.
132

133
  """
134
  master = qa_config.GetMasterNode()
135
  inst_name = qa_utils.ResolveInstanceName(instance)
136

    
137
  _ResetWatcherDaemon()
138

    
139
  for should_start in ([True] * 5) + [False]:
140
    _XmShutdownInstance(node, inst_name)
141
    _RunWatcherDaemon()
142
    time.sleep(5)
143

    
144
    if bool(_InstanceRunning(node, inst_name)) != should_start:
145
      if should_start:
146
        msg = "Instance not started when it should"
147
      else:
148
        msg = "Instance started when it shouldn't"
149
      raise qa_error.Error(msg)
150

    
151
  cmd = ['gnt-instance', 'info', inst_name]
152
  AssertEqual(StartSSH(master['primary'],
153
                       utils.ShellQuoteArgs(cmd)).wait(), 0)