Check existence without an exception

Related to saltstack/salt#27089 Saltcloud virtualbox provider
This commit is contained in:
LoveIsGrief 2015-12-26 18:32:32 +01:00
parent 5b9f99a3fc
commit 3a27c9bbf6
2 changed files with 20 additions and 1 deletions

View File

@ -329,3 +329,21 @@ def vb_xpcom_to_attribute_dict(xpcom
]
return dict(attribute_tuples)
def vb_machine_exists(name):
try:
vbox = vb_get_manager()
vbox.findMachine(name)
return True
except Exception as e:
if isinstance(e.message, str):
message = e.message
elif isinstance(e.msg, str):
message = e.msg
else:
message = ""
if 0 > message.find("Could not find a registered machine named"):
log.error(message)
return False

View File

@ -167,7 +167,8 @@ class VirtualboxProviderTest(integration.ShellCase):
# # if test instance is still present, delete it
# if ret in query:
# # self.run_cloud('-d {0} --assume-yes'.format(INSTANCE_NAME))
virtualbox.vb_destroy_machine(INSTANCE_NAME)
if virtualbox.vb_machine_exists(INSTANCE_NAME):
virtualbox.vb_destroy_machine(INSTANCE_NAME)
class BaseVirtualboxTests(unittest.TestCase):