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

View File

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

View File

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

View File

@ -91,10 +91,11 @@ class StorageAfterInstanceCreation(object):
storage = self.client.storage.index() storage = self.client.storage.index()
print("storage : %r" % storage) print("storage : %r" % storage)
print("instance_info.storage : %r" % instance_info.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): for index, device in enumerate(storage):
CheckInstance(None).attrs_exist(device._info, expected_attrs, CheckInstance(None).contains_allowed_attrs(
msg="Storage") device._info,
allowed_attrs, msg="Storage")
asserts.assert_equal(device.name, asserts.assert_equal(device.name,
instance_info.storage[index].name) instance_info.storage[index].name)
asserts.assert_equal(device.used, asserts.assert_equal(device.used,

View File

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