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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
import os
import mock
import httplib2
from nose.tools import assert_raises, assert_equal
from novaclient.shell import OpenStackShell, CommandError
from fakeserver import FakeServer
from utils import assert_in
# Patch os.environ to avoid required auth info.
def setup():
global _old_env
fake_env = {
'NOVA_USERNAME': 'username',
'NOVA_API_KEY': 'password',
'NOVA_PROJECT_ID': 'project_id'
}
_old_env, os.environ = os.environ, fake_env.copy()
# Make a fake shell object, a helping wrapper to call it, and a quick way
# of asserting that certain API calls were made.
global shell, _shell, assert_called, assert_called_anytime
_shell = OpenStackShell()
_shell._api_class = FakeServer
assert_called = lambda m, u, b=None: _shell.cs.assert_called(m, u, b)
assert_called_anytime = lambda m, u, b=None: \
_shell.cs.assert_called_anytime(m, u, b)
shell = lambda cmd: _shell.main(cmd.split())
def teardown():
global _old_env
os.environ = _old_env
def test_backup_schedule():
shell('backup-schedule 1234')
assert_called('GET', '/servers/1234/backup_schedule')
shell('backup-schedule sample-server --weekly monday')
assert_called(
'POST', '/servers/1234/backup_schedule',
{'backupSchedule': {'enabled': True, 'daily': 'DISABLED',
'weekly': 'MONDAY'}}
)
shell('backup-schedule sample-server '
'--weekly disabled --daily h_0000_0200')
assert_called(
'POST', '/servers/1234/backup_schedule',
{'backupSchedule': {'enabled': True, 'daily': 'H_0000_0200',
'weekly': 'DISABLED'}}
)
shell('backup-schedule sample-server --disable')
assert_called(
'POST', '/servers/1234/backup_schedule',
{'backupSchedule': {'enabled': False, 'daily': 'DISABLED',
'weekly': 'DISABLED'}}
)
def test_backup_schedule_delete():
shell('backup-schedule-delete 1234')
assert_called('DELETE', '/servers/1234/backup_schedule')
def test_boot():
shell('boot --image 1 some-server')
assert_called(
'POST', '/servers',
{'server': {'flavorId': 1, 'name': 'some-server', 'imageId': '1',
'min_count': 1, 'max_count': 1}}
)
shell('boot --image 1 --meta foo=bar --meta spam=eggs some-server ')
assert_called(
'POST', '/servers',
{'server': {'flavorId': 1, 'name': 'some-server', 'imageId': '1',
'min_count': 1, 'max_count': 1,
'metadata': {'foo': 'bar', 'spam': 'eggs'}}}
)
def test_boot_files():
testfile = os.path.join(os.path.dirname(__file__), 'testfile.txt')
expected_file_data = open(testfile).read().encode('base64')
shell('boot some-server --image 1 --file /tmp/foo=%s --file /tmp/bar=%s' %
(testfile, testfile))
assert_called(
'POST', '/servers',
{'server': {'flavorId': 1, 'name': 'some-server', 'imageId': '1',
'min_count': 1, 'max_count': 1,
'personality': [
{'path': '/tmp/bar', 'contents': expected_file_data},
{'path': '/tmp/foo', 'contents': expected_file_data}
]}
}
)
def test_boot_invalid_file():
invalid_file = os.path.join(os.path.dirname(__file__), 'asdfasdfasdfasdf')
assert_raises(CommandError, shell, 'boot some-server --image 1 '
'--file /foo=%s' % invalid_file)
def test_boot_key_auto():
mock_exists = mock.Mock(return_value=True)
mock_open = mock.Mock()
mock_open.return_value = mock.Mock()
mock_open.return_value.read = mock.Mock(return_value='SSHKEY')
@mock.patch('os.path.exists', mock_exists)
@mock.patch('__builtin__.open', mock_open)
def test_shell_call():
shell('boot some-server --image 1 --key')
assert_called(
'POST', '/servers',
{'server': {'flavorId': 1, 'name': 'some-server', 'imageId': '1',
'min_count': 1, 'max_count': 1,
'personality': [{
'path': '/root/.ssh/authorized_keys2',
'contents': ('SSHKEY').encode('base64')},
]}
}
)
test_shell_call()
def test_boot_key_auto_no_keys():
mock_exists = mock.Mock(return_value=False)
@mock.patch('os.path.exists', mock_exists)
def test_shell_call():
assert_raises(CommandError, shell, 'boot some-server --image 1 --key')
test_shell_call()
def test_boot_key_file():
testfile = os.path.join(os.path.dirname(__file__), 'testfile.txt')
expected_file_data = open(testfile).read().encode('base64')
shell('boot some-server --image 1 --key %s' % testfile)
assert_called(
'POST', '/servers',
{'server': {'flavorId': 1, 'name': 'some-server', 'imageId': '1',
'min_count': 1, 'max_count': 1,
'personality': [
{'path': '/root/.ssh/authorized_keys2', 'contents':
expected_file_data},
]}
}
)
def test_boot_invalid_keyfile():
invalid_file = os.path.join(os.path.dirname(__file__), 'asdfasdfasdfasdf')
assert_raises(CommandError, shell, 'boot some-server '
'--image 1 --key %s' % invalid_file)
def test_boot_ipgroup():
shell('boot --image 1 --ipgroup 1 some-server')
assert_called(
'POST', '/servers',
{'server': {'flavorId': 1, 'name': 'some-server', 'imageId': '1',
'sharedIpGroupId': 1, 'min_count': 1, 'max_count': 1}}
)
def test_boot_ipgroup_name():
shell('boot --image 1 --ipgroup group1 some-server')
assert_called(
'POST', '/servers',
{'server': {'flavorId': 1, 'name': 'some-server', 'imageId': '1',
'sharedIpGroupId': 1, 'min_count': 1, 'max_count': 1}}
)
def test_flavor_list():
shell('flavor-list')
assert_called_anytime('GET', '/flavors/detail')
def test_image_list():
shell('image-list')
assert_called('GET', '/images/detail')
def test_snapshot_create():
shell('image-create sample-server mysnapshot')
assert_called(
'POST', '/images',
{'image': {'name': 'mysnapshot', 'serverId': 1234}}
)
def test_image_delete():
shell('image-delete 1')
assert_called('DELETE', '/images/1')
def test_ip_share():
shell('ip-share sample-server 1 1.2.3.4')
assert_called(
'PUT', '/servers/1234/ips/public/1.2.3.4',
{'shareIp': {'sharedIpGroupId': 1, 'configureServer': True}}
)
def test_ip_unshare():
shell('ip-unshare sample-server 1.2.3.4')
assert_called('DELETE', '/servers/1234/ips/public/1.2.3.4')
def test_ipgroup_list():
shell('ipgroup-list')
assert_in(('GET', '/shared_ip_groups/detail', None),
_shell.cs.client.callstack)
assert_called('GET', '/servers/5678')
def test_ipgroup_show():
shell('ipgroup-show 1')
assert_called('GET', '/shared_ip_groups/1')
shell('ipgroup-show group2')
# does a search, not a direct GET
assert_called('GET', '/shared_ip_groups/detail')
def test_ipgroup_create():
shell('ipgroup-create a-group')
assert_called(
'POST', '/shared_ip_groups',
{'sharedIpGroup': {'name': 'a-group'}}
)
shell('ipgroup-create a-group sample-server')
assert_called(
'POST', '/shared_ip_groups',
{'sharedIpGroup': {'name': 'a-group', 'server': 1234}}
)
def test_ipgroup_delete():
shell('ipgroup-delete group1')
assert_called('DELETE', '/shared_ip_groups/1')
def test_list():
shell('list')
assert_called('GET', '/servers/detail')
def test_reboot():
shell('reboot sample-server')
assert_called('POST', '/servers/1234/action', {'reboot': {'type': 'SOFT'}})
shell('reboot sample-server --hard')
assert_called('POST', '/servers/1234/action', {'reboot': {'type': 'HARD'}})
def test_rebuild():
shell('rebuild sample-server 1')
assert_called('POST', '/servers/1234/action', {'rebuild': {'imageId': 1}})
def test_rename():
shell('rename sample-server newname')
assert_called('PUT', '/servers/1234', {'server': {'name': 'newname'}})
def test_resize():
shell('resize sample-server 1')
assert_called('POST', '/servers/1234/action', {'resize': {'flavorId': 1}})
def test_resize_confirm():
shell('resize-confirm sample-server')
assert_called('POST', '/servers/1234/action', {'confirmResize': None})
def test_resize_revert():
shell('resize-revert sample-server')
assert_called('POST', '/servers/1234/action', {'revertResize': None})
def test_backup():
shell('backup sample-server mybackup daily 1')
assert_called(
'POST', '/servers/1234/action',
{'createBackup': {'name': 'mybackup', 'backup_type': 'daily',
'rotation': 1}}
)
@mock.patch('getpass.getpass', mock.Mock(return_value='p'))
def test_root_password():
shell('root-password sample-server')
assert_called('PUT', '/servers/1234', {'server': {'adminPass': 'p'}})
def test_show():
shell('show 1234')
# XXX need a way to test multiple calls
# assert_called('GET', '/servers/1234')
assert_called('GET', '/images/2')
def test_delete():
shell('delete 1234')
assert_called('DELETE', '/servers/1234')
shell('delete sample-server')
assert_called('DELETE', '/servers/1234')
def test_zone():
shell('zone 1')
assert_called('GET', '/zones/1')
shell('zone 1 --api_url=http://zzz --zone_username=frank --password=xxx')
assert_called(
'PUT', '/zones/1',
{'zone': {'api_url': 'http://zzz', 'username': 'frank',
'password': 'xxx'}}
)
def test_zone_add():
shell('zone-add http://zzz frank xxx 0.0 1.0')
assert_called(
'POST', '/zones',
{'zone': {'api_url': 'http://zzz', 'username': 'frank',
'password': 'xxx',
'weight_offset': '0.0', 'weight_scale': '1.0'}}
)
def test_zone_delete():
shell('zone-delete 1')
assert_called('DELETE', '/zones/1')
def test_zone_list():
shell('zone-list')
assert_in(('GET', '/zones/detail', None),
_shell.cs.client.callstack)
def test_help():
@mock.patch.object(_shell.parser, 'print_help')
def test_help(m):
shell('help')
m.assert_called()
@mock.patch.object(_shell.subcommands['delete'], 'print_help')
def test_help_delete(m):
shell('help delete')
m.assert_called()
test_help()
test_help_delete()
assert_raises(CommandError, shell, 'help foofoo')
def test_debug():
httplib2.debuglevel = 0
shell('--debug list')
assert httplib2.debuglevel == 1
|