forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_snapshots.py
More file actions
1346 lines (1215 loc) · 44.9 KB
/
test_snapshots.py
File metadata and controls
1346 lines (1215 loc) · 44.9 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.
""" P1 tests for Snapshots
"""
# Import Local Modules
from nose.plugins.attrib import attr
from marvin.cloudstackTestCase import cloudstackTestCase
from marvin.lib.base import (Snapshot,
Template,
VirtualMachine,
Account,
ServiceOffering,
DiskOffering,
Volume)
from marvin.lib.common import (get_domain,
get_zone,
get_template,
list_events,
list_volumes,
list_snapshots,
list_templates,
list_virtual_machines,
)
from marvin.lib.utils import (cleanup_resources,
format_volume_to_ext3,
random_gen,
is_snapshot_on_nfs,
get_hypervisor_type)
from marvin.cloudstackAPI import detachVolume
import time
class Services:
"""Test Snapshots Services
"""
def __init__(self):
self.services = {
"account": {
"email": "test@test.com",
"firstname": "Test",
"lastname": "User",
"username": "test",
# Random characters are appended for unique
# username
"password": "password",
},
"service_offering": {
"name": "Tiny Instance",
"displaytext": "Tiny Instance",
"cpunumber": 1,
"cpuspeed": 200, # in MHz
"memory": 256, # In MBs
},
"disk_offering": {
"displaytext": "Small Disk",
"name": "Small Disk",
"disksize": 1
},
"server_with_disk":
{
"displayname": "Test VM -With Disk",
"username": "root",
"password": "password",
"ssh_port": 22,
"hypervisor": 'XenServer',
"privateport": 22,
"publicport": 22,
"protocol": 'TCP',
},
"server_without_disk":
{
"displayname": "Test VM-No Disk",
"username": "root",
"password": "password",
"ssh_port": 22,
"hypervisor": 'XenServer',
"privateport": 22,
# For NAT rule creation
"publicport": 22,
"protocol": 'TCP',
},
"server": {
"displayname": "TestVM",
"username": "root",
"password": "password",
"ssh_port": 22,
"hypervisor": 'XenServer',
"privateport": 22,
"publicport": 22,
"protocol": 'TCP',
},
"recurring_snapshot": {
"intervaltype": 'HOURLY',
# Frequency of snapshots
"maxsnaps": 1, # Should be min 2
"schedule": 1,
"timezone": 'US/Arizona',
# Timezone Formats -
# http://cloud.mindtouch.us/CloudStack_Documentation/Developer's_Guide%3A_CloudStack
},
"templates": {
"displaytext": 'Template',
"name": 'Template',
"ostype": "CentOS 5.3 (64-bit)",
"templatefilter": 'self',
},
"volume": {
"diskname": "APP Data Volume",
"size": 1, # in GBs
"xenserver": {"rootdiskdevice": "/dev/xvda",
"datadiskdevice_1": '/dev/xvdb',
"datadiskdevice_2": '/dev/xvdc', # Data Disk
},
"kvm": {"rootdiskdevice": "/dev/vda",
"datadiskdevice_1": "/dev/vdb",
"datadiskdevice_2": "/dev/vdc"
},
"vmware": {"rootdiskdevice": "/dev/hda",
"datadiskdevice_1": "/dev/hdb",
"datadiskdevice_2": "/dev/hdc"
}
},
"paths": {
"mount_dir": "/mnt/tmp",
"sub_dir": "test",
"sub_lvl_dir1": "test1",
"sub_lvl_dir2": "test2",
"random_data": "random.data",
},
"ostype": "CentOS 5.3 (64-bit)",
# Cent OS 5.3 (64 bit)
"sleep": 60,
"timeout": 10,
}
class TestSnapshots(cloudstackTestCase):
@classmethod
def setUpClass(cls):
cls.testClient = super(TestSnapshots, cls).getClsTestClient()
cls.api_client = cls.testClient.getApiClient()
cls.services = Services().services
# Get Zone, Domain and templates
cls.domain = get_domain(cls.api_client)
cls.zone = get_zone(cls.api_client, cls.testClient.getZoneForTests())
cls.services['mode'] = cls.zone.networktype
cls._cleanup = []
cls.unsupportedHypervisor = False
cls.hypervisor = str(get_hypervisor_type(cls.api_client)).lower()
if cls.hypervisor.lower() in ['hyperv', 'lxc']:
cls.unsupportedHypervisor = True
return
cls.disk_offering = DiskOffering.create(
cls.api_client,
cls.services["disk_offering"]
)
cls.template = get_template(
cls.api_client,
cls.zone.id,
cls.services["ostype"]
)
cls.services["domainid"] = cls.domain.id
cls.services["volume"]["zoneid"] = cls.services[
"server_with_disk"]["zoneid"] = cls.zone.id
cls.services["server_with_disk"]["diskoffering"] = cls.disk_offering.id
cls.services["server_without_disk"]["zoneid"] = cls.zone.id
cls.services["templates"]["ostypeid"] = cls.template.ostypeid
cls.services["zoneid"] = cls.zone.id
cls.services["diskoffering"] = cls.disk_offering.id
cls.service_offering = ServiceOffering.create(
cls.api_client,
cls.services["service_offering"]
)
cls._cleanup = [
cls.service_offering,
cls.disk_offering
]
return
@classmethod
def tearDownClass(cls):
try:
# Cleanup resources used
cleanup_resources(cls.api_client, cls._cleanup)
except Exception as e:
raise Exception("Warning: Exception during cleanup : %s" % e)
return
def setUp(self):
self.apiclient = self.testClient.getApiClient()
self.dbclient = self.testClient.getDbConnection()
self.cleanup = []
if self.unsupportedHypervisor:
self.skipTest("Skipping test because unsupported hypervisor: %s" %
self.hypervisor)
# Create VMs, NAT Rules etc
self.account = Account.create(
self.apiclient,
self.services["account"],
domainid=self.domain.id
)
self.cleanup.append(self.account)
self.virtual_machine = self.virtual_machine_with_disk = \
VirtualMachine.create(
self.api_client,
self.services["server_with_disk"],
templateid=self.template.id,
accountid=self.account.name,
domainid=self.account.domainid,
serviceofferingid=self.service_offering.id,
mode=self.services["mode"]
)
return
def tearDown(self):
try:
# Clean up, terminate the created instance, volumes and snapshots
cleanup_resources(self.apiclient, self.cleanup)
except Exception as e:
raise Exception("Warning: Exception during cleanup : %s" % e)
return
@attr(speed="slow")
@attr(tags=["advanced", "advancedns", "smoke"], required_hardware="true")
def test_02_snapshot_data_disk(self):
"""Test Snapshot Data Disk
"""
if self.hypervisor.lower() in ['hyperv']:
self.skipTest("Snapshots feature is not supported on Hyper-V")
volume = list_volumes(
self.apiclient,
virtualmachineid=self.virtual_machine_with_disk.id,
type='DATADISK',
listall=True
)
self.assertEqual(
isinstance(volume, list),
True,
"Check list response returns a valid list"
)
self.debug("Creating a Snapshot from data volume: %s" % volume[0].id)
snapshot = Snapshot.create(
self.apiclient,
volume[0].id,
account=self.account.name,
domainid=self.account.domainid
)
snapshots = list_snapshots(
self.apiclient,
id=snapshot.id
)
self.assertEqual(
isinstance(snapshots, list),
True,
"Check list response returns a valid list"
)
self.assertNotEqual(
snapshots,
None,
"Check if result exists in list item call"
)
self.assertEqual(
snapshots[0].id,
snapshot.id,
"Check resource id in list resources call"
)
self.assertTrue(
is_snapshot_on_nfs(
self.apiclient,
self.dbclient,
self.config,
self.zone.id,
snapshot.id))
return
@attr(speed="slow")
@attr(
tags=[
"advanced",
"advancedns",
"basic",
"sg"],
required_hardware="true")
def test_01_volume_from_snapshot(self):
"""Test Creating snapshot from volume having spaces in name(KVM)
"""
# Validate the following
# 1. Create a virtual machine and data volume
# 2. Attach data volume to VM
# 3. Login to machine; create temp/test directories on data volume
# and write some random data
# 4. Snapshot the Volume
# 5. Create another Volume from snapshot
# 6. Mount/Attach volume to another virtual machine
# 7. Compare data, data should match
if self.hypervisor.lower() in ['hyperv']:
self.skipTest("Snapshots feature is not supported on Hyper-V")
random_data_0 = random_gen(size=100)
random_data_1 = random_gen(size=100)
self.debug("random_data_0 : %s" % random_data_0)
self.debug("random_data_1: %s" % random_data_1)
try:
ssh_client = self.virtual_machine.get_ssh_client()
except Exception as e:
self.fail("SSH failed for VM: %s" %
self.virtual_machine.ipaddress)
volume = Volume.create(
self.apiclient,
self.services["volume"],
zoneid=self.zone.id,
account=self.account.name,
domainid=self.account.domainid,
diskofferingid=self.disk_offering.id
)
self.debug("Created volume with ID: %s" % volume.id)
self.virtual_machine.attach_volume(
self.apiclient,
volume
)
self.debug("Attach volume: %s to VM: %s" %
(volume.id, self.virtual_machine.id))
self.debug("Formatting volume: %s to ext3" % volume.id)
# Format partition using ext3
# Note that this is the second data disk partition of virtual machine
# as it was already containing data disk before attaching the new
# volume, Hence datadiskdevice_2
format_volume_to_ext3(
ssh_client,
self.services["volume"][self.hypervisor]["datadiskdevice_2"]
)
cmds = [
"fdisk -l",
"mkdir -p %s" %
self.services["paths"]["mount_dir"],
"mount -t ext3 %s1 %s" %
(self.services["volume"][
self.hypervisor]["datadiskdevice_2"],
self.services["paths"]["mount_dir"]),
"mkdir -p %s/%s/{%s,%s} " %
(self.services["paths"]["mount_dir"],
self.services["paths"]["sub_dir"],
self.services["paths"]["sub_lvl_dir1"],
self.services["paths"]["sub_lvl_dir2"]),
"echo %s > %s/%s/%s/%s" %
(random_data_0,
self.services["paths"]["mount_dir"],
self.services["paths"]["sub_dir"],
self.services["paths"]["sub_lvl_dir1"],
self.services["paths"]["random_data"]),
"echo %s > %s/%s/%s/%s" %
(random_data_1,
self.services["paths"]["mount_dir"],
self.services["paths"]["sub_dir"],
self.services["paths"]["sub_lvl_dir2"],
self.services["paths"]["random_data"]),
"cat %s/%s/%s/%s" %
(self.services["paths"]["mount_dir"],
self.services["paths"]["sub_dir"],
self.services["paths"]["sub_lvl_dir1"],
self.services["paths"]["random_data"])]
for c in cmds:
self.debug("Command: %s" % c)
result = ssh_client.execute(c)
self.debug(result)
# Unmount the Sec Storage
cmds = [
"umount %s" % (self.services["paths"]["mount_dir"]),
]
for c in cmds:
self.debug("Command: %s" % c)
ssh_client.execute(c)
list_volume_response = Volume.list(
self.apiclient,
virtualmachineid=self.virtual_machine.id,
type='DATADISK',
id=volume.id
)
self.assertEqual(
isinstance(list_volume_response, list),
True,
"Check list volume response for valid data"
)
volume_response = list_volume_response[0]
# Create snapshot from attached volume
snapshot = Snapshot.create(
self.apiclient,
volume_response.id,
account=self.account.name,
domainid=self.account.domainid
)
self.debug("Created snapshot: %s" % snapshot.id)
# Create volume from snapshot
volume_from_snapshot = Volume.create_from_snapshot(
self.apiclient,
snapshot.id,
self.services["volume"],
account=self.account.name,
domainid=self.account.domainid
)
# Detach the volume from virtual machine
self.virtual_machine.detach_volume(
self.apiclient,
volume
)
self.debug("Detached volume: %s from VM: %s" %
(volume.id, self.virtual_machine.id))
self.debug("Created Volume: %s from Snapshot: %s" % (
volume_from_snapshot.id,
snapshot.id))
volumes = Volume.list(
self.apiclient,
id=volume_from_snapshot.id
)
self.assertEqual(
isinstance(volumes, list),
True,
"Check list response returns a valid list"
)
self.assertNotEqual(
len(volumes),
None,
"Check Volume list Length"
)
self.assertEqual(
volumes[0].id,
volume_from_snapshot.id,
"Check Volume in the List Volumes"
)
# Attaching volume to new VM
new_virtual_machine = VirtualMachine.create(
self.apiclient,
self.services["server_without_disk"],
templateid=self.template.id,
accountid=self.account.name,
domainid=self.account.domainid,
serviceofferingid=self.service_offering.id,
mode=self.services["mode"]
)
self.debug("Deployed new VM for account: %s" % self.account.name)
# self.cleanup.append(new_virtual_machine)
self.debug("Attaching volume: %s to VM: %s" % (
volume_from_snapshot.id,
new_virtual_machine.id
))
new_virtual_machine.attach_volume(
self.apiclient,
volume_from_snapshot
)
# Rebooting is required so that newly attached disks are detected
self.debug("Rebooting : %s" % new_virtual_machine.id)
new_virtual_machine.reboot(self.apiclient)
try:
# Login to VM to verify test directories and files
ssh = new_virtual_machine.get_ssh_client()
# Mount datadiskdevice_1 because this is the first data disk of the
# new virtual machine
cmds = [
"fdisk -l",
"mkdir -p %s" %
self.services["paths"]["mount_dir"],
"mount -t ext3 %s1 %s" %
(self.services["volume"][
self.hypervisor]["datadiskdevice_1"],
self.services["paths"]["mount_dir"]),
]
for c in cmds:
self.debug("Command: %s" % c)
result = ssh.execute(c)
self.debug(result)
returned_data_0 = ssh.execute(
"cat %s/%s/%s/%s" % (
self.services["paths"]["mount_dir"],
self.services["paths"]["sub_dir"],
self.services["paths"]["sub_lvl_dir1"],
self.services["paths"]["random_data"]
))
returned_data_1 = ssh.execute(
"cat %s/%s/%s/%s" % (
self.services["paths"]["mount_dir"],
self.services["paths"]["sub_dir"],
self.services["paths"]["sub_lvl_dir2"],
self.services["paths"]["random_data"]
))
except Exception as e:
self.fail("SSH access failed for VM: %s, Exception: %s" %
(new_virtual_machine.ipaddress, e))
self.debug("returned_data_0: %s" % returned_data_0[0])
self.debug("returned_data_1: %s" % returned_data_1[0])
# Verify returned data
self.assertEqual(
random_data_0,
returned_data_0[0],
"Verify newly attached volume contents with existing one"
)
self.assertEqual(
random_data_1,
returned_data_1[0],
"Verify newly attached volume contents with existing one"
)
# Unmount the Sec Storage
cmds = [
"umount %s" % (self.services["paths"]["mount_dir"]),
]
for c in cmds:
self.debug("Command: %s" % c)
ssh_client.execute(c)
return
@attr(speed="slow")
@attr(tags=["advanced", "advancedns", "smoke"], required_hardware="true")
def test_04_delete_snapshot(self):
"""Test Delete Snapshot
"""
# 1. Snapshot the Volume
# 2. Delete the snapshot
# 3. Verify snapshot is removed by calling List Snapshots API
# 4. Verify snapshot was removed from image store
if self.hypervisor.lower() in ['hyperv']:
self.skipTest("Snapshots feature is not supported on Hyper-V")
self.debug("Creating volume under account: %s" % self.account.name)
volume = Volume.create(
self.apiclient,
self.services["volume"],
zoneid=self.zone.id,
account=self.account.name,
domainid=self.account.domainid,
diskofferingid=self.disk_offering.id
)
self.debug("Created volume: %s" % volume.id)
self.debug("Attaching volume to vm: %s" % self.virtual_machine.id)
self.virtual_machine.attach_volume(
self.apiclient,
volume
)
self.debug("Volume attached to vm")
volumes = list_volumes(
self.apiclient,
virtualmachineid=self.virtual_machine.id,
type='DATADISK',
id=volume.id
)
self.assertEqual(
isinstance(volumes, list),
True,
"Check list response returns a valid list"
)
snapshot = Snapshot.create(
self.apiclient,
volumes[0].id,
account=self.account.name,
domainid=self.account.domainid
)
snapshot.delete(self.apiclient)
snapshots = list_snapshots(
self.apiclient,
id=snapshot.id
)
self.assertEqual(
snapshots,
None,
"Check if result exists in list item call"
)
self.assertFalse(
is_snapshot_on_nfs(
self.apiclient,
self.dbclient,
self.config,
self.zone.id,
snapshot.id))
return
@attr(speed="slow")
@attr(
tags=[
"advanced",
"advancedns",
"basic",
"sg"],
required_hardware="true")
def test_03_snapshot_detachedDisk(self):
"""Test snapshot from detached disk
"""
# Validate the following
# 1. login in VM and write some data on data disk(use fdisk to
# partition datadisk,fdisk, and make filesystem using
# mkfs.ext3)
# 2. Detach the data disk and write some data on data disk
# 3. perform the snapshot on the detached volume
# 4. listvolumes with VM id shouldn't show the detached volume
# 5. listSnapshots should list the snapshot that was created
# 6. verify backup_snap_id was non null in the `snapshots` table
if self.hypervisor.lower() in ['hyperv']:
self.skipTest("Snapshots feature is not supported on Hyper-V")
volumes = list_volumes(
self.apiclient,
virtualmachineid=self.virtual_machine.id,
type='DATADISK',
listall=True
)
self.assertEqual(
isinstance(volumes, list),
True,
"Check list response returns a valid list"
)
volume = volumes[0]
random_data_0 = random_gen(size=100)
random_data_1 = random_gen(size=100)
try:
ssh_client = self.virtual_machine.get_ssh_client()
# Format partition using ext3
format_volume_to_ext3(
ssh_client,
self.services["volume"][self.hypervisor]["datadiskdevice_1"]
)
cmds = [
"mkdir -p %s" %
self.services["paths"]["mount_dir"],
"mount %s1 %s" %
(self.services["volume"][
self.hypervisor]["datadiskdevice_1"],
self.services["paths"]["mount_dir"]),
"pushd %s" %
self.services["paths"]["mount_dir"],
"mkdir -p %s/{%s,%s} " %
(self.services["paths"]["sub_dir"],
self.services["paths"]["sub_lvl_dir1"],
self.services["paths"]["sub_lvl_dir2"]),
"echo %s > %s/%s/%s" %
(random_data_0,
self.services["paths"]["sub_dir"],
self.services["paths"]["sub_lvl_dir1"],
self.services["paths"]["random_data"]),
"echo %s > %s/%s/%s" %
(random_data_1,
self.services["paths"]["sub_dir"],
self.services["paths"]["sub_lvl_dir2"],
self.services["paths"]["random_data"]),
"sync",
"umount %s" %
(self.services["paths"]["mount_dir"]),
]
for c in cmds:
self.debug(ssh_client.execute(c))
# detach volume from VM
cmd = detachVolume.detachVolumeCmd()
cmd.id = volume.id
self.apiclient.detachVolume(cmd)
# Create snapshot from detached volume
snapshot = Snapshot.create(self.apiclient, volume.id)
volumes = list_volumes(
self.apiclient,
virtualmachineid=self.virtual_machine.id,
type='DATADISK',
listall=True
)
self.assertEqual(
volumes,
None,
"Check Volume is detached"
)
# Verify the snapshot was created or not
snapshots = list_snapshots(
self.apiclient,
id=snapshot.id
)
self.assertNotEqual(
snapshots,
None,
"Check if result exists in list snapshots call"
)
self.assertEqual(
snapshots[0].id,
snapshot.id,
"Check snapshot id in list resources call"
)
except Exception as e:
self.fail("SSH failed for VM with IP: %s - %s" %
(self.virtual_machine.ssh_ip, e))
qresultset = self.dbclient.execute(
"select id from snapshots where uuid = '%s';"
% snapshot.id
)
self.assertNotEqual(
len(qresultset),
0,
"Check DB Query result set"
)
qresult = qresultset[0]
self.assertNotEqual(
str(qresult[0]),
'NULL',
"Check if backup_snap_id is not null"
)
return
@attr(speed="slow")
@attr(
tags=[
"advanced",
"advancedns",
"smoke",
"xen"],
required_hardware="true")
def test_07_template_from_snapshot(self):
"""Create Template from snapshot
"""
# 1. Login to machine; create temp/test directories on data volume
# 2. Snapshot the Volume
# 3. Create Template from snapshot
# 4. Deploy Virtual machine using this template
# 5. Login to newly created virtual machine
# 6. Compare data in the root disk with the one that was written on the
# volume, it should match
if self.hypervisor.lower() in ['hyperv']:
self.skipTest("Snapshots feature is not supported on Hyper-V")
userapiclient = self.testClient.getUserApiClient(
UserName=self.account.name,
DomainName=self.account.domain)
random_data_0 = random_gen(size=100)
random_data_1 = random_gen(size=100)
try:
# Login to virtual machine
ssh_client = self.virtual_machine.get_ssh_client()
cmds = [
"mkdir -p %s" % self.services["paths"]["mount_dir"],
"mount %s1 %s" % (
self.services["volume"][self.hypervisor]["rootdiskdevice"],
self.services["paths"]["mount_dir"]
),
"mkdir -p %s/%s/{%s,%s} " % (
self.services["paths"]["mount_dir"],
self.services["paths"]["sub_dir"],
self.services["paths"]["sub_lvl_dir1"],
self.services["paths"]["sub_lvl_dir2"]
),
"echo %s > %s/%s/%s/%s" % (
random_data_0,
self.services["paths"]["mount_dir"],
self.services["paths"]["sub_dir"],
self.services["paths"]["sub_lvl_dir1"],
self.services["paths"]["random_data"]
),
"echo %s > %s/%s/%s/%s" % (
random_data_1,
self.services["paths"]["mount_dir"],
self.services["paths"]["sub_dir"],
self.services["paths"]["sub_lvl_dir2"],
self.services["paths"]["random_data"]
),
"sync",
]
for c in cmds:
self.debug(c)
result = ssh_client.execute(c)
self.debug(result)
except Exception as e:
self.fail("SSH failed for VM with IP address: %s" %
self.virtual_machine.ipaddress)
# Unmount the Volume
cmds = [
"umount %s" % (self.services["paths"]["mount_dir"]),
]
for c in cmds:
self.debug(c)
ssh_client.execute(c)
volumes = list_volumes(
userapiclient,
virtualmachineid=self.virtual_machine.id,
type='ROOT',
listall=True
)
self.assertEqual(
isinstance(volumes, list),
True,
"Check list response returns a valid list"
)
volume = volumes[0]
# Create a snapshot of volume
snapshot = Snapshot.create(
userapiclient,
volume.id,
account=self.account.name,
domainid=self.account.domainid
)
self.debug("Snapshot created from volume ID: %s" % volume.id)
# Generate template from the snapshot
template = Template.create_from_snapshot(
userapiclient,
snapshot,
self.services["templates"]
)
self.cleanup.append(template)
self.debug("Template created from snapshot ID: %s" % snapshot.id)
# Verify created template
templates = list_templates(
userapiclient,
templatefilter=self.services["templates"]["templatefilter"],
id=template.id
)
self.assertNotEqual(
templates,
None,
"Check if result exists in list item call"
)
self.assertEqual(
templates[0].id,
template.id,
"Check new template id in list resources call"
)
self.debug("Deploying new VM from template: %s" % template.id)
# Deploy new virtual machine using template
new_virtual_machine = VirtualMachine.create(
userapiclient,
self.services["server_without_disk"],
templateid=template.id,
accountid=self.account.name,
domainid=self.account.domainid,
serviceofferingid=self.service_offering.id,
mode=self.services["mode"]
)
try:
# Login to VM & mount directory
ssh = new_virtual_machine.get_ssh_client()
cmds = [
"mkdir -p %s" % self.services["paths"]["mount_dir"],
"mount %s1 %s" % (
self.services["volume"][self.hypervisor]["rootdiskdevice"],
self.services["paths"]["mount_dir"]
)
]
for c in cmds:
ssh.execute(c)
returned_data_0 = ssh.execute("cat %s/%s/%s/%s" % (
self.services["paths"]["mount_dir"],
self.services["paths"]["sub_dir"],
self.services["paths"]["sub_lvl_dir1"],
self.services["paths"]["random_data"]
))
self.debug(returned_data_0)
returned_data_1 = ssh.execute("cat %s/%s/%s/%s" % (
self.services["paths"]["mount_dir"],
self.services["paths"]["sub_dir"],
self.services["paths"]["sub_lvl_dir2"],
self.services["paths"]["random_data"]
))
self.debug(returned_data_1)
except Exception as e:
self.fail("SSH failed for VM with IP address: %s" %
new_virtual_machine.ipaddress)
# Verify returned data
self.assertEqual(
random_data_0,
returned_data_0[0],
"Verify newly attached volume contents with existing one"
)
self.assertEqual(
random_data_1,
returned_data_1[0],
"Verify newly attached volume contents with existing one"
)
# Unmount the volume
cmds = [
"umount %s" % (self.services["paths"]["mount_dir"]),
]
try:
for c in cmds:
self.debug(c)
ssh_client.execute(c)
except Exception as e:
self.fail("SSH failed for VM with IP address: %s, Exception: %s" %
(new_virtual_machine.ipaddress, e))
return
class TestCreateVMSnapshotTemplate(cloudstackTestCase):
@classmethod
def setUpClass(cls):
cls.testClient = super(
TestCreateVMSnapshotTemplate,
cls).getClsTestClient()
cls.api_client = cls.testClient.getApiClient()
cls._cleanup = []
cls.services = Services().services
# Get Zone, Domain and templates
cls.domain = get_domain(cls.api_client)
cls.zone = get_zone(cls.api_client, cls.testClient.getZoneForTests())
cls.services['mode'] = cls.zone.networktype
cls.unsupportedHypervisor = False
cls.hypervisor = get_hypervisor_type(cls.api_client)
if cls.hypervisor.lower() in ['hyperv', 'lxc']:
cls.unsupportedHypervisor = True
return
cls.template = get_template(
cls.api_client,
cls.zone.id,
cls.services["ostype"]
)
cls.services["domainid"] = cls.domain.id
cls.services["server"]["zoneid"] = cls.zone.id
# Create VMs, NAT Rules etc
cls.account = Account.create(
cls.api_client,
cls.services["account"],
domainid=cls.domain.id
)