Rename attrs_exist() to contains_allowed_attrs()

attrs_exist() checks the attributes in the given list against the
attributes in the expected_attrs list. The name expected_attrs is
misleading since based on the name one would expect the given list
to contain all the attributes in the expected list but thats not
what this function does. Based on this, the following changes were
made:

- renamed attr_exist() to contains_allowed_attrs() which checks
  whether all the attributes in the given list is present in the
  allowed_attrs list.

Change-Id: Ie169271e211aa93c1bd3f64a2723aa1cd03a56e9
Closes-Bug: #1285964
This commit is contained in:
mariam john 2014-09-06 23:54:01 -05:00 committed by Mariam John
parent 5f252e3bc5
commit 3864816fd6
5 changed files with 122 additions and 98 deletions

View File

@ -113,9 +113,10 @@ def _test_configuration_is_applied_to_instance(instance, configuration_id):
# check the configs exist
attrcheck = AttrCheck()
expected_attrs = [actual_key for actual_key, actual_value in actual_values]
attrcheck.attrs_exist(testconfig_info.values, expected_attrs,
msg="Configurations parameters")
allowed_attrs = [actual_key for actual_key, actual_value in actual_values]
attrcheck.contains_allowed_attrs(
testconfig_info.values, allowed_attrs,
msg="Configurations parameters")
def _get_parameter_type(name):
instance_info.dbaas.configuration_parameters.get_parameter(
@ -171,15 +172,16 @@ class CreateConfigurations(ConfigurationsTestBase):
@test
def test_expected_configurations_parameters(self):
"""Test get expected configurations parameters."""
expected_attrs = ["configuration-parameters"]
allowed_attrs = ["configuration-parameters"]
instance_info.dbaas.configuration_parameters.parameters(
instance_info.dbaas_datastore,
instance_info.dbaas_datastore_version)
resp, body = instance_info.dbaas.client.last_response
attrcheck = AttrCheck()
config_parameters_dict = json.loads(body)
attrcheck.attrs_exist(config_parameters_dict, expected_attrs,
msg="Configurations parameters")
attrcheck.contains_allowed_attrs(
config_parameters_dict, allowed_attrs,
msg="Configurations parameters")
# sanity check that a few options are in the list
config_params_list = config_parameters_dict['configuration-parameters']
config_param_keys = []
@ -197,10 +199,10 @@ class CreateConfigurations(ConfigurationsTestBase):
def test_expected_get_configuration_parameter(self):
# tests get on a single parameter to verify it has expected attributes
param_name = 'key_buffer_size'
expected_config_params = ['name', 'restart_required',
'max', 'min', 'type',
'deleted', 'deleted_at',
'datastore_version_id']
allowed_config_params = ['name', 'restart_required',
'max', 'min', 'type',
'deleted', 'deleted_at',
'datastore_version_id']
param = instance_info.dbaas.configuration_parameters.get_parameter(
instance_info.dbaas_datastore,
instance_info.dbaas_datastore_version,
@ -212,8 +214,10 @@ class CreateConfigurations(ConfigurationsTestBase):
attrcheck = AttrCheck()
config_parameter_dict = json.loads(body)
print("config_parameter_dict: %s" % config_parameter_dict)
attrcheck.attrs_exist(config_parameter_dict, expected_config_params,
msg="Get Configuration parameter")
attrcheck.contains_allowed_attrs(
config_parameter_dict,
allowed_config_params,
msg="Get Configuration parameter")
assert_equal(param_name, config_parameter_dict['name'])
with TypeCheck('ConfigurationParameter', param) as parameter:
parameter.has_field('name', basestring)

View File

@ -139,23 +139,25 @@ class Flavors(object):
@test
def test_flavor_list_attrs(self):
expected_attrs = ['id', 'name', 'ram', 'links', 'local_storage']
allowed_attrs = ['id', 'name', 'ram', 'links', 'local_storage']
flavors = self.rd_client.flavors.list()
attrcheck = AttrCheck()
for flavor in flavors:
flavor_dict = flavor._info
attrcheck.attrs_exist(flavor_dict, expected_attrs,
msg="Flavors list")
attrcheck.contains_allowed_attrs(
flavor_dict, allowed_attrs,
msg="Flavors list")
attrcheck.links(flavor_dict['links'])
@test
def test_flavor_get_attrs(self):
expected_attrs = ['id', 'name', 'ram', 'links', 'local_storage']
allowed_attrs = ['id', 'name', 'ram', 'links', 'local_storage']
flavor = self.rd_client.flavors.get(1)
attrcheck = AttrCheck()
flavor_dict = flavor._info
attrcheck.attrs_exist(flavor_dict, expected_attrs,
msg="Flavor Get 1")
attrcheck.contains_allowed_attrs(
flavor_dict, allowed_attrs,
msg="Flavor Get 1")
attrcheck.links(flavor_dict['links'])
@test

View File

@ -462,15 +462,16 @@ class CreateInstanceFail(object):
def test_mgmt_get_instance_on_create(self):
if CONFIG.test_mgmt:
result = dbaas_admin.management.show(instance_info.id)
expected_attrs = ['account_id', 'addresses', 'created',
'databases', 'flavor', 'guest_status', 'host',
'hostname', 'id', 'name', 'datastore',
'server_state_description', 'status', 'updated',
'users', 'volume', 'root_enabled_at',
'root_enabled_by']
allowed_attrs = ['account_id', 'addresses', 'created',
'databases', 'flavor', 'guest_status', 'host',
'hostname', 'id', 'name', 'datastore',
'server_state_description', 'status', 'updated',
'users', 'volume', 'root_enabled_at',
'root_enabled_by']
with CheckInstance(result._info) as check:
check.attrs_exist(result._info, expected_attrs,
msg="Mgmt get instance")
check.contains_allowed_attrs(
result._info, allowed_attrs,
msg="Mgmt get instance")
check.flavor()
check.datastore()
check.guest_status()
@ -671,19 +672,20 @@ class CreateInstance(object):
"instance was actually created." % id)
# Check these attrs only are returned in create response
expected_attrs = ['created', 'flavor', 'addresses', 'id', 'links',
'name', 'status', 'updated', 'datastore']
allowed_attrs = ['created', 'flavor', 'addresses', 'id', 'links',
'name', 'status', 'updated', 'datastore']
if ROOT_ON_CREATE:
expected_attrs.append('password')
allowed_attrs.append('password')
if VOLUME_SUPPORT:
expected_attrs.append('volume')
allowed_attrs.append('volume')
if CONFIG.trove_dns_support:
expected_attrs.append('hostname')
allowed_attrs.append('hostname')
with CheckInstance(result._info) as check:
if create_new_instance():
check.attrs_exist(result._info, expected_attrs,
msg="Create response")
check.contains_allowed_attrs(
result._info, allowed_attrs,
msg="Create response")
# Don't CheckInstance if the instance already exists.
check.flavor()
check.datastore()
@ -994,9 +996,10 @@ class TestGuestProcess(object):
if CONFIG.test_mgmt:
hwinfo = dbaas_admin.hwinfo.get(instance_info.id)
print("hwinfo : %r" % hwinfo._info)
expected_attrs = ['hwinfo']
CheckInstance(None).attrs_exist(hwinfo._info, expected_attrs,
msg="Hardware information")
allowed_attrs = ['hwinfo']
CheckInstance(None).contains_allowed_attrs(
hwinfo._info, allowed_attrs,
msg="Hardware information")
# TODO(pdmars): instead of just checking that these are int's, get
# the instance flavor and verify that the values are correct for
# the flavor
@ -1061,18 +1064,19 @@ class TestInstanceListing(object):
@test
def test_index_list(self):
expected_attrs = ['id', 'links', 'name', 'status', 'flavor',
'datastore', 'ip', 'hostname']
allowed_attrs = ['id', 'links', 'name', 'status', 'flavor',
'datastore', 'ip', 'hostname']
if VOLUME_SUPPORT:
expected_attrs.append('volume')
allowed_attrs.append('volume')
instances = dbaas.instances.list()
assert_equal(200, dbaas.last_http_code)
for instance in instances:
instance_dict = instance._info
with CheckInstance(instance_dict) as check:
print("testing instance_dict=%s" % instance_dict)
check.attrs_exist(instance_dict, expected_attrs,
msg="Instance Index")
check.contains_allowed_attrs(
instance_dict, allowed_attrs,
msg="Instance Index")
check.links(instance_dict['links'])
check.flavor()
check.datastore()
@ -1080,20 +1084,21 @@ class TestInstanceListing(object):
@test
def test_get_instance(self):
expected_attrs = ['created', 'databases', 'flavor', 'hostname', 'id',
'links', 'name', 'status', 'updated', 'ip',
'datastore']
allowed_attrs = ['created', 'databases', 'flavor', 'hostname', 'id',
'links', 'name', 'status', 'updated', 'ip',
'datastore']
if VOLUME_SUPPORT:
expected_attrs.append('volume')
allowed_attrs.append('volume')
else:
expected_attrs.append('local_storage')
allowed_attrs.append('local_storage')
instance = dbaas.instances.get(instance_info.id)
assert_equal(200, dbaas.last_http_code)
instance_dict = instance._info
print("instance_dict=%s" % instance_dict)
with CheckInstance(instance_dict) as check:
check.attrs_exist(instance_dict, expected_attrs,
msg="Get Instance")
check.contains_allowed_attrs(
instance_dict, allowed_attrs,
msg="Get Instance")
check.flavor()
check.datastore()
check.links(instance_dict['links'])
@ -1160,14 +1165,15 @@ class TestInstanceListing(object):
@test(enabled=CONFIG.test_mgmt)
def test_mgmt_get_instance_after_started(self):
result = dbaas_admin.management.show(instance_info.id)
expected_attrs = ['account_id', 'addresses', 'created', 'databases',
'flavor', 'guest_status', 'host', 'hostname', 'id',
'name', 'root_enabled_at', 'root_enabled_by',
'server_state_description', 'status', 'datastore',
'updated', 'users', 'volume']
allowed_attrs = ['account_id', 'addresses', 'created', 'databases',
'flavor', 'guest_status', 'host', 'hostname', 'id',
'name', 'root_enabled_at', 'root_enabled_by',
'server_state_description', 'status', 'datastore',
'updated', 'users', 'volume']
with CheckInstance(result._info) as check:
check.attrs_exist(result._info, expected_attrs,
msg="Mgmt get instance")
check.contains_allowed_attrs(
result._info, allowed_attrs,
msg="Mgmt get instance")
check.flavor()
check.datastore()
check.guest_status()
@ -1425,18 +1431,20 @@ class CheckInstance(AttrCheck):
if 'flavor' not in self.instance:
self.fail("'flavor' not found in instance.")
else:
expected_attrs = ['id', 'links']
self.attrs_exist(self.instance['flavor'], expected_attrs,
msg="Flavor")
allowed_attrs = ['id', 'links']
self.contains_allowed_attrs(
self.instance['flavor'], allowed_attrs,
msg="Flavor")
self.links(self.instance['flavor']['links'])
def datastore(self):
if 'datastore' not in self.instance:
self.fail("'datastore' not found in instance.")
else:
expected_attrs = ['type', 'version']
self.attrs_exist(self.instance['datastore'], expected_attrs,
msg="datastore")
allowed_attrs = ['type', 'version']
self.contains_allowed_attrs(
self.instance['datastore'], allowed_attrs,
msg="datastore")
def volume_key_exists(self):
if 'volume' not in self.instance:
@ -1448,68 +1456,76 @@ class CheckInstance(AttrCheck):
if not VOLUME_SUPPORT:
return
if self.volume_key_exists():
expected_attrs = ['size']
allowed_attrs = ['size']
if not create_new_instance():
expected_attrs.append('used')
self.attrs_exist(self.instance['volume'], expected_attrs,
msg="Volumes")
allowed_attrs.append('used')
self.contains_allowed_attrs(
self.instance['volume'], allowed_attrs,
msg="Volumes")
def used_volume(self):
if not VOLUME_SUPPORT:
return
if self.volume_key_exists():
expected_attrs = ['size', 'used']
allowed_attrs = ['size', 'used']
print(self.instance)
self.attrs_exist(self.instance['volume'], expected_attrs,
msg="Volumes")
self.contains_allowed_attrs(
self.instance['volume'], allowed_attrs,
msg="Volumes")
def volume_mgmt(self):
if not VOLUME_SUPPORT:
return
if self.volume_key_exists():
expected_attrs = ['description', 'id', 'name', 'size']
self.attrs_exist(self.instance['volume'], expected_attrs,
msg="Volumes")
allowed_attrs = ['description', 'id', 'name', 'size']
self.contains_allowed_attrs(
self.instance['volume'], allowed_attrs,
msg="Volumes")
def addresses(self):
expected_attrs = ['addr', 'version']
allowed_attrs = ['addr', 'version']
print(self.instance)
networks = ['usernet']
for network in networks:
for address in self.instance['addresses'][network]:
self.attrs_exist(address, expected_attrs,
msg="Address")
self.contains_allowed_attrs(
address, allowed_attrs,
msg="Address")
def guest_status(self):
expected_attrs = ['created_at', 'deleted', 'deleted_at', 'instance_id',
'state', 'state_description', 'updated_at']
self.attrs_exist(self.instance['guest_status'], expected_attrs,
msg="Guest status")
allowed_attrs = ['created_at', 'deleted', 'deleted_at', 'instance_id',
'state', 'state_description', 'updated_at']
self.contains_allowed_attrs(
self.instance['guest_status'], allowed_attrs,
msg="Guest status")
def mgmt_volume(self):
if not VOLUME_SUPPORT:
return
expected_attrs = ['description', 'id', 'name', 'size']
self.attrs_exist(self.instance['volume'], expected_attrs,
msg="Volume")
allowed_attrs = ['description', 'id', 'name', 'size']
self.contains_allowed_attrs(
self.instance['volume'], allowed_attrs,
msg="Volume")
def slave_of(self):
if 'replica_of' not in self.instance:
self.fail("'replica_of' not found in instance.")
else:
expected_attrs = ['id', 'links']
self.attrs_exist(self.instance['replica_of'], expected_attrs,
msg="Replica-of links not found")
allowed_attrs = ['id', 'links']
self.contains_allowed_attrs(
self.instance['replica_of'], allowed_attrs,
msg="Replica-of links not found")
self.links(self.instance['replica_of']['links'])
def slaves(self):
if 'replicas' not in self.instance:
self.fail("'replicas' not found in instance.")
else:
expected_attrs = ['id', 'links']
allowed_attrs = ['id', 'links']
for slave in self.instance['replicas']:
self.attrs_exist(slave, expected_attrs,
msg="Replica links not found")
self.contains_allowed_attrs(
slave, allowed_attrs,
msg="Replica links not found")
self.links(slave['links'])
@ -1596,10 +1612,11 @@ class BadInstanceStatusBug():
def diagnostic_tests_helper(diagnostics):
print("diagnostics : %r" % diagnostics._info)
expected_attrs = ['version', 'fdSize', 'vmSize', 'vmHwm', 'vmRss',
'vmPeak', 'threads']
CheckInstance(None).attrs_exist(diagnostics._info, expected_attrs,
msg="Diagnostics")
allowed_attrs = ['version', 'fdSize', 'vmSize', 'vmHwm', 'vmRss',
'vmPeak', 'threads']
CheckInstance(None).contains_allowed_attrs(
diagnostics._info, allowed_attrs,
msg="Diagnostics")
assert_true(isinstance(diagnostics.fdSize, int))
assert_true(isinstance(diagnostics.threads, int))
assert_true(isinstance(diagnostics.vmHwm, int))

View File

@ -91,10 +91,11 @@ class StorageAfterInstanceCreation(object):
storage = self.client.storage.index()
print("storage : %r" % storage)
print("instance_info.storage : %r" % instance_info.storage)
expected_attrs = ['name', 'type', 'used', 'provision', 'capacity']
allowed_attrs = ['name', 'type', 'used', 'provision', 'capacity']
for index, device in enumerate(storage):
CheckInstance(None).attrs_exist(device._info, expected_attrs,
msg="Storage")
CheckInstance(None).contains_allowed_attrs(
device._info,
allowed_attrs, msg="Storage")
asserts.assert_equal(device.name,
instance_info.storage[index].name)
asserts.assert_equal(device.used,

View File

@ -111,16 +111,16 @@ class AttrCheck(Check):
def fail(self, msg):
self.true(False, msg)
def attrs_exist(self, list, expected_attrs, msg=None):
def contains_allowed_attrs(self, list, allowed_attrs, msg=None):
# Check these attrs only are returned in create response
for attr in list:
if attr not in expected_attrs:
if attr not in allowed_attrs:
self.fail("%s should not contain '%s'" % (msg, attr))
def links(self, links):
expected_attrs = ['href', 'rel']
allowed_attrs = ['href', 'rel']
for link in links:
self.attrs_exist(link, expected_attrs, msg="Links")
self.contains_allowed_attrs(link, allowed_attrs, msg="Links")
class CollectionCheck(Check):