Statistics
| Branch: | Tag: | Revision:

root / test / py / cmdlib / testsupport / processor_mock.py @ bd39b6bb

History | View | Annotate | Download (4.3 kB)

1 3efa7659 Thomas Thrainer
#
2 3efa7659 Thomas Thrainer
#
3 3efa7659 Thomas Thrainer
4 3efa7659 Thomas Thrainer
# Copyright (C) 2013 Google Inc.
5 3efa7659 Thomas Thrainer
#
6 3efa7659 Thomas Thrainer
# This program is free software; you can redistribute it and/or modify
7 3efa7659 Thomas Thrainer
# it under the terms of the GNU General Public License as published by
8 3efa7659 Thomas Thrainer
# the Free Software Foundation; either version 2 of the License, or
9 3efa7659 Thomas Thrainer
# (at your option) any later version.
10 3efa7659 Thomas Thrainer
#
11 3efa7659 Thomas Thrainer
# This program is distributed in the hope that it will be useful, but
12 3efa7659 Thomas Thrainer
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 3efa7659 Thomas Thrainer
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 3efa7659 Thomas Thrainer
# General Public License for more details.
15 3efa7659 Thomas Thrainer
#
16 3efa7659 Thomas Thrainer
# You should have received a copy of the GNU General Public License
17 3efa7659 Thomas Thrainer
# along with this program; if not, write to the Free Software
18 3efa7659 Thomas Thrainer
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 3efa7659 Thomas Thrainer
# 02110-1301, USA.
20 3efa7659 Thomas Thrainer
21 bd39b6bb Thomas Thrainer
22 bd39b6bb Thomas Thrainer
"""Support for mocking the opcode processor"""
23 bd39b6bb Thomas Thrainer
24 bd39b6bb Thomas Thrainer
25 3efa7659 Thomas Thrainer
import re
26 3efa7659 Thomas Thrainer
27 3efa7659 Thomas Thrainer
from ganeti import constants
28 3efa7659 Thomas Thrainer
from ganeti import mcpu
29 3efa7659 Thomas Thrainer
30 3efa7659 Thomas Thrainer
31 3efa7659 Thomas Thrainer
class LogRecordingCallback(mcpu.OpExecCbBase):
32 3efa7659 Thomas Thrainer
  """Helper class for log output recording.
33 3efa7659 Thomas Thrainer

34 3efa7659 Thomas Thrainer
  """
35 3efa7659 Thomas Thrainer
  def __init__(self, processor):
36 3efa7659 Thomas Thrainer
    self.processor = processor
37 3efa7659 Thomas Thrainer
38 3efa7659 Thomas Thrainer
  def Feedback(self, *args):
39 3efa7659 Thomas Thrainer
    assert len(args) < 3
40 3efa7659 Thomas Thrainer
41 3efa7659 Thomas Thrainer
    if len(args) == 1:
42 3efa7659 Thomas Thrainer
      log_type = constants.ELOG_MESSAGE
43 3efa7659 Thomas Thrainer
      log_msg = args[0]
44 3efa7659 Thomas Thrainer
    else:
45 3efa7659 Thomas Thrainer
      (log_type, log_msg) = args
46 3efa7659 Thomas Thrainer
47 3efa7659 Thomas Thrainer
    self.processor.log_entries.append((log_type, log_msg))
48 3efa7659 Thomas Thrainer
49 bd39b6bb Thomas Thrainer
  def SubmitManyJobs(self, jobs):
50 bd39b6bb Thomas Thrainer
    return mcpu.OpExecCbBase.SubmitManyJobs(self, jobs)
51 bd39b6bb Thomas Thrainer
52 3efa7659 Thomas Thrainer
53 3efa7659 Thomas Thrainer
class ProcessorMock(mcpu.Processor):
54 3efa7659 Thomas Thrainer
  """Mocked opcode processor for tests.
55 3efa7659 Thomas Thrainer

56 3efa7659 Thomas Thrainer
  This class actually performs much more than a mock, as it drives the
57 3efa7659 Thomas Thrainer
  execution of LU's. But it also provides access to the log output of the LU
58 3efa7659 Thomas Thrainer
  the result of the execution.
59 3efa7659 Thomas Thrainer

60 3efa7659 Thomas Thrainer
  See L{ExecOpCodeAndRecordOutput} for the main method of this class.
61 3efa7659 Thomas Thrainer

62 3efa7659 Thomas Thrainer
  """
63 3efa7659 Thomas Thrainer
64 3efa7659 Thomas Thrainer
  def __init__(self, context):
65 3efa7659 Thomas Thrainer
    super(ProcessorMock, self).__init__(context, 0, True)
66 3efa7659 Thomas Thrainer
    self.log_entries = []
67 3efa7659 Thomas Thrainer
68 3efa7659 Thomas Thrainer
  def ExecOpCodeAndRecordOutput(self, op):
69 3efa7659 Thomas Thrainer
    """Executes the given opcode and records the output for further inspection.
70 3efa7659 Thomas Thrainer

71 3efa7659 Thomas Thrainer
    @param op: the opcode to execute.
72 3efa7659 Thomas Thrainer
    @return: see L{mcpu.Processor.ExecOpCode}
73 3efa7659 Thomas Thrainer

74 3efa7659 Thomas Thrainer
    """
75 3efa7659 Thomas Thrainer
    return self.ExecOpCode(op, LogRecordingCallback(self))
76 3efa7659 Thomas Thrainer
77 3efa7659 Thomas Thrainer
  def GetLogEntries(self):
