Fix auto-detect crash on monasca-setup when using oslo.config

monasca_setup.detection.utils.load_oslo_configuration() should ignore
options to `from_cmd` that is not oslo.config built-in options. We are
only interested in the options --config-file and --config-dir.

Change-Id: I72dc53d8ee6dc7b0784e6931a19c461cdb322851
Story: 2001303
Task: 5854
This commit is contained in:
Tobias Johansson
2017-11-30 18:01:51 +01:00
parent 530599c705
commit 089d8c4e42
2 changed files with 92 additions and 16 deletions

View File

@@ -1,8 +1,10 @@
# (C) Copyright 2015-2016 Hewlett Packard Enterprise Development LP # (C) Copyright 2015-2016 Hewlett Packard Enterprise Development LP
# Copyright 2017 SUSE Linux GmbH # Copyright 2017 SUSE Linux GmbH
# Copyright 2017 OP5 AB
""" Util functions to assist in detection. """ Util functions to assist in detection.
""" """
import argparse
import logging import logging
import os import os
import pwd import pwd
@@ -133,7 +135,6 @@ def get_agent_username():
return _DETECTED_AGENT_USER return _DETECTED_AGENT_USER
# NOTE(trebskit) a little poetry never hurt anyone before...right ?
def load_oslo_configuration(from_cmd, in_project, def load_oslo_configuration(from_cmd, in_project,
for_opts, of_prog=None): for_opts, of_prog=None):
"""Loads configuration of an OpenStack project. """Loads configuration of an OpenStack project.
@@ -183,7 +184,19 @@ def load_oslo_configuration(from_cmd, in_project,
# /usr/bin/python, /usr/bin/python3 # /usr/bin/python, /usr/bin/python3
# and next actual binary of the program # and next actual binary of the program
# /usr/local/bin/nova-compute # /usr/local/bin/nova-compute
args = from_cmd[2:] # NOTE(tobiajo) Just keep built-in options for oslo.config
args = []
parser = argparse.ArgumentParser()
parser.add_argument('--config-file')
parser.add_argument('--config-dir')
namespace, _ = parser.parse_known_args(from_cmd[2:])
if namespace.config_file:
args.append('--config-file')
args.append(namespace.config_file)
if namespace.config_dir:
args.append('--config-dir')
args.append(namespace.config_dir)
conf_holder( conf_holder(
args=args, args=args,
project=in_project, project=in_project,

View File

@@ -1,4 +1,5 @@
# Copyright 2017 FUJITSU LIMITED # Copyright 2017 FUJITSU LIMITED
# Copyright 2017 OP5 AB
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may # 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 # not use this file except in compliance with the License. You may obtain
@@ -13,6 +14,7 @@
# under the License. # under the License.
import mock import mock
import os
from oslotest import base from oslotest import base
from oslo_config import cfg from oslo_config import cfg
@@ -40,8 +42,8 @@ class TestDetectionUtilsOsloConf(base.BaseTestCase):
opts = [ opts = [
{'opt': cfg.StrOpt('region_name')} {'opt': cfg.StrOpt('region_name')}
] ]
args = ['python', 'foo-api', '--config-dir', '/foo/bar', args = ['python', 'foo-api', '--config-file', '/foo/bar/file',
'--config-dir', '/tmp/foo'] '--config-dir', '/foo/bar']
self._run_load_oslo_test(co, opts, args) self._run_load_oslo_test(co, opts, args)
@@ -67,14 +69,18 @@ class TestDetectionUtilsOsloConf(base.BaseTestCase):
# test ensures that each instance created via load_oslo_configuration # test ensures that each instance created via load_oslo_configuration
# contains different values of the same opts # contains different values of the same opts
cmd_1 = ['python', 'test', '--foo', '1'] file_1, file_2 = self.create_tempfiles([('', ''), ('', '')])
cmd_2 = ['python', 'test', '--foo', '2'] base_dir = os.path.dirname(file_1)
os.makedirs(base_dir + '/1')
os.makedirs(base_dir + '/2')
cmd_1 = ['python', 'test', '--config-dir', base_dir + '/1']
cmd_2 = ['python', 'test', '--config-dir', base_dir + '/2']
cmd_3 = ['python', 'test', '--config-file', file_1]
cmd_4 = ['python', 'test', '--config-file', file_2]
opts = [ opts = [
{ {'opt': cfg.StrOpt('region_name')}
'opt': cfg.IntOpt(name='foo', default=-1),
'cli': True
}
] ]
cfg_1 = utils.load_oslo_configuration( cfg_1 = utils.load_oslo_configuration(
@@ -90,19 +96,76 @@ class TestDetectionUtilsOsloConf(base.BaseTestCase):
for_opts=opts for_opts=opts
) )
cfg_3 = utils.load_oslo_configuration( cfg_3 = utils.load_oslo_configuration(
from_cmd=[], from_cmd=cmd_3,
in_project=self.PROJECT,
of_prog=self.PROG,
for_opts=opts
)
cfg_4 = utils.load_oslo_configuration(
from_cmd=cmd_4,
in_project=self.PROJECT, in_project=self.PROJECT,
of_prog=self.PROG, of_prog=self.PROG,
for_opts=opts for_opts=opts
) )
self.assertIsNot(cfg_1, cfg_2) self.assertIsNot(cfg_1, cfg_2)
self.assertIsNot(cfg_2, cfg_3) self.assertIsNot(cfg_3, cfg_4)
self.assertIsNot(cfg_1, cfg_3) self.assertNotEqual(cfg_1, cfg_2)
self.assertNotEqual(cfg_3, cfg_4)
self.assertNotEqual(cfg_1.foo, cfg_2.foo) def test_just_keep_built_in_options(self):
self.assertNotEqual(cfg_2.foo, cfg_3.foo) # test ensures that non built-in oslo.config options to
self.assertNotEqual(cfg_1.foo, cfg_3.foo) # load_oslo_configuration is skipped
cmd_1 = ['python', 'test']
cmd_2 = ['python', 'test', '--log-file', '/var/log/test/test.log']
opts = [
{'opt': cfg.StrOpt('region_name')}
]
cfg_1 = utils.load_oslo_configuration(
from_cmd=cmd_1,
in_project=self.PROJECT,
of_prog=self.PROG,
for_opts=opts
)
cfg_2 = utils.load_oslo_configuration(
from_cmd=cmd_2,
in_project=self.PROJECT,
of_prog=self.PROG,
for_opts=opts
)
self.assertIsNot(cfg_1, cfg_2)
self.assertEqual(cfg_1, cfg_2)
def test_parsing_different_command_styles(self):
# test ensures that options sent to load_oslo_configuration with
# different command styles generates the same output
cmd_1 = ['python', 'test', '--config-dir', '.']
cmd_2 = ['python', 'test', '--config-dir=.']
opts = [
{'opt': cfg.StrOpt('region_name')}
]
cfg_1 = utils.load_oslo_configuration(
from_cmd=cmd_1,
in_project=self.PROJECT,
of_prog=self.PROG,
for_opts=opts
)
cfg_2 = utils.load_oslo_configuration(
from_cmd=cmd_2,
in_project=self.PROJECT,
of_prog=self.PROG,
for_opts=opts
)
self.assertIsNot(cfg_1, cfg_2)
self.assertEqual(cfg_1, cfg_2)
def _run_load_oslo_test(self, config_opts, opts, args): def _run_load_oslo_test(self, config_opts, opts, args):
actual_args = args[2:] actual_args = args[2:]