Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (7.6 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
import uuid
22

    
23
from ganeti import config
24
from ganeti import constants
25
from ganeti import objects
26

    
27
import mocks
28

    
29

    
30
def _StubGetEntResolver():
31
  return mocks.FakeGetentResolver()
32

    
33

    
34
class ConfigMock(config.ConfigWriter):
35
  """A mocked cluster configuration with added methods for easy customization.
36

37
  """
38

    
39
  def __init__(self):
40
    self._cur_group_id = 1
41
    self._cur_node_id = 1
42
    self._cur_inst_id = 1
43

    
44
    super(ConfigMock, self).__init__(cfg_file="/dev/null",
45
                                     _getents=_StubGetEntResolver())
46

    
47
  def _GetUuid(self):
48
    return str(uuid.uuid4())
49

    
50
  def AddNewNodeGroup(self,
51
                      uuid=None,
52
                      name=None,
53
                      ndparams=None,
54
                      diskparams=None,
55
                      ipolicy=None,
56
                      hv_state_static=None,
57
                      disk_state_static=None,
58
                      alloc_policy=None,
59
                      networks=[]):
60
    """Add a new L{objects.NodeGroup} to the cluster configuration
61

62
    See L{objects.NodeGroup} for parameter documentation.
63

64
    @rtype: L{objects.NodeGroup}
65
    @return: the newly added node group
66

67
    """
68
    group_id = self._cur_group_id
69
    self._cur_group_id += 1
70

    
71
    if uuid is None:
72
      uuid = self._GetUuid()
73
    if name is None:
74
      name = "mock_group_%d" % group_id
75

    
76
    group = objects.NodeGroup(uuid=uuid,
77
                              name=name,
78
                              ndparams=ndparams,
79
                              diskparams=diskparams,
80
                              ipolicy=ipolicy,
81
                              hv_state_static=hv_state_static,
82
                              disk_state_static=disk_state_static,
83
                              alloc_policy=alloc_policy,
84
                              networks=networks,
85
                              members=[])
86

    
87
    self.AddNodeGroup(group, None)
88
    return group
89

    
90
  def AddNewNode(self,
91
                 uuid=None,
92
                 name=None,
93
                 primary_ip=None,
94
                 secondary_ip=None,
95
                 master_candidate=True,
96
                 offline=False,
97
                 drained=False,
98
                 group=None,
99
                 master_capable=True,
100
                 vm_capable=True,
101
                 ndparams=None,
102
                 powered=True,
103
                 hv_state=None,
104
                 hv_state_static=None,
105
                 disk_state=None,
106
                 disk_state_static=None):
107
    """Add a new L{objects.Node} to the cluster configuration
108

109
    See L{objects.Node} for parameter documentation.
110

111
    @rtype: L{objects.Node}
112
    @return: the newly added node
113

114
    """
115
    node_id = self._cur_node_id
116
    self._cur_node_id += 1
117

    
118
    if uuid is None:
119
      uuid = self._GetUuid()
120
    if name is None:
121
      name = "mock_node_%d.example.com" % node_id
122
    if primary_ip is None:
123
      primary_ip = "192.168.0.%d" % node_id
124
    if secondary_ip is None:
125
      secondary_ip = "192.168.1.%d" % node_id
126
    if group is None:
127
      group = self._default_group.uuid
128
    if isinstance(group, objects.NodeGroup):
129
      group = group.uuid
130

    
131
    node = objects.Node(uuid=uuid,
132
                        name=name,
133
                        primary_ip=primary_ip,
134
                        secondary_ip=secondary_ip,
135
                        master_candidate=master_candidate,
136
                        offline=offline,
137
                        drained=drained,
138
                        group=group,
139
                        master_capable=master_capable,
140
                        vm_capable=vm_capable,
141
                        ndparams=ndparams,
142
                        powered=powered,
143
                        hv_state=hv_state,
144
                        hv_state_static=hv_state_static,
145
                        disk_state=disk_state,
146
                        disk_state_static=disk_state_static)
147

    
148
    self.AddNode(node, None)
149
    return node
150

    
151
  def AddNewInstance(self,
152
                     uuid=None,
153
                     name=None,
154
                     primary_node=None,
155
                     os="mocked_os",
156
                     hypervisor=constants.HT_FAKE,
157
                     hvparams={},
158
                     beparams={},
159
                     osparams={},
160
                     admin_state=constants.ADMINST_DOWN,
161
                     nics=[],
162
                     disks=[],
163
                     disk_template=constants.DT_DISKLESS,
164
                     disks_active=False,
165
                     network_port=None):
166
    """Add a new L{objects.Instance} to the cluster configuration
167

168
    See L{objects.Instance} for parameter documentation.
169

170
    @rtype: L{objects.Instance}
171
    @return: the newly added instance
172

173
    """
174
    inst_id = self._cur_inst_id
175
    self._cur_inst_id += 1
176

    
177
    if uuid is None:
178
      uuid = self._GetUuid()
179
    if name is None:
180
      name = "mock_inst_%d.example.com" % inst_id
181
    if primary_node is None:
182
      primary_node = self._master_node.uuid
183
    if isinstance(primary_node, objects.Node):
184
      primary_node = self._master_node.uuid
185

    
186
    inst = objects.Instance(uuid=uuid,
187
                            name=name,
188
                            primary_node=primary_node,
189
                            os=os,
190
                            hypervisor=hypervisor,
191
                            hvparams=hvparams,
192
                            beparams=beparams,
193
                            osparams=osparams,
194
                            admin_state=admin_state,
195
                            nics=nics,
196
                            disks=disks,
197
                            disk_template=disk_template,
198
                            disks_active=disks_active,
199
                            network_port=network_port)
200
    self.AddInstance(inst, None)
201
    return inst
202

    
203
  def _OpenConfig(self, accept_foreign):
204
    self._config_data = objects.ConfigData(
205
      version=constants.CONFIG_VERSION,
206
      cluster=None,
207
      nodegroups={},
208
      nodes={},
209
      instances={},
210
      networks={})
211

    
212
    master_node_uuid = self._GetUuid()
213

    
214
    self._cluster = objects.Cluster(
215
      serial_no=1,
216
      rsahostkeypub="",
217
      highest_used_port=(constants.FIRST_DRBD_PORT - 1),
218
      mac_prefix="aa:00:00",
219
      volume_group_name="xenvg",
220
      drbd_usermode_helper="/bin/true",
221
      nicparams={constants.PP_DEFAULT: constants.NICC_DEFAULTS},
222
      ndparams=constants.NDC_DEFAULTS,
223
      tcpudp_port_pool=set(),
224
      enabled_hypervisors=[constants.HT_FAKE],
225
      master_node=master_node_uuid,
226
      master_ip="192.168.0.254",
227
      master_netdev=constants.DEFAULT_BRIDGE,
228
      cluster_name="cluster.example.com",
229
      file_storage_dir="/tmp",
230
      uid_pool=[],
231
      )
232
    self._config_data.cluster = self._cluster
233

    
234
    self._default_group = self.AddNewNodeGroup(name="default")
235
    self._master_node = self.AddNewNode(uuid=master_node_uuid)
236

    
237
  def _WriteConfig(self, destination=None, feedback_fn=None):
238
    pass
239

    
240
  def _DistributeConfig(self, feedback_fn):
241
    pass
242

    
243
  def _GetRpc(self, address_list):
244
    raise NotImplementedError