Permalink
Browse files

Make TestCaseWithAccessor not cassandra specific

Signed-off-by: Thibault Chataigner <t.chataigner@criteo.com>
  • Loading branch information...
Thib17 authored and iksaif committed Jul 11, 2018
1 parent 4f19f8c commit 07273e3c12a2ec309fc5d0c0da973d87db3256f5
Showing with 183 additions and 122 deletions.
  1. +21 −113 biggraphite/test_utils.py
  2. +141 −0 biggraphite/test_utils_cassandra.py
  3. +21 −9 tests/test_cassandra.py
@@ -28,56 +28,22 @@
import logging
import uuid
from cassandra import cluster as c_cluster
import mock
from biggraphite import utils as bg_utils
from biggraphite import accessor as bg_accessor
from biggraphite.drivers import cassandra as bg_cassandra
from biggraphite.test_utils_cassandra import CassandraHelper
from biggraphite.drivers import memory as bg_memory
from biggraphite import metadata_cache as bg_metadata_cache
HAS_CASSANDRA_HOME = bool(os.getenv("CASSANDRA_HOME"))
CASSANDRA_HOSTPORT = os.getenv("CASSANDRA_HOSTPORT")
HAS_CASSANDRA = HAS_CASSANDRA_HOME or CASSANDRA_HOSTPORT
# Only try to import cassandra if we are going to use it. This is better
# than using try/except because the failure case is easier to handle.
if HAS_CASSANDRA_HOME:
from testing import cassandra3 as testing_cassandra
class _SlowerTestingCassandra(testing_cassandra.Cassandra):
"""Just like testing_cassandra.Cassandra but waits 5 minutes for start."""
# For older versions.
BOOT_TIMEOUT = 5 * 60
def setup_logging():
"""To be called to enable logs in unittests."""
logger = logging.getLogger()
stream_handler = logging.StreamHandler(sys.stdout)
logger.addHandler(stream_handler)
def create_unreplicated_keyspace(session, keyspace):
"""Create a keyspace, mostly used for tests."""
template = (
"CREATE KEYSPACE \"%s\" "
" WITH replication = {'class':'SimpleStrategy', 'replication_factor' : 1}"
" AND durable_writes = false;"
)
session.execute(template % keyspace)
def drop_keyspace(session, keyspace):
"""Drop a keyspace, mostly used for tests."""
template = (
"DROP KEYSPACE IF EXISTS \"%s\";"
)
session.execute(template % keyspace)
def prepare_graphite():
"""Make sure that we have a working Graphite environment."""
# Setup sys.path
@@ -204,80 +170,35 @@ def make_metric(self, name, metadata=None, **kwargs):
return make_metric(name, metadata=metadata, accessor=self.accessor, **kwargs)
@unittest.skipUnless(
HAS_CASSANDRA, "CASSANDRA_HOME must be set to a >=3.5 install",
)
class TestCaseWithAccessor(TestCaseWithTempDir):
"""A TestCase with an Accessor for an ephemeral Cassandra cluster."""
"""A TestCase with an Accessor."""
KEYSPACE = "testkeyspace"
CACHE_CLASS = bg_metadata_cache.MemoryCache
ACCESSOR_SETTINGS = {}
CACHE_CLASS = bg_metadata_cache.MemoryCache
cassandra_helper = None
@classmethod
def setUpClass(cls):
"""Create the test Cassandra Cluster as cls.cassandra."""
super(TestCaseWithAccessor, cls).setUpClass()
cls.cassandra = None
if CASSANDRA_HOSTPORT:
host, cls.port = CASSANDRA_HOSTPORT.split(':')
cls.contact_points = [host]
else:
cls.setUpCassandra()
# Make it easy to do raw queries to Cassandra.
cls.cluster = c_cluster.Cluster(cls.contact_points, cls.port)
cls.session = cls.cluster.connect()
cls._reset_keyspace(cls.session, cls.KEYSPACE)
cls._reset_keyspace(cls.session, cls.KEYSPACE + "_metadata")
cls.accessor = bg_cassandra.build(
keyspace=cls.KEYSPACE,
contact_points=cls.contact_points,
port=cls.port,
timeout=60,
**cls.ACCESSOR_SETTINGS
)
"""Create the test Accessor."""
driver_name = cls.ACCESSOR_SETTINGS.get('driver', bg_utils.DEFAULT_DRIVER)
if "cassandra" in driver_name:
cls.cassandra_helper = CassandraHelper()
cls.cassandra_helper.setUpClass()
cls.ACCESSOR_SETTINGS.update(
cls.cassandra_helper.get_accessor_settings())
cls.accessor = bg_utils.accessor_from_settings(cls.ACCESSOR_SETTINGS)
cls.accessor.syncdb()
cls.accessor.connect()
@classmethod
def setUpCassandra(cls):
"""Start Cassandra."""
cls.cassandra = _SlowerTestingCassandra(
auto_start=False,
boot_timeout=_SlowerTestingCassandra.BOOT_TIMEOUT
)
try:
cls.cassandra.setup()
cls.cassandra.start()
except Exception as e:
logging.exception(e)
print("fail to starting cassandra, logging potentially useful debug info",
file=sys.stderr)
for attr in "cassandra_home", "cassandra_yaml", "cassandra_bin", "base_dir", "settings":
print(attr, ":", getattr(cls.cassandra,
attr, "Unknown"), file=sys.stderr)
cls.cassandra.cleanup()
raise
# testing.cassandra is meant to be used with the Thrift API, so we need to
# extract the IPs and native port for use with the native driver.
cls.contact_points = [s.split(":")[0]
for s in cls.cassandra.server_list()]
cls.port = cls.cassandra.cassandra_yaml["native_transport_port"]
@classmethod
def tearDownClass(cls):
"""Stop the test Cassandra Cluster."""
"""Stop the test Accessor."""
super(TestCaseWithAccessor, cls).tearDownClass()
cls.accessor.shutdown()
cls.cluster.shutdown()
cls.session = None
cls.cluster = None
if cls.cassandra:
cls.cassandra.stop()
cls.cassandra = None
if cls.cassandra_helper:
cls.cassandra_helper.tearDownClass()
def setUp(self):
"""Create a new Accessor in self.acessor."""
@@ -294,24 +215,11 @@ def tearDown(self):
self.accessor.clear()
self.__drop_all_metrics()
@classmethod
def _reset_keyspace(cls, session, keyspace):
drop_keyspace(session, keyspace)
create_unreplicated_keyspace(session, keyspace)
def flush(self):
"""Flush all kind of buffers related to Cassandra."""
"""Flush all kind of buffers."""
self.accessor.flush()
# When using Lucene, we need to force a refresh on the index as the default
# refresh period is 60s.
if self.accessor.use_lucene:
statements = self.accessor.metadata_query_generator.sync_queries()
for statement in statements:
self.session.execute(statement)
# Hope that after this all async calls will have been processed.
self.cluster.refresh_schema_metadata()
if self.cassandra_helper:
self.cassandra_helper.flush(self.accessor)
def make_metric(self, name, metadata=None, **kwargs):
"""Create a bg_accessor.Metric with specified metadata."""
@@ -0,0 +1,141 @@
#!/usr/bin/env python
# Copyright 2018 Criteo
#
# 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.
"""Utilities factoring code across tests.
The dependencies for this module are not included in requirements.txt or in the package
dependencies, instead one needs the elements of tests-requirements.txt .
"""
from __future__ import absolute_import
from __future__ import print_function
import os
import sys
import logging
from cassandra import cluster as c_cluster
HAS_CASSANDRA_HOME = bool(os.getenv("CASSANDRA_HOME"))
CASSANDRA_HOSTPORT = os.getenv("CASSANDRA_HOSTPORT")
HAS_CASSANDRA = HAS_CASSANDRA_HOME or CASSANDRA_HOSTPORT
# Only try to import cassandra if we are going to use it. This is better
# than using try/except because the failure case is easier to handle.
if HAS_CASSANDRA_HOME:
from testing import cassandra3 as testing_cassandra
class CassandraHelper():
"""Helper for an ephemeral Cassandra cluster."""
KEYSPACE = "testkeyspace"
@classmethod
def get_accessor_settings(cls):
"""Prepare accessor settings for Cassandra driver."""
return {
"cassandra_keyspace": cls.KEYSPACE,
"cassandra_contact_points": cls.contact_points,
"cassandra_port": cls.port,
"cassandra_timeout": 60
}
@classmethod
def setUpClass(cls):
"""Create the test Cassandra Cluster as cls.cassandra."""
cls.cassandra = None
if CASSANDRA_HOSTPORT:
host, cls.port = CASSANDRA_HOSTPORT.split(':')
cls.contact_points = [host]
else:
cls.setUpCassandra()
# Make it easy to do raw queries to Cassandra.
cls.cluster = c_cluster.Cluster(cls.contact_points, cls.port)
cls.session = cls.cluster.connect()
cls._reset_keyspace(cls.session, cls.KEYSPACE)
cls._reset_keyspace(cls.session, cls.KEYSPACE + "_metadata")
@classmethod
def setUpCassandra(cls):
"""Start Cassandra."""
cls.cassandra = testing_cassandra.Cassandra(
auto_start=False,
)
try:
cls.cassandra.setup()
cls.cassandra.start()
except Exception as e:
logging.exception(e)
print("fail to starting cassandra, logging potentially useful debug info",
file=sys.stderr)
for attr in "cassandra_home", "cassandra_yaml", "cassandra_bin", "base_dir", "settings":
print(attr, ":", getattr(cls.cassandra,
attr, "Unknown"), file=sys.stderr)
cls.cassandra.cleanup()
raise
# testing.cassandra is meant to be used with the Thrift API, so we need to
# extract the IPs and native port for use with the native driver.
cls.contact_points = [s.split(":")[0]
for s in cls.cassandra.server_list()]
cls.port = cls.cassandra.cassandra_yaml["native_transport_port"]
@classmethod
def tearDownClass(cls):
"""Stop the test Cassandra Cluster."""
cls.cluster.shutdown()
cls.session = None
cls.cluster = None
if cls.cassandra:
cls.cassandra.stop()
cls.cassandra = None
@classmethod
def _reset_keyspace(cls, session, keyspace):
cls.drop_keyspace(session, keyspace)
cls.create_unreplicated_keyspace(session, keyspace)
@staticmethod
def create_unreplicated_keyspace(session, keyspace):
"""Create a keyspace, mostly used for tests."""
template = (
"CREATE KEYSPACE \"%s\" "
" WITH replication = {'class':'SimpleStrategy', 'replication_factor' : 1}"
" AND durable_writes = false;"
)
session.execute(template % keyspace)
@staticmethod
def drop_keyspace(session, keyspace):
"""Drop a keyspace, mostly used for tests."""
template = (
"DROP KEYSPACE IF EXISTS \"%s\";"
)
session.execute(template % keyspace)
def flush(self, accessor):
"""Flush all kind of buffers related to Cassandra."""
accessor.flush()
# When using Lucene, we need to force a refresh on the index as the default
# refresh period is 60s.
if accessor.use_lucene:
statements = accessor.metadata_query_generator.sync_queries()
for statement in statements:
self.session.execute(statement)
# Hope that after this all async calls will have been processed.
self.cluster.refresh_schema_metadata()
Oops, something went wrong.

0 comments on commit 07273e3

Please sign in to comment.