forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtestpath_vMotion_vmware.py
More file actions
2985 lines (2752 loc) · 136 KB
/
testpath_vMotion_vmware.py
File metadata and controls
2985 lines (2752 loc) · 136 KB
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
""" Test cases for Test Paths Storage Migration
"""
from nose.plugins.attrib import attr
from marvin.cloudstackTestCase import cloudstackTestCase
import unittest
from marvin.lib.utils import (cleanup_resources,
validateList,
is_server_ssh_ready
)
from marvin.lib.base import (Account,
ServiceOffering,
DiskOffering,
Volume,
Template,
VirtualMachine,
StoragePool,
Snapshot,
VmSnapshot,
Configurations,
Host,
NATRule,
FireWallRule
)
from marvin.lib.common import (get_domain,
get_zone,
get_template,
list_volumes,
list_virtual_machines,
list_clusters,
list_storage_pools,
list_hosts,
get_windows_template,
list_publicIP,
list_nat_rules,
list_ssvms
)
from marvin.cloudstackAPI import (enableStorageMaintenance,
cancelStorageMaintenance
)
from marvin.sshClient import SshClient
from marvin.codes import FAILED, PASS, FAIL
from ddt import ddt, data, unpack
import time
from threading import Thread
def MigrateDataVolume(self,
volume,
destinationPool,
islive=False,
expectexception=False
):
""" Migrate given volume to type of storage pool mentioned in migrateto:
Inputs:
1. volume: Volume to be migrated
2. migrate_to: Scope of desired Storage pool to which volume
is to be migrated
3. expectexception: If exception is expected while migration
"""
if expectexception:
with self.assertRaises(Exception):
Volume.migrate(
self.apiclient,
volumeid=volume.id,
storageid=destinationPool.id,
livemigrate=islive
)
else:
Volume.migrate(
self.apiclient,
volumeid=volume.id,
storageid=destinationPool.id,
livemigrate=islive
)
migrated_volume_response = list_volumes(
self.apiclient,
id=volume.id
)
self.assertEqual(
isinstance(migrated_volume_response, list),
True,
"Check list volumes response for valid list"
)
self.assertNotEqual(
migrated_volume_response,
None,
"Check if volume exists in ListVolumes"
)
migrated_volume = migrated_volume_response[0]
self.assertEqual(
str(migrated_volume.state).lower(),
'ready',
"Check migrated volume is in Ready state"
)
self.assertEqual(
migrated_volume.storage,
destinationPool.name,
"Check volume is on migrated pool"
)
return
def VmSnapshotToCheckDataIntegrity(self, vm):
"""
This method takes VMSnapshot of the VM post migration
to check data integrity.
VM snapshot is not possible if VM's volumes have snapshots.
So, first we will check if there are any volume
snapshots after migration and delete them if
there are any. Once VM snapshot is successful,
Delete the VM snapshot
"""
volumes = list_volumes(self.apiclient, virtualmachineid=vm.id,
listall=True)
for vol in volumes:
snapshot = Snapshot.list(self.apiclient, volumeid=vol.id,
listall=True)
if(snapshot):
for snap in snapshot:
try:
Snapshot.deletesnap(self.apiclient, snapid=snap.id)
except Exception as e:
raise Exception("Warning: Exception during Volume snapshot deletion : %s" % e)
#Take VM snapshot to check data integrity
try:
vm_snapshot = VmSnapshot.create(self.apiclient, vmid=vm.id)
except Exception as e:
raise Exception("Warning: Exception during VM snapshot creation : %s" % e)
#Delete the snapshot
try:
VmSnapshot.deleteVMSnapshot(self.apiclient, vmsnapshotid=vm_snapshot.id)
except Exception as e:
raise Exception("Warning: Exception during VM snapshot deletion : %s" % e)
return
def MigrateVmWithVolume(self, vm, destinationHost, volumes, pools):
"""
This method is used to migrate a vm and its volumes using migrate virtual machine with volume API
INPUTS:
1. vm -> virtual machine object
2. destinationHost -> the host to which VM will be migrated
3. volumes -> list of volumes which are to be migrated
4. pools -> list of destination pools
"""
if not destinationHost:
self.debug("Destination host is NULL so migration can't be performed")
return
vol_pool_map = {}
for vol, pool in zip(volumes, pools):
vol_pool_map.update({vol.id: pool.id})
vm.migrate_vm_with_volume(
self.apiclient,
hostid=destinationHost.id,
migrateto=vol_pool_map
)
vm.getState(
self.apiclient,
"Running"
)
#check for the VM's host and volume's storage post migration
migrated_vm_response = list_virtual_machines(self.apiclient, id=vm.id)
self.assertEqual(
isinstance(migrated_vm_response, list),
True,
"Check list virtual machines response for valid list"
)
self.assertEqual(
migrated_vm_response[0].hostid,
destinationHost.id,
"VM did not migrate to a specified host"
)
for vol, pool in zip(volumes, pools):
migrated_volume_response = list_volumes(self.apiclient, virtualmachineid=migrated_vm_response[0].id, name=vol.name, listall=True)
self.assertEqual(
isinstance(migrated_volume_response, list),
True,
"Check list virtual machines response for valid list"
)
self.assertEqual(
migrated_volume_response[0].storageid,
pool.id,
"Volume did not migrate to a specified pool"
)
self.assertEqual(
str(migrated_volume_response[0].state).lower(),
'ready',
"Check migrated volume is in Ready state"
)
return migrated_vm_response[0]
def MigrateVm(self, vm, destinationHost):
"""
This method is to migrate a VM using migrate virtual machine API
"""
if not destinationHost:
self.debug("Destination host is NULL so migration can't be performed")
return
vm.migrate(
self.apiclient,
hostid=destinationHost.id,
)
vm.getState(
self.apiclient,
"Running"
)
#check for the VM's host and volume's storage post migration
migrated_vm_response = list_virtual_machines(self.apiclient, id=vm.id)
self.assertEqual(
isinstance(migrated_vm_response, list),
True,
"Check list virtual machines response for valid list"
)
self.assertEqual(
migrated_vm_response[0].hostid,
destinationHost.id,
"VM did not migrate to a specified host"
)
return migrated_vm_response[0]
def get_destination_pools_hosts(self, vm, storage_scope, storage_type):
"""
Get destination Pools for all volumes and destination Host for the VM
This method is use in case we use the API migrate volume with storage
"""
destinationPools = []
vol_list = list_volumes(self.apiclient, virtualmachineid=vm.id, listall=True)
# For each volume get destination pool
for vol in vol_list:
pool = GetDestinationStoragePool(self, vol.storage, storage_scope, storage_type)
destinationPools.append(pool)
#Get destination host
destinationHost = self.GetDestinationHost(vm.hostid, vm, storage_scope)
return destinationHost, destinationPools, vol_list
def check_files(self, vm, destinationHost):
"""
Check for VMX and VMDK files
INPUTS :
1. vm -> The Virtual Machine object
2. destinationHost -> The host to which we want to migrate the VM
"""
# list volumes and their pools
# Here we list all the volumes of the VM , then login to the destination host
# and check for vmx and vmdk files in the storage
vm_volumes = list_volumes(self.apiclient, virtualmachineid=vm.id, listall=True)
for vol in vm_volumes:
spool = list_storage_pools(self.apiclient, id=vol.storageid)
split_path = spool[0].path.split("/")
pool_path = split_path[2]
if spool[0].type == "NetworkFilesystem":
pool_path = spool[0].id.replace("-", "")
sshclient = SshClient(
host=destinationHost.ipaddress,
port=self.testdata['configurableData']['host']["publicport"],
user=self.testdata['configurableData']['host']["username"],
passwd=self.testdata['configurableData']['host']["password"],
)
pool_data_vmdk = sshclient.execute("ls /vmfs/volumes/" + pool_path + "/" + vm.instancename + "| grep vmdk")
pool_data_vmx = sshclient.execute("ls /vmfs/volumes/" + pool_path + "/" + vm.instancename + "| grep vmx")
self.debug("------------------------volume's actual path is: %s" % vol.path)
vol_path_db = self.dbclient.execute("select path from volumes where uuid='%s';" % vol.id)
self.debug("-----------------------volume's path in DB is: %s" % vol_path_db)
vol_name_db = self.dbclient.execute("select name from volumes where uuid='%s';" % vol.id)
self.debug("-----------------------volume's name in DB is: %s" % vol_name_db)
if(pool_data_vmx):
vmx_file = vm.instancename + ".vmx"
if vol.type == "ROOT":
self.assertIn(
vmx_file,
pool_data_vmx,
"The VMX files are missing"
)
if(pool_data_vmdk):
vmdk_file1 = vol.path + ".vmdk"
vmdk_file2 = vol.path + "-flat.vmdk"
self.assertIn(
vmdk_file1,
pool_data_vmdk,
"The VMDK files are missing"
)
self.assertIn(
vmdk_file2,
pool_data_vmdk,
"The VMDK flat files are missing"
)
return
def GetDestinationStoragePool(self, poolsToavoid, storage_scope, storage_type):
""" Get destination pool which has scope same as migrateto
and which is not in avoid set
"""
destinationPool = None
destinationCluster = None
if storage_scope == "within_cluster" or storage_scope == "across_cluster":
scope = "CLUSTER"
else:
scope = "ZONE"
pool = list_storage_pools(self.apiclient, name=poolsToavoid)
clusters = list_clusters(self.apiclient, listall=True)
if storage_scope == "across_cluster":
for cluster in clusters:
if cluster.id not in pool[0].clusterid:
if len(list_storage_pools(self.apiclient, clusterid=cluster.id)) > 0:
destinationCluster = cluster
break
pools_in_cluster = list_storage_pools(self.apiclient, clusterid=destinationCluster.id, scope=scope)
for pool in pools_in_cluster:
if pool.type == storage_type:
destinationPool = pool
break
return destinationPool
elif storage_scope == "within_cluster":
destinationCluster = list_clusters(self.apiclient, id=pool[0].clusterid, listall=True)[0]
storagepools = list_storage_pools(self.apiclient, clusterid=destinationCluster.id, scope=scope)
for pool in storagepools:
if pool.name not in poolsToavoid and pool.type == storage_type:
destinationPool = pool
return destinationPool
elif storage_scope == "ZONE":
storagepools = list_storage_pools(self.apiclient, scope=scope)
for pool in storagepools:
if pool.name not in poolsToavoid and pool.type == storage_type:
destinationPool = pool
return destinationPool
def restart_mgmt_server(self, hostip, port, username, password):
"""Restarts the management server"""
try:
# Get the SSH client
ssh = is_server_ssh_ready(
hostip,
port,
username,
password,
)
result = ssh.execute("/etc/init.d/cloudstack-management restart")
res = str(result)
# Server Stop - OK
# Server Start - OK
if res.count("OK") != 2:
raise ("ErrorInReboot!")
except Exception as e:
raise e
return
def check_host_capacity(self, hostid, vm):
"""Checks whether host has enough capacity to migrate the VM
"""
host = list_hosts(self.apiclient, id=hostid, listall=True)[0]
host_memory_available_in_MB = (host.memorytotal - host.memoryallocated) / 1024 * 1024 * 0.8
memory_of_vm = vm.memory
host_cpu_available_in_MHz = (host.cpuspeed - host.cpuspeed * float(host.cpuallocated.replace("%", "")) / 100) * 0.8
cpu_of_vm = vm.cpuspeed
if host_memory_available_in_MB > memory_of_vm and host_cpu_available_in_MHz > cpu_of_vm:
return PASS
else:
return FAILED
def check_for_vm_access_by_ssh_using_nat(self, virtual_machine_1, ostype=None):
"""
This function allocated a public ip, and creates a nat rule for the VM
Then tries to ssh into the VM using that public IP
This function again is to check VM accessibility post migration
"""
if ostype == "windows":
self.debug("SSH check on the VM can't be done as it is a windows VM")
return
src_nat_ip_addrs = list_publicIP(
self.apiclient,
account=self.account.name,
domainid=self.account.domainid
)
self.assertEqual(
isinstance(src_nat_ip_addrs, list),
True,
"Check list response returns a valid list"
)
src_nat_ip_addr = src_nat_ip_addrs[0]
# Open up firewall port for SSH
firewall_rule = FireWallRule.create(
self.apiclient,
ipaddressid=src_nat_ip_addr.id,
protocol=self.testdata["natrule"]["protocol"],
cidrlist=['0.0.0.0/0'],
startport=self.testdata["natrule"]["publicport"],
endport=self.testdata["natrule"]["publicport"]
)
# Create NAT rule
nat_rule = NATRule.create(
self.apiclient,
virtual_machine_1,
self.testdata["natrule"],
src_nat_ip_addr.id
)
list_nat_rule_response = list_nat_rules(
self.apiclient,
id=nat_rule.id
)
self.assertEqual(
isinstance(list_nat_rule_response, list),
True,
"Check list response returns a valid list"
)
self.assertNotEqual(
len(list_nat_rule_response),
0,
"Check Port Forwarding Rule is created"
)
self.assertEqual(
list_nat_rule_response[0].id,
nat_rule.id,
"Check Correct Port forwarding Rule is returned"
)
# SSH virtual machine to test port forwarding
try:
self.debug("SSHing into VM with IP address %s with NAT IP %s" %
(
virtual_machine_1.ipaddress,
src_nat_ip_addr.ipaddress
))
virtual_machine_1.get_ssh_client(src_nat_ip_addr.ipaddress)
vm_response = VirtualMachine.list(
self.apiclient,
id=virtual_machine_1.id
)
if vm_response[0].state != 'Running':
self.fail(
"State of VM : %s is not found to be Running" % str(
virtual_machine_1.ipaddress))
except Exception as e:
self.fail(
"SSH Access failed for %s: %s" %
(virtual_machine_1.ipaddress, e)
)
try:
nat_rule.delete(self.apiclient)
except Exception as e:
self.fail("NAT Rule Deletion Failed: %s" % e)
try:
firewall_rule.delete(self.apiclient)
except Exception as e:
self.fail("Firewall Rule Deletion Failed: %s" % e)
return
@ddt
class TestStorageLiveMigrationVmware(cloudstackTestCase):
@classmethod
def setUpClass(cls):
testClient = super(TestStorageLiveMigrationVmware, cls).getClsTestClient()
cls.apiclient = testClient.getApiClient()
cls.testdata = testClient.getParsedTestDataConfig()
cls.hypervisor = cls.testClient.getHypervisorInfo()
cls.dbclient = cls.testClient.getDbConnection()
cls.exceptionList = []
# Get Zone, Domain and templates
cls.domain = get_domain(cls.apiclient)
cls.zone = get_zone(cls.apiclient, testClient.getZoneForTests())
cls.template = get_template(
cls.apiclient,
cls.zone.id,
cls.testdata["ostype"])
cls._cleanup = []
cls.skiptest = False
cls.skipReason = ""
if cls.hypervisor.lower() not in [
"vmware",
"kvm",
"xenserver",
"hyper-v"]:
cls.skiptest = True
cls.skipReason = "Storage Migration not supported on %s" % cls.hypervisor
return
# Get Hosts in the cluster and iscsi/vmfs storages for that cluster
iscsi_pools = []
nfs_pools = []
try:
cls.list_vmware_clusters = list_clusters(cls.apiclient, hypervisor="vmware")
except Exception as e:
raise unittest.SkipTest(e)
if len(cls.list_vmware_clusters) < 1:
cls.skiptest = True
cls.skipReason = "There is no cluster available in the setup"
else:
for cluster in cls.list_vmware_clusters:
try:
list_esx_hosts = list_hosts(cls.apiclient, clusterid=cluster.id)
except Exception as e:
raise unittest.SkipTest(e)
if len(list_esx_hosts) > 1:
try:
list_storage = list_storage_pools(cls.apiclient, clusterid=cluster.id)
except Exception as e:
raise unittest.SkipTest(e)
for storage in list_storage:
if storage.type == "VMFS":
iscsi_pools.append(storage)
if len(iscsi_pools) > 1:
break
else:
iscsi_pools = []
for storage in list_storage:
if storage.type == "NetworkFilesystem":
nfs_pools.append(storage)
if len(nfs_pools) > 1:
break
else:
nfs_pools = []
if len(iscsi_pools) < 2 and len(nfs_pools) < 2:
cls.skiptest = True
cls.skipReason = "Not enough storage pools available in the setup"
cls.hosts = list_esx_hosts
cls.pools = list_storage
# Create an account
cls.account = Account.create(
cls.apiclient,
cls.testdata["account"],
domainid=cls.domain.id
)
cls._cleanup.append(cls.account)
# Create Service offering
cls.service_offering = ServiceOffering.create(
cls.apiclient,
cls.testdata["service_offering"]
)
cls._cleanup.append(cls.service_offering)
if cls.zone.localstorageenabled:
cls.testdata["service_offering"]["storagetype"] = 'local'
cls.service_offering_local1 = ServiceOffering.create(
cls.apiclient,
cls.testdata["service_offering"]
)
cls._cleanup.append(cls.service_offering_local1)
# Create Disk offering
cls.disk_offering = DiskOffering.create(
cls.apiclient,
cls.testdata["disk_offering"]
)
cls._cleanup.append(cls.disk_offering)
# Create disk offering for resize
cls.resized_disk_offering = DiskOffering.create(
cls.apiclient,
cls.testdata["resized_disk_offering"]
)
cls._cleanup.append(cls.resized_disk_offering)
if cls.zone.localstorageenabled:
cls.testdata["disk_offering"]["storagetype"] = 'local'
cls.disk_offering_local1 = DiskOffering.create(
cls.apiclient,
cls.testdata["disk_offering"]
)
cls._cleanup.append(cls.disk_offering_local1)
# Register windows 2012 server Template if it is not present
cls.windows_template = get_windows_template(
cls.apiclient,
cls.zone.id,
ostype_desc="Windows Server 2012 (64-bit)",
template_type="USER",
hypervisor="VMware",
template_filter="all"
)
#cls.template = get_windows_template(cls.apiclient, cls.zone.id ,ostype_desc="Windows Server 2012 (64-bit)")
cls.testdata["vgpu"]["Windows Server 2012 (64-bit)"]["url"] = "http://10.147.28.7/templates/Windows2012/WindowsServer2012.ova"
cls.testdata["vgpu"]["Windows Server 2012 (64-bit)"]["format"] = "OVA"
if cls.windows_template == FAILED:
if "http://pleaseupdateURL/dummy.vhd" in cls.testdata[
"vgpu"]["Windows Server 2012 (64-bit)"]["url"]:
cls.skiptest = True
cls.skipReason = "Check Test Data file if it has the valid template URL"
cls.windows_template = Template.register(
cls.apiclient,
cls.testdata["vgpu"]["Windows Server 2012 (64-bit)"],
hypervisor="VMware",
zoneid=cls.zone.id,
)
timeout = cls.testdata["vgpu"]["timeout"]
while True:
time.sleep(cls.testdata["vgpu"]["sleep"])
list_template_response = Template.list(
cls.apiclient,
templatefilter=cls.testdata["templatefilter"],
id=cls.windows_template.id
)
if (isinstance(list_template_response, list)) is not True:
raise unittest.SkipTest(
"Check list template api response returns a valid list")
if len(list_template_response) is None:
raise unittest.SkipTest(
"Check template registered is in List Templates")
template_response = list_template_response[0]
if template_response.isready:
break
if timeout == 0:
cls.debug("Failed to download windows template, we will be skipping windows related tests below")
timeout = timeout - 1
return
@classmethod
def tearDownClass(cls):
try:
cleanup_resources(cls.apiclient, cls._cleanup)
except Exception as e:
raise Exception("Warning: Exception during cleanup : %s" % e)
def setUp(self):
self.apiclient = self.testClient.getApiClient()
self.dbclient = self.testClient.getDbConnection()
self.cleanup = []
if self.skiptest:
self.skipTest(self.skipReason)
def tearDown(self):
try:
cleanup_resources(self.apiclient, self.cleanup)
except Exception as e:
raise Exception("Warning: Exception during cleanup : %s" % e)
# Cancel maintenance state of all hosts
list_host = list_hosts(self.apiclient, listall=True)
for host in list_host:
if host.resourcestate == "Maintenance":
Host.cancelMaintenance(self.apiclient, id=host.id)
Host.getState(
self.apiclient,
host.id,
"Up",
"Enabled"
)
# Cancel maintenance state of all storage pools
list_pools = list_storage_pools(self.apiclient, listall=True)
for pool in list_pools:
if pool.state == "Maintenance":
cmd = cancelStorageMaintenance.cancelStorageMaintenanceCmd()
cmd.id = pool.id
self.apiclient.cancelStorageMaintenance(cmd)
StoragePool.getState(
self.apiclient,
pool.id,
"Up"
)
def get_ssvm_state(self, apiclient, vmid, state, timeout=600):
"""List VM and check if its state is as expected
@returnValue - List[Result, Reason]
1) Result - FAIL if there is any exception
in the operation or VM state does not change
to expected state in given time else PASS
2) Reason - Reason for failure"""
returnValue = [FAIL, "VM state not trasited to %s,\
operation timed out" % state]
while timeout > 0:
try:
projectid = None
if hasattr(self, "projectid"):
projectid = self.projectid
vms = list_ssvms(self.apiclient, projectid=projectid,
id=vmid, listAll=True)
validationresult = validateList(vms)
if validationresult[0] == FAIL:
raise Exception("VM list validation failed: %s" % validationresult[2])
elif str(vms[0].state).lower().decode("string_escape") == str(state).lower():
returnValue = [PASS, None]
break
except Exception as e:
returnValue = [FAIL, e]
break
time.sleep(60)
timeout -= 60
return returnValue
def deploy_virtual_machine(self, service_offering_id, vm, template_id):
"""
Function to Deploy VMs
"""
virtual_machine = VirtualMachine.create(
self.apiclient,
self.testdata[vm],
accountid=self.account.name,
zoneid=self.zone.id,
domainid=self.account.domainid,
serviceofferingid=service_offering_id,
templateid=template_id,
hostid=self.hosts[0].id
)
virtual_machine.getState(
self.apiclient,
"Running"
)
return virtual_machine
def GetDestinationHost(self, hostsToavoid, vm, scope):
"""
This method gives us the destination host to which VM will be migrated
It takes the source host i.e. hostsToavoid as input
"""
destinationHost = None
destinationCluster = None
host = list_hosts(self.apiclient, id=hostsToavoid)
clusters = list_clusters(self.apiclient, listall=True)
if scope == "across_cluster":
for cluster in clusters:
if cluster.id not in host[0].clusterid:
hosts_in_cluster = list_hosts(self.apiclient, clusterid=cluster.id)
if len(hosts_in_cluster) != 0:
destinationCluster = cluster
break
hosts = list_hosts(self.apiclient, clusterid=destinationCluster.id)
for host in hosts:
response = check_host_capacity(self, host.id, vm)
if response == PASS:
destinationHost = host
return destinationHost
elif scope == "within_cluster":
hosts = list_hosts(self.apiclient, clusterid=host[0].clusterid)
for host in hosts:
response = check_host_capacity(self, host.id, vm)
if host.id not in hostsToavoid and response is PASS:
destinationHost = host
break
return destinationHost
def GetDestinationHostLocal(self, hostsToavoid, vm, scope):
"""
This method gives us the destination host to which VM will be migrated
It takes the souce host i.e. hostsToavoid as input
"""
destinationHost = None
destinationCluster = None
if scope == "across_cluster":
host = list_hosts(self.apiclient, id=hostsToavoid)
clusters = list_clusters(self.apiclient, listall=True)
for cluster in clusters:
if cluster.id not in host[0].clusterid:
hosts_in_cluster = list_hosts(self.apiclient, clusterid=cluster.id)
if len(hosts_in_cluster) != 0:
destinationCluster = cluster
break
hosts = list_hosts(self.apiclient, clusterid=destinationCluster.id)
for host in hosts:
response = check_host_capacity(self, host.id, vm)
if host.id not in hostsToavoid and response == PASS:
pool = list_storage_pools(self.apiclient, scope="Host", name=host.name + " Local Storage")
if pool:
destinationHost = host
break
return destinationHost
for host in self.hosts:
response = check_host_capacity(self, host.id, vm)
if host.id not in hostsToavoid and response == PASS:
pool = list_storage_pools(self.apiclient, scope="Host", name=host.name + " Local Storage")
if pool:
destinationHost = host
break
return destinationHost
def takeVmSnapshotNegative(self, vm_id):
"""
This method takes VM snapshots and stores the exception
To be used in the negative scenario where we take snapshot when
migration is in progress
"""
try:
with self.assertRaises(Exception):
VmSnapshot.create(self.apiclient, vmid=vm_id)
except Exception as e:
self.exceptionList.append(e)
def resizeVolumeNegative(self, volume):
"""
This method resizes volume and stores the exception
To be used in the negative scenario where we resize a volume when
migration is in progress
"""
try:
with self.assertRaises(Exception):
volume.resize(self.apiclient, diskofferingid=self.resized_disk_offering.id)
except Exception as e:
self.exceptionList.append(e)
def takeVolumeSnapshotNegative(self, volumeid):
"""
This method takes volume snapshots and stores the exception
To be used in the negative scenario where we take snapshot when
migration is in progress
"""
try:
with self.assertRaises(Exception):
Snapshot.create(self.apiclient, volume_id=volumeid)
except Exception as e:
self.exceptionList.append(e)
def stopVmNegative(self, vm):
"""
This method tries to destroy a VM and stores the exception
To be used in the negative scenario where destroy a VM when
migration is in progress
"""
try:
with self.assertRaises(Exception):
vm.stop(self.apiclient)
except Exception as e:
self.exceptionList.append(e)
@data(('VMFS', 'within_cluster', 'linux'), ('VMFS', 'within_cluster', 'windows'), ('VMFS', 'across_cluster', 'linux'), ('VMFS', 'across_cluster', 'windows'),
('NetworkFilesystem', 'within_cluster', 'linux'), ('NetworkFilesystem', 'within_cluster', 'windows'), ('NetworkFilesystem', 'across_cluster', 'linux'),
('NetworkFilesystem', 'across_cluster', 'windows'))
@unpack
@attr(tags=["advanced", "basic", "vmware", "vmfs", "shared"], required_hardware="true")
def test_01_vm_and_volumes_live_migration_for_vmware_vmfs(self, first_value, second_value, third_value):
"""
This Test Path tests vMotion for NFS as well as VMFS within cluster,
across cluster and for both windows and linux VMs using DATA DRIVEN TESTING.
This test will run once for each of the 8 configurations give as @data
1. Migrate VM from one host to another
2. Migrate VMs ROOT volume from one storage to another
3. Migrate VM to another Host and ROOT volume to another storage
4. Attach a data disk to VM, migrate VM to a different host and its volumes to different pools.
5. Upload a volume, attach it to VM, migrate VM to a different host and its volumes to different pools.
6. Create volume snapshots on all volumes , migrate VM to a different host and its volumes to different pools.
7. Resize the data disk, migrate VM to a different host and its volumes to different pools.
8. Restore the VM, migrate VM to a different host and its volumes to different pools.
9. Detach the data disk, create another VM, attach the data disk to that VM and then migrate that VM and its volumes.
10. Detach upload volume, attach it to the 2nd VM, and then migrate that VM and its volumes.
11. Create snapshots for all volumes of 2nd vM, then migrate VM and its volumes.
After each storage migration step, following validation is done
a) Create VM snapshots to check data integrity - @method used : VmSnapshotToCheckDataIntegrity(self, vm)
b) Login to the Host/storage pool and check for the VMDK and VMX files for VM and its volumes - @method used : check_files(self, vm, destinationHost)
c) Check for VM accessibility by sshing to the VM - @method used : check_for_vm_access_by_ssh_using_nat(self, virtual_machine_1)
"""
storage_type = first_value
storage_scope = second_value
ostype = third_value
if ostype == 'windows' and not self.windows_template:
self.skipTest("Windows template is not present, so skipping this test")
elif ostype == 'windows':
template_id = self.windows_template.id
else:
template_id = self.template.id
count_host = 0
count_pool = 0
storage_pool = []
if len(self.list_vmware_clusters) < 2:
if (storage_scope == "across_cluster"):
raise self.skipTest("The setup doesn't have more than one cluster, so can't execute these set of tests")
if len(self.list_vmware_clusters) >= 2:
for cluster in self.list_vmware_clusters:
if len(list_hosts(self.apiclient, clusterid=cluster.id)) >= 1:
count_host += 1
pools = list_storage_pools(self.apiclient, clusterid=cluster.id)
for pool in pools:
if pool.storage == storage_type:
storage_pool.append(pool)
if len(storage_pool) >= 1:
count_pool += 1
storage_pool = []
#if storage_scope == "across_cluster":
if count_host < 2 or count_pool < 2:
raise self.skipTest("The setup doesn't have enough pools or enough hosts. To run these tests the setup must have atleast 2 clusters, \
each having min 1 host and 1 vmfs storage pools")
self.debug("---------------This is the test no 1--------------")
"""
Create a VM, live migrate the VM
"""
vm = "virtual_machine2"
virtual_machine_1 = self.deploy_virtual_machine(self.service_offering.id, vm, template_id)
self.cleanup.append(virtual_machine_1)
vm = list_virtual_machines(self.apiclient, id=virtual_machine_1.id, listall=True)[0]
#Get destination host
destinationHost = self.GetDestinationHost(vm.hostid, virtual_machine_1, storage_scope)
#Migrate the VM
if storage_scope == "across_cluster":
vol_list = []
destinationPools = []
vm = MigrateVmWithVolume(self, virtual_machine_1, destinationHost, vol_list, destinationPools)
VmSnapshotToCheckDataIntegrity(self, vm)
check_files(self, vm, destinationHost)
check_for_vm_access_by_ssh_using_nat(self, virtual_machine_1, ostype)
else:
vm = MigrateVm(self, virtual_machine_1, destinationHost)
check_for_vm_access_by_ssh_using_nat(self, virtual_machine_1, ostype)
self.debug("---------------This is the test no 2--------------")
"""
Migrate the ROOT Volume
Can't migrate a volume to another cluster, so won't run this test in that case
"""
# Get ROOT volume and destination pool
if storage_scope != "across_cluster":
vol_list = list_volumes(self.apiclient, virtualmachineid=vm.id, type="ROOT", listall=True)
root_vol = vol_list[0]
destinationPool = GetDestinationStoragePool(self, root_vol.storage, storage_scope, storage_type)
#Migrate ROOT volume
islive = True
MigrateDataVolume(self, root_vol, destinationPool, islive)
VmSnapshotToCheckDataIntegrity(self, vm)
check_files(self, vm, destinationHost)
check_for_vm_access_by_ssh_using_nat(self, virtual_machine_1, ostype)
self.debug("---------------This is the test no 3--------------")
"""
Migrate the VM and ROOT volume
"""
#Get all volumes to be migrated
vm = list_virtual_machines(self.apiclient, id=virtual_machine_1.id, listall=True)[0]
destinationHost, destinationPools, vol_list = get_destination_pools_hosts(self, vm, storage_scope, storage_type)
vm = MigrateVmWithVolume(self, virtual_machine_1, destinationHost, vol_list, destinationPools)
VmSnapshotToCheckDataIntegrity(self, vm)
check_files(self, vm, destinationHost)
check_for_vm_access_by_ssh_using_nat(self, virtual_machine_1, ostype)
self.debug("---------------This is the test no 4--------------")
"""
Add a data disk and migrate vm, data disk and root disk
"""
data_disk_1 = Volume.create(
self.apiclient,