From e6cc50bcff2605963f369cedad4a20913578c734 Mon Sep 17 00:00:00 2001 From: Dmitry Bogun Date: Fri, 9 Dec 2016 12:53:16 +0200 Subject: [PATCH] Remove interference of config objects Till now global config object was used. So we and project owns functional test fill same config object. This can lead to options/groups overlaps (two separate package add option with same name). Also second "read config" call will reset config and remove loaded options. "Read config" call happenes twice because we and package that own functional test both make this call. That why we must use separate config object. Change-Id: I7bc4ec995317fb6454deb19e75b82793e3d027a4 --- ramdisk_func_test/base.py | 33 ++-------------- ramdisk_func_test/conf.py | 20 ++++++++++ ramdisk_func_test/environment.py | 56 +++++++++++++++++++-------- ramdisk_func_test/network.py | 12 +++--- ramdisk_func_test/node.py | 10 ++--- ramdisk_func_test/utils.py | 12 +++--- ramdisk_func_test/webserver/server.py | 5 +-- 7 files changed, 83 insertions(+), 65 deletions(-) create mode 100644 ramdisk_func_test/conf.py diff --git a/ramdisk_func_test/base.py b/ramdisk_func_test/base.py index 27ca5bb..1f63243 100644 --- a/ramdisk_func_test/base.py +++ b/ramdisk_func_test/base.py @@ -16,47 +16,22 @@ import logging import uuid import os -import sys import libvirt import jinja2 from oslo_config import cfg +from ramdisk_func_test import conf import utils -def _setup_config(): - cfg.CONF([], default_config_files=[ - "/etc/ramdisk-func-test/ramdisk-func-test.conf"]) - - -def _setup_loggging(): - for pair in [ - 'paramiko=WARN', - 'ironic.openstack.common=WARN', - ]: - mod, _sep, level_name = pair.partition('=') - logger = logging.getLogger(mod) - # NOTE(AAzza) in python2.6 Logger.setLevel doesn't convert string name - # to integer code. - if sys.version_info < (2, 7): - level = logging.getLevelName(level_name) - logger.setLevel(level) - else: - logger.setLevel(level_name) - - -_setup_config() -_setup_loggging() - -opts = [ +CONF = conf.CONF +CONF.register_opts([ cfg.StrOpt('qemu_url', help='URL of qemu server.', default="qemu:///system"), -] -CONF = cfg.CONF -CONF.register_opts(opts) +]) LOG = logging.getLogger(__name__) diff --git a/ramdisk_func_test/conf.py b/ramdisk_func_test/conf.py new file mode 100644 index 0000000..8c8ee8f --- /dev/null +++ b/ramdisk_func_test/conf.py @@ -0,0 +1,20 @@ +# +# Copyright 2016 Cray Inc., All Rights Reserved +# +# 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. + +from oslo_config import cfg + +PROJECT_NAME = 'ramdisk-func-test' + +CONF = cfg.ConfigOpts() diff --git a/ramdisk_func_test/environment.py b/ramdisk_func_test/environment.py index 03ce2f5..49441c0 100644 --- a/ramdisk_func_test/environment.py +++ b/ramdisk_func_test/environment.py @@ -23,6 +23,7 @@ import sh from oslo_config import cfg +from ramdisk_func_test import conf from ramdisk_func_test import utils from ramdisk_func_test.base import TemplateEngine from ramdisk_func_test.base import ABS_PATH @@ -30,7 +31,8 @@ from ramdisk_func_test.network import Network from ramdisk_func_test.node import Node -opts = [ +CONF = conf.CONF +CONF.register_opts([ cfg.StrOpt('image_build_dir', default="/tmp/rft_image_build", help='A path where images from DIB will be build. Expected ' @@ -50,20 +52,20 @@ opts = [ # NOTE(oberezovskyi): path from Centos 7 taken as default cfg.StrOpt('pxelinux', default='/usr/share/syslinux/pxelinux.0', - help='Path to pxelinux.0 file') -] - -CONF = cfg.CONF -CONF.register_opts(opts) + help='Path to pxelinux.0 file'), + cfg.IntOpt('stub_webserver_port', + default=8011, + help='The port used by stub webserver') +]) CONF.import_opt('ramdisk_func_test_workdir', 'ramdisk_func_test.utils') LOG = logging.getLogger(__name__) class Environment(object): - HTTP_PORT = "8011" + _loaded_config = object() # to fail comparison with None - def __init__(self, node_templates): + def __init__(self, node_templates, config=None): super(Environment, self).__init__() self.templ_eng = TemplateEngine(node_templates) @@ -74,6 +76,8 @@ class Environment(object): self.rsync_dir = None self.image_mount_point = None + self._load_config(config) + def setupclass(self): """Global setup - single for all tests""" self.network = Network(self.templ_eng) @@ -138,7 +142,7 @@ class Environment(object): ramdisk=CONF.ramdisk, deployment_id=self.node.name, api_url="http://{0}:{1}".format(self.network.address, - self.HTTP_PORT) + CONF.stub_webserver_port) ) pxe_path = os.path.join(tftp_root, "pxelinux.cfg") @@ -149,11 +153,10 @@ class Environment(object): with open(conf_path, 'w') as f: f.write(pxe_config) - def _setup_webserver(self, port=HTTP_PORT): - + def _setup_webserver(self): + port = CONF.stub_webserver_port LOG.info("Starting stub webserver (at IP {0} port {1}, path to tenant " - "images folder is '{2}')".format(self.network.address, - port, + "images folder is '{2}')".format(self.network.address, port, self.tenant_images_dir)) # TODO(max_lobur) make webserver singletone @@ -172,11 +175,12 @@ class Environment(object): def get_url_for_stub_image(self): return "http://{0}:{1}/fake".format(self.network.address, - self.HTTP_PORT) + CONF.stub_webserver_port) def _get_swift_tenant_image_url(self, image_name): - return ("http://{0}:{1}/tenant_images/" - "{2}".format(self.network.address, self.HTTP_PORT, image_name)) + return ( + 'http://{0}:{1}/tenant_images/{2}'.format( + self.network.address, CONF.stub_webserver_port, image_name)) def _get_rsync_tenant_image_url(self, image_name): url = "{0}::ironic_rsync/{1}/".format(self.network.address, @@ -247,3 +251,23 @@ class Environment(object): if self.image_mount_point: sh.sudo.umount(self.image_mount_point) sh.rmdir(self.image_mount_point) + + @classmethod + def _load_config(cls, path): + if cls._loaded_config == path: + return + + LOG.debug('Load ramdisk-func-test configuration') + args = {} + if path: + args['default_config_files'] = [path] + conf.CONF([], project=conf.PROJECT_NAME, **args) + + # configure log level for libs we are using + for channel, level in [ + ('paramiko', logging.WARN), + ('ironic.openstack.common', logging.WARN)]: + logger = logging.getLogger(channel) + logger.setLevel(level) + + cls._loaded_config = path diff --git a/ramdisk_func_test/network.py b/ramdisk_func_test/network.py index f7639fa..a8a2cc7 100644 --- a/ramdisk_func_test/network.py +++ b/ramdisk_func_test/network.py @@ -20,12 +20,13 @@ import libvirt from oslo_config import cfg +from ramdisk_func_test import conf from ramdisk_func_test import utils from ramdisk_func_test.base import LibvirtBase -LOG = logging.getLogger(__name__) -opts = [ +CONF = conf.CONF +CONF.register_opts([ cfg.StrOpt('libvirt_net_head_octets', default="192.168", help='Head octets for libvirt network (choose free one).'), @@ -35,12 +36,11 @@ opts = [ cfg.IntOpt('libvirt_net_range_end', default=254, help='Libvirt network DHCP range end.') -] - -CONF = cfg.CONF -CONF.register_opts(opts) +]) CONF.import_opt('ramdisk_func_test_workdir', 'ramdisk_func_test.utils') +LOG = logging.getLogger(__name__) + class Network(LibvirtBase): def __init__(self, templ_engine): diff --git a/ramdisk_func_test/node.py b/ramdisk_func_test/node.py index 9671262..445478d 100644 --- a/ramdisk_func_test/node.py +++ b/ramdisk_func_test/node.py @@ -23,10 +23,13 @@ from lxml import etree from oslo_config import cfg +from ramdisk_func_test import conf from ramdisk_func_test import utils from ramdisk_func_test.base import LibvirtBase -opts = [ + +CONF = conf.CONF +CONF.register_opts([ cfg.IntOpt('node_boot_timeout', help='Time to wait slave node to boot (seconds)', default=360), @@ -34,10 +37,7 @@ opts = [ default='', help='Libvirt machine type (apply if it is not set in ' 'template)'), - -] -CONF = cfg.CONF -CONF.register_opts(opts) +]) CONF.import_opt('ramdisk_func_test_workdir', 'ramdisk_func_test.utils') LOG = logging.getLogger(__name__) diff --git a/ramdisk_func_test/utils.py b/ramdisk_func_test/utils.py index 50e7274..a7cd69a 100644 --- a/ramdisk_func_test/utils.py +++ b/ramdisk_func_test/utils.py @@ -13,27 +13,27 @@ # License for the specific language governing permissions and limitations # under the License. +import ConfigParser import os import logging import shutil import random import socket +from subprocess import check_output from time import time from time import sleep from oslo_config import cfg -from subprocess import check_output -import ConfigParser +from ramdisk_func_test import conf -opts = [ +CONF = conf.CONF +CONF.register_opts([ cfg.StrOpt('ramdisk_func_test_workdir', help='Path where virtualized node disks will be stored.', default="/tmp/ramdisk-func-test/"), -] -CONF = cfg.CONF -CONF.register_opts(opts) +]) LOG = logging.getLogger(__name__) diff --git a/ramdisk_func_test/webserver/server.py b/ramdisk_func_test/webserver/server.py index 3518535..a5744c2 100755 --- a/ramdisk_func_test/webserver/server.py +++ b/ramdisk_func_test/webserver/server.py @@ -24,12 +24,11 @@ import sys import traceback import re -from oslo_config import cfg - +from ramdisk_func_test import conf from ramdisk_func_test.base import ABS_PATH -CONF = cfg.CONF +CONF = conf.CONF LOG = logging.getLogger(__name__) logging.basicConfig(filename='/tmp/mock-web-server.log', level=logging.DEBUG,