Statistics
| Branch: | Tag: | Revision:

root / test / py / cmdlib / testsupport / netutils_mock.py @ 66222813

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

    
22
"""Support for mocking the netutils module"""
23

    
24
import mock
25

    
26
from ganeti import compat
27
from ganeti import netutils
28
from cmdlib.testsupport.util import patchModule
29

    
30

    
31
# pylint: disable=C0103
32
def patchNetutils(module_under_test):
33
  """Patches the L{ganeti.netutils} module for tests.
34

35
  This function is meant to be used as a decorator for test methods.
36

37
  @type module_under_test: string
38
  @param module_under_test: the module within cmdlib which is tested. The
39
        "ganeti.cmdlib" prefix is optional.
40

41
  """
42
  return patchModule(module_under_test, "netutils")
43

    
44

    
45
class HostnameMock(object):
46
  """Simple mocked version of L{netutils.Hostname}.
47

48
  """
49
  def __init__(self, name, ip):
50
    self.name = name
51
    self.ip = ip
52

    
53

    
54
def _IsOverwrittenReturnValue(value):
55
  return value is not None and value != mock.DEFAULT and \
56
      not isinstance(value, mock.Mock)
57

    
58

    
59
# pylint: disable=W0613
60
def _GetHostnameMock(cfg, mock_fct, name=None, family=None):
61
  if _IsOverwrittenReturnValue(mock_fct.return_value):
62
    return mock.DEFAULT
63

    
64
  if name is None:
65
    name = cfg.GetMasterNodeName()
66

    
67
  if name == cfg.GetClusterName():
68
    cluster = cfg.GetClusterInfo()
69
    return HostnameMock(cluster.cluster_name, cluster.master_ip)
70

    
71
  node = cfg.GetNodeInfoByName(name)
72
  if node is not None:
73
    return HostnameMock(node.name, node.primary_ip)
74

    
75
  return HostnameMock(name, "1.2.3.4")
76

    
77

    
78
# pylint: disable=W0613
79
def _TcpPingMock(cfg, mock_fct, target, port, timeout=None,
80
                 live_port_needed=None, source=None):
81
  if _IsOverwrittenReturnValue(mock_fct.return_value):
82
    return mock.DEFAULT
83

    
84
  if target == cfg.GetClusterName():
85
    return True
86
  if cfg.GetNodeInfoByName(target) is not None:
87
    return True
88
  if target in [node.primary_ip for node in cfg.GetAllNodesInfo().values()]:
89
    return True
90
  if target in [node.secondary_ip for node in cfg.GetAllNodesInfo().values()]:
91
    return True
92
  return False
93

    
94

    
95
def SetupDefaultNetutilsMock(netutils_mod, cfg):
96
  """Configures the given netutils_mod mock to work with the given config.
97

98
  All relevant functions in netutils_mod are stubbed in such a way that they
99
  are consistent with the configuration.
100

101
  @param netutils_mod: the mock module to configure
102
  @type cfg: cmdlib.testsupport.ConfigMock
103
  @param cfg: the configuration to query for netutils request
104

105
  """
106
  netutils_mod.GetHostname.side_effect = \
107
    compat.partial(_GetHostnameMock, cfg, netutils_mod.GetHostname)
108
  netutils_mod.TcpPing.side_effect = \
109
    compat.partial(_TcpPingMock, cfg, netutils_mod.TcpPing)
110
  netutils_mod.GetDaemonPort.side_effect = netutils.GetDaemonPort
111
  netutils_mod.FormatAddress.side_effect = netutils.FormatAddress
112
  netutils_mod.Hostname.GetNormalizedName.side_effect = \
113
    netutils.Hostname.GetNormalizedName