Statistics
| Branch: | Tag: | Revision:

root / qa / qa_daemon.py @ 28a6fbc8

History | View | Annotate | Download (3.8 kB)

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

    
18

    
19
"""Daemon related QA tests.
20

21
"""
22

    
23
import time
24

    
25
from ganeti import utils
26
from ganeti import constants
27

    
28
import qa_config
29
import qa_utils
30
import qa_error
31

    
32
from qa_utils import AssertEqual, StartSSH
33

    
34

    
35
def _InstanceRunning(node, name):
36
  """Checks whether an instance is running.
37

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

    
46

    
47
def _XmShutdownInstance(node, name):
48
  """Shuts down instance using "xm" and waits for completion.
49

50
  Args:
51
    node: Node the instance runs on
52
    name: Full name of Xen instance
53
  """
54
  master = qa_config.GetMasterNode()
55

    
56
  cmd = ['xm', 'shutdown', name]
57
  AssertEqual(StartSSH(node['primary'],
58
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
59

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

    
69

    
70
def _ResetWatcherDaemon():
71
  """Removes the watcher daemon's state file.
72

73
  Args:
74
    node: Node to be reset
75
  """
76
  master = qa_config.GetMasterNode()
77

    
78
  cmd = ['rm', '-f', constants.WATCHER_STATEFILE]
79
  AssertEqual(StartSSH(master['primary'],
80
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
81

    
82

    
83
def _RunWatcherDaemon():
84
  """Runs the ganeti-watcher daemon on the master node.
85

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

    
89
  cmd = ['ganeti-watcher', '-d']
90
  AssertEqual(StartSSH(master['primary'],
91
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
92

    
93

    
94
def PrintCronWarning():
95
  """Shows a warning about the cron job.
96

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

    
103

    
104
def TestInstanceAutomaticRestart(node, instance):
105
  """Test automatic restart of instance by ganeti-watcher.
106

107
  """
108
  master = qa_config.GetMasterNode()
109
  inst_name = qa_utils.ResolveInstanceName(instance)
110

    
111
  _ResetWatcherDaemon()
112
  _XmShutdownInstance(node, inst_name)
113

    
114
  _RunWatcherDaemon()
115
  time.sleep(5)
116

    
117
  if not _InstanceRunning(node, inst_name):
118
    raise qa_error.Error("Daemon didn't restart instance")
119

    
120
  cmd = ['gnt-instance', 'info', inst_name]
121
  AssertEqual(StartSSH(master['primary'],
122
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
123

    
124

    
125
def TestInstanceConsecutiveFailures(node, instance):
126
  """Test five consecutive instance failures.
127

128
  """
129
  master = qa_config.GetMasterNode()
130
  inst_name = qa_utils.ResolveInstanceName(instance)
131

    
132
  _ResetWatcherDaemon()
133

    
134
  for should_start in ([True] * 5) + [False]:
135
    _XmShutdownInstance(node, inst_name)
136
    _RunWatcherDaemon()
137
    time.sleep(5)
138

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

    
146
  cmd = ['gnt-instance', 'info', inst_name]
147
  AssertEqual(StartSSH(master['primary'],
148
                       utils.ShellQuoteArgs(cmd)).wait(), 0)