Files
python-cratonclient/cratonclient/tests/integration/base.py
Ian Cordasco a510d64cec Add Betamax for testing
Use keystoneauth1's Betamax test fixture to start recording
request-response interaction pairs in files. I'm starting this bit of
work with our hosts API module.

As a result of this testing, I discovered that we were not handling
keystoneauth1 exceptions inside of our session. This adds handling for
that with a new function for finding the right cratonclient exception to
raise in cratonclient.exceptions. Thus, this also includes testing for
the convenience functions in that module.

Users need only setup 4 environment variables for testing:

* CRATON_DEMO_USERNAME
* CRATON_DEMO_PROJECT
* CRATON_DEMO_TOKEN
* CRATON_DEMO_URL

Depends-On: I45de545b040d2b99ac8f3f4d3c81359615d328e8
Change-Id: Ib69d825a50a7e4179aefd11bcbfbed39c27c7fbe
Partially-implements: bp testing-plan
2017-03-21 10:27:33 -05:00

113 lines
4.1 KiB
Python

# -*- coding: utf-8 -*-
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""Module containing the base logic for cratonclient integration tests."""
import os
import betamax
from betamax_matchers import json_body
from keystoneauth1.fixture import keystoneauth_betamax as ksabetamax
from cratonclient import auth
from cratonclient.tests import base
from cratonclient.v1 import client
# NOTE(sigmavirus24): This allows us to use ``'json-body'`` as a matcher below
betamax.Betamax.register_request_matcher(json_body.JSONBodyMatcher)
envget = os.environ.get
CRATON_DEMO_USERNAME = envget('CRATON_DEMO_USERNAME', 'demo')
CRATON_DEMO_TOKEN = envget('CRATON_DEMO_TOKEN', 'demo')
CRATON_DEMO_PROJECT = envget('CRATON_DEMO_PROJECT',
'b9f10eca66ac4c279c139d01e65f96b5')
CRATON_ROOT_USERNAME = envget('CRATON_ROOT_USERNAME', 'root')
CRATON_ROOT_TOKEN = envget('CRATON_ROOT_TOKEN', 'root')
CRATON_ROOT_PROJECT = envget('CRATON_ROOT_PROJECT',
'b9f10eca66ac4c279c139d01e65f96b5')
CRATON_URL = envget('CRATON_URL', 'http://127.0.0.1:8080/v1')
class BetamaxTestCase(base.TestCase):
"""This sets up Betamax with Keystoneauth1 fixture for integration tests.
This relies on existing keystoneauth1 integration with the Betamax library
to make recording integration tests easier.
"""
CASSETTE_LIBRARY_DIR = 'cratonclient/tests/cassettes/'
def generate_cassette_name(self):
"""Generate a cassette name for the current test."""
full_test_name = self.id()
module, test_class, test_method = full_test_name.rsplit('.', 2)
return test_class + '-' + test_method
def setUp(self):
"""Set up betamax fixture for cratonclient."""
super(BetamaxTestCase, self).setUp()
self.cassette_name = self.generate_cassette_name()
self.record_mode = envget('BETAMAX_RECORD_MODE', 'once')
self.url = CRATON_URL
self.betamax_fixture = self.useFixture(ksabetamax.BetamaxFixture(
cassette_name=self.cassette_name,
cassette_library_dir=self.CASSETTE_LIBRARY_DIR,
record=self.record_mode,
))
self.demo_credentials = {
'username': CRATON_DEMO_USERNAME,
'token': CRATON_DEMO_TOKEN,
'project': CRATON_DEMO_PROJECT,
}
self.root_credentials = {
'username': CRATON_ROOT_USERNAME,
'token': CRATON_ROOT_TOKEN,
'project': CRATON_ROOT_PROJECT,
}
def create_client(self, username, token, project):
"""Create a Craton client using Craton Auth."""
self.session = auth.craton_auth(
username=username,
token=token,
project_id=project,
)
self.client = client.Client(self.session, self.url)
def create_demo_client(self):
"""Set up cratonclient with the demo user."""
self.create_client(**self.demo_credentials)
with betamax.Betamax.configure() as config:
config.define_cassette_placeholder(
'<craton-demo-username>', CRATON_DEMO_USERNAME,
)
config.define_cassette_placeholder(
'<craton-demo-token>', CRATON_DEMO_TOKEN,
)
config.define_cassette_placeholder(
'<craton-demo-project>', CRATON_DEMO_PROJECT,
)
config.define_cassette_placeholder(
'<craton-root-username>', CRATON_ROOT_USERNAME,
)
config.define_cassette_placeholder(
'<craton-root-token>', CRATON_ROOT_TOKEN,
)
config.define_cassette_placeholder(
'<craton-root-project>', CRATON_ROOT_PROJECT,
)
config.define_cassette_placeholder(
'<craton-url>', CRATON_URL,
)