Statistics
| Branch: | Tag: | Revision:

root / test / py / cmdlib / testsupport / cmdlib_testcase.py @ 3efa7659

History | View | Annotate | Download (2.4 kB)

1
#
2
#
3

    
4
# Copyright (C) 2013 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
from config_mock import *
22
from iallocator_mock import *
23
from lock_manager_mock import *
24
from processor_mock import *
25
from rpc_runner_mock import *
26

    
27
import testutils
28

    
29

    
30
class GanetiContextMock(object):
31
  def __init__(self, cfg, glm, rpc):
32
    self.cfg = cfg
33
    self.glm = glm
34
    self.rpc = rpc
35

    
36

    
37
class CmdlibTestCase(testutils.GanetiTestCase):
38
  """Base class for cmdlib tests.
39

40
  This class sets up a mocked environment for the execution of
41
  L{ganeti.cmdlib.base.LogicalUnit} subclasses.
42

43
  The environment can be customized via the following fields:
44

45
    * C{cfg}: @see L{ConfigMock}
46
    * C{glm}: @see L{LockManagerMock}
47
    * C{rpc}: @see L{CreateRpcRunnerMock}
48
    * C{iallocator}: @see L{CreateIAllocatorMock}
49
    * C{mcpu}: @see L{ProcessorMock}
50

51
  """
52
  def setUp(self):
53
    super(CmdlibTestCase, self).setUp()
54

    
55
    self.cfg = ConfigMock()
56
    self.glm = LockManagerMock()
57
    self.rpc = CreateRpcRunnerMock()
58
    self.iallocator = CreateIAllocatorMock()
59
    ctx = GanetiContextMock(self.cfg, self.glm, self.rpc)
60
    self.mcpu = ProcessorMock(ctx)
61

    
62
  def tearDown(self):
63
    super(CmdlibTestCase, self).tearDown()
64

    
65
  def ExecOpCode(self, opcode):
66
    """Executes the given opcode.
67

68
    @param opcode: the opcode to execute
69
    @return: the result of the LU's C{Exec} method
70
    """
71
    self.glm.AddLocksFromConfig(self.cfg)
72

    
73
    return self.mcpu.ExecOpCodeAndRecordOutput(opcode)
74

    
75
  def assertLogContainsMessage(self, expected_msg):
76
    """Shortcut for L{ProcessorMock.assertLogContainsMessage}
77

78
    """
79
    self.mcpu.assertLogContainsMessage(expected_msg)
80

    
81
  def assertLogContainsRegex(self, expected_regex):
82
    """Shortcut for L{ProcessorMock.assertLogContainsRegex}
83

84
    """
85
    self.mcpu.assertLogContainsRegex(expected_regex)