HP3PAR driver log the SHA1 for driver and mediator correctly

The HP3PAR driver is logging this information for support, but
it was calculating it incorrectly.

Change-Id: I8c325298b833b10d681d0082d12fe1a37dba4424
Closes-Bug: #1431559
This commit is contained in:
Mark Sturdevant 2015-03-12 16:57:11 -07:00
parent 920df86d1f
commit 0ca5d97f94
2 changed files with 41 additions and 23 deletions

View File

@ -17,6 +17,7 @@
import hashlib
import inspect
import logging
import os
from oslo_config import cfg
import six
@ -128,26 +129,10 @@ class HP3ParShareDriver(driver.ShareDriver):
try:
# Log the source SHA for support. Only do this with DEBUG.
if LOG.isEnabledFor(logging.DEBUG):
driver_source = inspect.getsourcelines(HP3ParShareDriver)
driver_sha1 = hashlib.sha1('blob %(source_size)s\0%('
'source_string)s' %
{
'source_size': len(
driver_source),
'source_string': driver_source,
})
LOG.debug('HP3ParShareDriver SHA1: %s',
driver_sha1.hexdigest())
mediator_source = inspect.getsourcelines(
hp_3par_mediator.HP3ParMediator)
mediator_sha1 = hashlib.sha1(
'blob %(source_size)s\0%(source_string)s' %
{
'source_size': len(mediator_source),
'source_string': mediator_source,
})
LOG.debug('HP3ParMediator SHA1: %s', mediator_sha1.hexdigest())
self.sha1_hash(HP3ParShareDriver))
LOG.debug('HP3ParMediator SHA1: %s',
self.sha1_hash(hp_3par_mediator.HP3ParMediator))
except Exception as e:
# Don't let any exceptions during the SHA1 logging interfere
# with startup. This is just debug info to identify the source
@ -155,6 +140,20 @@ class HP3ParShareDriver(driver.ShareDriver):
LOG.debug('Source code SHA1 not logged due to: %s',
six.text_type(e))
@staticmethod
def sha1_hash(clazz):
"""Get the SHA1 hash for the source of a class."""
source_file = inspect.getsourcefile(clazz)
file_size = os.path.getsize(source_file)
sha1 = hashlib.sha1()
sha1.update(("blob %u\0" % file_size).encode('utf-8'))
with open(source_file, 'rb') as f:
sha1.update(f.read())
return sha1.hexdigest()
@staticmethod
def _build_export_location(protocol, ip, path):
if protocol == 'NFS':

View File

@ -54,6 +54,7 @@ class HP3ParDriverTestCase(test.TestCase):
return None
self.conf.safe_get = safe_get
self.real_hp_3par_mediator = hp3parmediator.HP3ParMediator
self.mock_object(hp3parmediator, 'HP3ParMediator')
self.mock_mediator_constructor = hp3parmediator.HP3ParMediator
self.mock_mediator = self.mock_mediator_constructor()
@ -177,14 +178,32 @@ class HP3ParDriverTestCase(test.TestCase):
share_server)
return location
def test_driver_check_for_setup_error(self):
"""check_for_setup_error should not raise any exceptions."""
def test_driver_check_for_setup_error_success(self):
"""check_for_setup_error when things go well."""
# Generally this is always mocked, but here we reference the class.
hp3parmediator.HP3ParMediator = self.real_hp_3par_mediator
self.mock_object(hp3pardriver, 'LOG')
self.init_driver()
self.driver.check_for_setup_error()
expected_calls = [mock.call.debug(mock.ANY, mock.ANY),
mock.call.debug(mock.ANY, mock.ANY)]
expected_calls = [
mock.call.debug('HP3ParShareDriver SHA1: %s', mock.ANY),
mock.call.debug('HP3ParMediator SHA1: %s', mock.ANY)
]
hp3pardriver.LOG.assert_has_calls(expected_calls)
def test_driver_check_for_setup_error_exception(self):
"""check_for_setup_error catch and log any exceptions."""
# Since HP3ParMediator is mocked, we'll hit the except/log.
self.mock_object(hp3pardriver, 'LOG')
self.init_driver()
self.driver.check_for_setup_error()
expected_calls = [
mock.call.debug('HP3ParShareDriver SHA1: %s', mock.ANY),
mock.call.debug('Source code SHA1 not logged due to: %s', mock.ANY)
]
hp3pardriver.LOG.assert_has_calls(expected_calls)
def test_driver_create_cifs_share(self):