def test_connection_context_client(self):
        """Instantiate the client and ensure that it quacks like a duck.
        """
        es_context = ConnectionContext(config=self.config.elasticsearch)
        client = es_context.connection()

        # The client *must* have specific elements.
        ok_(client._connection)
        ok_(client.close)
        ok_(client.commit)
        ok_(client.config)
        ok_(client.rollback)

        # The underlying ES interface is exposed by _connection. This API is
        # exhaustive and well outside of the scope of this test suite; in the
        # interest of safety however, we'll check one here.
        ok_(client._connection.index)
    def test_connection_context_client(self):
        """Instantiate the client and ensure that it quacks like a duck.
        """
        es_context = ConnectionContext(config=self.config.elasticsearch)
        client = es_context.connection()

        # The client *must* have specific elements.
        ok_(client._connection)
        ok_(client.close)
        ok_(client.commit)
        ok_(client.config)
        ok_(client.rollback)

        # The underlying ES interface is exposed by _connection. This API is
        # exhaustive and well outside of the scope of this test suite; in the
        # interest of safety however, we'll check one here.
        ok_(client._connection.index)
Exemple #3
0
class ElasticsearchTestCase(TestCaseWithConfig):
    """Base class for Elastic Search related unit tests"""
    def setup_method(self):
        super().setup_method()
        self.config = self.get_base_config()
        self.es_context = ConnectionContext(self.config)
        self.crashstorage = ESCrashStorage(
            config=self.get_tuned_config(ESCrashStorage))

        self.index_client = self.es_context.indices_client()
        self.conn = self.es_context.connection()

        # Delete everything there first
        for index_name in self.es_context.get_indices():
            print(f"setup: delete test index: {index_name}")
            self.es_context.delete_index(index_name)

        to_create = [
            self.es_context.get_index_for_date(utc_now()),
            self.es_context.get_index_for_date(utc_now() - timedelta(days=7)),
        ]
        for index_name in to_create:
            print(f"setup: creating index: {index_name}")
            self.es_context.create_index(index_name)

    def teardown_method(self):
        for index_name in self.es_context.get_indices():
            print(f"teardown: delete test index: {index_name}")
            self.es_context.delete_index(index_name)
        super().teardown_method()

    def health_check(self):
        self.conn.cluster.health(wait_for_status="yellow", request_timeout=5)

    def get_url(self):
        """Returns the first url in the elasticsearch_urls list"""
        return self.config.elasticsearch_urls[0]

    def get_tuned_config(self, sources, extra_values=None):
        values_source = DEFAULT_VALUES.copy()
        if extra_values:
            values_source.update(extra_values)

        return super().get_tuned_config(sources, values_source)

    def get_base_config(self, cls=ConnectionContext, es_index=None):
        extra_values = None
        if es_index:
            extra_values = {
                "resource.elasticsearch.elasticsearch_index": es_index
            }

        return self.get_tuned_config(cls, extra_values=extra_values)

    def index_crash(self,
                    processed_crash=None,
                    raw_crash=None,
                    crash_id=None,
                    refresh=True):
        """Index a single crash and refresh"""
        if crash_id is None:
            crash_id = str(uuid.UUID(int=random.getrandbits(128)))

        raw_crash = raw_crash or {}
        processed_crash = processed_crash or {}
        raw_crash["uuid"] = crash_id
        processed_crash["crash_id"] = crash_id
        processed_crash["uuid"] = crash_id

        self.crashstorage.save_processed_crash(raw_crash, processed_crash)

        if refresh:
            self.es_context.refresh()

        return crash_id

    def index_many_crashes(self,
                           number,
                           processed_crash=None,
                           raw_crash=None,
                           loop_field=None):
        """Index multiple crashes and refresh at the end"""
        processed_crash = processed_crash or {}
        raw_crash = raw_crash or {}

        crash_ids = []
        for i in range(number):
            if loop_field is not None:
                processed_copy = processed_crash.copy()
                processed_copy[loop_field] = processed_crash[loop_field] % i
            else:
                processed_copy = processed_crash

            crash_ids.append(
                self.index_crash(raw_crash=raw_crash,
                                 processed_crash=processed_copy,
                                 refresh=False))

        self.es_context.refresh()
        return crash_ids