Merge "Google backup support client 1.8.2"

This commit is contained in:
Zuul 2020-06-04 23:08:30 +00:00 committed by Gerrit Code Review
commit 8e37faa18c
2 changed files with 18 additions and 9 deletions

View File

@ -43,13 +43,13 @@ try:
except ImportError:
client = None
import googleapiclient
from googleapiclient import discovery
from googleapiclient import errors
from googleapiclient import http
from oslo_config import cfg
from oslo_log import log as logging
from oslo_utils import timeutils
import pkg_resources
import six
from cinder.backup import chunkeddriver
@ -174,8 +174,9 @@ class GoogleBackupDriver(chunkeddriver.ChunkedBackupDriver):
# If we have google client that support google-auth library
# (v1.6.0 or higher) and all required libraries are installed use
# google-auth for the credentials
if (version.LooseVersion(googleapiclient.__version__) >=
version.LooseVersion('1.6.0') and service_account):
dist = pkg_resources.get_distribution('google-api-python-client')
if (version.LooseVersion(dist.version) >= version.LooseVersion('1.6.0')
and service_account):
creds = service_account.Credentials.from_service_account_file(
backup_credential)
OAUTH_EXCEPTIONS = (gexceptions.RefreshError,

View File

@ -623,51 +623,59 @@ class GoogleBackupDriverTestCase(test.TestCase):
self.assertEqual('none', result[0])
self.assertEqual(already_compressed_data, result[1])
@mock.patch('googleapiclient.__version__', '1.5.5')
@mock.patch('pkg_resources.get_distribution')
@mock.patch.object(google_dr.client.GoogleCredentials, 'from_stream')
@mock.patch.object(google_dr.discovery, 'build')
@mock.patch.object(google_dr, 'service_account')
def test_non_google_auth_version(self, account, build, from_stream):
def test_non_google_auth_version(self, account, build, from_stream,
get_dist_mock):
# Prior to v1.6.0 Google api client doesn't support google-auth library
get_dist_mock.return_value.version = '1.5.5'
google_dr.CONF.set_override('backup_gcs_credential_file',
'credentials_file')
google_dr.GoogleBackupDriver(self.ctxt)
get_dist_mock.assert_called_once_with('google-api-python-client')
from_stream.assert_called_once_with('credentials_file')
account.Credentials.from_service_account_file.assert_not_called()
build.assert_called_once_with('storage', 'v1', cache_discovery=False,
credentials=from_stream.return_value)
@mock.patch('googleapiclient.__version__', '1.6.6')
@mock.patch('pkg_resources.get_distribution')
@mock.patch.object(google_dr.client.GoogleCredentials, 'from_stream')
@mock.patch.object(google_dr.discovery, 'build')
@mock.patch.object(google_dr, 'service_account', None)
def test_no_httplib2_auth(self, build, from_stream):
def test_no_httplib2_auth(self, build, from_stream, get_dist_mock):
# Google api client requires google-auth-httplib2 if not present we
# use legacy credentials
get_dist_mock.return_value.version = '1.6.6'
google_dr.CONF.set_override('backup_gcs_credential_file',
'credentials_file')
google_dr.GoogleBackupDriver(self.ctxt)
get_dist_mock.assert_called_once_with('google-api-python-client')
from_stream.assert_called_once_with('credentials_file')
build.assert_called_once_with('storage', 'v1', cache_discovery=False,
credentials=from_stream.return_value)
@mock.patch('googleapiclient.__version__', '1.6.6')
@mock.patch('pkg_resources.get_distribution')
@mock.patch.object(google_dr, 'gexceptions', mock.Mock())
@mock.patch.object(google_dr.client.GoogleCredentials, 'from_stream')
@mock.patch.object(google_dr.discovery, 'build')
@mock.patch.object(google_dr, 'service_account')
def test_google_auth_used(self, account, build, from_stream):
def test_google_auth_used(self, account, build, from_stream,
get_dist_mock):
# Google api client requires google-auth-httplib2 if not present we
# use legacy credentials
get_dist_mock.return_value.version = '1.6.6'
google_dr.CONF.set_override('backup_gcs_credential_file',
'credentials_file')
google_dr.GoogleBackupDriver(self.ctxt)
get_dist_mock.assert_called_once_with('google-api-python-client')
from_stream.assert_not_called()
create_creds = account.Credentials.from_service_account_file
create_creds.assert_called_once_with('credentials_file')