Author: tomaz
Date: Wed May 22 01:17:09 2013
New Revision: 1485024
URL: http://svn.apache.org/r1485024
Log:
Subtract grace period from the auth token expires time to account for
an HTTP latency and add a test case for it.
Modified:
libcloud/trunk/libcloud/common/openstack.py
libcloud/trunk/libcloud/test/compute/test_openstack.py
Modified: libcloud/trunk/libcloud/common/openstack.py
URL:
http://svn.apache.org/viewvc/libcloud/trunk/libcloud/common/openstack.py?rev=1485024&r1=1485023&r2=1485024&view=diff
==============================================================================
--- libcloud/trunk/libcloud/common/openstack.py (original)
+++ libcloud/trunk/libcloud/common/openstack.py Wed May 22 01:17:09 2013
@@ -19,8 +19,7 @@ Common utilities for OpenStack
import sys
import binascii
import os
-
-from datetime import datetime
+import datetime
from libcloud.utils.py3 import httplib
from libcloud.utils.iso8601 import parse_date
@@ -44,11 +43,19 @@ AUTH_VERSIONS_WITH_EXPIRES = [
'2.0_password'
]
+# How many seconds to substract from the auth token expiration time before
+# testing if the token is still valid.
+# The time is subtracted to account for the HTTP request latency and prevent
+# user from getting "InvalidCredsError" if token is about to expire.
+AUTH_TOKEN_EXPIRES_GRACE_SECONDS = 5
+
__all__ = [
'OpenStackBaseConnection',
'OpenStackAuthConnection',
'OpenStackServiceCatalog',
- 'OpenStackDriverMixin'
+ 'OpenStackDriverMixin',
+
+ 'AUTH_TOKEN_EXPIRES_GRACE_SECONDS'
]
@@ -132,8 +139,8 @@ class OpenStackAuthConnection(Connection
"""
if not force and self.auth_version in AUTH_VERSIONS_WITH_EXPIRES \
and self._is_token_valid():
- # If token is still valid, there is no need to re-authenticate
- return self
+ # If token is still valid, there is no need to re-authenticate
+ return self
if self.auth_version == "1.0":
return self.authenticate_1_0()
@@ -288,8 +295,11 @@ class OpenStackAuthConnection(Connection
if not self.auth_token_expires:
return False
- time_tuple_expires = self.auth_token_expires.utctimetuple()
- time_tuple_now = datetime.utcnow().utctimetuple()
+ expires = self.auth_token_expires - \
+ datetime.timedelta(seconds=AUTH_TOKEN_EXPIRES_GRACE_SECONDS)
+
+ time_tuple_expires = expires.utctimetuple()
+ time_tuple_now = datetime.datetime.utcnow().utctimetuple()
# TODO: Subtract some reasonable grace time period
if time_tuple_now < time_tuple_expires:
Modified: libcloud/trunk/libcloud/test/compute/test_openstack.py
URL:
http://svn.apache.org/viewvc/libcloud/trunk/libcloud/test/compute/test_openstack.py?rev=1485024&r1=1485023&r2=1485024&view=diff
==============================================================================
--- libcloud/trunk/libcloud/test/compute/test_openstack.py (original)
+++ libcloud/trunk/libcloud/test/compute/test_openstack.py Wed May 22 01:17:09
2013
@@ -32,6 +32,7 @@ from libcloud.common.types import Invali
LibcloudError
from libcloud.common.openstack import OpenStackBaseConnection
from libcloud.common.openstack import OpenStackAuthConnection
+from libcloud.common.openstack import AUTH_TOKEN_EXPIRES_GRACE_SECONDS
from libcloud.compute.types import Provider
from libcloud.compute.providers import get_driver
from libcloud.compute.drivers.openstack import (
@@ -187,6 +188,23 @@ class OpenStackAuthConnectionTests(unitt
self.assertEqual(mocked_auth_method.call_count, 1)
+ # No force reauth, valid / non-expired token which is about to expire
in
+ # less than AUTH_TOKEN_EXPIRES_GRACE_SECONDS
+ soon = datetime.datetime.now() + \
+ datetime.timedelta(seconds=AUTH_TOKEN_EXPIRES_GRACE_SECONDS - 1)
+ osa.auth_token = None
+
+ mocked_auth_method.call_count = 0
+ self.assertEqual(mocked_auth_method.call_count, 0)
+
+ for i in range(0, count):
+ osa.authenticate(force=False)
+
+ if i == 0:
+ osa.auth_token_expires = soon
+
+ self.assertEqual(mocked_auth_method.call_count, 5)
+
def _get_mock_connection(self, mock_http_class):
connection = OpenStackBaseConnection(*OPENSTACK_PARAMS)
connection.conn_classes = (mock_http_class, mock_http_class)