Statistics
| Branch: | Tag: | Revision:

root / qa / qa_daemon.py @ cec9845c

History | View | Annotate | Download (4.2 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
import subprocess
25

    
26
from ganeti import utils
27
from ganeti import constants
28

    
29
import qa_config
30
import qa_utils
31
import qa_error
32

    
33
from qa_utils import AssertEqual, StartSSH
34

    
35

    
36
def _ResolveInstanceName(instance):
37
  """Gets the full Xen name of an instance.
38

39
  """
40
  master = qa_config.GetMasterNode()
41

    
42
  info_cmd = utils.ShellQuoteArgs(['gnt-instance', 'info', instance['name']])
43
  sed_cmd = utils.ShellQuoteArgs(['sed', '-n', '-e', 's/^Instance name: *//p'])
44

    
45
  cmd = '%s | %s' % (info_cmd, sed_cmd)
46
  ssh_cmd = qa_utils.GetSSHCommand(master['primary'], cmd)
47
  p = subprocess.Popen(ssh_cmd, shell=False, stdout=subprocess.PIPE)
48
  AssertEqual(p.wait(), 0)
49

    
50
  return p.stdout.read().strip()
51

    
52

    
53
def _InstanceRunning(node, name):
54
  """Checks whether an instance is running.
55

56
  Args:
57
    node: Node the instance runs on
58
    name: Full name of Xen instance
59
  """
60
  cmd = utils.ShellQuoteArgs(['xm', 'list', name]) + ' >/dev/null'
61
  ret = StartSSH(node['primary'], cmd).wait()
62
  return ret == 0
63

    
64

    
65
def _XmShutdownInstance(node, name):
66
  """Shuts down instance using "xm" and waits for completion.
67

68
  Args:
69
    node: Node the instance runs on
70
    name: Full name of Xen instance
71
  """
72
  master = qa_config.GetMasterNode()
73

    
74
  cmd = ['xm', 'shutdown', name]
75
  AssertEqual(StartSSH(master['primary'],
76
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
77

    
78
  # Wait up to a minute
79
  end = time.time() + 60
80
  while time.time() <= end:
81
    if not _InstanceRunning(node, name):
82
      break
83
    time.sleep(5)
84
  else:
85
    raise qa_error.Error("xm shutdown failed")
86

    
87

    
88
def _ResetWatcherDaemon(node):
89
  """Removes the watcher daemon's state file.
90

91
  Args:
92
    node: Node to be reset
93
  """
94
  cmd = ['rm', '-f', constants.WATCHER_STATEFILE]
95
  AssertEqual(StartSSH(node['primary'],
96
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
97

    
98

    
99
def TestInstanceAutomaticRestart(node, instance):
100
  """Test automatic restart of instance by ganeti-watcher.
101

102
  Note: takes up to 6 minutes to complete.
103
  """
104
  master = qa_config.GetMasterNode()
105
  inst_name = _ResolveInstanceName(instance)
106

    
107
  _ResetWatcherDaemon(node)
108
  _XmShutdownInstance(node, inst_name)
109

    
110
  # Give it a bit more than five minutes to start again
111
  restart_at = time.time() + 330
112

    
113
  # Wait until it's running again
114
  while time.time() <= restart_at:
115
    if _InstanceRunning(node, inst_name):
116
      break
117
    time.sleep(15)
118
  else:
119
    raise qa_error.Error("Daemon didn't restart instance in time")
120

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

    
125

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

129
  Note: takes at least 35 minutes to complete.
130
  """
131
  master = qa_config.GetMasterNode()
132
  inst_name = _ResolveInstanceName(instance)
133

    
134
  _ResetWatcherDaemon(node)
135
  _XmShutdownInstance(node, inst_name)
136

    
137
  # Do shutdowns for 30 minutes
138
  finished_at = time.time() + (35 * 60)
139

    
140
  while time.time() <= finished_at:
141
    if _InstanceRunning(node, inst_name):
142
      _XmShutdownInstance(node, inst_name)
143
    time.sleep(30)
144

    
145
  # Check for some time whether the instance doesn't start again
146
  check_until = time.time() + 330
147
  while time.time() <= check_until:
148
    if _InstanceRunning(node, inst_name):
149
      raise qa_error.Error("Instance started when it shouldn't")
150
    time.sleep(30)
151

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