root / test / py / cmdlib / testsupport / config_mock.py @ e969a81f
History | View | Annotate | Download (8.7 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 cluster configuration"""
|
23 |
|
24 |
|
25 |
import time |
26 |
import uuid as uuid_module |
27 |
|
28 |
from ganeti import config |
29 |
from ganeti import constants |
30 |
from ganeti import objects |
31 |
|
32 |
import mocks |
33 |
|
34 |
|
35 |
def _StubGetEntResolver(): |
36 |
return mocks.FakeGetentResolver()
|
37 |
|
38 |
|
39 |
# pylint: disable=R0904
|
40 |
class ConfigMock(config.ConfigWriter): |
41 |
"""A mocked cluster configuration with added methods for easy customization.
|
42 |
|
43 |
"""
|
44 |
|
45 |
def __init__(self): |
46 |
self._cur_group_id = 1 |
47 |
self._cur_node_id = 1 |
48 |
self._cur_inst_id = 1 |
49 |
|
50 |
super(ConfigMock, self).__init__(cfg_file="/dev/null", |
51 |
_getents=_StubGetEntResolver()) |
52 |
|
53 |
def _GetUuid(self): |
54 |
return str(uuid_module.uuid4()) |
55 |
|
56 |
def AddNewNodeGroup(self, |
57 |
uuid=None,
|
58 |
name=None,
|
59 |
ndparams=None,
|
60 |
diskparams=None,
|
61 |
ipolicy=None,
|
62 |
hv_state_static=None,
|
63 |
disk_state_static=None,
|
64 |
alloc_policy=None,
|
65 |
networks=None):
|
66 |
"""Add a new L{objects.NodeGroup} to the cluster configuration
|
67 |
|
68 |
See L{objects.NodeGroup} for parameter documentation.
|
69 |
|
70 |
@rtype: L{objects.NodeGroup}
|
71 |
@return: the newly added node group
|
72 |
|
73 |
"""
|
74 |
group_id = self._cur_group_id
|
75 |
self._cur_group_id += 1 |
76 |
|
77 |
if uuid is None: |
78 |
uuid = self._GetUuid()
|
79 |
if name is None: |
80 |
name = "mock_group_%d" % group_id
|
81 |
if networks is None: |
82 |
networks = {} |
83 |
|
84 |
group = objects.NodeGroup(uuid=uuid, |
85 |
name=name, |
86 |
ndparams=ndparams, |
87 |
diskparams=diskparams, |
88 |
ipolicy=ipolicy, |
89 |
hv_state_static=hv_state_static, |
90 |
disk_state_static=disk_state_static, |
91 |
alloc_policy=alloc_policy, |
92 |
networks=networks, |
93 |
members=[]) |
94 |
|
95 |
self.AddNodeGroup(group, None) |
96 |
return group
|
97 |
|
98 |
# pylint: disable=R0913
|
99 |
def AddNewNode(self, |
100 |
uuid=None,
|
101 |
name=None,
|
102 |
primary_ip=None,
|
103 |
secondary_ip=None,
|
104 |
master_candidate=True,
|
105 |
offline=False,
|
106 |
drained=False,
|
107 |
group=None,
|
108 |
master_capable=True,
|
109 |
vm_capable=True,
|
110 |
ndparams=None,
|
111 |
powered=True,
|
112 |
hv_state=None,
|
113 |
hv_state_static=None,
|
114 |
disk_state=None,
|
115 |
disk_state_static=None):
|
116 |
"""Add a new L{objects.Node} to the cluster configuration
|
117 |
|
118 |
See L{objects.Node} for parameter documentation.
|
119 |
|
120 |
@rtype: L{objects.Node}
|
121 |
@return: the newly added node
|
122 |
|
123 |
"""
|
124 |
node_id = self._cur_node_id
|
125 |
self._cur_node_id += 1 |
126 |
|
127 |
if uuid is None: |
128 |
uuid = self._GetUuid()
|
129 |
if name is None: |
130 |
name = "mock_node_%d.example.com" % node_id
|
131 |
if primary_ip is None: |
132 |
primary_ip = "192.168.0.%d" % node_id
|
133 |
if secondary_ip is None: |
134 |
secondary_ip = "192.168.1.%d" % node_id
|
135 |
if group is None: |
136 |
group = self._default_group.uuid
|
137 |
if isinstance(group, objects.NodeGroup): |
138 |
group = group.uuid |
139 |
if ndparams is None: |
140 |
ndparams = {} |
141 |
|
142 |
node = objects.Node(uuid=uuid, |
143 |
name=name, |
144 |
primary_ip=primary_ip, |
145 |
secondary_ip=secondary_ip, |
146 |
master_candidate=master_candidate, |
147 |
offline=offline, |
148 |
drained=drained, |
149 |
group=group, |
150 |
master_capable=master_capable, |
151 |
vm_capable=vm_capable, |
152 |
ndparams=ndparams, |
153 |
powered=powered, |
154 |
hv_state=hv_state, |
155 |
hv_state_static=hv_state_static, |
156 |
disk_state=disk_state, |
157 |
disk_state_static=disk_state_static) |
158 |
|
159 |
self.AddNode(node, None) |
160 |
return node
|
161 |
|
162 |
def AddNewInstance(self, |
163 |
uuid=None,
|
164 |
name=None,
|
165 |
primary_node=None,
|
166 |
os="mocked_os",
|
167 |
hypervisor=constants.HT_FAKE, |
168 |
hvparams=None,
|
169 |
beparams=None,
|
170 |
osparams=None,
|
171 |
admin_state=constants.ADMINST_DOWN, |
172 |
nics=None,
|
173 |
disks=None,
|
174 |
disk_template=constants.DT_DISKLESS, |
175 |
disks_active=False,
|
176 |
network_port=None):
|
177 |
"""Add a new L{objects.Instance} to the cluster configuration
|
178 |
|
179 |
See L{objects.Instance} for parameter documentation.
|
180 |
|
181 |
@rtype: L{objects.Instance}
|
182 |
@return: the newly added instance
|
183 |
|
184 |
"""
|
185 |
inst_id = self._cur_inst_id
|
186 |
self._cur_inst_id += 1 |
187 |
|
188 |
if uuid is None: |
189 |
uuid = self._GetUuid()
|
190 |
if name is None: |
191 |
name = "mock_inst_%d.example.com" % inst_id
|
192 |
if primary_node is None: |
193 |
primary_node = self._master_node.uuid
|
194 |
if isinstance(primary_node, objects.Node): |
195 |
primary_node = self._master_node.uuid
|
196 |
if hvparams is None: |
197 |
hvparams = {} |
198 |
if beparams is None: |
199 |
beparams = {} |
200 |
if osparams is None: |
201 |
osparams = {} |
202 |
if nics is None: |
203 |
nics = [] |
204 |
if disks is None: |
205 |
disks = [] |
206 |
|
207 |
inst = objects.Instance(uuid=uuid, |
208 |
name=name, |
209 |
primary_node=primary_node, |
210 |
os=os, |
211 |
hypervisor=hypervisor, |
212 |
hvparams=hvparams, |
213 |
beparams=beparams, |
214 |
osparams=osparams, |
215 |
admin_state=admin_state, |
216 |
nics=nics, |
217 |
disks=disks, |
218 |
disk_template=disk_template, |
219 |
disks_active=disks_active, |
220 |
network_port=network_port) |
221 |
self.AddInstance(inst, None) |
222 |
return inst
|
223 |
|
224 |
def _OpenConfig(self, accept_foreign): |
225 |
self._config_data = objects.ConfigData(
|
226 |
version=constants.CONFIG_VERSION, |
227 |
cluster=None,
|
228 |
nodegroups={}, |
229 |
nodes={}, |
230 |
instances={}, |
231 |
networks={}) |
232 |
|
233 |
master_node_uuid = self._GetUuid()
|
234 |
|
235 |
self._cluster = objects.Cluster(
|
236 |
serial_no=1,
|
237 |
rsahostkeypub="",
|
238 |
highest_used_port=(constants.FIRST_DRBD_PORT - 1),
|
239 |
tcpudp_port_pool=set(),
|
240 |
mac_prefix="aa:00:00",
|
241 |
volume_group_name="xenvg",
|
242 |
reserved_lvs=None,
|
243 |
drbd_usermode_helper="/bin/true",
|
244 |
master_node=master_node_uuid, |
245 |
master_ip="192.168.0.254",
|
246 |
master_netdev=constants.DEFAULT_BRIDGE, |
247 |
master_netmask=None,
|
248 |
use_external_mip_script=None,
|
249 |
cluster_name="cluster.example.com",
|
250 |
file_storage_dir="/tmp",
|
251 |
shared_file_storage_dir=None,
|
252 |
enabled_hypervisors=list(constants.HYPER_TYPES),
|
253 |
hvparams=None,
|
254 |
ipolicy=None,
|
255 |
os_hvp=None,
|
256 |
beparams=None,
|
257 |
osparams=None,
|
258 |
nicparams=None,
|
259 |
ndparams=None,
|
260 |
diskparams=None,
|
261 |
candidate_pool_size=3,
|
262 |
modify_etc_hosts=False,
|
263 |
modify_ssh_setup=False,
|
264 |
maintain_node_health=False,
|
265 |
uid_pool=None,
|
266 |
default_iallocator=None,
|
267 |
hidden_os=None,
|
268 |
blacklisted_os=None,
|
269 |
primary_ip_family=None,
|
270 |
prealloc_wipe_disks=None,
|
271 |
enabled_disk_templates=list(constants.DISK_TEMPLATES),
|
272 |
) |
273 |
self._cluster.ctime = self._cluster.mtime = time.time() |
274 |
self._cluster.UpgradeConfig()
|
275 |
self._config_data.cluster = self._cluster |
276 |
|
277 |
self._default_group = self.AddNewNodeGroup(name="default") |
278 |
self._master_node = self.AddNewNode(uuid=master_node_uuid) |
279 |
|
280 |
def _WriteConfig(self, destination=None, feedback_fn=None): |
281 |
pass
|
282 |
|
283 |
def _DistributeConfig(self, feedback_fn): |
284 |
pass
|
285 |
|
286 |
def _GetRpc(self, address_list): |
287 |
raise AssertionError("This should not be used during tests!") |