Provide context for castellan config validation

When starting the API we validate castellan's config. That bit might
require a context if you have use_barbican_key_manager = true. Adding a
context to keep config checking happy.

Change-Id: I63ea1d801339afb0bf861a0a480c408aaca2e25e
Closes-Bug: #1641897
This commit is contained in:
Mate Lakat 2016-11-15 12:01:16 +01:00
parent a182fea250
commit 07613622d4
2 changed files with 78 additions and 1 deletions

View File

@ -13,6 +13,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import contextlib
import os import os
from oslo_config import cfg from oslo_config import cfg
@ -25,6 +26,7 @@ import stevedore
from sahara.api import acl from sahara.api import acl
from sahara.common import config as common_config from sahara.common import config as common_config
from sahara import config from sahara import config
from sahara import context
from sahara.i18n import _LI from sahara.i18n import _LI
from sahara.plugins import base as plugins_base from sahara.plugins import base as plugins_base
from sahara.service import api from sahara.service import api
@ -77,7 +79,7 @@ def setup_common(possible_topdir, service_name):
# Validate other configurations (that may produce logs) here # Validate other configurations (that may produce logs) here
cinder.validate_config() cinder.validate_config()
castellan.validate_config() validate_castellan_config()
messaging.setup(service_name) messaging.setup(service_name)
@ -86,6 +88,11 @@ def setup_common(possible_topdir, service_name):
LOG.info(_LI('Sahara {service} started').format(service=service_name)) LOG.info(_LI('Sahara {service} started').format(service=service_name))
def validate_castellan_config():
with admin_context():
castellan.validate_config()
def setup_sahara_api(mode): def setup_sahara_api(mode):
ops = _get_ops_driver(mode) ops = _get_ops_driver(mode)
@ -149,3 +156,13 @@ def launch_api_service(launcher, service):
launcher.launch_service(service, workers=CONF.api_workers) launcher.launch_service(service, workers=CONF.api_workers)
service.start() service.start()
launcher.wait() launcher.wait()
@contextlib.contextmanager
def admin_context():
ctx = context.get_admin_context()
context.set_ctx(ctx)
try:
yield
finally:
context.set_ctx(None)

View File

@ -0,0 +1,60 @@
# Copyright (c) 2016 SUSE LINUX Products GmbH
#
# 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.
import mock
import testtools
from sahara import context
from sahara import main
def mock_validate_config():
return mock.patch(
'sahara.service.castellan.config.validate_config')
class SomeException(Exception):
pass
class ValidateCastellanTest(testtools.TestCase):
def test_castellan_validate_config_called(self):
with mock_validate_config() as validate_config:
main.validate_castellan_config()
validate_config.assert_called_once_with()
def test_context_present_when_calling_validate_config(self):
def check_context():
self.assertTrue(context.has_ctx())
with mock_validate_config() as validate_config:
validate_config.side_effect = check_context
main.validate_castellan_config()
def test_context_cleared(self):
with mock_validate_config():
main.validate_castellan_config()
self.assertFalse(context.has_ctx())
def test_context_cleared_in_case_of_exception(self):
with mock_validate_config() as validate_config:
validate_config.side_effect = SomeException
self.assertRaises(SomeException, main.validate_castellan_config)
self.assertFalse(context.has_ctx())