Statistics
| Branch: | Tag: | Revision:

root / qa / qa_daemon.py @ 96a12113

History | View | Annotate | Download (3.6 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

    
70

    
71
def _RunWatcherDaemon():
72
  """Runs the ganeti-watcher daemon on the master node.
73

74
  """
75
  AssertCommand(["ganeti-watcher", "-d", "--ignore-pause"])
76

    
77

    
78
def TestPauseWatcher():
79
  """Tests and pauses the watcher.
80

81
  """
82
  master = qa_config.GetMasterNode()
83

    
84
  AssertCommand(["gnt-cluster", "watcher", "pause", "4h"])
85

    
86
  cmd = ["gnt-cluster", "watcher", "info"]
87
  output = GetCommandOutput(master["primary"],
88
                            utils.ShellQuoteArgs(cmd))
89
  AssertMatch(output, r"^.*\bis paused\b.*")
90

    
91

    
92
def TestResumeWatcher():
93
  """Tests and unpauses the watcher.
94

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

    
98
  AssertCommand(["gnt-cluster", "watcher", "continue"])
99

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

    
105

    
106
def TestInstanceAutomaticRestart(instance):
107
  """Test automatic restart of instance by ganeti-watcher.
108

109
  """
110
  inst_name = qa_utils.ResolveInstanceName(instance["name"])
111

    
112
  _ResetWatcherDaemon()
113
  _ShutdownInstance(inst_name)
114

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

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

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

    
123

    
124
def TestInstanceConsecutiveFailures(instance):
125
  """Test five consecutive instance failures.
126

127
  """
128
  inst_name = qa_utils.ResolveInstanceName(instance["name"])
129

    
130
  _ResetWatcherDaemon()
131

    
132
  for should_start in ([True] * 5) + [False]:
133
    _ShutdownInstance(inst_name)
134
    _RunWatcherDaemon()
135
    time.sleep(5)
136

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

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