Fix keystone context for fuelclient==9.0.0

In the 9.0 release fuelclient was changed and the previously developed
approach does not work anymore. This change also includes modification
of test for the 8.0.0 version of the client.

Change-Id: Id1813b2365729c630a1aae0f6dd6a44e666d323e
This commit is contained in:
Ilya Kharin 2016-04-08 22:28:37 -05:00
parent b2da8dfa42
commit 865985bddd
2 changed files with 79 additions and 25 deletions

View File

@ -10,40 +10,68 @@
# License for the specific language governing permissions and limitations
# under the License.
from fuelclient import client
from fuelclient import fuelclient_settings
import mock
import pytest
from octane.util import fuel_client
def test_simple_overwrite(mocker):
def mock_fuelclient_80(mocker, user, password):
client = mocker.patch("fuelclient.client.APIClient",
new_callable=mock.Mock)
client.mock_add_spec(
["user", "password", "_session", "_keystone_client"],
spec_set=True,
)
client.user = user
client.password = password
return client, None
class TestContext(object):
user = "test user"
password = "test password"
def mock_fuelclient_90(mocker, user, password):
config = {
'OS_USERNAME': user,
'OS_PASSWORD': password,
}
get_settings = mocker.patch("fuelclient.fuelclient_settings.get_settings")
get_settings.return_value.configure_mock(config=config, **config)
get_settings.return_value.mock_add_spec(
["config", "OS_USERNAME", "OS_PASSWORD"],
spec_set=True,
)
client = mocker.patch("fuelclient.client.APIClient",
new_callable=mock.Mock)
client.mock_add_spec(["_session", "_keystone_client"], spec_set=True)
return client, config
conf = fuelclient_settings.get_settings()
client_val = "Not empty val"
# NOTE(akscram): It's not possible to use fixtures in parametrized tests
# as parameters and I use them as common functions. For more information
# take a look on this: https://github.com/pytest-dev/pytest/issues/349
@pytest.mark.parametrize(("auth_context", "fuelclient_fixture", "legacy"), [
(fuel_client.set_auth_context_80, mock_fuelclient_80, True),
(fuel_client.set_auth_context_90, mock_fuelclient_90, False),
])
def test_simple_overwrite(mocker, auth_context, fuelclient_fixture, legacy):
def assert_client_state(user, password):
if legacy:
assert mock_client.user == user
assert mock_client.password == password
else:
assert mock_config['OS_USERNAME'] == user
assert mock_config['OS_PASSWORD'] == password
assert conf.KEYSTONE_USER == client.APIClient.user
assert conf.KEYSTONE_PASS == client.APIClient.password
assert client.APIClient._session is None
assert client.APIClient._keystone_client is None
assert mock_client._session is None
assert mock_client._keystone_client is None
client.APIClient._session = client.APIClient._keystone_client = client_val
mock_client, mock_config = fuelclient_fixture(mocker, "userA", "passwordA")
context = mock.Mock(user="userB", password="passwordB",
spec=["user", "password"])
with fuel_client.set_auth_context(TestContext()):
assert TestContext.user == client.APIClient.user
assert TestContext.password == client.APIClient.password
assert client.APIClient._session is None
assert client.APIClient._keystone_client is None
with auth_context(context):
assert_client_state(context.user, context.password)
client.APIClient._session = client_val
client.APIClient._keystone_client = client_val
mock_client._session = mock.Mock()
mock_client._keystone_client = mock.Mock()
assert conf.KEYSTONE_USER == client.APIClient.user
assert conf.KEYSTONE_PASS == client.APIClient.password
assert client.APIClient._session is None
assert client.APIClient._keystone_client is None
assert_client_state("userA", "passwordA")

View File

@ -12,11 +12,13 @@
import contextlib
import fuelclient
from fuelclient import client
from fuelclient import fuelclient_settings
@contextlib.contextmanager
def set_auth_context(auth_context):
def set_auth_context_80(auth_context):
old_credentials = (client.APIClient.user, client.APIClient.password)
client.APIClient.user = auth_context.user
client.APIClient.password = auth_context.password
@ -26,3 +28,27 @@ def set_auth_context(auth_context):
finally:
(client.APIClient.user, client.APIClient.password) = old_credentials
client.APIClient._session = client.APIClient._keystone_client = None
@contextlib.contextmanager
def set_auth_context_90(auth_context):
settings = fuelclient_settings.get_settings()
config = settings.config
old_credentials = (settings.OS_USERNAME, settings.OS_PASSWORD)
config['OS_USERNAME'] = auth_context.user
config['OS_PASSWORD'] = auth_context.password
client.APIClient._session = client.APIClient._keystone_client = None
try:
yield
finally:
(config['OS_USERNAME'], config['OS_PASSWORD']) = old_credentials
client.APIClient._session = client.APIClient._keystone_client = None
# NOTE(akscram): The 9.0.0 release for fuelclient is not yet available
# on PyPI but to test it on master nodes with the 9.0 release some
# workaround is needed.
if fuelclient.__version__ == "8.0.0":
set_auth_context = set_auth_context_80
else:
set_auth_context = set_auth_context_90