Check for relevant environment variables
Currently, register-nodes does not check for environment variables on starting, which leads to keystoneclient printing error messages. Check and raise an exception before we start registering nodes. Change-Id: Id838fe47ff117182eddc69344e018b67aee44419
This commit is contained in:
parent
bdf8caa913
commit
63a695770d
@ -18,6 +18,7 @@ import simplejson
|
||||
import textwrap
|
||||
|
||||
from os_cloud_config import nodes
|
||||
from os_cloud_config import utils
|
||||
|
||||
|
||||
def parse_args():
|
||||
@ -52,6 +53,7 @@ def main():
|
||||
try:
|
||||
with open(args.nodes, 'r') as node_file:
|
||||
nodes_list = simplejson.load(node_file)
|
||||
utils._ensure_environment()
|
||||
|
||||
# TODO(StevenK): Filter out registered nodes.
|
||||
nodes.register_all_nodes(args.service_host, nodes_list)
|
||||
|
@ -25,6 +25,8 @@ from os_cloud_config.tests import base
|
||||
class RegisterNodesTest(base.TestCase):
|
||||
|
||||
@mock.patch('os_cloud_config.nodes.register_all_nodes')
|
||||
@mock.patch.dict('os.environ', {'OS_USERNAME': 'a', 'OS_PASSWORD': 'a',
|
||||
'OS_TENANT_NAME': 'a', 'OS_AUTH_URL': 'a'})
|
||||
@mock.patch.object(sys, 'argv', ['register-nodes', '--service-host',
|
||||
'seed', '--nodes'])
|
||||
def test_with_arguments(self, register_mock):
|
||||
@ -37,6 +39,8 @@ class RegisterNodesTest(base.TestCase):
|
||||
self.assertEqual(0, return_code)
|
||||
|
||||
@mock.patch('os_cloud_config.nodes.register_all_nodes')
|
||||
@mock.patch.dict('os.environ', {'OS_USERNAME': 'a', 'OS_PASSWORD': 'a',
|
||||
'OS_TENANT_NAME': 'a', 'OS_AUTH_URL': 'a'})
|
||||
@mock.patch.object(sys, 'argv', ['register-nodes', '--service-host',
|
||||
'seed', '--nodes'])
|
||||
def test_with_exception(self, register_mock):
|
||||
|
@ -48,3 +48,7 @@ class CloudConfigException(Exception):
|
||||
message = self.msg_fmt
|
||||
|
||||
super(CloudConfigException, self).__init__(message)
|
||||
|
||||
|
||||
class MissingEnvironment(CloudConfigException):
|
||||
message = "Required environment variables are not set."
|
||||
|
45
os_cloud_config/tests/test_utils.py
Normal file
45
os_cloud_config/tests/test_utils.py
Normal file
@ -0,0 +1,45 @@
|
||||
# Copyright (c) 2014 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.
|
||||
|
||||
import mock
|
||||
import testtools
|
||||
|
||||
from os_cloud_config import exception
|
||||
from os_cloud_config.tests import base
|
||||
from os_cloud_config import utils
|
||||
|
||||
|
||||
class UtilsTest(base.TestCase):
|
||||
|
||||
@mock.patch.dict('os.environ', {})
|
||||
def test_ensure_environment_missing_all(self):
|
||||
message = ("OS_AUTH_URL, OS_PASSWORD, OS_TENANT_NAME, OS_USERNAME "
|
||||
"environment variables are required to be set.")
|
||||
with testtools.ExpectedException(exception.MissingEnvironment,
|
||||
message):
|
||||
utils._ensure_environment()
|
||||
|
||||
@mock.patch.dict('os.environ', {'OS_PASSWORD': 'a', 'OS_AUTH_URL': 'a',
|
||||
'OS_TENANT_NAME': 'a'})
|
||||
def test_ensure_environment_missing_username(self):
|
||||
message = "OS_USERNAME environment variable is required to be set."
|
||||
with testtools.ExpectedException(exception.MissingEnvironment,
|
||||
message):
|
||||
utils._ensure_environment()
|
||||
|
||||
@mock.patch.dict('os.environ', {'OS_PASSWORD': 'a', 'OS_AUTH_URL': 'a',
|
||||
'OS_TENANT_NAME': 'a', 'OS_USERNAME': 'a'})
|
||||
def test_ensure_environment_missing_none(self):
|
||||
self.assertIs(None, utils._ensure_environment())
|
30
os_cloud_config/utils.py
Normal file
30
os_cloud_config/utils.py
Normal file
@ -0,0 +1,30 @@
|
||||
# Copyright (c) 2014 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.
|
||||
|
||||
import os
|
||||
|
||||
from os_cloud_config import exception
|
||||
|
||||
|
||||
def _ensure_environment():
|
||||
environ = ("OS_USERNAME", "OS_PASSWORD", "OS_AUTH_URL", "OS_TENANT_NAME")
|
||||
missing = set(environ).difference(os.environ)
|
||||
plural = "s are"
|
||||
if missing:
|
||||
if len(missing) == 1:
|
||||
plural = " is"
|
||||
message = ("%s environment variable%s required to be set." % (
|
||||
", ".join(sorted(missing)), plural))
|
||||
raise exception.MissingEnvironment(message)
|
Loading…
x
Reference in New Issue
Block a user