Browse Source

Remove redundant code and improve test coverage

storage/driver.py is redundant file
get_opts in elasticsearchv2.py is redundant

Change-Id: I2c6d892016508c21fbc782d1628136618342c21f
tags/7.1.0
gengchc2 1 year ago
parent
commit
857c239e97
4 changed files with 40 additions and 124 deletions
  1. +0
    -76
      freezer_api/storage/driver.py
  2. +0
    -33
      freezer_api/storage/elastic.py
  3. +0
    -3
      freezer_api/storage/elasticv2.py
  4. +40
    -12
      freezer_api/tests/unit/test_elastic.py

+ 0
- 76
freezer_api/storage/driver.py View File

@@ -1,76 +0,0 @@
"""
(c) Copyright 2014,2015 Hewlett-Packard Development Company, L.P.

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.

"""

from freezer_api.storage import elastic

from oslo_config import cfg
from oslo_log import log
from oslo_utils import importutils


CONF = cfg.CONF
LOG = log.getLogger(__name__)


# storage backend options to be registered
_OPTS = [
cfg.StrOpt("backend",
help="Database backend section name. This section "
"will be loaded by the proper driver to connect to "
"the database."
),
cfg.StrOpt('driver',
default='freezer_api.storage.elasticv2.ElasticSearchEngineV2',
help="Database driver to be used."
)
]


def register_storage_opts():
"""Register storage configuration options"""
opt_group = cfg.OptGroup(name='storage',
title='Freezer Storage Engine')
CONF.register_group(opt_group)
CONF.register_opts(_OPTS, group=opt_group)


def get_db(driver=None):
"""Automatically loads the database driver to be used."""
storage = CONF.get('storage')
if not driver:
driver = storage['driver']
driver_instance = importutils.import_object(
driver,
backend=storage['backend']
)

return driver_instance


def get_storage_opts():
"""Returns a dict that contains list of options for db backend"""
opts = {"storage": _OPTS}
opts.update(_get_elastic_opts())
return opts


def _get_elastic_opts(backend=None):
"""Return Opts for elasticsearch driver"""
if not backend:
backend = "elasticsearch"
es = elastic.ElasticSearchEngine(backend=backend)
return {backend: es.get_opts()}

+ 0
- 33
freezer_api/storage/elastic.py View File

@@ -280,34 +280,6 @@ class SessionTypeManager(TypeManager):

class ElasticSearchEngine(object):

_OPTS = [
cfg.ListOpt('hosts',
default=['http://127.0.0.1:9200'],
help='specify the storage hosts'),
cfg.StrOpt('index',
default='freezer',
help='specify the name of the elasticsearch index'),
cfg.IntOpt('timeout',
default=60,
help='specify the connection timeout'),
cfg.IntOpt('retries',
default=20,
help='number of retries to allow before raising and error'),
cfg.BoolOpt('use_ssl',
default=False,
help='explicitly turn on SSL'),
cfg.BoolOpt('verify_certs',
default=False,
help='turn on SSL certs verification'),
cfg.StrOpt('ca_certs',
help='path to CA certs on disk'),
cfg.IntOpt('number_of_replicas',
default=0,
help='Number of replicas for elk cluster. Default is 0. '
'Use 0 for no replicas. This should be set to (number '
'of node in the ES cluter -1).')
]

