From b2afe686320be2c0df13ecf4b8af7067c6c70d6f Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Tue, 31 Jan 2017 13:47:49 -0500 Subject: [PATCH] Pull out redis validation into validator class --- requirements-nover.txt | 1 + requirements.txt | 1 + test/test_validate_config.py | 11 --------- util/config/validator.py | 14 +++-------- util/config/validators/buildlogredis.py | 16 +++++++++++++ .../validators/test/test_buildlogredis.py | 24 +++++++++++++++++++ 6 files changed, 45 insertions(+), 22 deletions(-) create mode 100644 util/config/validators/buildlogredis.py create mode 100644 util/config/validators/test/test_buildlogredis.py diff --git a/requirements-nover.txt b/requirements-nover.txt index 1d28e929a..7fdef7c77 100644 --- a/requirements-nover.txt +++ b/requirements-nover.txt @@ -68,3 +68,4 @@ trollius tzlocal xhtml2pdf recaptcha2 +mockredispy diff --git a/requirements.txt b/requirements.txt index 0076672fa..eaf37efea 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 diff --git a/test/test_validate_config.py b/test/test_validate_config.py index 20d8b3d28..1c4dbdc89 100644 --- a/test/test_validate_config.py +++ b/test/test_validate_config.py @@ -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') diff --git a/util/config/validator.py b/util/config/validator.py index e37575400..860e67254 100644 --- a/util/config/validator.py +++ b/util/config/validator.py @@ -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'), diff --git a/util/config/validators/buildlogredis.py b/util/config/validators/buildlogredis.py new file mode 100644 index 000000000..92909dbf4 --- /dev/null +++ b/util/config/validators/buildlogredis.py @@ -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() diff --git a/util/config/validators/test/test_buildlogredis.py b/util/config/validators/test/test_buildlogredis.py new file mode 100644 index 000000000..9936527dc --- /dev/null +++ b/util/config/validators/test/test_buildlogredis.py @@ -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)