Pull out redis validation into validator class

This commit is contained in:
Joseph Schorr 2017-01-31 13:47:49 -05:00
parent f933b3e295
commit b2afe68632
6 changed files with 45 additions and 22 deletions

View file

@ -68,3 +68,4 @@ trollius
tzlocal
xhtml2pdf
recaptcha2
mockredispy

View file

@ -52,6 +52,7 @@ marisa-trie==0.7.2
MarkupSafe==0.23
mixpanel==4.3.1
mock==2.0.0
mockredispy==2.9.3
-e git+https://github.com/coreos/mockldap.git@59a46efbe8c7cd8146a87a7c4f2b09746b953e11#egg=mockldap
monotonic==1.2
moto==0.4.25

View file

@ -31,17 +31,6 @@ class TestValidateConfig(unittest.TestCase):
config['TESTING'] = True
VALIDATORS[service](config, user, password)
def test_validate_redis(self):
with self.assertRaisesRegexp(ConfigValidationException, 'Missing redis hostname'):
self.validate('redis', {})
with self.assertRaises(redis.ConnectionError):
self.validate('redis', {
'BUILDLOGS_REDIS': {
'host': 'somehost',
},
})
def test_validate_mail(self):
# Skip mail.
self.validated.add('mail')

View file

@ -29,7 +29,9 @@ from util.secscan.api import SecurityScannerAPI
from util.registry.torrent import torrent_jwt
from util.security.signing import SIGNING_ENGINES
from util.security.ssl import load_certificate, CertInvalidException, KeyInvalidException
from util.config.validators.database import DatabaseValidator
from util.config.validators.buildlogredis import RedisValidator
logger = logging.getLogger(__name__)
@ -93,16 +95,6 @@ def _validate_database(config, user_obj, _):
raise ex
def _validate_redis(config, user_obj, _):
""" Validates connecting to redis. """
redis_config = config.get('BUILDLOGS_REDIS', {})
if not 'host' in redis_config:
raise ConfigValidationException('Missing redis hostname')
client = redis.StrictRedis(socket_connect_timeout=5, **redis_config)
client.ping()
def _validate_registry_storage(config, user_obj, _):
""" Validates registry storage. """
replication_enabled = config.get('FEATURE_STORAGE_REPLICATION', False)
@ -524,7 +516,7 @@ def _validate_bittorrent(config, user_obj, _):
VALIDATORS = {
DatabaseValidator.name: DatabaseValidator.validate,
'redis': _validate_redis,
RedisValidator.name: RedisValidator.validate,
'registry-storage': _validate_registry_storage,
'mail': _validate_mailing,
'github-login': _validate_github('GITHUB_LOGIN_CONFIG'),

View file

@ -0,0 +1,16 @@
import redis
from util.config.validators import BaseValidator, ConfigValidationException
class RedisValidator(BaseValidator):
name = "redis"
@classmethod
def validate(cls, config, user, user_password):
""" Validates connecting to redis. """
redis_config = config.get('BUILDLOGS_REDIS', {})
if not 'host' in redis_config:
raise ConfigValidationException('Missing redis hostname')
client = redis.StrictRedis(socket_connect_timeout=5, **redis_config)
client.ping()

View file

@ -0,0 +1,24 @@
import pytest
import redis
from mock import patch
from mockredis import mock_strict_redis_client
from util.config.validators import ConfigValidationException
from util.config.validators.buildlogredis import RedisValidator
@pytest.mark.parametrize('unvalidated_config,user,user_password,use_mock,expected', [
({}, None, None, False, ConfigValidationException),
({'BUILDLOGS_REDIS': {}}, None, None, False, ConfigValidationException),
({'BUILDLOGS_REDIS': {'host': 'somehost'}}, None, None, False, redis.ConnectionError),
({'BUILDLOGS_REDIS': {'host': 'localhost'}}, None, None, True, None),
])
def test_validate_redis(unvalidated_config, user, user_password, use_mock, expected):
with patch('redis.StrictRedis' if use_mock else 'redis.None', mock_strict_redis_client):
validator = RedisValidator()
if expected is not None:
with pytest.raises(expected):
validator.validate(unvalidated_config, user, user_password)
else:
validator.validate(unvalidated_config, user, user_password)