Remove uses of assert in production code (#32079)

* Remove uses of assert in production code

* Fix assertion

* Add code smell test for assertions, currently limited to lib/ansible

* Fix assertion

* Add docs for no-assert

* Remove new assert from enos

* Fix assert in module_utils.connection
This commit is contained in:
Matt Martz
2017-11-13 10:51:18 -06:00
committed by ansibot
parent 464ded80f5
commit 99d4f5bab4
38 changed files with 195 additions and 89 deletions

View File

@@ -285,7 +285,8 @@ def check_dp_status(client, dp_id, status):
:returns: True or False
"""
assert isinstance(status, list)
if not isinstance(status, list):
raise AssertionError()
if pipeline_field(client, dp_id, field="@pipelineState") in status:
return True
else:

View File

@@ -380,7 +380,8 @@ class ClcGroup(object):
changed: Boolean- whether a change was made,
group: A clc group object for the group
"""
assert self.root_group, "Implementation Error: Root Group not set"
if not self.root_group:
raise AssertionError("Implementation Error: Root Group not set")
parent = parent_name if parent_name is not None else self.root_group.name
description = group_description
changed = False

View File

@@ -237,7 +237,8 @@ class Droplet(JsonfyMixIn):
self.update_attr(json)
def power_on(self):
assert self.status == 'off', 'Can only power on a closed one.'
if self.status != 'off':
raise AssertionError('Can only power on a closed one.')
json = self.manager.power_on_droplet(self.id)
self.update_attr(json)

View File

@@ -424,8 +424,10 @@ class PyVmomiDeviceHelper(object):
diskspec.device.backing.diskMode = 'persistent'
diskspec.device.controllerKey = scsi_ctl.device.key
assert self.next_disk_unit_number != 7
assert disk_index != 7
if self.next_disk_unit_number == 7:
raise AssertionError()
if disk_index == 7:
raise AssertionError()
"""
Configure disk unit number.
"""
@@ -1127,7 +1129,8 @@ class PyVmomiHelper(PyVmomi):
return datastore, datastore_name
def obj_has_parent(self, obj, parent):
assert obj is not None and parent is not None
if obj is None and parent is None:
raise AssertionError()
current_parent = obj
while True:
@@ -1573,7 +1576,7 @@ def main():
result["failed"] = False
else:
# This should not happen
assert False
raise AssertionError()
# VM doesn't exist
else:
if module.params['state'] in ['poweredon', 'poweredoff', 'present', 'restarted', 'suspended']:

View File

@@ -342,7 +342,7 @@ class PyVmomiHelper(object):
task = vm.RemoveAllSnapshots()
else:
# This should not happen
assert False
raise AssertionError()
if task:
self.wait_for_task(task)