This repository has been archived by the owner on Mar 6, 2024. It is now read-only.
/
vdc_tests.py
392 lines (379 loc) · 18.3 KB
/
vdc_tests.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
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# VMware vCloud Director Python SDK
# Copyright (c) 2018 VMware, Inc. All Rights Reserved.
#
# 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.
from uuid import uuid1
from pyvcloud.system_test_framework.base_test import BaseTestCase
from pyvcloud.system_test_framework.environment import CommonRoles
from pyvcloud.system_test_framework.environment import developerModeAware
from pyvcloud.system_test_framework.environment import Environment
from pyvcloud.system_test_framework.utils import create_independent_disk
from pyvcloud.vcd.client import TaskStatus
from pyvcloud.vcd.exceptions import AccessForbiddenException
from pyvcloud.vcd.exceptions import EntityNotFoundException
from pyvcloud.vcd.exceptions import OperationNotSupportedException
from pyvcloud.vcd.utils import extract_metadata_value
from pyvcloud.vcd.utils import get_non_admin_href
from pyvcloud.vcd.vdc import VDC
class TestOrgVDC(BaseTestCase):
"""Test OrgVDC functionalities implemented in pyvcloud."""
# All tests in this module should run as System Administrator.
_client = None
_new_vdc_name = 'org_vdc_' + str(uuid1())
_new_vdc_href = None
_non_existent_vdc_name = 'non_existent_org_vdc_' + str(uuid1())
_metadata_key = 'key_' + str(uuid1())
_metadata_value = 'value_' + str(uuid1())
_metadata_new_value = 'new_value_' + str(uuid1())
_non_existent_metadata_key = 'non_existent_key_' + str(uuid1())
_idisk_name = 'vdcSCSI'
_idisk_size = '5242880'
_idisk_description = '5Mb SCSI disk'
def test_0000_setup(self):
"""Setup the org vdc required for the other tests in this module.
Create one org vdc as per the configuration stated above. Test the
method Org.create_org_vdc().
This test passes if the vdc href is not None.
"""
logger = Environment.get_default_logger()
TestOrgVDC._client = Environment.get_sys_admin_client()
TestOrgVDC._org_admin_client = Environment.get_client_in_default_org(
CommonRoles.ORGANIZATION_ADMINISTRATOR)
org = Environment.get_test_org(TestOrgVDC._client)
vdc_name = TestOrgVDC._new_vdc_name
pvdc_name = Environment.get_test_pvdc_name()
storage_profiles = [{
'name': '*',
'enabled': True,
'units': 'MB',
'limit': 0,
'default': True
}]
vdc_resource = org.create_org_vdc(
vdc_name,
pvdc_name,
storage_profiles=storage_profiles,
uses_fast_provisioning=True,
is_thin_provision=True)
TestOrgVDC._client.get_task_monitor().wait_for_success(
task=vdc_resource.Tasks.Task[0])
logger.debug('Created ovdc ' + vdc_name + '.')
# The following contraption is required to get the non admin href of
# the ovdc. vdc_resource contains the admin version of the href since
# we created the ovdc as a sys admin.
org = Environment.get_test_org(TestOrgVDC._org_admin_client)
org.reload()
for vdc in org.list_vdcs():
if vdc.get('name').lower() == vdc_name.lower():
TestOrgVDC._new_vdc_href = vdc.get('href')
TestOrgVDC._vdc1 = VDC(TestOrgVDC._org_admin_client,
href=TestOrgVDC._new_vdc_href)
self.assertIsNotNone(TestOrgVDC._new_vdc_href)
#Create Independent disk
TestOrgVDC._idisk_id = TestOrgVDC._vdc1.\
create_disk(name=self._idisk_name,
size=self._idisk_size,
description=self._idisk_description).get('id')[16:]
self.assertIsNotNone(TestOrgVDC._idisk_id)
def test_0010_list_vdc(self):
"""Test the method VDC.list_vdcs().
This test passes if the vdc created during setup can be found in the
list of vdcs retrieved.
"""
org = Environment.get_test_org(TestOrgVDC._org_admin_client)
vdc_list = org.list_vdcs()
retrieved_vdc_names = []
retrieved_vdc_hrefs = []
for vdc in vdc_list:
retrieved_vdc_names.append(vdc['name'])
retrieved_vdc_hrefs.append(vdc['href'])
self.assertIn(TestOrgVDC._new_vdc_name, retrieved_vdc_names)
self.assertIn(TestOrgVDC._new_vdc_href, retrieved_vdc_hrefs)
def test_0020_get_vdc(self):
"""Test the method VDC.get_vdc().
This test passes if the expected vdc can be successfully retrieved by
name.
"""
org = Environment.get_test_org(TestOrgVDC._org_admin_client)
vdc = org.get_vdc(TestOrgVDC._new_vdc_name)
self.assertEqual(TestOrgVDC._new_vdc_name, vdc.get('name'))
self.assertEqual(TestOrgVDC._new_vdc_href, vdc.get('href'))
def test_0021_get_vdc_admin_href(self):
"""Test the method VDC.get_vdc().
This test passes if the expected vdc admin href.
"""
org = Environment.get_test_org(TestOrgVDC._client)
vdc = org.get_vdc(name=TestOrgVDC._new_vdc_name,
is_admin_operation=True)
self.assertTrue('/api/admin/' in vdc.get('href'))
def test_0022_get_vdc_non_admin_href(self):
"""Test the method VDC.get_vdc().
This test passes if the expected vdc non admin href.
"""
org = Environment.get_test_org(TestOrgVDC._client)
vdc = org.get_vdc(TestOrgVDC._new_vdc_name)
self.assertFalse('/api/admin/' in vdc.get('href'))
def test_0030_get_non_existent_vdc(self):
"""Test the method VDC.get_vdc().
This test passes if the non-existent vdc can't be successfully
retrieved by name.
"""
org = Environment.get_test_org(TestOrgVDC._client)
try:
resource = org.get_vdc(TestOrgVDC._non_existent_vdc_name)
self.assertIsNone(resource)
except EntityNotFoundException as e:
pass
def test_0040_enable_disable_vdc(self):
"""Test the method VDC.enable_vdc().
First disable the vdc, try to re-disable it (which should fail). Next,
enable the vdc back, and then try to re-enable the vdc (which should
fail).
This test passes if the state of vdc matches our expectation after each
operation.
"""
logger = Environment.get_default_logger()
vdc = VDC(TestOrgVDC._client, href=TestOrgVDC._new_vdc_href)
# vdc should be in enabled state after the previous tests.
vdc.enable_vdc(enable=False)
logger.debug('Disabled vdc ' + TestOrgVDC._new_vdc_name + '.')
try:
logger.debug('Trying to again disable vdc ' +
TestOrgVDC._new_vdc_name + '.')
vdc.enable_vdc(enable=False)
self.fail('Should not be able to disable vdc ' +
TestOrgVDC._new_vdc_href)
except OperationNotSupportedException as e:
pass
vdc.enable_vdc(enable=True)
logger.debug('Enabled vdc ' + TestOrgVDC._new_vdc_name + '.')
try:
logger.debug('Trying to again enable vdc ' +
TestOrgVDC._new_vdc_name + '.')
vdc.enable_vdc(enable=True)
self.fail('Should not be able to enable vdc ' +
TestOrgVDC._new_vdc_href)
except OperationNotSupportedException as e:
pass
def test_0050_vdc_acl(self):
"""Test the methods related to access control list in vdc.py.
This test passes if all the acl operations are successful.
"""
logger = Environment.get_default_logger()
vdc = VDC(TestOrgVDC._client,
href=get_non_admin_href(TestOrgVDC._new_vdc_href))
vdc_name = TestOrgVDC._new_vdc_name
vapp_user_name = Environment.get_username_for_role_in_test_org(
CommonRoles.VAPP_USER)
console_user_name = Environment.get_username_for_role_in_test_org(
CommonRoles.CONSOLE_ACCESS_ONLY)
# remove all
logger.debug('Removing all access control from vdc ' + vdc_name)
control_access = vdc.remove_access_settings(remove_all=True)
self.assertFalse(hasattr(control_access, 'AccessSettings'))
# add
logger.debug('Adding 2 access control rule to vdc ' + vdc_name)
vdc.reload()
control_access = vdc.add_access_settings(
access_settings_list=[{
'name': vapp_user_name,
'type': 'user'
}, {
'name': console_user_name,
'type': 'user',
'access_level': 'ReadOnly'
}])
self.assertEqual(len(control_access.AccessSettings.AccessSetting), 2)
# get
logger.debug('Fetching access control rules for vdc ' + vdc_name)
vdc.reload()
control_access = vdc.get_access_settings()
self.assertEqual(len(control_access.AccessSettings.AccessSetting), 2)
# remove
logger.debug('Removing 1 access control rule for vdc ' + vdc_name)
control_access = vdc.remove_access_settings(
access_settings_list=[{
'name': vapp_user_name,
'type': 'user'
}])
self.assertEqual(len(control_access.AccessSettings.AccessSetting), 1)
# share
logger.debug('Sharing vdc ' + vdc_name + ' with everyone in the org')
vdc.reload()
control_access = vdc.share_with_org_members()
self.assertEqual(control_access.IsSharedToEveryone.text, 'true')
self.assertEqual(control_access.EveryoneAccessLevel.text, 'ReadOnly')
# unshare
logger.debug(
'Un-sharing vdc ' + vdc_name + ' from everyone in the org')
vdc.reload()
control_access = vdc.unshare_from_org_members()
self.assertEqual(control_access.IsSharedToEveryone.text, 'false')
# re-share, before performing any other ACL operation to avoid
# running into https://github.com/vmware/pyvcloud/issues/279
logger.debug('Re-sharing vdc ' + vdc_name + ' with everyone in the ' +
'org')
vdc.reload()
control_access = vdc.share_with_org_members()
self.assertEqual(control_access.IsSharedToEveryone.text, 'true')
self.assertEqual(control_access.EveryoneAccessLevel.text, 'ReadOnly')
# remove the last access setting
logger.debug('Removing the last remaining access control from'
' vdc ' + vdc_name)
vdc.reload()
control_access = vdc.remove_access_settings(remove_all=True)
self.assertFalse(hasattr(control_access, 'AccessSettings'))
def test_0060_vdc_metadata(self):
"""Test the methods related to metadata manipulation in vdc.py.
This test passes if all the metadata operations are successful.
"""
vapp_author_client = None
sys_admin_client = None
try:
logger = Environment.get_default_logger()
vapp_author_client = Environment.get_client_in_default_org(
CommonRoles.VAPP_AUTHOR)
vdc_vapp_author_view = VDC(client=vapp_author_client,
href=get_non_admin_href(
TestOrgVDC._new_vdc_href))
sys_admin_client = Environment.get_sys_admin_client()
vdc_sys_admin_view = VDC(client=sys_admin_client,
href=TestOrgVDC._new_vdc_href)
# try to add new metadata as vapp author
try:
logger.debug(f'Adding metadata [key={TestOrgVDC._metadata_key}'
', value={TestOrgVDC._metadata_value}]) as vApp '
'author')
vdc_vapp_author_view.set_metadata(
key=TestOrgVDC._metadata_key,
value=TestOrgVDC._metadata_value)
self.assertFail('vApp author shouldn\'t have been able to '
'add new metadta entry.')
except OperationNotSupportedException as e:
pass
# add new metadata as sys admin
logger.debug(f'Adding metadata [key={TestOrgVDC._metadata_key},'
'value={TestOrgVDC._metadata_value}]) as Sys admin.')
task = vdc_sys_admin_view.set_metadata(
key=TestOrgVDC._metadata_key,
value=TestOrgVDC._metadata_value)
result = sys_admin_client.get_task_monitor().wait_for_success(task)
self.assertEqual(result.get('status'), TaskStatus.SUCCESS.value)
# retrieve metadata as vapp author
logger.debug(f'Retriving metadata with key='
'{TestOrgVDC._metadata_key} as vApp author.')
metadata_value = vdc_vapp_author_view.get_metadata_value(
key=TestOrgVDC._metadata_key)
self.assertEqual(extract_metadata_value(metadata_value),
TestOrgVDC._metadata_value)
# try to retrieve non existent metadata entry
try:
logger.debug(f'Retriving metadata with invalid key='
'{TestOrgVDC._non_existent_metadata_key} as vApp '
'author')
metadata_value = vdc_vapp_author_view.get_metadata_value(
key=TestOrgVDC._non_existent_metadata_key)
self.assertFail('Shouldn\'t have been able to retrieve metadta'
' entry with bad key.')
except AccessForbiddenException as e:
pass
# try to update metadata value as vapp author
try:
logger.debug(f'Trying to update metadata with key='
'{TestOrgVDC._metadata_key} to value='
'{TestOrgVDC._metadata_new_value} as vApp '
'author.')
vdc_vapp_author_view.set_metadata(
key=TestOrgVDC._metadata_key,
value=TestOrgVDC._metadata_new_value)
self.assertFail('Shouldn\'t have been able to update metadta'
' entry as vApp author.')
except OperationNotSupportedException as e:
pass
# update metadata value as sys admin
logger.debug(f'Updtaing metadata with key='
'{TestOrgVDC._metadata_key} to value='
'{TestOrgVDC._metadata_new_value} as Sys Admin.')
task = vdc_sys_admin_view.set_metadata(
key=TestOrgVDC._metadata_key,
value=TestOrgVDC._metadata_new_value)
sys_admin_client.get_task_monitor().wait_for_success(task)
metadata_value = vdc_sys_admin_view.get_metadata_value(
key=TestOrgVDC._metadata_key)
self.assertEqual(extract_metadata_value(metadata_value),
TestOrgVDC._metadata_new_value)
# try to remove metadata as vapp author
try:
logger.debug(f'Trying to remove metadata with key='
'{TestOrgVDC._metadata_key} as vApp author.')
task = vdc_vapp_author_view.remove_metadata(
key=TestOrgVDC._metadata_key)
self.assertFail('Shouldn\'t have been able to remove metadta '
'entry as vApp author.')
except OperationNotSupportedException as e:
pass
# remove metadata entry as sys admin
logger.debug(f'Removing metadata with '
'key={TestOrgVDC._metadata_key},as Sys Admin.')
task = vdc_sys_admin_view.remove_metadata(
key=TestOrgVDC._metadata_key)
result = sys_admin_client.get_task_monitor().wait_for_success(task)
self.assertEqual(result.get('status'), TaskStatus.SUCCESS.value)
finally:
if sys_admin_client is not None:
sys_admin_client.logout()
if vapp_author_client is not None:
vapp_author_client.logout()
def test_0070_list_media(self):
"""Test the method VDC.list_media_id().
This test passes if it lists vdc medias and its id.
"""
logger = Environment.get_default_logger()
vdc = Environment.get_test_vdc(TestOrgVDC._client)
vdc.reload()
vdc_resource = vdc.get_resource()
vdc = VDC(TestOrgVDC._client, href=vdc_resource.get('href'))
media_list = vdc.list_media_id()
self.assertTrue(len(media_list) > 0)
def test_0080_list_idisk(self):
"""Test the method VDC.list_idisk().
This test passes if it lists vdc idisk and its id.
"""
vdc = VDC(TestOrgVDC._client, href=TestOrgVDC._new_vdc_href)
idisk_list = vdc.list_idisk()
self.assertTrue(len(idisk_list) > 0)
@developerModeAware
def test_9998_teardown(self):
"""Test the method VDC.delete_vdc().
Invoke the method for the vdc created by setup.
This test passes if the task for deleting the vdc succeeds.
"""
logger = Environment.get_default_logger()
vdc = VDC(TestOrgVDC._client, href=TestOrgVDC._new_vdc_href)
task = vdc.delete_disk(name=self._idisk_name)
TestOrgVDC._client.get_task_monitor().wait_for_success(task=task)
# Disable the org vdc before deleting it. In case the org vdc is
# already disabled, we don't want the exception to leak out.
try:
vdc.enable_vdc(enable=False)
logger.debug('Disabled vdc ' + TestOrgVDC._new_vdc_name + '.')
except OperationNotSupportedException as e:
logger.debug('vdc ' + TestOrgVDC._new_vdc_name +
' is already disabled.')
pass
task = vdc.delete_vdc()
TestOrgVDC._client.get_task_monitor().wait_for_success(task=task)
logger.debug('Deleted vdc ' + TestOrgVDC._new_vdc_name + '.')
def test_9999_cleanup(self):
"""Release all resources held by this object for testing purposes."""
TestOrgVDC._client.logout()