78 3efa7659 Thomas Thrainer
    """Return the list of recorded log entries.
79 3efa7659 Thomas Thrainer

80 3efa7659 Thomas Thrainer
    @rtype: list of (string, string) tuples
81 3efa7659 Thomas Thrainer
    @return: the list of recorded log entries
82 3efa7659 Thomas Thrainer

83 3efa7659 Thomas Thrainer
    """
84 3efa7659 Thomas Thrainer
    return self.log_entries
85 3efa7659 Thomas Thrainer
86 3efa7659 Thomas Thrainer
  def GetLogMessages(self):
87 3efa7659 Thomas Thrainer
    """Return the list of recorded log messages.
88 3efa7659 Thomas Thrainer

89 3efa7659 Thomas Thrainer
    @rtype: list of string
90 3efa7659 Thomas Thrainer
    @return: the list of recorded log messages
91 3efa7659 Thomas Thrainer

92 3efa7659 Thomas Thrainer
    """
93 3efa7659 Thomas Thrainer
    return [msg for _, msg in self.log_entries]
94 3efa7659 Thomas Thrainer
95 3efa7659 Thomas Thrainer
  def GetLogEntriesString(self):
96 3efa7659 Thomas Thrainer
    """Return a string with all log entries separated by a newline.
97 3efa7659 Thomas Thrainer

98 3efa7659 Thomas Thrainer
    """
99 bd39b6bb Thomas Thrainer
    return "\n".join("%s: %s" % (log_type, msg)
100 bd39b6bb Thomas Thrainer
                     for log_type, msg in self.GetLogEntries())
101 3efa7659 Thomas Thrainer
102 3efa7659 Thomas Thrainer
  def GetLogMessagesString(self):
103 3efa7659 Thomas Thrainer
    """Return a string with all log messages separated by a newline.
104 3efa7659 Thomas Thrainer

105 3efa7659 Thomas Thrainer
    """
106 3efa7659 Thomas Thrainer
    return "\n".join("%s" % msg for _, msg in self.GetLogEntries())
107 3efa7659 Thomas Thrainer
108 3efa7659 Thomas Thrainer
  def assertLogContainsEntry(self, expected_type, expected_msg):
109 3efa7659 Thomas Thrainer
    """Asserts that the log contains the exact given entry.
110 3efa7659 Thomas Thrainer

111 3efa7659 Thomas Thrainer
    @type expected_type: string
112 3efa7659 Thomas Thrainer
    @param expected_type: the expected type
113 3efa7659 Thomas Thrainer
    @type expected_msg: string
114 3efa7659 Thomas Thrainer
    @param expected_msg: the expected message
115 3efa7659 Thomas Thrainer

116 3efa7659 Thomas Thrainer
    """
117 bd39b6bb Thomas Thrainer
    for log_type, msg in self.log_entries:
118 bd39b6bb Thomas Thrainer
      if log_type == expected_type and msg == expected_msg:
119 3efa7659 Thomas Thrainer
        return
120 3efa7659 Thomas Thrainer
121 3efa7659 Thomas Thrainer
    raise AssertionError(
122 3efa7659 Thomas Thrainer
      "Could not find '%s' (type '%s') in LU log messages. Log is:\n%s" %
123 3efa7659 Thomas Thrainer
      (expected_msg, expected_type, self.GetLogEntriesString()))
124 3efa7659 Thomas Thrainer
125 3efa7659 Thomas Thrainer
  def assertLogContainsMessage(self, expected_msg):
126 3efa7659 Thomas Thrainer
    """Asserts that the log contains the exact given message.
127 3efa7659 Thomas Thrainer

128 3efa7659 Thomas Thrainer
    @type expected_msg: string
129 3efa7659 Thomas Thrainer
    @param expected_msg: the expected message
130 3efa7659 Thomas Thrainer

131 3efa7659 Thomas Thrainer
    """
132 3efa7659 Thomas Thrainer
    for msg in self.GetLogMessages():
133 3efa7659 Thomas Thrainer
      if msg == expected_msg:
134 3efa7659 Thomas Thrainer
        return
135 3efa7659 Thomas Thrainer
136 3efa7659 Thomas Thrainer
    raise AssertionError(
137 3efa7659 Thomas Thrainer
      "Could not find '%s' in LU log messages. Log is:\n%s" %
138 3efa7659 Thomas Thrainer
      (expected_msg, self.GetLogMessagesString()))
139 3efa7659 Thomas Thrainer
140 3efa7659 Thomas Thrainer
  def assertLogContainsRegex(self, expected_regex):
141 3efa7659 Thomas Thrainer
    """Asserts that the log contains a message which matches the regex.
142 3efa7659 Thomas Thrainer

143 3efa7659 Thomas Thrainer
    @type expected_regex: string
144 3efa7659 Thomas Thrainer
    @param expected_regex: regular expression to match messages with.
145 3efa7659 Thomas Thrainer

146 3efa7659 Thomas Thrainer
    """
147 3efa7659 Thomas Thrainer
    for msg in self.GetLogMessages():
148 3efa7659 Thomas Thrainer
      if re.search(expected_regex, msg) is not None:
149 3efa7659 Thomas Thrainer
        return
150 3efa7659 Thomas Thrainer
151 3efa7659 Thomas Thrainer
    raise AssertionError(
152 3efa7659 Thomas Thrainer
      "Could not find '%s' in LU log messages. Log is:\n%s" %
153 3efa7659 Thomas Thrainer
      (expected_regex, self.GetLogMessagesString())
154 bd39b6bb Thomas Thrainer
    )