Catch exceptions from rmtree

pull/100/head
Julien Duponchelle 9 years ago
parent 4518404706
commit df8bdcc152

@ -195,7 +195,10 @@ class Project:
for vm in self._vms:
vm.close()
if cleanup and os.path.exists(self.path):
yield from wait_run_in_executor(shutil.rmtree, self.path)
try:
yield from wait_run_in_executor(shutil.rmtree, self.path)
except OSError as e:
raise aiohttp.web.HTTPInternalServerError(text="Could not delete the project directory: {}".format(e))
@asyncio.coroutine
def commit(self):
@ -205,7 +208,10 @@ class Project:
vm = self._vms_to_destroy.pop()
directory = self.vm_working_directory(vm)
if os.path.exists(directory):
yield from wait_run_in_executor(shutil.rmtree, directory)
try:
yield from wait_run_in_executor(shutil.rmtree, directory)
except OSError as e:
raise aiohttp.web.HTTPInternalServerError(text="Could not delete the project directory: {}".format(e))
self.remove_vm(vm)
@asyncio.coroutine

@ -117,4 +117,8 @@ def run_around_tests():
yield
shutil.rmtree(tmppath)
# An helper should not raise Exception
try:
shutil.rmtree(tmppath)
except:
pass

@ -20,6 +20,7 @@ import os
import asyncio
import pytest
import aiohttp
import shutil
from unittest.mock import patch
from gns3server.modules.project import Project
@ -99,6 +100,20 @@ def test_commit(manager, loop):
assert len(project.vms) == 0
def test_commit_permission_issue(manager, loop):
project = Project()
vm = VPCSVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager)
project.add_vm(vm)
directory = project.vm_working_directory(vm)
project.mark_vm_for_destruction(vm)
assert len(project._vms_to_destroy) == 1
assert os.path.exists(directory)
os.chmod(directory, 0)
with pytest.raises(aiohttp.web.HTTPInternalServerError):
loop.run_until_complete(asyncio.async(project.commit()))
os.chmod(directory, 700)
def test_project_delete(loop):
project = Project()
directory = project.path
@ -107,6 +122,16 @@ def test_project_delete(loop):
assert os.path.exists(directory) is False
def test_project_delete_permission_issue(loop):
project = Project()
directory = project.path
assert os.path.exists(directory)
os.chmod(directory, 0)
with pytest.raises(aiohttp.web.HTTPInternalServerError):
loop.run_until_complete(asyncio.async(project.delete()))
os.chmod(directory, 700)
def test_project_add_vm(manager):
project = Project()
vm = VPCSVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager)

Loading…
Cancel
Save