@@ -144,50 +144,59 @@ def setUpClass(cls):
144144 cls .services ["server" ]["zoneid" ] = cls .zone .id
145145
146146 cls .services ["template" ] = template .id
147+ cls ._cleanup = []
147148
148- # Create VMs, NAT Rules etc
149- cls .account = Account .create (
150- cls .api_client ,
151- cls .services ["account" ],
152- domainid = cls .domain .id
153- )
154-
155- cls .services ["account" ] = cls .account .name
156-
157- cls .service_offering = ServiceOffering .create (
158- cls .api_client ,
159- cls .services ["service_offering" ]
160- )
161- cls .virtual_machine = VirtualMachine .create (
149+ try :
150+ # Create VMs, NAT Rules etc
151+ cls .account = Account .create (
162152 cls .api_client ,
163- cls .services ["server" ],
164- templateid = template .id ,
165- accountid = cls .account .name ,
166- domainid = cls .account .domainid ,
167- serviceofferingid = cls .service_offering .id
153+ cls .services ["account" ],
154+ domainid = cls .domain .id
168155 )
169- # Get the Root disk of VM
170- volumes = list_volumes (
171- cls .api_client ,
172- virtualmachineid = cls .virtual_machine .id ,
173- type = 'ROOT' ,
174- listall = True
175- )
176- volume = volumes [0 ]
177-
178- # Create a snapshot from the ROOTDISK
179- cls .snapshot = Snapshot .create (cls .api_client , volumes [0 ].id )
180156
181- cls ._cleanup = [
182- cls .service_offering ,
183- ]
157+ cls .services ["account" ] = cls .account .name
158+ cls ._cleanup .append (cls .account )
159+
160+ cls .services ["service_offering" ]["storagetype" ] = "local"
161+ cls .service_offering = ServiceOffering .create (
162+ cls .api_client ,
163+ cls .services ["service_offering" ]
164+ )
165+ cls ._cleanup .append (cls .service_offering )
166+ cls .virtual_machine = VirtualMachine .create (
167+ cls .api_client ,
168+ cls .services ["server" ],
169+ templateid = template .id ,
170+ accountid = cls .account .name ,
171+ domainid = cls .account .domainid ,
172+ serviceofferingid = cls .service_offering .id
173+ )
174+ cls ._cleanup .append (cls .virtual_machine )
175+ # Get the Root disk of VM
176+ volumes = list_volumes (
177+ cls .api_client ,
178+ virtualmachineid = cls .virtual_machine .id ,
179+ type = 'ROOT' ,
180+ listall = True
181+ )
182+ volume = volumes [0 ]
183+
184+ # Create a snapshot from the ROOTDISK
185+ cls .snapshot = Snapshot .create (cls .api_client , volumes [0 ].id )
186+ cls ._cleanup .append (cls .snapshot )
187+ except Exception , e :
188+ cls .tearDownClass ()
189+ unittest .SkipTest ("setupClass fails for %s" % cls .__name__ )
190+ raise e
191+ else :
192+ cls ._cleanup .remove (cls .account )
184193 return
185194
186195 @classmethod
187196 def tearDownClass (cls ):
188197 try :
189198 #Cleanup resources used
190- cleanup_resources (cls .api_client , cls ._cleanup )
199+ cleanup_resources (cls .api_client , reversed ( cls ._cleanup ) )
191200 except Exception as e :
192201 raise Exception ("Warning: Exception during cleanup : %s" % e )
193202 return
@@ -206,6 +215,82 @@ def tearDown(self):
206215 raise Exception ("Warning: Exception during cleanup : %s" % e )
207216 return
208217
218+ def is_snapshot_on_nfs (self , snapshot_id ):
219+ """
220+ Checks whether a snapshot with id (not UUID) `snapshot_id` is present on the nfs storage
221+
222+ @param snapshot_id: id of the snapshot (not uuid)
223+ @return: True if snapshot is found, False otherwise
224+ """
225+ secondaryStores = ImageStore .list (self .apiclient , zoneid = self .zone .id )
226+ self .assertTrue (isinstance (secondaryStores , list ), "Not a valid response for listImageStores" )
227+ self .assertNotEqual (len (secondaryStores ), 0 , "No image stores found in zone %s" % self .zone .id )
228+ secondaryStore = secondaryStores [0 ]
229+ if str (secondaryStore .providername ).lower () != "nfs" :
230+ self .skipTest ("TODO: %s test works only against nfs secondary storage" % self ._testMethodName )
231+
232+ qresultset = self .dbclient .execute (
233+ "select install_path from snapshot_store_ref where snapshot_id='%s' and store_role='Image';" % snapshot_id
234+ )
235+ self .assertEqual (
236+ isinstance (qresultset , list ),
237+ True ,
238+ "Invalid db query response for snapshot %s" % self .snapshot .id
239+ )
240+ self .assertNotEqual (
241+ len (qresultset ),
242+ 0 ,
243+ "No such snapshot %s found in the cloudstack db" % self .snapshot .id
244+ )
245+ snapshotPath = qresultset [0 ][0 ]
246+ nfsurl = secondaryStore .url
247+ # parse_url = ['nfs:', '', '192.168.100.21', 'export', 'test']
248+ from urllib2 import urlparse
249+ parse_url = urlparse .urlsplit (nfsurl , scheme = 'nfs' )
250+ host , path = parse_url .netloc , parse_url .path
251+ # Sleep to ensure that snapshot is reflected in sec storage
252+ time .sleep (self .services ["sleep" ])
253+ snapshots = []
254+ try :
255+ # Login to Secondary storage VM to check snapshot present on sec disk
256+ ssh_client = remoteSSHClient (
257+ self .config .mgtSvr [0 ].mgtSvrIp ,
258+ 22 ,
259+ self .config .mgtSvr [0 ].user ,
260+ self .config .mgtSvr [0 ].passwd ,
261+ )
262+
263+ cmds = [
264+ "mkdir -p %s" % self .services ["paths" ]["mount_dir" ],
265+ "mount -t %s %s/%s %s" % (
266+ 'nfs' ,
267+ host ,
268+ path ,
269+ self .services ["paths" ]["mount_dir" ]
270+ ),
271+ "ls %s" % (
272+ snapshotPath
273+ ),
274+ ]
275+
276+ for c in cmds :
277+ self .debug ("command: %s" % c )
278+ result = ssh_client .execute (c )
279+ self .debug ("Result: %s" % result )
280+
281+ snapshots .extend (result )
282+ # Unmount the Sec Storage
283+ cmds = [
284+ "umount %s" % (self .services ["paths" ]["mount_dir" ]),
285+ ]
286+ for c in cmds :
287+ ssh_client .execute (c )
288+ except Exception as e :
289+ self .fail ("SSH failed for management server: %s - %s" %
290+ (self .services ["mgmt_server" ]["ipaddress" ], e ))
291+ res = str (snapshots )
292+ return res .count (snapshot_id ) == 1
293+
209294 @attr (speed = "slow" )
210295 @attr (tags = ["advanced" , "advancedns" , "basic" , "sg" ])
211296 def test_02_accountSnapshotClean (self ):
@@ -276,199 +361,49 @@ def test_02_accountSnapshotClean(self):
276361 self .assertNotEqual (
277362 snapshots ,
278363 None ,
279- "Check if result exists in list snapshots call"
364+ "No such snapshot %s found" % self . snapshot . id
280365 )
281366 self .assertEqual (
282367 snapshots [0 ].id ,
283368 self .snapshot .id ,
284369 "Check snapshot id in list resources call"
285370 )
286371
287- # Fetch values from database
288372 qresultset = self .dbclient .execute (
289- "select backup_snap_id, account_id, volume_id from snapshots where uuid = '%s';" \
373+ "select id from snapshots where uuid = '%s';" \
290374 % self .snapshot .id
291375 )
292376 self .assertEqual (
293377 isinstance (qresultset , list ),
294378 True ,
295- "Check DB response returns a valid list"
379+ "Invalid db query response for snapshot %s" % self . snapshot . id
296380 )
297381 self .assertNotEqual (
298382 len (qresultset ),
299383 0 ,
300- "Check DB Query result set"
384+ "No such snapshot %s found in the cloudstack db" % self . snapshot . id
301385 )
302386
303387 qresult = qresultset [0 ]
304- snapshot_uuid = qresult [0 ] # backup_snap_id = snapshot UUID
305- account_id = qresult [1 ]
306- volume_id = qresult [2 ]
388+ snapshot_uuid = qresult [0 ]
307389
308- # Get the Secondary Storage details from list Hosts
309- hosts = list_hosts (
310- self .apiclient ,
311- type = 'SecondaryStorage' ,
312- zoneid = self .zone .id
313- )
314- self .assertEqual (
315- isinstance (hosts , list ),
316- True ,
317- "Check list response returns a valid list"
318- )
319- uuids = []
320- for host in hosts :
321- # hosts[0].name = "nfs://192.168.100.21/export/test"
322- parse_url = (host .name ).split ('/' )
323- # parse_url = ['nfs:', '', '192.168.100.21', 'export', 'test']
324-
325- # Stripping end ':' from storage type
326- storage_type = parse_url [0 ][:- 1 ]
327- # Split IP address and export path from name
328- sec_storage_ip = parse_url [2 ]
329- # Sec Storage IP: 192.168.100.21
330-
331- if sec_storage_ip [- 1 ] != ":" :
332- sec_storage_ip = sec_storage_ip + ":"
333-
334- export_path = '/' .join (parse_url [3 :])
335- # Export path: export/test
336-
337- # Sleep to ensure that snapshot is reflected in sec storage
338- time .sleep (self .services ["sleep" ])
339- try :
340- # Login to Secondary storage VM to check snapshot present on sec disk
341- ssh_client = remoteSSHClient (
342- self .services ["mgmt_server" ]["ipaddress" ],
343- self .services ["mgmt_server" ]["port" ],
344- self .services ["mgmt_server" ]["username" ],
345- self .services ["mgmt_server" ]["password" ],
346- )
347-
348- cmds = [
349- "mkdir -p %s" % self .services ["paths" ]["mount_dir" ],
350- "mount -t %s %s/%s %s" % (
351- storage_type ,
352- sec_storage_ip ,
353- export_path ,
354- self .services ["paths" ]["mount_dir" ]
355- ),
356- "ls %s/snapshots/%s/%s" % (
357- self .services ["paths" ]["mount_dir" ],
358- account_id ,
359- volume_id
360- ),
361- ]
362-
363- for c in cmds :
364- self .debug ("command: %s" % c )
365- result = ssh_client .execute (c )
366- self .debug ("Result: %s" % result )
367-
368- uuids .append (result )
369-
370- # Unmount the Sec Storage
371- cmds = [
372- "umount %s" % (self .services ["mount_dir" ]),
373- ]
374- for c in cmds :
375- result = ssh_client .execute (c )
376- except Exception as e :
377- self .fail ("SSH failed for management server: %s - %s" %
378- (self .services ["mgmt_server" ]["ipaddress" ], e ))
379-
380- res = str (uuids )
381- self .assertEqual (
382- res .count (snapshot_uuid ),
383- 1 ,
384- "Check snapshot UUID in secondary storage and database"
385- )
390+ self .assertTrue (self .is_snapshot_on_nfs (snapshot_uuid ), "Snapshot was not found no NFS" )
386391
387392 self .debug ("Deleting account: %s" % self .account .name )
388393 # Delete account
389394 self .account .delete (self .apiclient )
390395
391- interval = list_configurations (
392- self .apiclient ,
393- name = 'account.cleanup.interval'
394- )
395- self .assertEqual (
396- isinstance (interval , list ),
397- True ,
398- "Check list response returns a valid list"
399- )
400- self .debug ("account.cleanup.interval: %s" % interval [0 ].value )
401-
402396 # Wait for account cleanup interval
403- time .sleep (int (interval [0 ].value ) * 2 )
404-
397+ wait_for_cleanup (self .apiclient , configs = ["account.cleanup.interval" ])
405398 accounts = list_accounts (
406399 self .apiclient ,
407400 id = self .account .id
408401 )
409-
410402 self .assertEqual (
411403 accounts ,
412404 None ,
413405 "List accounts should return empty list after account deletion"
414406 )
415407
416- uuids = []
417- for host in hosts :
418- # hosts[0].name = "nfs://192.168.100.21/export/test"
419- parse_url = (host .name ).split ('/' )
420- # parse_url = ['nfs:', '', '192.168.100.21', 'export', 'test']
421-
422- # Stripping end ':' from storage type
423- storage_type = parse_url [0 ][:- 1 ]
424- # Split IP address and export path from name
425- sec_storage_ip = parse_url [2 ]
426- # Sec Storage IP: 192.168.100.21
427-
428- if sec_storage_ip [- 1 ] != ":" :
429- sec_storage_ip = sec_storage_ip + ":"
430-
431- export_path = '/' .join (parse_url [3 :])
432- # Export path: export/test
433-
434- try :
435- cmds = [
436- "mount -t %s %s/%s %s" % (
437- storage_type ,
438- sec_storage_ip ,
439- export_path ,
440- self .services ["paths" ]["mount_dir" ]
441- ),
442- "ls %s/snapshots/%s/%s" % (
443- self .services ["paths" ]["mount_dir" ],
444- account_id ,
445- volume_id
446- ),
447- ]
448-
449- for c in cmds :
450- self .debug ("command: %s" % c )
451- result = ssh_client .execute (c )
452- self .debug ("Result: %s" % result )
453-
454- uuids .append (result )
455- # Unmount the Sec Storage
456- cmds = [
457- "umount %s" % (self .services ["paths" ]["mount_dir" ]),
458- ]
459- for c in cmds :
460- self .debug ("command: %s" % c )
461- result = ssh_client .execute (c )
462- self .debug ("Result: %s" % result )
463-
464- except Exception as e :
465- self .fail ("SSH failed for management server: %s - %s" %
466- (self .services ["mgmt_server" ]["ipaddress" ], e ))
467-
468- res = str (uuids )
469- self .assertNotEqual (
470- res .count (snapshot_uuid ),
471- 1 ,
472- "Check snapshot UUID in secondary storage and database"
473- )
408+ self .assertFalse (self .is_snapshot_on_nfs (snapshot_uuid ), "Snapshot was still found no NFS after account gc" )
474409 return
0 commit comments