55# to you under the Apache License, Version 2.0 (the
66# "License"); you may not use this file except in compliance
77# with the License. You may obtain a copy of the License at
8- #
8+ #
99# http://www.apache.org/licenses/LICENSE-2.0
10- #
10+ #
1111# Unless required by applicable law or agreed to in writing,
1212# software distributed under the License is distributed on an
1313# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1717""" P1 tests for Snapshots
1818"""
1919#Import Local Modules
20- from nose .plugins .attrib import attr
21- from marvin .cloudstackTestCase import *
22- from marvin .cloudstackAPI import *
23- from marvin .integration .lib .utils import *
24- from marvin .integration .lib .base import *
25- from marvin .integration .lib .common import *
26- from marvin .integration .lib .utils import is_snapshot_on_nfs
20+ from nose .plugins .attrib import attr
21+ from marvin .cloudstackTestCase import cloudstackTestCase , unittest
22+
23+ from marvin .integration .lib .base import (Snapshot ,
24+ Template ,
25+ VirtualMachine ,
26+ Account ,
27+ ServiceOffering ,
28+ DiskOffering ,
29+ Volume )
30+
31+ from marvin .integration .lib .common import (get_domain ,
32+ get_zone ,
33+ get_template ,
34+ list_events ,
35+ list_volumes ,
36+ list_snapshots ,
37+ list_templates ,
38+ list_virtual_machines ,
39+ get_hypervisor_type )
40+
41+ from marvin .integration .lib .utils import (cleanup_resources ,
42+ format_volume_to_ext3 ,
43+ random_gen ,
44+ is_snapshot_on_nfs )
45+
46+ from marvin .cloudstackAPI import detachVolume
47+ import time
48+
2749
2850
2951class Services :
@@ -104,7 +126,18 @@ def __init__(self):
104126 "volume" : {
105127 "diskname" : "APP Data Volume" ,
106128 "size" : 1 , # in GBs
107- "diskdevice" : ['/dev/xvdb' , '/dev/sdb' , '/dev/hdb' , '/dev/vdb' ], # Data Disk
129+ "xenserver" : {"rootdiskdevice" :"/dev/xvda" ,
130+ "datadiskdevice_1" : '/dev/xvdb' ,
131+ "datadiskdevice_2" : '/dev/xvdc' , # Data Disk
132+ },
133+ "kvm" : {"rootdiskdevice" : "/dev/vda" ,
134+ "datadiskdevice_1" : "/dev/vdb" ,
135+ "datadiskdevice_2" : "/dev/vdc"
136+ },
137+ "vmware" : {"rootdiskdevice" : "/dev/hda" ,
138+ "datadiskdevice_1" : "/dev/hdb" ,
139+ "datadiskdevice_2" : "/dev/hdc"
140+ }
108141 },
109142 "paths" : {
110143 "mount_dir" : "/mnt/tmp" ,
@@ -139,7 +172,7 @@ def setUpClass(cls):
139172 cls .zone .id ,
140173 cls .services ["ostype" ]
141174 )
142-
175+
143176 cls .services ["domainid" ] = cls .domain .id
144177 cls .services ["volume" ]["zoneid" ] = cls .services ["server_with_disk" ]["zoneid" ] = cls .zone .id
145178 cls .services ["server_with_disk" ]["diskoffering" ] = cls .disk_offering .id
@@ -174,6 +207,9 @@ def setUpClass(cls):
174207 mode = cls .services ["mode" ]
175208 )
176209
210+ # Get Hypervisor Type
211+ cls .hypervisor = (get_hypervisor_type (cls .api_client )).lower ()
212+
177213 cls ._cleanup = [
178214 cls .service_offering ,
179215 cls .disk_offering ,
@@ -259,15 +295,24 @@ def test_01_volume_from_snapshot(self):
259295 # Validate the following
260296 #1. Create a virtual machine and data volume
261297 #2. Attach data volume to VM
262- #3. Login to machine; create temp/test directories on data volume
298+ #3. Login to machine; create temp/test directories on data volume and write some random data
263299 #4. Snapshot the Volume
264300 #5. Create another Volume from snapshot
265- #6. Mount/Attach volume to another server
266- #7. Compare data
301+ #6. Mount/Attach volume to another virtual machine
302+ #7. Compare data, data should match
267303
268304 random_data_0 = random_gen (size = 100 )
269305 random_data_1 = random_gen (size = 100 )
270306
307+ self .debug ("random_data_0 : %s" % random_data_0 )
308+ self .debug ("random_data_1: %s" % random_data_1 )
309+
310+ try :
311+ ssh_client = self .virtual_machine .get_ssh_client ()
312+ except Exception as e :
313+ self .fail ("SSH failed for VM: %s" %
314+ self .virtual_machine .ipaddress )
315+
271316 volume = Volume .create (
272317 self .apiclient ,
273318 self .services ["volume" ],
@@ -285,22 +330,18 @@ def test_01_volume_from_snapshot(self):
285330 (volume .id , self .virtual_machine .id ))
286331
287332
288- try :
289- ssh_client = self .virtual_machine .get_ssh_client ()
290- except Exception as e :
291- self .fail ("SSH failed for VM: %s" %
292- self .virtual_machine .ipaddress )
293-
294333 self .debug ("Formatting volume: %s to ext3" % volume .id )
295334 #Format partition using ext3
335+ # Note that this is the second data disk partition of virtual machine as it was already containing
336+ # data disk before attaching the new volume, Hence datadiskdevice_2
296337 format_volume_to_ext3 (
297338 ssh_client ,
298- self .services ["volume" ]["diskdevice " ]
339+ self .services ["volume" ][self . hypervisor ][ "datadiskdevice_2 " ]
299340 )
300- cmds = [
341+ cmds = [ "fdisk -l" ,
301342 "mkdir -p %s" % self .services ["paths" ]["mount_dir" ],
302- "mount %s1 %s" % (
303- self .services ["volume" ]["diskdevice " ],
343+ "mount -t ext3 %s1 %s" % (
344+ self .services ["volume" ][self . hypervisor ][ "datadiskdevice_2 " ],
304345 self .services ["paths" ]["mount_dir" ]
305346 ),
306347 "mkdir -p %s/%s/{%s,%s} " % (
@@ -323,10 +364,17 @@ def test_01_volume_from_snapshot(self):
323364 self .services ["paths" ]["sub_lvl_dir2" ],
324365 self .services ["paths" ]["random_data" ]
325366 ),
367+ "cat %s/%s/%s/%s" % (
368+ self .services ["paths" ]["mount_dir" ],
369+ self .services ["paths" ]["sub_dir" ],
370+ self .services ["paths" ]["sub_lvl_dir1" ],
371+ self .services ["paths" ]["random_data" ]
372+ )
326373 ]
327374 for c in cmds :
328375 self .debug ("Command: %s" % c )
329- ssh_client .execute (c )
376+ result = ssh_client .execute (c )
377+ self .debug (result )
330378
331379 # Unmount the Sec Storage
332380 cmds = [
@@ -340,7 +388,7 @@ def test_01_volume_from_snapshot(self):
340388 self .apiclient ,
341389 virtualmachineid = self .virtual_machine .id ,
342390 type = 'DATADISK' ,
343- listall = True
391+ id = volume . id
344392 )
345393
346394 self .assertEqual (
@@ -365,6 +413,15 @@ def test_01_volume_from_snapshot(self):
365413 account = self .account .name ,
366414 domainid = self .account .domainid
367415 )
416+
417+ # Detach the volume from virtual machine
418+ self .virtual_machine .detach_volume (
419+ self .apiclient ,
420+ volume
421+ )
422+ self .debug ("Detached volume: %s from VM: %s" %
423+ (volume .id , self .virtual_machine .id ))
424+
368425 self .debug ("Created Volume: %s from Snapshot: %s" % (
369426 volume_from_snapshot .id ,
370427 snapshot .id ))
@@ -399,7 +456,7 @@ def test_01_volume_from_snapshot(self):
399456 mode = self .services ["mode" ]
400457 )
401458 self .debug ("Deployed new VM for account: %s" % self .account .name )
402- self .cleanup .append (new_virtual_machine )
459+ # self.cleanup.append(new_virtual_machine)
403460
404461 self .debug ("Attaching volume: %s to VM: %s" % (
405462 volume_from_snapshot .id ,
@@ -411,21 +468,28 @@ def test_01_volume_from_snapshot(self):
411468 volume_from_snapshot
412469 )
413470
471+ # Rebooting is required so that newly attached disks are detected
472+ self .debug ("Rebooting : %s" % new_virtual_machine .id )
473+
474+ new_virtual_machine .reboot (self .apiclient )
475+
414476 try :
415477 #Login to VM to verify test directories and files
416478 ssh = new_virtual_machine .get_ssh_client ()
417479
418- cmds = [
480+ # Mount datadiskdevice_1 because this is the first data disk of the new virtual machine
481+ cmds = ["fdisk -l" ,
419482 "mkdir -p %s" % self .services ["paths" ]["mount_dir" ],
420- "mount %s1 %s" % (
421- self .services ["volume" ]["diskdevice " ],
483+ "mount -t ext3 %s1 %s" % (
484+ self .services ["volume" ][self . hypervisor ][ "datadiskdevice_1 " ],
422485 self .services ["paths" ]["mount_dir" ]
423486 ),
424487 ]
425488
426489 for c in cmds :
427490 self .debug ("Command: %s" % c )
428- ssh .execute (c )
491+ result = ssh .execute (c )
492+ self .debug (result )
429493
430494 returned_data_0 = ssh .execute (
431495 "cat %s/%s/%s/%s" % (
@@ -442,8 +506,12 @@ def test_01_volume_from_snapshot(self):
442506 self .services ["paths" ]["random_data" ]
443507 ))
444508 except Exception as e :
445- self .fail ("SSH access failed for VM: %s" %
446- new_virtual_machine .ipaddress )
509+ self .fail ("SSH access failed for VM: %s, Exception: %s" %
510+ (new_virtual_machine .ipaddress , e ))
511+
512+ self .debug ("returned_data_0: %s" % returned_data_0 [0 ])
513+ self .debug ("returned_data_1: %s" % returned_data_1 [0 ])
514+
447515 #Verify returned data
448516 self .assertEqual (
449517 random_data_0 ,
@@ -512,7 +580,7 @@ def test_03_snapshot_detachedDisk(self):
512580 """
513581 # Validate the following
514582 # 1. login in VM and write some data on data disk(use fdisk to
515- # partition datadisk,fdisk /dev/sdb , and make filesystem using
583+ # partition datadisk,fdisk, and make filesystem using
516584 # mkfs.ext3)
517585 # 2. Detach the data disk and write some data on data disk
518586 # 3. perform the snapshot on the detached volume
@@ -540,12 +608,12 @@ def test_03_snapshot_detachedDisk(self):
540608 #Format partition using ext3
541609 format_volume_to_ext3 (
542610 ssh_client ,
543- self .services ["volume" ]["diskdevice " ]
611+ self .services ["volume" ][self . hypervisor ][ "datadiskdevice_1 " ]
544612 )
545613 cmds = [
546614 "mkdir -p %s" % self .services ["paths" ]["mount_dir" ],
547615 "mount %s1 %s" % (
548- self .services ["volume" ]["diskdevice " ],
616+ self .services ["volume" ][self . hypervisor ][ "datadiskdevice_1 " ],
549617 self .services ["paths" ]["mount_dir" ]
550618 ),
551619 "pushd %s" % self .services ["paths" ]["mount_dir" ],
@@ -567,6 +635,7 @@ def test_03_snapshot_detachedDisk(self):
567635 self .services ["paths" ]["random_data" ]
568636 ),
569637 "sync" ,
638+ "umount %s" % (self .services ["paths" ]["mount_dir" ]),
570639 ]
571640 for c in cmds :
572641 self .debug (ssh_client .execute (c ))
@@ -641,7 +710,7 @@ def test_07_template_from_snapshot(self):
641710 #3. Create Template from snapshot
642711 #4. Deploy Virtual machine using this template
643712 #5. Login to newly created virtual machine
644- #6. Compare data
713+ #6. Compare data in the root disk with the one that was written on the volume, it should match
645714
646715 random_data_0 = random_gen (size = 100 )
647716 random_data_1 = random_gen (size = 100 )
@@ -653,7 +722,7 @@ def test_07_template_from_snapshot(self):
653722 cmds = [
654723 "mkdir -p %s" % self .services ["paths" ]["mount_dir" ],
655724 "mount %s1 %s" % (
656- self .services ["volume" ]["diskdevice " ],
725+ self .services ["volume" ][self . hypervisor ][ "rootdiskdevice " ],
657726 self .services ["paths" ]["mount_dir" ]
658727 ),
659728 "mkdir -p %s/%s/{%s,%s} " % (
@@ -767,7 +836,7 @@ def test_07_template_from_snapshot(self):
767836 cmds = [
768837 "mkdir -p %s" % self .services ["paths" ]["mount_dir" ],
769838 "mount %s1 %s" % (
770- self .services ["volume" ]["diskdevice " ],
839+ self .services ["volume" ][self . hypervisor ][ "rootdiskdevice " ],
771840 self .services ["paths" ]["mount_dir" ]
772841 )
773842 ]
@@ -813,8 +882,8 @@ def test_07_template_from_snapshot(self):
813882 ssh_client .execute (c )
814883
815884 except Exception as e :
816- self .fail ("SSH failed for VM with IP address: %s" %
817- new_virtual_machine .ipaddress )
885+ self .fail ("SSH failed for VM with IP address: %s, Exception: %s " %
886+ ( new_virtual_machine .ipaddress , e ) )
818887 return
819888
820889
@@ -920,7 +989,7 @@ def test_01_createVM_snapshotTemplate(self):
920989 volume = volumes [0 ]
921990
922991 # Create a snapshot from the ROOTDISK
923- snapshot = Snapshot .create (self .apiclient , volumes [ 0 ] .id )
992+ snapshot = Snapshot .create (self .apiclient , volume .id )
924993 self .debug ("Snapshot created: ID - %s" % snapshot .id )
925994 self .cleanup .append (snapshot )
926995
@@ -1016,7 +1085,6 @@ def test_01_createVM_snapshotTemplate(self):
10161085 self .assertTrue (is_snapshot_on_nfs (self .apiclient , self .dbclient , self .config , self .zone .id , snapshot_uuid ))
10171086 return
10181087
1019-
10201088class TestSnapshotEvents (cloudstackTestCase ):
10211089
10221090 @classmethod
@@ -1087,7 +1155,7 @@ def tearDown(self):
10871155 except Exception as e :
10881156 raise Exception ("Warning: Exception during cleanup : %s" % e )
10891157 return
1090-
1158+
10911159 @attr (speed = "slow" )
10921160 @attr (tags = ["advanced" , "advancedns" ])
10931161 def test_05_snapshot_events (self ):
@@ -1113,7 +1181,7 @@ def test_05_snapshot_events(self):
11131181 volume = volumes [0 ]
11141182
11151183 # Create a snapshot from the ROOTDISK
1116- snapshot = Snapshot .create (self .apiclient , volumes [ 0 ] .id )
1184+ snapshot = Snapshot .create (self .apiclient , volume .id )
11171185 self .debug ("Snapshot created with ID: %s" % snapshot .id )
11181186
11191187 snapshots = list_snapshots (
0 commit comments