This repository has been archived by the owner on Aug 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
fakeprovider.py
299 lines (244 loc) · 8.77 KB
/
fakeprovider.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/usr/bin/env python
#
# Copyright 2013 OpenStack Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import StringIO
import logging
import novaclient
import requests.exceptions
import threading
import time
import uuid
from jenkins import JenkinsException
import shade
class Dummy(object):
IMAGE = 'Image'
INSTANCE = 'Instance'
FLAVOR = 'Flavor'
def __init__(self, kind, **kw):
self.__kind = kind
self.__kw = kw
for k, v in kw.items():
setattr(self, k, v)
def __repr__(self):
args = []
for k in self.__kw.keys():
args.append('%s=%s' % (k, getattr(self, k)))
args = ' '.join(args)
return '<%s %s %s>' % (self.__kind, id(self), args)
def delete(self):
self.manager.delete(self)
def update(self, data):
try:
if self.should_fail:
raise shade.OpenStackCloudException('This image has '
'SHOULD_FAIL set to True.')
except AttributeError:
pass
def fake_get_one_cloud(cloud_config, cloud_kwargs):
cloud_kwargs['validate'] = False
return cloud_config.get_one_cloud(**cloud_kwargs)
class FakeList(object):
log = logging.getLogger("nodepool.FakeList")
def __init__(self, l):
self._list = l
def list(self):
self.log.debug("List %s" % repr(self._list))
return self._list
def find(self, name):
for x in self._list:
if x.name == name:
return x
def get(self, image=None):
if image:
id = image
self.log.debug("Get %s in %s" % (id, repr(self._list)))
for x in self._list:
if x.id == id:
return x
raise novaclient.exceptions.NotFound(404)
def _finish(self, obj, delay, status):
time.sleep(delay)
obj.status = status
def delete(self, *args, **kw):
self.log.debug("Delete from %s" % repr(self._list))
if 'image' in kw:
self._list.remove(self.get(kw['image']))
else:
obj = args[0]
if hasattr(obj, 'id'):
self._list.remove(obj)
else:
self._list.remove(self.get(obj))
self.log.debug("Deleted from %s" % repr(self._list))
def create(self, **kw):
should_fail = kw.get('SHOULD_FAIL', '').lower() == 'true'
nics = kw.get('nics', [])
addresses = None
# if keyword 'ipv6-uuid' is found in provider config,
# ipv6 address will be available in public addr dict.
for nic in nics:
if 'ipv6-uuid' not in nic['net-id']:
continue
addresses = dict(
public=[dict(version=4, addr='fake'),
dict(version=6, addr='fake_v6')],
private=[dict(version=4, addr='fake')]
)
break
if not addresses:
addresses = dict(
public=[dict(version=4, addr='fake')],
private=[dict(version=4, addr='fake')]
)
s = Dummy(Dummy.INSTANCE,
id=uuid.uuid4().hex,
name=kw['name'],
status='BUILD',
adminPass='fake',
addresses=addresses,
metadata=kw.get('meta', {}),
manager=self,
should_fail=should_fail)
self._list.append(s)
t = threading.Thread(target=self._finish,
name='FakeProvider create',
args=(s, 0.1, 'ACTIVE'))
t.start()
return s
def create_image(self, server, image_name, metadata):
# XXX : validate metadata?
x = self.api.images.create(name=image_name)
return x.id
class FakeHTTPClient(object):
def get(self, path):
if path == '/extensions':
return None, dict(extensions=dict())
class BadHTTPClient(object):
'''Always raises a ProxyError'''
def get(self, path):
raise requests.exceptions.ProxyError
class FakeClient(object):
def __init__(self, images, *args, **kwargs):
self.flavors = FakeList([
Dummy(Dummy.FLAVOR, id='f1', ram=8192, name='Fake Flavor'),
Dummy(Dummy.FLAVOR, id='f2', ram=8192, name='Unreal Flavor'),
])
self.images = images
self.client = FakeHTTPClient()
self.servers = FakeList([])
self.servers.api = self
class BadClient(FakeClient):
def __init__(self, images):
super(BadClient, self).__init__(images)
self.client = BadHTTPClient()
class BadOpenstackCloud(object):
def __init__(self, images=None):
if images is None:
images = FakeList([Dummy(Dummy.IMAGE,
id='fake-image-id',
status='READY',
name='Fake Precise',
metadata={})])
self.nova_client = BadClient(images)
class FakeGlanceClient(object):
def __init__(self, images, **kwargs):
self.kwargs = kwargs
self.images = images
class FakeNeutronClient(object):
def __init__(self, networks=None):
if networks is None:
networks = [dict(id='fake-public-network-uuid',
name='fake-public-network-name'),
dict(id='fake-private-network-uuid',
name='fake-private-network-name')]
self.networks = networks
def list_networks(self):
return dict(networks=self.networks)
class FakeOpenStackCloud(object):
def __init__(self, images=None):
if images is None:
images = FakeList([Dummy(Dummy.IMAGE,
id='fake-image-id',
status='READY',
name='Fake Precise',
metadata={})])
self.nova_client = FakeClient(images)
self._glance_client = FakeGlanceClient(images)
self.neutron_client = FakeNeutronClient()
def create_image(self, **kwargs):
image = self._glance_client.images.create(**kwargs)
image.update('fake data')
return image
class FakeFile(StringIO.StringIO):
def __init__(self, path):
StringIO.StringIO.__init__(self)
self.__path = path
def close(self):
print "Wrote to %s:" % self.__path
print self.getvalue()
StringIO.StringIO.close(self)
class FakeSFTPClient(object):
def open(self, path, mode):
return FakeFile(path)
def close(self):
pass
class FakeSSHClient(object):
def __init__(self):
self.client = self
def ssh(self, description, cmd, output=False):
return True
def scp(self, src, dest):
return True
def open_sftp(self):
return FakeSFTPClient()
class FakeJenkins(object):
def __init__(self, user):
self._nodes = {}
self.quiet = False
self.down = False
if user == 'quiet':
self.quiet = True
if user == 'down':
self.down = True
def node_exists(self, name):
return name in self._nodes
def create_node(self, name, **kw):
self._nodes[name] = kw
def delete_node(self, name):
del self._nodes[name]
def get_info(self):
if self.down:
raise JenkinsException("Jenkins is down")
d = {u'assignedLabels': [{}],
u'description': None,
u'jobs': [{u'color': u'red',
u'name': u'test-job',
u'url': u'https://jenkins.example.com/job/test-job/'}],
u'mode': u'NORMAL',
u'nodeDescription': u'the master Jenkins node',
u'nodeName': u'',
u'numExecutors': 1,
u'overallLoad': {},
u'primaryView': {u'name': u'Overview',
u'url': u'https://jenkins.example.com/'},
u'quietingDown': self.quiet,
u'slaveAgentPort': 8090,
u'unlabeledLoad': {},
u'useCrumbs': False,
u'useSecurity': True,
u'views': [
{u'name': u'test-view',
u'url': u'https://jenkins.example.com/view/test-view/'}]}
return d