root / test / py / cmdlib / testsupport / netutils_mock.py @ 11414807
History | View | Annotate | Download (3.6 kB)
1 | 08cef8fc | Thomas Thrainer | #
|
---|---|---|---|
2 | 08cef8fc | Thomas Thrainer | #
|
3 | 08cef8fc | Thomas Thrainer | |
4 | 08cef8fc | Thomas Thrainer | # Copyright (C) 2013 Google Inc.
|
5 | 08cef8fc | Thomas Thrainer | #
|
6 | 08cef8fc | Thomas Thrainer | # This program is free software; you can redistribute it and/or modify
|
7 | 08cef8fc | Thomas Thrainer | # it under the terms of the GNU General Public License as published by
|
8 | 08cef8fc | Thomas Thrainer | # the Free Software Foundation; either version 2 of the License, or
|
9 | 08cef8fc | Thomas Thrainer | # (at your option) any later version.
|
10 | 08cef8fc | Thomas Thrainer | #
|
11 | 08cef8fc | Thomas Thrainer | # This program is distributed in the hope that it will be useful, but
|
12 | 08cef8fc | Thomas Thrainer | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 08cef8fc | Thomas Thrainer | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | 08cef8fc | Thomas Thrainer | # General Public License for more details.
|
15 | 08cef8fc | Thomas Thrainer | #
|
16 | 08cef8fc | Thomas Thrainer | # You should have received a copy of the GNU General Public License
|
17 | 08cef8fc | Thomas Thrainer | # along with this program; if not, write to the Free Software
|
18 | 08cef8fc | Thomas Thrainer | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | 08cef8fc | Thomas Thrainer | # 02110-1301, USA.
|
20 | 08cef8fc | Thomas Thrainer | |
21 | 08cef8fc | Thomas Thrainer | |
22 | 08cef8fc | Thomas Thrainer | """Support for mocking the netutils module"""
|
23 | 08cef8fc | Thomas Thrainer | |
24 | 08cef8fc | Thomas Thrainer | import mock |
25 | 08cef8fc | Thomas Thrainer | |
26 | 08cef8fc | Thomas Thrainer | from ganeti import compat |
27 | 08cef8fc | Thomas Thrainer | from ganeti import netutils |
28 | 08cef8fc | Thomas Thrainer | from cmdlib.testsupport.util import patchModule |
29 | 08cef8fc | Thomas Thrainer | |
30 | 08cef8fc | Thomas Thrainer | |
31 | 08cef8fc | Thomas Thrainer | # pylint: disable=C0103
|
32 | 08cef8fc | Thomas Thrainer | def patchNetutils(module_under_test): |
33 | 08cef8fc | Thomas Thrainer | """Patches the L{ganeti.netutils} module for tests.
|
34 | 08cef8fc | Thomas Thrainer |
|
35 | 08cef8fc | Thomas Thrainer | This function is meant to be used as a decorator for test methods.
|
36 | 08cef8fc | Thomas Thrainer |
|
37 | 08cef8fc | Thomas Thrainer | @type module_under_test: string
|
38 | 08cef8fc | Thomas Thrainer | @param module_under_test: the module within cmdlib which is tested. The
|
39 | 08cef8fc | Thomas Thrainer | "ganeti.cmdlib" prefix is optional.
|
40 | 08cef8fc | Thomas Thrainer |
|
41 | 08cef8fc | Thomas Thrainer | """
|
42 | 08cef8fc | Thomas Thrainer | return patchModule(module_under_test, "netutils") |
43 | 08cef8fc | Thomas Thrainer | |
44 | 08cef8fc | Thomas Thrainer | |
45 | 08cef8fc | Thomas Thrainer | class HostnameMock(object): |
46 | 08cef8fc | Thomas Thrainer | """Simple mocked version of L{netutils.Hostname}.
|
47 | 08cef8fc | Thomas Thrainer |
|
48 | 08cef8fc | Thomas Thrainer | """
|
49 | 08cef8fc | Thomas Thrainer | def __init__(self, name, ip): |
50 | 08cef8fc | Thomas Thrainer | self.name = name
|
51 | 08cef8fc | Thomas Thrainer | self.ip = ip
|
52 | 08cef8fc | Thomas Thrainer | |
53 | 08cef8fc | Thomas Thrainer | |
54 | 08cef8fc | Thomas Thrainer | def _IsOverwrittenReturnValue(value): |
55 | 08cef8fc | Thomas Thrainer | return value is not None and value != mock.DEFAULT and \ |
56 | 08cef8fc | Thomas Thrainer | not isinstance(value, mock.Mock) |
57 | 08cef8fc | Thomas Thrainer | |
58 | 08cef8fc | Thomas Thrainer | |
59 | 08cef8fc | Thomas Thrainer | # pylint: disable=W0613
|
60 | 08cef8fc | Thomas Thrainer | def _GetHostnameMock(cfg, mock_fct, name=None, family=None): |
61 | 08cef8fc | Thomas Thrainer | if _IsOverwrittenReturnValue(mock_fct.return_value):
|
62 | 08cef8fc | Thomas Thrainer | return mock.DEFAULT
|
63 | 08cef8fc | Thomas Thrainer | |
64 | 08cef8fc | Thomas Thrainer | if name is None: |
65 | 08cef8fc | Thomas Thrainer | name = cfg.GetMasterNodeName() |
66 | 08cef8fc | Thomas Thrainer | |
67 | 08cef8fc | Thomas Thrainer | if name == cfg.GetClusterName():
|
68 | 08cef8fc | Thomas Thrainer | cluster = cfg.GetClusterInfo() |
69 | 08cef8fc | Thomas Thrainer | return HostnameMock(cluster.cluster_name, cluster.master_ip)
|
70 | 08cef8fc | Thomas Thrainer | |
71 | 08cef8fc | Thomas Thrainer | node = cfg.GetNodeInfoByName(name) |
72 | 66222813 | Thomas Thrainer | if node is not None: |
73 | 66222813 | Thomas Thrainer | return HostnameMock(node.name, node.primary_ip)
|
74 | 08cef8fc | Thomas Thrainer | |
75 | 11414807 | Klaus Aehlig | return HostnameMock(name, "203.0.113.1") |
76 | 08cef8fc | Thomas Thrainer | |
77 | 08cef8fc | Thomas Thrainer | |
78 | 08cef8fc | Thomas Thrainer | # pylint: disable=W0613
|
79 | 08cef8fc | Thomas Thrainer | def _TcpPingMock(cfg, mock_fct, target, port, timeout=None, |
80 | 08cef8fc | Thomas Thrainer | live_port_needed=None, source=None): |
81 | 08cef8fc | Thomas Thrainer | if _IsOverwrittenReturnValue(mock_fct.return_value):
|
82 | 08cef8fc | Thomas Thrainer | return mock.DEFAULT
|
83 | 08cef8fc | Thomas Thrainer | |
84 | 08cef8fc | Thomas Thrainer | if target == cfg.GetClusterName():
|
85 | 08cef8fc | Thomas Thrainer | return True |
86 | 08cef8fc | Thomas Thrainer | if cfg.GetNodeInfoByName(target) is not None: |
87 | 08cef8fc | Thomas Thrainer | return True |
88 | 08cef8fc | Thomas Thrainer | if target in [node.primary_ip for node in cfg.GetAllNodesInfo().values()]: |
89 | 08cef8fc | Thomas Thrainer | return True |
90 | 08cef8fc | Thomas Thrainer | if target in [node.secondary_ip for node in cfg.GetAllNodesInfo().values()]: |
91 | 08cef8fc | Thomas Thrainer | return True |
92 | 08cef8fc | Thomas Thrainer | return False |
93 | 08cef8fc | Thomas Thrainer | |
94 | 08cef8fc | Thomas Thrainer | |
95 | 08cef8fc | Thomas Thrainer | def SetupDefaultNetutilsMock(netutils_mod, cfg): |
96 | 08cef8fc | Thomas Thrainer | """Configures the given netutils_mod mock to work with the given config.
|
97 | 08cef8fc | Thomas Thrainer |
|
98 | 08cef8fc | Thomas Thrainer | All relevant functions in netutils_mod are stubbed in such a way that they
|
99 | 08cef8fc | Thomas Thrainer | are consistent with the configuration.
|
100 | 08cef8fc | Thomas Thrainer |
|
101 | 08cef8fc | Thomas Thrainer | @param netutils_mod: the mock module to configure
|
102 | 08cef8fc | Thomas Thrainer | @type cfg: cmdlib.testsupport.ConfigMock
|
103 | 08cef8fc | Thomas Thrainer | @param cfg: the configuration to query for netutils request
|
104 | 08cef8fc | Thomas Thrainer |
|
105 | 08cef8fc | Thomas Thrainer | """
|
106 | 08cef8fc | Thomas Thrainer | netutils_mod.GetHostname.side_effect = \ |
107 | 08cef8fc | Thomas Thrainer | compat.partial(_GetHostnameMock, cfg, netutils_mod.GetHostname) |
108 | 08cef8fc | Thomas Thrainer | netutils_mod.TcpPing.side_effect = \ |
109 | 08cef8fc | Thomas Thrainer | compat.partial(_TcpPingMock, cfg, netutils_mod.TcpPing) |
110 | 08cef8fc | Thomas Thrainer | netutils_mod.GetDaemonPort.side_effect = netutils.GetDaemonPort |
111 | 08cef8fc | Thomas Thrainer | netutils_mod.FormatAddress.side_effect = netutils.FormatAddress |
112 | 66222813 | Thomas Thrainer | netutils_mod.Hostname.GetNormalizedName.side_effect = \ |
113 | 66222813 | Thomas Thrainer | netutils.Hostname.GetNormalizedName |
114 | 27619aac | Thomas Thrainer | netutils_mod.IPAddress = netutils.IPAddress |
115 | 27619aac | Thomas Thrainer | netutils_mod.IP4Address = netutils.IP4Address |
116 | 27619aac | Thomas Thrainer | netutils_mod.IP6Address = netutils.IP6Address |