def __init__(self, backend):
"""backend: name of the section in the config file to load
elasticsearch opts
@@ -319,8 +291,6 @@ class ElasticSearchEngine(object):
self.job_manager = None
self.action_manager = None
self.session_manager = None
# register elasticsearch opts
CONF.register_opts(self._OPTS, group=backend)
self.conf = dict(CONF.get(backend))
self.backend = backend
self._validate_opts()
@@ -332,9 +302,6 @@ class ElasticSearchEngine(object):
raise Exception("File not found: ca_certs file ({0}) not "
"found".format(self.conf.get('ca_certs')))

def get_opts(self):
return self._OPTS

def init(self, index='freezer', **kwargs):
self.index = index
self.es = elasticsearch.Elasticsearch(**kwargs)

+ 0
- 3
freezer_api/storage/elasticv2.py View File

@@ -341,9 +341,6 @@ class ElasticSearchEngineV2(object):
raise Exception("File not found: ca_certs file ({0}) not "
"found".format(self.conf.get('ca_certs')))

def get_opts(self):
return self._OPTS

def init(self, index='freezer', **kwargs):
self.index = index
self.es = elasticsearch.Elasticsearch(**kwargs)

+ 40
- 12
freezer_api/tests/unit/test_elastic.py View File

@@ -21,11 +21,15 @@ import unittest
import elasticsearch
import mock
from mock import patch
from oslo_config import cfg

from freezer_api.common import exceptions
from freezer_api.db.elasticsearch.driver import ElasticSearchDB
from freezer_api.storage import elastic
from freezer_api.tests.unit import common

CONF = cfg.CONF


class TypeManager(unittest.TestCase):
def setUp(self):
@@ -651,13 +655,17 @@ class SessionTypeManager(unittest.TestCase):
session_update_doc={'status': 'sleepy'})


class TestElasticSearchEngine_backup(unittest.TestCase):
class TestElasticSearchEngine_backup(unittest.TestCase, ElasticSearchDB):
@patch('freezer_api.storage.elastic.logging')
@patch('freezer_api.storage.elastic.elasticsearch')
def setUp(self, mock_logging, mock_elasticsearch):
backend = 'elasticsearch'
grp = cfg.OptGroup(backend)
CONF.register_group(grp)
CONF.register_opts(self._ES_OPTS, group=backend)
mock_elasticsearch.Elasticsearch.return_value = mock.Mock()
kwargs = {'hosts': 'http://elasticservaddr:1997'}
self.eng = elastic.ElasticSearchEngine(backend='elasticsearch')
self.eng = elastic.ElasticSearchEngine(backend=backend)
self.eng.init(index='freezer', **kwargs)
self.eng.backup_manager = mock.Mock()

@@ -758,13 +766,17 @@ class TestElasticSearchEngine_backup(unittest.TestCase):
backup_id=common.fake_data_0_backup_id)


class TestElasticSearchEngine_client(unittest.TestCase):
class TestElasticSearchEngine_client(unittest.TestCase, ElasticSearchDB):
@patch('freezer_api.storage.elastic.logging')
@patch('freezer_api.storage.elastic.elasticsearch')
def setUp(self, mock_logging, mock_elasticsearch):
backend = 'elasticsearch'
grp = cfg.OptGroup(backend)
CONF.register_group(grp)
CONF.register_opts(self._ES_OPTS, group=backend)
mock_elasticsearch.Elasticsearch.return_value = mock.Mock()
kwargs = {'hosts': 'http://elasticservaddr:1997'}
self.eng = elastic.ElasticSearchEngine(backend="elasticsearch")
self.eng = elastic.ElasticSearchEngine(backend=backend)
self.eng.init(index='freezer', **kwargs)
self.eng.client_manager = mock.Mock()

@@ -871,13 +883,17 @@ class TestElasticSearchEngine_client(unittest.TestCase):
client_id=common.fake_client_info_0['client_id'])


class TestElasticSearchEngine_job(unittest.TestCase):
class TestElasticSearchEngine_job(unittest.TestCase, ElasticSearchDB):
@patch('freezer_api.storage.elastic.logging')
@patch('freezer_api.storage.elastic.elasticsearch')
def setUp(self, mock_elasticsearch, mock_logging):
backend = 'elasticsearch'
grp = cfg.OptGroup(backend)
CONF.register_group(grp)
CONF.register_opts(self._ES_OPTS, group=backend)
mock_elasticsearch.Elasticsearch.return_value = mock.Mock()
kwargs = {'hosts': 'http://elasticservaddr:1997'}
self.eng = elastic.ElasticSearchEngine(backend="elasticsearch")
self.eng = elastic.ElasticSearchEngine(backend=backend)
self.eng.init(index='freezer', **kwargs)
self.eng.job_manager = mock.Mock()

@@ -1019,13 +1035,17 @@ class TestElasticSearchEngine_job(unittest.TestCase):
self.assertEqual(3, res)


class TestElasticSearchEngine_action(unittest.TestCase):
class TestElasticSearchEngine_action(unittest.TestCase, ElasticSearchDB):
@patch('freezer_api.storage.elastic.logging')
@patch('freezer_api.storage.elastic.elasticsearch')
def setUp(self, mock_elasticsearch, mock_logging):
backend = 'elasticsearch'
grp = cfg.OptGroup(backend)
CONF.register_group(grp)
CONF.register_opts(self._ES_OPTS, group=backend)
mock_elasticsearch.Elasticsearch.return_value = mock.Mock()
kwargs = {'hosts': 'http://elasticservaddr:1997'}
self.eng = elastic.ElasticSearchEngine(backend="elasticsearch")
self.eng = elastic.ElasticSearchEngine(backend=backend)
self.eng.init(index='freezer', **kwargs)
self.eng.action_manager = mock.Mock()

@@ -1176,13 +1196,17 @@ class TestElasticSearchEngine_action(unittest.TestCase):
# self.assertEqual(res, 3)


class TestElasticSearchEngine_session(unittest.TestCase):
class TestElasticSearchEngine_session(unittest.TestCase, ElasticSearchDB):
@patch('freezer_api.storage.elastic.logging')
@patch('freezer_api.storage.elastic.elasticsearch')
def setUp(self, mock_elasticsearch, mock_logging):
backend = 'elasticsearch'
grp = cfg.OptGroup(backend)
CONF.register_group(grp)
CONF.register_opts(self._ES_OPTS, group=backend)
mock_elasticsearch.Elasticsearch.return_value = mock.Mock()
kwargs = {'hosts': 'http://elasticservaddr:1997'}
self.eng = elastic.ElasticSearchEngine(backend="elasticsearch")
self.eng = elastic.ElasticSearchEngine(backend=backend)
self.eng.init(index='freezer', **kwargs)
self.eng.session_manager = mock.Mock()

@@ -1333,14 +1357,18 @@ class TestElasticSearchEngine_session(unittest.TestCase):
self.assertEqual(3, res)


class TestElasticSearchEngine(unittest.TestCase):
class TestElasticSearchEngine(unittest.TestCase, ElasticSearchDB):

@patch('freezer_api.storage.elastic.logging')
@patch('freezer_api.storage.elastic.elasticsearch')
def setUp(self, mock_elasticsearch, mock_logging):
backend = 'elasticsearch'
grp = cfg.OptGroup(backend)
CONF.register_group(grp)
CONF.register_opts(self._ES_OPTS, group=backend)
mock_elasticsearch.Elasticsearch.return_value = mock.Mock()
kwargs = {'hosts': 'http://elasticservaddr:1997'}
self.eng = elastic.ElasticSearchEngine(backend="elasticsearch")
self.eng = elastic.ElasticSearchEngine(backend=backend)
self.eng.init(index='freezer', **kwargs)

def test_raise_validate_opts_when_ca_certs_file_not_exist(self):

Loading…
Cancel
Save