oslo.config-5.2.0/0000775000175100017510000000000013224676374014000 5ustar zuulzuul00000000000000oslo.config-5.2.0/HACKING.rst0000666000175100017510000000017013224676052015567 0ustar zuulzuul00000000000000Style Commandments ================== Read the OpenStack Style Commandments https://docs.openstack.org/hacking/latest/ oslo.config-5.2.0/test-requirements.txt0000666000175100017510000000142713224676052020240 0ustar zuulzuul00000000000000# The order of packages is significant, because pip processes them in the order # of appearance. Changing the order has an impact on the overall integration # process, which may cause wedges in the gate later. hacking!=0.13.0,<0.14,>=0.12.0 # Apache-2.0 fixtures>=3.0.0 # Apache-2.0/BSD testrepository>=0.0.18 # Apache-2.0/BSD testscenarios>=0.4 # Apache-2.0/BSD testtools>=2.2.0 # MIT oslotest>=1.10.0 # Apache-2.0 # when we can require tox>= 1.4, this can go into tox.ini: # [testenv:cover] # deps = {[testenv]deps} coverage coverage!=4.4,>=4.0 # Apache-2.0 # this is required for the docs build jobs sphinx>=1.6.2 # BSD openstackdocstheme>=1.17.0 # Apache-2.0 reno>=2.5.0 # Apache-2.0 # mocking framework mock>=2.0.0 # BSD # Bandit security code scanner bandit>=1.1.0 # Apache-2.0 oslo.config-5.2.0/CONTRIBUTING.rst0000666000175100017510000000104013224676052016427 0ustar zuulzuul00000000000000If you would like to contribute to the development of OpenStack, you must follow the steps in this page: https://docs.openstack.org/infra/manual/developers.html Once those steps have been completed, changes to OpenStack should be submitted for review via the Gerrit tool, following the workflow documented at: https://docs.openstack.org/infra/manual/developers.html#development-workflow Pull requests submitted through GitHub will be ignored. Bugs should be filed on Launchpad, not GitHub: https://bugs.launchpad.net/oslo.config oslo.config-5.2.0/ChangeLog0000664000175100017510000006771113224676374015566 0ustar zuulzuul00000000000000CHANGES ======= 5.2.0 ----- * Fix the invalid links for doc file in oslo.config * sphinxext: Don't sometimes emit trailing newlines * Provide descriptions for choices * Remove -U from pip install * Avoid tox\_install.sh for constraints support * Clean up enforce\_type related test method's name 5.1.0 ----- * Remove setting of version/release from releasenotes * Updated from global requirements * Make help message include choices information for StrOpt 5.0.0 ----- * Remove the parameter enforce\_type from set\_override and set\_default * Correct documentation error in DeprecatedOpt 4.13.2 ------ * Prefer SNAP\_COMMON config files 4.13.1 ------ * Implement OptGroup.\_\_str\_\_ for log messages * Cleanup test-requirements 4.13.0 ------ * Updated from global requirements * Use boolean where expected * Fix sphinx option generation when advanced=True * Updated from global requirements 4.12.0 ------ * Remove redundant colon in doc * Updated from global requirements * Update reno for stable/pike * Updated from global requirements * Empty groupname will be 'DEFAULT' by default * Added exception when namespace option is missing * Fix the incorrect file path 4.11.0 ------ * add rst output mode for config generator * fix formatting of literal default values with spaces 4.10.0 ------ * fix an issue with looking up deprecated option names in code * handle option defaults that are not strings * Fix python 3.6 escape char warnings in strings 4.9.0 ----- * Update URLs in documents according to document migration 4.8.0 ----- * doc: Specify openstackdocs theme * Updated from global requirements * Remove log translations * sphinxext: Use lower logging level * sphinxext: Add arguments for oslo.config:group directive * switch from oslosphinx to openstackdocstheme * import content from the configuration reference guide * update the documentation URL in the readme 4.7.0 ----- * rearrange docs based on new standard layout * Updated from global requirements * Enable some off-by-default checks 4.6.0 ----- * add to group data model to for generator * Add snap package paths to default config dirs * Updated from global requirements * Fix html\_last\_updated\_fmt for Python3 4.5.0 ----- * Don't set enforce\_type if we are using the default 4.4.0 ----- * Rework the 'oslo-config-generator' documentation * sphinxext: Log with 'str', not 'repr' * Fix html\_last\_updated\_fmt for Python3 * Cast generator\_options to dict * Add PyYAML as a requirement 4.3.0 ----- * Updated from global requirements * Machine Readable Sample Config 4.2.0 ----- * Remove dependency oslo.i18n from test-requirements.txt * Updated from global requirements * Updated from global requirements 4.1.1 ----- * Updated from global requirements * Handle multi-line default values in olso-config-generator * Clarify info on a comment 4.1.0 ----- * add deprecation\_reason to log message 4.0.0 ----- * Fix string interpolation in ValueError * Remove usage of enforce\_type=True * Adjust removal\_version for argument enforce\_type * Optimize the link address * Make set\_override/set\_default with enforce\_type=True by default 3.24.0 ------ * sphinxext: Handle unescaped percentage signs * Remove log translations * Add HACKING.rst * Fix conversion to item\_type on \_\_call\_\_ in List type(again) * Use Sphinx 1.5 warning-is-error 3.23.0 ------ * Updated from global requirements * [Fix gate]Update test requirement * Fix up the option docs * Handle both - and \_ forms of deprecated opts * Handle getattrs to ConfigOpts for deprecated opts * Updated from global requirements * pbr.version.VersionInfo needs package name (oslo.xyz and not oslo\_xyz) * Update reno for stable/ocata * Fix typo in generator.rst 3.22.0 ------ * Fix broken links * Updated from global requirements * Remove references to Python 3.4 * fix interpolation of None value * Normalize group sections * config: Pass description and epilog through * Add Constraints support * Replace six.iteritems() with .items() * Fixing HostName and adding support for HostAddress 3.21.0 ------ 3.20.0 ------ * List comprehensions * Add defaults for config-dir * Fix filename in comment * Show team and repo badges on README * Add 'summarize' option * Revert "Fix conversion to item\_type on \_\_call\_\_ in List type" * Fix conversion to item\_type on \_\_call\_\_ in List type * doc: Fix Range type version * Updated from global requirements 3.19.0 ------ * Fixup list types handling tuples * Updated from global requirements * [TrivialFix] Replace 'assertTrue(a in b)' with 'assertIn(a, b)' 3.18.0 ------ * Fix wrong usage of DuplicateOptError * Enable release notes translation * Updated from global requirements * Disable warning for missing oslo.config.opts.defaults * Updated from global requirements * Updated from global requirements * Add doc about config option name and comment in config file * modify the home-page info with the developer documentation * Correct nits in Iedf808 * Updated from global requirements * Fix missing option types to config doc * Add 'schemes' argument to URIOpt * Fix repr for tuples in choices * Add some documentation about option deprecation * test: add enforce\_type test * standardize release note page ordering * Warn user about enforce\_type default change * Ensure test\_config\_dir\_doesnt\_exist() dir doesn't exist * Update reno for stable/newton * Add HostnameOpt and URIOpt types to sphinxext * Add missing exceptions to the documentation * Add IPOpt and PortOpt names for sphinxext * Updated from global requirements * Add Range type 3.17.0 ------ * Updated from global requirements 3.16.0 ------ * Fix: default value of prog should remove extension * Add Port type to allow configuration of a list of tcp/ip ports 3.15.0 ------ * Advanced Option * Add set\_override method test with ListOpt 3.14.0 ------ * disable lazy translation in sphinx extension * Trivial: adjust import order to fit the import order guideline * Make error message more clear * Add min and max values to Float type and Opt * Fix parameters of assertEqual are misplaced * Updated from global requirements * Add max\_length to URIOpt * Remove discover from test-requirements * update docs for sphinxconfiggen * Add URIOpt to doced option types 3.13.0 ------ * Add Python 3.5 classifier and venv * Enabling your project for mutable-config * Add namespace to \_list\_opts() in test * decode subprocess output so doc build works on python3 * Updated from global requirements * Updated from global requirements 3.12.0 ------ * Add URIOpt * Add support for a deprecated version * Updated from global requirements * Support template substitution of DictOpt * fixture: Provide parameter enforce\_type for method config 3.11.0 ------ * Fix typo: 'olso' to 'oslo' * Return [] for .config\_dirs when config files are not parsed * generator: format string default value for List type properly * Updated from global requirements * Required Option * Updated from global requirements * Make sure ConfigType is an abstract class * Added i18n formatting to log messages * Remove duplicated code in method test\_equal of HostnameTypeTests * Incorrect group name when deprecated\_group is not specified * Handle some native python types in config generation 3.10.0 ------ * sphinxconfiggen to generate multiple sample files * Fix typo in sphinxconfiggen docs * Updated from global requirements * fix wrong module name sphinxgenconfig in doc * Update Beta development status from classifiers * Add 'DEPRECATED: ' prefix for deprecated opts * Avoid duplicate sections in generator output * Updated from global requirements * Updated from global requirements * Add \* to .coverage in .gitignore * Add enfore\_type parameter for set\_default, * Don't set html\_last\_updated\_fmt without git * Updated from global requirements * Updated from global requirements * cfg: do not add --version if not version provided * Disallow config option name as same as attribute of ConfigOpts * doc:Log warning when can't get information from git * Explicitly exclude tests from bandit scan * Fix typo error in docstrings of oslo\_config.cfg.py * improve documentation about registering entry points for config generator * Fix isinstance call * Replace deprecated LOG.warn with LOG.warning * Update reno for stable/mitaka * Fallback if git is absent * Move bandit into pep8 * Cleanup open file handles in test cases 3.9.0 ----- * remove redundant call to set explicit target * clear the cache before mutating the config files * Updated from global requirements * Add None-check to find\_file * add support for mutable options in the config generator * add unreleased page to release notes build * add a release note mentioning our use of reno * Add reno for release notes management 3.8.0 ----- * Hooks around mutate\_config\_files * Add hostname config type * Add config\_dirs property with a list of directories * Fix wrong check with non-None value when format group 3.7.0 ----- * add generator hook for apps to update option defaults * Updated from global requirements * refactor generator.\_list\_opts for further enhancement 3.6.0 ----- * Add debtcollector as requirement * always show coverage output from tests * handle group objects in sphinxext * refactor sphinxext and add unit tests * have show-options load the generator config file * support multiple namespaces in sphinxext * refactor sphinxext to make reformatting easier * refactor generator closures to private methods * Updated from global requirements * remove specially attribute handling from \_Namespace 3.5.0 ----- * Fix unrecognized subparser args * Add test for BoolOpt with no default * Fix bandit entry in tox.ini * Removal of bandit.yaml * Log mutated options at INFO * Fix version 'mutable' was added * Fix PortOpt versionchanged * treat option group names as section headers when documenting them * Fix MultiConfigParser removal version * Retain correct order of defined config options 3.4.0 ----- * Do not fail on certain config option help strings * Make list\_all\_sections return a list * Report mutated options * Warn if immutable options change * Add 'mutable' param to Opt * Fake attrs on \_Namespace * Make \_Namespace the value-holder * Updated from global requirements * enhance sphinx integration to allow links to option definitions 3.3.0 ----- * Remove argparse from requirements * Updated from global requirements * replace reference to default value in option help text in docs * add built-in options to auto-generated documentation * register the built-in options for the config generator to find * Add reST work label for configuration options * Allow port number with 0 * Replace deprecated library function os.popen() with subprocess * Fix can't generate config sample with non-None value 3.2.1 ----- * Fixed deprecated\_for\_removal warning generation * Module index grouping * Docs class links * generator: ignore doubles * Numerous corrections to the docstrings * Make oslo-config-generator fail gracefully when no arguments * Replace assertEqual(\*, None) with assertIsNone in tests * Python 3 deprecated the logger.warn method in favor of warning * Replace assertEqual(None,\*) with assertIsNone(\*) * Cleanly handle an undefined attribute reference * Replace assertEqual with assertTrue and assertFalse * assertIsNone(val) instead of assertEqual(None,val) 3.2.0 ----- * Capitalize IP * Add support for a deprecation reason * Clarify option types documentation * Correct warnings during doc build * Remove warning for option type not a ConfigType * Removes MANIFEST.in as it is not needed explicitely by PBR * Correct the docstring parameters for PortOpt * Make PortOpt accept choices parameter * Test equality of types.String choices as a set * Add new parameter \`choices\` to Integer type 3.1.0 ----- * [fix-compat] Tolerate non-string defaults in String type * [fix-compat] Support older use case for Opt type * Handle None section name in MultiOptConfigParser#get * Update the list\_opts documentation for new api * Remove duplicated profiles section from bandit.yaml * Revert "Add 'mutable' param to Opt" * Drop python 2.6 support * Update docstrings in cfg.py * Add 'mutable' param to Opt * Allow method set\_override with parameter override=None for all types 3.0.0 ----- * Move type formatting from generator to type * Add a bandit target to tox.ini * Updated from global requirements * Allow naming of custom types * Remove python 2.6 classifier * improve wording of comment in previous commit * Remove 'raise' in \_get * Remove python 2.6 and cleanup tox.ini * Add DEFAULT group in NoSuchOptError when group=None * Adjust docstring of module cfg 2.7.0 ----- * Add help text generation for OptGroups * Add max length check to StrOpt * Remove "Kept for backward-compatibility" comment * Fix StrOpt to allow setting quotes and regex * Improve default type checking * Use versionadded and versionchanged in doc * update doc to use new type PortOpt * Fix versionchanged * Raise exception when wrong type is used as default 2.6.0 ----- * Add PortOpt for integer with range 1 to 65535 2.5.0 ----- * Fix typos in cfg.py * Case insensitiveness when comparing choices values * Add missing tests and generator code for IPOpt * Fix coverage configuration and execution * Add item\_type and bounds to ListOpt * Fix bad exception for StrOpt with invalid choice value * fix literal rendering in dictionary options docs * Fix home-page value in setup.cfg with Oslo wiki * docs - Set pbr 'warnerrors' option for doc build * Include changelog/history in docs * Document DictOpt class * Correct the comment of \_\_call\_\_ method * Updated from global requirements * Demonstrate that MultiStr values from multiple sources are merged 2.4.0 ----- * Updated from global requirements * Make Integer type class honor zero for min/max 2.3.0 ----- * Add sphinx extension to embed pretty descriptions of options * Add sphinx extension to build sample config * Updated from global requirements 2.2.0 ----- * Updated from global requirements * preserve preformatted help text 2.1.0 ----- * Updated from global requirements * Add regex validation to String type 2.0.0 ----- * Add enforce\_type option when setting an override * Updated from global requirements * Add \*.egg\* to .gitignore * Updated from global requirements * Remove oslo namespace package 1.15.0 ------ * Fix use of mock for 1.1.0 * Updated from global requirements * Expose min and max to IntOpt * Don't convert ValueError into NoSuchOptError in ConfigOpts 1.14.0 ------ * Updated from global requirements * Add FAQ entry for why we do not treat config options as API * Add set\_default() method to fixture * fix fixture tests for fixtures 1.3.x * Add tox target to find missing requirements 1.13.0 ------ * Do not use oslo namespace for oslo.i18n lib * Fixes example for cfg * Updated from global requirements * Fix sorting of optional arguments * Switch badges from 'pypip.in' to 'shields.io' * Updated from global requirements 1.12.1 ------ * Fix sorting issue in python 3 * Fix typo in Opt class doc string 1.12.0 ------ * Remove pbr requirement * Document properties as properties * make registering a cli opt on a filter object work * Remove outdated release notes * cfg: support interpolation of config option from other groups * Advertise support for Python3.4 / Remove support for 3.3 * Do not sync run\_cross\_tests.sh * Updated from global requirements * Fix issue when generating code with cfg.OptGroup * Option name missing from ConfigFileValueError * Remove run\_cross\_tests.sh * Enforce alphabetic loading order for --config-dir contents 1.11.0 ------ * Uncap library requirements for liberty * Allow loading custom config files into Config Fixture 1.10.0 ------ * Added Raw Value Loading to Test Fixture * Add pypi download + version badges * Update hacking to kilo level * Fix logging of deprecated opts with dest override * Add deprecated\_for\_removal support to the config generator * Generate docs for constructor * Fix doc build warning * Updated from global requirements 1.9.3 ----- * Switch to non-namespaced module imports 1.9.2 ----- * print better message when choices has an empty string * None in config choices breaks oslo-config-generator 1.9.1 ----- * Generate help text indicating possible values * Expand and clean up documentation for the config generator * Add FAQ section to documentation 1.9.0 ----- * fix bug link in readme * Add ability to deprecate opts for removal * Typo in StrOpt docstring: Integer to String 1.8.0 ----- * Add exception handling for entry points 1.7.0 ----- * Updated from global requirements * Add expose\_opt to CfgFilter * Log a warning when deprecated opts are used * Do not import our namespace package * Fixes deprecation warning for oslo.config in cfg.py 1.6.1 ----- * Clear up MultiStrOpt and related documentation * Add a list\_all\_sections method to ConfigOpts * Better check for integer range to account for 0 * Reword DeprecatedOpt docstring * Support i18n messages in config generator * Fix of wrong cli opts unregistration 1.6.0 ----- * Set the version string * Stop sorting options on output * Move files out of the namespace package * Workflow documentation is now in infra-manual * Fix wrong order of positional args in cli * add tests coverage for an oslo.messaging use case 1.5.0 ----- * Refactored help string generation * Updated from global requirements * Updated from global requirements * Fix class constant indentation * Updated from global requirements * Activate pep8 check that \_ is imported * Updated from global requirements * Updated from global requirements * Add pbr to installation requirements * Updated from global requirements * Add some guidance for group names * delay formatting debug log message * Check config default value is correct type * Report permission denied when parsing config * Fix docs example using generator config files * Updated from global requirements * do not use colons in section titles * Stop using intersphinx 1.4.0 ----- * Looks for variable subtitution in the same group * Fixed typo in docstring for \_get\_config\_dirs * Update contributing instructions 1.4.0.0a4 --------- * Updated from global requirements * Work toward Python 3.4 support and testing * warn against sorting requirements * Explain groups for DeprecatedOpts * Add missing newline to stderr output when argument value is wrong * Log a fixed length string of asterisks for obfuscation * Added link to bug tracker and documentation in oslo.config readme * Bump hacking to version 0.9.2 1.4.0.0a3 --------- * Update 1.4.0.0a2 release notes 1.4.0.0a2 --------- * Changes imports order to pass H305, enables check * generator: tweak how MultiStrOpt defaults are handled * Add release notes for 1.3.0 and 1.4.0.0a1/2 * Hook up config fixture docs * Hook IPOpt class into the docs * Replaced 'e.g.' with 'for example' * Add cfgfilter.ConfigFilter * generator: remove the default sanitizer feature * cfg,generator: add Opt.sample\_default attribute * generator: refactor MultiStrOpt handling * Fix flaws in methods' docstring * Introduce Opts for IP addresses * Refactors \_Namespace to make the code clearer * Add oslo-config-generator * Add CLI option support to config fixture 1.4.0.0a1 --------- * Updated from global requirements * Remove print statement from types.Dict * Add warning about interpolating values from groups * Add more tests for positional CLI opts * Add test case for hyphenated option names * Fixes an issue validating max integer values * Bump hacking to 0.9.x series * Updated from global requirements * Add a doc sample for how to use the required field * log: string format arguments changed to function parameters * Fix deprecated\_opts for cli options * Reject option names prefixed with '\_' * Graduate config fixture * Updated from global requirements * Fix test\_version on Python 3.4 * Add section titles and fix markup of docstring * Avoid using too generic names in \_Namespace * Import run\_cross\_tests.sh from oslo-incubator * Move py33 env before py2x 1.3.0 ----- * Include the 'meta' trove classifiers for python versions * Do substitution on overrides and defaults too * Follow style guide for help strings * Updated from global requirements * Fix docstring for \_Namespace.\_get\_cli\_value * Convert to oslo.test * Fix a whitespace in a comment * Fix docstring of parsing order * Switch over to oslosphinx * Remove extraneous vim configuration comments * Fix misspellings in oslo.config * Add py33 trove classifier * Support building wheels (PEP-427) * Add Style Guide for help of config options * Add docs for types * Throw exception if --config-dir doesn't exist * Remove copyright from empty files * Implemented support for custom opt types * Fix for parsing error with Dollar Sign ($) in values * Utilizes assertIsNone and assertIsNotNone * Fix spelling errors in docstrings and comments * Added support of operator '==' * Updated from global requirements * Updated from global requirements * Fix to make ConfigOpts no longer obscure IOErrors * Replace assertEquals with assertEqual * Updated from global requirements * Add the ability to validate default options value 1.3.0a0 ------- * Updated from global requirements 1.2.1 ----- * Fix subparsers add\_parser() regression * Expand DeprecatedOpt documentation 1.2.0 ----- * Release notes for 1.2.0 * Raises error if duplicate keys found in DictOpt * Fix DictOpt to split only the first colon * Update requirements * Release notes for 1.2.0a4 * Remove explicit version from setup.cfg 1.2.0a4 ------- * Fix first param in \_\_eq\_\_ impl to be self * Bump hacking to 0.7.0 * Add auto-create support for OptGroup instances * NoSuchOptError should be used not AttributeError * Fixes six imports * Fix python 3.3 test configuration * Allow use of hacking 0.6.0 and fix min version * Add eclipse project files to .gitignore * Add Python 3 support * Raise an exception when \_oparser is not initialized * Hook docstrings into the published docs * Use oslo.sphinx and remove local copy of doc theme 1.2.0a3 ------- * Add release notes for 1.2.0a3 * Ensure namespace package is installed * Add reload\_config\_files function * Modify \_\_clear\_cache function * Use assertEqual() rather than assertEquals() * python3: Fix traceback while running tests * Tar up .testr.conf as well * Rename tools/(pip|test)-requires to \*requirements.txt * python3: Fix traceback while running tests * python3: Fix traceback while running tests * python3: More python3 compat updates * python3: Remove mox support from oslo.config * Fix config help order to be alphabetical * Enable pep8 E125 check (w/o any changes) * python3: Introduce py33 to tox.ini * Fix the priority of CLI args vs config file values * Parse config files in an argparse callback * Revert additions to ConfigParser public APIs * Refactor type conversion * Adds group name normalization tests * Simplify help string for --config-file * Add release notes for 1.2.0a2 * Add release notes for 1.1.1 1.2.0a2 ------- * Restrict StrOpt to just a few allowed values * Fix MultiConfigParser API breakage * Add release notes for 1.2.0a1 1.2.0a1 ------- * Allow Multiple sets of deprecated name/group for options * Add CLI vs config file priority test * Add test cases for default\_config\_files * Move --config-dir tests into their own test case * Remove some redundant code for bool opt actions * Normalizes non lowercase option group names in conf files * Switch to flake8/hacking instead of pep8+pyflakes * Made a couple minor textual documentation / typo fixes * Re-work test\_cli to use testscenarios * Upgrade testing to use testr * Update build to use pbr * Add python3 compatability support * Use builtin startswith and endswith methods in iniparser * Fixes OptGroup title set typo * Remove debug print statements from tests * Python 3.x compatibility * Support for Dictionary Options * set\_defaults does not work for more than one kwarg * Adds pylint section in tox.ini in oslo.config to check for unused imports * Removes unused imports in the oslo.config.tests module * Improve test cases for boolean values * Rename oslo-config to oslo.config * Fix copyright - from LLC to Foundation * Add Environment::OpenStack classifier * Open 1.2.x development 1.1.0 ----- * Copyright rename LLC to Foundation * Correct some typos in docstrings in cfg.py 1.1.0b1 ------- * Change the name of the project to oslo.config * Switch to semantic versioning * Explicitly set ConfigOpts.\_args to sys.argv[1:] 2013.1b5 -------- * Add deprecated\_group Opt kwarg * Add AUTHORS and ChangeLog to .gitignore * Add missing files to the tarball 2013.1b4 -------- * Make sure to install the oslo package as well * Add LICENSE file 2013.1b3 -------- * Fix setup.py to install the correct package * Allow MultiConfigParser get from mutliple sections * Refactor \_cparser\_get\_with\_deprecated() 2013.1b2 -------- * sort options to make --help output prettier 2013.1b1 -------- * Implements import\_group * Fixes "is not", "not in" syntax usage * Add sphinx documentation * Fix version to 2013.1 * Start using fixtures and testtools * Sync latest setup code from oslo-incubator * Move logging config options into the log module * Add setuptools magic * Add oslo-config project infrastructure * Move files to new locations for oslo-config * Fixes import order errors * Make tox run doctests * Verbose should not enable debug level logging * Fix pep8 E125 errors * Revert "Support lookup of value using "group.key"" * Support lookup of value using "group.key" * Add deprecated --logdir common opt * Add deprecated --logfile common opt * Allow nova and others to override some logging defaults * Fixing the trim for ListOp when reading from config file * Fix set\_default() with boolean CLI options * Improve cfg's argparse sub-parsers support * Fix regression with cfg CLI arguments * Fix ListOpt to trim whitespace * Add another duplicate opt test case * Hide the GroupAttr conf and group attributes * Fix broken --help with CommonConfigOpts * updating sphinx documentation * Don't reference argparse.\_StoreAction * Fix minor coding style issue * Remove ConfigCliParser class * Add support for positional arguments * Use stock argparse behaviour for optional args * Use stock argparse --usage behaviour * Use stock argparse --version behaviour * Remove add\_option() method * Completely remove cfg's disable\_interspersed\_args() * argparse support for cfg * Add a missing comma in a docstring * cfg: fix required if option has a dash * cfg: clean up None value handling * Allow set\_default and set\_override to use None * Tilde expansion for --config-file and --config-dir * Add import\_opt() method to ConfigOpts * Modifies \_is\_opt\_registered fcn to check for duplicate opts * cfg: allow empty config values * cfg: Fix typo in documentation * Update common code to support pep 1.3. bug 1014216 * Use 'is not None' instead of '!= None' * Fix a pep8 error * Adds support for bol and eol spaces to ini files * Add support to include config aliases * Fix pep8 errors * cfg: add a global CONF object * cfg: add generators for iterating over all options * cfg: move constructor args to \_\_call\_\_() args * Run pep8 on tests * Alphabetize imports in openstack/common/cfg.py * cfg: make reset() clear defaults and overrides * cfg: automatically create option groups * cfg: allow options to be marked as required * cfg: use a list comprehension instead of map() * New ConfigOpts.find\_file() for locating conf files * Support for directory source of config files * Provide file extension when when looking for files * Some refactoring of the cfg cache * Add caching to openstack.common.cfg * Typofix, OptionGroup should be OptGroup * Use absolute import for iniparser * Finish implementing MultiStrOpt * Avoid leaking secrets into config logging * Fix bug 954488 * fix restructuredtext formatting in docstrings * Add ConfigOpts.print\_help() * cfg: fix a small comment typo * cfg: unneeded multiple inheritance * PEP8 cleanup (openstack-common) * Backslash continuations (misc.) * Disable ConfigParser interpolation (lp#930270) * Implements blueprint separate-nova-volumeapi * Makes common/cfg.py raise AttributeError * PEP8 type comparison cleanup * Add the Mapping interface to cfg.ConfigOpts * Add cfg test case for recursive substitution * Add support to cfg for disabling interspersed args * Get cfg test cases to 100% * Add cfg test case for exceptions' \_\_str\_\_ methods * Fix some cfg test case naming conflicts * Add new cfg module oslo.config-5.2.0/requirements.txt0000666000175100017510000000061213224676052017256 0ustar zuulzuul00000000000000# The order of packages is significant, because pip processes them in the order # of appearance. Changing the order has an impact on the overall integration # process, which may cause wedges in the gate later. debtcollector>=1.2.0 # Apache-2.0 netaddr>=0.7.18 # BSD six>=1.10.0 # MIT stevedore>=1.20.0 # Apache-2.0 oslo.i18n>=3.15.3 # Apache-2.0 rfc3986>=0.3.1 # Apache-2.0 PyYAML>=3.10 # MIT oslo.config-5.2.0/LICENSE0000666000175100017510000002665213224676052015013 0ustar zuulzuul00000000000000 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. --- License for python-keystoneclient versions prior to 2.1 --- All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of this project nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. oslo.config-5.2.0/AUTHORS0000664000175100017510000001446113224676374015056 0ustar zuulzuul00000000000000Adam Harwell Adit Sarfaty Akihiro Motoki Akihiro Motoki Alex Gaynor Alexis Lee Alexis Lee Alfredo Moralejo Andreas Jaeger Andreas Jaeger Anthony Young Atsushi SAKAI BaoLiang Cui Ben Nemec Brant Knudson Brian Stajkowski Brian Waldon Chandan Kumar ChangBo Guo(gcb) Chaozhe.Chen Chen Chris Hoge Chris Hoge Chris St. Pierre Christian Berendt Chuck Short Chung Chih, Hung Corey Bryant Cyril Roelandt Cyril Roelandt Daisuke Fujita Dan Prince Davanum Srinivas Davanum Srinivas David Ripton David Stanek Dharini Chandrasekar Dirk Mueller Doug Chivers Doug Hellmann Doug Hellmann Edan David Einst Crazy Elena Ezhova Eoghan Glynn Eric Brown Eric Guo Eric Harney Fengqian.Gao Flaper Fesp Flavio Percoco Gary Kotton Gevorg Davoian Giampaolo Lauria Hemanth Makkapati Ian Wienand Ihar Hrachyshka Ilya Shakhat Jakub Libosvar James Carey James E. Blair James Page Jamie Lennox Jamie Lennox Jason Kölker Javeme Javier Pena Jay S. Bryant Jeremy Stanley Joe Gordon Joe Heck Johannes Erdfelt John L. Villalovos Jonathan LaCour Joshua Harlow Joshua Harlow Joshua Harlow Julien Danjou Kevin L. Mitchell Kiseok Kim Lance Bragstad Lars Butler Laurence Miao Lianhao Lu Lin Tan LingxianKong Lucas Alvares Gomes Luis A. Garcia Mark McLoughlin Mark Vanderwiel Markus Zoeller Masaki Matsushita Matt Riedemann Matt Riedemann Matthew Treinish Maxim Kulkin Mehdi Abaakouk Mehdi Abaakouk Mehdi Abaakouk Michael Basnight Michael Krotscheck Miguel Grinberg Mike Fedosin Monty Taylor OpenStack Release Bot Paul Belanger Petr Blaho Piyush Raman Srivastava Radomir Dopieralski Rick Harris Ronald Bradford Roxana Gherle Rushi Agrawal Russell Bryant Ryan Moore Sabari Kumar Murugesan Sascha Peilicke Sascha Peilicke Sean Dague Sean Pryor Sergey Lukjanov Shane Wang Stephen Finucane Stephen Finucane Steven Deaton Swapnil Kulkarni (coolsvap) Takashi NATSUME Thomas Bechtold Thomas Herve Thomas Morin Tim Miller Tom Cammann Tong Damon Da Tony Breeds Trevor McCasland Victor Morales Victor Stinner Vincent Untz Vishvananda Ishaya Vu Cong Tuan Xi Yang YAMAMOTO Takashi Yaguo Zhou Yibo Cai Yuriy Taraday Yuval Brik ZhiQiang Fan Zhihai Song ZhongShengping Zhongyue Luo Zhongyue Luo Zuul avnish bhavani.cr blue55 chenxing gengchc2 hnyang howardlee liu-sheng liyingjun llg8212 loooosy lzyeval melissaml ricolin shangxiaobj skudriashev sonu.kumar ting.wang venkatamahesh vponomaryov yan.haifeng zhangyanxian oslo.config-5.2.0/PKG-INFO0000664000175100017510000000371113224676374015077 0ustar zuulzuul00000000000000Metadata-Version: 1.1 Name: oslo.config Version: 5.2.0 Summary: Oslo Configuration API Home-page: https://docs.openstack.org/oslo.config/latest/ Author: OpenStack Author-email: openstack-dev@lists.openstack.org License: UNKNOWN Description-Content-Type: UNKNOWN Description: ======================== Team and repository tags ======================== .. image:: https://governance.openstack.org/badges/oslo.config.svg :target: https://governance.openstack.org/reference/tags/index.html .. Change things from this point on ========================== Oslo Configuration Library ========================== .. image:: https://img.shields.io/pypi/v/oslo.config.svg :target: https://pypi.python.org/pypi/oslo.config/ :alt: Latest Version .. image:: https://img.shields.io/pypi/dm/oslo.config.svg :target: https://pypi.python.org/pypi/oslo.config/ :alt: Downloads The Oslo configuration API supports parsing command line arguments and .ini style configuration files. * License: Apache License, Version 2.0 * Documentation: https://docs.openstack.org/oslo.config/latest/ * Source: https://git.openstack.org/cgit/openstack/oslo.config * Bugs: https://bugs.launchpad.net/oslo.config Platform: UNKNOWN Classifier: Development Status :: 5 - Production/Stable Classifier: Environment :: OpenStack Classifier: Intended Audience :: Developers Classifier: Intended Audience :: Information Technology Classifier: License :: OSI Approved :: Apache Software License Classifier: Operating System :: OS Independent Classifier: Programming Language :: Python Classifier: Programming Language :: Python :: 2 Classifier: Programming Language :: Python :: 2.7 Classifier: Programming Language :: Python :: 3 Classifier: Programming Language :: Python :: 3.5 oslo.config-5.2.0/tox.ini0000666000175100017510000000306313224676052015310 0ustar zuulzuul00000000000000[tox] minversion = 2.0 distribute = False envlist = py35,py27,pep8 [testenv] install_command = pip install {opts} {packages} deps = -c{env:UPPER_CONSTRAINTS_FILE:https://git.openstack.org/cgit/openstack/requirements/plain/upper-constraints.txt} -r{toxinidir}/test-requirements.txt -r{toxinidir}/requirements.txt commands = python setup.py test --coverage --coverage-package-name=oslo_config --slowest --testr-args='{posargs}' coverage report --show-missing [testenv:pep8] commands = flake8 # Run security linter bandit -r oslo_config -x tests -n5 [testenv:cover] commands = python setup.py test --coverage --coverage-package-name=oslo_config --testr-args='{posargs}' coverage report --show-missing [testenv:venv] commands = {posargs} [testenv:docs] commands = python setup.py build_sphinx [testenv:bandit] commands = bandit -r oslo_config -x tests -n5 [flake8] show-source = True enable-extensions = H203,H106 exclude = .tox,dist,doc,*.egg,build [testenv:pip-missing-reqs] # do not install test-requirements as that will pollute the virtualenv for # determining missing packages # this also means that pip-missing-reqs must be installed separately, outside # of the requirements.txt files deps = pip_missing_reqs commands = pip-missing-reqs -d --ignore-module=oslo_config* --ignore-module=pkg_resources --ignore-file=oslo_config/tests/* --ignore-file=tests/ oslo_config [testenv:releasenotes] commands = sphinx-build -a -E -W -d releasenotes/build/doctrees -b html releasenotes/source releasenotes/build/html [hacking] import_exceptions = oslo_config._i18n oslo.config-5.2.0/oslo.config.egg-info/0000775000175100017510000000000013224676374017712 5ustar zuulzuul00000000000000oslo.config-5.2.0/oslo.config.egg-info/requires.txt0000664000175100017510000000016113224676374022310 0ustar zuulzuul00000000000000debtcollector>=1.2.0 netaddr>=0.7.18 six>=1.10.0 stevedore>=1.20.0 oslo.i18n>=3.15.3 rfc3986>=0.3.1 PyYAML>=3.10 oslo.config-5.2.0/oslo.config.egg-info/entry_points.txt0000664000175100017510000000021113224676374023202 0ustar zuulzuul00000000000000[console_scripts] oslo-config-generator = oslo_config.generator:main [oslo.config.opts] oslo.config = oslo_config._list_opts:list_opts oslo.config-5.2.0/oslo.config.egg-info/SOURCES.txt0000664000175100017510000000575313224676374021610 0ustar zuulzuul00000000000000.coveragerc .testr.conf AUTHORS CONTRIBUTING.rst ChangeLog HACKING.rst LICENSE README.rst requirements.txt setup.cfg setup.py test-requirements.txt tox.ini doc/source/conf.py doc/source/config-generator.conf doc/source/index.rst doc/source/cli/generator.rst doc/source/cli/index.rst doc/source/configuration/format.rst doc/source/configuration/index.rst doc/source/configuration/mutable.rst doc/source/contributor/index.rst doc/source/reference/builtins.rst doc/source/reference/cfg.rst doc/source/reference/cfgfilter.rst doc/source/reference/configopts.rst doc/source/reference/exceptions.rst doc/source/reference/faq.rst doc/source/reference/fixture.rst doc/source/reference/helpers.rst doc/source/reference/index.rst doc/source/reference/mutable.rst doc/source/reference/namespaces.rst doc/source/reference/opts.rst doc/source/reference/parser.rst doc/source/reference/sphinxconfiggen.rst doc/source/reference/sphinxext.rst doc/source/reference/styleguide.rst doc/source/reference/types.rst oslo.config.egg-info/PKG-INFO oslo.config.egg-info/SOURCES.txt oslo.config.egg-info/dependency_links.txt oslo.config.egg-info/entry_points.txt oslo.config.egg-info/not-zip-safe oslo.config.egg-info/pbr.json oslo.config.egg-info/requires.txt oslo.config.egg-info/top_level.txt oslo_config/__init__.py oslo_config/_i18n.py oslo_config/_list_opts.py oslo_config/cfg.py oslo_config/cfgfilter.py oslo_config/fixture.py oslo_config/generator.py oslo_config/iniparser.py oslo_config/sphinxconfiggen.py oslo_config/sphinxext.py oslo_config/types.py oslo_config/version.py oslo_config/tests/__init__.py oslo_config/tests/test_cfg.py oslo_config/tests/test_cfgfilter.py oslo_config/tests/test_fixture.conf oslo_config/tests/test_fixture.py oslo_config/tests/test_generator.py oslo_config/tests/test_iniparser.py oslo_config/tests/test_sphinxconfiggen.py oslo_config/tests/test_sphinxext.py oslo_config/tests/test_types.py oslo_config/tests/testmods/__init__.py oslo_config/tests/testmods/bar_foo_opt.py oslo_config/tests/testmods/baz_qux_opt.py oslo_config/tests/testmods/blaa_opt.py oslo_config/tests/testmods/fbaar_baa_opt.py oslo_config/tests/testmods/fbar_foo_opt.py oslo_config/tests/testmods/fblaa_opt.py releasenotes/notes/.placeholder releasenotes/notes/add-HostAddressOpt-6e7e2afe7c7863cb.yaml releasenotes/notes/add-default-config-dirs-03340ff6689afe94.yaml releasenotes/notes/add-float-min-max-b1a2e16301c8435c.yaml releasenotes/notes/add-port_type-8704295c6a56265d.yaml releasenotes/notes/add-reno-71dc832ce29b962f.yaml releasenotes/notes/machine-readable-sample-config-e8f8ba43ababcf99.yaml releasenotes/notes/show-deprecated-reason-361a8eb31e05c97e.yaml releasenotes/notes/support-choice-descriptions-8b2d0c14fbd16b2a.yaml releasenotes/source/conf.py releasenotes/source/index.rst releasenotes/source/liberty.rst releasenotes/source/mitaka.rst releasenotes/source/newton.rst releasenotes/source/ocata.rst releasenotes/source/pike.rst releasenotes/source/unreleased.rst releasenotes/source/_static/.placeholder releasenotes/source/_templates/.placeholderoslo.config-5.2.0/oslo.config.egg-info/pbr.json0000664000175100017510000000005613224676374021371 0ustar zuulzuul00000000000000{"git_version": "5578616", "is_release": true}oslo.config-5.2.0/oslo.config.egg-info/dependency_links.txt0000664000175100017510000000000113224676374023760 0ustar zuulzuul00000000000000 oslo.config-5.2.0/oslo.config.egg-info/not-zip-safe0000664000175100017510000000000113224676334022134 0ustar zuulzuul00000000000000 oslo.config-5.2.0/oslo.config.egg-info/PKG-INFO0000664000175100017510000000371113224676374021011 0ustar zuulzuul00000000000000Metadata-Version: 1.1 Name: oslo.config Version: 5.2.0 Summary: Oslo Configuration API Home-page: https://docs.openstack.org/oslo.config/latest/ Author: OpenStack Author-email: openstack-dev@lists.openstack.org License: UNKNOWN Description-Content-Type: UNKNOWN Description: ======================== Team and repository tags ======================== .. image:: https://governance.openstack.org/badges/oslo.config.svg :target: https://governance.openstack.org/reference/tags/index.html .. Change things from this point on ========================== Oslo Configuration Library ========================== .. image:: https://img.shields.io/pypi/v/oslo.config.svg :target: https://pypi.python.org/pypi/oslo.config/ :alt: Latest Version .. image:: https://img.shields.io/pypi/dm/oslo.config.svg :target: https://pypi.python.org/pypi/oslo.config/ :alt: Downloads The Oslo configuration API supports parsing command line arguments and .ini style configuration files. * License: Apache License, Version 2.0 * Documentation: https://docs.openstack.org/oslo.config/latest/ * Source: https://git.openstack.org/cgit/openstack/oslo.config * Bugs: https://bugs.launchpad.net/oslo.config Platform: UNKNOWN Classifier: Development Status :: 5 - Production/Stable Classifier: Environment :: OpenStack Classifier: Intended Audience :: Developers Classifier: Intended Audience :: Information Technology Classifier: License :: OSI Approved :: Apache Software License Classifier: Operating System :: OS Independent Classifier: Programming Language :: Python Classifier: Programming Language :: Python :: 2 Classifier: Programming Language :: Python :: 2.7 Classifier: Programming Language :: Python :: 3 Classifier: Programming Language :: Python :: 3.5 oslo.config-5.2.0/oslo.config.egg-info/top_level.txt0000664000175100017510000000001413224676374022437 0ustar zuulzuul00000000000000oslo_config oslo.config-5.2.0/oslo_config/0000775000175100017510000000000013224676374016301 5ustar zuulzuul00000000000000oslo.config-5.2.0/oslo_config/tests/0000775000175100017510000000000013224676374017443 5ustar zuulzuul00000000000000oslo.config-5.2.0/oslo_config/tests/test_cfg.py0000666000175100017510000060170013224676052021612 0ustar zuulzuul00000000000000# Copyright 2014 Red Hat, Inc. # # 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 argparse import errno import functools import logging import os import shutil import sys import tempfile import fixtures import mock from oslotest import base import six from six import moves import testscenarios from oslo_config import cfg from oslo_config import types load_tests = testscenarios.load_tests_apply_scenarios class ExceptionsTestCase(base.BaseTestCase): def test_error(self): msg = str(cfg.Error('foobar')) self.assertEqual('foobar', msg) def test_args_already_parsed_error(self): msg = str(cfg.ArgsAlreadyParsedError('foobar')) self.assertEqual('arguments already parsed: foobar', msg) def test_no_such_opt_error(self): msg = str(cfg.NoSuchOptError('foo')) self.assertEqual('no such option foo in group [DEFAULT]', msg) def test_no_such_opt_error_with_group(self): msg = str(cfg.NoSuchOptError('foo', cfg.OptGroup('bar'))) self.assertEqual('no such option foo in group [bar]', msg) def test_no_such_group_error(self): msg = str(cfg.NoSuchGroupError('bar')) self.assertEqual('no such group [bar]', msg) def test_duplicate_opt_error(self): msg = str(cfg.DuplicateOptError('foo')) self.assertEqual('duplicate option: foo', msg) def test_required_opt_error(self): msg = str(cfg.RequiredOptError('foo')) self.assertEqual('value required for option foo in group [DEFAULT]', msg) def test_required_opt_error_with_group(self): msg = str(cfg.RequiredOptError('foo', cfg.OptGroup('bar'))) self.assertEqual('value required for option foo in group [bar]', msg) def test_template_substitution_error(self): msg = str(cfg.TemplateSubstitutionError('foobar')) self.assertEqual('template substitution error: foobar', msg) def test_config_files_not_found_error(self): msg = str(cfg.ConfigFilesNotFoundError(['foo', 'bar'])) self.assertEqual('Failed to find some config files: foo,bar', msg) def test_config_files_permission_denied_error(self): msg = str(cfg.ConfigFilesPermissionDeniedError(['foo', 'bar'])) self.assertEqual('Failed to open some config files: foo,bar', msg) def test_config_dir_not_found_error(self): msg = str(cfg.ConfigDirNotFoundError('foobar')) self.assertEqual('Failed to read config file directory: foobar', msg) def test_config_file_parse_error(self): msg = str(cfg.ConfigFileParseError('foo', 'foobar')) self.assertEqual('Failed to parse foo: foobar', msg) class BaseTestCase(base.BaseTestCase): class TestConfigOpts(cfg.ConfigOpts): def __call__(self, args=None, default_config_files=[], default_config_dirs=[]): return cfg.ConfigOpts.__call__( self, args=args, prog='test', version='1.0', usage='%(prog)s FOO BAR', description='somedesc', epilog='tepilog', default_config_files=default_config_files, default_config_dirs=default_config_dirs, validate_default_values=True) def setUp(self): super(BaseTestCase, self).setUp() self.useFixture(fixtures.NestedTempfile()) self.conf = self.TestConfigOpts() self.tempdirs = [] def create_tempfiles(self, files, ext='.conf'): tempfiles = [] for (basename, contents) in files: if not os.path.isabs(basename): # create all the tempfiles in a tempdir tmpdir = tempfile.mkdtemp() path = os.path.join(tmpdir, basename + ext) # the path can start with a subdirectory so create # it if it doesn't exist yet if not os.path.exists(os.path.dirname(path)): os.makedirs(os.path.dirname(path)) else: path = basename + ext fd = os.open(path, os.O_CREAT | os.O_WRONLY) tempfiles.append(path) try: os.write(fd, contents.encode('utf-8')) finally: os.close(fd) return tempfiles class UsageTestCase(BaseTestCase): def test_print_usage(self): f = moves.StringIO() self.conf([]) self.conf.print_usage(file=f) self.assertIn('usage: test FOO BAR', f.getvalue()) self.assertNotIn('somedesc', f.getvalue()) self.assertNotIn('tepilog', f.getvalue()) self.assertNotIn('optional:', f.getvalue()) def test_print_help(self): f = moves.StringIO() self.conf([]) self.conf.print_help(file=f) self.assertIn('usage: test FOO BAR', f.getvalue()) self.assertIn('somedesc', f.getvalue()) self.assertIn('tepilog', f.getvalue()) self.assertNotIn('optional:', f.getvalue()) class HelpTestCase(BaseTestCase): def test_print_help(self): f = moves.StringIO() self.conf([]) self.conf.print_help(file=f) self.assertIn('usage: test FOO BAR', f.getvalue()) self.assertIn('optional', f.getvalue()) self.assertIn('-h, --help', f.getvalue()) def test_print_strOpt_with_choices_help(self): f = moves.StringIO() cli_opts = [ cfg.StrOpt('aa', short='a', default='xx', choices=['xx', 'yy', 'zz'], help='StrOpt with choices.'), cfg.StrOpt('bb', short='b', default='yy', choices=[None, 'yy', 'zz'], help='StrOpt with choices.'), cfg.StrOpt('cc', short='c', default='zz', choices=['', 'yy', 'zz'], help='StrOpt with choices.'), ] self.conf.register_cli_opts(cli_opts) self.conf([]) self.conf.print_help(file=f) self.assertIn('usage: test FOO BAR', f.getvalue()) self.assertIn('optional', f.getvalue()) self.assertIn('-h, --help', f.getvalue()) self.assertIn('StrOpt with choices. Allowed values: xx, yy, zz', f.getvalue()) self.assertIn('StrOpt with choices. Allowed values: , yy, zz', f.getvalue()) self.assertIn("StrOpt with choices. Allowed values: '', yy, zz", f.getvalue()) def test_print_sorted_help(self): f = moves.StringIO() self.conf.register_cli_opt(cfg.StrOpt('abc')) self.conf.register_cli_opt(cfg.StrOpt('zba')) self.conf.register_cli_opt(cfg.StrOpt('ghi')) self.conf.register_cli_opt(cfg.StrOpt('deb')) self.conf([]) self.conf.print_help(file=f) zba = f.getvalue().find('--zba') abc = f.getvalue().find('--abc') ghi = f.getvalue().find('--ghi') deb = f.getvalue().find('--deb') list = [abc, deb, ghi, zba] self.assertEqual(sorted(list), list) def test_print_sorted_help_with_positionals(self): f = moves.StringIO() self.conf.register_cli_opt(cfg.StrOpt('pst', positional=True)) self.conf.register_cli_opt(cfg.StrOpt('abc')) self.conf.register_cli_opt(cfg.StrOpt('zba')) self.conf.register_cli_opt(cfg.StrOpt('ghi')) self.conf([]) self.conf.print_help(file=f) zba = f.getvalue().find('--zba') abc = f.getvalue().find('--abc') ghi = f.getvalue().find('--ghi') list = [abc, ghi, zba] self.assertEqual(sorted(list), list) def test_print_help_with_deprecated(self): f = moves.StringIO() abc = cfg.StrOpt('a-bc', deprecated_opts=[cfg.DeprecatedOpt('d-ef')]) uvw = cfg.StrOpt('u-vw', deprecated_name='x-yz') self.conf.register_cli_opt(abc) self.conf.register_cli_opt(uvw) self.conf([]) self.conf.print_help(file=f) self.assertIn('--a-bc A_BC, --d-ef A_BC, --d_ef A_BC', f.getvalue()) self.assertIn('--u-vw U_VW, --x-yz U_VW, --x_yz U_VW', f.getvalue()) class FindConfigFilesTestCase(BaseTestCase): def test_find_config_files(self): config_files = [os.path.expanduser('~/.blaa/blaa.conf'), '/etc/foo.conf'] self.useFixture(fixtures.MonkeyPatch('sys.argv', ['foo'])) self.useFixture(fixtures.MonkeyPatch('os.path.exists', lambda p: p in config_files)) self.assertEqual(cfg.find_config_files(project='blaa'), config_files) def test_find_config_files_snap(self): config_files = ['/snap/nova/current/etc/blaa/blaa.conf', '/var/snap/nova/common/etc/blaa/blaa.conf'] fake_env = {'SNAP': '/snap/nova/current/', 'SNAP_COMMON': '/var/snap/nova/common/'} self.useFixture(fixtures.MonkeyPatch('sys.argv', ['foo'])) self.useFixture(fixtures.MonkeyPatch('os.path.exists', lambda p: p in config_files)) self.useFixture(fixtures.MonkeyPatch('os.environ', fake_env)) self.assertEqual(cfg.find_config_files(project='blaa'), ['/var/snap/nova/common/etc/blaa/blaa.conf']) def test_find_config_files_with_extension(self): config_files = ['/etc/foo.json'] self.useFixture(fixtures.MonkeyPatch('sys.argv', ['foo'])) self.useFixture(fixtures.MonkeyPatch('os.path.exists', lambda p: p in config_files)) self.assertEqual(cfg.find_config_files(project='blaa'), []) self.assertEqual(cfg.find_config_files(project='blaa', extension='.json'), config_files) class FindConfigDirsTestCase(BaseTestCase): def test_find_config_dirs(self): config_dirs = [os.path.expanduser('~/.blaa/blaa.conf.d'), '/etc/foo.conf.d'] self.useFixture(fixtures.MonkeyPatch('sys.argv', ['foo'])) self.useFixture(fixtures.MonkeyPatch('os.path.exists', lambda p: p in config_dirs)) self.assertEqual(cfg.find_config_dirs(project='blaa'), config_dirs) def test_find_config_dirs_snap(self): config_dirs = ['/var/snap/nova/common/etc/blaa/blaa.conf.d'] fake_env = {'SNAP': '/snap/nova/current/', 'SNAP_COMMON': '/var/snap/nova/common/'} self.useFixture(fixtures.MonkeyPatch('sys.argv', ['foo'])) self.useFixture(fixtures.MonkeyPatch('os.path.exists', lambda p: p in config_dirs)) self.useFixture(fixtures.MonkeyPatch('os.environ', fake_env)) self.assertEqual(cfg.find_config_dirs(project='blaa'), config_dirs) def test_find_config_dirs_non_exists(self): self.useFixture(fixtures.MonkeyPatch('sys.argv', ['foo'])) self.assertEqual(cfg.find_config_dirs(project='blaa'), []) def test_find_config_dirs_with_extension(self): config_dirs = ['/etc/foo.json.d'] self.useFixture(fixtures.MonkeyPatch('sys.argv', ['foo'])) self.useFixture(fixtures.MonkeyPatch('os.path.exists', lambda p: p in config_dirs)) self.assertEqual(cfg.find_config_dirs(project='blaa'), []) self.assertEqual(cfg.find_config_dirs(project='blaa', extension='.json.d'), config_dirs) class DefaultConfigFilesTestCase(BaseTestCase): def test_use_default(self): self.conf.register_opt(cfg.StrOpt('foo')) paths = self.create_tempfiles([('foo-', '[DEFAULT]\n''foo = bar\n')]) self.conf.register_cli_opt(cfg.StrOpt('config-file-foo')) self.conf(args=['--config-file-foo', 'foo.conf'], default_config_files=[paths[0]]) self.assertEqual([paths[0]], self.conf.config_file) self.assertEqual('bar', self.conf.foo) def test_do_not_use_default_multi_arg(self): self.conf.register_opt(cfg.StrOpt('foo')) paths = self.create_tempfiles([('foo-', '[DEFAULT]\n''foo = bar\n')]) self.conf(args=['--config-file', paths[0]], default_config_files=['bar.conf']) self.assertEqual([paths[0]], self.conf.config_file) self.assertEqual('bar', self.conf.foo) def test_do_not_use_default_single_arg(self): self.conf.register_opt(cfg.StrOpt('foo')) paths = self.create_tempfiles([('foo-', '[DEFAULT]\n''foo = bar\n')]) self.conf(args=['--config-file=' + paths[0]], default_config_files=['bar.conf']) self.assertEqual([paths[0]], self.conf.config_file) self.assertEqual('bar', self.conf.foo) def test_no_default_config_file(self): self.conf(args=[]) self.assertEqual([], self.conf.config_file) def test_find_default_config_file(self): paths = self.create_tempfiles([('def', '[DEFAULT]')]) self.useFixture(fixtures.MonkeyPatch( 'oslo_config.cfg.find_config_files', lambda project, prog: paths)) self.conf(args=[], default_config_files=None) self.assertEqual(paths, self.conf.config_file) def test_default_config_file(self): paths = self.create_tempfiles([('def', '[DEFAULT]')]) self.conf(args=[], default_config_files=paths) self.assertEqual(paths, self.conf.config_file) def test_default_config_file_with_value(self): self.conf.register_cli_opt(cfg.StrOpt('foo')) paths = self.create_tempfiles([('def', '[DEFAULT]\n''foo = bar\n')]) self.conf(args=[], default_config_files=paths) self.assertEqual(paths, self.conf.config_file) self.assertEqual('bar', self.conf.foo) def test_default_config_file_priority(self): self.conf.register_cli_opt(cfg.StrOpt('foo')) paths = self.create_tempfiles([('def', '[DEFAULT]\n''foo = bar\n')]) self.conf(args=['--foo=blaa'], default_config_files=paths) self.assertEqual(paths, self.conf.config_file) self.assertEqual('blaa', self.conf.foo) class DefaultConfigDirsTestCase(BaseTestCase): def test_use_default(self): self.conf.register_opt(cfg.StrOpt('foo')) paths = self.create_tempfiles([('foo.conf.d/foo', '[DEFAULT]\n''foo = bar\n')]) p = os.path.dirname(paths[0]) self.conf.register_cli_opt(cfg.StrOpt('config-dir-foo')) self.conf(args=['--config-dir-foo', 'foo.conf.d'], default_config_dirs=[p]) self.assertEqual([p], self.conf.config_dir) self.assertEqual('bar', self.conf.foo) def test_do_not_use_default_multi_arg(self): self.conf.register_opt(cfg.StrOpt('foo')) paths = self.create_tempfiles([('foo.conf.d/foo', '[DEFAULT]\n''foo = bar\n')]) p = os.path.dirname(paths[0]) self.conf(args=['--config-dir', p], default_config_dirs=['bar.conf.d']) self.assertEqual([p], self.conf.config_dirs) self.assertEqual('bar', self.conf.foo) def test_do_not_use_default_single_arg(self): self.conf.register_opt(cfg.StrOpt('foo')) paths = self.create_tempfiles([('foo.conf.d/foo', '[DEFAULT]\n''foo = bar\n')]) p = os.path.dirname(paths[0]) self.conf(args=['--config-dir=' + p], default_config_dirs=['bar.conf.d']) self.assertEqual([p], self.conf.config_dir) self.assertEqual('bar', self.conf.foo) def test_no_default_config_dir(self): self.conf(args=[]) self.assertEqual([], self.conf.config_dir) def test_find_default_config_dir(self): paths = self.create_tempfiles([('def.conf.d/def', '[DEFAULT]')]) p = os.path.dirname(paths[0]) self.useFixture(fixtures.MonkeyPatch( 'oslo_config.cfg.find_config_dirs', lambda project, prog: p)) self.conf(args=[], default_config_dirs=None) self.assertEqual([p], self.conf.config_dir) def test_default_config_dir(self): paths = self.create_tempfiles([('def.conf.d/def', '[DEFAULT]')]) p = os.path.dirname(paths[0]) self.conf(args=[], default_config_dirs=[p]) self.assertEqual([p], self.conf.config_dir) def test_default_config_dir_with_value(self): self.conf.register_cli_opt(cfg.StrOpt('foo')) paths = self.create_tempfiles([('def.conf.d/def', '[DEFAULT]\n''foo = bar\n')]) p = os.path.dirname(paths[0]) self.conf(args=[], default_config_dirs=[p]) self.assertEqual([p], self.conf.config_dir) self.assertEqual('bar', self.conf.foo) def test_default_config_dir_priority(self): self.conf.register_cli_opt(cfg.StrOpt('foo')) paths = self.create_tempfiles([('def.conf.d/def', '[DEFAULT]\n''foo = bar\n')]) p = os.path.dirname(paths[0]) self.conf(args=['--foo=blaa'], default_config_dirs=[p]) self.assertEqual([p], self.conf.config_dir) self.assertEqual('blaa', self.conf.foo) class CliOptsTestCase(BaseTestCase): """Test CLI Options. Each test scenario takes a name for the scenarios, as well as a dict: opt_class - class of the type of option that should be tested default - a default value for the option cli_args - a list containing a representation of an input command line value - the result value that is expected to be found deps - a tuple of deprecated name/group """ IPv4Opt = functools.partial(cfg.IPOpt, version=4) IPv6Opt = functools.partial(cfg.IPOpt, version=6) multi_int = functools.partial(cfg.MultiOpt, item_type=types.Integer()) multi_float = functools.partial(cfg.MultiOpt, item_type=types.Float()) multi_string = functools.partial(cfg.MultiOpt, item_type=types.String()) scenarios = [ ('str_default', dict(opt_class=cfg.StrOpt, default=None, cli_args=[], value=None, deps=(None, None))), ('str_arg', dict(opt_class=cfg.StrOpt, default=None, cli_args=['--foo', 'bar'], value='bar', deps=(None, None))), ('str_arg_deprecated_name', dict(opt_class=cfg.StrOpt, default=None, cli_args=['--oldfoo', 'bar'], value='bar', deps=('oldfoo', None))), ('str_arg_deprecated_group', dict(opt_class=cfg.StrOpt, default=None, cli_args=['--old-foo', 'bar'], value='bar', deps=(None, 'old'))), ('str_arg_deprecated_group_default', dict(opt_class=cfg.StrOpt, default=None, cli_args=['--foo', 'bar'], value='bar', deps=(None, 'DEFAULT'))), ('str_arg_deprecated_group_and_name', dict(opt_class=cfg.StrOpt, default=None, cli_args=['--old-oof', 'bar'], value='bar', deps=('oof', 'old'))), ('bool_default', dict(opt_class=cfg.BoolOpt, default=False, cli_args=[], value=False, deps=(None, None))), ('bool_arg', dict(opt_class=cfg.BoolOpt, default=None, cli_args=['--foo'], value=True, deps=(None, None))), ('bool_arg_deprecated_name', dict(opt_class=cfg.BoolOpt, default=None, cli_args=['--oldfoo'], value=True, deps=('oldfoo', None))), ('bool_arg_deprecated_group', dict(opt_class=cfg.BoolOpt, default=None, cli_args=['--old-foo'], value=True, deps=(None, 'old'))), ('bool_arg_deprecated_group_default', dict(opt_class=cfg.BoolOpt, default=None, cli_args=['--foo'], value=True, deps=(None, 'DEFAULT'))), ('bool_arg_deprecated_group_and_name', dict(opt_class=cfg.BoolOpt, default=None, cli_args=['--old-oof'], value=True, deps=('oof', 'old'))), ('bool_arg_inverse', dict(opt_class=cfg.BoolOpt, default=None, cli_args=['--foo', '--nofoo'], value=False, deps=(None, None))), ('bool_arg_inverse_deprecated_name', dict(opt_class=cfg.BoolOpt, default=None, cli_args=['--oldfoo', '--nooldfoo'], value=False, deps=('oldfoo', None))), ('bool_arg_inverse_deprecated_group', dict(opt_class=cfg.BoolOpt, default=None, cli_args=['--old-foo', '--old-nofoo'], value=False, deps=(None, 'old'))), ('bool_arg_inverse_deprecated_group_default', dict(opt_class=cfg.BoolOpt, default=None, cli_args=['--foo', '--nofoo'], value=False, deps=(None, 'DEFAULT'))), ('bool_arg_inverse_deprecated_group_and_name', dict(opt_class=cfg.BoolOpt, default=None, cli_args=['--old-oof', '--old-nooof'], value=False, deps=('oof', 'old'))), ('int_default', dict(opt_class=cfg.IntOpt, default=10, cli_args=[], value=10, deps=(None, None))), ('int_arg', dict(opt_class=cfg.IntOpt, default=None, cli_args=['--foo=20'], value=20, deps=(None, None))), ('int_arg_deprecated_name', dict(opt_class=cfg.IntOpt, default=None, cli_args=['--oldfoo=20'], value=20, deps=('oldfoo', None))), ('int_arg_deprecated_group', dict(opt_class=cfg.IntOpt, default=None, cli_args=['--old-foo=20'], value=20, deps=(None, 'old'))), ('int_arg_deprecated_group_default', dict(opt_class=cfg.IntOpt, default=None, cli_args=['--foo=20'], value=20, deps=(None, 'DEFAULT'))), ('int_arg_deprecated_group_and_name', dict(opt_class=cfg.IntOpt, default=None, cli_args=['--old-oof=20'], value=20, deps=('oof', 'old'))), ('float_default', dict(opt_class=cfg.FloatOpt, default=1.0, cli_args=[], value=1.0, deps=(None, None))), ('float_arg', dict(opt_class=cfg.FloatOpt, default=None, cli_args=['--foo', '2.0'], value=2.0, deps=(None, None))), ('float_arg_deprecated_name', dict(opt_class=cfg.FloatOpt, default=None, cli_args=['--oldfoo', '2.0'], value=2.0, deps=('oldfoo', None))), ('float_arg_deprecated_group', dict(opt_class=cfg.FloatOpt, default=None, cli_args=['--old-foo', '2.0'], value=2.0, deps=(None, 'old'))), ('float_arg_deprecated_group_default', dict(opt_class=cfg.FloatOpt, default=None, cli_args=['--foo', '2.0'], value=2.0, deps=(None, 'DEFAULT'))), ('float_arg_deprecated_group_and_name', dict(opt_class=cfg.FloatOpt, default=None, cli_args=['--old-oof', '2.0'], value=2.0, deps=('oof', 'old'))), ('float_default_as_integer', dict(opt_class=cfg.FloatOpt, default=2, cli_args=['--old-oof', '2.0'], value=2.0, deps=('oof', 'old'))), ('ipv4addr_arg', dict(opt_class=IPv4Opt, default=None, cli_args=['--foo', '192.168.0.1'], value='192.168.0.1', deps=(None, None))), ('ipaddr_arg_implicitv4', dict(opt_class=cfg.IPOpt, default=None, cli_args=['--foo', '192.168.0.1'], value='192.168.0.1', deps=(None, None))), ('ipaddr_arg_implicitv6', dict(opt_class=cfg.IPOpt, default=None, cli_args=['--foo', 'abcd:ef::1'], value='abcd:ef::1', deps=(None, None))), ('ipv6addr_arg', dict(opt_class=IPv6Opt, default=None, cli_args=['--foo', 'abcd:ef::1'], value='abcd:ef::1', deps=(None, None))), ('list_default', dict(opt_class=cfg.ListOpt, default=['bar'], cli_args=[], value=['bar'], deps=(None, None))), ('list_arg', dict(opt_class=cfg.ListOpt, default=None, cli_args=['--foo', 'blaa,bar'], value=['blaa', 'bar'], deps=(None, None))), ('list_arg_with_spaces', dict(opt_class=cfg.ListOpt, default=None, cli_args=['--foo', 'blaa ,bar'], value=['blaa', 'bar'], deps=(None, None))), ('list_arg_deprecated_name', dict(opt_class=cfg.ListOpt, default=None, cli_args=['--oldfoo', 'blaa,bar'], value=['blaa', 'bar'], deps=('oldfoo', None))), ('list_arg_deprecated_group', dict(opt_class=cfg.ListOpt, default=None, cli_args=['--old-foo', 'blaa,bar'], value=['blaa', 'bar'], deps=(None, 'old'))), ('list_arg_deprecated_group_default', dict(opt_class=cfg.ListOpt, default=None, cli_args=['--foo', 'blaa,bar'], value=['blaa', 'bar'], deps=(None, 'DEFAULT'))), ('list_arg_deprecated_group_and_name', dict(opt_class=cfg.ListOpt, default=None, cli_args=['--old-oof', 'blaa,bar'], value=['blaa', 'bar'], deps=('oof', 'old'))), ('dict_default', dict(opt_class=cfg.DictOpt, default={'foo': 'bar'}, cli_args=[], value={'foo': 'bar'}, deps=(None, None))), ('dict_arg', dict(opt_class=cfg.DictOpt, default=None, cli_args=['--foo', 'key1:blaa,key2:bar'], value={'key1': 'blaa', 'key2': 'bar'}, deps=(None, None))), ('dict_arg_multiple_keys_last_wins', dict(opt_class=cfg.DictOpt, default=None, cli_args=['--foo', 'key1:blaa', '--foo', 'key2:bar'], value={'key2': 'bar'}, deps=(None, None))), ('dict_arg_with_spaces', dict(opt_class=cfg.DictOpt, default=None, cli_args=['--foo', 'key1:blaa ,key2:bar'], value={'key1': 'blaa', 'key2': 'bar'}, deps=(None, None))), ('dict_arg_deprecated_name', dict(opt_class=cfg.DictOpt, default=None, cli_args=['--oldfoo', 'key1:blaa', '--oldfoo', 'key2:bar'], value={'key2': 'bar'}, deps=('oldfoo', None))), ('dict_arg_deprecated_group', dict(opt_class=cfg.DictOpt, default=None, cli_args=['--old-foo', 'key1:blaa,key2:bar'], value={'key1': 'blaa', 'key2': 'bar'}, deps=(None, 'old'))), ('dict_arg_deprecated_group2', dict(opt_class=cfg.DictOpt, default=None, cli_args=['--old-foo', 'key1:blaa', '--old-foo', 'key2:bar'], value={'key2': 'bar'}, deps=(None, 'old'))), ('dict_arg_deprecated_group_default', dict(opt_class=cfg.DictOpt, default=None, cli_args=['--foo', 'key1:blaa', '--foo', 'key2:bar'], value={'key2': 'bar'}, deps=(None, 'DEFAULT'))), ('dict_arg_deprecated_group_and_name', dict(opt_class=cfg.DictOpt, default=None, cli_args=['--old-oof', 'key1:blaa,key2:bar'], value={'key1': 'blaa', 'key2': 'bar'}, deps=('oof', 'old'))), ('dict_arg_deprecated_group_and_name2', dict(opt_class=cfg.DictOpt, default=None, cli_args=['--old-oof', 'key1:blaa', '--old-oof', 'key2:bar'], value={'key2': 'bar'}, deps=('oof', 'old'))), ('port_default', dict(opt_class=cfg.PortOpt, default=80, cli_args=[], value=80, deps=(None, None))), ('port_arg', dict(opt_class=cfg.PortOpt, default=None, cli_args=['--foo=80'], value=80, deps=(None, None))), ('port_arg_deprecated_name', dict(opt_class=cfg.PortOpt, default=None, cli_args=['--oldfoo=80'], value=80, deps=('oldfoo', None))), ('port_arg_deprecated_group', dict(opt_class=cfg.PortOpt, default=None, cli_args=['--old-foo=80'], value=80, deps=(None, 'old'))), ('port_arg_deprecated_group_default', dict(opt_class=cfg.PortOpt, default=None, cli_args=['--foo=80'], value=80, deps=(None, 'DEFAULT'))), ('port_arg_deprecated_group_and_name', dict(opt_class=cfg.PortOpt, default=None, cli_args=['--old-oof=80'], value=80, deps=('oof', 'old'))), ('uri_default', dict(opt_class=cfg.URIOpt, default='http://example.com', cli_args=[], value='http://example.com', deps=(None, None))), ('uri_arg', dict(opt_class=cfg.URIOpt, default=None, cli_args=['--foo', 'http://example.com'], value='http://example.com', deps=(None, None))), ('multistr_default', dict(opt_class=cfg.MultiStrOpt, default=['bar'], cli_args=[], value=['bar'], deps=(None, None))), ('multistr_arg', dict(opt_class=cfg.MultiStrOpt, default=None, cli_args=['--foo', 'blaa', '--foo', 'bar'], value=['blaa', 'bar'], deps=(None, None))), ('multistr_arg_deprecated_name', dict(opt_class=cfg.MultiStrOpt, default=None, cli_args=['--oldfoo', 'blaa', '--oldfoo', 'bar'], value=['blaa', 'bar'], deps=('oldfoo', None))), ('multistr_arg_deprecated_group', dict(opt_class=cfg.MultiStrOpt, default=None, cli_args=['--old-foo', 'blaa', '--old-foo', 'bar'], value=['blaa', 'bar'], deps=(None, 'old'))), ('multistr_arg_deprecated_group_default', dict(opt_class=cfg.MultiStrOpt, default=None, cli_args=['--foo', 'blaa', '--foo', 'bar'], value=['blaa', 'bar'], deps=(None, 'DEFAULT'))), ('multistr_arg_deprecated_group_and_name', dict(opt_class=cfg.MultiStrOpt, default=None, cli_args=['--old-oof', 'blaa', '--old-oof', 'bar'], value=['blaa', 'bar'], deps=('oof', 'old'))), ('multiopt_arg_int', dict(opt_class=multi_int, default=None, cli_args=['--foo', '1', '--foo', '2'], value=[1, 2], deps=(None, None))), ('multiopt_float_int', dict(opt_class=multi_float, default=None, cli_args=['--foo', '1.2', '--foo', '2.3'], value=[1.2, 2.3], deps=(None, None))), ('multiopt_string', dict(opt_class=multi_string, default=None, cli_args=['--foo', 'bar', '--foo', 'baz'], value=["bar", "baz"], deps=(None, None))), ] def test_cli(self): self.conf.register_cli_opt( self.opt_class('foo', default=self.default, deprecated_name=self.deps[0], deprecated_group=self.deps[1])) self.conf(self.cli_args) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(self.value, self.conf.foo) class CliSpecialOptsTestCase(BaseTestCase): def test_help(self): self.useFixture(fixtures.MonkeyPatch('sys.stdout', moves.StringIO())) self.assertRaises(SystemExit, self.conf, ['--help']) self.assertIn('FOO BAR', sys.stdout.getvalue()) self.assertIn('--version', sys.stdout.getvalue()) self.assertIn('--help', sys.stdout.getvalue()) self.assertIn('--config-file', sys.stdout.getvalue()) def test_version(self): # In Python 3.4+, argparse prints the version on stdout; before 3.4, it # printed it on stderr. if sys.version_info >= (3, 4): stream_name = 'stdout' else: stream_name = 'stderr' self.useFixture(fixtures.MonkeyPatch("sys.%s" % stream_name, moves.StringIO())) self.assertRaises(SystemExit, self.conf, ['--version']) self.assertIn('1.0', getattr(sys, stream_name).getvalue()) def test_config_file(self): paths = self.create_tempfiles([('1', '[DEFAULT]'), ('2', '[DEFAULT]')]) self.conf(['--config-file', paths[0], '--config-file', paths[1]]) self.assertEqual(paths, self.conf.config_file) class PositionalTestCase(BaseTestCase): def _do_pos_test(self, opt_class, default, cli_args, value): self.conf.register_cli_opt(opt_class('foo', default=default, positional=True)) self.conf(cli_args) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(value, self.conf.foo) def test_positional_str_none_default(self): self._do_pos_test(cfg.StrOpt, None, [], None) def test_positional_str_default(self): self._do_pos_test(cfg.StrOpt, 'bar', [], 'bar') def test_positional_str_arg(self): self._do_pos_test(cfg.StrOpt, None, ['bar'], 'bar') def test_positional_int_none_default(self): self._do_pos_test(cfg.IntOpt, None, [], None) def test_positional_int_default(self): self._do_pos_test(cfg.IntOpt, 10, [], 10) def test_positional_int_arg(self): self._do_pos_test(cfg.IntOpt, None, ['20'], 20) def test_positional_float_none_default(self): self._do_pos_test(cfg.FloatOpt, None, [], None) def test_positional_float_default(self): self._do_pos_test(cfg.FloatOpt, 1.0, [], 1.0) def test_positional_float_arg(self): self._do_pos_test(cfg.FloatOpt, None, ['2.0'], 2.0) def test_positional_list_none_default(self): self._do_pos_test(cfg.ListOpt, None, [], None) def test_positional_list_empty_default(self): self._do_pos_test(cfg.ListOpt, [], [], []) def test_positional_list_default(self): self._do_pos_test(cfg.ListOpt, ['bar'], [], ['bar']) def test_positional_list_arg(self): self._do_pos_test(cfg.ListOpt, None, ['blaa,bar'], ['blaa', 'bar']) def test_positional_dict_none_default(self): self._do_pos_test(cfg.DictOpt, None, [], None) def test_positional_dict_empty_default(self): self._do_pos_test(cfg.DictOpt, {}, [], {}) def test_positional_dict_default(self): self._do_pos_test(cfg.DictOpt, {'key1': 'bar'}, [], {'key1': 'bar'}) def test_positional_dict_arg(self): self._do_pos_test(cfg.DictOpt, None, ['key1:blaa,key2:bar'], {'key1': 'blaa', 'key2': 'bar'}) def test_positional_ip_none_default(self): self._do_pos_test(cfg.IPOpt, None, [], None) def test_positional_ip_default(self): self._do_pos_test(cfg.IPOpt, '127.0.0.1', [], '127.0.0.1') def test_positional_ip_arg(self): self._do_pos_test(cfg.IPOpt, None, ['127.0.0.1'], '127.0.0.1') def test_positional_port_none_default(self): self._do_pos_test(cfg.PortOpt, None, [], None) def test_positional_port_default(self): self._do_pos_test(cfg.PortOpt, 80, [], 80) def test_positional_port_arg(self): self._do_pos_test(cfg.PortOpt, None, ['443'], 443) def test_positional_uri_default(self): self._do_pos_test(cfg.URIOpt, 'http://example.com', [], 'http://example.com') def test_positional_uri_none_default(self): self._do_pos_test(cfg.URIOpt, None, [], None) def test_positional_uri_arg(self): self._do_pos_test(cfg.URIOpt, None, ['http://example.com'], 'http://example.com') def test_positional_multistr_none_default(self): self._do_pos_test(cfg.MultiStrOpt, None, [], None) def test_positional_multistr_empty_default(self): self._do_pos_test(cfg.MultiStrOpt, [], [], []) def test_positional_multistr_default(self): self._do_pos_test(cfg.MultiStrOpt, ['bar'], [], ['bar']) def test_positional_multistr_arg(self): self._do_pos_test(cfg.MultiStrOpt, None, ['blaa', 'bar'], ['blaa', 'bar']) def test_positional_bool(self): self.assertRaises(ValueError, cfg.BoolOpt, 'foo', positional=True) def test_required_positional_opt(self): self.conf.register_cli_opt( cfg.StrOpt('foo', required=True, positional=True)) self.conf(['bar']) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('bar', self.conf.foo) def test_missing_required_cli_opt(self): self.conf.register_cli_opt( cfg.StrOpt('foo', required=True, positional=True)) self.assertRaises(cfg.RequiredOptError, self.conf, []) def test_positional_opts_order(self): self.conf.register_cli_opts(( cfg.StrOpt('command', positional=True), cfg.StrOpt('arg1', positional=True), cfg.StrOpt('arg2', positional=True)) ) self.conf(['command', 'arg1', 'arg2']) self.assertEqual('command', self.conf.command) self.assertEqual('arg1', self.conf.arg1) self.assertEqual('arg2', self.conf.arg2) def test_positional_opt_order(self): self.conf.register_cli_opt( cfg.StrOpt('command', positional=True)) self.conf.register_cli_opt( cfg.StrOpt('arg1', positional=True)) self.conf.register_cli_opt( cfg.StrOpt('arg2', positional=True)) self.conf(['command', 'arg1', 'arg2']) self.assertEqual('command', self.conf.command) self.assertEqual('arg1', self.conf.arg1) self.assertEqual('arg2', self.conf.arg2) def test_positional_opt_unregister(self): command = cfg.StrOpt('command', positional=True) arg1 = cfg.StrOpt('arg1', positional=True) arg2 = cfg.StrOpt('arg2', positional=True) self.conf.register_cli_opt(command) self.conf.register_cli_opt(arg1) self.conf.register_cli_opt(arg2) self.conf(['command', 'arg1', 'arg2']) self.assertEqual('command', self.conf.command) self.assertEqual('arg1', self.conf.arg1) self.assertEqual('arg2', self.conf.arg2) self.conf.reset() self.conf.unregister_opt(arg1) self.conf.unregister_opt(arg2) arg0 = cfg.StrOpt('arg0', positional=True) self.conf.register_cli_opt(arg0) self.conf.register_cli_opt(arg1) self.conf(['command', 'arg0', 'arg1']) self.assertEqual('command', self.conf.command) self.assertEqual('arg0', self.conf.arg0) self.assertEqual('arg1', self.conf.arg1) class ConfigFileOptsTestCase(BaseTestCase): def setUp(self): super(ConfigFileOptsTestCase, self).setUp() self.logger = self.useFixture( fixtures.FakeLogger( format='%(message)s', level=logging.WARNING, nuke_handlers=True, ) ) def _do_deprecated_test(self, opt_class, value, result, key, section='DEFAULT', dname=None, dgroup=None): self.conf.register_opt(opt_class('newfoo', deprecated_name=dname, deprecated_group=dgroup)) paths = self.create_tempfiles([('test', '[' + section + ']\n' + key + ' = ' + value + '\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'newfoo')) self.assertEqual(result, self.conf.newfoo) def _do_dname_test_use(self, opt_class, value, result): self._do_deprecated_test(opt_class, value, result, 'oldfoo', dname='oldfoo') def _do_dgroup_test_use(self, opt_class, value, result): self._do_deprecated_test(opt_class, value, result, 'newfoo', section='old', dgroup='old') def _do_default_dgroup_test_use(self, opt_class, value, result): self._do_deprecated_test(opt_class, value, result, 'newfoo', section='DEFAULT', dgroup='DEFAULT') def _do_dgroup_and_dname_test_use(self, opt_class, value, result): self._do_deprecated_test(opt_class, value, result, 'oof', section='old', dgroup='old', dname='oof') def _do_dname_test_ignore(self, opt_class, value, result): self._do_deprecated_test(opt_class, value, result, 'newfoo', dname='oldfoo') def _do_dgroup_test_ignore(self, opt_class, value, result): self._do_deprecated_test(opt_class, value, result, 'newfoo', section='DEFAULT', dgroup='old') def _do_dgroup_and_dname_test_ignore(self, opt_class, value, result): self._do_deprecated_test(opt_class, value, result, 'oof', section='old', dgroup='old', dname='oof') def test_conf_file_str_default(self): self.conf.register_opt(cfg.StrOpt('foo', default='bar')) paths = self.create_tempfiles([('test', '[DEFAULT]\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('bar', self.conf.foo) def test_conf_file_str_value(self): self.conf.register_opt(cfg.StrOpt('foo')) paths = self.create_tempfiles([('test', '[DEFAULT]\n''foo = bar\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('bar', self.conf.foo) def test_conf_file_str_value_override(self): self.conf.register_cli_opt(cfg.StrOpt('foo')) paths = self.create_tempfiles([('1', '[DEFAULT]\n' 'foo = baar\n'), ('2', '[DEFAULT]\n' 'foo = baaar\n')]) self.conf(['--foo', 'bar', '--config-file', paths[0], '--config-file', paths[1]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('baaar', self.conf.foo) def test_conf_file_str_value_override_use_deprecated(self): """last option should always win, even if last uses deprecated.""" self.conf.register_cli_opt( cfg.StrOpt('newfoo', deprecated_name='oldfoo')) paths = self.create_tempfiles([('0', '[DEFAULT]\n' 'newfoo = middle\n'), ('1', '[DEFAULT]\n' 'oldfoo = last\n')]) self.conf(['--newfoo', 'first', '--config-file', paths[0], '--config-file', paths[1]]) self.assertTrue(hasattr(self.conf, 'newfoo')) self.assertTrue(hasattr(self.conf, 'oldfoo')) self.assertEqual('last', self.conf.newfoo) log_out = self.logger.output self.assertIn('is deprecated', log_out) self.assertIn('Use option "newfoo"', log_out) def test_use_deprecated_for_removal_without_reason(self): self.conf.register_cli_opt( cfg.StrOpt('oldfoo', deprecated_for_removal=True)) paths = self.create_tempfiles([('0', '[DEFAULT]\n' 'oldfoo = middle\n')]) self.conf(['--oldfoo', 'first', '--config-file', paths[0]]) log_out = self.logger.output self.assertIn('deprecated for removal.', log_out) def test_use_deprecated_for_removal_with_reason(self): self.conf.register_cli_opt( cfg.StrOpt('oldfoo', deprecated_for_removal=True, deprecated_reason='a very good reason')) paths = self.create_tempfiles([('0', '[DEFAULT]\n' 'oldfoo = middle\n')]) self.conf(['--oldfoo', 'first', '--config-file', paths[0]]) log_out = self.logger.output self.assertIn('deprecated for removal (a very good reason).', log_out) def test_conf_file_str_use_dname(self): self._do_dname_test_use(cfg.StrOpt, 'value1', 'value1') def test_conf_file_str_use_dgroup(self): self._do_dgroup_test_use(cfg.StrOpt, 'value1', 'value1') def test_conf_file_str_use_default_dgroup(self): self._do_default_dgroup_test_use(cfg.StrOpt, 'value1', 'value1') def test_conf_file_str_use_dgroup_and_dname(self): self._do_dgroup_and_dname_test_use(cfg.StrOpt, 'value1', 'value1') def test_conf_file_str_ignore_dname(self): self._do_dname_test_ignore(cfg.StrOpt, 'value2', 'value2') def test_conf_file_str_ignore_dgroup(self): self._do_dgroup_test_ignore(cfg.StrOpt, 'value2', 'value2') def test_conf_file_str_ignore_dgroup_and_dname(self): self._do_dgroup_and_dname_test_ignore(cfg.StrOpt, 'value2', 'value2') def test_conf_file_str_value_with_good_choice_value(self): self.conf.register_opt(cfg.StrOpt('foo', choices=['bar', 'baz'])) paths = self.create_tempfiles([('test', '[DEFAULT]\n''foo = bar\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('bar', self.conf.foo) def test_conf_file_bool_default_none(self): self.conf.register_opt(cfg.BoolOpt('foo')) paths = self.create_tempfiles([('test', '[DEFAULT]\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertIsNone(self.conf.foo) def test_conf_file_bool_default_false(self): self.conf.register_opt(cfg.BoolOpt('foo', default=False)) paths = self.create_tempfiles([('test', '[DEFAULT]\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertFalse(self.conf.foo) def test_conf_file_bool_value(self): self.conf.register_opt(cfg.BoolOpt('foo')) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = true\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertTrue(self.conf.foo) def test_conf_file_bool_cli_value_override(self): self.conf.register_cli_opt(cfg.BoolOpt('foo')) paths = self.create_tempfiles([('1', '[DEFAULT]\n' 'foo = 0\n')]) self.conf(['--foo', '--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertFalse(self.conf.foo) def test_conf_file_bool_cli_inverse_override(self): self.conf.register_cli_opt(cfg.BoolOpt('foo')) paths = self.create_tempfiles([('1', '[DEFAULT]\n' 'foo = true\n')]) self.conf(['--nofoo', '--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertTrue(self.conf.foo) def test_conf_file_bool_cli_order_override(self): self.conf.register_cli_opt(cfg.BoolOpt('foo')) paths = self.create_tempfiles([('1', '[DEFAULT]\n' 'foo = false\n')]) self.conf(['--config-file', paths[0], '--foo']) self.assertTrue(hasattr(self.conf, 'foo')) self.assertTrue(self.conf.foo) def test_conf_file_bool_file_value_override(self): self.conf.register_cli_opt(cfg.BoolOpt('foo')) paths = self.create_tempfiles([('1', '[DEFAULT]\n' 'foo = 0\n'), ('2', '[DEFAULT]\n' 'foo = yes\n')]) self.conf(['--config-file', paths[0], '--config-file', paths[1]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertTrue(self.conf.foo) def test_conf_file_bool_use_dname(self): self._do_dname_test_use(cfg.BoolOpt, 'yes', True) def test_conf_file_bool_use_dgroup(self): self._do_dgroup_test_use(cfg.BoolOpt, 'yes', True) def test_conf_file_bool_use_default_dgroup(self): self._do_default_dgroup_test_use(cfg.BoolOpt, 'yes', True) def test_conf_file_bool_use_dgroup_and_dname(self): self._do_dgroup_and_dname_test_use(cfg.BoolOpt, 'yes', True) def test_conf_file_bool_ignore_dname(self): self._do_dname_test_ignore(cfg.BoolOpt, 'no', False) def test_conf_file_bool_ignore_dgroup(self): self._do_dgroup_test_ignore(cfg.BoolOpt, 'no', False) def test_conf_file_bool_ignore_group_and_dname(self): self._do_dgroup_and_dname_test_ignore(cfg.BoolOpt, 'no', False) def test_conf_file_int_default(self): self.conf.register_opt(cfg.IntOpt('foo', default=666)) paths = self.create_tempfiles([('test', '[DEFAULT]\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(666, self.conf.foo) def test_conf_file_int_string_default_type(self): self.conf.register_opt(cfg.IntOpt('foo', default='666')) self.conf([]) self.assertEqual(666, self.conf.foo) def test_conf_file_int_value(self): self.conf.register_opt(cfg.IntOpt('foo')) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 666\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(666, self.conf.foo) def test_conf_file_int_value_override(self): self.conf.register_cli_opt(cfg.IntOpt('foo')) paths = self.create_tempfiles([('1', '[DEFAULT]\n' 'foo = 66\n'), ('2', '[DEFAULT]\n' 'foo = 666\n')]) self.conf(['--foo', '6', '--config-file', paths[0], '--config-file', paths[1]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(666, self.conf.foo) def test_conf_file_int_min_max(self): self.conf.register_opt(cfg.IntOpt('foo', min=1, max=5)) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 10\n')]) self.conf(['--config-file', paths[0]]) self.assertRaises(cfg.ConfigFileValueError, self.conf._get, 'foo') def test_conf_file_int_min_greater_max(self): self.assertRaises(ValueError, cfg.IntOpt, 'foo', min=5, max=1) def test_conf_file_int_use_dname(self): self._do_dname_test_use(cfg.IntOpt, '66', 66) def test_conf_file_int_use_dgroup(self): self._do_dgroup_test_use(cfg.IntOpt, '66', 66) def test_conf_file_int_use_default_dgroup(self): self._do_default_dgroup_test_use(cfg.IntOpt, '66', 66) def test_conf_file_int_use_dgroup_and_dname(self): self._do_dgroup_and_dname_test_use(cfg.IntOpt, '66', 66) def test_conf_file_int_ignore_dname(self): self._do_dname_test_ignore(cfg.IntOpt, '64', 64) def test_conf_file_int_ignore_dgroup(self): self._do_dgroup_test_ignore(cfg.IntOpt, '64', 64) def test_conf_file_int_ignore_dgroup_and_dname(self): self._do_dgroup_and_dname_test_ignore(cfg.IntOpt, '64', 64) def test_conf_file_float_default(self): self.conf.register_opt(cfg.FloatOpt('foo', default=6.66)) paths = self.create_tempfiles([('test', '[DEFAULT]\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(6.66, self.conf.foo) def test_conf_file_float_default_wrong_type(self): self.assertRaises(cfg.DefaultValueError, cfg.FloatOpt, 'foo', default='foobar6.66') def test_conf_file_float_value(self): self.conf.register_opt(cfg.FloatOpt('foo')) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 6.66\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(6.66, self.conf.foo) def test_conf_file_float_value_override(self): self.conf.register_cli_opt(cfg.FloatOpt('foo')) paths = self.create_tempfiles([('1', '[DEFAULT]\n' 'foo = 6.6\n'), ('2', '[DEFAULT]\n' 'foo = 6.66\n')]) self.conf(['--foo', '6', '--config-file', paths[0], '--config-file', paths[1]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(6.66, self.conf.foo) def test_conf_file_float_use_dname(self): self._do_dname_test_use(cfg.FloatOpt, '66.54', 66.54) def test_conf_file_float_use_dgroup(self): self._do_dgroup_test_use(cfg.FloatOpt, '66.54', 66.54) def test_conf_file_float_use_default_dgroup(self): self._do_default_dgroup_test_use(cfg.FloatOpt, '66.54', 66.54) def test_conf_file_float_use_dgroup_and_dname(self): self._do_dgroup_and_dname_test_use(cfg.FloatOpt, '66.54', 66.54) def test_conf_file_float_ignore_dname(self): self._do_dname_test_ignore(cfg.FloatOpt, '64.54', 64.54) def test_conf_file_float_ignore_dgroup(self): self._do_dgroup_test_ignore(cfg.FloatOpt, '64.54', 64.54) def test_conf_file_float_ignore_dgroup_and_dname(self): self._do_dgroup_and_dname_test_ignore(cfg.FloatOpt, '64.54', 64.54) def test_conf_file_float_min_max_above_max(self): self.conf.register_opt(cfg.FloatOpt('foo', min=1.1, max=5.5)) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 10.5\n')]) self.conf(['--config-file', paths[0]]) self.assertRaises(cfg.ConfigFileValueError, self.conf._get, 'foo') def test_conf_file_float_only_max_above_max(self): self.conf.register_opt(cfg.FloatOpt('foo', max=5.5)) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 10.5\n')]) self.conf(['--config-file', paths[0]]) self.assertRaises(cfg.ConfigFileValueError, self.conf._get, 'foo') def test_conf_file_float_min_max_below_min(self): self.conf.register_opt(cfg.FloatOpt('foo', min=1.1, max=5.5)) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 0.5\n')]) self.conf(['--config-file', paths[0]]) self.assertRaises(cfg.ConfigFileValueError, self.conf._get, 'foo') def test_conf_file_float_only_min_below_min(self): self.conf.register_opt(cfg.FloatOpt('foo', min=1.1)) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 0.5\n')]) self.conf(['--config-file', paths[0]]) self.assertRaises(cfg.ConfigFileValueError, self.conf._get, 'foo') def test_conf_file_float_min_max_in_range(self): self.conf.register_opt(cfg.FloatOpt('foo', min=1.1, max=5.5)) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 4.5\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(4.5, self.conf.foo) def test_conf_file_float_only_max_in_range(self): self.conf.register_opt(cfg.FloatOpt('foo', max=5.5)) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 4.5\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(4.5, self.conf.foo) def test_conf_file_float_only_min_in_range(self): self.conf.register_opt(cfg.FloatOpt('foo', min=3.5)) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 4.5\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(4.5, self.conf.foo) def test_conf_file_float_min_greater_max(self): self.assertRaises(ValueError, cfg.FloatOpt, 'foo', min=5.5, max=1.5) def test_conf_file_list_default(self): self.conf.register_opt(cfg.ListOpt('foo', default=['bar'])) paths = self.create_tempfiles([('test', '[DEFAULT]\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(['bar'], self.conf.foo) def test_conf_file_list_default_wrong_type(self): self.assertRaises(cfg.DefaultValueError, cfg.ListOpt, 'foo', default=25) def test_conf_file_list_value(self): self.conf.register_opt(cfg.ListOpt('foo')) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = bar\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(['bar'], self.conf.foo) def test_conf_file_list_value_override(self): self.conf.register_cli_opt(cfg.ListOpt('foo')) paths = self.create_tempfiles([('1', '[DEFAULT]\n' 'foo = bar,bar\n'), ('2', '[DEFAULT]\n' 'foo = b,a,r\n')]) self.conf(['--foo', 'bar', '--config-file', paths[0], '--config-file', paths[1]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(['b', 'a', 'r'], self.conf.foo) def test_conf_file_list_item_type(self): self.conf.register_cli_opt(cfg.ListOpt('foo', item_type=types.Integer())) paths = self.create_tempfiles([('1', '[DEFAULT]\n' 'foo = 1,2\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual([1, 2], self.conf.foo) def test_conf_file_list_item_wrong_type(self): self.assertRaises(cfg.DefaultValueError, cfg.ListOpt, 'foo', default="bar", item_type=types.Integer()) def test_conf_file_list_bounds(self): self.conf.register_cli_opt(cfg.ListOpt('foo', item_type=types.Integer(), default="[1,2]", bounds=True)) self.conf([]) self.assertEqual([1, 2], self.conf.foo) def test_conf_file_list_use_dname(self): self._do_dname_test_use(cfg.ListOpt, 'a,b,c', ['a', 'b', 'c']) def test_conf_file_list_use_dgroup(self): self._do_dgroup_test_use(cfg.ListOpt, 'a,b,c', ['a', 'b', 'c']) def test_conf_file_list_use_default_dgroup(self): self._do_default_dgroup_test_use(cfg.ListOpt, 'a,b,c', ['a', 'b', 'c']) def test_conf_file_list_use_dgroup_and_dname(self): self._do_dgroup_and_dname_test_use(cfg.ListOpt, 'a,b,c', ['a', 'b', 'c']) def test_conf_file_list_ignore_dname(self): self._do_dname_test_ignore(cfg.ListOpt, 'd,e,f', ['d', 'e', 'f']) def test_conf_file_list_ignore_dgroup(self): self._do_dgroup_test_ignore(cfg.ListOpt, 'd,e,f', ['d', 'e', 'f']) def test_conf_file_list_ignore_dgroup_and_dname(self): self._do_dgroup_and_dname_test_ignore( cfg.ListOpt, 'd,e,f', ['d', 'e', 'f']) def test_conf_file_list_spaces_use_dname(self): self._do_dname_test_use(cfg.ListOpt, 'a, b, c', ['a', 'b', 'c']) def test_conf_file_list_spaces_use_dgroup(self): self._do_dgroup_test_use(cfg.ListOpt, 'a, b, c', ['a', 'b', 'c']) def test_conf_file_list_spaces_use_default_dgroup(self): self._do_default_dgroup_test_use( cfg.ListOpt, 'a, b, c', ['a', 'b', 'c']) def test_conf_file_list_spaces_use_dgroup_and_dname(self): self._do_dgroup_and_dname_test_use( cfg.ListOpt, 'a, b, c', ['a', 'b', 'c']) def test_conf_file_list_spaces_ignore_dname(self): self._do_dname_test_ignore(cfg.ListOpt, 'd, e, f', ['d', 'e', 'f']) def test_conf_file_list_spaces_ignore_dgroup(self): self._do_dgroup_test_ignore(cfg.ListOpt, 'd, e, f', ['d', 'e', 'f']) def test_conf_file_list_spaces_ignore_dgroup_and_dname(self): self._do_dgroup_and_dname_test_ignore(cfg.ListOpt, 'd, e, f', ['d', 'e', 'f']) def test_conf_file_dict_default(self): self.conf.register_opt(cfg.DictOpt('foo', default={'key': 'bar'})) paths = self.create_tempfiles([('test', '[DEFAULT]\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual({'key': 'bar'}, self.conf.foo) def test_conf_file_dict_value(self): self.conf.register_opt(cfg.DictOpt('foo')) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = key:bar\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual({'key': 'bar'}, self.conf.foo) def test_conf_file_dict_colon_in_value(self): self.conf.register_opt(cfg.DictOpt('foo')) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = key:bar:baz\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual({'key': 'bar:baz'}, self.conf.foo) def test_conf_file_dict_value_no_colon(self): self.conf.register_opt(cfg.DictOpt('foo')) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = key:bar,baz\n')]) self.conf(['--config-file', paths[0]]) self.assertRaises(cfg.ConfigFileValueError, self.conf._get, 'foo') self.assertRaises(ValueError, getattr, self.conf, 'foo') def test_conf_file_dict_value_duplicate_key(self): self.conf.register_opt(cfg.DictOpt('foo')) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = key:bar,key:baz\n')]) self.conf(['--config-file', paths[0]]) self.assertRaises(cfg.ConfigFileValueError, self.conf._get, 'foo') self.assertRaises(ValueError, getattr, self.conf, 'foo') def test_conf_file_dict_values_override_deprecated(self): self.conf.register_cli_opt(cfg.DictOpt('foo', deprecated_name='oldfoo')) paths = self.create_tempfiles([('1', '[DEFAULT]\n' 'foo = key1:bar1\n'), ('2', '[DEFAULT]\n' 'oldfoo = key2:bar2\n' 'oldfoo = key3:bar3\n')]) self.conf(['--foo', 'key0:bar0', '--config-file', paths[0], '--config-file', paths[1]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual({'key3': 'bar3'}, self.conf.foo) def test_conf_file_dict_deprecated(self): self.conf.register_opt(cfg.DictOpt('newfoo', deprecated_name='oldfoo')) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'oldfoo= key1:bar1\n' 'oldfoo = key2:bar2\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'newfoo')) self.assertEqual({'key2': 'bar2'}, self.conf.newfoo) def test_conf_file_dict_value_override(self): self.conf.register_cli_opt(cfg.DictOpt('foo')) paths = self.create_tempfiles([('1', '[DEFAULT]\n' 'foo = key:bar,key2:bar\n'), ('2', '[DEFAULT]\n' 'foo = k1:v1,k2:v2\n')]) self.conf(['--foo', 'x:y,x2:y2', '--config-file', paths[0], '--config-file', paths[1]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual({'k1': 'v1', 'k2': 'v2'}, self.conf.foo) def test_conf_file_dict_use_dname(self): self._do_dname_test_use(cfg.DictOpt, 'k1:a,k2:b,k3:c', {'k1': 'a', 'k2': 'b', 'k3': 'c'}) def test_conf_file_dict_use_dgroup(self): self._do_dgroup_test_use(cfg.DictOpt, 'k1:a,k2:b,k3:c', {'k1': 'a', 'k2': 'b', 'k3': 'c'}) def test_conf_file_dict_use_default_dgroup(self): self._do_default_dgroup_test_use(cfg.DictOpt, 'k1:a,k2:b,k3:c', {'k1': 'a', 'k2': 'b', 'k3': 'c'}) def test_conf_file_dict_use_dgroup_and_dname(self): self._do_dgroup_and_dname_test_use(cfg.DictOpt, 'k1:a,k2:b,k3:c', {'k1': 'a', 'k2': 'b', 'k3': 'c'}) def test_conf_file_dict_ignore_dname(self): self._do_dname_test_ignore(cfg.DictOpt, 'k1:d,k2:e,k3:f', {'k1': 'd', 'k2': 'e', 'k3': 'f'}) def test_conf_file_dict_ignore_dgroup(self): self._do_dgroup_test_ignore(cfg.DictOpt, 'k1:d,k2:e,k3:f', {'k1': 'd', 'k2': 'e', 'k3': 'f'}) def test_conf_file_dict_ignore_dgroup_and_dname(self): self._do_dgroup_and_dname_test_ignore(cfg.DictOpt, 'k1:d,k2:e,k3:f', {'k1': 'd', 'k2': 'e', 'k3': 'f'}) def test_conf_file_dict_spaces_use_dname(self): self._do_dname_test_use(cfg.DictOpt, 'k1:a,k2:b,k3:c', {'k1': 'a', 'k2': 'b', 'k3': 'c'}) def test_conf_file_dict_spaces_use_dgroup(self): self._do_dgroup_test_use(cfg.DictOpt, 'k1:a,k2:b,k3:c', {'k1': 'a', 'k2': 'b', 'k3': 'c'}) def test_conf_file_dict_spaces_use_default_dgroup(self): self._do_default_dgroup_test_use(cfg.DictOpt, 'k1:a,k2:b,k3:c', {'k1': 'a', 'k2': 'b', 'k3': 'c'}) def test_conf_file_dict_spaces_use_dgroup_and_dname(self): self._do_dgroup_and_dname_test_use(cfg.DictOpt, 'k1:a,k2:b,k3:c', {'k1': 'a', 'k2': 'b', 'k3': 'c'}) def test_conf_file_dict_spaces_ignore_dname(self): self._do_dname_test_ignore(cfg.DictOpt, 'k1:d,k2:e,k3:f', {'k1': 'd', 'k2': 'e', 'k3': 'f'}) def test_conf_file_dict_spaces_ignore_dgroup(self): self._do_dgroup_test_ignore(cfg.DictOpt, 'k1:d,k2:e,k3:f', {'k1': 'd', 'k2': 'e', 'k3': 'f'}) def test_conf_file_dict_spaces_ignore_dgroup_and_dname(self): self._do_dgroup_and_dname_test_ignore(cfg.DictOpt, 'k1:d,k2:e,k3:f', {'k1': 'd', 'k2': 'e', 'k3': 'f'}) def test_conf_file_port_outside_range(self): self.conf.register_opt(cfg.PortOpt('foo')) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 65536\n')]) self.conf(['--config-file', paths[0]]) self.assertRaises(cfg.ConfigFileValueError, self.conf._get, 'foo') def test_conf_file_port_list(self): self.conf.register_opt(cfg.ListOpt('foo', item_type=types.Port())) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 22, 80\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual([22, 80], self.conf.foo) def test_conf_file_port_list_default(self): self.conf.register_opt(cfg.ListOpt('foo', item_type=types.Port(), default=[55, 77])) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 22, 80\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual([22, 80], self.conf.foo) def test_conf_file_port_list_only_default(self): self.conf.register_opt(cfg.ListOpt('foo', item_type=types.Port(), default=[55, 77])) paths = self.create_tempfiles([('test', '[DEFAULT]\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual([55, 77], self.conf.foo) def test_conf_file_port_list_outside_range(self): self.conf.register_opt(cfg.ListOpt('foo', item_type=types.Port())) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 1,65536\n')]) self.conf(['--config-file', paths[0]]) self.assertRaises(cfg.ConfigFileValueError, self.conf._get, 'foo') def test_conf_file_port_min_max_above_max(self): self.conf.register_opt(cfg.PortOpt('foo', min=1, max=5)) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 10\n')]) self.conf(['--config-file', paths[0]]) self.assertRaises(cfg.ConfigFileValueError, self.conf._get, 'foo') def test_conf_file_port_only_max_above_max(self): self.conf.register_opt(cfg.PortOpt('foo', max=500)) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 600\n')]) self.conf(['--config-file', paths[0]]) self.assertRaises(cfg.ConfigFileValueError, self.conf._get, 'foo') def test_conf_file_port_min_max_below_min(self): self.conf.register_opt(cfg.PortOpt('foo', min=100, max=500)) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 99\n')]) self.conf(['--config-file', paths[0]]) self.assertRaises(cfg.ConfigFileValueError, self.conf._get, 'foo') def test_conf_file_port_only_min_below_min(self): self.conf.register_opt(cfg.PortOpt('foo', min=1025)) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 1024\n')]) self.conf(['--config-file', paths[0]]) self.assertRaises(cfg.ConfigFileValueError, self.conf._get, 'foo') def test_conf_file_port_min_max_in_range(self): self.conf.register_opt(cfg.PortOpt('foo', min=1025, max=6000)) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 2500\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(2500, self.conf.foo) def test_conf_file_port_only_max_in_range(self): self.conf.register_opt(cfg.PortOpt('foo', max=5000)) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 45\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(45, self.conf.foo) def test_conf_file_port_only_min_in_range(self): self.conf.register_opt(cfg.PortOpt('foo', min=35)) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 45\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(45, self.conf.foo) def test_conf_file_port_min_greater_max(self): self.assertRaises(ValueError, cfg.PortOpt, 'foo', min=55, max=15) def test_conf_file_multistr_default(self): self.conf.register_opt(cfg.MultiStrOpt('foo', default=['bar'])) paths = self.create_tempfiles([('test', '[DEFAULT]\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(['bar'], self.conf.foo) def test_conf_file_multistr_value(self): self.conf.register_opt(cfg.MultiStrOpt('foo')) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = bar\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(['bar'], self.conf.foo) def test_conf_file_multistr_values_append_deprecated(self): self.conf.register_cli_opt(cfg.MultiStrOpt('foo', deprecated_name='oldfoo')) paths = self.create_tempfiles([('1', '[DEFAULT]\n' 'foo = bar1\n'), ('2', '[DEFAULT]\n' 'oldfoo = bar2\n' 'oldfoo = bar3\n')]) self.conf(['--foo', 'bar0', '--config-file', paths[0], '--config-file', paths[1]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(['bar0', 'bar1', 'bar2', 'bar3'], self.conf.foo) def test_conf_file_multistr_values_append(self): self.conf.register_cli_opt(cfg.MultiStrOpt('foo')) paths = self.create_tempfiles([('1', '[DEFAULT]\n' 'foo = bar1\n'), ('2', '[DEFAULT]\n' 'foo = bar2\n' 'foo = bar3\n')]) self.conf(['--foo', 'bar0', '--config-file', paths[0], '--config-file', paths[1]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(['bar0', 'bar1', 'bar2', 'bar3'], self.conf.foo) def test_conf_file_multistr_deprecated(self): self.conf.register_opt( cfg.MultiStrOpt('newfoo', deprecated_name='oldfoo')) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'oldfoo= bar1\n' 'oldfoo = bar2\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'newfoo')) self.assertEqual(['bar1', 'bar2'], self.conf.newfoo) def test_conf_file_multiple_opts(self): self.conf.register_opts([cfg.StrOpt('foo'), cfg.StrOpt('bar')]) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = bar\n' 'bar = foo\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('bar', self.conf.foo) self.assertTrue(hasattr(self.conf, 'bar')) self.assertEqual('foo', self.conf.bar) def test_conf_file_raw_value(self): self.conf.register_opt(cfg.StrOpt('foo')) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = bar-%08x\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('bar-%08x', self.conf.foo) def test_conf_file_sorted_group(self): # Create enough groups for the sorted problem to appear for i in range(10): group = cfg.OptGroup('group%s' % i, 'options') self.conf.register_group(group) self.conf.register_cli_opt(cfg.StrOpt('opt1'), group=group) paths = self.create_tempfiles( [('test', '[group1]\nopt1 = foo\n[group2]\nopt2 = bar\n')]) self.conf(['--config-file', paths[0]]) self.assertEqual('foo', self.conf.group1.opt1) class ConfigFileReloadTestCase(BaseTestCase): def test_conf_files_reload(self): self.conf.register_cli_opt(cfg.StrOpt('foo')) paths = self.create_tempfiles([('1', '[DEFAULT]\n' 'foo = baar\n'), ('2', '[DEFAULT]\n' 'foo = baaar\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('baar', self.conf.foo) shutil.copy(paths[1], paths[0]) self.conf.reload_config_files() self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('baaar', self.conf.foo) def test_conf_files_reload_default(self): self.conf.register_cli_opt(cfg.StrOpt('foo1')) self.conf.register_cli_opt(cfg.StrOpt('foo2')) paths = self.create_tempfiles([('1', '[DEFAULT]\n' 'foo1 = default1\n'), ('2', '[DEFAULT]\n' 'foo2 = default2\n')]) paths_change = self.create_tempfiles([('1', '[DEFAULT]\n' 'foo1 = change_default1\n'), ('2', '[DEFAULT]\n' 'foo2 = change_default2\n')]) self.conf(args=[], default_config_files=paths) self.assertTrue(hasattr(self.conf, 'foo1')) self.assertEqual('default1', self.conf.foo1) self.assertTrue(hasattr(self.conf, 'foo2')) self.assertEqual('default2', self.conf.foo2) shutil.copy(paths_change[0], paths[0]) shutil.copy(paths_change[1], paths[1]) self.conf.reload_config_files() self.assertTrue(hasattr(self.conf, 'foo1')) self.assertEqual('change_default1', self.conf.foo1) self.assertTrue(hasattr(self.conf, 'foo2')) self.assertEqual('change_default2', self.conf.foo2) def test_conf_files_reload_file_not_found(self): self.conf.register_cli_opt(cfg.StrOpt('foo', required=True)) paths = self.create_tempfiles([('1', '[DEFAULT]\n' 'foo = baar\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('baar', self.conf.foo) os.remove(paths[0]) self.conf.reload_config_files() self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('baar', self.conf.foo) def test_conf_files_reload_error(self): self.conf.register_cli_opt(cfg.StrOpt('foo', required=True)) self.conf.register_cli_opt(cfg.StrOpt('foo1', required=True)) paths = self.create_tempfiles([('1', '[DEFAULT]\n' 'foo = test1\n' 'foo1 = test11\n'), ('2', '[DEFAULT]\n' 'foo2 = test2\n' 'foo3 = test22\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('test1', self.conf.foo) self.assertTrue(hasattr(self.conf, 'foo1')) self.assertEqual('test11', self.conf.foo1) shutil.copy(paths[1], paths[0]) self.conf.reload_config_files() self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('test1', self.conf.foo) self.assertTrue(hasattr(self.conf, 'foo1')) self.assertEqual('test11', self.conf.foo1) class ConfigFileMutateTestCase(BaseTestCase): def setUp(self): super(ConfigFileMutateTestCase, self).setUp() self.my_group = cfg.OptGroup('group', 'group options') self.conf.register_group(self.my_group) def _test_conf_files_mutate(self): paths = self.create_tempfiles([ ('1', '[DEFAULT]\n' 'foo = old_foo\n' '[group]\n' 'boo = old_boo\n'), ('2', '[DEFAULT]\n' 'foo = new_foo\n' '[group]\n' 'boo = new_boo\n')]) self.conf(['--config-file', paths[0]]) shutil.copy(paths[1], paths[0]) return self.conf.mutate_config_files() def test_conf_files_mutate_none(self): """Test that immutable opts are not reloaded""" self.conf.register_cli_opt(cfg.StrOpt('foo')) self._test_conf_files_mutate() self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('old_foo', self.conf.foo) def test_conf_files_mutate_foo(self): """Test that a mutable opt can be reloaded.""" self.conf.register_cli_opt(cfg.StrOpt('foo', mutable=True)) self._test_conf_files_mutate() self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('new_foo', self.conf.foo) def test_conf_files_mutate_group(self): """Test that mutable opts in groups can be reloaded.""" self.conf.register_cli_opt(cfg.StrOpt('boo', mutable=True), group=self.my_group) self._test_conf_files_mutate() self.assertTrue(hasattr(self.conf, 'group')) self.assertTrue(hasattr(self.conf.group, 'boo')) self.assertEqual('new_boo', self.conf.group.boo) def test_warn_immutability(self): self.log_fixture = self.useFixture(fixtures.FakeLogger()) self.conf.register_cli_opt(cfg.StrOpt('foo', mutable=True)) self.conf.register_cli_opt(cfg.StrOpt('boo'), group=self.my_group) self._test_conf_files_mutate() self.assertEqual( "Ignoring change to immutable option group.boo\n" "Option DEFAULT.foo changed from [old_foo] to [new_foo]\n", self.log_fixture.output) def test_diff(self): self.log_fixture = self.useFixture(fixtures.FakeLogger()) self.conf.register_cli_opt(cfg.StrOpt('imm')) self.conf.register_cli_opt(cfg.StrOpt('blank', mutable=True)) self.conf.register_cli_opt(cfg.StrOpt('foo', mutable=True)) self.conf.register_cli_opt(cfg.StrOpt('boo', mutable=True), group=self.my_group) diff = self._test_conf_files_mutate() self.assertEqual( {(None, 'foo'): ('old_foo', 'new_foo'), ('group', 'boo'): ('old_boo', 'new_boo')}, diff) expected = ("Option DEFAULT.foo changed from [old_foo] to [new_foo]\n" "Option group.boo changed from [old_boo] to [new_boo]\n") self.assertEqual(expected, self.log_fixture.output) def test_hooks_invoked_once(self): fresh = {} result = [0] def foo(conf, foo_fresh): self.assertEqual(conf, self.conf) self.assertEqual(fresh, foo_fresh) result[0] += 1 self.conf.register_mutate_hook(foo) self.conf.register_mutate_hook(foo) self._test_conf_files_mutate() self.assertEqual(1, result[0]) def test_hooks_see_new_values(self): def foo(conf, fresh): # Verify that we see the new value inside the mutate hook. self.assertEqual('new_foo', conf.foo) self.conf.register_cli_opt(cfg.StrOpt('foo', mutable=True)) self.conf.register_mutate_hook(foo) paths = self.create_tempfiles([ ('1', '[DEFAULT]\n' 'foo = old_foo\n' '[group]\n' 'boo = old_boo\n'), ('2', '[DEFAULT]\n' 'foo = new_foo\n' '[group]\n' 'boo = new_boo\n')]) self.conf(['--config-file', paths[0]]) # We access the value once before mutating it to ensure the # cache is populated. self.assertEqual('old_foo', self.conf.foo) shutil.copy(paths[1], paths[0]) self.conf.mutate_config_files() # Verify that we see the new value after mutation is complete. self.assertEqual('new_foo', self.conf.foo) def test_clear(self): """Show that #clear doesn't undeclare opts. This justifies not clearing mutate_hooks either. ResetAndClearTestCase shows that values are cleared. """ self.conf.register_cli_opt(cfg.StrOpt('cli')) self.conf.register_opt(cfg.StrOpt('foo')) dests = [info['opt'].dest for info, _ in self.conf._all_opt_infos()] self.assertIn('cli', dests) self.assertIn('foo', dests) self.conf.clear() dests = [info['opt'].dest for info, _ in self.conf._all_opt_infos()] self.assertIn('cli', dests) self.assertIn('foo', dests) class OptGroupsTestCase(BaseTestCase): def test_arg_group(self): blaa_group = cfg.OptGroup('blaa', 'blaa options') self.conf.register_group(blaa_group) self.conf.register_cli_opt(cfg.StrOpt('foo'), group=blaa_group) self.conf(['--blaa-foo', 'bar']) self.assertTrue(hasattr(self.conf, 'blaa')) self.assertTrue(hasattr(self.conf.blaa, 'foo')) self.assertEqual('bar', self.conf.blaa.foo) self.assertEqual('blaa', str(blaa_group)) def test_autocreate_group_by_name(self): self.conf.register_cli_opt(cfg.StrOpt('foo'), group='blaa') self.conf(['--blaa-foo', 'bar']) self.assertTrue(hasattr(self.conf, 'blaa')) self.assertTrue(hasattr(self.conf.blaa, 'foo')) self.assertEqual('bar', self.conf.blaa.foo) def test_autocreate_group_by_group(self): group = cfg.OptGroup(name='blaa', title='Blaa options') self.conf.register_cli_opt(cfg.StrOpt('foo'), group=group) self.conf(['--blaa-foo', 'bar']) self.assertTrue(hasattr(self.conf, 'blaa')) self.assertTrue(hasattr(self.conf.blaa, 'foo')) self.assertEqual('bar', self.conf.blaa.foo) def test_autocreate_title(self): blaa_group = cfg.OptGroup('blaa') self.assertEqual(blaa_group.title, 'blaa options') def test_arg_group_by_name(self): self.conf.register_group(cfg.OptGroup('blaa')) self.conf.register_cli_opt(cfg.StrOpt('foo'), group='blaa') self.conf(['--blaa-foo', 'bar']) self.assertTrue(hasattr(self.conf, 'blaa')) self.assertTrue(hasattr(self.conf.blaa, 'foo')) self.assertEqual('bar', self.conf.blaa.foo) def test_arg_group_with_default(self): self.conf.register_group(cfg.OptGroup('blaa')) self.conf.register_cli_opt( cfg.StrOpt('foo', default='bar'), group='blaa') self.conf([]) self.assertTrue(hasattr(self.conf, 'blaa')) self.assertTrue(hasattr(self.conf.blaa, 'foo')) self.assertEqual('bar', self.conf.blaa.foo) def test_arg_group_with_conf_and_group_opts(self): self.conf.register_cli_opt(cfg.StrOpt('conf'), group='blaa') self.conf.register_cli_opt(cfg.StrOpt('group'), group='blaa') self.conf(['--blaa-conf', 'foo', '--blaa-group', 'bar']) self.assertTrue(hasattr(self.conf, 'blaa')) self.assertTrue(hasattr(self.conf.blaa, 'conf')) self.assertEqual('foo', self.conf.blaa.conf) self.assertTrue(hasattr(self.conf.blaa, 'group')) self.assertEqual('bar', self.conf.blaa.group) def test_arg_group_in_config_file(self): self.conf.register_group(cfg.OptGroup('blaa')) self.conf.register_opt(cfg.StrOpt('foo'), group='blaa') paths = self.create_tempfiles([('test', '[blaa]\n' 'foo = bar\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'blaa')) self.assertTrue(hasattr(self.conf.blaa, 'foo')) self.assertEqual('bar', self.conf.blaa.foo) def test_arg_group_in_config_file_with_deprecated_name(self): self.conf.register_group(cfg.OptGroup('blaa')) self.conf.register_opt(cfg.StrOpt('foo', deprecated_name='oldfoo'), group='blaa') paths = self.create_tempfiles([('test', '[blaa]\n' 'oldfoo = bar\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'blaa')) self.assertTrue(hasattr(self.conf.blaa, 'foo')) self.assertEqual('bar', self.conf.blaa.foo) def test_arg_group_in_config_file_with_deprecated_group(self): self.conf.register_group(cfg.OptGroup('blaa')) self.conf.register_opt(cfg.StrOpt('foo', deprecated_group='DEFAULT'), group='blaa') paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = bar\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'blaa')) self.assertTrue(hasattr(self.conf.blaa, 'foo')) self.assertEqual('bar', self.conf.blaa.foo) def test_arg_group_in_config_file_with_deprecated_group_and_name(self): self.conf.register_group(cfg.OptGroup('blaa')) self.conf.register_opt( cfg.StrOpt('foo', deprecated_group='DEFAULT', deprecated_name='oldfoo'), group='blaa') paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'oldfoo = bar\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'blaa')) self.assertTrue(hasattr(self.conf.blaa, 'foo')) self.assertEqual('bar', self.conf.blaa.foo) def test_arg_group_in_config_file_override_deprecated_name(self): self.conf.register_group(cfg.OptGroup('blaa')) self.conf.register_opt(cfg.StrOpt('foo', deprecated_name='oldfoo'), group='blaa') paths = self.create_tempfiles([('test', '[blaa]\n' 'foo = bar\n' 'oldfoo = blabla\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'blaa')) self.assertTrue(hasattr(self.conf.blaa, 'foo')) self.assertEqual('bar', self.conf.blaa.foo) def test_arg_group_in_config_file_override_deprecated_group(self): self.conf.register_group(cfg.OptGroup('blaa')) self.conf.register_opt(cfg.StrOpt('foo', deprecated_group='DEFAULT'), group='blaa') paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = blabla\n' '[blaa]\n' 'foo = bar\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'blaa')) self.assertTrue(hasattr(self.conf.blaa, 'foo')) self.assertEqual('bar', self.conf.blaa.foo) def test_arg_group_in_config_file_override_deprecated_group_and_name(self): self.conf.register_group(cfg.OptGroup('blaa')) self.conf.register_opt( cfg.StrOpt('foo', deprecated_group='DEFAULT', deprecated_name='oldfoo'), group='blaa') paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'oldfoo = blabla\n' '[blaa]\n' 'foo = bar\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'blaa')) self.assertTrue(hasattr(self.conf.blaa, 'foo')) self.assertEqual('bar', self.conf.blaa.foo) def test_arg_group_in_config_file_with_capital_name(self): self.conf.register_group(cfg.OptGroup('blaa')) self.conf.register_opt(cfg.StrOpt('foo'), group='blaa') paths = self.create_tempfiles([('test', '[BLAA]\n' 'foo = bar\n')]) self.conf(['--config-file', paths[0]]) self.assertFalse(hasattr(self.conf, 'BLAA')) self.assertTrue(hasattr(self.conf, 'blaa')) self.assertTrue(hasattr(self.conf.blaa, 'foo')) self.assertEqual('bar', self.conf.blaa.foo) def test_arg_group_in_config_file_with_capital_name_on_legacy_code(self): self.conf.register_group(cfg.OptGroup('BLAA')) self.conf.register_opt(cfg.StrOpt('foo'), group='BLAA') paths = self.create_tempfiles([('test', '[BLAA]\n' 'foo = bar\n')]) self.conf(['--config-file', paths[0]]) self.assertFalse(hasattr(self.conf, 'blaa')) self.assertTrue(hasattr(self.conf, 'BLAA')) self.assertTrue(hasattr(self.conf.BLAA, 'foo')) self.assertEqual('bar', self.conf.BLAA.foo) class MappingInterfaceTestCase(BaseTestCase): def test_mapping_interface(self): self.conf.register_cli_opt(cfg.StrOpt('foo')) self.conf(['--foo', 'bar']) self.assertIn('foo', self.conf) self.assertIn('config_file', self.conf) self.assertEqual(len(self.conf), 3) self.assertEqual('bar', self.conf['foo']) self.assertEqual('bar', self.conf.get('foo')) self.assertIn('bar', list(self.conf.values())) def test_mapping_interface_with_group(self): self.conf.register_group(cfg.OptGroup('blaa')) self.conf.register_cli_opt(cfg.StrOpt('foo'), group='blaa') self.conf(['--blaa-foo', 'bar']) self.assertIn('blaa', self.conf) self.assertIn('foo', list(self.conf['blaa'])) self.assertEqual(len(self.conf['blaa']), 1) self.assertEqual('bar', self.conf['blaa']['foo']) self.assertEqual('bar', self.conf['blaa'].get('foo')) self.assertIn('bar', self.conf['blaa'].values()) self.assertEqual(self.conf['blaa'], self.conf.blaa) class OptNameSeparatorTestCase(BaseTestCase): # NOTE(bnemec): The broken* values in these scenarios are opt dests or # config file names that are not actually valid, but can be added via a # DeprecatedOpt. The tests only verify that those values do not end up # in the final config object. scenarios = [ ('hyphen', dict(opt_name='foo-bar', opt_dest='foo_bar', broken_opt_dest='foo-bar', cf_name='foo_bar', broken_cf_name='foo-bar', cli_name='foo-bar', hyphen=True)), ('underscore', dict(opt_name='foo_bar', opt_dest='foo_bar', broken_opt_dest='foo-bar', cf_name='foo_bar', broken_cf_name='foo-bar', cli_name='foo_bar', hyphen=False)), ] def test_attribute_and_key_name(self): self.conf.register_opt(cfg.StrOpt(self.opt_name)) self.assertTrue(hasattr(self.conf, self.opt_dest)) self.assertFalse(hasattr(self.conf, self.broken_opt_dest)) self.assertIn(self.opt_dest, self.conf) self.assertNotIn(self.broken_opt_dest, self.conf) def test_cli_opt_name(self): self.conf.register_cli_opt(cfg.BoolOpt(self.opt_name)) self.conf(['--' + self.cli_name]) self.assertTrue(getattr(self.conf, self.opt_dest)) def test_config_file_opt_name(self): self.conf.register_opt(cfg.BoolOpt(self.opt_name)) paths = self.create_tempfiles([('test', '[DEFAULT]\n' + self.cf_name + ' = True\n' + self.broken_cf_name + ' = False\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(getattr(self.conf, self.opt_dest)) def test_deprecated_name(self): self.conf.register_opt(cfg.StrOpt('foobar', deprecated_name=self.opt_name)) self.assertTrue(hasattr(self.conf, 'foobar')) # TODO(mtreinish): Add a check on the log message self.assertTrue(hasattr(self.conf, self.opt_dest)) self.assertFalse(hasattr(self.conf, self.broken_opt_dest)) self.assertIn('foobar', self.conf) self.assertNotIn(self.opt_dest, self.conf) self.assertNotIn(self.broken_opt_dest, self.conf) def test_deprecated_name_alternate_group(self): self.conf.register_opt( cfg.StrOpt('foobar', deprecated_name=self.opt_name, deprecated_group='testing'), group=cfg.OptGroup('testing'), ) self.assertTrue(hasattr(self.conf.testing, 'foobar')) # TODO(mtreinish): Add a check on the log message self.assertTrue(hasattr(self.conf.testing, self.opt_dest)) self.assertFalse(hasattr(self.conf.testing, self.broken_opt_dest)) self.assertIn('foobar', self.conf.testing) self.assertNotIn(self.opt_dest, self.conf.testing) self.assertNotIn(self.broken_opt_dest, self.conf.testing) def test_deprecated_name_cli(self): self.conf.register_cli_opt(cfg.BoolOpt('foobar', deprecated_name=self.opt_name)) self.conf(['--' + self.cli_name]) self.assertTrue(self.conf.foobar) def test_deprecated_name_config_file(self): self.conf.register_opt(cfg.BoolOpt('foobar', deprecated_name=self.opt_name)) paths = self.create_tempfiles([('test', '[DEFAULT]\n' + self.cf_name + ' = True\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(self.conf.foobar) def test_deprecated_opts(self): oldopts = [cfg.DeprecatedOpt(self.opt_name)] self.conf.register_opt(cfg.StrOpt('foobar', deprecated_opts=oldopts)) self.assertTrue(hasattr(self.conf, 'foobar')) # TODO(mtreinish): Add check for the log message self.assertTrue(hasattr(self.conf, self.opt_dest)) self.assertFalse(hasattr(self.conf, self.broken_opt_dest)) self.assertIn('foobar', self.conf) self.assertNotIn(self.opt_dest, self.conf) self.assertNotIn(self.broken_opt_dest, self.conf) def test_deprecated_opts_cli(self): oldopts = [cfg.DeprecatedOpt(self.opt_name)] self.conf.register_cli_opt(cfg.BoolOpt('foobar', deprecated_opts=oldopts)) self.conf(['--' + self.cli_name]) self.assertTrue(self.conf.foobar) def test_deprecated_opts_config_file(self): oldopts = [cfg.DeprecatedOpt(self.opt_name)] self.conf.register_opt(cfg.BoolOpt('foobar', deprecated_opts=oldopts)) paths = self.create_tempfiles([('test', '[DEFAULT]\n' + self.cf_name + ' = True\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(self.conf.foobar) class ReRegisterOptTestCase(BaseTestCase): def test_conf_file_re_register_opt(self): opt = cfg.StrOpt('foo') self.assertTrue(self.conf.register_opt(opt)) self.assertFalse(self.conf.register_opt(opt)) def test_conf_file_re_register_opt_in_group(self): group = cfg.OptGroup('blaa') self.conf.register_group(group) self.conf.register_group(group) # not an error opt = cfg.StrOpt('foo') self.assertTrue(self.conf.register_opt(opt, group=group)) self.assertFalse(self.conf.register_opt(opt, group='blaa')) class RegisterOptNameTestCase(BaseTestCase): def test_register_opt_with_disallow_name(self): for name in cfg.ConfigOpts.disallow_names: opt = cfg.StrOpt(name) self.assertRaises(ValueError, self.conf.register_opt, opt) class TemplateSubstitutionTestCase(BaseTestCase): def _prep_test_str_sub(self, foo_default=None, bar_default=None): self.conf.register_cli_opt(cfg.StrOpt('foo', default=foo_default)) self.conf.register_cli_opt(cfg.StrOpt('bar', default=bar_default)) def _assert_str_sub(self): self.assertTrue(hasattr(self.conf, 'bar')) self.assertEqual('blaa', self.conf.bar) def test_str_sub_default_from_default(self): self._prep_test_str_sub(foo_default='blaa', bar_default='$foo') self.conf([]) self._assert_str_sub() def test_str_sub_default_from_default_recurse(self): self.conf.register_cli_opt(cfg.StrOpt('blaa', default='blaa')) self._prep_test_str_sub(foo_default='$blaa', bar_default='$foo') self.conf([]) self._assert_str_sub() def test_str_sub_default_from_arg(self): self._prep_test_str_sub(bar_default='$foo') self.conf(['--foo', 'blaa']) self._assert_str_sub() def test_str_sub_default_from_config_file(self): self._prep_test_str_sub(bar_default='$foo') paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = blaa\n')]) self.conf(['--config-file', paths[0]]) self._assert_str_sub() def test_str_sub_arg_from_default(self): self._prep_test_str_sub(foo_default='blaa') self.conf(['--bar', '$foo']) self._assert_str_sub() def test_str_sub_arg_from_arg(self): self._prep_test_str_sub() self.conf(['--foo', 'blaa', '--bar', '$foo']) self._assert_str_sub() def test_str_sub_arg_from_config_file(self): self._prep_test_str_sub() paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = blaa\n')]) self.conf(['--config-file', paths[0], '--bar=$foo']) self._assert_str_sub() def test_str_sub_config_file_from_default(self): self._prep_test_str_sub(foo_default='blaa') paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'bar = $foo\n')]) self.conf(['--config-file', paths[0]]) self._assert_str_sub() def test_str_sub_config_file_from_arg(self): self._prep_test_str_sub() paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'bar = $foo\n')]) self.conf(['--config-file', paths[0], '--foo=blaa']) self._assert_str_sub() def test_str_sub_config_file_from_config_file(self): self._prep_test_str_sub() paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'bar = $foo\n' 'foo = blaa\n')]) self.conf(['--config-file', paths[0]]) self._assert_str_sub() def test_str_sub_with_dollar_escape_char(self): self._prep_test_str_sub() paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'bar=foo-somethin$$k2\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'bar')) self.assertEqual('foo-somethin$k2', self.conf.bar) def test_str_sub_with_backslash_escape_char(self): self._prep_test_str_sub() paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'bar=foo-somethin\$k2\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'bar')) self.assertEqual('foo-somethin$k2', self.conf.bar) def test_str_sub_group_from_default(self): self.conf.register_cli_opt(cfg.StrOpt('foo', default='blaa')) self.conf.register_group(cfg.OptGroup('ba')) self.conf.register_cli_opt(cfg.StrOpt('r', default='$foo'), group='ba') self.conf([]) self.assertTrue(hasattr(self.conf, 'ba')) self.assertTrue(hasattr(self.conf.ba, 'r')) self.assertEqual('blaa', self.conf.ba.r) def test_str_sub_set_default(self): self._prep_test_str_sub() self.conf.set_default('bar', '$foo') self.conf.set_default('foo', 'blaa') self.conf([]) self._assert_str_sub() def test_str_sub_set_override(self): self._prep_test_str_sub() self.conf.set_override('bar', '$foo') self.conf.set_override('foo', 'blaa') self.conf([]) self._assert_str_sub() def _prep_test_str_int_sub(self, foo_default=None, bar_default=None): self.conf.register_cli_opt(cfg.StrOpt('foo', default=foo_default)) self.conf.register_cli_opt(cfg.IntOpt('bar', default=bar_default)) def _assert_int_sub(self): self.assertTrue(hasattr(self.conf, 'bar')) self.assertEqual(123, self.conf.bar) def test_sub_default_from_default(self): self._prep_test_str_int_sub(foo_default='123', bar_default='$foo') self.conf([]) self._assert_int_sub() def test_sub_default_from_default_recurse(self): self.conf.register_cli_opt(cfg.StrOpt('blaa', default='123')) self._prep_test_str_int_sub(foo_default='$blaa', bar_default='$foo') self.conf([]) self._assert_int_sub() def test_sub_default_from_arg(self): self._prep_test_str_int_sub(bar_default='$foo') self.conf(['--foo', '123']) self._assert_int_sub() def test_sub_default_from_config_file(self): self._prep_test_str_int_sub(bar_default='$foo') paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 123\n')]) self.conf(['--config-file', paths[0]]) self._assert_int_sub() def test_sub_arg_from_default(self): self._prep_test_str_int_sub(foo_default='123') self.conf(['--bar', '$foo']) self._assert_int_sub() def test_sub_arg_from_arg(self): self._prep_test_str_int_sub() self.conf(['--foo', '123', '--bar', '$foo']) self._assert_int_sub() def test_sub_arg_from_config_file(self): self._prep_test_str_int_sub() paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = 123\n')]) self.conf(['--config-file', paths[0], '--bar=$foo']) self._assert_int_sub() def test_sub_config_file_from_default(self): self._prep_test_str_int_sub(foo_default='123') paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'bar = $foo\n')]) self.conf(['--config-file', paths[0]]) self._assert_int_sub() def test_sub_config_file_from_arg(self): self._prep_test_str_int_sub() paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'bar = $foo\n')]) self.conf(['--config-file', paths[0], '--foo=123']) self._assert_int_sub() def test_sub_config_file_from_config_file(self): self._prep_test_str_int_sub() paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'bar = $foo\n' 'foo = 123\n')]) self.conf(['--config-file', paths[0]]) self._assert_int_sub() def test_sub_group_from_default(self): self.conf.register_cli_opt(cfg.StrOpt('foo', default='123')) self.conf.register_group(cfg.OptGroup('ba')) self.conf.register_cli_opt(cfg.IntOpt('r', default='$foo'), group='ba') self.conf([]) self.assertTrue(hasattr(self.conf, 'ba')) self.assertTrue(hasattr(self.conf.ba, 'r')) self.assertEqual('123', self.conf.foo) self.assertEqual(123, self.conf.ba.r) def test_sub_group_from_default_deprecated(self): self.conf.register_group(cfg.OptGroup('ba')) self.conf.register_cli_opt(cfg.StrOpt( 'foo', default='123', deprecated_group='DEFAULT'), group='ba') self.conf.register_cli_opt(cfg.IntOpt('r', default='$foo'), group='ba') self.conf([]) self.assertTrue(hasattr(self.conf, 'ba')) self.assertTrue(hasattr(self.conf.ba, 'foo')) self.assertEqual('123', self.conf.ba.foo) self.assertTrue(hasattr(self.conf.ba, 'r')) self.assertEqual(123, self.conf.ba.r) def test_sub_group_from_args_deprecated(self): self.conf.register_group(cfg.OptGroup('ba')) self.conf.register_cli_opt(cfg.StrOpt( 'foo', default='123', deprecated_group='DEFAULT'), group='ba') self.conf.register_cli_opt(cfg.IntOpt('r', default='$foo'), group='ba') self.conf(['--ba-foo=4242']) self.assertTrue(hasattr(self.conf, 'ba')) self.assertTrue(hasattr(self.conf.ba, 'foo')) self.assertTrue(hasattr(self.conf.ba, 'r')) self.assertEqual('4242', self.conf.ba.foo) self.assertEqual(4242, self.conf.ba.r) def test_sub_group_from_configfile_deprecated(self): self.conf.register_group(cfg.OptGroup('ba')) self.conf.register_cli_opt(cfg.StrOpt( 'foo', default='123', deprecated_group='DEFAULT'), group='ba') self.conf.register_cli_opt(cfg.IntOpt('r', default='$foo'), group='ba') paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo=4242\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'ba')) self.assertTrue(hasattr(self.conf.ba, 'foo')) self.assertTrue(hasattr(self.conf.ba, 'r')) self.assertEqual('4242', self.conf.ba.foo) self.assertEqual(4242, self.conf.ba.r) def test_dict_sub_default_from_default(self): self.conf.register_cli_opt(cfg.StrOpt('foo', default='floo')) self.conf.register_cli_opt(cfg.StrOpt('bar', default='blaa')) self.conf.register_cli_opt(cfg.DictOpt('dt', default={'$foo': '$bar'})) self.conf([]) self.assertEqual('blaa', self.conf.dt['floo']) def test_dict_sub_default_from_default_multi(self): self.conf.register_cli_opt(cfg.StrOpt('foo', default='floo')) self.conf.register_cli_opt(cfg.StrOpt('bar', default='blaa')) self.conf.register_cli_opt(cfg.StrOpt('goo', default='gloo')) self.conf.register_cli_opt(cfg.StrOpt('har', default='hlaa')) self.conf.register_cli_opt(cfg.DictOpt('dt', default={'$foo': '$bar', '$goo': 'goo', 'har': '$har', 'key1': 'str', 'key2': 12345})) self.conf([]) self.assertEqual('blaa', self.conf.dt['floo']) self.assertEqual('goo', self.conf.dt['gloo']) self.assertEqual('hlaa', self.conf.dt['har']) self.assertEqual('str', self.conf.dt['key1']) self.assertEqual(12345, self.conf.dt['key2']) def test_dict_sub_default_from_default_recurse(self): self.conf.register_cli_opt(cfg.StrOpt('foo', default='$foo2')) self.conf.register_cli_opt(cfg.StrOpt('foo2', default='floo')) self.conf.register_cli_opt(cfg.StrOpt('bar', default='$bar2')) self.conf.register_cli_opt(cfg.StrOpt('bar2', default='blaa')) self.conf.register_cli_opt(cfg.DictOpt('dt', default={'$foo': '$bar'})) self.conf([]) self.assertEqual('blaa', self.conf.dt['floo']) def test_dict_sub_default_from_arg(self): self.conf.register_cli_opt(cfg.StrOpt('foo', default=None)) self.conf.register_cli_opt(cfg.StrOpt('bar', default=None)) self.conf.register_cli_opt(cfg.DictOpt('dt', default={'$foo': '$bar'})) self.conf(['--foo', 'floo', '--bar', 'blaa']) self.assertTrue(hasattr(self.conf, 'dt')) self.assertEqual('blaa', self.conf.dt['floo']) def test_dict_sub_default_from_config_file(self): self.conf.register_cli_opt(cfg.StrOpt('foo', default='floo')) self.conf.register_cli_opt(cfg.StrOpt('bar', default='blaa')) self.conf.register_cli_opt(cfg.DictOpt('dt', default={})) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'dt = $foo:$bar\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'dt')) self.assertEqual('blaa', self.conf.dt['floo']) class ConfigDirTestCase(BaseTestCase): def test_config_dir(self): snafu_group = cfg.OptGroup('snafu') self.conf.register_group(snafu_group) self.conf.register_cli_opt(cfg.StrOpt('foo')) self.conf.register_cli_opt(cfg.StrOpt('bell'), group=snafu_group) dir = tempfile.mkdtemp() self.tempdirs.append(dir) paths = self.create_tempfiles([(os.path.join(dir, '00-test'), '[DEFAULT]\n' 'foo = bar-00\n' '[snafu]\n' 'bell = whistle-00\n'), (os.path.join(dir, '02-test'), '[snafu]\n' 'bell = whistle-02\n' '[DEFAULT]\n' 'foo = bar-02\n'), (os.path.join(dir, '01-test'), '[DEFAULT]\n' 'foo = bar-01\n')]) self.conf(['--foo', 'bar', '--config-dir', os.path.dirname(paths[0])]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('bar-02', self.conf.foo) self.assertTrue(hasattr(self.conf, 'snafu')) self.assertTrue(hasattr(self.conf.snafu, 'bell')) self.assertEqual('whistle-02', self.conf.snafu.bell) def test_config_dir_multistr(self): # Demonstrate that values for multistr options found in # different sources are combined. self.conf.register_cli_opt(cfg.MultiStrOpt('foo')) dir = tempfile.mkdtemp() self.tempdirs.append(dir) paths = self.create_tempfiles([(os.path.join(dir, '00-test'), '[DEFAULT]\n' 'foo = bar-00\n'), (os.path.join(dir, '02-test'), '[DEFAULT]\n' 'foo = bar-02\n'), (os.path.join(dir, '01-test'), '[DEFAULT]\n' 'foo = bar-01\n')]) self.conf(['--foo', 'bar', '--config-dir', os.path.dirname(paths[0])]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(['bar', 'bar-00', 'bar-01', 'bar-02'], self.conf.foo) def test_config_dir_file_precedence(self): snafu_group = cfg.OptGroup('snafu') self.conf.register_group(snafu_group) self.conf.register_cli_opt(cfg.StrOpt('foo')) self.conf.register_cli_opt(cfg.StrOpt('bell'), group=snafu_group) dir = tempfile.mkdtemp() self.tempdirs.append(dir) paths = self.create_tempfiles([(os.path.join(dir, '00-test'), '[DEFAULT]\n' 'foo = bar-00\n'), ('01-test', '[snafu]\n' 'bell = whistle-01\n' '[DEFAULT]\n' 'foo = bar-01\n'), ('03-test', '[snafu]\n' 'bell = whistle-03\n' '[DEFAULT]\n' 'foo = bar-03\n'), (os.path.join(dir, '02-test'), '[DEFAULT]\n' 'foo = bar-02\n')]) self.conf(['--foo', 'bar', '--config-file', paths[1], '--config-dir', os.path.dirname(paths[0]), '--config-file', paths[2], ]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('bar-03', self.conf.foo) self.assertTrue(hasattr(self.conf, 'snafu')) self.assertTrue(hasattr(self.conf.snafu, 'bell')) self.assertEqual('whistle-03', self.conf.snafu.bell) def test_config_dir_default_file_precedence(self): snafu_group = cfg.OptGroup('snafu') self.conf.register_group(snafu_group) self.conf.register_cli_opt(cfg.StrOpt('foo')) self.conf.register_cli_opt(cfg.StrOpt('bell'), group=snafu_group) dir = tempfile.mkdtemp() self.tempdirs.append(dir) paths = self.create_tempfiles([(os.path.join(dir, '00-test'), '[DEFAULT]\n' 'foo = bar-00\n' '[snafu]\n' 'bell = whistle-11\n'), ('01-test', '[snafu]\n' 'bell = whistle-01\n' '[DEFAULT]\n' 'foo = bar-01\n'), ('03-test', '[snafu]\n' 'bell = whistle-03\n' '[DEFAULT]\n' 'foo = bar-03\n'), (os.path.join(dir, '02-test'), '[DEFAULT]\n' 'foo = bar-02\n')]) self.conf(['--foo', 'bar', '--config-dir', os.path.dirname(paths[0])], default_config_files=[paths[1], paths[2]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('bar-02', self.conf.foo) self.assertTrue(hasattr(self.conf, 'snafu')) self.assertTrue(hasattr(self.conf.snafu, 'bell')) self.assertEqual('whistle-11', self.conf.snafu.bell) def test_config_dir_doesnt_exist(self): tmpdir = tempfile.mkdtemp() os.rmdir(tmpdir) self.assertRaises(cfg.ConfigDirNotFoundError, self.conf, ['--config-dir', tmpdir] ) class ReparseTestCase(BaseTestCase): def test_reparse(self): self.conf.register_group(cfg.OptGroup('blaa')) self.conf.register_cli_opt( cfg.StrOpt('foo', default='r'), group='blaa') paths = self.create_tempfiles([('test', '[blaa]\n' 'foo = b\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'blaa')) self.assertTrue(hasattr(self.conf.blaa, 'foo')) self.assertEqual('b', self.conf.blaa.foo) self.conf(['--blaa-foo', 'a']) self.assertTrue(hasattr(self.conf, 'blaa')) self.assertTrue(hasattr(self.conf.blaa, 'foo')) self.assertEqual('a', self.conf.blaa.foo) self.conf([]) self.assertTrue(hasattr(self.conf, 'blaa')) self.assertTrue(hasattr(self.conf.blaa, 'foo')) self.assertEqual('r', self.conf.blaa.foo) class OverridesTestCase(BaseTestCase): def test_default_none(self): self.conf.register_opt(cfg.StrOpt('foo', default='foo')) self.conf([]) self.assertEqual('foo', self.conf.foo) self.conf.set_default('foo', None) self.assertIsNone(self.conf.foo) self.conf.clear_default('foo') self.assertEqual('foo', self.conf.foo) def test_no_default_override(self): self.conf.register_opt(cfg.StrOpt('foo')) self.conf([]) self.assertIsNone(self.conf.foo) self.conf.set_default('foo', 'bar') self.assertEqual('bar', self.conf.foo) self.conf.clear_default('foo') self.assertIsNone(self.conf.foo) def test_default_override(self): self.conf.register_opt(cfg.StrOpt('foo', default='foo')) self.conf([]) self.assertEqual('foo', self.conf.foo) self.conf.set_default('foo', 'bar') self.assertEqual('bar', self.conf.foo) self.conf.clear_default('foo') self.assertEqual('foo', self.conf.foo) def test_set_default_not_in_choices(self): self.conf.register_group(cfg.OptGroup('f')) self.conf.register_cli_opt(cfg.StrOpt('oo', choices=('a', 'b')), group='f') self.assertRaises(ValueError, self.conf.set_default, 'oo', 'c', 'f') def test_wrong_type_default_override(self): self.conf.register_opt(cfg.IntOpt('foo', default=1)) self.conf([]) self.assertEqual(1, self.conf.foo) self.assertRaises(ValueError, self.conf.set_default, 'foo', 'not_really_a_int') def test_override(self): self.conf.register_opt(cfg.StrOpt('foo')) self.conf.set_override('foo', 'bar') self.conf([]) self.assertEqual('bar', self.conf.foo) self.conf.clear_override('foo') self.assertIsNone(self.conf.foo) def test_override_none(self): self.conf.register_opt(cfg.StrOpt('foo', default='foo')) self.conf([]) self.assertEqual('foo', self.conf.foo) self.conf.set_override('foo', None) self.assertIsNone(self.conf.foo) self.conf.clear_override('foo') self.assertEqual('foo', self.conf.foo) def test_group_no_default_override(self): self.conf.register_group(cfg.OptGroup('blaa')) self.conf.register_opt(cfg.StrOpt('foo'), group='blaa') self.conf([]) self.assertIsNone(self.conf.blaa.foo) self.conf.set_default('foo', 'bar', group='blaa') self.assertEqual('bar', self.conf.blaa.foo) self.conf.clear_default('foo', group='blaa') self.assertIsNone(self.conf.blaa.foo) def test_group_default_override(self): self.conf.register_group(cfg.OptGroup('blaa')) self.conf.register_opt(cfg.StrOpt('foo', default='foo'), group='blaa') self.conf([]) self.assertEqual('foo', self.conf.blaa.foo) self.conf.set_default('foo', 'bar', group='blaa') self.assertEqual('bar', self.conf.blaa.foo) self.conf.clear_default('foo', group='blaa') self.assertEqual('foo', self.conf.blaa.foo) def test_group_override(self): self.conf.register_group(cfg.OptGroup('blaa')) self.conf.register_opt(cfg.StrOpt('foo'), group='blaa') self.assertIsNone(self.conf.blaa.foo) self.conf.set_override('foo', 'bar', group='blaa') self.conf([]) self.assertEqual('bar', self.conf.blaa.foo) self.conf.clear_override('foo', group='blaa') self.assertIsNone(self.conf.blaa.foo) def test_cli_bool_default(self): self.conf.register_cli_opt(cfg.BoolOpt('foo')) self.conf.set_default('foo', True) self.assertTrue(self.conf.foo) self.conf([]) self.assertTrue(self.conf.foo) self.conf.set_default('foo', False) self.assertFalse(self.conf.foo) self.conf.clear_default('foo') self.assertIsNone(self.conf.foo) def test_cli_bool_override(self): self.conf.register_cli_opt(cfg.BoolOpt('foo')) self.conf.set_override('foo', True) self.assertTrue(self.conf.foo) self.conf([]) self.assertTrue(self.conf.foo) self.conf.set_override('foo', False) self.assertFalse(self.conf.foo) self.conf.clear_override('foo') self.assertIsNone(self.conf.foo) def test__str_override(self): self.conf.register_opt(cfg.StrOpt('foo')) self.conf.set_override('foo', True) self.conf([]) self.assertEqual('True', self.conf.foo) self.conf.clear_override('foo') self.assertIsNone(self.conf.foo) def test__wrong_type_override(self): self.conf.register_opt(cfg.IntOpt('foo')) self.assertRaises(ValueError, self.conf.set_override, 'foo', "not_really_a_int") def test_set_override_in_choices(self): self.conf.register_group(cfg.OptGroup('f')) self.conf.register_cli_opt(cfg.StrOpt('oo', choices=('a', 'b')), group='f') self.conf.set_override('oo', 'b', 'f') self.assertEqual('b', self.conf.f.oo) def test_set_override_not_in_choices(self): self.conf.register_group(cfg.OptGroup('f')) self.conf.register_cli_opt(cfg.StrOpt('oo', choices=('a', 'b')), group='f') self.assertRaises(ValueError, self.conf.set_override, 'oo', 'c', 'f') def test_bool_override(self): self.conf.register_opt(cfg.BoolOpt('foo')) self.conf.set_override('foo', 'True') self.conf([]) self.assertTrue(self.conf.foo) self.conf.clear_override('foo') self.assertIsNone(self.conf.foo) def test_int_override_with_None(self): self.conf.register_opt(cfg.IntOpt('foo')) self.conf.set_override('foo', None) self.conf([]) self.assertIsNone(self.conf.foo) self.conf.clear_override('foo') self.assertIsNone(self.conf.foo) def test_str_override_with_None(self): self.conf.register_opt(cfg.StrOpt('foo')) self.conf.set_override('foo', None) self.conf([]) self.assertIsNone(self.conf.foo) self.conf.clear_override('foo') self.assertIsNone(self.conf.foo) def test_List_override(self): self.conf.register_opt(cfg.ListOpt('foo')) self.conf.set_override('foo', ['aa', 'bb']) self.conf([]) self.assertEqual(['aa', 'bb'], self.conf.foo) self.conf.clear_override('foo') self.assertIsNone(self.conf.foo) class ResetAndClearTestCase(BaseTestCase): def test_clear(self): self.conf.register_cli_opt(cfg.StrOpt('foo')) self.conf.register_cli_opt(cfg.StrOpt('bar'), group='blaa') self.assertIsNone(self.conf.foo) self.assertIsNone(self.conf.blaa.bar) self.conf(['--foo', 'foo', '--blaa-bar', 'bar']) self.assertEqual('foo', self.conf.foo) self.assertEqual('bar', self.conf.blaa.bar) self.conf.clear() self.assertIsNone(self.conf.foo) self.assertIsNone(self.conf.blaa.bar) def test_reset_and_clear_with_defaults_and_overrides(self): self.conf.register_cli_opt(cfg.StrOpt('foo')) self.conf.register_cli_opt(cfg.StrOpt('bar'), group='blaa') self.conf.set_default('foo', 'foo') self.conf.set_override('bar', 'bar', group='blaa') self.conf(['--foo', 'foofoo']) self.assertEqual('foofoo', self.conf.foo) self.assertEqual('bar', self.conf.blaa.bar) self.conf.clear() self.assertEqual('foo', self.conf.foo) self.assertEqual('bar', self.conf.blaa.bar) self.conf.reset() self.assertIsNone(self.conf.foo) self.assertIsNone(self.conf.blaa.bar) class UnregisterOptTestCase(BaseTestCase): def test_unregister_opt(self): opts = [cfg.StrOpt('foo'), cfg.StrOpt('bar')] self.conf.register_opts(opts) self.assertTrue(hasattr(self.conf, 'foo')) self.assertTrue(hasattr(self.conf, 'bar')) self.conf.unregister_opt(opts[0]) self.assertFalse(hasattr(self.conf, 'foo')) self.assertTrue(hasattr(self.conf, 'bar')) self.conf([]) self.assertRaises(cfg.ArgsAlreadyParsedError, self.conf.unregister_opt, opts[1]) self.conf.clear() self.assertTrue(hasattr(self.conf, 'bar')) self.conf.unregister_opts(opts) def test_unregister_opt_from_group(self): opt = cfg.StrOpt('foo') self.conf.register_opt(opt, group='blaa') self.assertTrue(hasattr(self.conf, 'blaa')) self.assertTrue(hasattr(self.conf.blaa, 'foo')) self.conf.unregister_opt(opt, group='blaa') self.assertFalse(hasattr(self.conf.blaa, 'foo')) class ImportOptTestCase(BaseTestCase): def test_import_opt(self): self.assertFalse(hasattr(cfg.CONF, 'blaa')) cfg.CONF.import_opt('blaa', 'oslo_config.tests.testmods.blaa_opt') self.assertTrue(hasattr(cfg.CONF, 'blaa')) def test_import_opt_in_group(self): self.assertFalse(hasattr(cfg.CONF, 'bar')) cfg.CONF.import_opt('foo', 'oslo_config.tests.testmods.bar_foo_opt', group='bar') self.assertTrue(hasattr(cfg.CONF, 'bar')) self.assertTrue(hasattr(cfg.CONF.bar, 'foo')) def test_import_opt_import_errror(self): self.assertRaises(ImportError, cfg.CONF.import_opt, 'blaa', 'oslo_config.tests.testmods.blaablaa_opt') def test_import_opt_no_such_opt(self): self.assertRaises(cfg.NoSuchOptError, cfg.CONF.import_opt, 'blaablaa', 'oslo_config.tests.testmods.blaa_opt') def test_import_opt_no_such_group(self): self.assertRaises(cfg.NoSuchGroupError, cfg.CONF.import_opt, 'blaa', 'oslo_config.tests.testmods.blaa_opt', group='blaa') class ImportGroupTestCase(BaseTestCase): def test_import_group(self): self.assertFalse(hasattr(cfg.CONF, 'qux')) cfg.CONF.import_group('qux', 'oslo_config.tests.testmods.baz_qux_opt') self.assertTrue(hasattr(cfg.CONF, 'qux')) self.assertTrue(hasattr(cfg.CONF.qux, 'baz')) def test_import_group_import_error(self): self.assertRaises(ImportError, cfg.CONF.import_group, 'qux', 'oslo_config.tests.testmods.bazzz_quxxx_opt') def test_import_group_no_such_group(self): self.assertRaises(cfg.NoSuchGroupError, cfg.CONF.import_group, 'quxxx', 'oslo_config.tests.testmods.baz_qux_opt') class RequiredOptsTestCase(BaseTestCase): def setUp(self): BaseTestCase.setUp(self) self.conf.register_opt(cfg.StrOpt('boo', required=False)) def test_required_opt(self): self.conf.register_opt(cfg.StrOpt('foo', required=True)) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = bar')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('bar', self.conf.foo) def test_required_cli_opt(self): self.conf.register_cli_opt(cfg.StrOpt('foo', required=True)) self.conf(['--foo', 'bar']) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('bar', self.conf.foo) def test_required_cli_opt_with_dash(self): self.conf.register_cli_opt(cfg.StrOpt('foo-bar', required=True)) self.conf(['--foo-bar', 'baz']) self.assertTrue(hasattr(self.conf, 'foo_bar')) self.assertEqual('baz', self.conf.foo_bar) def test_missing_required_opt(self): self.conf.register_opt(cfg.StrOpt('foo', required=True)) self.assertRaises(cfg.RequiredOptError, self.conf, []) def test_missing_required_cli_opt(self): self.conf.register_cli_opt(cfg.StrOpt('foo', required=True)) self.assertRaises(cfg.RequiredOptError, self.conf, []) def test_required_group_opt(self): self.conf.register_group(cfg.OptGroup('blaa')) self.conf.register_opt(cfg.StrOpt('foo', required=True), group='blaa') paths = self.create_tempfiles([('test', '[blaa]\n' 'foo = bar')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'blaa')) self.assertTrue(hasattr(self.conf.blaa, 'foo')) self.assertEqual('bar', self.conf.blaa.foo) def test_required_cli_group_opt(self): self.conf.register_group(cfg.OptGroup('blaa')) self.conf.register_cli_opt( cfg.StrOpt('foo', required=True), group='blaa') self.conf(['--blaa-foo', 'bar']) self.assertTrue(hasattr(self.conf, 'blaa')) self.assertTrue(hasattr(self.conf.blaa, 'foo')) self.assertEqual('bar', self.conf.blaa.foo) def test_missing_required_group_opt(self): self.conf.register_group(cfg.OptGroup('blaa')) self.conf.register_opt(cfg.StrOpt('foo', required=True), group='blaa') self.assertRaises(cfg.RequiredOptError, self.conf, []) def test_missing_required_cli_group_opt(self): self.conf.register_group(cfg.OptGroup('blaa')) self.conf.register_cli_opt( cfg.StrOpt('foo', required=True), group='blaa') self.assertRaises(cfg.RequiredOptError, self.conf, []) def test_required_opt_with_default(self): self.conf.register_cli_opt(cfg.StrOpt('foo', required=True)) self.conf.set_default('foo', 'bar') self.conf([]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('bar', self.conf.foo) def test_required_opt_with_override(self): self.conf.register_cli_opt(cfg.StrOpt('foo', required=True)) self.conf.set_override('foo', 'bar') self.conf([]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('bar', self.conf.foo) class SadPathTestCase(BaseTestCase): def test_unknown_attr(self): self.conf([]) self.assertFalse(hasattr(self.conf, 'foo')) self.assertRaises(AttributeError, getattr, self.conf, 'foo') self.assertRaises(cfg.NoSuchOptError, self.conf._get, 'foo') self.assertRaises(cfg.NoSuchOptError, self.conf.__getattr__, 'foo') def test_unknown_attr_is_attr_error(self): self.conf([]) self.assertFalse(hasattr(self.conf, 'foo')) self.assertRaises(AttributeError, getattr, self.conf, 'foo') def test_unknown_group_attr(self): self.conf.register_group(cfg.OptGroup('blaa')) self.conf([]) self.assertTrue(hasattr(self.conf, 'blaa')) self.assertFalse(hasattr(self.conf.blaa, 'foo')) self.assertRaises(cfg.NoSuchOptError, getattr, self.conf.blaa, 'foo') def test_ok_duplicate(self): opt = cfg.StrOpt('foo') self.conf.register_cli_opt(opt) opt2 = cfg.StrOpt('foo') self.conf.register_cli_opt(opt2) self.conf([]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertIsNone(self.conf.foo) def test_error_duplicate(self): self.conf.register_cli_opt(cfg.StrOpt('foo', help='bar')) self.assertRaises(cfg.DuplicateOptError, self.conf.register_cli_opt, cfg.StrOpt('foo')) def test_error_duplicate_with_different_dest(self): self.conf.register_cli_opt(cfg.StrOpt('foo', dest='f')) self.conf.register_cli_opt(cfg.StrOpt('foo')) self.assertRaises(cfg.DuplicateOptError, self.conf, []) def test_error_duplicate_short(self): self.conf.register_cli_opt(cfg.StrOpt('foo', short='f')) self.conf.register_cli_opt(cfg.StrOpt('bar', short='f')) self.assertRaises(cfg.DuplicateOptError, self.conf, []) def test_already_parsed(self): self.conf([]) self.assertRaises(cfg.ArgsAlreadyParsedError, self.conf.register_cli_opt, cfg.StrOpt('foo')) def test_bad_cli_arg(self): self.conf.register_opt(cfg.BoolOpt('foo')) self.useFixture(fixtures.MonkeyPatch('sys.stderr', moves.StringIO())) self.assertRaises(SystemExit, self.conf, ['--foo']) self.assertIn('error', sys.stderr.getvalue()) self.assertIn('--foo', sys.stderr.getvalue()) def _do_test_bad_cli_value(self, opt_class): self.conf.register_cli_opt(opt_class('foo')) self.useFixture(fixtures.MonkeyPatch('sys.stderr', moves.StringIO())) self.assertRaises(SystemExit, self.conf, ['--foo', 'bar']) self.assertIn('foo', sys.stderr.getvalue()) self.assertIn('bar', sys.stderr.getvalue()) def test_bad_int_arg(self): self._do_test_bad_cli_value(cfg.IntOpt) def test_bad_float_arg(self): self._do_test_bad_cli_value(cfg.FloatOpt) def test_conf_file_not_found(self): (fd, path) = tempfile.mkstemp() os.remove(path) self.assertRaises(cfg.ConfigFilesNotFoundError, self.conf, ['--config-file', path]) def test_conf_file_permission_denied(self): (fd, path) = tempfile.mkstemp() os.chmod(path, 0x000) self.assertRaises(cfg.ConfigFilesPermissionDeniedError, self.conf, ['--config-file', path]) os.remove(path) def test_conf_file_broken(self): paths = self.create_tempfiles([('test', 'foo')]) self.assertRaises(cfg.ConfigFileParseError, self.conf, ['--config-file', paths[0]]) def _do_test_conf_file_bad_value(self, opt_class): self.conf.register_opt(opt_class('foo')) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = bar\n')]) self.conf(['--config-file', paths[0]]) self.assertRaises(ValueError, getattr, self.conf, 'foo') self.assertRaises(cfg.ConfigFileValueError, self.conf._get, 'foo') def test_conf_file_bad_bool(self): self._do_test_conf_file_bad_value(cfg.BoolOpt) def test_conf_file_bad_int(self): self._do_test_conf_file_bad_value(cfg.IntOpt) def test_conf_file_bad_float(self): self._do_test_conf_file_bad_value(cfg.FloatOpt) def test_str_sub_none_value(self): self.conf.register_cli_opt(cfg.StrOpt('oo')) self.conf.register_cli_opt(cfg.StrOpt('bar', default='$oo')) self.conf.register_cli_opt(cfg.StrOpt('barbar', default='foo $oo foo')) self.conf([]) self.assertTrue(hasattr(self.conf, 'bar')) self.assertEqual('', self.conf.bar) self.assertEqual("foo foo", self.conf.barbar) def test_str_sub_from_group(self): self.conf.register_group(cfg.OptGroup('f')) self.conf.register_cli_opt(cfg.StrOpt('oo', default='blaa'), group='f') self.conf.register_cli_opt(cfg.StrOpt('bar', default='$f.oo')) self.conf([]) self.assertTrue(hasattr(self.conf, 'bar')) self.assertEqual("blaa", self.conf.bar) def test_str_sub_from_group_with_brace(self): self.conf.register_group(cfg.OptGroup('f')) self.conf.register_cli_opt(cfg.StrOpt('oo', default='blaa'), group='f') self.conf.register_cli_opt(cfg.StrOpt('bar', default='${f.oo}')) self.conf([]) self.assertTrue(hasattr(self.conf, 'bar')) self.assertEqual("blaa", self.conf.bar) def test_set_default_unknown_attr(self): self.conf([]) self.assertRaises( cfg.NoSuchOptError, self.conf.set_default, 'foo', 'bar') def test_set_default_unknown_group(self): self.conf([]) self.assertRaises(cfg.NoSuchGroupError, self.conf.set_default, 'foo', 'bar', group='blaa') def test_set_override_unknown_attr(self): self.conf([]) self.assertRaises( cfg.NoSuchOptError, self.conf.set_override, 'foo', 'bar') def test_set_override_unknown_group(self): self.conf([]) self.assertRaises(cfg.NoSuchGroupError, self.conf.set_override, 'foo', 'bar', group='blaa') class FindFileTestCase(BaseTestCase): def test_find_file_without_init(self): self.assertRaises(cfg.NotInitializedError, self.conf.find_file, 'foo.json') def test_find_policy_file(self): policy_file = '/etc/policy.json' self.useFixture(fixtures.MonkeyPatch( 'os.path.exists', lambda p: p == policy_file)) self.conf([]) self.assertIsNone(self.conf.find_file('foo.json')) self.assertEqual(policy_file, self.conf.find_file('policy.json')) def test_find_policy_file_with_config_file(self): dir = tempfile.mkdtemp() self.tempdirs.append(dir) paths = self.create_tempfiles([(os.path.join(dir, 'test.conf'), '[DEFAULT]'), (os.path.join(dir, 'policy.json'), '{}')], ext='') self.conf(['--config-file', paths[0]]) self.assertEqual(paths[1], self.conf.find_file('policy.json')) def test_find_policy_file_with_multiple_config_dirs(self): dir1 = tempfile.mkdtemp() self.tempdirs.append(dir1) dir2 = tempfile.mkdtemp() self.tempdirs.append(dir2) self.conf(['--config-dir', dir1, '--config-dir', dir2]) self.assertEqual(2, len(self.conf.config_dirs)) self.assertEqual(dir1, self.conf.config_dirs[0]) self.assertEqual(dir2, self.conf.config_dirs[1]) def test_config_dirs_empty_list_when_nothing_parsed(self): self.assertEqual([], self.conf.config_dirs) def test_find_policy_file_with_config_dir(self): dir = tempfile.mkdtemp() self.tempdirs.append(dir) dir2 = tempfile.mkdtemp() self.tempdirs.append(dir2) path = self.create_tempfiles([(os.path.join(dir, 'policy.json'), '{}')], ext='')[0] self.conf(['--config-dir', dir, '--config-dir', dir2]) self.assertEqual(path, self.conf.find_file('policy.json')) class OptDumpingTestCase(BaseTestCase): class FakeLogger(object): def __init__(self, test_case, expected_lvl): self.test_case = test_case self.expected_lvl = expected_lvl self.logged = [] def log(self, lvl, fmt, *args): self.test_case.assertEqual(lvl, self.expected_lvl) self.logged.append(fmt % args) def setUp(self): super(OptDumpingTestCase, self).setUp() self._args = ['--foo', 'this', '--blaa-bar', 'that', '--blaa-key', 'admin', '--passwd', 'hush'] def _do_test_log_opt_values(self, args): self.conf.register_cli_opt(cfg.StrOpt('foo')) self.conf.register_cli_opt(cfg.StrOpt('passwd', secret=True)) self.conf.register_group(cfg.OptGroup('blaa')) self.conf.register_cli_opt(cfg.StrOpt('bar'), 'blaa') self.conf.register_cli_opt(cfg.StrOpt('key', secret=True), 'blaa') self.conf(args) logger = self.FakeLogger(self, 666) self.conf.log_opt_values(logger, 666) self.assertEqual([ "*" * 80, "Configuration options gathered from:", "command line args: ['--foo', 'this', '--blaa-bar', " "'that', '--blaa-key', 'admin', '--passwd', 'hush']", "config files: []", "=" * 80, "config_dir = []", "config_file = []", "foo = this", "passwd = ****", "blaa.bar = that", "blaa.key = ****", "*" * 80, ], logger.logged) def test_log_opt_values(self): self._do_test_log_opt_values(self._args) def test_log_opt_values_from_sys_argv(self): self.useFixture(fixtures.MonkeyPatch('sys.argv', ['foo'] + self._args)) self._do_test_log_opt_values(None) def test_log_opt_values_empty_config(self): empty_conf = cfg.ConfigOpts() logger = self.FakeLogger(self, 666) empty_conf.log_opt_values(logger, 666) self.assertEqual([ "*" * 80, "Configuration options gathered from:", "command line args: None", "config files: []", "=" * 80, "*" * 80, ], logger.logged) class ConfigParserTestCase(BaseTestCase): def test_parse_file(self): paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = bar\n' '[BLAA]\n' 'bar = foo\n')]) sections = {} parser = cfg.ConfigParser(paths[0], sections) parser.parse() self.assertIn('DEFAULT', sections) self.assertIn('BLAA', sections) self.assertEqual(sections['DEFAULT']['foo'], ['bar']) self.assertEqual(sections['BLAA']['bar'], ['foo']) def test_parse_file_with_normalized(self): paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = bar\n' '[BLAA]\n' 'bar = foo\n')]) sections = {} normalized = {} parser = cfg.ConfigParser(paths[0], sections) parser._add_normalized(normalized) parser.parse() self.assertIn('DEFAULT', sections) self.assertIn('DEFAULT', normalized) self.assertIn('BLAA', sections) self.assertIn('blaa', normalized) self.assertEqual(sections['DEFAULT']['foo'], ['bar']) self.assertEqual(normalized['DEFAULT']['foo'], ['bar']) self.assertEqual(sections['BLAA']['bar'], ['foo']) self.assertEqual(normalized['blaa']['bar'], ['foo']) def test_no_section(self): with tempfile.NamedTemporaryFile() as tmpfile: tmpfile.write(six.b('foo = bar')) tmpfile.flush() parser = cfg.ConfigParser(tmpfile.name, {}) self.assertRaises(cfg.ParseError, parser.parse) def test__parse_file_ioerror(self): # Test that IOErrors (other than 'No such file or directory') # are propagated. filename = 'fake' namespace = mock.Mock() with mock.patch('oslo_config.cfg.ConfigParser.parse') as parse: parse.side_effect = IOError(errno.EMFILE, filename, 'Too many open files') self.assertRaises(IOError, cfg.ConfigParser._parse_file, filename, namespace) class MultiConfigParserTestCase(BaseTestCase): def test_parse_single_file(self): paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = bar\n' '[BLAA]\n' 'bar = foo\n')]) parser = cfg.MultiConfigParser() read_ok = parser.read(paths) self.assertEqual(read_ok, paths) self.assertIn('DEFAULT', parser.parsed[0]) self.assertEqual(parser.parsed[0]['DEFAULT']['foo'], ['bar']) self.assertEqual(parser.get([('DEFAULT', 'foo')]), ['bar']) self.assertEqual(parser.get([('DEFAULT', 'foo')], multi=True), ['bar']) self.assertEqual(parser.get([('DEFAULT', 'foo')], multi=True), ['bar']) self.assertEqual(parser.get([(None, 'foo')], multi=True), ['bar']) self.assertEqual(parser._get([('DEFAULT', 'foo')], multi=True, normalized=True), ['bar']) self.assertIn('BLAA', parser.parsed[0]) self.assertEqual(parser.parsed[0]['BLAA']['bar'], ['foo']) self.assertEqual(parser.get([('BLAA', 'bar')]), ['foo']) self.assertEqual(parser.get([('BLAA', 'bar')], multi=True), ['foo']) self.assertEqual(parser._get([('blaa', 'bar')], multi=True, normalized=True), ['foo']) def test_parse_multiple_files(self): paths = self.create_tempfiles([('test1', '[DEFAULT]\n' 'foo = bar\n' '[BLAA]\n' 'bar = foo'), ('test2', '[DEFAULT]\n' 'foo = barbar\n' '[BLAA]\n' 'bar = foofoo\n' '[bLAa]\n' 'bar = foofoofoo\n')]) parser = cfg.MultiConfigParser() read_ok = parser.read(paths) self.assertEqual(read_ok, paths) self.assertIn('DEFAULT', parser.parsed[0]) self.assertEqual(parser.parsed[0]['DEFAULT']['foo'], ['barbar']) self.assertIn('DEFAULT', parser.parsed[1]) self.assertEqual(parser.parsed[1]['DEFAULT']['foo'], ['bar']) self.assertEqual(parser.get([('DEFAULT', 'foo')]), ['barbar']) self.assertEqual(parser.get([('DEFAULT', 'foo')], multi=True), ['bar', 'barbar']) self.assertIn('BLAA', parser.parsed[0]) self.assertIn('bLAa', parser.parsed[0]) self.assertEqual(parser.parsed[0]['BLAA']['bar'], ['foofoo']) self.assertEqual(parser.parsed[0]['bLAa']['bar'], ['foofoofoo']) self.assertIn('BLAA', parser.parsed[1]) self.assertEqual(parser.parsed[1]['BLAA']['bar'], ['foo']) self.assertEqual(parser.get([('BLAA', 'bar')]), ['foofoo']) self.assertEqual(parser.get([('bLAa', 'bar')]), ['foofoofoo']) self.assertEqual(parser.get([('BLAA', 'bar')], multi=True), ['foo', 'foofoo']) self.assertEqual(parser._get([('BLAA', 'bar')], multi=True, normalized=True), ['foo', 'foofoo', 'foofoofoo']) class NamespaceTestCase(BaseTestCase): def setUp(self): super(NamespaceTestCase, self).setUp() self.ns = cfg._Namespace(self.conf) def read(self, *texts): paths = ((str(i), t) for i, t in enumerate(texts)) for path in self.create_tempfiles(paths): cfg.ConfigParser._parse_file(path, self.ns) def assertAbsent(self, key, normalized=False): self.assertRaises(KeyError, self.ns._get_value, [key], normalized=normalized) def assertValue(self, key, expect, multi=False, normalized=False): actual = self.ns._get_value([key], multi=multi, normalized=normalized) self.assertEqual(actual, expect) def test_cli(self): self.conf.register_cli_opt(cfg.StrOpt('foo')) key = (None, 'foo') self.assertAbsent(key) self.read('[DEFAULT]\n' 'foo = file0\n') self.assertValue(key, 'file0') self.read('[DEFAULT]\n' 'foo = file1\n') self.assertEqual('file1', self.ns._get_cli_value([key])) def test_single_file(self): self.read('[DEFAULT]\n' 'foo = bar\n' '[BLAA]\n' 'bar = foo\n') self.assertValue(('DEFAULT', 'foo'), 'bar') self.assertValue(('DEFAULT', 'foo'), ['bar'], multi=True) self.assertValue(('DEFAULT', 'foo'), ['bar'], multi=True) self.assertValue((None, 'foo'), ['bar'], multi=True) self.assertValue(('DEFAULT', 'foo'), ['bar'], multi=True, normalized=True) self.assertValue(('BLAA', 'bar'), 'foo') self.assertValue(('BLAA', 'bar'), ['foo'], multi=True) self.assertValue(('blaa', 'bar'), ['foo'], multi=True, normalized=True) def test_multiple_files(self): self.read('[DEFAULT]\n' 'foo = bar\n' '[BLAA]\n' 'bar = foo', '[DEFAULT]\n' 'foo = barbar\n' '[BLAA]\n' 'bar = foofoo\n' '[bLAa]\n' 'bar = foofoofoo\n') self.assertValue(('DEFAULT', 'foo'), 'barbar') self.assertValue(('DEFAULT', 'foo'), ['bar', 'barbar'], multi=True) self.assertValue(('BLAA', 'bar'), 'foofoo') self.assertValue(('bLAa', 'bar'), 'foofoofoo') self.assertValue(('BLAA', 'bar'), ['foo', 'foofoo'], multi=True) self.assertValue(('Blaa', 'bar'), ['foo', 'foofoo', 'foofoofoo'], multi=True, normalized=True) def test_attrs_subparser(self): CONF = cfg.ConfigOpts() CONF.register_cli_opt(cfg.SubCommandOpt( 'foo', handler=lambda sub: sub.add_parser('foo'))) CONF(['foo']) def test_attrs_subparser_failure(self): CONF = cfg.ConfigOpts() CONF.register_cli_opt(cfg.SubCommandOpt( 'foo', handler=lambda sub: sub.add_parser('foo'))) self.assertRaises(SystemExit, CONF, ['foo', 'bar']) class TildeExpansionTestCase(BaseTestCase): def test_config_file_tilde(self): homedir = os.path.expanduser('~') tmpfile = tempfile.mktemp(dir=homedir, prefix='cfg-', suffix='.conf') tmpbase = os.path.basename(tmpfile) try: self.conf(['--config-file', os.path.join('~', tmpbase)]) except cfg.ConfigFilesNotFoundError as cfnfe: self.assertIn(homedir, str(cfnfe)) self.useFixture(fixtures.MonkeyPatch( 'os.path.exists', lambda p: p == tmpfile)) self.assertEqual(tmpfile, self.conf.find_file(tmpbase)) def test_config_dir_tilde(self): homedir = os.path.expanduser('~') try: tmpdir = tempfile.mkdtemp(dir=homedir, prefix='cfg-', suffix='.d') tmpfile = os.path.join(tmpdir, 'foo.conf') self.useFixture(fixtures.MonkeyPatch( 'glob.glob', lambda p: [tmpfile])) e = self.assertRaises(cfg.ConfigFilesNotFoundError, self.conf, ['--config-dir', os.path.join('~', os.path.basename(tmpdir))] ) self.assertIn(tmpdir, str(e)) finally: try: shutil.rmtree(tmpdir) except OSError as exc: if exc.errno != 2: raise class SubCommandTestCase(BaseTestCase): def test_sub_command(self): def add_parsers(subparsers): sub = subparsers.add_parser('a') sub.add_argument('bar', type=int) self.conf.register_cli_opt( cfg.SubCommandOpt('cmd', handler=add_parsers)) self.assertTrue(hasattr(self.conf, 'cmd')) self.conf(['a', '10']) self.assertTrue(hasattr(self.conf.cmd, 'name')) self.assertTrue(hasattr(self.conf.cmd, 'bar')) self.assertEqual('a', self.conf.cmd.name) self.assertEqual(10, self.conf.cmd.bar) def test_sub_command_with_parent(self): def add_parsers(subparsers): parent = argparse.ArgumentParser(add_help=False) parent.add_argument('bar', type=int) subparsers.add_parser('a', parents=[parent]) self.conf.register_cli_opt( cfg.SubCommandOpt('cmd', handler=add_parsers)) self.assertTrue(hasattr(self.conf, 'cmd')) self.conf(['a', '10']) self.assertTrue(hasattr(self.conf.cmd, 'name')) self.assertTrue(hasattr(self.conf.cmd, 'bar')) self.assertEqual('a', self.conf.cmd.name) self.assertEqual(10, self.conf.cmd.bar) def test_sub_command_with_dest(self): def add_parsers(subparsers): subparsers.add_parser('a') self.conf.register_cli_opt( cfg.SubCommandOpt('cmd', dest='command', handler=add_parsers)) self.assertTrue(hasattr(self.conf, 'command')) self.conf(['a']) self.assertEqual('a', self.conf.command.name) def test_sub_command_with_group(self): def add_parsers(subparsers): sub = subparsers.add_parser('a') sub.add_argument('--bar', choices='XYZ') self.conf.register_cli_opt( cfg.SubCommandOpt('cmd', handler=add_parsers), group='blaa') self.assertTrue(hasattr(self.conf, 'blaa')) self.assertTrue(hasattr(self.conf.blaa, 'cmd')) self.conf(['a', '--bar', 'Z']) self.assertTrue(hasattr(self.conf.blaa.cmd, 'name')) self.assertTrue(hasattr(self.conf.blaa.cmd, 'bar')) self.assertEqual('a', self.conf.blaa.cmd.name) self.assertEqual('Z', self.conf.blaa.cmd.bar) def test_sub_command_not_cli(self): self.conf.register_opt(cfg.SubCommandOpt('cmd')) self.conf([]) def test_sub_command_resparse(self): def add_parsers(subparsers): subparsers.add_parser('a') self.conf.register_cli_opt( cfg.SubCommandOpt('cmd', handler=add_parsers)) foo_opt = cfg.StrOpt('foo') self.conf.register_cli_opt(foo_opt) self.conf(['--foo=bar', 'a']) self.assertTrue(hasattr(self.conf.cmd, 'name')) self.assertEqual('a', self.conf.cmd.name) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('bar', self.conf.foo) self.conf.clear() self.conf.unregister_opt(foo_opt) self.conf(['a']) self.assertTrue(hasattr(self.conf.cmd, 'name')) self.assertEqual('a', self.conf.cmd.name) self.assertFalse(hasattr(self.conf, 'foo')) def test_sub_command_no_handler(self): self.conf.register_cli_opt(cfg.SubCommandOpt('cmd')) self.useFixture(fixtures.MonkeyPatch('sys.stderr', moves.StringIO())) self.assertRaises(SystemExit, self.conf, []) self.assertIn('error', sys.stderr.getvalue()) def test_sub_command_with_help(self): def add_parsers(subparsers): subparsers.add_parser('a') self.conf.register_cli_opt(cfg.SubCommandOpt('cmd', title='foo foo', description='bar bar', help='blaa blaa', handler=add_parsers)) self.useFixture(fixtures.MonkeyPatch('sys.stdout', moves.StringIO())) self.assertRaises(SystemExit, self.conf, ['--help']) self.assertIn('foo foo', sys.stdout.getvalue()) self.assertIn('bar bar', sys.stdout.getvalue()) self.assertIn('blaa blaa', sys.stdout.getvalue()) def test_sub_command_errors(self): def add_parsers(subparsers): sub = subparsers.add_parser('a') sub.add_argument('--bar') self.conf.register_cli_opt(cfg.BoolOpt('bar')) self.conf.register_cli_opt( cfg.SubCommandOpt('cmd', handler=add_parsers)) self.conf(['a']) self.assertRaises(cfg.DuplicateOptError, getattr, self.conf.cmd, 'bar') self.assertRaises(cfg.NoSuchOptError, getattr, self.conf.cmd, 'foo') def test_sub_command_multiple(self): self.conf.register_cli_opt(cfg.SubCommandOpt('cmd1')) self.conf.register_cli_opt(cfg.SubCommandOpt('cmd2')) self.useFixture(fixtures.MonkeyPatch('sys.stderr', moves.StringIO())) self.assertRaises(SystemExit, self.conf, []) self.assertIn('multiple', sys.stderr.getvalue()) class SetDefaultsTestCase(BaseTestCase): def test_default_to_none(self): opts = [cfg.StrOpt('foo', default='foo')] self.conf.register_opts(opts) cfg.set_defaults(opts, foo=None) self.conf([]) self.assertIsNone(self.conf.foo) def test_default_from_none(self): opts = [cfg.StrOpt('foo')] self.conf.register_opts(opts) cfg.set_defaults(opts, foo='bar') self.conf([]) self.assertEqual('bar', self.conf.foo) def test_change_default(self): opts = [cfg.StrOpt('foo', default='foo')] self.conf.register_opts(opts) cfg.set_defaults(opts, foo='bar') self.conf([]) self.assertEqual('bar', self.conf.foo) def test_change_default_many(self): opts = [cfg.StrOpt('foo', default='foo'), cfg.StrOpt('foo2', default='foo2')] self.conf.register_opts(opts) cfg.set_defaults(opts, foo='bar', foo2='bar2') self.conf([]) self.assertEqual('bar', self.conf.foo) self.assertEqual('bar2', self.conf.foo2) def test_group_default_to_none(self): opts = [cfg.StrOpt('foo', default='foo')] self.conf.register_opts(opts, group='blaa') cfg.set_defaults(opts, foo=None) self.conf([]) self.assertIsNone(self.conf.blaa.foo) def test_group_default_from_none(self): opts = [cfg.StrOpt('foo')] self.conf.register_opts(opts, group='blaa') cfg.set_defaults(opts, foo='bar') self.conf([]) self.assertEqual('bar', self.conf.blaa.foo) def test_group_change_default(self): opts = [cfg.StrOpt('foo', default='foo')] self.conf.register_opts(opts, group='blaa') cfg.set_defaults(opts, foo='bar') self.conf([]) self.assertEqual('bar', self.conf.blaa.foo) class DeprecatedOptionsTestCase(BaseTestCase): def test_deprecated_opts_equal(self): d1 = cfg.DeprecatedOpt('oldfoo', group='oldgroup') d2 = cfg.DeprecatedOpt('oldfoo', group='oldgroup') self.assertEqual(d1, d2) def test_deprecated_opts_not_equal(self): d1 = cfg.DeprecatedOpt('oldfoo', group='oldgroup') d2 = cfg.DeprecatedOpt('oldfoo2', group='oldgroup') self.assertNotEqual(d1, d2) class MultipleDeprecatedOptionsTestCase(BaseTestCase): def test_conf_file_override_use_deprecated_name_and_group(self): self.conf.register_group(cfg.OptGroup('blaa')) self.conf.register_opt(cfg.StrOpt('foo', deprecated_name='oldfoo', deprecated_group='oldgroup'), group='blaa') paths = self.create_tempfiles([('test', '[oldgroup]\n' 'oldfoo = bar\n')]) self.conf(['--config-file', paths[0]]) self.assertEqual('bar', self.conf.blaa.foo) def test_conf_file_override_use_deprecated_opts(self): self.conf.register_group(cfg.OptGroup('blaa')) oldopts = [cfg.DeprecatedOpt('oldfoo', group='oldgroup')] self.conf.register_opt(cfg.StrOpt('foo', deprecated_opts=oldopts), group='blaa') paths = self.create_tempfiles([('test', '[oldgroup]\n' 'oldfoo = bar\n')]) self.conf(['--config-file', paths[0]]) self.assertEqual('bar', self.conf.blaa.foo) def test_conf_file_override_use_deprecated_multi_opts(self): self.conf.register_group(cfg.OptGroup('blaa')) oldopts = [cfg.DeprecatedOpt('oldfoo', group='oldgroup'), cfg.DeprecatedOpt('oldfoo2', group='oldgroup2')] self.conf.register_opt(cfg.StrOpt('foo', deprecated_opts=oldopts), group='blaa') paths = self.create_tempfiles([('test', '[oldgroup2]\n' 'oldfoo2 = bar\n')]) self.conf(['--config-file', paths[0]]) self.assertEqual('bar', self.conf.blaa.foo) class MultipleDeprecatedCliOptionsTestCase(BaseTestCase): def test_conf_file_override_use_deprecated_name_and_group(self): self.conf.register_group(cfg.OptGroup('blaa')) self.conf.register_cli_opt(cfg.StrOpt('foo', deprecated_name='oldfoo', deprecated_group='oldgroup'), group='blaa') paths = self.create_tempfiles([('test', '[oldgroup]\n' 'oldfoo = bar\n')]) self.conf(['--config-file', paths[0]]) self.assertEqual('bar', self.conf.blaa.foo) def test_conf_file_override_use_deprecated_opts(self): self.conf.register_group(cfg.OptGroup('blaa')) oldopts = [cfg.DeprecatedOpt('oldfoo', group='oldgroup')] self.conf.register_cli_opt(cfg.StrOpt('foo', deprecated_opts=oldopts), group='blaa') paths = self.create_tempfiles([('test', '[oldgroup]\n' 'oldfoo = bar\n')]) self.conf(['--config-file', paths[0]]) self.assertEqual('bar', self.conf.blaa.foo) def test_conf_file_override_use_deprecated_multi_opts(self): self.conf.register_group(cfg.OptGroup('blaa')) oldopts = [cfg.DeprecatedOpt('oldfoo', group='oldgroup'), cfg.DeprecatedOpt('oldfoo2', group='oldgroup2')] self.conf.register_cli_opt(cfg.StrOpt('foo', deprecated_opts=oldopts), group='blaa') paths = self.create_tempfiles([('test', '[oldgroup2]\n' 'oldfoo2 = bar\n')]) self.conf(['--config-file', paths[0]]) self.assertEqual('bar', self.conf.blaa.foo) def test_conf_file_common_deprecated_group(self): self.conf.register_group(cfg.OptGroup('foo')) self.conf.register_group(cfg.OptGroup('bar')) oldopts = [cfg.DeprecatedOpt('foo', group='DEFAULT')] self.conf.register_opt(cfg.StrOpt('common_opt', deprecated_opts=oldopts), group='bar') self.conf.register_opt(cfg.StrOpt('common_opt', deprecated_opts=oldopts), group='foo') paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = bla\n')]) self.conf(['--config-file', paths[0]]) self.assertEqual('bla', self.conf.foo.common_opt) self.assertEqual('bla', self.conf.bar.common_opt) paths = self.create_tempfiles([('test', '[DEFAULT]\n' 'foo = bla\n' '[bar]\n' 'common_opt = blabla\n')]) self.conf(['--config-file', paths[0]]) self.assertEqual('bla', self.conf.foo.common_opt) self.assertEqual('blabla', self.conf.bar.common_opt) paths = self.create_tempfiles([('test', '[foo]\n' 'common_opt = bla\n' '[bar]\n' 'common_opt = blabla\n')]) self.conf(['--config-file', paths[0]]) self.assertEqual('bla', self.conf.foo.common_opt) self.assertEqual('blabla', self.conf.bar.common_opt) class ChoicesTestCase(BaseTestCase): def test_choice_default(self): self.conf.register_cli_opt(cfg.StrOpt('protocol', default='http', choices=['http', 'https', 'ftp'])) self.conf([]) self.assertEqual('http', self.conf.protocol) def test_choice_good(self): self.conf.register_cli_opt(cfg.StrOpt('foo', choices=['bar1', 'bar2'])) self.conf(['--foo', 'bar1']) self.assertEqual('bar1', self.conf.foo) def test_choice_bad(self): self.conf.register_cli_opt(cfg.StrOpt('foo', choices=['bar1', 'bar2'])) self.assertRaises(SystemExit, self.conf, ['--foo', 'bar3']) def test_conf_file_choice_value(self): self.conf.register_opt(cfg.StrOpt('foo', choices=['bar1', 'bar2'])) paths = self.create_tempfiles([('test', '[DEFAULT]\n''foo = bar1\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('bar1', self.conf.foo) def test_conf_file_choice_empty_value(self): self.conf.register_opt(cfg.StrOpt('foo', choices=['', 'bar1', 'bar2'])) paths = self.create_tempfiles([('test', '[DEFAULT]\n''foo = \n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('', self.conf.foo) def test_conf_file_choice_none_value(self): self.conf.register_opt(cfg.StrOpt('foo', default=None, choices=[None, 'bar1', 'bar2'])) paths = self.create_tempfiles([('test', '[DEFAULT]\n''\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertIsNone(self.conf.foo) def test_conf_file_bad_choice_value(self): self.conf.register_opt(cfg.StrOpt('foo', choices=['bar1', 'bar2'])) paths = self.create_tempfiles([('test', '[DEFAULT]\n''foo = bar3\n')]) self.conf(['--config-file', paths[0]]) self.assertRaises(cfg.ConfigFileValueError, self.conf._get, 'foo') self.assertRaises(ValueError, getattr, self.conf, 'foo') def test_conf_file_choice_value_override(self): self.conf.register_cli_opt(cfg.StrOpt('foo', choices=['baar', 'baaar'])) paths = self.create_tempfiles([('1', '[DEFAULT]\n' 'foo = baar\n'), ('2', '[DEFAULT]\n' 'foo = baaar\n')]) self.conf(['--foo', 'baar', '--config-file', paths[0], '--config-file', paths[1]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('baaar', self.conf.foo) def test_conf_file_choice_bad_default(self): self.assertRaises(cfg.DefaultValueError, cfg.StrOpt, 'foo', choices=['baar', 'baaar'], default='foobaz') class PortChoicesTestCase(BaseTestCase): def test_choice_default(self): self.conf.register_cli_opt(cfg.PortOpt('port', default=455, choices=[80, 455])) self.conf([]) self.assertEqual(455, self.conf.port) def test_choice_good_with_list(self): self.conf.register_cli_opt(cfg.PortOpt('port', choices=[80, 8080])) self.conf(['--port', '80']) self.assertEqual(80, self.conf.port) def test_choice_good_with_tuple(self): self.conf.register_cli_opt(cfg.PortOpt('port', choices=(80, 8080))) self.conf(['--port', '80']) self.assertEqual(80, self.conf.port) def test_choice_bad(self): self.conf.register_cli_opt(cfg.PortOpt('port', choices=[80, 8080])) self.assertRaises(SystemExit, self.conf, ['--port', '8181']) def test_choice_out_range(self): self.assertRaisesRegexp(ValueError, 'out of bounds', cfg.PortOpt, 'port', choices=[80, 65537, 0]) def test_conf_file_choice_value(self): self.conf.register_opt(cfg.PortOpt('port', choices=[80, 8080])) paths = self.create_tempfiles([('test', '[DEFAULT]\n''port = 80\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'port')) self.assertEqual(80, self.conf.port) def test_conf_file_bad_choice_value(self): self.conf.register_opt(cfg.PortOpt('port', choices=[80, 8080])) paths = self.create_tempfiles([('test', '[DEFAULT]\n''port = 8181\n')]) self.conf(['--config-file', paths[0]]) self.assertRaises(cfg.ConfigFileValueError, self.conf._get, 'port') self.assertRaises(ValueError, getattr, self.conf, 'port') def test_conf_file_choice_value_override(self): self.conf.register_cli_opt(cfg.PortOpt('port', choices=[80, 8080])) paths = self.create_tempfiles([('1', '[DEFAULT]\n' 'port = 80\n'), ('2', '[DEFAULT]\n' 'port = 8080\n')]) self.conf(['--port', '80', '--config-file', paths[0], '--config-file', paths[1]]) self.assertTrue(hasattr(self.conf, 'port')) self.assertEqual(8080, self.conf.port) def test_conf_file_choice_bad_default(self): self.assertRaises(cfg.DefaultValueError, cfg.PortOpt, 'port', choices=[80, 8080], default=8181) class RegexTestCase(BaseTestCase): def test_regex_good(self): self.conf.register_cli_opt(cfg.StrOpt('foo', regex='foo|bar')) self.conf(['--foo', 'bar']) self.assertEqual('bar', self.conf.foo) self.conf(['--foo', 'foo']) self.assertEqual('foo', self.conf.foo) self.conf(['--foo', 'foobar']) self.assertEqual('foobar', self.conf.foo) def test_regex_bad(self): self.conf.register_cli_opt(cfg.StrOpt('foo', regex='bar')) self.assertRaises(SystemExit, self.conf, ['--foo', 'foo']) def test_conf_file_regex_value(self): self.conf.register_opt(cfg.StrOpt('foo', regex='bar')) paths = self.create_tempfiles([('test', '[DEFAULT]\n''foo = bar\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('bar', self.conf.foo) def test_conf_file_regex_bad_value(self): self.conf.register_opt(cfg.StrOpt('foo', regex='bar')) paths = self.create_tempfiles([('test', '[DEFAULT]\n''foo = other\n')]) self.conf(['--config-file', paths[0]]) self.assertRaisesRegex(cfg.ConfigFileValueError, "doesn't match regex", self.conf._get, 'foo') self.assertRaisesRegex(ValueError, "doesn't match regex", getattr, self.conf, 'foo') def test_regex_with_choice(self): self.assertRaises(ValueError, cfg.StrOpt, 'foo', choices=['bar1'], regex='bar2') class QuotesTestCase(BaseTestCase): def test_quotes_good(self): self.conf.register_cli_opt(cfg.StrOpt('foo', quotes=True)) self.conf(['--foo', '"foobar1"']) self.assertEqual('foobar1', self.conf.foo) self.conf(['--foo', "'foobar2'"]) self.assertEqual('foobar2', self.conf.foo) self.conf(['--foo', 'foobar3']) self.assertEqual('foobar3', self.conf.foo) self.conf(['--foo', 'foobar4"']) self.assertEqual('foobar4"', self.conf.foo) def test_quotes_bad(self): self.conf.register_cli_opt(cfg.StrOpt('foo', quotes=True)) self.assertRaises(SystemExit, self.conf, ['--foo', '"foobar\'']) self.assertRaises(SystemExit, self.conf, ['--foo', '\'foobar"']) self.assertRaises(SystemExit, self.conf, ['--foo', '"foobar']) self.assertRaises(SystemExit, self.conf, ['--foo', "'foobar"]) def test_conf_file_quotes_good_value(self): self.conf.register_opt(cfg.StrOpt('foo', quotes=True)) paths = self.create_tempfiles([('test', '[DEFAULT]\n''foo = "bar"\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('bar', self.conf.foo) def test_conf_file_quotes_bad_value(self): self.conf.register_opt(cfg.StrOpt('foo', quotes=True)) paths = self.create_tempfiles([('test', '[DEFAULT]\n''foo = "bar\n')]) self.conf(['--config-file', paths[0]]) self.assertRaisesRegex(cfg.ConfigFileValueError, 'Non-closed quote:', self.conf._get, 'foo') self.assertRaisesRegex(ValueError, 'Non-closed quote:', getattr, self.conf, 'foo') class IgnoreCaseTestCase(BaseTestCase): def test_ignore_case_with_choices(self): self.conf.register_cli_opt(cfg.StrOpt('foo', ignore_case=True, choices=['bar1', 'bar2', 'BAR3'])) self.conf(['--foo', 'bAr1']) self.assertEqual('bAr1', self.conf.foo) self.conf(['--foo', 'BaR2']) self.assertEqual('BaR2', self.conf.foo) self.conf(['--foo', 'baR3']) self.assertEqual('baR3', self.conf.foo) def test_ignore_case_with_regex(self): self.conf.register_cli_opt(cfg.StrOpt('foo', ignore_case=True, regex='fOO|bar')) self.conf(['--foo', 'foo']) self.assertEqual('foo', self.conf.foo) self.conf(['--foo', 'Bar']) self.assertEqual('Bar', self.conf.foo) self.conf(['--foo', 'FOObar']) self.assertEqual('FOObar', self.conf.foo) def test_conf_file_ignore_case_with_choices(self): self.conf.register_opt(cfg.StrOpt('foo', ignore_case=True, choices=['bar1', 'bar2', 'BAR3'])) paths = self.create_tempfiles([('test', '[DEFAULT]\n''foo = bAr2\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('bAr2', self.conf.foo) def test_conf_file_ignore_case_with_regex(self): self.conf.register_opt(cfg.StrOpt('foo', ignore_case=True, regex='bAr')) paths = self.create_tempfiles([('test', '[DEFAULT]\n''foo = BaR\n')]) self.conf(['--config-file', paths[0]]) self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('BaR', self.conf.foo) class StrOptMaxLengthTestCase(BaseTestCase): def test_stropt_max_length_good(self): self.conf.register_cli_opt(cfg.StrOpt('foo', max_length=5)) self.conf(['--foo', '12345']) self.assertEqual('12345', self.conf.foo) def test_stropt_max_length_bad(self): self.conf.register_cli_opt(cfg.StrOpt('foo', max_length=5)) self.assertRaises(SystemExit, self.conf, ['--foo', '123456']) class URIOptMaxLengthTestCase(BaseTestCase): def test_uriopt_max_length_good(self): self.conf.register_cli_opt(cfg.URIOpt('foo', max_length=30)) self.conf(['--foo', 'http://www.example.com']) self.assertEqual('http://www.example.com', self.conf.foo) def test_uriopt_max_length_bad(self): self.conf.register_cli_opt(cfg.URIOpt('foo', max_length=30)) self.assertRaises(SystemExit, self.conf, ['--foo', 'http://www.example.com/versions']) class URIOptSchemesTestCase(BaseTestCase): def test_uriopt_schemes_good(self): self.conf.register_cli_opt(cfg.URIOpt('foo', schemes=['http', 'ftp'])) self.conf(['--foo', 'http://www.example.com']) self.assertEqual('http://www.example.com', self.conf.foo) self.conf(['--foo', 'ftp://example.com/archives']) self.assertEqual('ftp://example.com/archives', self.conf.foo) def test_uriopt_schemes_bad(self): self.conf.register_cli_opt(cfg.URIOpt('foo', schemes=['http', 'ftp'])) self.assertRaises(SystemExit, self.conf, ['--foo', 'https://www.example.com']) self.assertRaises(SystemExit, self.conf, ['--foo', 'file://www.example.com']) class PrintHelpTestCase(base.BaseTestCase): def test_print_help_without_init(self): conf = cfg.ConfigOpts() conf.register_opts([]) self.assertRaises(cfg.NotInitializedError, conf.print_help) def test_print_help_with_clear(self): conf = cfg.ConfigOpts() conf.register_opts([]) conf([]) conf.clear() self.assertRaises(cfg.NotInitializedError, conf.print_help) class OptTestCase(base.BaseTestCase): def test_opt_eq(self): d1 = cfg.ListOpt('oldfoo') d2 = cfg.ListOpt('oldfoo') self.assertEqual(d1, d2) def test_opt_not_eq(self): d1 = cfg.ListOpt('oldfoo') d2 = cfg.ListOpt('oldbar') self.assertNotEqual(d1, d2) def test_illegal_name(self): self.assertRaises(ValueError, cfg.BoolOpt, '_foo') class SectionsTestCase(BaseTestCase): def test_list_all_sections(self): paths = self.create_tempfiles([('test.ini', '[DEFAULT]\n' 'foo = bar\n' '[BLAA]\n' 'bar = foo\n'), ('test2.ini', '[DEFAULT]\n' 'foo = bar\n' '[BLAA]\n' 'bar = foo\n')]) self.conf(args=[], default_config_files=paths) self.assertEqual(['BLAA', 'DEFAULT'], self.conf.list_all_sections()) def test_list_all_sections_post_mutate(self): paths = self.create_tempfiles([('test.ini', '[DEFAULT]\n' 'foo = bar\n' '[BLAA]\n' 'bar = foo\n'), ('test2.ini', '[WOMBAT]\n' 'woo = war\n' '[BLAA]\n' 'bar = foo\n')]) self.conf(args=[], default_config_files=paths[:1]) self.assertEqual(['BLAA', 'DEFAULT'], self.conf.list_all_sections()) shutil.copy(paths[1], paths[0]) self.conf.mutate_config_files() self.assertEqual(['BLAA', 'DEFAULT', 'WOMBAT'], self.conf.list_all_sections()) class DeprecationWarningTestBase(BaseTestCase): def setUp(self): super(DeprecationWarningTestBase, self).setUp() self.log_fixture = self.useFixture(fixtures.FakeLogger()) self._parser_class = cfg.MultiConfigParser class DeprecationWarningTestScenarios(DeprecationWarningTestBase): scenarios = [('default-deprecated', dict(deprecated=True, group='DEFAULT')), ('default-not-deprecated', dict(deprecated=False, group='DEFAULT')), ('other-deprecated', dict(deprecated=True, group='other')), ('other-not-deprecated', dict(deprecated=False, group='other')), ] def test_deprecated_logging(self): self.conf.register_opt(cfg.StrOpt('foo', deprecated_name='bar')) self.conf.register_group(cfg.OptGroup('other')) self.conf.register_opt(cfg.StrOpt('foo', deprecated_name='bar'), group='other') if self.deprecated: content = 'bar=baz' else: content = 'foo=baz' paths = self.create_tempfiles([('test', '[' + self.group + ']\n' + content + '\n')]) self.conf(['--config-file', paths[0]]) # Reference these twice to verify they only get logged once if self.group == 'DEFAULT': self.assertEqual('baz', self.conf.foo) self.assertEqual('baz', self.conf.foo) else: self.assertEqual('baz', self.conf.other.foo) self.assertEqual('baz', self.conf.other.foo) if self.deprecated: expected = (self._parser_class._deprecated_opt_message % {'dep_option': 'bar', 'dep_group': self.group, 'option': 'foo', 'group': self.group} + '\n') else: expected = '' self.assertEqual(expected, self.log_fixture.output) class DeprecationWarningTests(DeprecationWarningTestBase): def test_DeprecatedOpt(self): default_deprecated = [cfg.DeprecatedOpt('bar')] other_deprecated = [cfg.DeprecatedOpt('baz', group='other')] self.conf.register_group(cfg.OptGroup('other')) self.conf.register_opt(cfg.StrOpt('foo', deprecated_opts=default_deprecated)) self.conf.register_opt(cfg.StrOpt('foo', deprecated_opts=other_deprecated), group='other') paths = self.create_tempfiles([('test', '[DEFAULT]\n' + 'bar=baz\n' + '[other]\n' + 'baz=baz\n')]) self.conf(['--config-file', paths[0]]) self.assertEqual('baz', self.conf.foo) self.assertEqual('baz', self.conf.other.foo) self.assertIn('Option "bar" from group "DEFAULT"', self.log_fixture.output) self.assertIn('Option "baz" from group "other"', self.log_fixture.output) def test_check_deprecated(self): parser = self._parser_class() deprecated_list = [('DEFAULT', 'bar')] parser._check_deprecated(('DEFAULT', 'bar'), (None, 'foo'), deprecated_list) self.assert_message_logged('bar', 'DEFAULT', 'foo', 'DEFAULT') def assert_message_logged(self, deprecated_name, deprecated_group, current_name, current_group): expected = (self._parser_class._deprecated_opt_message % {'dep_option': deprecated_name, 'dep_group': deprecated_group, 'option': current_name, 'group': current_group} ) self.assertEqual(expected + '\n', self.log_fixture.output) def test_deprecated_for_removal(self): self.conf.register_opt(cfg.StrOpt('foo', deprecated_for_removal=True)) self.conf.register_opt(cfg.StrOpt('bar', deprecated_for_removal=True)) paths = self.create_tempfiles([('test', '[DEFAULT]\n' + 'foo=bar\n')]) self.conf(['--config-file', paths[0]]) # Multiple references should be logged only once. self.assertEqual('bar', self.conf.foo) self.assertEqual('bar', self.conf.foo) # Options not set in the config should not be logged. self.assertIsNone(self.conf.bar) expected = ('Option "foo" from group "DEFAULT" is deprecated for ' 'removal. Its value may be silently ignored in the ' 'future.\n') self.assertEqual(expected, self.log_fixture.output) def test_deprecated_for_removal_with_group(self): self.conf.register_group(cfg.OptGroup('other')) self.conf.register_opt(cfg.StrOpt('foo', deprecated_for_removal=True), group='other') self.conf.register_opt(cfg.StrOpt('bar', deprecated_for_removal=True), group='other') paths = self.create_tempfiles([('test', '[other]\n' + 'foo=bar\n')]) self.conf(['--config-file', paths[0]]) # Multiple references should be logged only once. self.assertEqual('bar', self.conf.other.foo) self.assertEqual('bar', self.conf.other.foo) # Options not set in the config should not be logged. self.assertIsNone(self.conf.other.bar) expected = ('Option "foo" from group "other" is deprecated for ' 'removal. Its value may be silently ignored in the ' 'future.\n') self.assertEqual(expected, self.log_fixture.output) def test_deprecated_with_dest(self): self.conf.register_group(cfg.OptGroup('other')) self.conf.register_opt(cfg.StrOpt('foo-bar', deprecated_name='bar', dest='foo'), group='other') content = 'bar=baz' paths = self.create_tempfiles([('test', '[other]\n' + content + '\n')]) self.conf(['--config-file', paths[0]]) self.assertEqual('baz', self.conf.other.foo) expected = (self._parser_class._deprecated_opt_message % {'dep_option': 'bar', 'dep_group': 'other', 'option': 'foo-bar', 'group': 'other'} + '\n') self.assertEqual(expected, self.log_fixture.output) oslo.config-5.2.0/oslo_config/tests/test_sphinxext.py0000666000175100017510000003612613224676105023110 0ustar zuulzuul00000000000000# 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 textwrap import mock from oslotest import base from oslo_config import cfg from oslo_config import sphinxext class FormatGroupTest(base.BaseTestCase): def test_none_in_default(self): # option with None group placed in DEFAULT results = '\n'.join(list(sphinxext._format_group( app=mock.Mock(), namespace=None, group_name=None, group_obj=None, opt_list=[ cfg.StrOpt('opt_name', help='this appears in the default group'), ], ))) self.assertEqual(textwrap.dedent(''' .. oslo.config:group:: DEFAULT .. oslo.config:option:: opt_name :Type: string :Default: ```` this appears in the default group ''').lstrip(), results) def test_with_default_value(self): results = '\n'.join(list(sphinxext._format_group( app=mock.Mock(), namespace=None, group_name=None, group_obj=None, opt_list=[ cfg.StrOpt('opt_name', default='this is the default', help='this appears in the default group'), ], ))) self.assertEqual(textwrap.dedent(''' .. oslo.config:group:: DEFAULT .. oslo.config:option:: opt_name :Type: string :Default: ``this is the default`` this appears in the default group ''').lstrip(), results) def test_with_min(self): results = '\n'.join(list(sphinxext._format_group( app=mock.Mock(), namespace=None, group_name=None, group_obj=None, opt_list=[ cfg.IntOpt('opt_name', min=1), ], ))) self.assertEqual(textwrap.dedent(''' .. oslo.config:group:: DEFAULT .. oslo.config:option:: opt_name :Type: integer :Default: ```` :Minimum Value: 1 ''').lstrip(), results) def test_with_min_0(self): results = '\n'.join(list(sphinxext._format_group( app=mock.Mock(), namespace=None, group_name=None, group_obj=None, opt_list=[ cfg.IntOpt('opt_name', min=0), ], ))) self.assertEqual(textwrap.dedent(''' .. oslo.config:group:: DEFAULT .. oslo.config:option:: opt_name :Type: integer :Default: ```` :Minimum Value: 0 ''').lstrip(), results) def test_with_max(self): results = '\n'.join(list(sphinxext._format_group( app=mock.Mock(), namespace=None, group_name=None, group_obj=None, opt_list=[ cfg.IntOpt('opt_name', max=1), ], ))) self.assertEqual(textwrap.dedent(''' .. oslo.config:group:: DEFAULT .. oslo.config:option:: opt_name :Type: integer :Default: ```` :Maximum Value: 1 ''').lstrip(), results) def test_with_max_0(self): results = '\n'.join(list(sphinxext._format_group( app=mock.Mock(), namespace=None, group_name=None, group_obj=None, opt_list=[ cfg.IntOpt('opt_name', max=0), ], ))) self.assertEqual(textwrap.dedent(''' .. oslo.config:group:: DEFAULT .. oslo.config:option:: opt_name :Type: integer :Default: ```` :Maximum Value: 0 ''').lstrip(), results) def test_with_choices(self): results = '\n'.join(list(sphinxext._format_group( app=mock.Mock(), namespace=None, group_name=None, group_obj=None, opt_list=[ cfg.StrOpt('opt_name', choices=['a', 'b', 'c', None, '']), ], ))) self.assertEqual(textwrap.dedent(''' .. oslo.config:group:: DEFAULT .. oslo.config:option:: opt_name :Type: string :Default: ```` :Valid Values: a, b, c, , '' ''').lstrip(), results) def test_with_choices_with_descriptions(self): results = '\n'.join(list(sphinxext._format_group( app=mock.Mock(), namespace=None, group_name=None, group_obj=None, opt_list=[ cfg.StrOpt( 'opt_name', choices=[ ('a', 'a is the best'), ('b', 'Actually, may-b I am better'), ('c', 'c, I am clearly the greatest'), (None, 'I am having none of this'), ('', '')]), ], ))) self.assertEqual(textwrap.dedent(''' .. oslo.config:group:: DEFAULT .. oslo.config:option:: opt_name :Type: string :Default: ```` :Valid Values: a, b, c, , '' .. rubric:: Possible values a a is the best b Actually, may-b I am better c c, I am clearly the greatest I am having none of this '' ''').lstrip(), results) def test_group_obj_without_help(self): # option with None group placed in DEFAULT results = '\n'.join(list(sphinxext._format_group( app=mock.Mock(), namespace=None, group_name='group', group_obj=cfg.OptGroup('group'), opt_list=[cfg.StrOpt('opt_name')], ))) self.assertEqual(textwrap.dedent(''' .. oslo.config:group:: group .. oslo.config:option:: opt_name :Type: string :Default: ```` ''').lstrip(), results) def test_group_obj_with_help(self): # option with None group placed in DEFAULT results = '\n'.join(list(sphinxext._format_group( app=mock.Mock(), namespace=None, group_name='group', group_obj=cfg.OptGroup('group', help='group help'), opt_list=[cfg.StrOpt('opt_name')], ))) self.assertEqual(textwrap.dedent(''' .. oslo.config:group:: group group help .. oslo.config:option:: opt_name :Type: string :Default: ```` ''').lstrip(), results) def test_deprecated_opts_without_deprecated_group(self): results = '\n'.join(list(sphinxext._format_group( app=mock.Mock(), namespace=None, group_name=None, group_obj=None, opt_list=[ cfg.StrOpt('opt_name', deprecated_name='deprecated_name', ) ], ))) self.assertEqual(textwrap.dedent(''' .. oslo.config:group:: DEFAULT .. oslo.config:option:: opt_name :Type: string :Default: ```` .. list-table:: Deprecated Variations :header-rows: 1 - * Group * Name - * DEFAULT * deprecated_name ''').lstrip(), results) def test_deprecated_opts_with_deprecated_group(self): results = '\n'.join(list(sphinxext._format_group( app=mock.Mock(), namespace=None, group_name=None, group_obj=None, opt_list=[ cfg.StrOpt('opt_name', deprecated_name='deprecated_name', deprecated_group='deprecated_group', ) ], ))) self.assertEqual(textwrap.dedent(''' .. oslo.config:group:: DEFAULT .. oslo.config:option:: opt_name :Type: string :Default: ```` .. list-table:: Deprecated Variations :header-rows: 1 - * Group * Name - * deprecated_group * deprecated_name ''').lstrip(), results) def test_deprecated_for_removal(self): results = '\n'.join(list(sphinxext._format_group( app=mock.Mock(), namespace=None, group_name=None, group_obj=None, opt_list=[ cfg.StrOpt('opt_name', deprecated_for_removal=True, deprecated_reason='because I said so', deprecated_since='13.0', ) ], ))) self.assertIn('.. warning::', results) self.assertIn('because I said so', results) self.assertIn('since 13.0', results) def test_mutable(self): results = '\n'.join(list(sphinxext._format_group( app=mock.Mock(), namespace=None, group_name=None, group_obj=None, opt_list=[ cfg.IntOpt('opt_name', mutable=True), ], ))) self.assertEqual(textwrap.dedent(''' .. oslo.config:group:: DEFAULT .. oslo.config:option:: opt_name :Type: integer :Default: ```` :Mutable: This option can be changed without restarting. ''').lstrip(), results) def test_not_mutable(self): results = '\n'.join(list(sphinxext._format_group( app=mock.Mock(), namespace=None, group_name=None, group_obj=None, opt_list=[ cfg.IntOpt('opt_name', mutable=False), ], ))) self.assertEqual(textwrap.dedent(''' .. oslo.config:group:: DEFAULT .. oslo.config:option:: opt_name :Type: integer :Default: ```` ''').lstrip(), results) def test_advanced(self): results = '\n'.join(list(sphinxext._format_group( app=mock.Mock(), namespace=None, group_name=None, group_obj=None, opt_list=[ cfg.StrOpt('opt_name', advanced=True), ], ))) self.assertEqual(textwrap.dedent(''' .. oslo.config:group:: DEFAULT .. oslo.config:option:: opt_name :Type: string :Default: ```` :Advanced Option: Intended for advanced users and not used by the majority of users, and might have a significant effect on stability and/or performance. ''').lstrip(), results) def test_not_advanced(self): results = '\n'.join(list(sphinxext._format_group( app=mock.Mock(), namespace=None, group_name=None, group_obj=None, opt_list=[ cfg.StrOpt('opt_name', advanced=False), ], ))) self.assertEqual(textwrap.dedent(''' .. oslo.config:group:: DEFAULT .. oslo.config:option:: opt_name :Type: string :Default: ```` ''').lstrip(), results) class FormatOptionHelpTest(base.BaseTestCase): @mock.patch('oslo_config.generator._list_opts') @mock.patch('oslo_config.sphinxext._format_group') def test_split_namespaces(self, _format_group, _list_opts): _list_opts.return_value = [ ('namespace1', [(None, ['opt1'])]), ('namespace2', [(None, ['opt2'])]), ] list(sphinxext._format_option_help( app=None, namespaces=['namespace1', 'namespace2'], split_namespaces=True)) _format_group.assert_any_call( app=None, namespace='namespace1', group_name='DEFAULT', group_obj=None, opt_list=['opt1'], ) _format_group.assert_any_call( app=None, namespace='namespace2', group_name='DEFAULT', group_obj=None, opt_list=['opt2'], ) @mock.patch('oslo_config.generator._list_opts') @mock.patch('oslo_config.sphinxext._format_group') def test_dont_split_namespaces(self, _format_group, _list_opts): _list_opts.return_value = [ ('namespace1', [(None, ['opt1'])]), ('namespace2', [(None, ['opt2'])]), ] list(sphinxext._format_option_help( app=None, namespaces=['namespace1', 'namespace2'], split_namespaces=False)) _format_group.assert_called_once_with( app=None, namespace=None, group_name='DEFAULT', group_obj=None, opt_list=['opt1', 'opt2'], ) @mock.patch('oslo_config.generator._list_opts') @mock.patch('oslo_config.sphinxext._format_group') def test_dont_split_namespaces_with_group(self, _format_group, _list_opts): grp_obj = cfg.OptGroup('grp1') _list_opts.return_value = [ ('namespace1', [(grp_obj, ['opt1'])]), ('namespace2', [('grp1', ['opt2'])]), ] list(sphinxext._format_option_help( app=None, namespaces=['namespace1', 'namespace2'], split_namespaces=False)) _format_group.assert_any_call( app=None, namespace=None, group_name='grp1', group_obj=grp_obj, opt_list=['opt1', 'opt2'], ) @mock.patch('oslo_config.generator._list_opts') @mock.patch('oslo_config.sphinxext._format_group') def test_split_namespaces_with_group(self, _format_group, _list_opts): grp_obj = cfg.OptGroup('grp1') _list_opts.return_value = [ ('namespace1', [(grp_obj, ['opt1'])]), ('namespace2', [('grp1', ['opt2'])]), ] list(sphinxext._format_option_help( app=None, namespaces=['namespace1', 'namespace2'], split_namespaces=True)) print(_format_group.call_args_list) _format_group.assert_any_call( app=None, namespace='namespace1', group_name='grp1', group_obj=grp_obj, opt_list=['opt1'], ) _format_group.assert_any_call( app=None, namespace='namespace2', group_name='grp1', group_obj=None, opt_list=['opt2'], ) oslo.config-5.2.0/oslo_config/tests/testmods/0000775000175100017510000000000013224676374021305 5ustar zuulzuul00000000000000oslo.config-5.2.0/oslo_config/tests/testmods/fbar_foo_opt.py0000666000175100017510000000131213224676052024306 0ustar zuulzuul00000000000000# Copyright 2013 Red Hat, Inc. # # 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 CONF = cfg.CONF opt = cfg.StrOpt('foo') CONF.register_opt(opt, group='fbar') oslo.config-5.2.0/oslo_config/tests/testmods/fbaar_baa_opt.py0000666000175100017510000000131313224676052024410 0ustar zuulzuul00000000000000# Copyright 2012 Red Hat, Inc. # # 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 CONF = cfg.CONF opt = cfg.StrOpt('baa') CONF.register_opt(opt, group='fbaar') oslo.config-5.2.0/oslo_config/tests/testmods/__init__.py0000666000175100017510000000000013224676052023377 0ustar zuulzuul00000000000000oslo.config-5.2.0/oslo_config/tests/testmods/baz_qux_opt.py0000666000175100017510000000130713224676052024206 0ustar zuulzuul00000000000000# Copyright (c) 2013 Intel Corporation. # # 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 CONF = cfg.CONF CONF.register_opt(cfg.StrOpt('baz'), group='qux') oslo.config-5.2.0/oslo_config/tests/testmods/fblaa_opt.py0000666000175100017510000000127613224676052023607 0ustar zuulzuul00000000000000# Copyright 2012 Red Hat, Inc. # # 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 CONF = cfg.CONF opt = cfg.StrOpt('fblaa') CONF.register_opt(opt) oslo.config-5.2.0/oslo_config/tests/testmods/blaa_opt.py0000666000175100017510000000126213224676052023434 0ustar zuulzuul00000000000000# Copyright 2012 Red Hat, Inc. # # 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 CONF = cfg.CONF CONF.register_opt(cfg.StrOpt('blaa')) oslo.config-5.2.0/oslo_config/tests/testmods/bar_foo_opt.py0000666000175100017510000000127613224676052024151 0ustar zuulzuul00000000000000# Copyright 2012 Red Hat, Inc. # # 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 CONF = cfg.CONF CONF.register_opt(cfg.StrOpt('foo'), group='bar') oslo.config-5.2.0/oslo_config/tests/__init__.py0000666000175100017510000000000013224676052021535 0ustar zuulzuul00000000000000oslo.config-5.2.0/oslo_config/tests/test_fixture.conf0000666000175100017510000000010613224676052023027 0ustar zuulzuul00000000000000[DEFAULT] first_test_opt=loaded_value_1 second_test_opt=loaded_value_2oslo.config-5.2.0/oslo_config/tests/test_iniparser.py0000666000175100017510000000743113224676052023050 0ustar zuulzuul00000000000000# Copyright 2012 OpenStack Foundation # # 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 unittest from oslo_config import iniparser class TestParser(iniparser.BaseParser): comment_called = False values = None section = '' def __init__(self): self.values = {} def assignment(self, key, value): self.values.setdefault(self.section, {}) self.values[self.section][key] = value def new_section(self, section): self.section = section def comment(self, section): self.comment_called = True class BaseParserTestCase(unittest.TestCase): def setUp(self): self.parser = iniparser.BaseParser() def _assertParseError(self, *lines): self.assertRaises(iniparser.ParseError, self.parser.parse, lines) def test_invalid_assignment(self): self._assertParseError("foo - bar") def test_empty_key(self): self._assertParseError(": bar") def test_unexpected_continuation(self): self._assertParseError(" baz") def test_invalid_section(self): self._assertParseError("[section") def test_no_section_name(self): self._assertParseError("[]") class ParserTestCase(unittest.TestCase): def setUp(self): self.parser = TestParser() def test_blank_line(self): lines = [""] self.parser.parse(lines) self.assertEqual({}, self.parser.values) def test_assignment_equal(self): lines = ["foo = bar"] self.parser.parse(lines) self.assertEqual({'': {'foo': ['bar']}}, self.parser.values) def test_assignment_colon(self): lines = ["foo: bar"] self.parser.parse(lines) self.assertEqual({'': {'foo': ['bar']}}, self.parser.values) def test_assignment_multiline(self): lines = ["foo = bar0", " bar1"] self.parser.parse(lines) self.assertEqual({'': {'foo': ['bar0', 'bar1']}}, self.parser.values) def test_assignment_multline_empty(self): lines = ["foo = bar0", "", " bar1"] self.assertRaises(iniparser.ParseError, self.parser.parse, lines) def test_section_assignment(self): lines = ["[test]", "foo = bar"] self.parser.parse(lines) self.assertEqual({'test': {'foo': ['bar']}}, self.parser.values) def test_new_section(self): lines = ["[foo]"] self.parser.parse(lines) self.assertEqual('foo', self.parser.section) def test_comment(self): lines = ["# foobar"] self.parser.parse(lines) self.assertTrue(self.parser.comment_called) def test_empty_assignment(self): lines = ["foo = "] self.parser.parse(lines) self.assertEqual({'': {'foo': ['']}}, self.parser.values) def test_assignment_space_single_quote(self): lines = ["foo = ' bar '"] self.parser.parse(lines) self.assertEqual({'': {'foo': [' bar ']}}, self.parser.values) def test_assignment_space_double_quote(self): lines = ["foo = \" bar \""] self.parser.parse(lines) self.assertEqual({'': {'foo': [' bar ']}}, self.parser.values) class ExceptionTestCase(unittest.TestCase): def test_parseerror(self): exc = iniparser.ParseError('test', 42, 'example') self.assertEqual(str(exc), "at line 42, test: 'example'") oslo.config-5.2.0/oslo_config/tests/test_sphinxconfiggen.py0000666000175100017510000001324113224676052024241 0ustar zuulzuul00000000000000# 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 from oslotest import base from oslo_config import sphinxconfiggen class SingleSampleGenerationTest(base.BaseTestCase): @mock.patch('os.path.isdir') @mock.patch('os.path.isfile') @mock.patch('oslo_config.generator.main') def test_sample_gen_with_single_config_file(self, main, isfile, isdir): isfile.side_effect = [False, True] isdir.return_value = True config = mock.Mock(config_generator_config_file='nova-gen.conf', sample_config_basename='nova') app = mock.Mock(srcdir='/opt/nova', config=config) sphinxconfiggen.generate_sample(app) main.assert_called_once_with(args=['--config-file', '/opt/nova/nova-gen.conf', '--output-file', '/opt/nova/nova.conf.sample' ]) @mock.patch('os.path.isdir') @mock.patch('os.path.isfile') @mock.patch('oslo_config.generator.main') def test_sample_gen_with_single_config_file_no_base(self, main, isfile, isdir): isfile.side_effect = [False, True] isdir.return_value = True config = mock.Mock(config_generator_config_file='nova-gen.conf', sample_config_basename=None) app = mock.Mock(srcdir='/opt/nova', config=config) sphinxconfiggen.generate_sample(app) main.assert_called_once_with(args=['--config-file', '/opt/nova/nova-gen.conf', '--output-file', '/opt/nova/sample.config']) class MultipleSampleGenerationTest(base.BaseTestCase): @mock.patch('os.path.isdir') @mock.patch('os.path.isfile') @mock.patch('oslo_config.generator.main') def test_multi_sample_gen(self, main, isfile, isdir): isfile.side_effect = [False, True, False, True] isdir.return_value = True multiple_configs = [('glance-api-gen.conf', 'glance-api'), ('glance-reg-gen.conf', 'glance-reg')] config = mock.Mock(config_generator_config_file=multiple_configs) app = mock.Mock(srcdir='/opt/glance', config=config) sphinxconfiggen.generate_sample(app) self.assertEqual(main.call_count, 2) main.assert_any_call(args=['--config-file', '/opt/glance/glance-api-gen.conf', '--output-file', '/opt/glance/glance-api.conf.sample']) main.assert_any_call(args=['--config-file', '/opt/glance/glance-reg-gen.conf', '--output-file', '/opt/glance/glance-reg.conf.sample']) @mock.patch('os.path.isdir') @mock.patch('os.path.isfile') @mock.patch('oslo_config.generator.main') def test_multi_sample_gen_with_without_one_base(self, main, isfile, isdir): isfile.side_effect = [False, True, False, True] isdir.return_value = True multiple_configs = [('glance-api-gen.conf', 'glance-api'), ('glance-reg-gen.conf', None)] config = mock.Mock(config_generator_config_file=multiple_configs) app = mock.Mock(srcdir='/opt/glance', config=config) sphinxconfiggen.generate_sample(app) self.assertEqual(main.call_count, 2) main.assert_any_call(args=['--config-file', '/opt/glance/glance-api-gen.conf', '--output-file', '/opt/glance/glance-api.conf.sample']) main.assert_any_call(args=['--config-file', '/opt/glance/glance-reg-gen.conf', '--output-file', '/opt/glance/glance-reg-gen.conf.sample']) @mock.patch('os.path.isdir') @mock.patch('os.path.isfile') @mock.patch('oslo_config.generator.main') def test_multi_sample_gen_with_without_any_base(self, main, isfile, isdir): isfile.side_effect = [False, True, False, True] isdir.return_value = True multiple_configs = [('glance-api-gen.conf', None), ('glance-reg-gen.conf', None)] config = mock.Mock(config_generator_config_file=multiple_configs) app = mock.Mock(srcdir='/opt/glance', config=config) sphinxconfiggen.generate_sample(app) self.assertEqual(main.call_count, 2) main.assert_any_call(args=['--config-file', '/opt/glance/glance-api-gen.conf', '--output-file', '/opt/glance/glance-api-gen.conf.sample']) main.assert_any_call(args=['--config-file', '/opt/glance/glance-reg-gen.conf', '--output-file', '/opt/glance/glance-reg-gen.conf.sample']) oslo.config-5.2.0/oslo_config/tests/test_fixture.py0000666000175100017510000001716713224676052022551 0ustar zuulzuul00000000000000# # Copyright 2013 Mirantis, Inc. # Copyright 2013 OpenStack Foundation # 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 oslotest import base from oslo_config import cfg from oslo_config import fixture as config class ConfigTestCase(base.BaseTestCase): def _make_fixture(self): conf = cfg.ConfigOpts() config_fixture = config.Config(conf) config_fixture.setUp() config_fixture.register_opt(cfg.StrOpt( 'testing_option', default='initial_value')) config_fixture.register_opt(cfg.IntOpt( 'test2', min=0, default=5)) config_fixture.register_opt(cfg.StrOpt( 'test3', choices=['a', 'b'], default='a')) return config_fixture def test_overridden_value(self): f = self._make_fixture() self.assertEqual(f.conf.get('testing_option'), 'initial_value') f.config(testing_option='changed_value') self.assertEqual('changed_value', f.conf.get('testing_option')) def test_overridden_value_with_wrong_type(self): f = self._make_fixture() self.assertEqual(5, f.conf.get('test2')) self.assertEqual('a', f.conf.get('test3')) # enforce type will always be true now self.assertRaises(ValueError, f.config, test2=-1) self.assertRaises(ValueError, f.config, test3='c') def test_cleanup(self): f = self._make_fixture() f.config(testing_option='changed_value') self.assertEqual(f.conf.get('testing_option'), 'changed_value') f.conf.reset() self.assertEqual(f.conf.get('testing_option'), 'initial_value') def test_register_option(self): f = self._make_fixture() opt = cfg.StrOpt('new_test_opt', default='initial_value') f.register_opt(opt) self.assertEqual(f.conf.get('new_test_opt'), opt.default) def test_register_options(self): f = self._make_fixture() opt1 = cfg.StrOpt('first_test_opt', default='initial_value_1') opt2 = cfg.StrOpt('second_test_opt', default='initial_value_2') f.register_opts([opt1, opt2]) self.assertEqual(f.conf.get('first_test_opt'), opt1.default) self.assertEqual(f.conf.get('second_test_opt'), opt2.default) def test_cleanup_unregister_option(self): f = self._make_fixture() opt = cfg.StrOpt('new_test_opt', default='initial_value') f.register_opt(opt) self.assertEqual(f.conf.get('new_test_opt'), opt.default) f.cleanUp() self.assertRaises(cfg.NoSuchOptError, f.conf.get, 'new_test_opt') def test_register_cli_option(self): f = self._make_fixture() opt = cfg.StrOpt('new_test_opt', default='initial_value') f.register_cli_opt(opt) self.assertEqual(f.conf.get('new_test_opt'), opt.default) def test_register_cli_options(self): f = self._make_fixture() opt1 = cfg.StrOpt('first_test_opt', default='initial_value_1') opt2 = cfg.StrOpt('second_test_opt', default='initial_value_2') f.register_cli_opts([opt1, opt2]) self.assertEqual(f.conf.get('first_test_opt'), opt1.default) self.assertEqual(f.conf.get('second_test_opt'), opt2.default) def test_cleanup_unregister_cli_option(self): f = self._make_fixture() opt = cfg.StrOpt('new_test_opt', default='initial_value') f.register_cli_opt(opt) self.assertEqual(f.conf.get('new_test_opt'), opt.default) f.cleanUp() self.assertRaises(cfg.NoSuchOptError, f.conf.get, 'new_test_opt') def test_load_raw_values(self): f = self._make_fixture() f.load_raw_values(first_test_opt='loaded_value_1', second_test_opt='loaded_value_2') # Must not be registered. self.assertRaises(cfg.NoSuchOptError, f.conf.get, 'first_test_opt') self.assertRaises(cfg.NoSuchOptError, f.conf.get, 'second_test_opt') opt1 = cfg.StrOpt('first_test_opt', default='initial_value_1') opt2 = cfg.StrOpt('second_test_opt', default='initial_value_2') f.register_opt(opt1) f.register_opt(opt2) self.assertEqual(f.conf.first_test_opt, 'loaded_value_1') self.assertEqual(f.conf.second_test_opt, 'loaded_value_2') # Cleanup. f.cleanUp() # Must no longer be registered. self.assertRaises(cfg.NoSuchOptError, f.conf.get, 'first_test_opt') self.assertRaises(cfg.NoSuchOptError, f.conf.get, 'second_test_opt') # Even when registered, must be default. f.register_opt(opt1) f.register_opt(opt2) self.assertEqual(f.conf.first_test_opt, 'initial_value_1') self.assertEqual(f.conf.second_test_opt, 'initial_value_2') def test_assert_default_files_cleanup(self): """Assert that using the fixture forces a clean list.""" f = self._make_fixture() self.assertNotIn('default_config_files', f.conf) self.assertNotIn('default_config_dirs', f.conf) config_files = ['./test_fixture.conf'] config_dirs = ['./test_fixture.conf.d'] f.set_config_files(config_files) f.set_config_dirs(config_dirs) self.assertEqual(f.conf.default_config_files, config_files) self.assertEqual(f.conf.default_config_dirs, config_dirs) f.cleanUp() self.assertNotIn('default_config_files', f.conf) self.assertNotIn('default_config_dirs', f.conf) def test_load_custom_files(self): f = self._make_fixture() self.assertNotIn('default_config_files', f.conf) config_files = ['./oslo_config/tests/test_fixture.conf'] f.set_config_files(config_files) opt1 = cfg.StrOpt('first_test_opt', default='initial_value_1') opt2 = cfg.StrOpt('second_test_opt', default='initial_value_2') f.register_opt(opt1) f.register_opt(opt2) self.assertEqual('loaded_value_1', f.conf.get('first_test_opt')) self.assertEqual('loaded_value_2', f.conf.get('second_test_opt')) def test_set_default(self): f = self._make_fixture() opt = cfg.StrOpt('new_test_opt', default='initial_value') # Register the option directly so it is not cleaned up by the # fixture. f.conf.register_opt(opt) f.set_default( name='new_test_opt', default='alternate_value', ) self.assertEqual('alternate_value', f.conf.new_test_opt) f.cleanUp() self.assertEqual('initial_value', f.conf.new_test_opt) def test_set_default_group(self): f = self._make_fixture() opt = cfg.StrOpt('new_test_opt', default='initial_value') # Register the option directly so it is not cleaned up by the # fixture. f.conf.register_opt(opt, group='foo') f.set_default( name='new_test_opt', default='alternate_value', group='foo', ) self.assertEqual('alternate_value', f.conf.foo.new_test_opt) f.cleanUp() self.assertEqual('initial_value', f.conf.foo.new_test_opt) oslo.config-5.2.0/oslo_config/tests/test_cfgfilter.py0000666000175100017510000003372313224676052023024 0ustar zuulzuul00000000000000# Copyright 2014 Red Hat, Inc. # # 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 oslotest import base as test_base import testtools from oslo_config import cfg from oslo_config import cfgfilter class BaseTestCase(test_base.BaseTestCase): def setUp(self, conf=None): super(BaseTestCase, self).setUp() if conf is None: self.conf = cfg.ConfigOpts() else: self.conf = conf self.fconf = cfgfilter.ConfigFilter(self.conf) class RegisterTestCase(BaseTestCase): def test_register_opt_default(self): self.fconf.register_opt(cfg.StrOpt('foo', default='bar')) self.assertEqual('bar', self.fconf.foo) self.assertEqual('bar', self.fconf['foo']) self.assertIn('foo', self.fconf) self.assertEqual(['foo'], list(self.fconf)) self.assertEqual(1, len(self.fconf)) self.assertNotIn('foo', self.conf) self.assertEqual(0, len(self.conf)) self.assertRaises(cfg.NoSuchOptError, getattr, self.conf, 'foo') def test_register_opt_none_default(self): self.fconf.register_opt(cfg.StrOpt('foo')) self.assertIsNone(self.fconf.foo) self.assertIsNone(self.fconf['foo']) self.assertIn('foo', self.fconf) self.assertEqual(['foo'], list(self.fconf)) self.assertEqual(1, len(self.fconf)) self.assertNotIn('foo', self.conf) self.assertEqual(0, len(self.conf)) self.assertRaises(cfg.NoSuchOptError, getattr, self.conf, 'foo') def test_register_grouped_opt_default(self): self.fconf.register_opt(cfg.StrOpt('foo', default='bar'), group='blaa') self.assertEqual('bar', self.fconf.blaa.foo) self.assertEqual('bar', self.fconf['blaa']['foo']) self.assertIn('blaa', self.fconf) self.assertIn('foo', self.fconf.blaa) self.assertEqual(['blaa'], list(self.fconf)) self.assertEqual(['foo'], list(self.fconf.blaa)) self.assertEqual(1, len(self.fconf)) self.assertEqual(1, len(self.fconf.blaa)) self.assertNotIn('blaa', self.conf) self.assertEqual(0, len(self.conf)) self.assertRaises(cfg.NoSuchOptError, getattr, self.conf, 'blaa') def test_register_grouped_opt_none_default(self): self.fconf.register_opt(cfg.StrOpt('foo'), group='blaa') self.assertIsNone(self.fconf.blaa.foo) self.assertIsNone(self.fconf['blaa']['foo']) self.assertIn('blaa', self.fconf) self.assertIn('foo', self.fconf.blaa) self.assertEqual(['blaa'], list(self.fconf)) self.assertEqual(['foo'], list(self.fconf.blaa)) self.assertEqual(1, len(self.fconf)) self.assertEqual(1, len(self.fconf.blaa)) self.assertNotIn('blaa', self.conf) self.assertEqual(0, len(self.conf)) self.assertRaises(cfg.NoSuchOptError, getattr, self.conf, 'blaa') def test_register_group(self): group = cfg.OptGroup('blaa') self.fconf.register_group(group) self.fconf.register_opt(cfg.StrOpt('foo'), group=group) self.assertIsNone(self.fconf.blaa.foo) self.assertIsNone(self.fconf['blaa']['foo']) self.assertIn('blaa', self.fconf) self.assertIn('foo', self.fconf.blaa) self.assertEqual(['blaa'], list(self.fconf)) self.assertEqual(['foo'], list(self.fconf.blaa)) self.assertEqual(1, len(self.fconf)) self.assertEqual(1, len(self.fconf.blaa)) self.assertNotIn('blaa', self.conf) self.assertEqual(0, len(self.conf)) self.assertRaises(cfg.NoSuchOptError, getattr, self.conf, 'blaa') def test_register_opts(self): self.fconf.register_opts([cfg.StrOpt('foo'), cfg.StrOpt('bar')]) self.assertIn('foo', self.fconf) self.assertIn('bar', self.fconf) self.assertNotIn('foo', self.conf) self.assertNotIn('bar', self.conf) def test_register_known_cli_opt(self): self.conf.register_opt(cfg.StrOpt('foo')) self.fconf.register_cli_opt(cfg.StrOpt('foo')) self.assertIn('foo', self.fconf) self.assertIn('foo', self.conf) def test_register_unknown_cli_opt(self): with testtools.ExpectedException(cfgfilter.CliOptRegisteredError): self.fconf.register_cli_opt(cfg.StrOpt('foo')) def test_register_known_cli_opts(self): self.conf.register_cli_opts([cfg.StrOpt('foo'), cfg.StrOpt('bar')]) self.fconf.register_cli_opts([cfg.StrOpt('foo'), cfg.StrOpt('bar')]) self.assertIn('foo', self.fconf) self.assertIn('bar', self.fconf) self.assertIn('foo', self.conf) self.assertIn('bar', self.conf) def test_register_unknown_cli_opts(self): self.conf.register_cli_opt(cfg.StrOpt('foo')) with testtools.ExpectedException(cfgfilter.CliOptRegisteredError): self.fconf.register_cli_opts([ cfg.StrOpt('foo'), cfg.StrOpt('bar') ]) def test_register_opts_grouped(self): self.fconf.register_opts([cfg.StrOpt('foo'), cfg.StrOpt('bar')], group='blaa') self.assertIn('foo', self.fconf.blaa) self.assertIn('bar', self.fconf.blaa) self.assertNotIn('blaa', self.conf) def test_register_known_cli_opt_grouped(self): self.conf.register_cli_opt(cfg.StrOpt('foo'), group='blaa') self.fconf.register_cli_opt(cfg.StrOpt('foo'), group='blaa') self.assertIn('foo', self.fconf.blaa) self.assertIn('blaa', self.fconf) self.assertIn('blaa', self.conf) def test_register_unknown_cli_opt_grouped(self): with testtools.ExpectedException(cfgfilter.CliOptRegisteredError): self.fconf.register_cli_opt(cfg.StrOpt('foo'), group='blaa') def test_register_known_cli_opts_grouped(self): self.conf.register_cli_opts([cfg.StrOpt('foo'), cfg.StrOpt('bar')], group='blaa') self.fconf.register_cli_opts([cfg.StrOpt('foo'), cfg.StrOpt('bar')], group='blaa') self.assertIn('foo', self.fconf.blaa) self.assertIn('bar', self.fconf.blaa) self.assertIn('blaa', self.fconf) self.assertIn('blaa', self.conf) def test_register_unknown_opts_grouped(self): self.conf.register_cli_opts([cfg.StrOpt('bar')], group='blaa') with testtools.ExpectedException(cfgfilter.CliOptRegisteredError): self.fconf.register_cli_opts([ cfg.StrOpt('foo'), cfg.StrOpt('bar') ], group='blaa') def test_unknown_opt(self): self.assertNotIn('foo', self.fconf) self.assertEqual(0, len(self.fconf)) self.assertRaises(cfg.NoSuchOptError, getattr, self.fconf, 'foo') self.assertNotIn('blaa', self.conf) def test_blocked_opt(self): self.conf.register_opt(cfg.StrOpt('foo')) self.assertIn('foo', self.conf) self.assertEqual(1, len(self.conf)) self.assertIsNone(self.conf.foo) self.assertNotIn('foo', self.fconf) self.assertEqual(0, len(self.fconf)) self.assertRaises(cfg.NoSuchOptError, getattr, self.fconf, 'foo') def test_already_registered_opt(self): self.conf.register_opt(cfg.StrOpt('foo')) self.fconf.register_opt(cfg.StrOpt('foo')) self.assertIn('foo', self.conf) self.assertEqual(1, len(self.conf)) self.assertIsNone(self.conf.foo) self.assertIn('foo', self.fconf) self.assertEqual(1, len(self.fconf)) self.assertIsNone(self.fconf.foo) self.conf.set_override('foo', 'bar') self.assertEqual('bar', self.conf.foo) self.assertEqual('bar', self.fconf.foo) def test_already_registered_opts(self): self.conf.register_opts([cfg.StrOpt('foo'), cfg.StrOpt('fu')]) self.fconf.register_opts([cfg.StrOpt('foo'), cfg.StrOpt('bu')]) self.assertIn('foo', self.conf) self.assertIn('fu', self.conf) self.assertNotIn('bu', self.conf) self.assertEqual(2, len(self.conf)) self.assertIsNone(self.conf.foo) self.assertIsNone(self.conf.fu) self.assertIn('foo', self.fconf) self.assertIn('bu', self.fconf) self.assertNotIn('fu', self.fconf) self.assertEqual(2, len(self.fconf)) self.assertIsNone(self.fconf.foo) self.assertIsNone(self.fconf.bu) self.conf.set_override('foo', 'bar') self.assertEqual('bar', self.conf.foo) self.assertEqual('bar', self.fconf.foo) def test_already_registered_cli_opt(self): self.conf.register_cli_opt(cfg.StrOpt('foo')) self.fconf.register_cli_opt(cfg.StrOpt('foo')) self.assertIn('foo', self.conf) self.assertEqual(1, len(self.conf)) self.assertIsNone(self.conf.foo) self.assertIn('foo', self.fconf) self.assertEqual(1, len(self.fconf)) self.assertIsNone(self.fconf.foo) self.conf.set_override('foo', 'bar') self.assertEqual('bar', self.conf.foo) self.assertEqual('bar', self.fconf.foo) def test_already_registered_cli_opts(self): self.conf.register_cli_opts([cfg.StrOpt('foo'), cfg.StrOpt('fu')]) self.fconf.register_cli_opts([cfg.StrOpt('foo'), cfg.StrOpt('fu')]) self.assertIn('foo', self.conf) self.assertIn('fu', self.conf) self.assertEqual(2, len(self.conf)) self.assertIsNone(self.conf.foo) self.assertIsNone(self.conf.fu) self.assertIn('foo', self.fconf) self.assertIn('fu', self.fconf) self.assertEqual(2, len(self.fconf)) self.assertIsNone(self.fconf.foo) self.assertIsNone(self.fconf.fu) self.conf.set_override('foo', 'bar') self.assertEqual('bar', self.conf.foo) self.assertEqual('bar', self.fconf.foo) class ImportTestCase(BaseTestCase): def setUp(self): super(ImportTestCase, self).setUp(cfg.CONF) def test_import_opt(self): self.assertFalse(hasattr(self.conf, 'fblaa')) self.conf.import_opt('fblaa', 'oslo_config.tests.testmods.fblaa_opt') self.assertTrue(hasattr(self.conf, 'fblaa')) self.assertFalse(hasattr(self.fconf, 'fblaa')) self.fconf.import_opt('fblaa', 'oslo_config.tests.testmods.fblaa_opt') self.assertTrue(hasattr(self.fconf, 'fblaa')) def test_import_opt_in_group(self): self.assertFalse(hasattr(self.conf, 'fbar')) self.conf.import_opt('foo', 'oslo_config.tests.testmods.fbar_foo_opt', group='fbar') self.assertTrue(hasattr(self.conf, 'fbar')) self.assertTrue(hasattr(self.conf.fbar, 'foo')) self.assertFalse(hasattr(self.fconf, 'fbar')) self.fconf.import_opt('foo', 'oslo_config.tests.testmods.fbar_foo_opt', group='fbar') self.assertTrue(hasattr(self.fconf, 'fbar')) self.assertTrue(hasattr(self.fconf.fbar, 'foo')) def test_import_group(self): self.assertFalse(hasattr(self.conf, 'fbaar')) self.conf.import_group('fbaar', 'oslo_config.tests.testmods.fbaar_baa_opt') self.assertTrue(hasattr(self.conf, 'fbaar')) self.assertTrue(hasattr(self.conf.fbaar, 'baa')) self.assertFalse(hasattr(self.fconf, 'fbaar')) self.fconf.import_group('fbaar', 'oslo_config.tests.testmods.fbaar_baa_opt') self.assertTrue(hasattr(self.fconf, 'fbaar')) self.assertTrue(hasattr(self.fconf.fbaar, 'baa')) class ExposeTestCase(BaseTestCase): def test_expose_opt(self): self.assertFalse(hasattr(self.conf, 'foo')) self.assertFalse(hasattr(self.fconf, 'foo')) self.conf.register_opt(cfg.StrOpt('foo')) self.conf.set_override('foo', 'bar') self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual('bar', self.conf.foo) self.assertFalse(hasattr(self.fconf, 'foo')) self.fconf.expose_opt('foo') self.assertTrue(hasattr(self.conf, 'foo')) self.assertTrue(hasattr(self.fconf, 'foo')) self.assertEqual('bar', self.fconf.foo) def test_expose_opt_with_group(self): self.assertFalse(hasattr(self.conf, 'foo')) self.assertFalse(hasattr(self.fconf, 'foo')) self.conf.register_opt(cfg.StrOpt('foo'), group='group') self.conf.set_override('foo', 'bar', group='group') self.assertTrue(hasattr(self.conf.group, 'foo')) self.assertEqual('bar', self.conf.group.foo) self.assertFalse(hasattr(self.fconf, 'group')) self.fconf.expose_opt('foo', group='group') self.assertTrue(hasattr(self.conf.group, 'foo')) self.assertTrue(hasattr(self.fconf.group, 'foo')) self.assertEqual('bar', self.fconf.group.foo) def test_expose_group(self): self.conf.register_opts([cfg.StrOpt('foo'), cfg.StrOpt('bar')], group='group') self.conf.register_opts([cfg.StrOpt('foo'), cfg.StrOpt('bar')], group='another') self.conf.set_override('foo', 'a', group='group') self.conf.set_override('bar', 'b', group='group') self.fconf.expose_group('group') self.assertEqual('a', self.fconf.group.foo) self.assertEqual('b', self.fconf.group.bar) self.assertFalse(hasattr(self.fconf, 'another')) self.assertTrue(hasattr(self.conf, 'another')) oslo.config-5.2.0/oslo_config/tests/test_types.py0000666000175100017510000010346313224676105022221 0ustar zuulzuul00000000000000# Copyright 2013 Mirantis, Inc. # # 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 re import unittest from oslo_config import types from six.moves import range as compat_range class ConfigTypeTests(unittest.TestCase): def test_none_concrete_class(self): class MyString(types.ConfigType): def __init__(self, type_name='mystring value'): super(MyString, self).__init__(type_name=type_name) self.assertRaises(TypeError, MyString) def test_concrete_class(self): class MyString(types.ConfigType): def __init__(self, type_name='mystring value'): super(MyString, self).__init__(type_name=type_name) def _formatter(self, value): return value MyString() class TypeTestHelper(object): def setUp(self): super(TypeTestHelper, self).setUp() self.type_instance = self.type def assertConvertedValue(self, s, expected): self.assertEqual(expected, self.type_instance(s)) def assertInvalid(self, value): self.assertRaises(ValueError, self.type_instance, value) class StringTypeTests(TypeTestHelper, unittest.TestCase): type = types.String() def test_empty_string_passes(self): self.assertConvertedValue('', '') def test_should_return_same_string_if_valid(self): self.assertConvertedValue('foo bar', 'foo bar') def test_listed_value(self): self.type_instance = types.String(choices=['foo', 'bar']) self.assertConvertedValue('foo', 'foo') def test_listed_value_tuple(self): self.type_instance = types.String(choices=('foo', 'bar')) self.assertConvertedValue('foo', 'foo') def test_listed_value_dict(self): self.type_instance = types.String(choices=[ ('foo', 'ab'), ('bar', 'xy')]) self.assertConvertedValue('foo', 'foo') def test_unlisted_value(self): self.type_instance = types.String(choices=['foo', 'bar']) self.assertInvalid('baz') def test_with_no_values_returns_error(self): self.type_instance = types.String(choices=[]) self.assertInvalid('foo') def test_string_with_non_closed_quote_is_invalid(self): self.type_instance = types.String(quotes=True) self.assertInvalid('"foo bar') self.assertInvalid("'bar baz") def test_quotes_are_stripped(self): self.type_instance = types.String(quotes=True) self.assertConvertedValue('"foo bar"', 'foo bar') def test_trailing_quote_is_ok(self): self.type_instance = types.String(quotes=True) self.assertConvertedValue('foo bar"', 'foo bar"') def test_repr(self): t = types.String() self.assertEqual('String', repr(t)) def test_repr_with_choices(self): t = types.String(choices=['foo', 'bar']) self.assertEqual('String(choices=[\'foo\', \'bar\'])', repr(t)) def test_repr_with_choices_tuple(self): t = types.String(choices=('foo', 'bar')) self.assertEqual('String(choices=[\'foo\', \'bar\'])', repr(t)) def test_repr_with_choices_dict(self): t = types.String(choices=[('foo', 'ab'), ('bar', 'xy')]) self.assertEqual('String(choices=[\'foo\', \'bar\'])', repr(t)) def test_equal(self): self.assertTrue(types.String() == types.String()) def test_equal_with_same_choices(self): t1 = types.String(choices=['foo', 'bar']) t2 = types.String(choices=['foo', 'bar']) t3 = types.String(choices=('foo', 'bar')) t4 = types.String(choices=['bar', 'foo']) t5 = types.String(choices=[('foo', 'ab'), ('bar', 'xy')]) self.assertTrue(t1 == t2 == t3 == t4 == t5) def test_not_equal_with_different_choices(self): t1 = types.String(choices=['foo', 'bar']) t2 = types.String(choices=['foo', 'baz']) t3 = types.String(choices=('foo', 'baz')) self.assertFalse(t1 == t2) self.assertFalse(t1 == t3) def test_equal_with_equal_quote_falgs(self): t1 = types.String(quotes=True) t2 = types.String(quotes=True) self.assertTrue(t1 == t2) def test_not_equal_with_different_quote_falgs(self): t1 = types.String(quotes=False) t2 = types.String(quotes=True) self.assertFalse(t1 == t2) def test_not_equal_to_other_class(self): self.assertFalse(types.String() == types.Integer()) def test_regex_matches(self): self.type_instance = types.String(regex=re.compile("^[A-Z]")) self.assertConvertedValue("Foo", "Foo") def test_regex_matches_uncompiled(self): self.type_instance = types.String(regex="^[A-Z]") self.assertConvertedValue("Foo", "Foo") def test_regex_fails(self): self.type_instance = types.String(regex=re.compile("^[A-Z]")) self.assertInvalid("foo") def test_regex_and_choices_raises(self): self.assertRaises(ValueError, types.String, regex=re.compile("^[A-Z]"), choices=["Foo", "Bar", "baz"]) def test_equal_with_same_regex(self): t1 = types.String(regex=re.compile("^[A-Z]")) t2 = types.String(regex=re.compile("^[A-Z]")) self.assertTrue(t1 == t2) def test_not_equal_with_different_regex(self): t1 = types.String(regex=re.compile("^[A-Z]")) t2 = types.String(regex=re.compile("^[a-z]")) self.assertFalse(t1 == t2) def test_ignore_case(self): self.type_instance = types.String(choices=['foo', 'bar'], ignore_case=True) self.assertConvertedValue('Foo', 'Foo') self.assertConvertedValue('bAr', 'bAr') def test_ignore_case_raises(self): self.type_instance = types.String(choices=['foo', 'bar'], ignore_case=False) self.assertRaises(ValueError, self.assertConvertedValue, 'Foo', 'Foo') def test_regex_and_ignore_case(self): self.type_instance = types.String(regex=re.compile("^[A-Z]"), ignore_case=True) self.assertConvertedValue("foo", "foo") def test_regex_and_ignore_case_str(self): self.type_instance = types.String(regex="^[A-Z]", ignore_case=True) self.assertConvertedValue("foo", "foo") def test_regex_preserve_flags(self): self.type_instance = types.String(regex=re.compile("^[A-Z]", re.I), ignore_case=False) self.assertConvertedValue("foo", "foo") def test_max_length(self): self.type_instance = types.String(max_length=5) self.assertInvalid('123456') self.assertConvertedValue('12345', '12345') class BooleanTypeTests(TypeTestHelper, unittest.TestCase): type = types.Boolean() def test_True(self): self.assertConvertedValue('True', True) def test_yes(self): self.assertConvertedValue('yes', True) def test_on(self): self.assertConvertedValue('on', True) def test_1(self): self.assertConvertedValue('1', True) def test_False(self): self.assertConvertedValue('False', False) def test_no(self): self.assertConvertedValue('no', False) def test_off(self): self.assertConvertedValue('off', False) def test_0(self): self.assertConvertedValue('0', False) def test_other_values_produce_error(self): self.assertInvalid('foo') def test_repr(self): self.assertEqual('Boolean', repr(types.Boolean())) def test_equal(self): self.assertEqual(types.Boolean(), types.Boolean()) def test_not_equal_to_other_class(self): self.assertFalse(types.Boolean() == types.String()) class IntegerTypeTests(TypeTestHelper, unittest.TestCase): type = types.Integer() def test_empty_string(self): self.assertConvertedValue('', None) def test_whitespace_string(self): self.assertConvertedValue(" \t\t\t\t", None) def test_positive_values_are_valid(self): self.assertConvertedValue('123', 123) def test_zero_is_valid(self): self.assertConvertedValue('0', 0) def test_negative_values_are_valid(self): self.assertConvertedValue('-123', -123) def test_leading_whitespace_is_ignored(self): self.assertConvertedValue(' 5', 5) def test_trailing_whitespace_is_ignored(self): self.assertConvertedValue('7 ', 7) def test_non_digits_are_invalid(self): self.assertInvalid('12a45') def test_repr(self): t = types.Integer() self.assertEqual('Integer', repr(t)) def test_repr_with_min(self): t = types.Integer(min=123) self.assertEqual('Integer(min=123)', repr(t)) def test_repr_with_max(self): t = types.Integer(max=456) self.assertEqual('Integer(max=456)', repr(t)) def test_repr_with_min_and_max(self): t = types.Integer(min=123, max=456) self.assertEqual('Integer(min=123, max=456)', repr(t)) t = types.Integer(min=0, max=0) self.assertEqual('Integer(min=0, max=0)', repr(t)) def test_repr_with_choices(self): t = types.Integer(choices=[80, 457]) self.assertEqual('Integer(choices=[80, 457])', repr(t)) def test_repr_with_choices_tuple(self): t = types.Integer(choices=(80, 457)) self.assertEqual('Integer(choices=[80, 457])', repr(t)) def test_repr_with_choices_dict(self): t = types.Integer(choices=[(80, 'ab'), (457, 'xy')]) self.assertEqual('Integer(choices=[80, 457])', repr(t)) def test_equal(self): self.assertTrue(types.Integer() == types.Integer()) def test_equal_with_same_min_and_no_max(self): self.assertTrue(types.Integer(min=123) == types.Integer(min=123)) def test_equal_with_same_max_and_no_min(self): self.assertTrue(types.Integer(max=123) == types.Integer(max=123)) def test_equal_with_same_min_and_max(self): t1 = types.Integer(min=1, max=123) t2 = types.Integer(min=1, max=123) self.assertTrue(t1 == t2) def test_equal_with_same_choices(self): t1 = types.Integer(choices=[80, 457]) t2 = types.Integer(choices=[457, 80]) t3 = types.Integer(choices=(457, 80)) t4 = types.Integer(choices=[(80, 'ab'), (457, 'xy')]) self.assertTrue(t1 == t2 == t3 == t4) def test_not_equal(self): self.assertFalse(types.Integer(min=123) == types.Integer(min=456)) self.assertFalse(types.Integer(choices=[80, 457]) == types.Integer(choices=[80, 40])) self.assertFalse(types.Integer(choices=[80, 457]) == types.Integer()) def test_not_equal_to_other_class(self): self.assertFalse(types.Integer() == types.String()) def test_choices_with_min_max(self): self.assertRaises(ValueError, types.Integer, min=100, choices=[50, 60]) self.assertRaises(ValueError, types.Integer, max=10, choices=[50, 60]) types.Integer(min=10, max=100, choices=[50, 60]) def test_min_greater_max(self): self.assertRaises(ValueError, types.Integer, min=100, max=50) self.assertRaises(ValueError, types.Integer, min=-50, max=-100) self.assertRaises(ValueError, types.Integer, min=0, max=-50) self.assertRaises(ValueError, types.Integer, min=50, max=0) def test_with_max_and_min(self): t = types.Integer(min=123, max=456) self.assertRaises(ValueError, t, 122) t(123) t(300) t(456) self.assertRaises(ValueError, t, 0) self.assertRaises(ValueError, t, 457) def test_with_min_zero(self): t = types.Integer(min=0, max=456) self.assertRaises(ValueError, t, -1) t(0) t(123) t(300) t(456) self.assertRaises(ValueError, t, -201) self.assertRaises(ValueError, t, 457) def test_with_max_zero(self): t = types.Integer(min=-456, max=0) self.assertRaises(ValueError, t, 1) t(0) t(-123) t(-300) t(-456) self.assertRaises(ValueError, t, 201) self.assertRaises(ValueError, t, -457) def _test_with_choices(self, t): self.assertRaises(ValueError, t, 1) self.assertRaises(ValueError, t, 200) self.assertRaises(ValueError, t, -457) t(80) t(457) def test_with_choices_list(self): t = types.Integer(choices=[80, 457]) self._test_with_choices(t) def test_with_choices_tuple(self): t = types.Integer(choices=(80, 457)) self._test_with_choices(t) def test_with_choices_dict(self): t = types.Integer(choices=[(80, 'ab'), (457, 'xy')]) self._test_with_choices(t) class FloatTypeTests(TypeTestHelper, unittest.TestCase): type = types.Float() def test_decimal_format(self): v = self.type_instance('123.456') self.assertAlmostEqual(v, 123.456) def test_decimal_format_negative_float(self): v = self.type_instance('-123.456') self.assertAlmostEqual(v, -123.456) def test_exponential_format(self): v = self.type_instance('123e-2') self.assertAlmostEqual(v, 1.23) def test_non_float_is_invalid(self): self.assertInvalid('123,345') self.assertInvalid('foo') def test_repr(self): self.assertEqual('Float', repr(types.Float())) def test_repr_with_min(self): t = types.Float(min=1.1) self.assertEqual('Float(min=1.1)', repr(t)) def test_repr_with_max(self): t = types.Float(max=2.2) self.assertEqual('Float(max=2.2)', repr(t)) def test_repr_with_min_and_max(self): t = types.Float(min=1.1, max=2.2) self.assertEqual('Float(min=1.1, max=2.2)', repr(t)) t = types.Float(min=1.0, max=2) self.assertEqual('Float(min=1, max=2)', repr(t)) t = types.Float(min=0, max=0) self.assertEqual('Float(min=0, max=0)', repr(t)) def test_equal(self): self.assertTrue(types.Float() == types.Float()) def test_equal_with_same_min_and_no_max(self): self.assertTrue(types.Float(min=123.1) == types.Float(min=123.1)) def test_equal_with_same_max_and_no_min(self): self.assertTrue(types.Float(max=123.1) == types.Float(max=123.1)) def test_not_equal(self): self.assertFalse(types.Float(min=123.1) == types.Float(min=456.1)) self.assertFalse(types.Float(max=123.1) == types.Float(max=456.1)) self.assertFalse(types.Float(min=123.1) == types.Float(max=123.1)) self.assertFalse(types.Float(min=123.1, max=456.1) == types.Float(min=123.1, max=456.2)) def test_not_equal_to_other_class(self): self.assertFalse(types.Float() == types.Integer()) def test_equal_with_same_min_and_max(self): t1 = types.Float(min=1.1, max=2.2) t2 = types.Float(min=1.1, max=2.2) self.assertTrue(t1 == t2) def test_min_greater_max(self): self.assertRaises(ValueError, types.Float, min=100.1, max=50) self.assertRaises(ValueError, types.Float, min=-50, max=-100.1) self.assertRaises(ValueError, types.Float, min=0.1, max=-50.0) self.assertRaises(ValueError, types.Float, min=50.0, max=0.0) def test_with_max_and_min(self): t = types.Float(min=123.45, max=678.9) self.assertRaises(ValueError, t, 123) self.assertRaises(ValueError, t, 123.1) t(124.1) t(300) t(456.0) self.assertRaises(ValueError, t, 0) self.assertRaises(ValueError, t, 800.5) def test_with_min_zero(self): t = types.Float(min=0, max=456.1) self.assertRaises(ValueError, t, -1) t(0.0) t(123.1) t(300.2) t(456.1) self.assertRaises(ValueError, t, -201.0) self.assertRaises(ValueError, t, 457.0) def test_with_max_zero(self): t = types.Float(min=-456.1, max=0) self.assertRaises(ValueError, t, 1) t(0.0) t(-123.0) t(-300.0) t(-456.0) self.assertRaises(ValueError, t, 201.0) self.assertRaises(ValueError, t, -457.0) class ListTypeTests(TypeTestHelper, unittest.TestCase): type = types.List() def test_empty_value(self): self.assertConvertedValue('', []) def test_single_value(self): self.assertConvertedValue(' foo bar ', ['foo bar']) def test_tuple_of_values(self): self.assertConvertedValue(('foo', 'bar'), ['foo', 'bar']) def test_list_of_values(self): self.assertConvertedValue(' foo bar, baz ', ['foo bar', 'baz']) def test_list_of_values_containing_commas(self): self.type_instance = types.List(types.String(quotes=True)) self.assertConvertedValue('foo,"bar, baz",bam', ['foo', 'bar, baz', 'bam']) def test_list_of_lists(self): self.type_instance = types.List( types.List(types.String(), bounds=True) ) self.assertConvertedValue('[foo],[bar, baz],[bam]', [['foo'], ['bar', 'baz'], ['bam']]) def test_list_of_custom_type(self): self.type_instance = types.List(types.Integer()) self.assertConvertedValue('1,2,3,5', [1, 2, 3, 5]) def test_tuple_of_custom_type(self): self.type_instance = types.List(types.Integer()) self.assertConvertedValue(('1', '2', '3', '5'), [1, 2, 3, 5]) def test_bounds_parsing(self): self.type_instance = types.List(types.Integer(), bounds=True) self.assertConvertedValue('[1,2,3]', [1, 2, 3]) def test_bounds_required(self): self.type_instance = types.List(types.Integer(), bounds=True) self.assertInvalid('1,2,3') self.assertInvalid('[1,2,3') self.assertInvalid('1,2,3]') def test_repr(self): t = types.List(types.Integer()) self.assertEqual('List of Integer', repr(t)) def test_equal(self): self.assertTrue(types.List() == types.List()) def test_equal_with_equal_custom_item_types(self): it1 = types.Integer() it2 = types.Integer() self.assertTrue(types.List(it1) == types.List(it2)) def test_not_equal_with_non_equal_custom_item_types(self): it1 = types.Integer() it2 = types.String() self.assertFalse(it1 == it2) self.assertFalse(types.List(it1) == types.List(it2)) def test_not_equal_to_other_class(self): self.assertFalse(types.List() == types.Integer()) class RangeTypeTests(TypeTestHelper, unittest.TestCase): type = types.Range() def assertRange(self, s, r1, r2, step=1): self.assertEqual(list(compat_range(r1, r2, step)), list(self.type_instance(s))) def test_range(self): self.assertRange('0-2', 0, 3) self.assertRange('-2-0', -2, 1) self.assertRange('2-0', 2, -1, -1) self.assertRange('-3--1', -3, 0) self.assertRange('-1--3', -1, -4, -1) self.assertRange('-1', -1, 0) self.assertInvalid('--1') self.assertInvalid('4-') self.assertInvalid('--') self.assertInvalid('1.1-1.2') self.assertInvalid('a-b') def test_range_bounds(self): self.type_instance = types.Range(1, 3) self.assertRange('1-3', 1, 4) self.assertRange('2-2', 2, 3) self.assertRange('2', 2, 3) self.assertInvalid('1-4') self.assertInvalid('0-3') self.assertInvalid('0-4') def test_range_exclusive(self): self.type_instance = types.Range(inclusive=False) self.assertRange('0-2', 0, 2) self.assertRange('-2-0', -2, 0) self.assertRange('2-0', 2, 0, -1) self.assertRange('-3--1', -3, -1) self.assertRange('-1--3', -1, -3, -1) self.assertRange('-1', -1, -1) class DictTypeTests(TypeTestHelper, unittest.TestCase): type = types.Dict() def test_empty_value(self): self.assertConvertedValue('', {}) def test_single_value(self): self.assertConvertedValue(' foo: bar ', {'foo': 'bar'}) def test_dict_of_values(self): self.assertConvertedValue(' foo: bar, baz: 123 ', {'foo': 'bar', 'baz': '123'}) def test_custom_value_type(self): self.type_instance = types.Dict(types.Integer()) self.assertConvertedValue('foo:123, bar: 456', {'foo': 123, 'bar': 456}) def test_dict_of_values_containing_commas(self): self.type_instance = types.Dict(types.String(quotes=True)) self.assertConvertedValue('foo:"bar, baz",bam:quux', {'foo': 'bar, baz', 'bam': 'quux'}) def test_dict_of_dicts(self): self.type_instance = types.Dict( types.Dict(types.String(), bounds=True) ) self.assertConvertedValue('k1:{k1:v1,k2:v2},k2:{k3:v3}', {'k1': {'k1': 'v1', 'k2': 'v2'}, 'k2': {'k3': 'v3'}}) def test_bounds_parsing(self): self.type_instance = types.Dict(types.String(), bounds=True) self.assertConvertedValue('{foo:bar,baz:123}', {'foo': 'bar', 'baz': '123'}) def test_bounds_required(self): self.type_instance = types.Dict(types.String(), bounds=True) self.assertInvalid('foo:bar,baz:123') self.assertInvalid('{foo:bar,baz:123') self.assertInvalid('foo:bar,baz:123}') def test_no_mapping_produces_error(self): self.assertInvalid('foo,bar') def test_repr(self): t = types.Dict(types.Integer()) self.assertEqual('Dict of Integer', repr(t)) def test_equal(self): self.assertTrue(types.Dict() == types.Dict()) def test_equal_with_equal_custom_item_types(self): it1 = types.Integer() it2 = types.Integer() self.assertTrue(types.Dict(it1) == types.Dict(it2)) def test_not_equal_with_non_equal_custom_item_types(self): it1 = types.Integer() it2 = types.String() self.assertFalse(it1 == it2) self.assertFalse(types.Dict(it1) == types.Dict(it2)) def test_not_equal_to_other_class(self): self.assertFalse(types.Dict() == types.Integer()) class IPAddressTypeTests(TypeTestHelper, unittest.TestCase): type = types.IPAddress() def test_ipv4_address(self): self.assertConvertedValue('192.168.0.1', '192.168.0.1') def test_ipv6_address(self): self.assertConvertedValue('abcd:ef::1', 'abcd:ef::1') def test_strings(self): self.assertInvalid('') self.assertInvalid('foo') def test_numbers(self): self.assertInvalid(1) self.assertInvalid(-1) self.assertInvalid(3.14) class IPv4AddressTypeTests(IPAddressTypeTests): type = types.IPAddress(4) def test_ipv6_address(self): self.assertInvalid('abcd:ef::1') class IPv6AddressTypeTests(IPAddressTypeTests): type = types.IPAddress(6) def test_ipv4_address(self): self.assertInvalid('192.168.0.1') class HostAddressTypeTests(TypeTestHelper, unittest.TestCase): type = types.HostAddress() def test_invalid_host_addresses(self): self.assertInvalid('-1') self.assertInvalid('_foo') self.assertInvalid('3.14') self.assertInvalid('10.0') self.assertInvalid('host..name') self.assertInvalid('org.10') self.assertInvalid('0.0.00') def test_valid_host_addresses(self): self.assertConvertedValue('foo.bar', 'foo.bar') self.assertConvertedValue('192.168.0.1', '192.168.0.1') self.assertConvertedValue('abcd:ef::1', 'abcd:ef::1') self.assertConvertedValue('home-site-here.org.com', 'home-site-here.org.com') self.assertConvertedValue('3com.com', '3com.com') self.assertConvertedValue('10.org', '10.org') self.assertConvertedValue('cell1.nova.site1', 'cell1.nova.site1') self.assertConvertedValue('ab-c.com', 'ab-c.com') self.assertConvertedValue('abc.com-org', 'abc.com-org') self.assertConvertedValue('abc.0-0', 'abc.0-0') class HostnameTypeTests(TypeTestHelper, unittest.TestCase): type = types.Hostname() def assertConvertedEqual(self, value): self.assertConvertedValue(value, value) def test_empty_hostname_fails(self): self.assertInvalid('') def test_should_return_same_hostname_if_valid(self): self.assertConvertedEqual('foo.bar') def test_trailing_quote_is_invalid(self): self.assertInvalid('foo.bar"') def test_repr(self): self.assertEqual('Hostname', repr(types.Hostname())) def test_equal(self): self.assertEqual(types.Hostname(), types.Hostname()) def test_not_equal_to_other_class(self): self.assertNotEqual(types.Hostname(), types.Integer()) self.assertNotEqual(types.Hostname(), types.String()) def test_invalid_characters(self): self.assertInvalid('"host"') self.assertInvalid("h'ost'") self.assertInvalid("h'ost") self.assertInvalid("h$ost") self.assertInvalid("h%ost") self.assertInvalid("host_01.co.uk") self.assertInvalid("host;name=99") self.assertInvalid('___site0.1001') self.assertInvalid('_site01001') self.assertInvalid("host..name") self.assertInvalid(".host.name.com") self.assertInvalid("no spaces") def test_invalid_hostnames_with_numeric_characters(self): self.assertInvalid("10.0.0.0") self.assertInvalid("3.14") self.assertInvalid("org.10") self.assertInvalid('0.0.00') def test_no_start_end_hyphens(self): self.assertInvalid("-host.com") self.assertInvalid("-hostname.com-") self.assertInvalid("hostname.co.uk-") def test_strip_trailing_dot(self): self.assertConvertedValue('cell1.nova.site1.', 'cell1.nova.site1') self.assertConvertedValue('cell1.', 'cell1') def test_valid_hostname(self): self.assertConvertedEqual('cell1.nova.site1') self.assertConvertedEqual('site01001') self.assertConvertedEqual('home-site-here.org.com') self.assertConvertedEqual('localhost') self.assertConvertedEqual('3com.com') self.assertConvertedEqual('10.org') self.assertConvertedEqual('10ab.10ab') self.assertConvertedEqual('ab-c.com') self.assertConvertedEqual('abc.com-org') self.assertConvertedEqual('abc.0-0') def test_max_segment_size(self): self.assertConvertedEqual('host.%s.com' % ('x' * 63)) self.assertInvalid('host.%s.com' % ('x' * 64)) def test_max_hostname_size(self): test_str = '.'.join('x'*31 for x in range(8)) self.assertEqual(255, len(test_str)) self.assertInvalid(test_str) self.assertConvertedEqual(test_str[:-2]) class URITypeTests(TypeTestHelper, unittest.TestCase): type = types.URI() def test_uri(self): self.assertConvertedValue('http://example.com', 'http://example.com') self.assertInvalid('invalid') # it doesn't include a scheme self.assertInvalid('http://') # it doesn't include an authority def test_repr(self): self.assertEqual('URI', repr(types.URI())) def test_max_length(self): self.type_instance = types.String(max_length=30) self.assertInvalid('http://www.example.com/versions') self.assertConvertedValue('http://www.example.com', 'http://www.example.com') class PortTypeTests(TypeTestHelper, unittest.TestCase): type = types.Port() def test_port(self): self.assertInvalid(-1) self.assertInvalid(65536) self.assertConvertedValue('80', 80) self.assertConvertedValue('65535', 65535) def test_repr(self): self.assertEqual('Port(min=0, max=65535)', repr(types.Port())) def test_repr_with_min(self): t = types.Port(min=123) self.assertEqual('Port(min=123, max=65535)', repr(t)) def test_repr_with_max(self): t = types.Port(max=456) self.assertEqual('Port(min=0, max=456)', repr(t)) def test_repr_with_min_and_max(self): t = types.Port(min=123, max=456) self.assertEqual('Port(min=123, max=456)', repr(t)) t = types.Port(min=0, max=0) self.assertEqual('Port(min=0, max=0)', repr(t)) def test_repr_with_choices(self): t = types.Port(choices=[80, 457]) self.assertEqual('Port(choices=[80, 457])', repr(t)) def test_repr_with_choices_tuple(self): t = types.Port(choices=(80, 457)) self.assertEqual('Port(choices=[80, 457])', repr(t)) def _test_with_choices(self, t): self.assertRaises(ValueError, t, 1) self.assertRaises(ValueError, t, 200) self.assertRaises(ValueError, t, -457) t(80) t(457) def test_with_choices_list(self): t = types.Port(choices=[80, 457]) self._test_with_choices(t) def test_with_choices_tuple(self): t = types.Port(choices=(80, 457)) self._test_with_choices(t) def test_with_choices_dict(self): t = types.Port(choices=[(80, 'ab'), (457, 'xy')]) self._test_with_choices(t) def test_invalid_choices(self): """Check for choices that are specifically invalid for ports.""" self.assertRaises(ValueError, types.Port, choices=[-1, 457]) self.assertRaises(ValueError, types.Port, choices=[1, 2, 3, 65536]) def test_equal(self): self.assertTrue(types.Port() == types.Port()) def test_equal_with_same_min_and_no_max(self): self.assertTrue(types.Port(min=123) == types.Port(min=123)) def test_equal_with_same_max_and_no_min(self): self.assertTrue(types.Port(max=123) == types.Port(max=123)) def test_equal_with_same_min_and_max(self): t1 = types.Port(min=1, max=123) t2 = types.Port(min=1, max=123) self.assertTrue(t1 == t2) def test_equal_with_same_choices(self): t1 = types.Port(choices=[80, 457]) t2 = types.Port(choices=[457, 80]) t3 = types.Port(choices=(457, 80)) t4 = types.Port(choices=[(457, 'ab'), (80, 'xy')]) self.assertTrue(t1 == t2 == t3 == t4) def test_not_equal(self): self.assertFalse(types.Port(min=123) == types.Port(min=456)) self.assertFalse(types.Port(choices=[80, 457]) == types.Port(choices=[80, 40])) self.assertFalse(types.Port(choices=[80, 457]) == types.Port()) def test_not_equal_to_other_class(self): self.assertFalse(types.Port() == types.Integer()) def test_choices_with_min_max(self): self.assertRaises(ValueError, types.Port, min=100, choices=[50, 60]) self.assertRaises(ValueError, types.Port, max=10, choices=[50, 60]) types.Port(min=10, max=100, choices=[50, 60]) def test_min_greater_max(self): self.assertRaises(ValueError, types.Port, min=100, max=50) self.assertRaises(ValueError, types.Port, min=-50, max=-100) self.assertRaises(ValueError, types.Port, min=0, max=-50) self.assertRaises(ValueError, types.Port, min=50, max=0) def test_illegal_min(self): self.assertRaises(ValueError, types.Port, min=-1, max=50) self.assertRaises(ValueError, types.Port, min=-50) def test_illegal_max(self): self.assertRaises(ValueError, types.Port, min=100, max=65537) self.assertRaises(ValueError, types.Port, max=100000) def test_with_max_and_min(self): t = types.Port(min=123, max=456) self.assertRaises(ValueError, t, 122) t(123) t(300) t(456) self.assertRaises(ValueError, t, 0) self.assertRaises(ValueError, t, 457) def test_with_min_zero(self): t = types.Port(min=0, max=456) self.assertRaises(ValueError, t, -1) t(0) t(123) t(300) t(456) self.assertRaises(ValueError, t, -201) self.assertRaises(ValueError, t, 457) def test_with_max_zero(self): t = types.Port(max=0) self.assertRaises(ValueError, t, 1) t(0) oslo.config-5.2.0/oslo_config/tests/test_generator.py0000666000175100017510000016652313224676105023051 0ustar zuulzuul00000000000000# Copyright 2014 Red Hat, Inc. # # 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 sys import fixtures import mock from oslotest import base from six import moves import tempfile import testscenarios from oslo_config import cfg from oslo_config import fixture as config_fixture from oslo_config import generator from oslo_config import types import yaml load_tests = testscenarios.load_tests_apply_scenarios def custom_type(a): """Something that acts like a type, but isn't known""" return a class GeneratorTestCase(base.BaseTestCase): groups = { 'group1': cfg.OptGroup(name='group1', help='Lorem ipsum dolor sit amet, consectetur ' 'adipisicing elit, sed do eiusmod tempor ' 'incididunt ut labore et dolore magna ' 'aliqua. Ut enim ad minim veniam, quis ' 'nostrud exercitation ullamco laboris ' 'nisi ut aliquip ex ea commodo ' 'consequat. Duis aute irure dolor in.'), 'group2': cfg.OptGroup(name='group2', title='Group 2'), 'foo': cfg.OptGroup(name='foo', title='Foo Title', help='foo help'), } opts = { 'foo': cfg.StrOpt('foo', help='foo option'), 'bar': cfg.StrOpt('bar', help='bar option'), 'foo-bar': cfg.StrOpt('foo-bar', help='foobar'), 'no_help': cfg.StrOpt('no_help'), 'long_help': cfg.StrOpt('long_help', help='Lorem ipsum dolor sit amet, consectetur ' 'adipisicing elit, sed do eiusmod tempor ' 'incididunt ut labore et dolore magna ' 'aliqua. Ut enim ad minim veniam, quis ' 'nostrud exercitation ullamco laboris ' 'nisi ut aliquip ex ea commodo ' 'consequat. Duis aute irure dolor in ' 'reprehenderit in voluptate velit esse ' 'cillum dolore eu fugiat nulla ' 'pariatur. Excepteur sint occaecat ' 'cupidatat non proident, sunt in culpa ' 'qui officia deserunt mollit anim id est ' 'laborum.'), 'long_help_pre': cfg.StrOpt('long_help_pre', help='This is a very long help text which ' 'is preformatted with line breaks. ' 'It should break when it is too long ' 'but also keep the specified line ' 'breaks. This makes it possible to ' 'create lists with items:\n' '\n' '* item 1\n' '* item 2\n' '\n' 'and should increase the ' 'readability.'), 'choices_opt': cfg.StrOpt('choices_opt', default='a', choices=(None, '', 'a', 'b', 'c'), help='a string with choices'), 'deprecated_opt_without_deprecated_group': cfg.StrOpt( 'bar', deprecated_name='foobar', help='deprecated'), 'deprecated_for_removal_opt': cfg.StrOpt( 'bar', deprecated_for_removal=True, help='deprecated for removal'), 'deprecated_reason_opt': cfg.BoolOpt( 'turn_off_stove', default=False, deprecated_for_removal=True, deprecated_reason='This was supposed to work but it really, ' 'really did not. Always buy house insurance.', help='DEPRECATED: Turn off stove'), 'deprecated_opt_with_deprecated_since': cfg.BoolOpt( 'tune_in', deprecated_for_removal=True, deprecated_since='13.0'), 'deprecated_opt_with_deprecated_group': cfg.StrOpt( 'bar', deprecated_name='foobar', deprecated_group='group1', help='deprecated'), 'opt_with_DeprecatedOpt': cfg.BoolOpt( 'foo-bar', help='Opt with DeprecatedOpt', deprecated_opts = [cfg.DeprecatedOpt('foo-bar', group='deprecated')]), # Unknown Opt default must be a string 'unknown_type': cfg.Opt('unknown_opt', default='123', help='unknown', type=types.String(type_name='unknown type')), 'str_opt': cfg.StrOpt('str_opt', default='foo bar', help='a string'), 'str_opt_sample_default': cfg.StrOpt('str_opt', default='fooishbar', help='a string'), 'str_opt_with_space': cfg.StrOpt('str_opt', default=' foo bar ', help='a string with spaces'), 'str_opt_multiline': cfg.StrOpt('str_opt', default='foo\nbar\nbaz', help='a string with newlines'), 'bool_opt': cfg.BoolOpt('bool_opt', default=False, help='a boolean'), 'int_opt': cfg.IntOpt('int_opt', default=10, min=1, max=20, help='an integer'), 'int_opt_min_0': cfg.IntOpt('int_opt_min_0', default=10, min=0, max=20, help='an integer'), 'int_opt_max_0': cfg.IntOpt('int_opt_max_0', default=-1, max=0, help='an integer'), 'float_opt': cfg.FloatOpt('float_opt', default=0.1, help='a float'), 'list_opt': cfg.ListOpt('list_opt', default=['1', '2', '3'], help='a list'), 'list_opt_single': cfg.ListOpt('list_opt_single', default='1', help='a list'), 'list_int_opt': cfg.ListOpt('list_int_opt', default=[1, 2, 3], help='a list'), 'dict_opt': cfg.DictOpt('dict_opt', default={'1': 'yes', '2': 'no'}, help='a dict'), 'ip_opt': cfg.IPOpt('ip_opt', default='127.0.0.1', help='an ip address'), 'port_opt': cfg.PortOpt('port_opt', default=80, help='a port'), 'hostname_opt': cfg.HostnameOpt('hostname_opt', default='compute01.nova.site1', help='a hostname'), 'uri_opt': cfg.URIOpt('uri_opt', default='http://example.com', help='a URI'), 'multi_opt': cfg.MultiStrOpt('multi_opt', default=['1', '2', '3'], help='multiple strings'), 'multi_opt_none': cfg.MultiStrOpt('multi_opt_none', help='multiple strings'), 'multi_opt_empty': cfg.MultiStrOpt('multi_opt_empty', default=[], help='multiple strings'), 'multi_opt_sample_default': cfg.MultiStrOpt('multi_opt', default=['1', '2', '3'], sample_default=['5', '6'], help='multiple strings'), 'string_type_with_bad_default': cfg.Opt('string_type_with_bad_default', help='string with bad default', default=4096), 'native_str_type': cfg.Opt('native_str_type', help='native help', type=str), 'native_int_type': cfg.Opt('native_int_type', help='native help', type=int), 'native_float_type': cfg.Opt('native_float_type', help='native help', type=float), 'custom_type': cfg.Opt('custom_type', help='custom help', type=custom_type), 'custom_type_name': cfg.Opt('custom_opt_type', type=types.Integer(type_name='port' ' number'), default=5511, help='this is a port'), } content_scenarios = [ ('empty', dict(opts=[], expected='''[DEFAULT] ''')), ('single_namespace', dict(opts=[('test', [(None, [opts['foo']])])], expected='''[DEFAULT] # # From test # # foo option (string value) #foo = ''')), ('multiple_namespaces', dict(opts=[('test', [(None, [opts['foo']])]), ('other', [(None, [opts['bar']])])], expected='''[DEFAULT] # # From other # # bar option (string value) #bar = # # From test # # foo option (string value) #foo = ''')), ('group', dict(opts=[('test', [(groups['group1'], [opts['foo']])])], expected='''[DEFAULT] [group1] # Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do # eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim # ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut # aliquip ex ea commodo consequat. Duis aute irure dolor in. # # From test # # foo option (string value) #foo = ''')), ('empty_group', dict(opts=[('test', [(groups['group1'], [])])], expected='''[DEFAULT] ''')), ('multiple_groups', dict(opts=[('test', [(groups['group1'], [opts['foo']]), (groups['group2'], [opts['bar']])])], expected='''[DEFAULT] [group1] # Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do # eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim # ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut # aliquip ex ea commodo consequat. Duis aute irure dolor in. # # From test # # foo option (string value) #foo = [group2] # # From test # # bar option (string value) #bar = ''')), ('group_in_multiple_namespaces', dict(opts=[('test', [(groups['group1'], [opts['foo']])]), ('other', [(groups['group1'], [opts['bar']])])], expected='''[DEFAULT] [group1] # Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do # eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim # ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut # aliquip ex ea commodo consequat. Duis aute irure dolor in. # # From other # # bar option (string value) #bar = # # From test # # foo option (string value) #foo = ''')), ('hyphenated_name', dict(opts=[('test', [(None, [opts['foo-bar']])])], expected='''[DEFAULT] # # From test # # foobar (string value) #foo_bar = ''')), ('no_help', dict(opts=[('test', [(None, [opts['no_help']])])], log_warning=('"%s" is missing a help string', 'no_help'), expected='''[DEFAULT] # # From test # # (string value) #no_help = ''')), ('long_help', dict(opts=[('test', [(None, [opts['long_help']])])], expected='''[DEFAULT] # # From test # # Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do # eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim # ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut # aliquip ex ea commodo consequat. Duis aute irure dolor in # reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla # pariatur. Excepteur sint occaecat cupidatat non proident, sunt in # culpa qui officia deserunt mollit anim id est laborum. (string # value) #long_help = ''')), ('long_help_wrap_at_40', dict(opts=[('test', [(None, [opts['long_help']])])], wrap_width=40, expected='''[DEFAULT] # # From test # # Lorem ipsum dolor sit amet, # consectetur adipisicing elit, sed do # eiusmod tempor incididunt ut labore et # dolore magna aliqua. Ut enim ad minim # veniam, quis nostrud exercitation # ullamco laboris nisi ut aliquip ex ea # commodo consequat. Duis aute irure # dolor in reprehenderit in voluptate # velit esse cillum dolore eu fugiat # nulla pariatur. Excepteur sint # occaecat cupidatat non proident, sunt # in culpa qui officia deserunt mollit # anim id est laborum. (string value) #long_help = ''')), ('long_help_no_wrapping', dict(opts=[('test', [(None, [opts['long_help']])])], wrap_width=0, expected='''[DEFAULT] # # From test # ''' # noqa '# Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod ' 'tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, ' 'quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo ' 'consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse ' 'cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat ' 'non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. ' '(string value)' ''' #long_help = ''')), ('long_help_with_preformatting', dict(opts=[('test', [(None, [opts['long_help_pre']])])], wrap_width=70, expected='''[DEFAULT] # # From test # # This is a very long help text which is preformatted with line # breaks. It should break when it is too long but also keep the # specified line breaks. This makes it possible to create lists with # items: # # * item 1 # * item 2 # # and should increase the readability. (string value) #long_help_pre = ''')), ('choices_opt', dict(opts=[('test', [(None, [opts['choices_opt']])])], expected='''[DEFAULT] # # From test # # a string with choices (string value) # Possible values: # - # '' - # a - # b - # c - #choices_opt = a ''')), ('deprecated opt without deprecated group', dict(opts=[('test', [(groups['foo'], [opts['deprecated_opt_without_deprecated_group']])])], expected='''[DEFAULT] [foo] # foo help # # From test # # deprecated (string value) # Deprecated group/name - [foo]/foobar #bar = ''')), ('deprecated_for_removal', dict(opts=[('test', [(groups['foo'], [opts['deprecated_for_removal_opt']])])], expected='''[DEFAULT] [foo] # foo help # # From test # # DEPRECATED: deprecated for removal (string value) # This option is deprecated for removal. # Its value may be silently ignored in the future. #bar = ''')), ('deprecated_reason', dict(opts=[('test', [(groups['foo'], [opts['deprecated_reason_opt']])])], expected='''[DEFAULT] [foo] # foo help # # From test # # DEPRECATED: Turn off stove (boolean value) # This option is deprecated for removal. # Its value may be silently ignored in the future. # Reason: This was supposed to work but it really, really did not. # Always buy house insurance. #turn_off_stove = false ''')), ('deprecated_opt_with_deprecated_group', dict(opts=[('test', [(groups['foo'], [opts['deprecated_opt_with_deprecated_group']])])], expected='''[DEFAULT] [foo] # foo help # # From test # # deprecated (string value) # Deprecated group/name - [group1]/foobar #bar = ''')), ('unknown_type', dict(opts=[('test', [(None, [opts['unknown_type']])])], expected='''[DEFAULT] # # From test # # unknown (unknown type) #unknown_opt = 123 ''')), ('str_opt', dict(opts=[('test', [(None, [opts['str_opt']])])], expected='''[DEFAULT] # # From test # # a string (string value) #str_opt = foo bar ''')), ('str_opt_with_space', dict(opts=[('test', [(None, [opts['str_opt_with_space']])])], expected='''[DEFAULT] # # From test # # a string with spaces (string value) #str_opt = " foo bar " ''')), ('str_opt_multiline', dict(opts=[('test', [(None, [opts['str_opt_multiline']])])], expected='''[DEFAULT] # # From test # # a string with newlines (string value) #str_opt = foo # bar # baz ''')), ('bool_opt', dict(opts=[('test', [(None, [opts['bool_opt']])])], expected='''[DEFAULT] # # From test # # a boolean (boolean value) #bool_opt = false ''')), ('int_opt', dict(opts=[('test', [(None, [opts['int_opt']])])], expected='''[DEFAULT] # # From test # # an integer (integer value) # Minimum value: 1 # Maximum value: 20 #int_opt = 10 ''')), ('int_opt_min_0', dict(opts=[('test', [(None, [opts['int_opt_min_0']])])], expected='''[DEFAULT] # # From test # # an integer (integer value) # Minimum value: 0 # Maximum value: 20 #int_opt_min_0 = 10 ''')), ('int_opt_max_0', dict(opts=[('test', [(None, [opts['int_opt_max_0']])])], expected='''[DEFAULT] # # From test # # an integer (integer value) # Maximum value: 0 #int_opt_max_0 = -1 ''')), ('float_opt', dict(opts=[('test', [(None, [opts['float_opt']])])], expected='''[DEFAULT] # # From test # # a float (floating point value) #float_opt = 0.1 ''')), ('list_opt', dict(opts=[('test', [(None, [opts['list_opt']])])], expected='''[DEFAULT] # # From test # # a list (list value) #list_opt = 1,2,3 ''')), ('list_opt_single', dict(opts=[('test', [(None, [opts['list_opt_single']])])], expected='''[DEFAULT] # # From test # # a list (list value) #list_opt_single = 1 ''')), ('list_int_opt', dict(opts=[('test', [(None, [opts['list_int_opt']])])], expected='''[DEFAULT] # # From test # # a list (list value) #list_int_opt = 1,2,3 ''')), ('dict_opt', dict(opts=[('test', [(None, [opts['dict_opt']])])], expected='''[DEFAULT] # # From test # # a dict (dict value) #dict_opt = 1:yes,2:no ''')), ('ip_opt', dict(opts=[('test', [(None, [opts['ip_opt']])])], expected='''[DEFAULT] # # From test # # an ip address (IP address value) #ip_opt = 127.0.0.1 ''')), ('port_opt', dict(opts=[('test', [(None, [opts['port_opt']])])], expected='''[DEFAULT] # # From test # # a port (port value) # Minimum value: 0 # Maximum value: 65535 #port_opt = 80 ''')), ('hostname_opt', dict(opts=[('test', [(None, [opts['hostname_opt']])])], expected='''[DEFAULT] # # From test # # a hostname (hostname value) #hostname_opt = compute01.nova.site1 ''')), ('multi_opt', dict(opts=[('test', [(None, [opts['multi_opt']])])], expected='''[DEFAULT] # # From test # # multiple strings (multi valued) #multi_opt = 1 #multi_opt = 2 #multi_opt = 3 ''')), ('multi_opt_none', dict(opts=[('test', [(None, [opts['multi_opt_none']])])], expected='''[DEFAULT] # # From test # # multiple strings (multi valued) #multi_opt_none = ''')), ('multi_opt_empty', dict(opts=[('test', [(None, [opts['multi_opt_empty']])])], expected='''[DEFAULT] # # From test # # multiple strings (multi valued) #multi_opt_empty = ''')), ('str_opt_sample_default', dict(opts=[('test', [(None, [opts['str_opt_sample_default']])])], expected='''[DEFAULT] # # From test # # a string (string value) #str_opt = fooishbar ''')), ('native_str_type', dict(opts=[('test', [(None, [opts['native_str_type']])])], expected='''[DEFAULT] # # From test # # native help (string value) #native_str_type = ''')), ('native_int_type', dict(opts=[('test', [(None, [opts['native_int_type']])])], expected='''[DEFAULT] # # From test # # native help (integer value) #native_int_type = ''')), ('native_float_type', dict(opts=[('test', [(None, [opts['native_float_type']])])], expected='''[DEFAULT] # # From test # # native help (floating point value) #native_float_type = ''')), ('multi_opt_sample_default', dict(opts=[('test', [(None, [opts['multi_opt_sample_default']])])], expected='''[DEFAULT] # # From test # # multiple strings (multi valued) #multi_opt = 5 #multi_opt = 6 ''')), ('custom_type_name', dict(opts=[('test', [(None, [opts['custom_type_name']])])], expected='''[DEFAULT] # # From test # # this is a port (port number) #custom_opt_type = 5511 ''')), ('custom_type', dict(opts=[('test', [(None, [opts['custom_type']])])], expected='''[DEFAULT] # # From test # # custom help (unknown value) #custom_type = ''')), ('string_type_with_bad_default', dict(opts=[('test', [(None, [opts['string_type_with_bad_default']])])], expected='''[DEFAULT] # # From test # # string with bad default (string value) #string_type_with_bad_default = 4096 ''')), ('str_opt_str_group', dict(opts=[('test', [('foo', [opts['str_opt']]), (groups['foo'], [opts['int_opt']])]), ('foo', [('foo', [opts['bool_opt']])])], expected='''[DEFAULT] [foo] # foo help # # From foo # # a boolean (boolean value) #bool_opt = false # # From test # # a string (string value) #str_opt = foo bar # # From test # # an integer (integer value) # Minimum value: 1 # Maximum value: 20 #int_opt = 10 ''')), ('opt_str_opt_group', dict(opts=[('test', [(groups['foo'], [opts['int_opt']]), ('foo', [opts['str_opt']])]), ('foo', [(groups['foo'], [opts['bool_opt']])])], expected='''[DEFAULT] [foo] # foo help # # From foo # # a boolean (boolean value) #bool_opt = false # # From test # # an integer (integer value) # Minimum value: 1 # Maximum value: 20 #int_opt = 10 # # From test # # a string (string value) #str_opt = foo bar ''')), ('opt_with_DeprecatedOpt', dict(opts=[('test', [(None, [opts['opt_with_DeprecatedOpt']])])], expected='''[DEFAULT] # # From test # # Opt with DeprecatedOpt (boolean value) # Deprecated group/name - [deprecated]/foo_bar #foo_bar = ''')), ] output_file_scenarios = [ ('stdout', dict(stdout=True, output_file=None)), ('output_file', dict(output_file='sample.conf', stdout=False)), ] @classmethod def generate_scenarios(cls): cls.scenarios = testscenarios.multiply_scenarios( cls.content_scenarios, cls.output_file_scenarios) def setUp(self): super(GeneratorTestCase, self).setUp() self.conf = cfg.ConfigOpts() self.config_fixture = config_fixture.Config(self.conf) self.config = self.config_fixture.config self.useFixture(self.config_fixture) self.tempdir = self.useFixture(fixtures.TempDir()) def _capture_stream(self, stream_name): self.useFixture(fixtures.MonkeyPatch("sys.%s" % stream_name, moves.StringIO())) return getattr(sys, stream_name) def _capture_stdout(self): return self._capture_stream('stdout') @mock.patch.object(generator, '_get_raw_opts_loaders') @mock.patch.object(generator, 'LOG') def test_generate(self, mock_log, raw_opts_loader): generator.register_cli_opts(self.conf) namespaces = [i[0] for i in self.opts] self.config(namespace=namespaces) for group in self.groups.values(): self.conf.register_group(group) wrap_width = getattr(self, 'wrap_width', None) if wrap_width is not None: self.config(wrap_width=wrap_width) if self.stdout: stdout = self._capture_stdout() else: output_file = self.tempdir.join(self.output_file) self.config(output_file=output_file) # We have a static data structure matching what should be # returned by _list_opts() but we're mocking out a lower level # function that needs to return a namespace and a callable to # return options from that namespace. We have to pass opts to # the lambda to cache a reference to the name because the list # comprehension changes the thing pointed to by the name each # time through the loop. raw_opts_loader.return_value = [ (ns, lambda opts=opts: opts) for ns, opts in self.opts ] generator.generate(self.conf) if self.stdout: self.assertEqual(self.expected, stdout.getvalue()) else: with open(output_file, 'r') as f: actual = f.read() self.assertEqual(self.expected, actual) log_warning = getattr(self, 'log_warning', None) if log_warning is not None: mock_log.warning.assert_called_once_with(*log_warning) else: self.assertFalse(mock_log.warning.called) class DriverOptionTestCase(base.BaseTestCase): def setUp(self): super(DriverOptionTestCase, self).setUp() self.conf = cfg.ConfigOpts() self.config_fixture = config_fixture.Config(self.conf) self.config = self.config_fixture.config self.useFixture(self.config_fixture) @mock.patch.object(generator, '_get_driver_opts_loaders') @mock.patch.object(generator, '_get_raw_opts_loaders') @mock.patch.object(generator, 'LOG') def test_driver_option(self, mock_log, raw_opts_loader, driver_opts_loader): group = cfg.OptGroup( name='test_group', title='Test Group', driver_option='foo', ) regular_opts = [ cfg.MultiStrOpt('foo', help='foo option'), cfg.StrOpt('bar', help='bar option'), ] driver_opts = { 'd1': [ cfg.StrOpt('d1-foo', help='foo option'), ], 'd2': [ cfg.StrOpt('d2-foo', help='foo option'), ], } # We have a static data structure matching what should be # returned by _list_opts() but we're mocking out a lower level # function that needs to return a namespace and a callable to # return options from that namespace. We have to pass opts to # the lambda to cache a reference to the name because the list # comprehension changes the thing pointed to by the name each # time through the loop. raw_opts_loader.return_value = [ ('testing', lambda: [(group, regular_opts)]), ] driver_opts_loader.return_value = [ ('testing', lambda: driver_opts), ] # Initialize the generator to produce YAML output to a buffer. generator.register_cli_opts(self.conf) self.config(namespace=['test_generator'], format_='yaml') stdout = moves.StringIO() # Generate the output and parse it back to a data structure. generator.generate(self.conf, output_file=stdout) body = stdout.getvalue() actual = yaml.safe_load(body) test_section = actual['options']['test_group'] self.assertEqual('foo', test_section['driver_option']) found_option_names = [ o['name'] for o in test_section['opts'] ] self.assertEqual( ['foo', 'bar', 'd1-foo', 'd2-foo'], found_option_names ) self.assertEqual( {'d1': ['d1-foo'], 'd2': ['d2-foo']}, test_section['driver_opts'], ) GENERATOR_OPTS = {'format_': 'yaml', 'minimal': False, 'namespace': ['test'], 'output_file': None, 'summarize': False, 'wrap_width': 70} class MachineReadableGeneratorTestCase(base.BaseTestCase): all_opts = GeneratorTestCase.opts all_groups = GeneratorTestCase.groups content_scenarios = [ ('single_namespace', dict(opts=[('test', [(None, [all_opts['foo']])])], expected={'deprecated_options': {}, 'generator_options': GENERATOR_OPTS, 'options': { 'DEFAULT': { 'driver_option': '', 'driver_opts': {}, 'dynamic_group_owner': '', 'help': '', 'standard_opts': ['foo'], 'opts': [{'advanced': False, 'choices': [], 'default': None, 'deprecated_for_removal': False, 'deprecated_opts': [], 'deprecated_reason': None, 'deprecated_since': None, 'dest': 'foo', 'help': 'foo option', 'max': None, 'metavar': None, 'min': None, 'mutable': False, 'name': 'foo', 'namespace': 'test', 'positional': False, 'required': False, 'sample_default': None, 'secret': False, 'short': None, 'type': 'string value'}]}}})), ('long_help', dict(opts=[('test', [(None, [all_opts['long_help']])])], expected={'deprecated_options': {}, 'generator_options': GENERATOR_OPTS, 'options': { 'DEFAULT': { 'driver_option': '', 'driver_opts': {}, 'dynamic_group_owner': '', 'help': '', 'standard_opts': ['long_help'], 'opts': [{'advanced': False, 'choices': [], 'default': None, 'deprecated_for_removal': False, 'deprecated_opts': [], 'deprecated_reason': None, 'deprecated_since': None, 'dest': 'long_help', 'help': all_opts['long_help'].help, 'max': None, 'metavar': None, 'min': None, 'mutable': False, 'name': 'long_help', 'namespace': 'test', 'positional': False, 'required': False, 'sample_default': None, 'secret': False, 'short': None, 'type': 'string value'}]}}})), ('long_help_pre', dict(opts=[('test', [(None, [all_opts['long_help_pre']])])], expected={'deprecated_options': {}, 'generator_options': GENERATOR_OPTS, 'options': { 'DEFAULT': { 'driver_option': '', 'driver_opts': {}, 'dynamic_group_owner': '', 'help': '', 'standard_opts': ['long_help_pre'], 'opts': [{'advanced': False, 'choices': [], 'default': None, 'deprecated_for_removal': False, 'deprecated_opts': [], 'deprecated_reason': None, 'deprecated_since': None, 'dest': 'long_help_pre', 'help': all_opts['long_help_pre'].help, 'max': None, 'metavar': None, 'min': None, 'mutable': False, 'name': 'long_help_pre', 'namespace': 'test', 'positional': False, 'required': False, 'sample_default': None, 'secret': False, 'short': None, 'type': 'string value'}]}}})), ('opt_with_DeprecatedOpt', dict(opts=[('test', [(None, [all_opts['opt_with_DeprecatedOpt']])])], expected={ 'deprecated_options': { 'deprecated': [{'name': 'foo_bar', 'replacement_group': 'DEFAULT', 'replacement_name': 'foo-bar'}]}, 'generator_options': GENERATOR_OPTS, 'options': { 'DEFAULT': { 'driver_option': '', 'driver_opts': {}, 'dynamic_group_owner': '', 'help': '', 'standard_opts': ['foo-bar'], 'opts': [{ 'advanced': False, 'choices': [], 'default': None, 'deprecated_for_removal': False, 'deprecated_opts': [{'group': 'deprecated', 'name': 'foo_bar'}], 'deprecated_reason': None, 'deprecated_since': None, 'dest': 'foo_bar', 'help': all_opts['opt_with_DeprecatedOpt'].help, 'max': None, 'metavar': None, 'min': None, 'mutable': False, 'name': 'foo-bar', 'namespace': 'test', 'positional': False, 'required': False, 'sample_default': None, 'secret': False, 'short': None, 'type': 'boolean value'}]}}})), ('choices_opt', dict(opts=[('test', [(None, [all_opts['choices_opt']])])], expected={'deprecated_options': {}, 'generator_options': GENERATOR_OPTS, 'options': { 'DEFAULT': { 'driver_option': '', 'driver_opts': {}, 'dynamic_group_owner': '', 'help': '', 'standard_opts': ['choices_opt'], 'opts': [{'advanced': False, 'choices': [ (None, None), ('', None), ('a', None), ('b', None), ('c', None) ], 'default': 'a', 'deprecated_for_removal': False, 'deprecated_opts': [], 'deprecated_reason': None, 'deprecated_since': None, 'dest': 'choices_opt', 'help': all_opts['choices_opt'].help, 'max': None, 'metavar': None, 'min': None, 'mutable': False, 'name': 'choices_opt', 'namespace': 'test', 'positional': False, 'required': False, 'sample_default': None, 'secret': False, 'short': None, 'type': 'string value'}]}}})), ('int_opt', dict(opts=[('test', [(None, [all_opts['int_opt']])])], expected={'deprecated_options': {}, 'generator_options': GENERATOR_OPTS, 'options': { 'DEFAULT': { 'driver_option': '', 'driver_opts': {}, 'dynamic_group_owner': '', 'help': '', 'standard_opts': ['int_opt'], 'opts': [{'advanced': False, 'choices': [], 'default': 10, 'deprecated_for_removal': False, 'deprecated_opts': [], 'deprecated_reason': None, 'deprecated_since': None, 'dest': 'int_opt', 'help': all_opts['int_opt'].help, 'max': 20, 'metavar': None, 'min': 1, 'mutable': False, 'name': 'int_opt', 'namespace': 'test', 'positional': False, 'required': False, 'sample_default': None, 'secret': False, 'short': None, 'type': 'integer value'}]}}})), ('group_help', dict(opts=[('test', [(all_groups['group1'], [all_opts['foo']])])], expected={'deprecated_options': {}, 'generator_options': GENERATOR_OPTS, 'options': { 'DEFAULT': { # 'driver_option': '', # 'driver_opts': [], # 'dynamic_group_owner': '', 'help': '', 'standard_opts': [], 'opts': [] }, 'group1': { 'driver_option': '', 'driver_opts': {}, 'dynamic_group_owner': '', 'help': all_groups['group1'].help, 'standard_opts': ['foo'], 'opts': [{'advanced': False, 'choices': [], 'default': None, 'deprecated_for_removal': False, 'deprecated_opts': [], 'deprecated_reason': None, 'deprecated_since': None, 'dest': 'foo', 'help': all_opts['foo'].help, 'max': None, 'metavar': None, 'min': None, 'mutable': False, 'name': 'foo', 'namespace': 'test', 'positional': False, 'required': False, 'sample_default': None, 'secret': False, 'short': None, 'type': 'string value'}]}}})), ] def setUp(self): super(MachineReadableGeneratorTestCase, self).setUp() self.conf = cfg.ConfigOpts() self.config_fixture = config_fixture.Config(self.conf) self.config = self.config_fixture.config self.useFixture(self.config_fixture) @classmethod def generate_scenarios(cls): cls.scenarios = testscenarios.multiply_scenarios( cls.content_scenarios) @mock.patch.object(generator, '_get_raw_opts_loaders') def test_generate(self, raw_opts_loader): generator.register_cli_opts(self.conf) namespaces = [i[0] for i in self.opts] self.config(namespace=namespaces, format_='yaml') # We have a static data structure matching what should be # returned by _list_opts() but we're mocking out a lower level # function that needs to return a namespace and a callable to # return options from that namespace. We have to pass opts to # the lambda to cache a reference to the name because the list # comprehension changes the thing pointed to by the name each # time through the loop. raw_opts_loader.return_value = [ (ns, lambda opts=opts: opts) for ns, opts in self.opts ] test_groups = generator._get_groups( generator._list_opts(self.conf.namespace)) self.assertEqual(self.expected, generator._generate_machine_readable_data(test_groups, self.conf)) class IgnoreDoublesTestCase(base.BaseTestCase): opts = [cfg.StrOpt('foo', help='foo option'), cfg.StrOpt('bar', help='bar option'), cfg.StrOpt('foo_bar', help='foobar'), cfg.StrOpt('str_opt', help='a string'), cfg.BoolOpt('bool_opt', help='a boolean'), cfg.IntOpt('int_opt', help='an integer')] def test_cleanup_opts_default(self): o = [("namespace1", [ ("group1", self.opts)])] self.assertEqual(o, generator._cleanup_opts(o)) def test_cleanup_opts_dup_opt(self): o = [("namespace1", [ ("group1", self.opts + [self.opts[0]])])] e = [("namespace1", [ ("group1", self.opts)])] self.assertEqual(e, generator._cleanup_opts(o)) def test_cleanup_opts_dup_groups_opt(self): o = [("namespace1", [ ("group1", self.opts + [self.opts[1]]), ("group2", self.opts), ("group3", self.opts + [self.opts[2]])])] e = [("namespace1", [ ("group1", self.opts), ("group2", self.opts), ("group3", self.opts)])] self.assertEqual(e, generator._cleanup_opts(o)) def test_cleanup_opts_dup_mixed_case_groups_opt(self): o = [("namespace1", [ ("default", self.opts), ("Default", self.opts + [self.opts[1]]), ("DEFAULT", self.opts + [self.opts[2]]), ("group1", self.opts + [self.opts[1]]), ("Group1", self.opts), ("GROUP1", self.opts + [self.opts[2]])])] e = [("namespace1", [ ("DEFAULT", self.opts), ("group1", self.opts)])] self.assertEqual(e, generator._cleanup_opts(o)) def test_cleanup_opts_dup_namespace_groups_opts(self): o = [("namespace1", [ ("group1", self.opts + [self.opts[1]]), ("group2", self.opts)]), ("namespace2", [ ("group1", self.opts + [self.opts[2]]), ("group2", self.opts)])] e = [("namespace1", [ ("group1", self.opts), ("group2", self.opts)]), ("namespace2", [ ("group1", self.opts), ("group2", self.opts)])] self.assertEqual(e, generator._cleanup_opts(o)) @mock.patch.object(generator, '_get_raw_opts_loaders') def test_list_ignores_doubles(self, raw_opts_loaders): config_opts = [ (None, [cfg.StrOpt('foo'), cfg.StrOpt('bar')]), ] # These are the very same config options, but read twice. # This is possible if one misconfigures the entry point for the # sample config generator. raw_opts_loaders.return_value = [ ('namespace', lambda: config_opts), ('namespace', lambda: config_opts), ] slurped_opts = 0 for _, listing in generator._list_opts(['namespace']): for _, opts in listing: slurped_opts += len(opts) self.assertEqual(2, slurped_opts) class GeneratorAdditionalTestCase(base.BaseTestCase): opts = [cfg.StrOpt('foo', help='foo option', default='fred'), cfg.StrOpt('bar', help='bar option'), cfg.StrOpt('foo_bar', help='foobar'), cfg.StrOpt('str_opt', help='a string'), cfg.BoolOpt('bool_opt', help='a boolean'), cfg.IntOpt('int_opt', help='an integer')] def test_get_groups_empty_ns(self): groups = generator._get_groups([]) self.assertEqual({'DEFAULT': {'object': None, 'namespaces': []}}, groups) def test_get_groups_single_ns(self): config = [("namespace1", [ ("beta", self.opts), ("alpha", self.opts)])] groups = generator._get_groups(config) self.assertEqual(['DEFAULT', 'alpha', 'beta'], sorted(groups)) def test_get_groups_multiple_ns(self): config = [("namespace1", [ ("beta", self.opts), ("alpha", self.opts)]), ("namespace2", [ ("gamma", self.opts), ("alpha", self.opts)])] groups = generator._get_groups(config) self.assertEqual(['DEFAULT', 'alpha', 'beta', 'gamma'], sorted(groups)) def test_output_opts_empty_default(self): config = [("namespace1", [ ("alpha", [])])] groups = generator._get_groups(config) fd, tmp_file = tempfile.mkstemp() with open(tmp_file, 'w+') as f: formatter = generator._OptFormatter(output_file=f) generator._output_opts(formatter, 'DEFAULT', groups.pop('DEFAULT')) expected = '''[DEFAULT] ''' with open(tmp_file, 'r') as f: actual = f.read() self.assertEqual(expected, actual) def test_output_opts_group(self): config = [("namespace1", [ ("alpha", [self.opts[0]])])] groups = generator._get_groups(config) fd, tmp_file = tempfile.mkstemp() with open(tmp_file, 'w+') as f: formatter = generator._OptFormatter(output_file=f) generator._output_opts(formatter, 'alpha', groups.pop('alpha')) expected = '''[alpha] # # From namespace1 # # foo option (string value) #foo = fred ''' with open(tmp_file, 'r') as f: actual = f.read() self.assertEqual(expected, actual) def _test_output_default_list_opt_with_string_value(self, default): opt = cfg.ListOpt('list_opt', help='a list', default=default) config = [("namespace1", [ ("alpha", [opt])])] groups = generator._get_groups(config) fd, tmp_file = tempfile.mkstemp() f = open(tmp_file, 'w+') formatter = generator._OptFormatter(output_file=f) expected = '''[alpha] # # From namespace1 # # a list (list value) #list_opt = %(default)s ''' % {'default': default} generator._output_opts(formatter, 'alpha', groups.pop('alpha')) f.close() content = open(tmp_file).read() self.assertEqual(expected, content) def test_output_default_list_opt_with_string_value_multiple_entries(self): self._test_output_default_list_opt_with_string_value('foo,bar') def test_output_default_list_opt_with_string_value_single_entry(self): self._test_output_default_list_opt_with_string_value('foo') class GeneratorMutableOptionTestCase(base.BaseTestCase): def test_include_message(self): out = moves.StringIO() opt = cfg.StrOpt('foo', help='foo option', mutable=True) gen = generator._OptFormatter(output_file=out) gen.format(opt, 'group1') result = out.getvalue() self.assertIn( 'This option can be changed without restarting.', result, ) def test_do_not_include_message(self): out = moves.StringIO() opt = cfg.StrOpt('foo', help='foo option', mutable=False) gen = generator._OptFormatter(output_file=out) gen.format(opt, 'group1') result = out.getvalue() self.assertNotIn( 'This option can be changed without restarting.', result, ) class GeneratorRaiseErrorTestCase(base.BaseTestCase): def test_generator_raises_error(self): """Verifies that errors from extension manager are not suppressed.""" class FakeException(Exception): pass class FakeEP(object): def __init__(self): self.name = 'callback_is_expected' self.require = self.resolve self.load = self.resolve def resolve(self, *args, **kwargs): raise FakeException() fake_ep = FakeEP() self.conf = cfg.ConfigOpts() self.conf.register_opts(generator._generator_opts) self.conf.set_default('namespace', [fake_ep.name]) fake_eps = mock.Mock(return_value=[fake_ep]) with mock.patch('pkg_resources.iter_entry_points', fake_eps): self.assertRaises(FakeException, generator.generate, self.conf) def test_generator_call_with_no_arguments_raises_system_exit(self): testargs = ['oslo-config-generator'] with mock.patch('sys.argv', testargs): self.assertRaises(SystemExit, generator.main, []) class ChangeDefaultsTestCase(base.BaseTestCase): @mock.patch.object(generator, '_get_opt_default_updaters') @mock.patch.object(generator, '_get_raw_opts_loaders') def test_no_modifiers_registered(self, raw_opts_loaders, get_updaters): orig_opt = cfg.StrOpt('foo', default='bar') raw_opts_loaders.return_value = [ ('namespace', lambda: [(None, [orig_opt])]), ] get_updaters.return_value = [] opts = generator._list_opts(['namespace']) # NOTE(dhellmann): Who designed this data structure? the_opt = opts[0][1][0][1][0] self.assertEqual('bar', the_opt.default) self.assertIs(orig_opt, the_opt) @mock.patch.object(generator, '_get_opt_default_updaters') @mock.patch.object(generator, '_get_raw_opts_loaders') def test_change_default(self, raw_opts_loaders, get_updaters): orig_opt = cfg.StrOpt('foo', default='bar') raw_opts_loaders.return_value = [ ('namespace', lambda: [(None, [orig_opt])]), ] def updater(): cfg.set_defaults([orig_opt], foo='blah') get_updaters.return_value = [updater] opts = generator._list_opts(['namespace']) # NOTE(dhellmann): Who designed this data structure? the_opt = opts[0][1][0][1][0] self.assertEqual('blah', the_opt.default) self.assertIs(orig_opt, the_opt) class RequiredOptionTestCase(base.BaseTestCase): opts = [cfg.StrOpt('foo', help='foo option', default='fred'), cfg.StrOpt('bar', help='bar option', required=True), cfg.StrOpt('foo_bar', help='foobar'), cfg.StrOpt('bars', help='bars foo', required=True)] def test_required_option_order_single_ns(self): config = [("namespace1", [ ("alpha", self.opts)])] groups = generator._get_groups(config) fd, tmp_file = tempfile.mkstemp() with open(tmp_file, 'w+') as f: formatter = generator._OptFormatter(output_file=f) generator._output_opts(formatter, 'alpha', groups.pop('alpha'), minimal=True) expected = '''[alpha] # # From namespace1 # # bar option (string value) bar = # bars foo (string value) bars = ''' with open(tmp_file, 'r') as f: actual = f.read() self.assertEqual(expected, actual) class SummarizedOptionsTestCase(base.BaseTestCase): """Validate 'summarize' config option. The 'summarize' switch ensures only summaries of each configuration option are output. """ opts = [ cfg.StrOpt( 'foo', default='fred', help="""This is the summary line for a config option. I can provide a lot more detail here, but I may not want to bloat my config file. In this scenario, I can use the 'summarize' config option to ensure only a summary of the option is output to the config file. However, the Sphinx-generated documentation, hosted online, remains unchanged. Hopefully this works. """), cfg.StrOpt( 'bar', required=True, help="""This is a less carefully formatted configuration option, where the author has not broken their description into a brief summary line and larger description. Watch this person's commit messages!""")] def test_summarized_option_order_single_ns(self): config = [('namespace1', [('alpha', self.opts)])] groups = generator._get_groups(config) fd, tmp_file = tempfile.mkstemp() with open(tmp_file, 'w+') as f: formatter = generator._OptFormatter(output_file=f) generator._output_opts(formatter, 'alpha', groups.pop('alpha'), summarize=True) expected = '''[alpha] # # From namespace1 # # This is the summary line for a config option. For more information, # refer to the documentation. (string value) #foo = fred # This is a less carefully formatted configuration # option, where the author has not broken their description into a # brief # summary line and larger description. Watch this person's commit # messages! (string value) #bar = ''' with open(tmp_file, 'r') as f: actual = f.read() self.assertEqual(expected, actual) class AdvancedOptionsTestCase(base.BaseTestCase): opts = [cfg.StrOpt('foo', help='foo option', default='fred'), cfg.StrOpt('bar', help='bar option', advanced=True), cfg.StrOpt('foo_bar', help='foobar'), cfg.BoolOpt('bars', help='bars foo', default=True, advanced=True)] def test_advanced_option_order_single_ns(self): config = [("namespace1", [ ("alpha", self.opts)])] groups = generator._get_groups(config) fd, tmp_file = tempfile.mkstemp() with open(tmp_file, 'w+') as f: formatter = generator._OptFormatter(output_file=f) generator._output_opts(formatter, 'alpha', groups.pop('alpha')) expected = '''[alpha] # # From namespace1 # # foo option (string value) #foo = fred # foobar (string value) #foo_bar = # bar option (string value) # Advanced Option: intended for advanced users and not used # by the majority of users, and might have a significant # effect on stability and/or performance. #bar = # bars foo (boolean value) # Advanced Option: intended for advanced users and not used # by the majority of users, and might have a significant # effect on stability and/or performance. #bars = true ''' with open(tmp_file, 'r') as f: actual = f.read() self.assertEqual(expected, actual) GeneratorTestCase.generate_scenarios() MachineReadableGeneratorTestCase.generate_scenarios() oslo.config-5.2.0/oslo_config/cfg.py0000666000175100017510000036311313224676105017413 0ustar zuulzuul00000000000000# Copyright 2012 Red Hat, Inc. # # 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. r""" Configuration options may be set on the command line or in config files. The schema for each option is defined using the :class:`Opt` class or its sub-classes, for example: :: from oslo_config import cfg from oslo_config import types PortType = types.Integer(1, 65535) common_opts = [ cfg.StrOpt('bind_host', default='0.0.0.0', help='IP address to listen on.'), cfg.Opt('bind_port', type=PortType, default=9292, help='Port number to listen on.') ] Option Types ------------ Options can have arbitrary types via the `type` parameter to the :class:`Opt` constructor. The `type` parameter is a callable object that takes a string and either returns a value of that particular type or raises :class:`ValueError` if the value can not be converted. For convenience, there are predefined option subclasses in :mod:`oslo_config.cfg` that set the option `type` as in the following table: ====================================== ====== Type Option ====================================== ====== :class:`oslo_config.types.String` :class:`oslo_config.cfg.StrOpt` :class:`oslo_config.types.String` :class:`oslo_config.cfg.SubCommandOpt` :class:`oslo_config.types.Boolean` :class:`oslo_config.cfg.BoolOpt` :class:`oslo_config.types.Integer` :class:`oslo_config.cfg.IntOpt` :class:`oslo_config.types.Float` :class:`oslo_config.cfg.FloatOpt` :class:`oslo_config.types.Port` :class:`oslo_config.cfg.PortOpt` :class:`oslo_config.types.List` :class:`oslo_config.cfg.ListOpt` :class:`oslo_config.types.Dict` :class:`oslo_config.cfg.DictOpt` :class:`oslo_config.types.IPAddress` :class:`oslo_config.cfg.IPOpt` :class:`oslo_config.types.Hostname` :class:`oslo_config.cfg.HostnameOpt` :class:`oslo_config.types.HostAddress` :class:`oslo_config.cfg.HostAddressOpt` :class:`oslo_config.types.URI` :class:`oslo_config.cfg.URIOpt` ====================================== ====== For :class:`oslo_config.cfg.MultiOpt` the `item_type` parameter defines the type of the values. For convenience, :class:`oslo_config.cfg.MultiStrOpt` is :class:`~oslo_config.cfg.MultiOpt` with the `item_type` parameter set to :class:`oslo_config.types.MultiString`. The following example defines options using the convenience classes:: enabled_apis_opt = cfg.ListOpt('enabled_apis', default=['ec2', 'osapi_compute'], help='List of APIs to enable by default.') DEFAULT_EXTENSIONS = [ 'nova.api.openstack.compute.contrib.standard_extensions' ] osapi_compute_extension_opt = cfg.MultiStrOpt('osapi_compute_extension', default=DEFAULT_EXTENSIONS) Registering Options ------------------- Option schemas are registered with the config manager at runtime, but before the option is referenced:: class ExtensionManager(object): enabled_apis_opt = cfg.ListOpt(...) def __init__(self, conf): self.conf = conf self.conf.register_opt(enabled_apis_opt) ... def _load_extensions(self): for ext_factory in self.conf.osapi_compute_extension: .... A common usage pattern is for each option schema to be defined in the module or class which uses the option:: opts = ... def add_common_opts(conf): conf.register_opts(opts) def get_bind_host(conf): return conf.bind_host def get_bind_port(conf): return conf.bind_port An option may optionally be made available via the command line. Such options must be registered with the config manager before the command line is parsed (for the purposes of --help and CLI arg validation):: cli_opts = [ cfg.BoolOpt('verbose', short='v', default=False, help='Print more verbose output.'), cfg.BoolOpt('debug', short='d', default=False, help='Print debugging output.'), ] def add_common_opts(conf): conf.register_cli_opts(cli_opts) Loading Config Files -------------------- The config manager has two CLI options defined by default, --config-file and --config-dir:: class ConfigOpts(object): def __call__(self, ...): opts = [ MultiStrOpt('config-file', ...), StrOpt('config-dir', ...), ] self.register_cli_opts(opts) Option values are parsed from any supplied config files using oslo_config.iniparser. If none are specified, a default set is used for example glance-api.conf and glance-common.conf:: glance-api.conf: [DEFAULT] bind_port = 9292 glance-common.conf: [DEFAULT] bind_host = 0.0.0.0 Lines in a configuration file should not start with whitespace. A configuration file also supports comments, which must start with '#' or ';'. Option values in config files and those on the command line are parsed in order. The same option (includes deprecated option name and current option name) can appear many times, in config files or on the command line. Later values always override earlier ones. The order of configuration files inside the same configuration directory is defined by the alphabetic sorting order of their file names. The parsing of CLI args and config files is initiated by invoking the config manager for example:: conf = cfg.ConfigOpts() conf.register_opt(cfg.BoolOpt('verbose', ...)) conf(sys.argv[1:]) if conf.verbose: ... Option Groups ------------- Options can be registered as belonging to a group:: rabbit_group = cfg.OptGroup(name='rabbit', title='RabbitMQ options') rabbit_host_opt = cfg.StrOpt('host', default='localhost', help='IP/hostname to listen on.'), rabbit_port_opt = cfg.PortOpt('port', default=5672, help='Port number to listen on.') def register_rabbit_opts(conf): conf.register_group(rabbit_group) # options can be registered under a group in either of these ways: conf.register_opt(rabbit_host_opt, group=rabbit_group) conf.register_opt(rabbit_port_opt, group='rabbit') If no group attributes are required other than the group name, the group need not be explicitly registered for example:: def register_rabbit_opts(conf): # The group will automatically be created, equivalent calling:: # conf.register_group(OptGroup(name='rabbit')) conf.register_opt(rabbit_port_opt, group='rabbit') If no group is specified, options belong to the 'DEFAULT' section of config files:: glance-api.conf: [DEFAULT] bind_port = 9292 ... [rabbit] host = localhost port = 5672 use_ssl = False userid = guest password = guest virtual_host = / Command-line options in a group are automatically prefixed with the group name:: --rabbit-host localhost --rabbit-port 9999 Dynamic Groups -------------- Groups can be registered dynamically by application code. This introduces a challenge for the sample generator, discovery mechanisms, and validation tools, since they do not know in advance the names of all of the groups. The ``dynamic_group_owner`` parameter to the constructor specifies the full name of an option registered in another group that controls repeated instances of a dynamic group. This option is usually a MultiStrOpt. For example, Cinder supports multiple storage backend devices and services. To configure Cinder to communicate with multiple backends, the ``enabled_backends`` option is set to the list of names of backends. Each backend group includes the options for communicating with that device or service. Driver Groups ------------- Groups can have dynamic sets of options, usually based on a driver that has unique requirements. This works at runtime because the code registers options before it uses them, but it introduces a challenge for the sample generator, discovery mechanisms, and validation tools because they do not know in advance the correct options for a group. To address this issue, the driver option for a group can be named using the ``driver_option`` parameter. Each driver option should define its own discovery entry point namespace to return the set of options for that driver, named using the prefix ``"oslo.config.opts."`` followed by the driver option name. In the Cinder case described above, a ``volume_backend_name`` option is part of the static definition of the group, so ``driver_option`` should be set to ``"volume_backend_name"``. And plugins should be registered under ``"oslo.config.opts.volume_backend_name"`` using the same names as the main plugin registered with ``"oslo.config.opts"``. The drivers residing within the Cinder code base have an entry point named ``"cinder"`` registered. Accessing Option Values In Your Code ------------------------------------ Option values in the default group are referenced as attributes/properties on the config manager; groups are also attributes on the config manager, with attributes for each of the options associated with the group:: server.start(app, conf.bind_port, conf.bind_host, conf) self.connection = kombu.connection.BrokerConnection( hostname=conf.rabbit.host, port=conf.rabbit.port, ...) Option Value Interpolation -------------------------- Option values may reference other values using PEP 292 string substitution:: opts = [ cfg.StrOpt('state_path', default=os.path.join(os.path.dirname(__file__), '../'), help='Top-level directory for maintaining nova state.'), cfg.StrOpt('sqlite_db', default='nova.sqlite', help='File name for SQLite.'), cfg.StrOpt('sql_connection', default='sqlite:///$state_path/$sqlite_db', help='Connection string for SQL database.'), ] .. note:: Interpolation can be avoided by using `$$`. .. note:: You can use `.` to delimit option from other groups, e.g. ${mygroup.myoption}. Special Handling Instructions ----------------------------- Options may be declared as required so that an error is raised if the user does not supply a value for the option:: opts = [ cfg.StrOpt('service_name', required=True), cfg.StrOpt('image_id', required=True), ... ] Options may be declared as secret so that their values are not leaked into log files:: opts = [ cfg.StrOpt('s3_store_access_key', secret=True), cfg.StrOpt('s3_store_secret_key', secret=True), ... ] Dictionary Options ------------------ If you need end users to specify a dictionary of key/value pairs, then you can use the DictOpt:: opts = [ cfg.DictOpt('foo', default={}) ] The end users can then specify the option foo in their configuration file as shown below: .. code-block:: ini [DEFAULT] foo = k1:v1,k2:v2 Global ConfigOpts ----------------- This module also contains a global instance of the ConfigOpts class in order to support a common usage pattern in OpenStack:: from oslo_config import cfg opts = [ cfg.StrOpt('bind_host', default='0.0.0.0'), cfg.PortOpt('bind_port', default=9292), ] CONF = cfg.CONF CONF.register_opts(opts) def start(server, app): server.start(app, CONF.bind_port, CONF.bind_host) Positional Command Line Arguments --------------------------------- Positional command line arguments are supported via a 'positional' Opt constructor argument:: >>> conf = cfg.ConfigOpts() >>> conf.register_cli_opt(cfg.MultiStrOpt('bar', positional=True)) True >>> conf(['a', 'b']) >>> conf.bar ['a', 'b'] Sub-Parsers ----------- It is also possible to use argparse "sub-parsers" to parse additional command line arguments using the SubCommandOpt class: >>> def add_parsers(subparsers): ... list_action = subparsers.add_parser('list') ... list_action.add_argument('id') ... >>> conf = cfg.ConfigOpts() >>> conf.register_cli_opt(cfg.SubCommandOpt('action', handler=add_parsers)) True >>> conf(args=['list', '10']) >>> conf.action.name, conf.action.id ('list', '10') Advanced Option --------------- Use if you need to label an option as advanced in sample files, indicating the option is not normally used by the majority of users and might have a significant effect on stability and/or performance:: from oslo_config import cfg opts = [ cfg.StrOpt('option1', default='default_value', advanced=True, help='This is help ' 'text.'), cfg.PortOpt('option2', default='default_value', help='This is help text.'), ] CONF = cfg.CONF CONF.register_opts(opts) This will result in the option being pushed to the bottom of the namespace and labeled as advanced in the sample files, with a notation about possible effects:: [DEFAULT] ... # This is help text. (string value) # option2 = default_value ... ... # This is help text. (string value) # Advanced Option: intended for advanced users and not used # by the majority of users, and might have a significant # effect on stability and/or performance. # option1 = default_value Option Deprecation ------------------ If you want to rename some options, move them to another group or remove completely, you may change their declarations using `deprecated_name`, `deprecated_group` and `deprecated_for_removal` parameters to the :class:`Opt` constructor:: from oslo_config import cfg conf = cfg.ConfigOpts() opt_1 = cfg.StrOpt('opt_1', default='foo', deprecated_name='opt1') opt_2 = cfg.StrOpt('opt_2', default='spam', deprecated_group='DEFAULT') opt_3 = cfg.BoolOpt('opt_3', default=False, deprecated_for_removal=True) conf.register_opt(opt_1, group='group_1') conf.register_opt(opt_2, group='group_2') conf.register_opt(opt_3) conf(['--config-file', 'config.conf']) assert conf.group_1.opt_1 == 'bar' assert conf.group_2.opt_2 == 'eggs' assert conf.opt_3 Assuming that the file config.conf has the following content:: [group_1] opt1 = bar [DEFAULT] opt_2 = eggs opt_3 = True the script will succeed, but will log three respective warnings about the given deprecated options. There are also `deprecated_reason` and `deprecated_since` parameters for specifying some additional information about a deprecation. All the mentioned parameters can be mixed together in any combinations. """ import argparse import collections import copy import errno import functools import glob import itertools import logging import os import string import sys from debtcollector import removals import six from oslo_config import iniparser from oslo_config import types LOG = logging.getLogger(__name__) class Error(Exception): """Base class for cfg exceptions.""" def __init__(self, msg=None): self.msg = msg def __str__(self): return self.msg class NotInitializedError(Error): """Raised if parser is not initialized yet.""" def __str__(self): return "call expression on parser has not been invoked" class ArgsAlreadyParsedError(Error): """Raised if a CLI opt is registered after parsing.""" def __str__(self): ret = "arguments already parsed" if self.msg: ret += ": " + self.msg return ret class NoSuchOptError(Error, AttributeError): """Raised if an opt which doesn't exist is referenced.""" def __init__(self, opt_name, group=None): self.opt_name = opt_name self.group = group def __str__(self): group_name = 'DEFAULT' if self.group is None else self.group.name return "no such option %s in group [%s]" % (self.opt_name, group_name) class NoSuchGroupError(Error): """Raised if a group which doesn't exist is referenced.""" def __init__(self, group_name): self.group_name = group_name def __str__(self): return "no such group [%s]" % self.group_name class DuplicateOptError(Error): """Raised if multiple opts with the same name are registered.""" def __init__(self, opt_name): self.opt_name = opt_name def __str__(self): return "duplicate option: %s" % self.opt_name class RequiredOptError(Error): """Raised if an option is required but no value is supplied by the user.""" def __init__(self, opt_name, group=None): self.opt_name = opt_name self.group = group def __str__(self): group_name = 'DEFAULT' if self.group is None else self.group.name return "value required for option %s in group [%s]" % (self.opt_name, group_name) class TemplateSubstitutionError(Error): """Raised if an error occurs substituting a variable in an opt value.""" def __str__(self): return "template substitution error: %s" % self.msg class ConfigFilesNotFoundError(Error): """Raised if one or more config files are not found.""" def __init__(self, config_files): self.config_files = config_files def __str__(self): return ('Failed to find some config files: %s' % ",".join(self.config_files)) class ConfigFilesPermissionDeniedError(Error): """Raised if one or more config files are not readable.""" def __init__(self, config_files): self.config_files = config_files def __str__(self): return ('Failed to open some config files: %s' % ",".join(self.config_files)) class ConfigDirNotFoundError(Error): """Raised if the requested config-dir is not found.""" def __init__(self, config_dir): self.config_dir = config_dir def __str__(self): return ('Failed to read config file directory: %s' % self.config_dir) class ConfigFileParseError(Error): """Raised if there is an error parsing a config file.""" def __init__(self, config_file, msg): self.config_file = config_file self.msg = msg def __str__(self): return 'Failed to parse %s: %s' % (self.config_file, self.msg) class ConfigFileValueError(Error, ValueError): """Raised if a config file value does not match its opt type.""" pass class DefaultValueError(Error, ValueError): """Raised if a default config type does not fit the opt type.""" def _fixpath(p): """Apply tilde expansion and absolutization to a path.""" return os.path.abspath(os.path.expanduser(p)) def _get_config_dirs(project=None): """Return a list of directories where config files may be located. :param project: an optional project name If a project is specified, following directories are returned:: ~/.${project}/ ~/ /etc/${project}/ /etc/ If a project is specified and installed from a snap package, following directories are also returned: ${SNAP_COMMON}/etc/${project} ${SNAP}/etc/${project} Otherwise, if project is not specified, these directories are returned: ~/ /etc/ """ snap = os.environ.get('SNAP') snap_c = os.environ.get('SNAP_COMMON') cfg_dirs = [ _fixpath(os.path.join('~', '.' + project)) if project else None, _fixpath('~'), os.path.join('/etc', project) if project else None, '/etc', os.path.join(snap_c, "etc", project) if snap_c and project else None, os.path.join(snap, "etc", project) if snap and project else None, ] return [x for x in cfg_dirs if x] def _search_dirs(dirs, basename, extension=""): """Search a list of directories for a given filename or directory name. Iterator over the supplied directories, returning the first file found with the supplied name and extension. :param dirs: a list of directories :param basename: the filename or directory name, for example 'glance-api' :param extension: the file extension, for example '.conf' :returns: the path to a matching file or directory, or None """ for d in dirs: path = os.path.join(d, '%s%s' % (basename, extension)) if os.path.exists(path): return path def _find_config_files(project, prog, extension): if prog is None: prog = os.path.basename(sys.argv[0]) if prog.endswith(".py"): prog = prog[:-3] cfg_dirs = _get_config_dirs(project) config_files = (_search_dirs(cfg_dirs, p, extension) for p in [project, prog] if p) return [x for x in config_files if x] def find_config_files(project=None, prog=None, extension='.conf'): """Return a list of default configuration files. :param project: an optional project name :param prog: the program name, defaulting to the basename of sys.argv[0], without extension .py :param extension: the type of the config file We default to two config files: [${project}.conf, ${prog}.conf] And we look for those config files in the following directories:: ~/.${project}/ ~/ /etc/${project}/ /etc/ ${SNAP_COMMON}/etc/${project} ${SNAP}/etc/${project} We return an absolute path for (at most) one of each the default config files, for the topmost directory it exists in. For example, if project=foo, prog=bar and /etc/foo/foo.conf, /etc/bar.conf and ~/.foo/bar.conf all exist, then we return ['/etc/foo/foo.conf', '~/.foo/bar.conf'] If no project name is supplied, we only look for ${prog}.conf. """ return _find_config_files(project, prog, extension) def find_config_dirs(project=None, prog=None, extension='.conf.d'): """Return a list of default configuration dirs. :param project: an optional project name :param prog: the program name, defaulting to the basename of sys.argv[0], without extension .py :param extension: the type of the config directory. Defaults to '.conf.d' We default to two config dirs: [${project}.conf.d/, ${prog}.conf.d/]. If no project name is supplied, we only look for ${prog.conf.d/}. And we look for those config dirs in the following directories:: ~/.${project}/ ~/ /etc/${project}/ /etc/ ${SNAP_COMMON}/etc/${project} ${SNAP}/etc/${project} We return an absolute path for each of the two config dirs, in the first place we find it (iff we find it). For example, if project=foo, prog=bar and /etc/foo/foo.conf.d/, /etc/bar.conf.d/ and ~/.foo/bar.conf.d/ all exist, then we return ['/etc/foo/foo.conf.d/', '~/.foo/bar.conf.d/'] """ return _find_config_files(project, prog, extension) def _is_opt_registered(opts, opt): """Check whether an opt with the same name is already registered. The same opt may be registered multiple times, with only the first registration having any effect. However, it is an error to attempt to register a different opt with the same name. :param opts: the set of opts already registered :param opt: the opt to be registered :returns: True if the opt was previously registered, False otherwise :raises: DuplicateOptError if a naming conflict is detected """ if opt.dest in opts: if opts[opt.dest]['opt'] != opt: raise DuplicateOptError(opt.name) return True else: return False def set_defaults(opts, **kwargs): for opt in opts: if opt.dest in kwargs: opt.default = kwargs[opt.dest] def _normalize_group_name(group_name): if group_name == 'DEFAULT': return group_name return group_name.lower() @functools.total_ordering class Opt(object): """Base class for all configuration options. The only required parameter is the option's name. However, it is common to also supply a default and help string for all options. :param name: the option's name :param type: the option's type. Must be a callable object that takes string and returns converted and validated value :param dest: the name of the corresponding :class:`.ConfigOpts` property :param short: a single character CLI option name :param default: the default value of the option :param positional: True if the option is a positional CLI argument :param metavar: the option argument to show in --help :param help: an explanation of how the option is used :param secret: true if the value should be obfuscated in log output :param required: true if a value must be supplied for this option :param deprecated_name: deprecated name option. Acts like an alias :param deprecated_group: the group containing a deprecated alias :param deprecated_opts: list of :class:`.DeprecatedOpt` :param sample_default: a default string for sample config files :param deprecated_for_removal: indicates whether this opt is planned for removal in a future release :param deprecated_reason: indicates why this opt is planned for removal in a future release. Silently ignored if deprecated_for_removal is False :param deprecated_since: indicates which release this opt was deprecated in. Accepts any string, though valid version strings are encouraged. Silently ignored if deprecated_for_removal is False :param mutable: True if this option may be reloaded :param advanced: a bool True/False value if this option has advanced usage and is not normally used by the majority of users An Opt object has no public methods, but has a number of public properties: .. py:attribute:: name the name of the option, which may include hyphens .. py:attribute:: type a callable object that takes string and returns converted and validated value. Default types are available from :class:`oslo_config.types` .. py:attribute:: dest the (hyphen-less) :class:`.ConfigOpts` property which contains the option value .. py:attribute:: short a single character CLI option name .. py:attribute:: default the default value of the option .. py:attribute:: sample_default a sample default value string to include in sample config files .. py:attribute:: positional True if the option is a positional CLI argument .. py:attribute:: metavar the name shown as the argument to a CLI option in --help output .. py:attribute:: help a string explaining how the option's value is used .. py:attribute:: advanced in sample files, a bool value indicating the option is advanced .. versionchanged:: 1.2 Added *deprecated_opts* parameter. .. versionchanged:: 1.4 Added *sample_default* parameter. .. versionchanged:: 1.9 Added *deprecated_for_removal* parameter. .. versionchanged:: 2.7 An exception is now raised if the default value has the wrong type. .. versionchanged:: 3.2 Added *deprecated_reason* parameter. .. versionchanged:: 3.5 Added *mutable* parameter. .. versionchanged:: 3.12 Added *deprecated_since* parameter. .. versionchanged:: 3.15 Added *advanced* parameter and attribute. """ multi = False def __init__(self, name, type=None, dest=None, short=None, default=None, positional=False, metavar=None, help=None, secret=False, required=False, deprecated_name=None, deprecated_group=None, deprecated_opts=None, sample_default=None, deprecated_for_removal=False, deprecated_reason=None, deprecated_since=None, mutable=False, advanced=False): if name.startswith('_'): raise ValueError('illegal name %s with prefix _' % (name,)) self.name = name if type is None: type = types.String() if not callable(type): raise TypeError('type must be callable') self.type = type if dest is None: self.dest = self.name.replace('-', '_') else: self.dest = dest self.short = short self.default = default self.sample_default = sample_default self.positional = positional self.metavar = metavar self.help = help self.secret = secret self.required = required self.deprecated_for_removal = deprecated_for_removal self.deprecated_reason = deprecated_reason self.deprecated_since = deprecated_since self._logged_deprecation = False self.deprecated_opts = copy.deepcopy(deprecated_opts) or [] for o in self.deprecated_opts: if '-' in o.name: self.deprecated_opts.append(DeprecatedOpt( o.name.replace('-', '_'), group=o.group)) if deprecated_name is not None or deprecated_group is not None: self.deprecated_opts.append(DeprecatedOpt(deprecated_name, group=deprecated_group)) if deprecated_name and '-' in deprecated_name: self.deprecated_opts.append(DeprecatedOpt( deprecated_name.replace('-', '_'), group=deprecated_group)) self._check_default() self.mutable = mutable self.advanced = advanced def _default_is_ref(self): """Check if default is a reference to another var.""" if isinstance(self.default, six.string_types): tmpl = self.default.replace(r'\$', '').replace('$$', '') return '$' in tmpl return False def _check_default(self): if (self.default is not None and not self._default_is_ref()): try: self.type(self.default) except Exception: raise DefaultValueError("Error processing default value " "%(default)s for Opt type of %(opt)s." % {'default': self.default, 'opt': self.type}) def __ne__(self, another): return vars(self) != vars(another) def __eq__(self, another): return vars(self) == vars(another) __hash__ = object.__hash__ def _get_from_namespace(self, namespace, group_name): """Retrieves the option value from a _Namespace object. :param namespace: a _Namespace object :param group_name: a group name """ names = [(group_name, self.dest)] current_name = (group_name, self.name) for opt in self.deprecated_opts: dname, dgroup = opt.name, opt.group if dname or dgroup: names.append((dgroup if dgroup else group_name, dname if dname else self.dest)) value = namespace._get_value( names, multi=self.multi, positional=self.positional, current_name=current_name) # The previous line will raise a KeyError if no value is set in the # config file, so we'll only log deprecations for set options. if self.deprecated_for_removal and not self._logged_deprecation: self._logged_deprecation = True pretty_group = group_name or 'DEFAULT' if self.deprecated_reason: pretty_reason = ' ({})'.format(self.deprecated_reason) else: pretty_reason = '' LOG.warning('Option "%(option)s" from group "%(group)s" is ' 'deprecated for removal%(reason)s. Its value may be ' 'silently ignored in the future.', {'option': self.dest, 'group': pretty_group, 'reason': pretty_reason}) return value def _add_to_cli(self, parser, group=None): """Makes the option available in the command line interface. This is the method ConfigOpts uses to add the opt to the CLI interface as appropriate for the opt type. Some opt types may extend this method, others may just extend the helper methods it uses. :param parser: the CLI option parser :param group: an optional OptGroup object """ container = self._get_argparse_container(parser, group) kwargs = self._get_argparse_kwargs(group) prefix = self._get_argparse_prefix('', group.name if group else None) deprecated_names = [] for opt in self.deprecated_opts: deprecated_name = self._get_deprecated_cli_name(opt.name, opt.group) if deprecated_name is not None: deprecated_names.append(deprecated_name) self._add_to_argparse(parser, container, self.name, self.short, kwargs, prefix, self.positional, deprecated_names) def _add_to_argparse(self, parser, container, name, short, kwargs, prefix='', positional=False, deprecated_names=None): """Add an option to an argparse parser or group. :param container: an argparse._ArgumentGroup object :param name: the opt name :param short: the short opt name :param kwargs: the keyword arguments for add_argument() :param prefix: an optional prefix to prepend to the opt name :param positional: whether the option is a positional CLI argument :param deprecated_names: list of deprecated option names """ def hyphen(arg): return arg if not positional else '' args = [hyphen('--') + prefix + name] if short: args.append(hyphen('-') + short) for deprecated_name in deprecated_names: args.append(hyphen('--') + deprecated_name) parser.add_parser_argument(container, *args, **kwargs) def _get_argparse_container(self, parser, group): """Returns an argparse._ArgumentGroup. :param parser: an argparse.ArgumentParser :param group: an (optional) OptGroup object :returns: an argparse._ArgumentGroup if group is given, else parser """ if group is not None: return group._get_argparse_group(parser) else: return parser def _get_argparse_kwargs(self, group, **kwargs): r"""Build a dict of keyword arguments for argparse's add_argument(). Most opt types extend this method to customize the behaviour of the options added to argparse. :param group: an optional group :param \*\*kwargs: optional keyword arguments to add to :returns: a dict of keyword arguments """ if not self.positional: dest = self.dest if group is not None: dest = group.name + '_' + dest kwargs['dest'] = dest else: kwargs['nargs'] = '?' kwargs.update({'default': None, 'metavar': self.metavar, 'help': self.help, }) return kwargs def _get_argparse_prefix(self, prefix, group_name): """Build a prefix for the CLI option name, if required. CLI options in a group are prefixed with the group's name in order to avoid conflicts between similarly named options in different groups. :param prefix: an existing prefix to append to (for example 'no' or '') :param group_name: an optional group name :returns: a CLI option prefix including the group name, if appropriate """ if group_name is not None: return group_name + '-' + prefix else: return prefix def _get_deprecated_cli_name(self, dname, dgroup, prefix=''): """Build a CLi arg name for deprecated options. Either a deprecated name or a deprecated group or both or neither can be supplied: dname, dgroup -> dgroup + '-' + dname dname -> dname dgroup -> dgroup + '-' + self.name neither -> None :param dname: a deprecated name, which can be None :param dgroup: a deprecated group, which can be None :param prefix: an prefix to append to (for example 'no' or '') :returns: a CLI argument name """ if dgroup == 'DEFAULT': dgroup = None if dname is None and dgroup is None: return None if dname is None: dname = self.name return self._get_argparse_prefix(prefix, dgroup) + dname def __lt__(self, another): return hash(self) < hash(another) class DeprecatedOpt(object): """Represents a Deprecated option. Here's how you can use it:: oldopts = [cfg.DeprecatedOpt('oldopt1', group='group1'), cfg.DeprecatedOpt('oldopt2', group='group2')] cfg.CONF.register_group(cfg.OptGroup('group1')) cfg.CONF.register_opt(cfg.StrOpt('newopt', deprecated_opts=oldopts), group='group1') For options which have a single value (like in the example above), if the new option is present ("[group1]/newopt" above), it will override any deprecated options present ("[group1]/oldopt1" and "[group2]/oldopt2" above). If no group is specified for a DeprecatedOpt option (i.e. the group is None), lookup will happen within the same group the new option is in. For example, if no group was specified for the second option 'oldopt2' in oldopts list:: oldopts = [cfg.DeprecatedOpt('oldopt1', group='group1'), cfg.DeprecatedOpt('oldopt2')] cfg.CONF.register_group(cfg.OptGroup('group1')) cfg.CONF.register_opt(cfg.StrOpt('newopt', deprecated_opts=oldopts), group='group1') then lookup for that option will happen in group 'group1'. If the new option is not present and multiple deprecated options are present, the option corresponding to the first element of deprecated_opts will be chosen. Multi-value options will return all new and deprecated options. So if we have a multi-value option "[group1]/opt1" whose deprecated option is "[group2]/opt2", and the conf file has both these options specified like so:: [group1] opt1=val10,val11 [group2] opt2=val21,val22 Then the value of "[group1]/opt1" will be ['val10', 'val11', 'val21', 'val22']. .. versionadded:: 1.2 """ def __init__(self, name, group=None): """Constructs an DeprecatedOpt object. :param name: the name of the option :param group: the group of the option """ self.name = name self.group = group def __key(self): return (self.name, self.group) def __eq__(self, other): return self.__key() == other.__key() def __hash__(self): return hash(self.__key()) class StrOpt(Opt): r"""Option with String type Option with ``type`` :class:`oslo_config.types.String` :param name: the option's name :param choices: Optional sequence of either valid values or tuples of valid values with descriptions. :param quotes: If True and string is enclosed with single or double quotes, will strip those quotes. :param regex: Optional regular expression (string or compiled regex) that the value must match on an unanchored search. :param ignore_case: If True case differences (uppercase vs. lowercase) between 'choices' or 'regex' will be ignored. :param max_length: If positive integer, the value must be less than or equal to this parameter. :param \*\*kwargs: arbitrary keyword arguments passed to :class:`Opt` .. versionchanged:: 2.7 Added *quotes* parameter .. versionchanged:: 2.7 Added *regex* parameter .. versionchanged:: 2.7 Added *ignore_case* parameter .. versionchanged:: 2.7 Added *max_length* parameter .. versionchanged:: 5.2 The *choices* parameter will now accept a sequence of tuples, where each tuple is of form (*choice*, *description*) """ def __init__(self, name, choices=None, quotes=None, regex=None, ignore_case=False, max_length=None, **kwargs): super(StrOpt, self).__init__(name, type=types.String( choices=choices, quotes=quotes, regex=regex, ignore_case=ignore_case, max_length=max_length), **kwargs) def _get_choice_text(self, choice): if choice is None: return '' elif choice == '': return "''" return six.text_type(choice) def _get_argparse_kwargs(self, group, **kwargs): """Extends the base argparse keyword dict for the config dir option.""" kwargs = super(StrOpt, self)._get_argparse_kwargs(group) if getattr(self.type, 'choices', None): choices_text = ', '.join([self._get_choice_text(choice) for choice in self.type.choices]) if kwargs['help'] is None: kwargs['help'] = '' kwargs['help'].rstrip('\n') kwargs['help'] += '\n Allowed values: %s\n' % choices_text return kwargs class BoolOpt(Opt): r"""Boolean options. Bool opts are set to True or False on the command line using --optname or --nooptname respectively. In config files, boolean values are cast with Boolean type. :param name: the option's name :param \*\*kwargs: arbitrary keyword arguments passed to :class:`Opt` """ def __init__(self, name, **kwargs): if 'positional' in kwargs: raise ValueError('positional boolean args not supported') super(BoolOpt, self).__init__(name, type=types.Boolean(), **kwargs) def _add_to_cli(self, parser, group=None): """Extends the base class method to add the --nooptname option.""" super(BoolOpt, self)._add_to_cli(parser, group) self._add_inverse_to_argparse(parser, group) def _add_inverse_to_argparse(self, parser, group): """Add the --nooptname option to the option parser.""" container = self._get_argparse_container(parser, group) kwargs = self._get_argparse_kwargs(group, action='store_false') prefix = self._get_argparse_prefix('no', group.name if group else None) deprecated_names = [] for opt in self.deprecated_opts: deprecated_name = self._get_deprecated_cli_name(opt.name, opt.group, prefix='no') if deprecated_name is not None: deprecated_names.append(deprecated_name) kwargs["help"] = "The inverse of --" + self.name self._add_to_argparse(parser, container, self.name, None, kwargs, prefix, self.positional, deprecated_names) def _get_argparse_kwargs(self, group, action='store_true', **kwargs): """Extends the base argparse keyword dict for boolean options.""" kwargs = super(BoolOpt, self)._get_argparse_kwargs(group, **kwargs) # type has no effect for BoolOpt, it only matters for # values that came from config files if 'type' in kwargs: del kwargs['type'] # metavar has no effect for BoolOpt if 'metavar' in kwargs: del kwargs['metavar'] kwargs['action'] = action return kwargs class IntOpt(Opt): r"""Option with Integer type Option with ``type`` :class:`oslo_config.types.Integer` :param name: the option's name :param min: minimum value the integer can take :param max: maximum value the integer can take :param \*\*kwargs: arbitrary keyword arguments passed to :class:`Opt` .. versionchanged:: 1.15 Added *min* and *max* parameters. """ def __init__(self, name, min=None, max=None, **kwargs): super(IntOpt, self).__init__(name, type=types.Integer(min, max), **kwargs) class FloatOpt(Opt): r"""Option with Float type Option with ``type`` :class:`oslo_config.types.Float` :param name: the option's name :param min: minimum value the float can take :param max: maximum value the float can take :param \*\*kwargs: arbitrary keyword arguments passed to :class:`Opt` .. versionchanged:: 3.14 Added *min* and *max* parameters. """ def __init__(self, name, min=None, max=None, **kwargs): super(FloatOpt, self).__init__(name, type=types.Float(min, max), **kwargs) class ListOpt(Opt): r"""Option with List(String) type Option with ``type`` :class:`oslo_config.types.List` :param name: the option's name :param item_type: type of items (see :class:`oslo_config.types`) :param bounds: if True the value should be inside "[" and "]" pair :param \*\*kwargs: arbitrary keyword arguments passed to :class:`Opt` .. versionchanged:: 2.5 Added *item_type* and *bounds* parameters. """ def __init__(self, name, item_type=None, bounds=None, **kwargs): super(ListOpt, self).__init__(name, type=types.List(item_type=item_type, bounds=bounds), **kwargs) class DictOpt(Opt): r"""Option with Dict(String) type Option with ``type`` :class:`oslo_config.types.Dict` :param name: the option's name :param \*\*kwargs: arbitrary keyword arguments passed to :class:`Opt` .. versionadded:: 1.2 """ def __init__(self, name, **kwargs): super(DictOpt, self).__init__(name, type=types.Dict(), **kwargs) class IPOpt(Opt): r"""Opt with IPAddress type Option with ``type`` :class:`oslo_config.types.IPAddress` :param name: the option's name :param version: one of either ``4``, ``6``, or ``None`` to specify either version. :param \*\*kwargs: arbitrary keyword arguments passed to :class:`Opt` .. versionadded:: 1.4 """ def __init__(self, name, version=None, **kwargs): super(IPOpt, self).__init__(name, type=types.IPAddress(version), **kwargs) class PortOpt(Opt): r"""Option for a TCP/IP port number. Ports can range from 0 to 65535. Option with ``type`` :class:`oslo_config.types.Integer` :param name: the option's name :param min: minimum value the port can take :param max: maximum value the port can take :param choices: Optional sequence of either valid values or tuples of valid values with descriptions. :param \*\*kwargs: arbitrary keyword arguments passed to :class:`Opt` .. versionadded:: 2.6 .. versionchanged:: 3.2 Added *choices* parameter. .. versionchanged:: 3.4 Allow port number with 0. .. versionchanged:: 3.16 Added *min* and *max* parameters. .. versionchanged:: 5.2 The *choices* parameter will now accept a sequence of tuples, where each tuple is of form (*choice*, *description*) """ def __init__(self, name, min=None, max=None, choices=None, **kwargs): type = types.Port(min=min, max=max, choices=choices, type_name='port value') super(PortOpt, self).__init__(name, type=type, **kwargs) class HostnameOpt(Opt): r"""Option for a hostname. Only accepts valid hostnames. Option with ``type`` :class:`oslo_config.types.Hostname` :param name: the option's name :param \*\*kwargs: arbitrary keyword arguments passed to :class:`Opt` .. versionadded:: 3.8 """ def __init__(self, name, **kwargs): super(HostnameOpt, self).__init__(name, type=types.Hostname(), **kwargs) class HostAddressOpt(Opt): r"""Option for either an IP or a hostname. Accepts valid hostnames and valid IP addresses. Option with ``type`` :class:`oslo_config.types.HostAddress` :param name: the option's name :param version: one of either ``4``, ``6``, or ``None`` to specify either version. :param \*\*kwargs: arbitrary keyword arguments passed to :class:`Opt` .. versionadded:: 3.22 """ def __init__(self, name, version=None, **kwargs): super(HostAddressOpt, self).__init__(name, type=types.HostAddress(version), **kwargs) class URIOpt(Opt): r"""Opt with URI type Option with ``type`` :class:`oslo_config.types.URI` :param name: the option's name :param max_length: If positive integer, the value must be less than or equal to this parameter. :param schemes: list of valid URI schemes, e.g. 'https', 'ftp', 'git' :param \*\*kwargs: arbitrary keyword arguments passed to :class:`Opt` .. versionadded:: 3.12 .. versionchanged:: 3.14 Added *max_length* parameter .. versionchanged:: 3.18 Added *schemes* parameter """ def __init__(self, name, max_length=None, schemes=None, **kwargs): type = types.URI(max_length=max_length, schemes=schemes) super(URIOpt, self).__init__(name, type=type, **kwargs) class MultiOpt(Opt): r"""Multi-value option. Multi opt values are typed opts which may be specified multiple times. The opt value is a list containing all the values specified. :param name: the option's name :param item_type: Type of items (see :class:`oslo_config.types`) :param \*\*kwargs: arbitrary keyword arguments passed to :class:`Opt` For example:: cfg.MultiOpt('foo', item_type=types.Integer(), default=None, help="Multiple foo option") The command line ``--foo=1 --foo=2`` would result in ``cfg.CONF.foo`` containing ``[1,2]`` .. versionadded:: 1.3 """ multi = True def __init__(self, name, item_type, **kwargs): super(MultiOpt, self).__init__(name, item_type, **kwargs) def _get_argparse_kwargs(self, group, **kwargs): """Extends the base argparse keyword dict for multi value options.""" kwargs = super(MultiOpt, self)._get_argparse_kwargs(group) if not self.positional: kwargs['action'] = 'append' else: kwargs['nargs'] = '*' return kwargs class MultiStrOpt(MultiOpt): r"""MultiOpt with a MultiString ``item_type``. MultiOpt with a default :class:`oslo_config.types.MultiString` item type. :param name: the option's name :param \*\*kwargs: arbitrary keyword arguments passed to :class:`MultiOpt` """ def __init__(self, name, **kwargs): super(MultiStrOpt, self).__init__(name, item_type=types.MultiString(), **kwargs) class SubCommandOpt(Opt): """Sub-command options. Sub-command options allow argparse sub-parsers to be used to parse additional command line arguments. The handler argument to the SubCommandOpt constructor is a callable which is supplied an argparse subparsers object. Use this handler callable to add sub-parsers. The opt value is SubCommandAttr object with the name of the chosen sub-parser stored in the 'name' attribute and the values of other sub-parser arguments available as additional attributes. :param name: the option's name :param dest: the name of the corresponding :class:`.ConfigOpts` property :param handler: callable which is supplied subparsers object when invoked :param title: title of the sub-commands group in help output :param description: description of the group in help output :param help: a help string giving an overview of available sub-commands """ def __init__(self, name, dest=None, handler=None, title=None, description=None, help=None): """Construct an sub-command parsing option. This behaves similarly to other Opt sub-classes but adds a 'handler' argument. The handler is a callable which is supplied an subparsers object when invoked. The add_parser() method on this subparsers object can be used to register parsers for sub-commands. """ super(SubCommandOpt, self).__init__(name, type=types.String(), dest=dest, help=help) self.handler = handler self.title = title self.description = description def _add_to_cli(self, parser, group=None): """Add argparse sub-parsers and invoke the handler method.""" dest = self.dest if group is not None: dest = group.name + '_' + dest subparsers = parser.add_subparsers(dest=dest, title=self.title, description=self.description, help=self.help) # NOTE(jd) Set explicitly to True for Python 3 # See http://bugs.python.org/issue9253 for context subparsers.required = True if self.handler is not None: self.handler(subparsers) class _ConfigFileOpt(Opt): """The --config-file option. This is an private option type which handles the special processing required for --config-file options. As each --config-file option is encountered on the command line, we parse the file and store the parsed values in the _Namespace object. This allows us to properly handle the precedence of --config-file options over previous command line arguments, but not over subsequent arguments. .. versionadded:: 1.2 """ class ConfigFileAction(argparse.Action): """An argparse action for --config-file. As each --config-file option is encountered, this action adds the value to the config_file attribute on the _Namespace object but also parses the configuration file and stores the values found also in the _Namespace object. """ def __call__(self, parser, namespace, values, option_string=None): """Handle a --config-file command line argument. :raises: ConfigFileParseError, ConfigFileValueError """ if getattr(namespace, self.dest, None) is None: setattr(namespace, self.dest, []) items = getattr(namespace, self.dest) items.append(values) ConfigParser._parse_file(values, namespace) def __init__(self, name, **kwargs): super(_ConfigFileOpt, self).__init__(name, lambda x: x, **kwargs) def _get_argparse_kwargs(self, group, **kwargs): """Extends the base argparse keyword dict for the config file opt.""" kwargs = super(_ConfigFileOpt, self)._get_argparse_kwargs(group) kwargs['action'] = self.ConfigFileAction return kwargs class _ConfigDirOpt(Opt): """The --config-dir option. This is an private option type which handles the special processing required for --config-dir options. As each --config-dir option is encountered on the command line, we parse the files in that directory and store the parsed values in the _Namespace object. This allows us to properly handle the precedence of --config-dir options over previous command line arguments, but not over subsequent arguments. .. versionadded:: 1.2 """ class ConfigDirAction(argparse.Action): """An argparse action for --config-dir. As each --config-dir option is encountered, this action sets the config_dir attribute on the _Namespace object but also parses the configuration files and stores the values found also in the _Namespace object. """ def __call__(self, parser, namespace, values, option_string=None): """Handle a --config-dir command line argument. :raises: ConfigFileParseError, ConfigFileValueError, ConfigDirNotFoundError """ namespace._config_dirs.append(values) setattr(namespace, self.dest, values) values = os.path.expanduser(values) if not os.path.exists(values): raise ConfigDirNotFoundError(values) config_dir_glob = os.path.join(values, '*.conf') for config_file in sorted(glob.glob(config_dir_glob)): ConfigParser._parse_file(config_file, namespace) def __init__(self, name, **kwargs): super(_ConfigDirOpt, self).__init__(name, type=types.List(), **kwargs) def _get_argparse_kwargs(self, group, **kwargs): """Extends the base argparse keyword dict for the config dir option.""" kwargs = super(_ConfigDirOpt, self)._get_argparse_kwargs(group) kwargs['action'] = self.ConfigDirAction return kwargs class OptGroup(object): """Represents a group of opts. CLI opts in the group are automatically prefixed with the group name. Each group corresponds to a section in config files. An OptGroup object has no public methods, but has a number of public string properties: .. py:attribute:: name the name of the group .. py:attribute:: title the group title as displayed in --help .. py:attribute:: help the group description as displayed in --help :param name: the group name :type name: str :param title: the group title for --help :type title: str :param help: the group description for --help :type help: str :param dynamic_group_owner: The name of the option that controls repeated instances of this group. :type dynamic_group_owner: str :param driver_option: The name of the option within the group that controls which driver will register options. :type driver_option: str """ def __init__(self, name, title=None, help=None, dynamic_group_owner='', driver_option=''): """Constructs an OptGroup object.""" self.name = name self.title = "%s options" % name if title is None else title self.help = help self.dynamic_group_owner = dynamic_group_owner self.driver_option = driver_option self._opts = {} # dict of dicts of (opt:, override:, default:) self._argparse_group = None self._driver_opts = {} # populated by the config generator def _save_driver_opts(self, opts): """Save known driver opts. :param opts: mapping between driver name and list of opts :type opts: dict """ self._driver_opts.update(opts) def _get_generator_data(self): "Return a dict with data for the sample generator." return { 'help': self.help or '', 'dynamic_group_owner': self.dynamic_group_owner, 'driver_option': self.driver_option, 'driver_opts': self._driver_opts, } def _register_opt(self, opt, cli=False): """Add an opt to this group. :param opt: an Opt object :param cli: whether this is a CLI option :returns: False if previously registered, True otherwise :raises: DuplicateOptError if a naming conflict is detected """ if _is_opt_registered(self._opts, opt): return False self._opts[opt.dest] = {'opt': opt, 'cli': cli} return True def _unregister_opt(self, opt): """Remove an opt from this group. :param opt: an Opt object """ if opt.dest in self._opts: del self._opts[opt.dest] def _get_argparse_group(self, parser): if self._argparse_group is None: """Build an argparse._ArgumentGroup for this group.""" self._argparse_group = parser.add_argument_group(self.title, self.help) return self._argparse_group def _clear(self): """Clear this group's option parsing state.""" self._argparse_group = None def __str__(self): return self.name class ParseError(iniparser.ParseError): def __init__(self, msg, lineno, line, filename): super(ParseError, self).__init__(msg, lineno, line) self.filename = filename def __str__(self): return 'at %s:%d, %s: %r' % (self.filename, self.lineno, self.msg, self.line) class ConfigParser(iniparser.BaseParser): """Parses a single config file, populating 'sections' to look like: {'DEFAULT': {'key': [value, ...], ...}, ...} Also populates self._normalized which looks the same but with normalized section names. """ def __init__(self, filename, sections): super(ConfigParser, self).__init__() self.filename = filename self.sections = sections self._normalized = None self.section = None def _add_normalized(self, normalized): self._normalized = normalized def parse(self): with open(self.filename) as f: return super(ConfigParser, self).parse(f) def new_section(self, section): self.section = section self.sections.setdefault(self.section, {}) if self._normalized is not None: self._normalized.setdefault(_normalize_group_name(self.section), {}) def assignment(self, key, value): if not self.section: raise self.error_no_section() value = '\n'.join(value) def append(sections, section): sections[section].setdefault(key, []) sections[section][key].append(value) append(self.sections, self.section) if self._normalized is not None: append(self._normalized, _normalize_group_name(self.section)) def parse_exc(self, msg, lineno, line=None): return ParseError(msg, lineno, line, self.filename) def error_no_section(self): return self.parse_exc('Section must be started before assignment', self.lineno) @classmethod def _parse_file(cls, config_file, namespace): """Parse a config file and store any values in the namespace. :raises: ConfigFileParseError, ConfigFileValueError """ config_file = _fixpath(config_file) sections = {} normalized = {} parser = cls(config_file, sections) parser._add_normalized(normalized) try: parser.parse() except iniparser.ParseError as pe: raise ConfigFileParseError(pe.filename, str(pe)) except IOError as err: if err.errno == errno.ENOENT: namespace._file_not_found(config_file) return if err.errno == errno.EACCES: namespace._file_permission_denied(config_file) return raise namespace._add_parsed_config_file(sections, normalized) namespace._parse_cli_opts_from_config_file(sections, normalized) @removals.remove(version='3.4', removal_version='4.0') class MultiConfigParser(object): """A ConfigParser which handles multi-opts. All methods in this class which accept config names should treat a section name of None as 'DEFAULT'. This class was deprecated in Mitaka and should be removed in Ocata. _Namespace holds values, ConfigParser._parse_file reads one file into a _Namespace and ConfigOpts._parse_config_files reads multiple files into a _Namespace. """ _deprecated_opt_message = ('Option "%(dep_option)s" from group ' '"%(dep_group)s" is deprecated. Use option ' '"%(option)s" from group "%(group)s".') def __init__(self): self.parsed = [] self._normalized = [] self._emitted_deprecations = set() def read(self, config_files): read_ok = [] for filename in config_files: sections = {} normalized = {} parser = ConfigParser(filename, sections) parser._add_normalized(normalized) try: parser.parse() except IOError: continue self._add_parsed_config_file(sections, normalized) read_ok.append(filename) return read_ok def _add_parsed_config_file(self, sections, normalized): """Add a parsed config file to the list of parsed files. :param sections: a mapping of section name to dicts of config values :param normalized: sections mapping with section names normalized :raises: ConfigFileValueError """ self.parsed.insert(0, sections) self._normalized.insert(0, normalized) def get(self, names, multi=False): return self._get(names, multi=multi) def _get(self, names, multi=False, normalized=False, current_name=None): """Fetch a config file value from the parsed files. :param names: a list of (section, name) tuples :param multi: a boolean indicating whether to return multiple values :param normalized: whether to normalize group names to lowercase :param current_name: current name in tuple being checked """ rvalue = [] def normalize(name): if name is None: name = 'DEFAULT' return _normalize_group_name(name) if normalized else name names = [(normalize(section), name) for section, name in names] for sections in (self._normalized if normalized else self.parsed): for section, name in names: if section not in sections: continue if name in sections[section]: current_name = current_name or names[0] self._check_deprecated((section, name), current_name, names[1:]) val = sections[section][name] if multi: rvalue = val + rvalue else: return val if multi and rvalue != []: return rvalue raise KeyError def _check_deprecated(self, name, current, deprecated): """Check for usage of deprecated names. :param name: A tuple of the form (group, name) representing the group and name where an opt value was found. :param current: A tuple of the form (group, name) representing the current name for an option. :param deprecated: A list of tuples with the same format as the name param which represent any deprecated names for an option. If the name param matches any entries in this list a deprecation warning will be logged. """ if name in deprecated and name not in self._emitted_deprecations: self._emitted_deprecations.add(name) current = (current[0] or 'DEFAULT', current[1]) # NOTE(bnemec): Not using versionutils for this to avoid a # circular dependency between oslo.config and whatever library # versionutils ends up in. LOG.warning(self._deprecated_opt_message, {'dep_option': name[1], 'dep_group': name[0], 'option': current[1], 'group': current[0]}) class _Namespace(argparse.Namespace): """An argparse namespace which also stores config file values. As we parse command line arguments, the values get set as attributes on a namespace object. However, we also want to parse config files as they are specified on the command line and collect the values alongside the option values parsed from the command line. Note, we don't actually assign values from config files as attributes on the namespace because config file options be registered after the command line has been parsed, so we may not know how to properly parse or convert a config file value at this point. """ _deprecated_opt_message = ('Option "%(dep_option)s" from group ' '"%(dep_group)s" is deprecated. Use option ' '"%(option)s" from group "%(group)s".') def __init__(self, conf): self._conf = conf self._parsed = [] self._normalized = [] self._emitted_deprecations = set() self._files_not_found = [] self._files_permission_denied = [] self._config_dirs = [] def _parse_cli_opts_from_config_file(self, sections, normalized): """Parse CLI options from a config file. CLI options are special - we require they be registered before the command line is parsed. This means that as we parse config files, we can go ahead and apply the appropriate option-type specific conversion to the values in config files for CLI options. We can't do this for non-CLI options, because the schema describing those options may not be registered until after the config files are parsed. This method relies on that invariant in order to enforce proper priority of option values - i.e. that the order in which an option value is parsed, whether the value comes from the CLI or a config file, determines which value specified for a given option wins. The way we implement this ordering is that as we parse each config file, we look for values in that config file for CLI options only. Any values for CLI options found in the config file are treated like they had appeared on the command line and set as attributes on the namespace objects. Values in later config files or on the command line will override values found in this file. """ namespace = _Namespace(self._conf) namespace._add_parsed_config_file(sections, normalized) for opt, group in self._conf._all_cli_opts(): group_name = group.name if group is not None else None try: value = opt._get_from_namespace(namespace, group_name) except KeyError: continue except ValueError as ve: raise ConfigFileValueError( "Value for option %s is not valid: %s" % (opt.name, str(ve))) if group_name is None: dest = opt.dest else: dest = group_name + '_' + opt.dest if opt.multi: if getattr(self, dest, None) is None: setattr(self, dest, []) values = getattr(self, dest) values.extend(value) else: setattr(self, dest, value) def _add_parsed_config_file(self, sections, normalized): """Add a parsed config file to the list of parsed files. :param sections: a mapping of section name to dicts of config values :param normalized: sections mapping with section names normalized :raises: ConfigFileValueError """ self._parsed.insert(0, sections) self._normalized.insert(0, normalized) def _file_not_found(self, config_file): """Record that we were unable to open a config file. :param config_file: the path to the failed file """ self._files_not_found.append(config_file) def _file_permission_denied(self, config_file): """Record that we have no permission to open a config file. :param config_file: the path to the failed file """ self._files_permission_denied.append(config_file) def _get_cli_value(self, names, positional=False): """Fetch a CLI option value. Look up the value of a CLI option. The value itself may have come from parsing the command line or parsing config files specified on the command line. Type conversion have already been performed for CLI options at this point. :param names: a list of (section, name) tuples :param positional: whether this is a positional option """ for group_name, name in names: name = name if group_name is None else group_name + '_' + name value = getattr(self, name, None) if value is not None: # argparse ignores default=None for nargs='*' and returns [] if positional and not value: continue return value raise KeyError def _get_file_value( self, names, multi=False, normalized=False, current_name=None): """Fetch a config file value from the parsed files. :param names: a list of (section, name) tuples :param multi: a boolean indicating whether to return multiple values :param normalized: whether to normalize group names to lowercase :param current_name: current name in tuple being checked """ rvalue = [] def normalize(name): if name is None: name = 'DEFAULT' return _normalize_group_name(name) if normalized else name names = [(normalize(section), name) for section, name in names] for sections in (self._normalized if normalized else self._parsed): for section, name in names: if section not in sections: continue if name in sections[section]: current_name = current_name or names[0] self._check_deprecated((section, name), current_name, names[1:]) val = sections[section][name] if multi: rvalue = val + rvalue else: return val if multi and rvalue != []: return rvalue raise KeyError def _check_deprecated(self, name, current, deprecated): """Check for usage of deprecated names. :param name: A tuple of the form (group, name) representing the group and name where an opt value was found. :param current: A tuple of the form (group, name) representing the current name for an option. :param deprecated: A list of tuples with the same format as the name param which represent any deprecated names for an option. If the name param matches any entries in this list a deprecation warning will be logged. """ if name in deprecated and name not in self._emitted_deprecations: self._emitted_deprecations.add(name) current = (current[0] or 'DEFAULT', current[1]) # NOTE(bnemec): Not using versionutils for this to avoid a # circular dependency between oslo.config and whatever library # versionutils ends up in. LOG.warning(self._deprecated_opt_message, {'dep_option': name[1], 'dep_group': name[0], 'option': current[1], 'group': current[0]}) def _get_value(self, names, multi=False, positional=False, current_name=None, normalized=True): """Fetch a value from config files. Multiple names for a given configuration option may be supplied so that we can transparently handle files containing deprecated option names or groups. :param names: a list of (section, name) tuples :param positional: whether this is a positional option :param multi: a boolean indicating whether to return multiple values :param normalized: whether to normalize group names to lowercase """ try: return self._get_cli_value(names, positional) except KeyError: names = [(g if g is not None else 'DEFAULT', n) for g, n in names] values = self._get_file_value( names, multi=multi, normalized=normalized, current_name=current_name) return values if multi else values[-1] def _sections(self): for sections in self._parsed: for section in sections: yield section class _CachedArgumentParser(argparse.ArgumentParser): """class for caching/collecting command line arguments. It also sorts the arguments before initializing the ArgumentParser. We need to do this since ArgumentParser by default does not sort the argument options and the only way to influence the order of arguments in '--help' is to ensure they are added in the sorted order. """ def __init__(self, prog=None, usage=None, **kwargs): super(_CachedArgumentParser, self).__init__(prog, usage, **kwargs) self._args_cache = {} def add_parser_argument(self, container, *args, **kwargs): values = [] if container in self._args_cache: values = self._args_cache[container] values.append({'args': args, 'kwargs': kwargs}) self._args_cache[container] = values def initialize_parser_arguments(self): # NOTE(mfedosin): The code below looks a little bit weird, but # it's done because we need to sort only optional opts and do # not touch positional. For the reason optional opts go first in # the values we only need to find an index of the first positional # option and then sort the values slice. for container, values in self._args_cache.items(): index = 0 has_positional = False for index, argument in enumerate(values): if not argument['args'][0].startswith('-'): has_positional = True break size = index if has_positional else len(values) values[:size] = sorted(values[:size], key=lambda x: x['args']) for argument in values: try: container.add_argument(*argument['args'], **argument['kwargs']) except argparse.ArgumentError: options = ','.join(argument['args']) raise DuplicateOptError(options) self._args_cache = {} def parse_args(self, args=None, namespace=None): self.initialize_parser_arguments() return super(_CachedArgumentParser, self).parse_args(args, namespace) def print_help(self, file=None): self.initialize_parser_arguments() super(_CachedArgumentParser, self).print_help(file) def print_usage(self, file=None): self.initialize_parser_arguments() super(_CachedArgumentParser, self).print_usage(file) class ConfigOpts(collections.Mapping): """Config options which may be set on the command line or in config files. ConfigOpts is a configuration option manager with APIs for registering option schemas, grouping options, parsing option values and retrieving the values of options. It has built-in support for :oslo.config:option:`config_file` and :oslo.config:option:`config_dir` options. """ disallow_names = ('project', 'prog', 'version', 'usage', 'default_config_files', 'default_config_dirs') def __init__(self): """Construct a ConfigOpts object.""" self._opts = {} # dict of dicts of (opt:, override:, default:) self._groups = {} self._deprecated_opts = {} self._args = None self._oparser = None self._namespace = None self._mutable_ns = None self._mutate_hooks = set([]) self.__cache = {} self._config_opts = [] self._cli_opts = collections.deque() self._validate_default_values = False def _pre_setup(self, project, prog, version, usage, description, epilog, default_config_files, default_config_dirs): """Initialize a ConfigCliParser object for option parsing.""" if prog is None: prog = os.path.basename(sys.argv[0]) if prog.endswith(".py"): prog = prog[:-3] if default_config_files is None: default_config_files = find_config_files(project, prog) if default_config_dirs is None: default_config_dirs = find_config_dirs(project, prog) self._oparser = _CachedArgumentParser( prog=prog, usage=usage, description=description, epilog=epilog) if version is not None: self._oparser.add_parser_argument(self._oparser, '--version', action='version', version=version) return prog, default_config_files, default_config_dirs @staticmethod def _make_config_options(default_config_files, default_config_dirs): return [ _ConfigFileOpt('config-file', default=default_config_files, metavar='PATH', help=('Path to a config file to use. Multiple ' 'config files can be specified, with values ' 'in later files taking precedence. Defaults ' 'to %(default)s.')), _ConfigDirOpt('config-dir', metavar='DIR', default=default_config_dirs, help='Path to a config directory to pull `*.conf` ' 'files from. This file set is sorted, so as to ' 'provide a predictable parse order if ' 'individual options are over-ridden. The set ' 'is parsed after the file(s) specified via ' 'previous --config-file, arguments hence ' 'over-ridden options in the directory take ' 'precedence.'), ] def _setup(self, project, prog, version, usage, default_config_files, default_config_dirs): """Initialize a ConfigOpts object for option parsing.""" self._config_opts = self._make_config_options(default_config_files, default_config_dirs) self.register_cli_opts(self._config_opts) self.project = project self.prog = prog self.version = version self.usage = usage self.default_config_files = default_config_files self.default_config_dirs = default_config_dirs def __clear_cache(f): @functools.wraps(f) def __inner(self, *args, **kwargs): if kwargs.pop('clear_cache', True): result = f(self, *args, **kwargs) self.__cache.clear() return result else: return f(self, *args, **kwargs) return __inner def __call__(self, args=None, project=None, prog=None, version=None, usage=None, default_config_files=None, default_config_dirs=None, validate_default_values=False, description=None, epilog=None): """Parse command line arguments and config files. Calling a ConfigOpts object causes the supplied command line arguments and config files to be parsed, causing opt values to be made available as attributes of the object. The object may be called multiple times, each time causing the previous set of values to be overwritten. Automatically registers the --config-file option with either a supplied list of default config files, or a list from find_config_files(). If the --config-dir option is set, any *.conf files from this directory are pulled in, after all the file(s) specified by the --config-file option. :param args: command line arguments (defaults to sys.argv[1:]) :param project: the toplevel project name, used to locate config files :param prog: the name of the program (defaults to sys.argv[0] basename, without extension .py) :param version: the program version (for --version) :param usage: a usage string (%prog will be expanded) :param description: A description of what the program does :param epilog: Text following the argument descriptions :param default_config_files: config files to use by default :param default_config_dirs: config dirs to use by default :param validate_default_values: whether to validate the default values :raises: SystemExit, ConfigFilesNotFoundError, ConfigFileParseError, ConfigFilesPermissionDeniedError, RequiredOptError, DuplicateOptError """ self.clear() self._validate_default_values = validate_default_values prog, default_config_files, default_config_dirs = self._pre_setup( project, prog, version, usage, description, epilog, default_config_files, default_config_dirs) self._setup(project, prog, version, usage, default_config_files, default_config_dirs) self._namespace = self._parse_cli_opts(args if args is not None else sys.argv[1:]) if self._namespace._files_not_found: raise ConfigFilesNotFoundError(self._namespace._files_not_found) if self._namespace._files_permission_denied: raise ConfigFilesPermissionDeniedError( self._namespace._files_permission_denied) self._check_required_opts() def __getattr__(self, name): """Look up an option value and perform string substitution. :param name: the opt name (or 'dest', more precisely) :returns: the option value (after string substitution) or a GroupAttr :raises: ValueError or NoSuchOptError """ try: return self._get(name) except ValueError: raise except Exception: raise NoSuchOptError(name) def __getitem__(self, key): """Look up an option value and perform string substitution.""" return self.__getattr__(key) def __contains__(self, key): """Return True if key is the name of a registered opt or group.""" return key in self._opts or key in self._groups def __iter__(self): """Iterate over all registered opt and group names.""" for key in itertools.chain(self._opts.keys(), self._groups.keys()): yield key def __len__(self): """Return the number of options and option groups.""" return len(self._opts) + len(self._groups) def reset(self): """Clear the object state and unset overrides and defaults.""" self._unset_defaults_and_overrides() self.clear() @__clear_cache def clear(self): """Reset the state of the object to before options were registered. This method removes all registered options and discards the data from the command line and configuration files. Any subparsers added using the add_cli_subparsers() will also be removed as a side-effect of this method. """ self._args = None self._oparser = None self._namespace = None self._mutable_ns = None # Keep _mutate_hooks self._validate_default_values = False self.unregister_opts(self._config_opts) for group in self._groups.values(): group._clear() def _add_cli_opt(self, opt, group): if {'opt': opt, 'group': group} in self._cli_opts: return if opt.positional: self._cli_opts.append({'opt': opt, 'group': group}) else: self._cli_opts.appendleft({'opt': opt, 'group': group}) def _track_deprecated_opts(self, opt, group=None): if hasattr(opt, 'deprecated_opts'): for dep_opt in opt.deprecated_opts: dep_group = dep_opt.group or 'DEFAULT' dep_dest = dep_opt.name if dep_dest: dep_dest = dep_dest.replace('-', '_') if dep_group not in self._deprecated_opts: self._deprecated_opts[dep_group] = { dep_dest: { 'opt': opt, 'group': group } } else: self._deprecated_opts[dep_group][dep_dest] = { 'opt': opt, 'group': group } @__clear_cache def register_opt(self, opt, group=None, cli=False): """Register an option schema. Registering an option schema makes any option value which is previously or subsequently parsed from the command line or config files available as an attribute of this object. :param opt: an instance of an Opt sub-class :param group: an optional OptGroup object or group name :param cli: whether this is a CLI option :return: False if the opt was already registered, True otherwise :raises: DuplicateOptError """ if group is not None: group = self._get_group(group, autocreate=True) if cli: self._add_cli_opt(opt, group) self._track_deprecated_opts(opt, group=group) return group._register_opt(opt, cli) # NOTE(gcb) We can't use some names which are same with attributes of # Opts in default group. They includes project, prog, version, usage, # default_config_files and default_config_dirs. if group is None: if opt.name in self.disallow_names: raise ValueError('Name %s was reserved for oslo.config.' % opt.name) if cli: self._add_cli_opt(opt, None) if _is_opt_registered(self._opts, opt): return False self._opts[opt.dest] = {'opt': opt, 'cli': cli} self._track_deprecated_opts(opt) return True @__clear_cache def register_opts(self, opts, group=None): """Register multiple option schemas at once.""" for opt in opts: self.register_opt(opt, group, clear_cache=False) @__clear_cache def register_cli_opt(self, opt, group=None): """Register a CLI option schema. CLI option schemas must be registered before the command line and config files are parsed. This is to ensure that all CLI options are shown in --help and option validation works as expected. :param opt: an instance of an Opt sub-class :param group: an optional OptGroup object or group name :return: False if the opt was already registered, True otherwise :raises: DuplicateOptError, ArgsAlreadyParsedError """ if self._args is not None: raise ArgsAlreadyParsedError("cannot register CLI option") return self.register_opt(opt, group, cli=True, clear_cache=False) @__clear_cache def register_cli_opts(self, opts, group=None): """Register multiple CLI option schemas at once.""" for opt in opts: self.register_cli_opt(opt, group, clear_cache=False) def register_group(self, group): """Register an option group. An option group must be registered before options can be registered with the group. :param group: an OptGroup object """ if group.name in self._groups: return self._groups[group.name] = copy.copy(group) @__clear_cache def unregister_opt(self, opt, group=None): """Unregister an option. :param opt: an Opt object :param group: an optional OptGroup object or group name :raises: ArgsAlreadyParsedError, NoSuchGroupError """ if self._args is not None: raise ArgsAlreadyParsedError("reset before unregistering options") remitem = None for item in self._cli_opts: if (item['opt'].dest == opt.dest and (group is None or self._get_group(group).name == item['group'].name)): remitem = item break if remitem is not None: self._cli_opts.remove(remitem) if group is not None: self._get_group(group)._unregister_opt(opt) elif opt.dest in self._opts: del self._opts[opt.dest] @__clear_cache def unregister_opts(self, opts, group=None): """Unregister multiple CLI option schemas at once.""" for opt in opts: self.unregister_opt(opt, group, clear_cache=False) def import_opt(self, name, module_str, group=None): """Import an option definition from a module. Import a module and check that a given option is registered. This is intended for use with global configuration objects like cfg.CONF where modules commonly register options with CONF at module load time. If one module requires an option defined by another module it can use this method to explicitly declare the dependency. :param name: the name/dest of the opt :param module_str: the name of a module to import :param group: an option OptGroup object or group name :raises: NoSuchOptError, NoSuchGroupError """ __import__(module_str) self._get_opt_info(name, group) def import_group(self, group, module_str): """Import an option group from a module. Import a module and check that a given option group is registered. This is intended for use with global configuration objects like cfg.CONF where modules commonly register options with CONF at module load time. If one module requires an option group defined by another module it can use this method to explicitly declare the dependency. :param group: an option OptGroup object or group name :param module_str: the name of a module to import :raises: ImportError, NoSuchGroupError """ __import__(module_str) self._get_group(group) @__clear_cache def set_override(self, name, override, group=None): """Override an opt value. Override the command line, config file and default values of a given option. :param name: the name/dest of the opt :param override: the override value :param group: an option OptGroup object or group name :raises: NoSuchOptError, NoSuchGroupError """ opt_info = self._get_opt_info(name, group) opt_info['override'] = self._get_enforced_type_value( opt_info['opt'], override) @__clear_cache def set_default(self, name, default, group=None): """Override an opt's default value. Override the default value of given option. A command line or config file value will still take precedence over this default. :param name: the name/dest of the opt :param default: the default value :param group: an option OptGroup object or group name :raises: NoSuchOptError, NoSuchGroupError """ opt_info = self._get_opt_info(name, group) opt_info['default'] = self._get_enforced_type_value( opt_info['opt'], default) def _get_enforced_type_value(self, opt, value): if value is None: return None return self._convert_value(value, opt) @__clear_cache def clear_override(self, name, group=None): """Clear an override an opt value. Clear a previously set override of the command line, config file and default values of a given option. :param name: the name/dest of the opt :param group: an option OptGroup object or group name :raises: NoSuchOptError, NoSuchGroupError """ opt_info = self._get_opt_info(name, group) opt_info.pop('override', None) @__clear_cache def clear_default(self, name, group=None): """Clear an override an opt's default value. Clear a previously set override of the default value of given option. :param name: the name/dest of the opt :param group: an option OptGroup object or group name :raises: NoSuchOptError, NoSuchGroupError """ opt_info = self._get_opt_info(name, group) opt_info.pop('default', None) def _all_opt_infos(self): """A generator function for iteration opt infos.""" for info in self._opts.values(): yield info, None for group in self._groups.values(): for info in group._opts.values(): yield info, group def _all_cli_opts(self): """A generator function for iterating CLI opts.""" for item in self._cli_opts: yield item['opt'], item['group'] def _unset_defaults_and_overrides(self): """Unset any default or override on all options.""" for info, group in self._all_opt_infos(): info.pop('default', None) info.pop('override', None) @property def config_dirs(self): if self._namespace is None: return [] return self._namespace._config_dirs def find_file(self, name): """Locate a file located alongside the config files. Search for a file with the supplied basename in the directories which we have already loaded config files from and other known configuration directories. The directory, if any, supplied by the config_dir option is searched first. Then the config_file option is iterated over and each of the base directories of the config_files values are searched. Failing both of these, the standard directories searched by the module level find_config_files() function is used. The first matching file is returned. :param name: the filename, for example 'policy.json' :returns: the path to a matching file, or None """ if not self._namespace: raise NotInitializedError() dirs = [] if self._namespace._config_dirs: for directory in self._namespace._config_dirs: dirs.append(_fixpath(directory)) for cf in reversed(self.config_file): dirs.append(os.path.dirname(_fixpath(cf))) dirs.extend(_get_config_dirs(self.project)) return _search_dirs(dirs, name) def log_opt_values(self, logger, lvl): """Log the value of all registered opts. It's often useful for an app to log its configuration to a log file at startup for debugging. This method dumps to the entire config state to the supplied logger at a given log level. :param logger: a logging.Logger object :param lvl: the log level (for example logging.DEBUG) arg to logger.log() """ logger.log(lvl, "*" * 80) logger.log(lvl, "Configuration options gathered from:") logger.log(lvl, "command line args: %s", self._args) logger.log(lvl, "config files: %s", hasattr(self, 'config_file') and self.config_file or []) logger.log(lvl, "=" * 80) def _sanitize(opt, value): """Obfuscate values of options declared secret.""" return value if not opt.secret else '*' * 4 for opt_name in sorted(self._opts): opt = self._get_opt_info(opt_name)['opt'] logger.log(lvl, "%-30s = %s", opt_name, _sanitize(opt, getattr(self, opt_name))) for group_name in self._groups: group_attr = self.GroupAttr(self, self._get_group(group_name)) for opt_name in sorted(self._groups[group_name]._opts): opt = self._get_opt_info(opt_name, group_name)['opt'] logger.log(lvl, "%-30s = %s", "%s.%s" % (group_name, opt_name), _sanitize(opt, getattr(group_attr, opt_name))) logger.log(lvl, "*" * 80) def print_usage(self, file=None): """Print the usage message for the current program. This method is for use after all CLI options are known registered using __call__() method. If this method is called before the __call__() is invoked, it throws NotInitializedError :param file: the File object (if None, output is on sys.stdout) :raises: NotInitializedError """ if not self._oparser: raise NotInitializedError() self._oparser.print_usage(file) def print_help(self, file=None): """Print the help message for the current program. This method is for use after all CLI options are known registered using __call__() method. If this method is called before the __call__() is invoked, it throws NotInitializedError :param file: the File object (if None, output is on sys.stdout) :raises: NotInitializedError """ if not self._oparser: raise NotInitializedError() self._oparser.print_help(file) def _get(self, name, group=None, namespace=None): if isinstance(group, OptGroup): key = (group.name, name) else: key = (group, name) if namespace is None: try: return self.__cache[key] except KeyError: # nosec: Valid control flow instruction pass value = self._do_get(name, group, namespace) self.__cache[key] = value return value def _do_get(self, name, group=None, namespace=None): """Look up an option value. :param name: the opt name (or 'dest', more precisely) :param group: an OptGroup :param namespace: the namespace object to get the option value from :returns: the option value, or a GroupAttr object :raises: NoSuchOptError, NoSuchGroupError, ConfigFileValueError, TemplateSubstitutionError """ if group is None and name in self._groups: return self.GroupAttr(self, self._get_group(name)) info = self._get_opt_info(name, group) opt = info['opt'] if isinstance(opt, SubCommandOpt): return self.SubCommandAttr(self, group, opt.dest) if 'override' in info: return self._substitute(info['override']) def convert(value): return self._convert_value( self._substitute(value, group, namespace), opt) if opt.mutable and namespace is None: namespace = self._mutable_ns if namespace is None: namespace = self._namespace if namespace is not None: group_name = group.name if group else None try: return convert(opt._get_from_namespace(namespace, group_name)) except KeyError: # nosec: Valid control flow instruction pass except ValueError as ve: raise ConfigFileValueError( "Value for option %s is not valid: %s" % (opt.name, str(ve))) if 'default' in info: return self._substitute(info['default']) if self._validate_default_values: if opt.default is not None: try: convert(opt.default) except ValueError as e: raise ConfigFileValueError( "Default value for option %s is not valid: %s" % (opt.name, str(e))) if opt.default is not None: return convert(opt.default) return None def _substitute(self, value, group=None, namespace=None): """Perform string template substitution. Substitute any template variables (for example $foo, ${bar}) in the supplied string value(s) with opt values. :param value: the string value, or list of string values :param group: the group that retrieves the option value from :param namespace: the namespace object that retrieves the option value from :returns: the substituted string(s) """ if isinstance(value, list): return [self._substitute(i, group=group, namespace=namespace) for i in value] elif isinstance(value, str): # Treat a backslash followed by the dollar sign "\$" # the same as the string template escape "$$" as it is # a bit more natural for users if r'\$' in value: value = value.replace(r'\$', '$$') tmpl = self.Template(value) ret = tmpl.safe_substitute( self.StrSubWrapper(self, group=group, namespace=namespace)) return ret elif isinstance(value, dict): # Substitute template variables in both key and value return {self._substitute(key, group=group, namespace=namespace): self._substitute(val, group=group, namespace=namespace) for key, val in value.items()} else: return value class Template(string.Template): idpattern = r'[_a-z][\._a-z0-9]*' def _convert_value(self, value, opt): """Perform value type conversion. Converts values using option's type. Handles cases when value is actually a list of values (for example for multi opts). :param value: the string value, or list of string values :param opt: option definition (instance of Opt class or its subclasses) :returns: converted value """ if opt.multi: return [opt.type(v) for v in value] else: return opt.type(value) def _get_group(self, group_or_name, autocreate=False): """Looks up a OptGroup object. Helper function to return an OptGroup given a parameter which can either be the group's name or an OptGroup object. The OptGroup object returned is from the internal dict of OptGroup objects, which will be a copy of any OptGroup object that users of the API have access to. If autocreate is True, the group will be created if it's not found. If group is an instance of OptGroup, that same instance will be registered, otherwise a new instance of OptGroup will be created. :param group_or_name: the group's name or the OptGroup object itself :param autocreate: whether to auto-create the group if it's not found :raises: NoSuchGroupError """ group = group_or_name if isinstance(group_or_name, OptGroup) else None group_name = group.name if group else group_or_name if group_name not in self._groups: if not autocreate: raise NoSuchGroupError(group_name) self.register_group(group or OptGroup(name=group_name)) return self._groups[group_name] def _find_deprecated_opts(self, opt_name, group=None): real_opt_name = None real_group_name = None group_name = group or 'DEFAULT' if hasattr(group_name, 'name'): group_name = group_name.name dep_group = self._deprecated_opts.get(group_name) if dep_group: real_opt_dict = dep_group.get(opt_name) if real_opt_dict: real_opt_name = real_opt_dict['opt'].name if real_opt_dict['group']: real_group_name = real_opt_dict['group'].name return real_opt_name, real_group_name def _get_opt_info(self, opt_name, group=None): """Return the (opt, override, default) dict for an opt. :param opt_name: an opt name/dest :param group: an optional group name or OptGroup object :raises: NoSuchOptError, NoSuchGroupError """ if group is None: opts = self._opts else: group = self._get_group(group) opts = group._opts if opt_name not in opts: real_opt_name, real_group_name = self._find_deprecated_opts( opt_name, group=group) if not real_opt_name: raise NoSuchOptError(opt_name, group) log_real_group_name = real_group_name or 'DEFAULT' dep_message = ('Config option %(dep_group)s.%(dep_option)s ' ' is deprecated. Use option %(group)s.' '%(option)s instead.') LOG.warning(dep_message, {'dep_option': opt_name, 'dep_group': group, 'option': real_opt_name, 'group': log_real_group_name}) opt_name = real_opt_name if real_group_name: group = self._get_group(real_group_name) opts = group._opts return opts[opt_name] def _check_required_opts(self, namespace=None): """Check that all opts marked as required have values specified. :param namespace: the namespace object be checked the required options :raises: RequiredOptError """ for info, group in self._all_opt_infos(): opt = info['opt'] if opt.required: if 'default' in info or 'override' in info: continue if self._get(opt.dest, group, namespace) is None: raise RequiredOptError(opt.name, group) def _parse_cli_opts(self, args): """Parse command line options. Initializes the command line option parser and parses the supplied command line arguments. :param args: the command line arguments :returns: a _Namespace object containing the parsed option values :raises: SystemExit, DuplicateOptError ConfigFileParseError, ConfigFileValueError """ self._args = args for opt, group in self._all_cli_opts(): opt._add_to_cli(self._oparser, group) return self._parse_config_files() def _parse_config_files(self): """Parse configure files options. :raises: SystemExit, ConfigFilesNotFoundError, ConfigFileParseError, ConfigFilesPermissionDeniedError, RequiredOptError, DuplicateOptError """ namespace = _Namespace(self) # handle --config-file args or the default_config_files for arg in self._args: if arg == '--config-file' or arg.startswith('--config-file='): break else: for config_file in self.default_config_files: ConfigParser._parse_file(config_file, namespace) # handle --config-dir args or the default_config_dirs for arg in self._args: if arg == '--config-dir' or arg.startswith('--config-dir='): break else: for config_dir in self.default_config_dirs: # for the default config-dir directories we just continue # if the directories do not exist. This is different to the # case where --config-dir is given on the command line. if not os.path.exists(config_dir): continue config_dir_glob = os.path.join(config_dir, '*.conf') for config_file in sorted(glob.glob(config_dir_glob)): ConfigParser._parse_file(config_file, namespace) self._oparser.parse_args(self._args, namespace) self._validate_cli_options(namespace) return namespace def _validate_cli_options(self, namespace): for opt, group in sorted(self._all_cli_opts(), key=lambda x: x[0].name): group_name = group.name if group else None try: value = opt._get_from_namespace(namespace, group_name) except KeyError: continue value = self._substitute(value, group=group, namespace=namespace) try: self._convert_value(value, opt) except ValueError: sys.stderr.write("argument --%s: Invalid %s value: %s\n" % ( opt.dest, repr(opt.type), value)) raise SystemExit def _reload_config_files(self): namespace = self._parse_config_files() if namespace._files_not_found: raise ConfigFilesNotFoundError(namespace._files_not_found) if namespace._files_permission_denied: raise ConfigFilesPermissionDeniedError( namespace._files_permission_denied) self._check_required_opts(namespace) return namespace @__clear_cache def reload_config_files(self): """Reload configure files and parse all options :return: False if reload configure files failed or else return True """ try: namespace = self._reload_config_files() except SystemExit as exc: LOG.warning("Caught SystemExit while reloading configure " "files with exit code: %d", exc.code) return False except Error as err: LOG.warning("Caught Error while reloading configure files: " "%s", err) return False else: self._namespace = namespace return True def register_mutate_hook(self, hook): """Registers a hook to be called by mutate_config_files. :param hook: a function accepting this ConfigOpts object and a dict of config mutations, as returned by mutate_config_files. :return: None """ self._mutate_hooks.add(hook) def mutate_config_files(self): """Reload configure files and parse all options. Only options marked as 'mutable' will appear to change. Hooks are called in a NON-DETERMINISTIC ORDER. Do not expect hooks to be called in the same order as they were added. :return: {(None or 'group', 'optname'): (old_value, new_value), ... } :raises: Error if reloading fails """ self.__cache.clear() old_mutate_ns = self._mutable_ns or self._namespace self._mutable_ns = self._reload_config_files() self._warn_immutability() fresh = self._diff_ns(old_mutate_ns, self._mutable_ns) def key_fn(item): # Py3 won't sort heterogeneous types. Sort None as TAB which has a # very low ASCII value. (groupname, optname) = item[0] return item[0] if groupname else ('\t', optname) sorted_fresh = sorted(fresh.items(), key=key_fn) for (groupname, optname), (old, new) in sorted_fresh: groupname = groupname if groupname else 'DEFAULT' LOG.info("Option %(group)s.%(option)s changed from " "[%(old_val)s] to [%(new_val)s]", {'group': groupname, 'option': optname, 'old_val': old, 'new_val': new}) for hook in self._mutate_hooks: hook(self, fresh) return fresh def _warn_immutability(self): """Check immutable opts have not changed. _do_get won't return the new values but presumably someone changed the config file expecting them to change so we should warn them they won't. """ for info, group in self._all_opt_infos(): opt = info['opt'] if opt.mutable: continue groupname = group.name if group else 'DEFAULT' try: old = opt._get_from_namespace(self._namespace, groupname) except KeyError: old = None try: new = opt._get_from_namespace(self._mutable_ns, groupname) except KeyError: new = None if old != new: LOG.warning("Ignoring change to immutable option " "%(group)s.%(option)s", {"group": groupname, "option": opt.name}) def _diff_ns(self, old_ns, new_ns): """Compare mutable option values between two namespaces. This can be used to only reconfigure stateful sessions when necessary. :return {(None or 'group', 'optname'): (old_value, new_value), ... } """ diff = {} for info, group in self._all_opt_infos(): opt = info['opt'] if not opt.mutable: continue groupname = group.name if group else None try: old = opt._get_from_namespace(old_ns, groupname) except KeyError: old = None try: new = opt._get_from_namespace(new_ns, groupname) except KeyError: new = None if old != new: diff[(groupname, opt.name)] = (old, new) return diff def list_all_sections(self): """List all sections from the configuration. Returns a sorted list of all section names found in the configuration files, whether declared beforehand or not. """ s = set([]) if self._mutable_ns: s |= set(self._mutable_ns._sections()) if self._namespace: s |= set(self._namespace._sections()) return sorted(s) class GroupAttr(collections.Mapping): """Helper class. Represents the option values of a group as a mapping and attributes. """ def __init__(self, conf, group): """Construct a GroupAttr object. :param conf: a ConfigOpts object :param group: an OptGroup object """ self._conf = conf self._group = group def __getattr__(self, name): """Look up an option value and perform template substitution.""" return self._conf._get(name, self._group) def __getitem__(self, key): """Look up an option value and perform string substitution.""" return self.__getattr__(key) def __contains__(self, key): """Return True if key is the name of a registered opt or group.""" return key in self._group._opts def __iter__(self): """Iterate over all registered opt and group names.""" for key in self._group._opts.keys(): yield key def __len__(self): """Return the number of options and option groups.""" return len(self._group._opts) class SubCommandAttr(object): """Helper class. Represents the name and arguments of an argparse sub-parser. """ def __init__(self, conf, group, dest): """Construct a SubCommandAttr object. :param conf: a ConfigOpts object :param group: an OptGroup object :param dest: the name of the sub-parser """ self._conf = conf self._group = group self._dest = dest def __getattr__(self, name): """Look up a sub-parser name or argument value.""" if name == 'name': name = self._dest if self._group is not None: name = self._group.name + '_' + name return getattr(self._conf._namespace, name) if name in self._conf: raise DuplicateOptError(name) try: return getattr(self._conf._namespace, name) except AttributeError: raise NoSuchOptError(name) class StrSubWrapper(object): """Helper class. Exposes opt values as a dict for string substitution. """ def __init__(self, conf, group=None, namespace=None): """Construct a StrSubWrapper object. :param conf: a ConfigOpts object :param group: an OptGroup object :param namespace: the namespace object that retrieves the option value from """ self.conf = conf self.group = group self.namespace = namespace def __getitem__(self, key): """Look up an opt value from the ConfigOpts object. :param key: an opt name :returns: an opt value """ try: group_name, option = key.split(".", 1) except ValueError: group = self.group option = key else: group = OptGroup(name=group_name) try: value = self.conf._get(option, group=group, namespace=self.namespace) except NoSuchOptError: value = self.conf._get(key, namespace=self.namespace) if isinstance(value, self.conf.GroupAttr): raise TemplateSubstitutionError( 'substituting group %s not supported' % key) if value is None: return '' return value CONF = ConfigOpts() oslo.config-5.2.0/oslo_config/_i18n.py0000666000175100017510000000227013224676052017565 0ustar zuulzuul00000000000000# Copyright 2016 OpenStack Foundation # # 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. """oslo.i18n integration module. See https://docs.openstack.org/oslo.i18n/latest/user/index.html. """ import oslo_i18n DOMAIN = "oslo.config" _translators = oslo_i18n.TranslatorFactory(domain=DOMAIN) # The primary translation function using the well-known name "_" _ = _translators.primary # The contextual translation function using the name "_C" # requires oslo.i18n >=2.1.0 _C = _translators.contextual_form # The plural translation function using the name "_P" # requires oslo.i18n >=2.1.0 _P = _translators.plural_form def get_available_languages(): return oslo_i18n.get_available_languages(DOMAIN) oslo.config-5.2.0/oslo_config/types.py0000666000175100017510000007173513224676105020026 0ustar zuulzuul00000000000000# Copyright 2013 Mirantis, Inc. # # 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. """Type conversion and validation classes for configuration options. Use these classes as values for the `type` argument to :class:`oslo_config.cfg.Opt` and its subclasses. .. versionadded:: 1.3 """ import collections import operator import re import warnings import abc import netaddr import rfc3986 import six from six.moves import range as compat_range @six.add_metaclass(abc.ABCMeta) class ConfigType(object): def __init__(self, type_name='unknown type'): self.type_name = type_name NONE_DEFAULT = '' def format_defaults(self, default, sample_default=None): """Return a list of formatted default values. """ if sample_default is not None: default_str = sample_default elif default is None: default_str = self.NONE_DEFAULT else: default_str = self._formatter(default) return [default_str] def quote_trailing_and_leading_space(self, str_val): if not isinstance(str_val, six.string_types): warnings.warn('converting \'%s\' to a string' % str_val) str_val = six.text_type(str_val) if str_val.strip() != str_val: return '"%s"' % str_val return str_val @abc.abstractmethod def _formatter(self, value): pass class String(ConfigType): """String type. String values do not get transformed and are returned as str objects. :param choices: Optional sequence of either valid values or tuples of valid values with descriptions. Mutually exclusive with 'regex'. :param quotes: If True and string is enclosed with single or double quotes, will strip those quotes. Will signal error if string have quote at the beginning and no quote at the end. Turned off by default. Useful if used with container types like List. :param regex: Optional regular expression (string or compiled regex) that the value must match on an unanchored search. Mutually exclusive with 'choices'. :param ignore_case: If True case differences (uppercase vs. lowercase) between 'choices' or 'regex' will be ignored; defaults to False. :param max_length: Optional integer. If a positive value is specified, a maximum length of an option value must be less than or equal to this parameter. Otherwise no length check will be done. :param type_name: Type name to be used in the sample config file. .. versionchanged:: 2.1 Added *regex* parameter. .. versionchanged:: 2.5 Added *ignore_case* parameter. .. versionchanged:: 2.7 Added *max_length* parameter. Added *type_name* parameter. .. versionchanged:: 5.2 The *choices* parameter will now accept a sequence of tuples, where each tuple is of form (*choice*, *description*) """ def __init__(self, choices=None, quotes=False, regex=None, ignore_case=False, max_length=None, type_name='string value'): super(String, self).__init__(type_name=type_name) if choices and regex: raise ValueError("'choices' and 'regex' cannot both be specified") self.ignore_case = ignore_case self.quotes = quotes self.max_length = max_length or 0 if choices is not None: if not all(isinstance(choice, tuple) for choice in choices): choices = [(choice, None) for choice in choices] self.choices = collections.OrderedDict(choices) else: self.choices = None self.lower_case_choices = None if self.choices is not None and self.ignore_case: self.lower_case_choices = [c.lower() for c in self.choices] self.regex = regex if self.regex is not None: re_flags = re.IGNORECASE if self.ignore_case else 0 # Check if regex is a string or an already compiled regex if isinstance(regex, six.string_types): self.regex = re.compile(regex, re_flags) else: self.regex = re.compile(regex.pattern, re_flags | regex.flags) def __call__(self, value): value = str(value) if self.quotes and value: if value[0] in "\"'": if value[-1] != value[0]: raise ValueError('Non-closed quote: %s' % value) value = value[1:-1] if self.max_length > 0 and len(value) > self.max_length: raise ValueError("Value '%s' exceeds maximum length %d" % (value, self.max_length)) if self.regex and not self.regex.search(value): raise ValueError("Value %r doesn't match regex %r" % (value, self.regex.pattern)) if self.choices is None: return value # Check for case insensitive processed_value, choices = ((value.lower(), self.lower_case_choices) if self.ignore_case else (value, self.choices.keys())) if processed_value in choices: return value raise ValueError( 'Valid values are [%s], but found %s' % ( ', '.join([str(v) for v in self.choices]), repr(value))) def __repr__(self): details = [] if self.choices is not None: details.append("choices={!r}".format(list(self.choices.keys()))) if self.regex: details.append("regex=%r" % self.regex.pattern) if details: return "String(%s)" % ",".join(details) return 'String' def __eq__(self, other): return ( (self.__class__ == other.__class__) and (self.quotes == other.quotes) and (self.regex == other.regex) and (set([x for x in self.choices or []]) == set([x for x in other.choices or []]) if self.choices and other.choices else self.choices == other.choices) ) def _formatter(self, value): return self.quote_trailing_and_leading_space(value) class MultiString(String): """Multi-valued string.""" def __init__(self, type_name='multi valued'): super(MultiString, self).__init__(type_name=type_name) NONE_DEFAULT = [''] def format_defaults(self, default, sample_default=None): """Return a list of formatted default values. """ if sample_default is not None: default_list = self._formatter(sample_default) elif not default: default_list = self.NONE_DEFAULT else: default_list = self._formatter(default) return default_list def _formatter(self, value): return [self.quote_trailing_and_leading_space(v) for v in value] class Boolean(ConfigType): """Boolean type. Values are case insensitive and can be set using 1/0, yes/no, true/false or on/off. :param type_name: Type name to be used in the sample config file. .. versionchanged:: 2.7 Added *type_name* parameter. """ TRUE_VALUES = ['true', '1', 'on', 'yes'] FALSE_VALUES = ['false', '0', 'off', 'no'] def __init__(self, type_name='boolean value'): super(Boolean, self).__init__(type_name=type_name) def __call__(self, value): if isinstance(value, bool): return value s = value.lower() if s in self.TRUE_VALUES: return True elif s in self.FALSE_VALUES: return False else: raise ValueError('Unexpected boolean value %r' % value) def __repr__(self): return 'Boolean' def __eq__(self, other): return self.__class__ == other.__class__ def _formatter(self, value): return str(value).lower() class Number(ConfigType): """Number class, base for Integer and Float. :param num_type: the type of number used for casting (i.e int, float) :param type_name: Type name to be used in the sample config file. :param min: Optional check that value is greater than or equal to min. :param max: Optional check that value is less than or equal to max. :param choices: Optional sequence of either valid values or tuples of valid values with descriptions. .. versionadded:: 3.14 .. versionchanged:: 5.2 The *choices* parameter will now accept a sequence of tuples, where each tuple is of form (*choice*, *description*) """ def __init__(self, num_type, type_name, min=None, max=None, choices=None): super(Number, self).__init__(type_name=type_name) if min is not None and max is not None and max < min: raise ValueError('Max value is less than min value') if choices is not None: if not all(isinstance(choice, tuple) for choice in choices): choices = [(choice, None) for choice in choices] self.choices = collections.OrderedDict(choices) else: self.choices = None invalid_choices = [c for c in self.choices or [] if (min is not None and min > c) or (max is not None and max < c)] if invalid_choices: raise ValueError("Choices %s are out of bounds [%s..%s]" % (invalid_choices, min, max)) self.min = min self.max = max self.num_type = num_type def __call__(self, value): if not isinstance(value, self.num_type): s = str(value).strip() if s == '': return None value = self.num_type(value) if self.choices is None: if self.min is not None and value < self.min: raise ValueError('Should be greater than or equal to %g' % self.min) if self.max is not None and value > self.max: raise ValueError('Should be less than or equal to %g' % self.max) else: if value not in self.choices: raise ValueError('Valid values are %r, but found %g' % ( self.choices, value)) return value def __repr__(self): props = [] if self.choices is not None: props.append("choices={!r}".format(list(self.choices.keys()))) else: if self.min is not None: props.append('min=%g' % self.min) if self.max is not None: props.append('max=%g' % self.max) if props: return self.__class__.__name__ + '(%s)' % ', '.join(props) return self.__class__.__name__ def __eq__(self, other): return ( (self.__class__ == other.__class__) and (self.min == other.min) and (self.max == other.max) and (set([x for x in self.choices or []]) == set([x for x in other.choices or []]) if self.choices and other.choices else self.choices == other.choices) ) def _formatter(self, value): return str(value) class Integer(Number): """Integer type. Converts value to an integer optionally doing range checking. If value is whitespace or empty string will return None. :param min: Optional check that value is greater than or equal to min. :param max: Optional check that value is less than or equal to max. :param type_name: Type name to be used in the sample config file. :param choices: Optional sequence of either valid values or tuples of valid values with descriptions. .. versionchanged:: 2.4 The class now honors zero for *min* and *max* parameters. .. versionchanged:: 2.7 Added *type_name* parameter. .. versionchanged:: 3.2 Added *choices* parameter. .. versionchanged:: 3.16 *choices* is no longer mutually exclusive with *min*/*max*. If those are supplied, all choices are verified to be within the range. .. versionchanged:: 5.2 The *choices* parameter will now accept a sequence of tuples, where each tuple is of form (*choice*, *description*) """ def __init__(self, min=None, max=None, type_name='integer value', choices=None): super(Integer, self).__init__(int, type_name, min=min, max=max, choices=choices) class Float(Number): """Float type. :param min: Optional check that value is greater than or equal to min. :param max: Optional check that value is less than or equal to max. :param type_name: Type name to be used in the sample config file. .. versionchanged:: 2.7 Added *type_name* parameter. .. versionchanged:: 3.14 Added *min* and *max* parameters. If *choices* are also supplied, they must be within the range. """ def __init__(self, min=None, max=None, type_name='floating point value'): super(Float, self).__init__(float, type_name, min=min, max=max) class Port(Integer): """Port type Represents a L4 Port. :param min: Optional check that value is greater than or equal to min. :param max: Optional check that value is less than or equal to max. :param type_name: Type name to be used in the sample config file. :param choices: Optional sequence of either valid values or tuples of valid values with descriptions. .. versionadded:: 3.16 .. versionchanged:: 5.2 The *choices* parameter will now accept a sequence of tuples, where each tuple is of form (*choice*, *description*) """ PORT_MIN = 0 PORT_MAX = 65535 def __init__(self, min=None, max=None, type_name='port', choices=None): min = self.PORT_MIN if min is None else min max = self.PORT_MAX if max is None else max if min < self.PORT_MIN: raise ValueError('Min value cannot be less than %(min)d' % {'min': self.PORT_MIN}) if max > self.PORT_MAX: raise ValueError('Max value cannot be more than %(max)d' % {'max': self.PORT_MAX}) super(Port, self).__init__(min=min, max=max, type_name=type_name, choices=choices) class List(ConfigType): """List type. Represent values of other (item) type, separated by commas. The resulting value is a list containing those values. List doesn't know if item type can also contain commas. To workaround this it tries the following: if the next part fails item validation, it appends comma and next item until validation succeeds or there is no parts left. In the later case it will signal validation error. :param item_type: type of list items :param bounds: if True, value should be inside "[" and "]" pair :param type_name: Type name to be used in the sample config file. .. versionchanged:: 2.7 Added *type_name* parameter. """ def __init__(self, item_type=None, bounds=False, type_name='list value'): super(List, self).__init__(type_name=type_name) if item_type is None: item_type = String() if not callable(item_type): raise TypeError('item_type must be callable') self.item_type = item_type self.bounds = bounds def __call__(self, value): if isinstance(value, (list, tuple)): return list(six.moves.map(self.item_type, value)) s = value.strip() if self.bounds: if not s.startswith('['): raise ValueError('Value should start with "["') if not s.endswith(']'): raise ValueError('Value should end with "]"') s = s[1:-1] if s: values = s.split(',') else: values = [] if not values: return [] result = [] while values: value = values.pop(0) while True: first_error = None try: validated_value = self.item_type(value.strip()) break except ValueError as e: if not first_error: first_error = e if len(values) == 0: raise first_error value += ',' + values.pop(0) result.append(validated_value) return result def __repr__(self): return 'List of %s' % repr(self.item_type) def __eq__(self, other): return ( (self.__class__ == other.__class__) and (self.item_type == other.item_type) ) def _formatter(self, value): if isinstance(value, six.string_types): return value if isinstance(value, list): value = [ six.text_type(v) for v in value ] return ','.join(value) class Range(ConfigType): """Range type. Represents a range of integers. A range is identified by an integer both sides of a '-' character. Negatives are allowed. A single number is also a valid range. :param min: Optional check that lower bound is greater than or equal to min. :param max: Optional check that upper bound is less than or equal to max. :param inclusive: True if the right bound is to be included in the range. :param type_name: Type name to be used in the sample config file. .. versionadded:: 3.18 """ def __init__(self, min=None, max=None, inclusive=True, type_name='range value'): super(Range, self).__init__(type_name) self.min = min self.max = max self.inclusive = inclusive def __call__(self, value): value = str(value) num = "0|-?[1-9][0-9]*" m = re.match("^(%s)(?:-(%s))?$" % (num, num), value) if not m: raise ValueError('Invalid Range: %s' % value) left = int(m.group(1)) right = int(left if m.group(2) is None else m.group(2)) if left < right: left = Integer(min=self.min)(left) right = Integer(max=self.max)(right) step = 1 else: left = Integer(max=self.max)(left) right = Integer(min=self.min)(right) step = -1 if self.inclusive: right += step return compat_range(left, right, step) def __eq__(self, other): return ( (self.__class__ == other.__class__) and (self.min == other.min) and (self.max == other.max) ) def _formatter(self, value): return value class Dict(ConfigType): """Dictionary type. Dictionary type values are key:value pairs separated by commas. The resulting value is a dictionary of these key/value pairs. Type of dictionary key is always string, but dictionary value type can be customized. :param value_type: type of values in dictionary :param bounds: if True, value should be inside "{" and "}" pair :param type_name: Type name to be used in the sample config file. .. versionchanged:: 2.7 Added *type_name* parameter. """ def __init__(self, value_type=None, bounds=False, type_name='dict value'): super(Dict, self).__init__(type_name=type_name) if value_type is None: value_type = String() if not callable(value_type): raise TypeError('value_type must be callable') self.value_type = value_type self.bounds = bounds def __call__(self, value): if isinstance(value, dict): return value result = {} s = value.strip() if self.bounds: if not s.startswith('{'): raise ValueError('Value should start with "{"') if not s.endswith('}'): raise ValueError('Value should end with "}"') s = s[1:-1] if s == '': return result pairs = s.split(',') while pairs: pair = pairs.pop(0) while True: first_error = None try: key_value = pair.split(':', 1) if len(key_value) < 2: raise ValueError('Value should be NAME:VALUE pairs ' 'separated by ","') key, value = key_value key = key.strip() value = value.strip() value = self.value_type(value) break except ValueError as e: if not first_error: first_error = e if not pairs: raise first_error pair += ',' + pairs.pop(0) if key == '': raise ValueError('Key name should not be empty') if key in result: raise ValueError('Duplicate key %s' % key) result[key] = value return result def __repr__(self): return 'Dict of %s' % repr(self.value_type) def __eq__(self, other): return ( (self.__class__ == other.__class__) and (self.value_type == other.value_type) ) def _formatter(self, value): sorted_items = sorted(value.items(), key=operator.itemgetter(0)) return ','.join(['%s:%s' % i for i in sorted_items]) class IPAddress(ConfigType): """IP address type Represents either ipv4 or ipv6. Without specifying version parameter both versions are checked :param version: defines which version should be explicitly checked (4 or 6) :param type_name: Type name to be used in the sample config file. .. versionchanged:: 2.7 Added *type_name* parameter. """ def __init__(self, version=None, type_name='IP address value'): super(IPAddress, self).__init__(type_name=type_name) version_checkers = { None: self._check_both_versions, 4: self._check_ipv4, 6: self._check_ipv6 } self.version_checker = version_checkers.get(version) if self.version_checker is None: raise TypeError("%s is not a valid IP version." % version) def __call__(self, value): value = str(value) if not value: raise ValueError("IP address cannot be an empty string") self.version_checker(value) return value def __repr__(self): return "IPAddress" def __eq__(self, other): return self.__class__ == other.__class__ def _check_ipv4(self, address): if not netaddr.valid_ipv4(address, netaddr.core.INET_PTON): raise ValueError("%s is not an IPv4 address" % address) def _check_ipv6(self, address): if not netaddr.valid_ipv6(address, netaddr.core.INET_PTON): raise ValueError("%s is not an IPv6 address" % address) def _check_both_versions(self, address): if not (netaddr.valid_ipv4(address, netaddr.core.INET_PTON) or netaddr.valid_ipv6(address, netaddr.core.INET_PTON)): raise ValueError("%s is not IPv4 or IPv6 address" % address) def _formatter(self, value): return value class Hostname(ConfigType): """Host domain name type. A hostname refers to a valid DNS or hostname. It must not be longer than 253 characters, have a segment greater than 63 characters, nor start or end with a hyphen. :param type_name: Type name to be used in the sample config file. """ def __init__(self, type_name='hostname value'): super(Hostname, self).__init__(type_name=type_name) def __call__(self, value): """Check hostname is valid. Ensures that each segment - Contains at least one character and a maximum of 63 characters - Consists only of allowed characters: letters (A-Z and a-z), digits (0-9), and hyphen (-) - Ensures that the final segment (representing the top level domain name) contains at least one non-numeric character - Does not begin or end with a hyphen - maximum total length of 253 characters For more details , please see: http://tools.ietf.org/html/rfc1035, https://www.ietf.org/rfc/rfc1912, and https://tools.ietf.org/html/rfc1123 """ if len(value) == 0: raise ValueError("Cannot have an empty hostname") if len(value) > 253: raise ValueError("hostname is greater than 253 characters: %s" % value) if value.endswith("."): value = value[:-1] allowed = re.compile("(?!-)[A-Z0-9-]{1,63}(? self.max_length: raise ValueError("Value '%s' exceeds maximum length %d" % (value, self.max_length)) if self.schemes: scheme = rfc3986.uri_reference(value).scheme if scheme not in self.schemes: raise ValueError("URI scheme '%s' not in %s" % (scheme, self.schemes)) self.value = value return value def __repr__(self): return 'URI' def __eq__(self, other): return ( (self.__class__ == other.__class__) and (self.value == other.value) ) def _formatter(self, value): return value oslo.config-5.2.0/oslo_config/__init__.py0000666000175100017510000000000013224676052020373 0ustar zuulzuul00000000000000oslo.config-5.2.0/oslo_config/cfgfilter.py0000666000175100017510000003165313224676052020623 0ustar zuulzuul00000000000000# Copyright 2014 Red Hat, Inc. # # 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. r""" There are three use cases for the ConfigFilter class: 1. Help enforce that a given module does not access options registered by another module, without first declaring those cross-module dependencies using import_opt(). 2. Prevent private configuration opts from being visible to modules other than the one which registered it. 3. Limit the options on a Cfg object that can be accessed. .. versionadded:: 1.4 Cross-Module Option Dependencies -------------------------------- When using the global cfg.CONF object, it is quite common for a module to require the existence of configuration options registered by other modules. For example, if module 'foo' registers the 'blaa' option and the module 'bar' uses the 'blaa' option then 'bar' might do:: import foo print(CONF.blaa) However, it's completely non-obvious why foo is being imported (is it unused, can we remove the import) and where the 'blaa' option comes from. The CONF.import_opt() method allows such a dependency to be explicitly declared:: CONF.import_opt('blaa', 'foo') print(CONF.blaa) However, import_opt() has a weakness - if 'bar' imports 'foo' using the import builtin and doesn't use import_opt() to import 'blaa', then 'blaa' can still be used without problems. Similarly, where multiple options are registered a module imported via importopt(), a lazy programmer can get away with only declaring a dependency on a single option. The ConfigFilter class provides a way to ensure that options are not available unless they have been registered in the module or imported using import_opt() for example with:: CONF = ConfigFilter(cfg.CONF) CONF.import_opt('blaa', 'foo') print(CONF.blaa) no other options other than 'blaa' are available via CONF. Private Configuration Options ----------------------------- Libraries which register configuration options typically do not want users of the library API to access those configuration options. If API users do access private configuration options, those users will be disrupted if and when a configuration option is renamed. In other words, one does not typically wish for the name of the private config options to be part of the public API. The ConfigFilter class provides a way for a library to register options such that they are not visible via the ConfigOpts instance which the API user supplies to the library. For example:: from __future__ import print_function from oslo_config.cfg import * from oslo_config.cfgfilter import * class Widget(object): def __init__(self, conf): self.conf = conf self._private_conf = ConfigFilter(self.conf) self._private_conf.register_opt(StrOpt('foo')) @property def foo(self): return self._private_conf.foo conf = ConfigOpts() widget = Widget(conf) print(widget.foo) print(conf.foo) # raises NoSuchOptError Limited Configuration Options ----------------------------- It may be required that when passing a CONF object to other functions we want to filter that the receiving code is only able to access a restricted subset of the options that are available on the CONF object. This is essentially a more general case of the Private Configuration Options and Cross-Module Options whereby we expose an option that is already present on the underlying CONF object without providing any means to load it if not present. So given a CONF object with options defined:: CONF.register_opt(StrOpt('foo')) CONF.register_opt(StrOpt('bar')) we can expose options such that only those options are present:: restricted_conf = CfgFilter(CONF) restricted_conf.expose_opt('foo') print(restricted_conf.foo) print(restricted_conf.bar) # raises NoSuchOptError """ import collections import itertools from oslo_config import cfg class CliOptRegisteredError(cfg.Error): """Raised when registering cli opt not in original ConfigOpts. .. versionadded:: 1.12 """ def __str__(self): ret = "Cannot register a cli option that was not present in the" \ " original ConfigOpts." if self.msg: ret += ": " + self.msg return ret class ConfigFilter(collections.Mapping): """A helper class which wraps a ConfigOpts object. ConfigFilter enforces the explicit declaration of dependencies on external options and allows private options which are not registered with the wrapped Configopts object. """ def __init__(self, conf): """Construct a ConfigFilter object. :param conf: a ConfigOpts object """ self._conf = conf self._fconf = cfg.ConfigOpts() self._sync() self._imported_opts = set() self._imported_groups = dict() def _sync(self): if self._fconf._namespace is not self._conf._namespace: self._fconf.clear() self._fconf._namespace = self._conf._namespace self._fconf._args = self._conf._args def __getattr__(self, name): """Look up an option value. :param name: the opt name (or 'dest', more precisely) :returns: the option value (after string subsititution) or a GroupAttr :raises: NoSuchOptError,ConfigFileValueError,TemplateSubstitutionError """ if name in self._imported_groups: return self._imported_groups[name] elif name in self._imported_opts: return getattr(self._conf, name) else: self._sync() return getattr(self._fconf, name) def __getitem__(self, key): """Look up an option value.""" return getattr(self, key) def __contains__(self, key): """Return True if key is the name of a registered opt or group.""" return (key in self._fconf or key in self._imported_opts or key in self._imported_groups) def __iter__(self): """Iterate over all registered opt and group names.""" return itertools.chain(self._fconf.keys(), self._imported_opts, self._imported_groups.keys()) def __len__(self): """Return the number of options and option groups.""" return (len(self._fconf) + len(self._imported_opts) + len(self._imported_groups)) @staticmethod def _already_registered(conf, opt, group=None): group_name = group.name if isinstance(group, cfg.OptGroup) else group return ((group_name is None and opt.dest in conf) or (group_name is not None and group_name in conf and opt.dest in conf[group_name])) def register_opt(self, opt, group=None): """Register an option schema. :param opt: an instance of an Opt sub-class :param group: an optional OptGroup object or group name :return: False if the opt was already registered, True otherwise :raises: DuplicateOptError """ if self._already_registered(self._conf, opt, group): # Raises DuplicateError if there is another opt with the same name ret = self._conf.register_opt(opt, group) self._import_opt(opt.dest, group) return ret else: return self._fconf.register_opt(opt, group) def register_opts(self, opts, group=None): """Register multiple option schemas at once.""" for opt in opts: self.register_opt(opt, group) def register_cli_opt(self, opt, group=None): """Register a CLI option schema. :param opt: an instance of an Opt sub-class :param group: an optional OptGroup object or group name :return: False if the opt was already register, True otherwise :raises: DuplicateOptError, ArgsAlreadyParsedError """ if self._already_registered(self._conf, opt, group): # Raises DuplicateError if there is another opt with the same name ret = self._conf.register_opt( opt, group, cli=True, clear_cache=False) self._import_opt(opt.dest, group) return ret else: raise CliOptRegisteredError( "Opt '{0}' cannot be registered.".format(opt.dest)) def register_cli_opts(self, opts, group=None): """Register multiple CLI option schemas at once.""" for opt in opts: self.register_cli_opt(opt, group) def register_group(self, group): """Register an option group. :param group: an OptGroup object """ self._fconf.register_group(group) def import_opt(self, opt_name, module_str, group=None): """Import an option definition from a module. :param name: the name/dest of the opt :param module_str: the name of a module to import :param group: an option OptGroup object or group name :raises: NoSuchOptError, NoSuchGroupError """ self._conf.import_opt(opt_name, module_str, group) self._import_opt(opt_name, group) def import_group(self, group, module_str): """Import an option group from a module. Note that this allows access to all options registered with the group whether or not those options were registered by the given module. :param group: an option OptGroup object or group name :param module_str: the name of a module to import :raises: ImportError, NoSuchGroupError """ self._conf.import_group(group, module_str) group = self._import_group(group) group._all_opts = True def _import_opt(self, opt_name, group): if group is None: self._imported_opts.add(opt_name) return True else: group = self._import_group(group) return group._import_opt(opt_name) def _import_group(self, group_or_name): if isinstance(group_or_name, cfg.OptGroup): group_name = group_or_name.name else: group_name = group_or_name if group_name in self._imported_groups: return self._imported_groups[group_name] else: group = self.GroupAttr(self._conf, group_name) self._imported_groups[group_name] = group return group def expose_opt(self, opt_name, group=None): """Expose an option from the underlying conf object. This allows an object that has already been imported or used from the base conf object to be seen from the filter object. :param opt_name: the name/dest of the opt :param group: an option OptGroup object or group name """ self._import_opt(opt_name, group) def expose_group(self, group): """Expose all option from a group in the underlying conf object. This allows an object that has already been imported or used from the base conf object to be seen from the filter object. :param group: an option OptGroup object or group name """ group = self._import_group(group) group._all_opts = True class GroupAttr(collections.Mapping): """Helper class to wrap a group object. Represents the option values of a group as a mapping and attributes. """ def __init__(self, conf, group): """Construct a GroupAttr object. :param conf: a ConfigOpts object :param group: an OptGroup object """ self._conf = conf self._group = group self._imported_opts = set() self._all_opts = False def __getattr__(self, name): """Look up an option value.""" if not self._all_opts and name not in self._imported_opts: raise cfg.NoSuchOptError(name) return getattr(self._conf[self._group], name) def __getitem__(self, key): """Look up an option value.""" return getattr(self, key) def __contains__(self, key): """Return True if key is the name of a registered opt or group.""" return key in self._imported_opts def __iter__(self): """Iterate over all registered opt and group names.""" for key in self._imported_opts: yield key def __len__(self): """Return the number of options and option groups.""" return len(self._imported_opts) def _import_opt(self, opt_name): self._imported_opts.add(opt_name) oslo.config-5.2.0/oslo_config/iniparser.py0000666000175100017510000001015713224676052020646 0ustar zuulzuul00000000000000# Copyright 2012 OpenStack Foundation # # 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. class ParseError(Exception): def __init__(self, message, lineno, line): self.msg = message self.line = line self.lineno = lineno def __str__(self): return 'at line %d, %s: %r' % (self.lineno, self.msg, self.line) class BaseParser(object): lineno = 0 parse_exc = ParseError def _assignment(self, key, value): self.assignment(key, value) return None, [] def _get_section(self, line): if not line.endswith(']'): return self.error_no_section_end_bracket(line) if len(line) <= 2: return self.error_no_section_name(line) return line[1:-1] def _split_key_value(self, line): colon = line.find(':') equal = line.find('=') if colon < 0 and equal < 0: return self.error_invalid_assignment(line) if colon < 0 or (equal >= 0 and equal < colon): key, value = line[:equal], line[equal + 1:] else: key, value = line[:colon], line[colon + 1:] value = value.strip() if value and value[0] == value[-1] and value.startswith(("\"", "'")): value = value[1:-1] return key.strip(), [value] def parse(self, lineiter): key = None value = [] for line in lineiter: self.lineno += 1 line = line.rstrip() if not line: # Blank line, ends multi-line values if key: key, value = self._assignment(key, value) continue elif line.startswith((' ', '\t')): # Continuation of previous assignment if key is None: self.error_unexpected_continuation(line) else: value.append(line.lstrip()) continue if key: # Flush previous assignment, if any key, value = self._assignment(key, value) if line.startswith('['): # Section start section = self._get_section(line) if section: self.new_section(section) elif line.startswith(('#', ';')): self.comment(line[1:].lstrip()) else: key, value = self._split_key_value(line) if not key: return self.error_empty_key(line) if key: # Flush previous assignment, if any self._assignment(key, value) def assignment(self, key, value): """Called when a full assignment is parsed.""" raise NotImplementedError() def new_section(self, section): """Called when a new section is started.""" raise NotImplementedError() def comment(self, comment): """Called when a comment is parsed.""" pass def error_invalid_assignment(self, line): raise self.parse_exc("No ':' or '=' found in assignment", self.lineno, line) def error_empty_key(self, line): raise self.parse_exc('Key cannot be empty', self.lineno, line) def error_unexpected_continuation(self, line): raise self.parse_exc('Unexpected continuation line', self.lineno, line) def error_no_section_end_bracket(self, line): raise self.parse_exc('Invalid section (must end with ])', self.lineno, line) def error_no_section_name(self, line): raise self.parse_exc('Empty section name', self.lineno, line) oslo.config-5.2.0/oslo_config/fixture.py0000666000175100017510000002161113224676052020335 0ustar zuulzuul00000000000000# # Copyright 2013 Mirantis, Inc. # Copyright 2013 OpenStack Foundation # 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. import fixtures from oslo_config import cfg class Config(fixtures.Fixture): """Allows overriding configuration settings for the test. `conf` will be reset on cleanup. """ def __init__(self, conf=cfg.CONF): self.conf = conf def setUp(self): super(Config, self).setUp() # NOTE(morganfainberg): unregister must be added to cleanup before # reset is because cleanup works in reverse order of registered items, # and a reset must occur before unregistering options can occur. self.addCleanup(self._reset_default_config_files) self.addCleanup(self._reset_default_config_dirs) self.addCleanup(self._unregister_config_opts) self.addCleanup(self.conf.reset) self._registered_config_opts = {} # Grab an old copy of the default config files/dirs - if it exists - # for subsequent cleanup. if hasattr(self.conf, 'default_config_files'): self._default_config_files = self.conf.default_config_files else: self._default_config_files = None if hasattr(self.conf, 'default_config_dirs'): self._default_config_dirs = self.conf.default_config_dirs else: self._default_config_dirs = None def config(self, **kw): """Override configuration values. The keyword arguments are the names of configuration options to override and their values. If a `group` argument is supplied, the overrides are applied to the specified configuration option group, otherwise the overrides are applied to the ``default`` group. """ group = kw.pop('group', None) for k, v in kw.items(): self.conf.set_override(k, v, group) def _unregister_config_opts(self): for group in self._registered_config_opts: self.conf.unregister_opts(self._registered_config_opts[group], group=group) def _reset_default_config_files(self): if not hasattr(self.conf, 'default_config_files'): return if self._default_config_files: self.conf.default_config_files = self._default_config_files else: # Delete, because we could conceivably begin with the property # being unset. self.conf.default_config_files = None def _reset_default_config_dirs(self): if not hasattr(self.conf, 'default_config_dirs'): return if self._default_config_dirs: self.conf.default_config_dirs = self._default_config_dirs else: # Delete, because we could conceivably begin with the property # being unset. self.conf.default_config_dirs = None def register_opt(self, opt, group=None): """Register a single option for the test run. Options registered in this manner will automatically be unregistered during cleanup. If a `group` argument is supplied, it will register the new option to that group, otherwise the option is registered to the ``default`` group. """ self.conf.register_opt(opt, group=group) self._registered_config_opts.setdefault(group, set()).add(opt) def register_opts(self, opts, group=None): """Register multiple options for the test run. This works in the same manner as register_opt() but takes a list of options as the first argument. All arguments will be registered to the same group if the ``group`` argument is supplied, otherwise all options will be registered to the ``default`` group. """ for opt in opts: self.register_opt(opt, group=group) def register_cli_opt(self, opt, group=None): """Register a single CLI option for the test run. Options registered in this manner will automatically be unregistered during cleanup. If a `group` argument is supplied, it will register the new option to that group, otherwise the option is registered to the ``default`` group. CLI options must be registered before the command line and config files are parsed. This is to ensure that all CLI options are shown in --help and option validation works as expected. """ self.conf.register_cli_opt(opt, group=group) self._registered_config_opts.setdefault(group, set()).add(opt) def register_cli_opts(self, opts, group=None): """Register multiple CLI options for the test run. This works in the same manner as register_opt() but takes a list of options as the first argument. All arguments will be registered to the same group if the ``group`` argument is supplied, otherwise all options will be registered to the ``default`` group. CLI options must be registered before the command line and config files are parsed. This is to ensure that all CLI options are shown in --help and option validation works as expected. """ for opt in opts: self.register_cli_opt(opt, group=group) def load_raw_values(self, group=None, **kwargs): """Load raw values into the configuration without registering them. This method adds a series of parameters into the current config instance, as if they had been loaded by a ConfigParser. This method does not require that you register the configuration options first, however the values loaded will not be accessible until you do. """ # Make sure the namespace exists for our tests. if not self.conf._namespace: self.conf.__call__(args=[]) # Default out the group name group = 'DEFAULT' if not group else group raw_config = dict() raw_config[group] = dict() for key, value in kwargs.items(): # Parsed values are an array of raw strings. raw_config[group][key] = [str(value)] self.conf._namespace._add_parsed_config_file(raw_config, raw_config) def set_config_files(self, config_files): """Specify a list of config files to read. This method allows you to predefine the list of configuration files that are loaded by oslo_config. It will ensure that your tests do not attempt to autodetect, and accidentally pick up config files from locally installed services. """ if not isinstance(config_files, list): raise AttributeError("Please pass a list() to set_config_files()") # Make sure the namespace exists for our tests. if not self.conf._namespace: self.conf.__call__(args=[]) self.conf.default_config_files = config_files self.conf.reload_config_files() def set_config_dirs(self, config_dirs): """Specify a list of config dirs to read. This method allows you to predefine the list of configuration dirs that are loaded by oslo_config. It will ensure that your tests do not attempt to autodetect, and accidentally pick up config files from locally installed services. """ if not isinstance(config_dirs, list): raise AttributeError("Please pass a list() to set_config_dirs()") # Make sure the namespace exists for our tests. if not self.conf._namespace: self.conf([]) self.conf.default_config_dirs = config_dirs self.conf.reload_config_files() def set_default(self, name, default, group=None): """Set a default value for an option. This method is not necessarily meant to be invoked directly. It is here to allow the set_defaults() functions in various Oslo libraries to work with a Config fixture instead of a ConfigOpts instance. Use it like:: class MyTest(testtools.TestCase): def setUp(self): super(MyTest, self).setUp() self.conf = self.useFixture(fixture.Config()) def test_something(self): some_library.set_defaults(self.conf, name='value') some_library.do_something_exciting() """ self.conf.set_default(name, default, group) self.addCleanup(self.conf.clear_default, name, group) oslo.config-5.2.0/oslo_config/_list_opts.py0000666000175100017510000000214113224676052021023 0ustar zuulzuul00000000000000# 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 def list_opts(): default_config_files = [ '~/.project/project.conf', '~/project.conf', '/etc/project/project.conf', '/etc/project.conf', ] default_config_dirs = [ '~/.project/project.conf.d/', '~/project.conf.d/', '/etc/project/project.conf.d/', '/etc/project.conf.d/', ] return [ (None, cfg.ConfigOpts._make_config_options(default_config_files, default_config_dirs)), ] oslo.config-5.2.0/oslo_config/sphinxconfiggen.py0000666000175100017510000000646313224676052022050 0ustar zuulzuul00000000000000# Copyright 2015 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 oslo_config import generator def generate_sample(app): if not app.config.config_generator_config_file: app.warn("No config_generator_config_file is specified, " "skipping sample config generation") return # Decided to update the existing config option # config_generator_config_file to support a value that is a list of # tuples, containing the file names as (input, output). # We need to retain support for the option referring to a single string, # and using the sample_config_basename for the output file in that case. # After we release support for both forms of the option, we can update # projects to always use the list of tuples, then remove # sample_config_basename and the support for config_generator_config_file # being a single string. if isinstance(app.config.config_generator_config_file, list): for config_file, base_name in app.config.config_generator_config_file: if base_name is None: base_name = _get_default_basename(config_file) _generate_sample(app, config_file, base_name) else: _generate_sample(app, app.config.config_generator_config_file, app.config.sample_config_basename) def _get_default_basename(config_file): return os.path.splitext(os.path.basename(config_file))[0] def _generate_sample(app, config_file, base_name): def info(msg): app.info('[%s] %s' % (__name__, msg)) # If we are given a file that isn't an absolute path, look for it # in the source directory if it doesn't exist. candidates = [ config_file, os.path.join(app.srcdir, config_file,), ] for c in candidates: if os.path.isfile(c): info('reading config generator instructions from %s' % c) config_path = c break else: raise ValueError( "Could not find config_generator_config_file %r" % app.config.config_generator_config_file) if base_name: out_file = os.path.join(app.srcdir, base_name) + '.conf.sample' if not os.path.isdir(os.path.dirname(os.path.abspath(out_file))): os.mkdir(os.path.dirname(os.path.abspath(out_file))) else: file_name = 'sample.config' out_file = os.path.join(app.srcdir, file_name) info('writing sample configuration to %s' % out_file) generator.main(args=['--config-file', config_path, '--output-file', out_file]) def setup(app): app.add_config_value('config_generator_config_file', None, 'env') app.add_config_value('sample_config_basename', None, 'env') app.connect('builder-inited', generate_sample) oslo.config-5.2.0/oslo_config/generator.py0000666000175100017510000007225113224676105020642 0ustar zuulzuul00000000000000# Copyright 2012 SINA Corporation # Copyright 2014 Cisco Systems, Inc. # All Rights Reserved. # Copyright 2014 Red Hat, Inc. # # 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. """Sample configuration generator Tool for generating a sample configuration file. See ../doc/source/cli/generator.rst for details. .. versionadded:: 1.4 """ import collections import copy import logging import operator import sys import textwrap import json import pkg_resources import six import yaml from oslo_config import cfg import stevedore.named # noqa LOG = logging.getLogger(__name__) UPPER_CASE_GROUP_NAMES = ['DEFAULT'] _generator_opts = [ cfg.StrOpt( 'output-file', help='Path of the file to write to. Defaults to stdout.'), cfg.IntOpt( 'wrap-width', default=70, help='The maximum length of help lines.'), cfg.MultiStrOpt( 'namespace', required=True, help='Option namespace under "oslo.config.opts" in which to query ' 'for options.'), cfg.BoolOpt( 'minimal', default=False, help='Generate a minimal required configuration.'), cfg.BoolOpt( 'summarize', default=False, help='Only output summaries of help text to config files. Retain ' 'longer help text for Sphinx documents.'), cfg.StrOpt( 'format', help='Desired format for the output.', default='ini', choices=[ ('ini', 'The only format that can be used directly with ' 'oslo.config.'), ('json', 'Intended for third-party tools that want to write ' 'config files based on the sample config data.'), ('yaml', 'Same as json'), ('rst', 'Can be used to dump the text given to Sphinx when ' 'building documentation using the Sphinx extension. ' 'Useful for debugging,') ], dest='format_'), ] def register_cli_opts(conf): """Register the formatter's CLI options with a ConfigOpts instance. Note, this must be done before the ConfigOpts instance is called to parse the configuration. :param conf: a ConfigOpts instance :raises: DuplicateOptError, ArgsAlreadyParsedError """ conf.register_cli_opts(_generator_opts) def _format_defaults(opt): "Return a list of formatted default values." if isinstance(opt, cfg.MultiStrOpt): if opt.sample_default is not None: defaults = opt.sample_default elif not opt.default: defaults = [''] else: defaults = opt.default else: if opt.sample_default is not None: default_str = str(opt.sample_default) elif opt.default is None: default_str = '' elif (isinstance(opt, (cfg.StrOpt, cfg.IPOpt, cfg.HostnameOpt, cfg.HostAddressOpt, cfg.URIOpt))): default_str = opt.default elif isinstance(opt, cfg.BoolOpt): default_str = str(opt.default).lower() elif isinstance(opt, (cfg.IntOpt, cfg.FloatOpt, cfg.PortOpt)): default_str = str(opt.default) elif isinstance(opt, (cfg.ListOpt, cfg._ConfigFileOpt, cfg._ConfigDirOpt)): default_str = ','.join(six.text_type(d) for d in opt.default) elif isinstance(opt, cfg.DictOpt): sorted_items = sorted(opt.default.items(), key=operator.itemgetter(0)) default_str = ','.join(['%s:%s' % i for i in sorted_items]) else: LOG.warning('Unknown option type: %s', repr(opt)) default_str = str(opt.default) defaults = [default_str] results = [] for default_str in defaults: if not isinstance(default_str, six.text_type): default_str = six.text_type(default_str) if default_str.strip() != default_str: default_str = '"%s"' % default_str results.append(default_str) return results _TYPE_NAMES = { str: 'string value', int: 'integer value', float: 'floating point value', } def _format_type_name(opt_type): """Format the type name to use in describing an option""" try: return opt_type.type_name except AttributeError: # nosec pass try: return _TYPE_NAMES[opt_type] except KeyError: # nosec pass return 'unknown value' class _OptFormatter(object): """Format configuration option descriptions to a file.""" def __init__(self, output_file=None, wrap_width=70): """Construct an OptFormatter object. :param output_file: a writeable file object :param wrap_width: The maximum length of help lines, 0 to not wrap """ self.output_file = output_file or sys.stdout self.wrap_width = wrap_width def _format_help(self, help_text): """Format the help for a group or option to the output file. :param help_text: The text of the help string """ if self.wrap_width is not None and self.wrap_width > 0: wrapped = "" for line in help_text.splitlines(): text = "\n".join(textwrap.wrap(line, self.wrap_width, initial_indent='# ', subsequent_indent='# ', break_long_words=False, replace_whitespace=False)) wrapped += "#" if text == "" else text wrapped += "\n" lines = [wrapped] else: lines = ['# ' + help_text + '\n'] return lines def _get_choice_text(self, choice): if choice is None: return '' elif choice == '': return "''" return six.text_type(choice) def format_group(self, group_or_groupname): """Format the description of a group header to the output file :param group_or_groupname: a cfg.OptGroup instance or a name of group :returns: a formatted group description string """ if isinstance(group_or_groupname, cfg.OptGroup): group = group_or_groupname lines = ['[%s]\n' % group.name] if group.help: lines += self._format_help(group.help) else: groupname = group_or_groupname lines = ['[%s]\n' % groupname] self.writelines(lines) def format(self, opt, group_name, minimal=False, summarize=False): """Format a description of an option to the output file. :param opt: a cfg.Opt instance :param group_name: name of the group to which the opt is assigned :param minimal: enable option by default, marking it as required :param summarize: output a summarized description of the opt :returns: a formatted opt description string """ if not opt.help: LOG.warning('"%s" is missing a help string', opt.dest) opt_type = _format_type_name(opt.type) opt_prefix = '' if (opt.deprecated_for_removal and not opt.help.startswith('DEPRECATED')): opt_prefix = 'DEPRECATED: ' if opt.help: # an empty line signifies a new paragraph. We only want the # summary line if summarize: _split = opt.help.split('\n\n') opt_help = _split[0].rstrip(':').rstrip('.') if len(_split) > 1: opt_help += '. For more information, refer to the ' opt_help += 'documentation.' else: opt_help = opt.help help_text = u'%s%s (%s)' % (opt_prefix, opt_help, opt_type) else: help_text = u'(%s)' % opt_type lines = self._format_help(help_text) if getattr(opt.type, 'min', None) is not None: lines.append('# Minimum value: %d\n' % opt.type.min) if getattr(opt.type, 'max', None) is not None: lines.append('# Maximum value: %d\n' % opt.type.max) if getattr(opt.type, 'choices', None): lines.append('# Possible values:\n') for choice in opt.type.choices: help_text = '%s - %s' % ( self._get_choice_text(choice), opt.type.choices[choice] or '') lines.extend(self._format_help(help_text)) try: if opt.mutable: lines.append( '# Note: This option can be changed without restarting.\n' ) except AttributeError as err: # NOTE(dhellmann): keystoneauth defines its own Opt class, # and neutron (at least) returns instances of those # classes instead of oslo_config Opt instances. The new # mutable attribute is the first property where the API # isn't supported in the external class, so we can use # this failure to emit a warning. See # https://bugs.launchpad.net/keystoneauth/+bug/1548433 for # more details. import warnings if not isinstance(opt, cfg.Opt): warnings.warn( 'Incompatible option class for %s (%r): %s' % (opt.dest, opt.__class__, err), ) else: warnings.warn('Failed to fully format sample for %s: %s' % (opt.dest, err)) for d in opt.deprecated_opts: # NOTE(bnemec): opt names with a - are not valid in a config file, # but it is possible to add a DeprecatedOpt with a - name. We # want to ignore those as they won't work anyway. if d.name and '-' not in d.name: lines.append('# Deprecated group/name - [%s]/%s\n' % (d.group or group_name, d.name or opt.dest)) if opt.deprecated_for_removal: if opt.deprecated_since: lines.append( '# This option is deprecated for removal since %s.\n' % ( opt.deprecated_since)) else: lines.append( '# This option is deprecated for removal.\n') lines.append( '# Its value may be silently ignored in the future.\n') if opt.deprecated_reason: lines.extend( self._format_help('Reason: ' + opt.deprecated_reason)) if opt.advanced: lines.append( '# Advanced Option: intended for advanced users and not used\n' '# by the majority of users, and might have a significant\n' '# effect on stability and/or performance.\n' ) if hasattr(opt.type, 'format_defaults'): defaults = opt.type.format_defaults(opt.default, opt.sample_default) else: LOG.debug( "The type for option %(name)s which is %(type)s is not a " "subclass of types.ConfigType and doesn't provide a " "'format_defaults' method. A default formatter is not " "available so the best-effort formatter will be used.", {'type': opt.type, 'name': opt.name}) defaults = _format_defaults(opt) for default_str in defaults: if default_str: default_str = ' ' + default_str.replace('\n', '\n# ') if minimal: lines.append('%s =%s\n' % (opt.dest, default_str)) else: lines.append('#%s =%s\n' % (opt.dest, default_str)) self.writelines(lines) def write(self, s): """Write an arbitrary string to the output file. :param s: an arbitrary string """ self.output_file.write(s) def writelines(self, l): """Write an arbitrary sequence of strings to the output file. :param l: a list of arbitrary strings """ self.output_file.writelines(l) def _cleanup_opts(read_opts): """Cleanup duplicate options in namespace groups Return a structure which removes duplicate options from a namespace group. NOTE:(rbradfor) This does not remove duplicated options from repeating groups in different namespaces: :param read_opts: a list (namespace, [(group, [opt_1, opt_2])]) tuples :returns: a list of (namespace, [(group, [opt_1, opt_2])]) tuples """ # OrderedDict is used specifically in the three levels to maintain the # source order of namespace/group/opt values clean = collections.OrderedDict() for namespace, listing in read_opts: if namespace not in clean: clean[namespace] = collections.OrderedDict() for group, opts in listing: # NOTE: Normalize group names to lowe-case except those defined in # UPPER_CASE_GROUP_NAMES if group: group_name = getattr(group, 'name', str(group)) if group_name.upper() in UPPER_CASE_GROUP_NAMES: normalized_gn = group_name.upper() else: normalized_gn = group_name.lower() if normalized_gn != group_name: LOG.warning('normalizing group name %r to %r', group_name, normalized_gn) if hasattr(group, 'name'): group.name = normalized_gn else: group = normalized_gn if group not in clean[namespace]: clean[namespace][group] = collections.OrderedDict() for opt in opts: clean[namespace][group][opt.dest] = opt # recreate the list of (namespace, [(group, [opt_1, opt_2])]) tuples # from the cleaned structure. cleaned_opts = [ (namespace, [(g, list(clean[namespace][g].values())) for g in clean[namespace]]) for namespace in clean ] return cleaned_opts def _get_raw_opts_loaders(namespaces): """List the options available via the given namespaces. :param namespaces: a list of namespaces registered under 'oslo.config.opts' :returns: a list of (namespace, [(group, [opt_1, opt_2])]) tuples """ mgr = stevedore.named.NamedExtensionManager( 'oslo.config.opts', names=namespaces, on_load_failure_callback=on_load_failure_callback, invoke_on_load=False) return [(e.name, e.plugin) for e in mgr] def _get_driver_opts_loaders(namespaces, driver_option_name): mgr = stevedore.named.NamedExtensionManager( namespace='oslo.config.opts.' + driver_option_name, names=namespaces, on_load_failure_callback=on_load_failure_callback, invoke_on_load=False) return [(e.name, e.plugin) for e in mgr] def _get_driver_opts(driver_option_name, namespaces): """List the options available from plugins for drivers based on the option. :param driver_option_name: The name of the option controlling the driver options. :param namespaces: a list of namespaces registered under 'oslo.config.opts.' + driver_option_name :returns: a dict mapping driver name to option list """ all_opts = {} loaders = _get_driver_opts_loaders(namespaces, driver_option_name) for plugin_name, loader in loaders: for driver_name, option_list in loader().items(): all_opts.setdefault(driver_name, []).extend(option_list) return all_opts def _get_opt_default_updaters(namespaces): mgr = stevedore.named.NamedExtensionManager( 'oslo.config.opts.defaults', names=namespaces, warn_on_missing_entrypoint=False, on_load_failure_callback=on_load_failure_callback, invoke_on_load=False) return [ep.plugin for ep in mgr] def _update_defaults(namespaces): "Let application hooks update defaults inside libraries." for update in _get_opt_default_updaters(namespaces): update() def _list_opts(namespaces): """List the options available via the given namespaces. Duplicate options from a namespace are removed. :param namespaces: a list of namespaces registered under 'oslo.config.opts' :returns: a list of (namespace, [(group, [opt_1, opt_2])]) tuples """ # Load the functions to get the options. loaders = _get_raw_opts_loaders(namespaces) # Update defaults, which might change global settings in library # modules. _update_defaults(namespaces) # Ask for the option definitions. At this point any global default # changes made by the updaters should be in effect. response = [] for namespace, loader in loaders: # The loaders return iterables for the group opts, and we need # to extend them, so build a list. namespace_values = [] # Look through the groups and find any that need drivers so we # can load those extra options. for group, group_opts in loader(): # group_opts is an iterable but we are going to extend it # so convert it to a list. group_opts = list(group_opts) if isinstance(group, cfg.OptGroup): if group.driver_option: # Load the options for all of the known drivers. driver_opts = _get_driver_opts( group.driver_option, namespaces, ) # Save the list of names of options for each # driver in the group for use later. Add the # options to the group_opts list so they are # processed along with the static options in that # group. driver_opt_names = {} for driver_name, opts in sorted(driver_opts.items()): # Multiple plugins may add values to the same # driver name, so combine the lists we do # find. driver_opt_names.setdefault(driver_name, []).extend( o.name for o in opts) group_opts.extend(opts) group._save_driver_opts(driver_opt_names) namespace_values.append((group, group_opts)) response.append((namespace, namespace_values)) return _cleanup_opts(response) def on_load_failure_callback(*args, **kwargs): raise def _output_opts(f, group, group_data, minimal=False, summarize=False): f.format_group(group_data['object'] or group) for (namespace, opts) in sorted(group_data['namespaces'], key=operator.itemgetter(0)): f.write('\n#\n# From %s\n#\n' % namespace) for opt in sorted(opts, key=operator.attrgetter('advanced')): try: if minimal and not opt.required: pass else: f.write('\n') f.format(opt, group, minimal, summarize) except Exception as err: f.write('# Warning: Failed to format sample for %s\n' % (opt.dest,)) f.write('# %s\n' % (err,)) def _get_groups(conf_ns): """Invert a list of groups by namespace into a dict by group name. :param conf_ns: a list of (namespace, [(, [opt_1, opt_2])]) tuples, such as returned by _list_opts. :returns: {, {'object': , 'namespaces': [(, )]}} may be a string or a group object. is always a string. will only be set if was a group object in at least one namespace. Keying by group_name avoids adding duplicate group names in case a group is added as both an OptGroup and as a str, but still makes the additional OptGroup data available to the output code when possible. """ groups = {'DEFAULT': {'object': None, 'namespaces': []}} for namespace, listing in conf_ns: for group, opts in listing: if not opts: continue group = group if group else 'DEFAULT' is_optgroup = hasattr(group, 'name') group_name = group.name if is_optgroup else group if group_name not in groups: groups[group_name] = {'object': None, 'namespaces': []} if is_optgroup: groups[group_name]['object'] = group groups[group_name]['namespaces'].append((namespace, opts)) return groups def _build_entry(opt, group, namespace, conf): """Return a dict representing the passed in opt The dict will contain all public attributes of opt, as well as additional entries for namespace, choices, min, and max. Any DeprecatedOpts contained in the deprecated_opts member will be converted to a dict with the format: {'group': , 'name': } :param opt: The Opt object to represent as a dict. :param group: The name of the group containing opt. :param namespace: The name of the namespace containing opt. :param conf: The ConfigOpts object containing the options for the generator tool """ entry = {key: value for key, value in opt.__dict__.items() if not key.startswith('_')} entry['namespace'] = namespace # Where present, we store choices as an OrderedDict. The default repr for # this is not very machine readable, thus, it is switched to a list of # tuples here. In addition, in some types, choices is explicitly set to # None. Force these cases to [] so it is always an iterable type. if getattr(entry['type'], 'choices', None): entry['choices'] = list(entry['type'].choices.items()) else: entry['choices'] = [] entry['min'] = getattr(entry['type'], 'min', None) entry['max'] = getattr(entry['type'], 'max', None) entry['type'] = _format_type_name(entry['type']) deprecated_opts = [] for deprecated_opt in entry['deprecated_opts']: # NOTE(bnemec): opt names with a - are not valid in a config file, # but it is possible to add a DeprecatedOpt with a - name. We # want to ignore those as they won't work anyway. if not deprecated_opt.name or '-' not in deprecated_opt.name: deprecated_opts.append( {'group': deprecated_opt.group or group, 'name': deprecated_opt.name or entry['name'], }) entry['deprecated_opts'] = deprecated_opts return entry def _generate_machine_readable_data(groups, conf): """Create data structure for machine readable sample config Returns a dictionary with the top-level keys 'options', 'deprecated_options', and 'generator_options'. 'options' contains a dict mapping group names to a list of options in that group. Each option is represented by the result of a call to _build_entry. Only non-deprecated options are included in this list. 'deprecated_options' contains a dict mapping groups names to a list of opts from that group which were deprecated. 'generator_options' is a dict mapping the options for the sample config generator itself to their values. :param groups: A dict of groups as returned by _get_groups. :param conf: The ConfigOpts object containing the options for the generator tool """ output_data = {'options': {}, 'deprecated_options': {}, 'generator_options': {}} # See _get_groups for details on the structure of group_data for group_name, group_data in groups.items(): output_group = {'opts': [], 'help': ''} output_data['options'][group_name] = output_group for namespace in group_data['namespaces']: for opt in namespace[1]: if group_data['object']: output_group.update( group_data['object']._get_generator_data() ) else: output_group.update({ 'dynamic_group_owner': '', 'driver_option': '', 'driver_opts': {}, }) entry = _build_entry(opt, group_name, namespace[0], conf) output_group['opts'].append(entry) # Need copies of the opts because we modify them for deprecated_opt in copy.deepcopy(entry['deprecated_opts']): group = deprecated_opt.pop('group') deprecated_options = output_data['deprecated_options'] deprecated_options.setdefault(group, []) deprecated_opt['replacement_name'] = entry['name'] deprecated_opt['replacement_group'] = group_name deprecated_options[group].append(deprecated_opt) # Build the list of options in the group that are not tied to # a driver. non_driver_opt_names = [ o['name'] for o in output_group['opts'] if not any(o['name'] in output_group['driver_opts'][d] for d in output_group['driver_opts']) ] output_group['standard_opts'] = non_driver_opt_names output_data['generator_options'] = dict(conf) return output_data def _output_machine_readable(groups, output_file, conf): """Write a machine readable sample config file Take the data returned by _generate_machine_readable_data and write it in the format specified by the format_ attribute of conf. :param groups: A dict of groups as returned by _get_groups. :param output_file: A file-like object to which the data should be written. :param conf: The ConfigOpts object containing the options for the generator tool """ output_data = _generate_machine_readable_data(groups, conf) if conf.format_ == 'yaml': output_file.write(yaml.safe_dump(output_data, default_flow_style=False)) else: output_file.write(json.dumps(output_data, sort_keys=True)) output_file.write('\n') def _output_human_readable(namespaces, output_file): """Write an RST formated version of the docs for the options. :param groups: A list of the namespaces to use for discovery. :param output_file: A file-like object to which the data should be written. """ try: from oslo_config import sphinxext except ImportError: raise RuntimeError( 'Could not import sphinxext. ' 'Please install Sphinx and try again.', ) output_data = list(sphinxext._format_option_help( LOG, namespaces, False)) output_file.write('\n'.join(output_data)) def generate(conf, output_file=None): """Generate a sample config file. List all of the options available via the namespaces specified in the given configuration and write a description of them to the specified output file. :param conf: a ConfigOpts instance containing the generator's configuration """ conf.register_opts(_generator_opts) if output_file is None: output_file = (open(conf.output_file, 'w') if conf.output_file else sys.stdout) groups = _get_groups(_list_opts(conf.namespace)) if conf.format_ == 'ini': formatter = _OptFormatter(output_file=output_file, wrap_width=conf.wrap_width) # Output the "DEFAULT" section as the very first section _output_opts(formatter, 'DEFAULT', groups.pop('DEFAULT'), conf.minimal, conf.summarize) # output all other config sections with groups in alphabetical order for group, group_data in sorted(groups.items()): formatter.write('\n\n') _output_opts(formatter, group, group_data, conf.minimal, conf.summarize) elif conf.format_ == 'rst': _output_human_readable( conf.namespace, output_file=output_file, ) else: _output_machine_readable(groups, output_file=output_file, conf=conf) def main(args=None): """The main function of oslo-config-generator.""" version = pkg_resources.get_distribution('oslo.config').version logging.basicConfig(level=logging.WARN) conf = cfg.ConfigOpts() register_cli_opts(conf) try: conf(args, version=version) except cfg.RequiredOptError: conf.print_help() if not sys.argv[1:]: raise SystemExit raise generate(conf) if __name__ == '__main__': main() oslo.config-5.2.0/oslo_config/version.py0000666000175100017510000000122313224676052020331 0ustar zuulzuul00000000000000# Copyright 2016 OpenStack Foundation # # 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 pbr.version version_info = pbr.version.VersionInfo('oslo.config') oslo.config-5.2.0/oslo_config/sphinxext.py0000666000175100017510000004025313224676105020703 0ustar zuulzuul00000000000000# 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 docutils import nodes from docutils.parsers import rst from docutils.parsers.rst import directives from docutils.statemachine import ViewList from sphinx import addnodes from sphinx.directives import ObjectDescription from sphinx.domains import Domain from sphinx.domains import ObjType from sphinx.roles import XRefRole from sphinx.util.nodes import make_refnode from sphinx.util.nodes import nested_parse_with_titles from oslo_config import cfg from oslo_config import generator import oslo_i18n import six def _list_table(headers, data, title='', columns=None): """Build a list-table directive. :param add: Function to add one row to output. :param headers: List of header values. :param data: Iterable of row data, yielding lists or tuples with rows. """ yield '.. list-table:: %s' % title yield ' :header-rows: 1' if columns: yield ' :widths: %s' % (','.join(str(c) for c in columns)) yield '' yield ' - * %s' % headers[0] for h in headers[1:]: yield ' * %s' % h for row in data: yield ' - * %s' % row[0] for r in row[1:]: yield ' * %s' % r def _indent(text, n=2): padding = ' ' * n return '\n'.join(padding + l for l in text.splitlines()) def _make_anchor_target(group_name, option_name): # We need to ensure this is unique across entire documentation # http://www.sphinx-doc.org/en/stable/markup/inline.html#ref-role target = '%s.%s' % (cfg._normalize_group_name(group_name), option_name.lower()) return target _TYPE_DESCRIPTIONS = { cfg.StrOpt: 'string', cfg.BoolOpt: 'boolean', cfg.IntOpt: 'integer', cfg.FloatOpt: 'floating point', cfg.ListOpt: 'list', cfg.DictOpt: 'dict', cfg.MultiStrOpt: 'multi-valued', cfg.IPOpt: 'ip address', cfg.PortOpt: 'port number', cfg.HostnameOpt: 'hostname', cfg.URIOpt: 'URI', cfg.HostAddressOpt: 'host address', cfg._ConfigFileOpt: 'list of filenames', cfg._ConfigDirOpt: 'list of directory names', } def _get_choice_text(choice): if choice is None: return '' elif choice == '': return "''" return six.text_type(choice) def _format_group(app, namespace, group_name, group_obj, opt_list): group_name = group_name or 'DEFAULT' app.debug('[oslo.config] %s %s' % (namespace, group_name)) yield '.. oslo.config:group:: %s' % group_name if namespace: yield ' :namespace: %s' % namespace yield '' if group_obj and group_obj.help: yield _indent(group_obj.help.rstrip()) yield '' for opt in opt_list: opt_type = _TYPE_DESCRIPTIONS.get(type(opt), 'unknown type') yield '.. oslo.config:option:: %s' % opt.dest yield '' yield _indent(':Type: %s' % opt_type) for default in generator._format_defaults(opt): if default: default = '``' + default + '``' yield _indent(':Default: %s' % default) if getattr(opt.type, 'min', None) is not None: yield _indent(':Minimum Value: %s' % opt.type.min) if getattr(opt.type, 'max', None) is not None: yield _indent(':Maximum Value: %s' % opt.type.max) if getattr(opt.type, 'choices', None): choices_text = ', '.join([_get_choice_text(choice) for choice in opt.type.choices]) yield _indent(':Valid Values: %s' % choices_text) try: if opt.mutable: yield _indent( ':Mutable: This option can be changed without restarting.' ) except AttributeError as err: # NOTE(dhellmann): keystoneauth defines its own Opt class, # and neutron (at least) returns instances of those # classes instead of oslo_config Opt instances. The new # mutable attribute is the first property where the API # isn't supported in the external class, so we can use # this failure to emit a warning. See # https://bugs.launchpad.net/keystoneauth/+bug/1548433 for # more details. import warnings if not isinstance(cfg.Opt, opt): warnings.warn( 'Incompatible option class for %s (%r): %s' % (opt.dest, opt.__class__, err), ) else: warnings.warn('Failed to fully format sample for %s: %s' % (opt.dest, err)) if opt.advanced: yield _indent( ':Advanced Option: Intended for advanced users and not used') yield _indent( 'by the majority of users, and might have a significant', 6) yield _indent( 'effect on stability and/or performance.', 6) try: help_text = opt.help % {'default': 'the value above'} except (TypeError, KeyError, ValueError): # There is no mention of the default in the help string, # the string had some unknown key, or the string contained # invalid formatting characters help_text = opt.help if help_text: yield '' yield _indent(help_text) # We don't bother outputting this if not using new-style choices with # inline descriptions if getattr(opt.type, 'choices', None) and not all( x is None for x in opt.type.choices.values()): yield '' yield _indent('.. rubric:: Possible values') for choice in opt.type.choices: yield '' yield _indent(_get_choice_text(choice)) yield _indent(_indent( opt.type.choices[choice] or '')) if opt.deprecated_opts: yield '' for line in _list_table( ['Group', 'Name'], ((d.group or group_name, d.name or opt.dest or 'UNSET') for d in opt.deprecated_opts), title='Deprecated Variations'): yield _indent(line) if opt.deprecated_for_removal: yield '' yield _indent('.. warning::') if opt.deprecated_since: yield _indent(' This option is deprecated for removal ' 'since %s.' % opt.deprecated_since) else: yield _indent(' This option is deprecated for removal.') yield _indent(' Its value may be silently ignored ') yield _indent(' in the future.') if opt.deprecated_reason: yield '' yield _indent(' :Reason: ' + opt.deprecated_reason) yield '' def _format_option_help(app, namespaces, split_namespaces): """Generate a series of lines of restructuredtext. Format the option help as restructuredtext and return it as a list of lines. """ opts = generator._list_opts(namespaces) if split_namespaces: for namespace, opt_list in opts: for group, opts in opt_list: if isinstance(group, cfg.OptGroup): group_name = group.name else: group_name = group group = None if group_name is None: group_name = 'DEFAULT' lines = _format_group( app=app, namespace=namespace, group_name=group_name, group_obj=group, opt_list=opts, ) for line in lines: yield line else: # Merge the options from different namespaces that belong to # the same group together and format them without the # namespace. by_section = {} group_objs = {} for ignore, opt_list in opts: for group, group_opts in opt_list: if isinstance(group, cfg.OptGroup): group_name = group.name else: group_name = group group = None if group_name is None: group_name = 'DEFAULT' group_objs.setdefault(group_name, group) by_section.setdefault(group_name, []).extend(group_opts) for group_name, group_opts in sorted(by_section.items()): lines = _format_group( app=app, namespace=None, group_name=group_name, group_obj=group_objs.get(group_name), opt_list=group_opts, ) for line in lines: yield line class ShowOptionsDirective(rst.Directive): option_spec = { 'split-namespaces': directives.flag, 'config-file': directives.unchanged, } has_content = True def run(self): env = self.state.document.settings.env app = env.app split_namespaces = 'split-namespaces' in self.options config_file = self.options.get('config-file') if config_file: app.info('loading config file %s' % config_file) conf = cfg.ConfigOpts() conf.register_opts(generator._generator_opts) conf( args=['--config-file', config_file], project='oslo.config.sphinxext', ) namespaces = conf.namespace[:] else: namespaces = [ c.strip() for c in self.content if c.strip() ] result = ViewList() source_name = '<' + __name__ + '>' for line in _format_option_help(app, namespaces, split_namespaces): result.append(line, source_name) node = nodes.section() node.document = self.state.document nested_parse_with_titles(self.state, result, node) return node.children class ConfigGroupXRefRole(XRefRole): "Handles :oslo.config:group: roles pointing to configuration groups." def __init__(self): super(ConfigGroupXRefRole, self).__init__( warn_dangling=True, ) def process_link(self, env, refnode, has_explicit_title, title, target): # The anchor for the group link is the group name. return target, target class ConfigOptXRefRole(XRefRole): "Handles :oslo.config:option: roles pointing to configuration options." def __init__(self): super(ConfigOptXRefRole, self).__init__( warn_dangling=True, ) def process_link(self, env, refnode, has_explicit_title, title, target): if not has_explicit_title: title = target if '.' in target: group, opt_name = target.split('.') else: group = 'DEFAULT' opt_name = target anchor = _make_anchor_target(group, opt_name) return title, anchor class ConfigGroup(rst.Directive): required_arguments = 1 optional_arguments = 0 has_content = True option_spec = { 'namespace': directives.unchanged, } def run(self): env = self.state.document.settings.env app = env.app group_name = self.arguments[0] namespace = self.options.get('namespace') cached_groups = env.domaindata['oslo.config']['groups'] # Store the current group for use later in option directives env.temp_data['oslo.config:group'] = group_name app.debug('oslo.config group %s' % group_name) # Store the location where this group is being defined # for use when resolving cross-references later. # FIXME: This should take the source namespace into account, too cached_groups[group_name] = env.docname result = ViewList() source_name = '<' + __name__ + '>' def _add(text): "Append some text to the output result view to be parsed." result.append(text, source_name) if namespace: title = '%s: %s' % (namespace, group_name) else: title = group_name _add(title) _add('-' * len(title)) _add('') for line in self.content: _add(line) node = nodes.section() node.document = self.state.document nested_parse_with_titles(self.state, result, node) first_child = node.children[0] # Compute the normalized target and set the node to have that # as an id target_name = cfg._normalize_group_name(group_name) first_child['ids'].append(target_name) indexnode = addnodes.index(entries=[]) return [indexnode] + node.children class ConfigOption(ObjectDescription): "Description of a configuration option (.. option)." def handle_signature(self, sig, signode): """Transform an option description into RST nodes.""" optname = sig self.env.app.debug('oslo.config option %s' % optname) # Insert a node into the output showing the option name signode += addnodes.desc_name(optname, optname) signode['allnames'] = [optname] return optname def add_target_and_index(self, firstname, sig, signode): cached_options = self.env.domaindata['oslo.config']['options'] # Look up the current group name from the processing context currgroup = self.env.temp_data.get('oslo.config:group') # Compute the normalized target name for the option and give # that to the node as an id target_name = _make_anchor_target(currgroup, sig) signode['ids'].append(target_name) self.state.document.note_explicit_target(signode) # Store the location of the option definition for later use in # resolving cross-references # FIXME: This should take the source namespace into account, too cached_options[target_name] = self.env.docname class ConfigDomain(Domain): """oslo.config domain.""" name = 'oslo.config' label = 'oslo.config' object_types = { 'configoption': ObjType('configuration option', 'option'), } directives = { 'group': ConfigGroup, 'option': ConfigOption, } roles = { 'option': ConfigOptXRefRole(), 'group': ConfigGroupXRefRole(), } initial_data = { 'options': {}, 'groups': {}, } def resolve_xref(self, env, fromdocname, builder, typ, target, node, contnode): if typ == 'option': group_name, option_name = target.split('.', 1) return make_refnode( builder, fromdocname, env.domaindata['oslo.config']['options'][target], target, contnode, option_name, ) if typ == 'group': return make_refnode( builder, fromdocname, env.domaindata['oslo.config']['groups'][target], target, contnode, target, ) return None def setup(app): # NOTE(dhellmann): Try to turn off lazy translation from oslo_i18n # so any translated help text or deprecation messages associated # with configuration options are treated as regular strings # instead of Message objects. Unfortunately this is a bit # order-dependent, and so it's still possible that importing code # from another module such as through the autodoc features, or # even through the plugin scanner, will turn lazy evaluation back # on. oslo_i18n.enable_lazy(False) app.add_directive('show-options', ShowOptionsDirective) app.add_domain(ConfigDomain) oslo.config-5.2.0/releasenotes/0000775000175100017510000000000013224676374016471 5ustar zuulzuul00000000000000oslo.config-5.2.0/releasenotes/source/0000775000175100017510000000000013224676374017771 5ustar zuulzuul00000000000000oslo.config-5.2.0/releasenotes/source/conf.py0000666000175100017510000002204413224676052021265 0ustar zuulzuul00000000000000# -*- coding: utf-8 -*- # 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. # oslo.config Release Notes documentation build configuration file, created by # sphinx-quickstart on Tue Nov 3 17:40:50 2015. # # This file is execfile()d with the current directory set to its # containing dir. # # Note that not all possible configuration values are present in this # autogenerated file. # # All configuration values have a default; values that are commented out # serve to show the default. # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. # sys.path.insert(0, os.path.abspath('.')) # -- General configuration ------------------------------------------------ # If your documentation needs a minimal Sphinx version, state it here. # needs_sphinx = '1.0' # Add any Sphinx extension module names here, as strings. They can be # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom # ones. extensions = [ 'openstackdocstheme', 'reno.sphinxext', ] # openstackdocstheme options repository_name = 'openstack/oslo.config' bug_project = 'oslo.config' bug_tag = '' html_last_updated_fmt = '%Y-%m-%d %H:%M' # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] # The suffix of source filenames. source_suffix = '.rst' # The encoding of source files. # source_encoding = 'utf-8-sig' # The master toctree document. master_doc = 'index' # General information about the project. project = u'oslo.config Release Notes' copyright = u'2016, oslo.config Developers' # Release notes do not need a version in the title, they span # multiple versions. # The full version, including alpha/beta/rc tags. release = '' # The short X.Y version. version = '' # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. # language = None # There are two options for replacing |today|: either, you set today to some # non-false value, then it is used: # today = '' # Else, today_fmt is used as the format for a strftime call. # today_fmt = '%B %d, %Y' # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. exclude_patterns = [] # The reST default role (used for this markup: `text`) to use for all # documents. # default_role = None # If true, '()' will be appended to :func: etc. cross-reference text. # add_function_parentheses = True # If true, the current module name will be prepended to all description # unit titles (such as .. function::). # add_module_names = True # If true, sectionauthor and moduleauthor directives will be shown in the # output. They are ignored by default. # show_authors = False # The name of the Pygments (syntax highlighting) style to use. pygments_style = 'sphinx' # A list of ignored prefixes for module index sorting. # modindex_common_prefix = [] # If true, keep warnings as "system message" paragraphs in the built documents. # keep_warnings = False # -- Options for HTML output ---------------------------------------------- # The theme to use for HTML and HTML Help pages. See the documentation for # a list of builtin themes. html_theme = 'openstackdocs' # Theme options are theme-specific and customize the look and feel of a theme # further. For a list of options available for each theme, see the # documentation. # html_theme_options = {} # Add any paths that contain custom themes here, relative to this directory. # html_theme_path = [] # The name for this set of Sphinx documents. If None, it defaults to # " v documentation". # html_title = None # A shorter title for the navigation bar. Default is the same as html_title. # html_short_title = None # The name of an image file (relative to this directory) to place at the top # of the sidebar. # html_logo = None # The name of an image file (within the static path) to use as favicon of the # docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 # pixels large. # html_favicon = None # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". html_static_path = ['_static'] # Add any extra paths that contain custom files (such as robots.txt or # .htaccess) here, relative to this directory. These files are copied # directly to the root of the documentation. # html_extra_path = [] # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, # using the given strftime format. # html_last_updated_fmt = '%b %d, %Y' # If true, SmartyPants will be used to convert quotes and dashes to # typographically correct entities. # html_use_smartypants = True # Custom sidebar templates, maps document names to template names. # html_sidebars = {} # Additional templates that should be rendered to pages, maps page names to # template names. # html_additional_pages = {} # If false, no module index is generated. # html_domain_indices = True # If false, no index is generated. # html_use_index = True # If true, the index is split into individual pages for each letter. # html_split_index = False # If true, links to the reST sources are added to the pages. # html_show_sourcelink = True # If true, "Created using Sphinx" is shown in the HTML footer. Default is True. # html_show_sphinx = True # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. # html_show_copyright = True # If true, an OpenSearch description file will be output, and all pages will # contain a tag referring to it. The value of this option must be the # base URL from which the finished HTML is served. # html_use_opensearch = '' # This is the file name suffix for HTML files (e.g. ".xhtml"). # html_file_suffix = None # Output file base name for HTML help builder. htmlhelp_basename = 'oslo.configReleaseNotesdoc' # -- Options for LaTeX output --------------------------------------------- latex_elements = { # The paper size ('letterpaper' or 'a4paper'). # 'papersize': 'letterpaper', # The font size ('10pt', '11pt' or '12pt'). # 'pointsize': '10pt', # Additional stuff for the LaTeX preamble. # 'preamble': '', } # Grouping the document tree into LaTeX files. List of tuples # (source start file, target name, title, # author, documentclass [howto, manual, or own class]). latex_documents = [ ('index', 'oslo.configReleaseNotes.tex', u'oslo.config Release Notes Documentation', u'oslo.config Developers', 'manual'), ] # The name of an image file (relative to this directory) to place at the top of # the title page. # latex_logo = None # For "manual" documents, if this is true, then toplevel headings are parts, # not chapters. # latex_use_parts = False # If true, show page references after internal links. # latex_show_pagerefs = False # If true, show URL addresses after external links. # latex_show_urls = False # Documents to append as an appendix to all manuals. # latex_appendices = [] # If false, no module index is generated. # latex_domain_indices = True # -- Options for manual page output --------------------------------------- # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). man_pages = [ ('index', 'oslo.configreleasenotes', u'oslo.config Release Notes Documentation', [u'oslo.config Developers'], 1) ] # If true, show URL addresses after external links. # man_show_urls = False # -- Options for Texinfo output ------------------------------------------- # Grouping the document tree into Texinfo files. List of tuples # (source start file, target name, title, author, # dir menu entry, description, category) texinfo_documents = [ ('index', 'oslo.configReleaseNotes', u'oslo.config Release Notes Documentation', u'oslo.config Developers', 'oslo.configReleaseNotes', 'An OpenStack library for parsing configuration options from the command' ' line and configuration files.', 'Miscellaneous'), ] # Documents to append as an appendix to all manuals. # texinfo_appendices = [] # If false, no module index is generated. # texinfo_domain_indices = True # How to display URL addresses: 'footnote', 'no', or 'inline'. # texinfo_show_urls = 'footnote' # If true, do not generate a @detailmenu in the "Top" node's menu. # texinfo_no_detailmenu = False # -- Options for Internationalization output ------------------------------ locale_dirs = ['locale/'] oslo.config-5.2.0/releasenotes/source/newton.rst0000666000175100017510000000021613224676052022027 0ustar zuulzuul00000000000000============================= Newton Series Release Notes ============================= .. release-notes:: :branch: origin/stable/newton oslo.config-5.2.0/releasenotes/source/_static/0000775000175100017510000000000013224676374021417 5ustar zuulzuul00000000000000oslo.config-5.2.0/releasenotes/source/_static/.placeholder0000666000175100017510000000000013224676052023663 0ustar zuulzuul00000000000000oslo.config-5.2.0/releasenotes/source/unreleased.rst0000666000175100017510000000014413224676052022644 0ustar zuulzuul00000000000000========================== Unreleased Release Notes ========================== .. release-notes:: oslo.config-5.2.0/releasenotes/source/mitaka.rst0000666000175100017510000000023213224676052021761 0ustar zuulzuul00000000000000=================================== Mitaka Series Release Notes =================================== .. release-notes:: :branch: origin/stable/mitaka oslo.config-5.2.0/releasenotes/source/index.rst0000666000175100017510000000027013224676052021624 0ustar zuulzuul00000000000000=========================== oslo.config Release Notes =========================== .. toctree:: :maxdepth: 1 unreleased pike ocata newton mitaka liberty oslo.config-5.2.0/releasenotes/source/ocata.rst0000666000175100017510000000023013224676052021600 0ustar zuulzuul00000000000000=================================== Ocata Series Release Notes =================================== .. release-notes:: :branch: origin/stable/ocata oslo.config-5.2.0/releasenotes/source/_templates/0000775000175100017510000000000013224676374022126 5ustar zuulzuul00000000000000oslo.config-5.2.0/releasenotes/source/_templates/.placeholder0000666000175100017510000000000013224676052024372 0ustar zuulzuul00000000000000oslo.config-5.2.0/releasenotes/source/pike.rst0000666000175100017510000000021713224676052021446 0ustar zuulzuul00000000000000=================================== Pike Series Release Notes =================================== .. release-notes:: :branch: stable/pike oslo.config-5.2.0/releasenotes/source/liberty.rst0000666000175100017510000000021513224676052022166 0ustar zuulzuul00000000000000============================ Liberty Series Release Notes ============================ .. release-notes:: :branch: origin/stable/liberty oslo.config-5.2.0/releasenotes/notes/0000775000175100017510000000000013224676374017621 5ustar zuulzuul00000000000000oslo.config-5.2.0/releasenotes/notes/add-port_type-8704295c6a56265d.yaml0000666000175100017510000000031113224676052025254 0ustar zuulzuul00000000000000--- features: - Integer and Float now support *min*, *max* and *choices*. Choices must respect *min* and *max* (if provided). - Added Port type as an Integer in the closed interval [0, 65535]. oslo.config-5.2.0/releasenotes/notes/show-deprecated-reason-361a8eb31e05c97e.yaml0000666000175100017510000000026313224676052027261 0ustar zuulzuul00000000000000--- features: - | When a deprecated option is used, the message warning that the option is deprecated now includes the reason for the deprecation, if there is one given.oslo.config-5.2.0/releasenotes/notes/add-reno-71dc832ce29b962f.yaml0000666000175100017510000000007313224676052024416 0ustar zuulzuul00000000000000--- other: - Start using reno for managing release notes.oslo.config-5.2.0/releasenotes/notes/add-default-config-dirs-03340ff6689afe94.yaml0000666000175100017510000000267713224676052027241 0ustar zuulzuul00000000000000--- features: - | Add default config-dir paths if no --config-dir switches are given on the command line. This is similar to the default config-file handling oslo.config already supports. If no --config-dir switches are given, oslo.config searches now in a couple of directories (depending on the given project name) for config file snippets. Non-existing directories are simply skipped. The directories, if no project name is given, are: * ~/${prog}.conf.d/ * /etc/${prog}.conf.d/ Only the first directory is used if that is available. If a project is given, the directories searched is a bit more complicated. 2 directories are searched, first search is for the project related dir: * ~/.${project}/${project}.conf.d/ * ~/${project}.conf.d/ * /etc/${project}/${project}.conf.d/ * /etc/${project}.conf.d/ Then for the program name related configs, the following directories are searched: * ~/.${project}/${prog}.conf.d/ * ~/${prog}.conf.d/ * /etc/${project}/${prog}.conf.d/ * /etc/${prog}.conf.d/ other: - Adding some default config-dirs makes it possible to use config dir snippets also in wsgi environments (like Apache) where it is not easily possible to pass command line parameters to a wsgi app. upgrade: - Similar to 'default_config_files', 'default_config_dirs' is no longer an allowed config key. If that key is used, a ValueError() will be raised. oslo.config-5.2.0/releasenotes/notes/machine-readable-sample-config-e8f8ba43ababcf99.yaml0000666000175100017510000000044413224676052031110 0ustar zuulzuul00000000000000--- features: - | The sample config generator can now generate machine-readable formats of the sample config data. This can be consumed by deployment tools to automatically generate configuration files that contain all of the information in the traditional sample configs. oslo.config-5.2.0/releasenotes/notes/add-HostAddressOpt-6e7e2afe7c7863cb.yaml0000666000175100017510000000115213224676052026524 0ustar zuulzuul00000000000000--- prelude: > Configuration option type of ``HostAddressOpt`` added to accept and validate both IP addresses and hostnames. Please refer to the ``features`` section for more information. features: - Configuration option type of ``HostAddressOpt`` added to accept both valid IP address (IPv4 and IPv6) values as well as hostnames. The ``HostAddressOpt`` will accept both IPv4 and IPv6 addresses and ensure that strict checks are performed on the IP versions. This option type will also accept and accurately validate hostnames ensuring that no invalid IP passes as a valid hostname. oslo.config-5.2.0/releasenotes/notes/support-choice-descriptions-8b2d0c14fbd16b2a.yaml0000666000175100017510000000113613224676105030505 0ustar zuulzuul00000000000000--- features: - | String, Number, Integer, Float and Port now support value-description tuples in the interable provided for the *choice* parameter. Support for value-only definitions is retained. - | StringOpt and PortOpt now support a value-description tuples in the iterable provided for the *choice* parameter. Support for value-only definitions is retained. - | *oslo-config-generator* and the Sphinx extension will now output descriptions for option choices where provided. This will impact tooling that relies on the *yaml* and *json* output of the former. oslo.config-5.2.0/releasenotes/notes/add-float-min-max-b1a2e16301c8435c.yaml0000666000175100017510000000010513224676052026010 0ustar zuulzuul00000000000000--- features: - Added minimum and maximum value limits to FloatOpt.oslo.config-5.2.0/releasenotes/notes/.placeholder0000666000175100017510000000000013224676052022065 0ustar zuulzuul00000000000000oslo.config-5.2.0/.coveragerc0000666000175100017510000000016113224676052016112 0ustar zuulzuul00000000000000[run] branch = True source = oslo_config omit = oslo_config/tests/* [report] ignore_errors = True precision = 2 oslo.config-5.2.0/.testr.conf0000666000175100017510000000032213224676052016056 0ustar zuulzuul00000000000000[DEFAULT] test_command=OS_STDOUT_CAPTURE=1 OS_STDERR_CAPTURE=1 OS_TEST_TIMEOUT=60 ${PYTHON:-python} -m subunit.run discover -t ./ . $LISTOPT $IDOPTION test_id_option=--load-list $IDFILE test_list_option=--list oslo.config-5.2.0/README.rst0000666000175100017510000000163413224676052015466 0ustar zuulzuul00000000000000======================== Team and repository tags ======================== .. image:: https://governance.openstack.org/badges/oslo.config.svg :target: https://governance.openstack.org/reference/tags/index.html .. Change things from this point on ========================== Oslo Configuration Library ========================== .. image:: https://img.shields.io/pypi/v/oslo.config.svg :target: https://pypi.python.org/pypi/oslo.config/ :alt: Latest Version .. image:: https://img.shields.io/pypi/dm/oslo.config.svg :target: https://pypi.python.org/pypi/oslo.config/ :alt: Downloads The Oslo configuration API supports parsing command line arguments and .ini style configuration files. * License: Apache License, Version 2.0 * Documentation: https://docs.openstack.org/oslo.config/latest/ * Source: https://git.openstack.org/cgit/openstack/oslo.config * Bugs: https://bugs.launchpad.net/oslo.config oslo.config-5.2.0/doc/0000775000175100017510000000000013224676374014545 5ustar zuulzuul00000000000000oslo.config-5.2.0/doc/source/0000775000175100017510000000000013224676374016045 5ustar zuulzuul00000000000000oslo.config-5.2.0/doc/source/conf.py0000666000175100017510000000431313224676052017340 0ustar zuulzuul00000000000000# -*- coding: utf-8 -*- import os import sys sys.path.insert(0, os.path.abspath('../..')) # -- General configuration ---------------------------------------------------- # Add any Sphinx extension module names here, as strings. They can be # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom ones. extensions = [ 'sphinx.ext.autodoc', 'openstackdocstheme', 'oslo_config.sphinxconfiggen', 'oslo_config.sphinxext', ] # openstackdocstheme options repository_name = 'openstack/oslo.config' bug_project = 'oslo.config' bug_tag = '' html_last_updated_fmt = '%Y-%m-%d %H:%M' config_generator_config_file = 'config-generator.conf' # autodoc generation is a bit aggressive and a nuisance when doing heavy # text edit cycles. # execute "export SPHINX_DEBUG=1" in your terminal to disable # Add any paths that contain templates here, relative to this directory. # templates_path = [] # The suffix of source filenames. source_suffix = '.rst' # The master toctree document. master_doc = 'index' # General information about the project. project = u'oslo.config' copyright = u'2013, OpenStack Foundation' # If true, '()' will be appended to :func: etc. cross-reference text. add_function_parentheses = True # If true, the current module name will be prepended to all description # unit titles (such as .. function::). add_module_names = True # The name of the Pygments (syntax highlighting) style to use. pygments_style = 'sphinx' # A list of ignored prefixes for module index sorting. modindex_common_prefix = ['oslo_config.'] # -- Options for HTML output -------------------------------------------------- # The theme to use for HTML and HTML Help pages. Major themes that come with # Sphinx are currently 'default' and 'sphinxdoc'. # html_theme_path = ["."] # html_theme = '_theme' # html_static_path = ['static'] html_theme = 'openstackdocs' # Output file base name for HTML help builder. htmlhelp_basename = '%sdoc' % project # Grouping the document tree into LaTeX files. List of tuples # (source start file, target name, title, author, documentclass # [howto/manual]). latex_documents = [ ('index', '%s.tex' % project, '%s Documentation' % project, 'OpenStack Foundation', 'manual'), ] oslo.config-5.2.0/doc/source/configuration/0000775000175100017510000000000013224676374020714 5ustar zuulzuul00000000000000oslo.config-5.2.0/doc/source/configuration/format.rst0000666000175100017510000001345213224676052022736 0ustar zuulzuul00000000000000========================= Configuration file format ========================= OpenStack uses the INI file format for configuration files. An INI file is a simple text file that specifies options as ``key=value`` pairs, grouped into sections. The ``DEFAULT`` section contains most of the configuration options. Lines starting with a hash sign (``#``) are comment lines. For example: .. code-block:: ini [DEFAULT] # Print debugging output (set logging level to DEBUG instead # of default WARNING level). (boolean value) debug = true [database] # The SQLAlchemy connection string used to connect to the # database (string value) connection = mysql+pymysql://keystone:KEYSTONE_DBPASS@controller/keystone Options can have different types for values. The comments in the sample config files always mention these and the tables mention the ``Opt`` value as first item like ``(BoolOpt) Toggle...``. The following types are used by OpenStack: boolean value (``BoolOpt``) Enables or disables an option. The allowed values are ``true`` and ``false``. .. code-block:: ini # Enable the experimental use of database reconnect on # connection lost (boolean value) use_db_reconnect = false floating point value (``FloatOpt``) A floating point number like ``0.25`` or ``1000``. .. code-block:: ini # Sleep time in seconds for polling an ongoing async task # (floating point value) task_poll_interval = 0.5 integer value (``IntOpt``) An integer number is a number without fractional components, like ``0`` or ``42``. .. code-block:: ini # The port which the OpenStack Compute service listens on. # (integer value) compute_port = 8774 IP address (``IPOpt``) An IPv4 or IPv6 address. .. code-block:: ini # Address to bind the server. Useful when selecting a particular network # interface. (ip address value) bind_host = 0.0.0.0 key-value pairs (``DictOpt``) A key-value pairs, also known as a dictionary. The key value pairs are separated by commas and a colon is used to separate key and value. Example: ``key1:value1,key2:value2``. .. code-block:: ini # Parameter for l2_l3 workflow setup. (dict value) l2_l3_setup_params = data_ip_address:192.168.200.99, \ data_ip_mask:255.255.255.0,data_port:1,gateway:192.168.200.1,ha_port:2 list value (``ListOpt``) Represents values of other types, separated by commas. As an example, the following sets ``allowed_rpc_exception_modules`` to a list containing the four elements ``oslo.messaging.exceptions``, ``nova.exception``, ``cinder.exception``, and ``exceptions``: .. code-block:: ini # Modules of exceptions that are permitted to be recreated # upon receiving exception data from an rpc call. (list value) allowed_rpc_exception_modules = oslo.messaging.exceptions,nova.exception multi valued (``MultiStrOpt``) A multi-valued option is a string value and can be given more than once, all values will be used. .. code-block:: ini # Driver or drivers to handle sending notifications. (multi valued) notification_driver = nova.openstack.common.notifier.rpc_notifier notification_driver = ceilometer.compute.nova_notifier port value (``PortOpt``) A TCP/IP port number. Ports can range from 1 to 65535. .. code-block:: ini # Port to which the UDP socket is bound. (port value) # Minimum value: 1 # Maximum value: 65535 udp_port = 4952 string value (``StrOpt``) Strings can be optionally enclosed with single or double quotes. .. code-block:: ini # Enables or disables publication of error events. (boolean value) publish_errors = false # The format for an instance that is passed with the log message. # (string value) instance_format = "[instance: %(uuid)s] " Sections ~~~~~~~~ Configuration options are grouped by section. Most configuration files support at least the following sections: [DEFAULT] Contains most configuration options. If the documentation for a configuration option does not specify its section, assume that it appears in this section. [database] Configuration options for the database that stores the state of the OpenStack service. Substitution ~~~~~~~~~~~~ The configuration file supports variable substitution. After you set a configuration option, it can be referenced in later configuration values when you precede it with a ``$``, like ``$OPTION``. The following example uses the values of ``rabbit_host`` and ``rabbit_port`` to define the value of the ``rabbit_hosts`` option, in this case as ``controller:5672``. .. code-block:: ini # The RabbitMQ broker address where a single node is used. # (string value) rabbit_host = controller # The RabbitMQ broker port where a single node is used. # (integer value) rabbit_port = 5672 # RabbitMQ HA cluster host:port pairs. (list value) rabbit_hosts = $rabbit_host:$rabbit_port To avoid substitution, use ``$$``, it is replaced by a single ``$``. For example, if your LDAP DNS password is ``$xkj432``, specify it, as follows: .. code-block:: ini ldap_dns_password = $$xkj432 The code uses the Python ``string.Template.safe_substitute()`` method to implement variable substitution. For more details on how variable substitution is resolved, see https://docs.python.org/2/library/string.html#template-strings and `PEP 292 `_. Whitespace ~~~~~~~~~~ To include whitespace in a configuration value, use a quoted string. For example: .. code-block:: ini ldap_dns_password='a password with spaces' Define an alternate location for a config file ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Most services and the ``*-manage`` command-line clients load the configuration file. To define an alternate location for the configuration file, pass the ``--config-file CONFIG_FILE`` parameter when you start a service or call a ``*-manage`` command. oslo.config-5.2.0/doc/source/configuration/index.rst0000666000175100017510000000016713224676052022554 0ustar zuulzuul00000000000000===================== Configuration Guide ===================== .. toctree:: :maxdepth: 2 format mutable oslo.config-5.2.0/doc/source/configuration/mutable.rst0000666000175100017510000000165313224676052023077 0ustar zuulzuul00000000000000========================== Changing config at runtime ========================== OpenStack Newton introduces the ability to reload (or 'mutate') certain configuration options at runtime without a service restart. The following projects support this: * Compute (nova) Check individual options to discover if they are mutable. In practice ~~~~~~~~~~~ A common use case is to enable debug logging after a failure. Use the mutable config option called 'debug' to do this (providing ``log_config_append`` has not been set). An admin user may perform the following steps: #. Log onto the compute node. #. Edit the config file (EG ``nova.conf``) and change 'debug' to ``True``. #. Send a SIGHUP signal to the nova process (For example, ``pkill -HUP nova``). A log message will be written out confirming that the option has been changed. If you use a CMS like Ansible, Chef, or Puppet, we recommend scripting these steps through your CMS. oslo.config-5.2.0/doc/source/config-generator.conf0000666000175100017510000000006213224676052022136 0ustar zuulzuul00000000000000[DEFAULT] wrap_width = 79 namespace = oslo.config oslo.config-5.2.0/doc/source/cli/0000775000175100017510000000000013224676374016614 5ustar zuulzuul00000000000000oslo.config-5.2.0/doc/source/cli/index.rst0000666000175100017510000000021713224676052020450 0ustar zuulzuul00000000000000================================ oslo.config Command Line Tools ================================ .. toctree:: :maxdepth: 2 generator oslo.config-5.2.0/doc/source/cli/generator.rst0000666000175100017510000002765413224676052021345 0ustar zuulzuul00000000000000======================= oslo-config-generator ======================= `oslo-config-generator` is a utility for generating sample config files in a variety of formats. Sample config files list all of the available options, along with their help string, type, deprecated aliases and defaults. These sample files can be used as config files for `oslo.config` itself (``ini``) or by configuration management tools (``json``, ``yaml``). .. versionadded:: 1.4.0 .. versionchanged:: 4.3.0 The :option:`oslo-config-generator --format` parameter was added, which allows outputting in additional formats. Usage ----- .. program:: oslo-config-generator .. code-block:: shell oslo-config-generator --namespace [--namespace ...] [--output-file ] [--wrap-width ] [--format ] [--minimal] [--summarize] .. option:: --namespace Option namespace under ``oslo.config.opts`` in which to query for options. .. option:: --output-file Path of the file to write to. :Default: stdout .. option:: --wrap-width The maximum length of help lines. :Default: 70 .. option:: --format Desired format for the output. ``ini`` is the only format that can be used directly with `oslo.config`. ``json`` and ``yaml`` are intended for third-party tools that want to write config files based on the sample config data. For more information, refer to :ref:`machine-readable-configs`. :Choices: ini, json, yaml .. option:: --minimal Generate a minimal required configuration. .. option:: --summarize Only output summaries of help text to config files. Retain longer help text for Sphinx documents. For example, to generate a sample config file for `oslo.messaging` you would run: .. code-block:: shell $ oslo-config-generator --namespace oslo.messaging > oslo.messaging.conf To generate a sample config file for an application ``myapp`` that has its own options and uses `oslo.messaging`, you would list both namespaces: .. code-block:: shell $ oslo-config-generator --namespace myapp \ --namespace oslo.messaging > myapp.conf To generate a sample config file for `oslo.messaging` in `JSON` format, you would run: .. code-block:: shell $ oslo-config-generator --namespace oslo.messaging \ --format json > oslo.messaging.conf Defining Option Discovery Entry Points -------------------------------------- The :option:`oslo-config-generator --namespace` option specifies an entry point name registered under the ``oslo.config.opts`` entry point namespace. For example, in the `oslo.messaging` ``setup.cfg`` we have: .. code-block:: ini [entry_points] oslo.config.opts = oslo.messaging = oslo.messaging.opts:list_opts The callable referenced by the entry point should take no arguments and return a list of ``(group, [opt_1, opt_2])`` tuples, where ``group`` is either a group name as a string or an ``OptGroup`` object. Passing the ``OptGroup`` object allows the consumer of the ``list_opts`` method to access and publish group help. An example, using both styles: .. code-block:: python from oslo_config import cfg opts1 = [ cfg.StrOpt('foo'), cfg.StrOpt('bar'), ] opts2 = [ cfg.StrOpt('baz'), ] baz_group = cfg.OptGroup(name='baz_group' title='Baz group options', help='Baz group help text') cfg.CONF.register_group(baz_group) cfg.CONF.register_opts(opts1, group='blaa') cfg.CONF.register_opts(opts2, group=baz_group) def list_opts(): # Allows the generation of the help text for # the baz_group OptGroup object. No help # text is generated for the 'blaa' group. return [('blaa', opts1), (baz_group, opts2)] .. note:: You should return the original options, not a copy, because the default update hooks depend on the original option object being returned. The module holding the entry point *must* be importable, even if the dependencies of that module are not installed. For example, driver modules that define options but have optional dependencies on third-party modules must still be importable if those modules are not installed. To accomplish this, the optional dependency can either be imported using :func:`oslo.utils.importutils.try_import` or the option definitions can be placed in a file that does not try to import the optional dependency. Modifying Defaults from Other Namespaces ---------------------------------------- Occasionally applications need to override the defaults for options defined in libraries. At runtime this is done using an API within the library. Since the config generator cannot guarantee the order in which namespaces will be imported, we can't ensure that application code can change the option defaults before the generator loads the options from a library. Instead, a separate optional processing hook is provided for applications to register a function to update default values after *all* options are loaded. The hooks are registered in a separate entry point namespace (``oslo.config.opts.defaults``), using the same entry point name as **the application's** ``list_opts()`` function. .. code-block:: ini [entry_points] oslo.config.opts.defaults = keystone = keystone.common.config:update_opt_defaults .. warning:: Never, under any circumstances, register an entry point using a name owned by another project. Doing so causes unexpected interplay between projects within the config generator and will result in failure to generate the configuration file or invalid values showing in the sample. In this case, the name of the entry point for the default override function *must* match the name of one of the entry points defining options for the application in order to be detected and used. Applications that have multiple list_opts functions should use one that is present in the inputs for the config generator where the changed defaults need to appear. For example, if an application defines ``foo.api`` to list the API-related options, and needs to override the defaults in the ``oslo.middleware.cors`` library, the application should register ``foo.api`` under ``oslo.config.opts.defaults`` and point to a function within the application code space that changes the defaults for ``oslo.middleware.cors``. The update function should take no arguments. It should invoke the public :func:`set_defaults` functions in any libraries for which it has option defaults to override, just as the application does during its normal startup process. .. code-block:: python from oslo_log import log def update_opt_defaults(): log.set_defaults( default_log_levels=log.get_default_log_levels() + ['noisy=WARN'], ) .. _machine-readable-configs: Generating Machine Readable Configs ----------------------------------- All deployment tools have to solve a similar problem: how to generate the config files for each service at deployment time. To help with this problem, `oslo-config-generator` can generate machine-readable sample config files that output the same data as the INI files used by `oslo.config` itself, but in a YAML or JSON format that can be more easily consumed by deployment tools. .. important:: The YAML and JSON-formatted files generated by `oslo-config-generator` cannot be used by `oslo.config` itself - they are only for use by other tools. For example, some YAML-formatted output might look like so: .. code-block:: yaml generator_options: config_dir: [] config_file: [] format_: yaml minimal: false namespace: - keystone output_file: null summarize: false wrap_width: 70 options: DEFAULT: help: '' opts: - advanced: false choices: [] default: null deprecated_for_removal: false deprecated_opts: [] deprecated_reason: null deprecated_since: null dest: admin_token help: Using this feature is *NOT* recommended. Instead, use the `keystone-manage bootstrap` command. The value of this option is treated as a "shared secret" that can be used to bootstrap Keystone through the API. This "token" does not represent a user (it has no identity), and carries no explicit authorization (it effectively bypasses most authorization checks). If set to `None`, the value is ignored and the `admin_token` middleware is effectively disabled. However, to completely disable `admin_token` in production (highly recommended, as it presents a security risk), remove `AdminTokenAuthMiddleware` (the `admin_token_auth` filter) from your paste application pipelines (for example, in `keystone-paste.ini`). max: null metavar: null min: null mutable: false name: admin_token namespace: keystone positional: false required: false sample_default: null secret: true short: null type: string value - ... ... deprecated_options: DEFAULT: - name: bind_host replacement_group: eventlet_server replacement_name: public_bind_host where the top-level keys are: ``generator_options`` The options passed to the :program:`oslo-config-generator` tool itself ``options`` All options registered in the provided namespace(s). These are grouped under the ``OptGroup`` they are assigned to which defaults to ``DEFAULT`` if unset. For information on the various attributes of each option, refer to :ref:`option-definitions`. ``deprecated_options`` All **deprecated** options registered in the provided namespace(s). Like ``options``, these options are grouped by ``OptGroup``. Generating Multiple Sample Configs ---------------------------------- A single codebase might have multiple programs, each of which use a subset of the total set of options registered by the codebase. In that case, you can register multiple entry points: .. code-block:: ini [entry_points] oslo.config.opts = nova.common = nova.config:list_common_opts nova.api = nova.config:list_api_opts nova.compute = nova.config:list_compute_opts and generate a config file specific to each program: .. code-block:: shell $ oslo-config-generator --namespace oslo.messaging \ --namespace nova.common \ --namespace nova.api > nova-api.conf $ oslo-config-generator --namespace oslo.messaging \ --namespace nova.common \ --namespace nova.compute > nova-compute.conf To make this more convenient, you can use config files to describe your config files: .. code-block:: shell $ cat > config-generator/api.conf < config-generator/compute.conf <`__. Some OpenStack projects didn't initially use this approach, but most now do. The idea is that having all projects use the same approach is more important than the objections to the approach. Sharing code between projects is great, but by also having projects use the same idioms for stuff like this it makes it much easier for people to work on multiple projects. This debate will probably never completely go away, though. See `this latest discussion in August, 2014 `__ Why are configuration options not part of a library's API? ========================================================== Configuration options are a way for deployers to change the behavior of OpenStack. Applications are not supposed to be aware of the configuration options defined and used within libraries, because the library API is supposed to work transparently no matter which backend is configured. Configuration options in libraries can be renamed, moved, and deprecated just like configuration options in applications. However, if applications are allowed to read or write the configuration options directly, treating them as an API, the option cannot be renamed without breaking the application. Instead, libraries should provide a programmatic API (usually a :func:`set_defaults` function) for setting the defaults for configuration options. For example, this function from ``oslo.log`` lets the caller change the format string and default logging levels: :: def set_defaults(logging_context_format_string=None, default_log_levels=None): """Set default values for the configuration options used by oslo.log.""" # Just in case the caller is not setting the # default_log_level. This is insurance because # we introduced the default_log_level parameter # later in a backwards in-compatible change if default_log_levels is not None: cfg.set_defaults( _options.log_opts, default_log_levels=default_log_levels) if logging_context_format_string is not None: cfg.set_defaults( _options.log_opts, logging_context_format_string=logging_context_format_string) If the name of either option changes, the API of :func:`set_defaults` can be updated to allow both names, and warn if the old one is provided. Using a supported API like this is better than having an application call :func:`set_default` on the configuration object directly, such as: :: cfg.CONF.set_default('default_log_levels', default_log_levels) This form will trigger an error if the logging options are moved out of the default option group into their own section of the configuration file. It will also fail if the ``default_log_levels`` option is not yet registered, or if it is renamed. All of those cases can be protected against with a :func:`set_defaults` function in the library that owns the options. Similarly, code that does not *own* the configuration option definition should not read the option value. An application should never, for example, do something like: :: log_file = cfg.CONF.log_file The type, name, and existence of the ``log_file`` configuration option is subject to change. ``oslo.config`` makes it easy to communicate that change to a deployer in a way that allows their old configuration files to continue to work. It has no mechanism for doing that in application code, however. oslo.config-5.2.0/doc/source/reference/styleguide.rst0000666000175100017510000000613113224676105022706 0ustar zuulzuul00000000000000---------------------------- Style Guide for Help Strings ---------------------------- This document provides style guidance for writing the required help strings for configuration options using the ``oslo.config`` code. The help strings are parsed from the code to appear in sample configuration files, such as ``etc/cinder/cinder.conf`` in the cinder repository. They are also displayed in the `OpenStack Configuration Reference `_. Examples:: cfg.StrOpt('bind_host', default='0.0.0.0', help='IP address to listen on.'), cfg.PortOpt('bind_port', default=9292, help='Port number to listen on.') Style Guide ----------- 1. Use sentence-style capitalization for help strings: Capitalize or uppercase the first character (see the examples above). 2. Only use single spaces, no double spaces. 3. Properly capitalize words. If in doubt check the `OpenStack Glossary `_. 4. End each segment with a period and write complete sentences if possible. Examples:: cfg.StrOpt('osapi_volume_base_URL', default=None, help='Base URL that appears in links to the OpenStack ' 'Block Storage API.') cfg.StrOpt('host', default=socket.gethostname(), help='Name of this node. This can be an opaque identifier. ' 'It is not necessarily a host name, FQDN, or IP address.') 5. Use valid service names and API names. Valid service names include nova, cinder, swift, glance, heat, neutron, trove, ceilometer, horizon, keystone, and marconi. Valid API names include Compute API, Image Service API, Identity Service API, Object Storage API, Block Storage API, Database API, and Networking API. Format ------ 1. For multi-line strings, remember that strings are concatenated directly and thus spaces need to be inserted normally. This document recommends to add the space at the end of a line and not at the beginning. Example:: cfg.BoolOpt('glance_api_ssl_compression', default=False, help='Enables or disables negotiation of SSL layer ' 'compression. In some cases disabling compression ' 'can improve data throughput, such as when high ' 'network bandwidth is available and you use ' 'compressed image formats like qcow2.') 2. It is possible to preformat the multi-line strings to increase readability. Line break characters ``\n`` will be kept as they are used in the help text. Example:: cfg.IntOpt('sync_power_state_interval', default=600, help='Interval to sync power states between the database and ' 'the hypervisor.\n' '\n' '-1: disables the sync \n' ' 0: run at the default rate.\n' '>0: the interval in seconds') oslo.config-5.2.0/doc/source/reference/fixture.rst0000666000175100017510000000016313224676052022216 0ustar zuulzuul00000000000000------------ Test Fixture ------------ .. currentmodule:: oslo_config.fixture .. autoclass:: Config :members: oslo.config-5.2.0/doc/source/reference/builtins.rst0000666000175100017510000000012713224676052022361 0ustar zuulzuul00000000000000================== Built-in Options ================== .. show-options:: oslo.config oslo.config-5.2.0/doc/source/reference/exceptions.rst0000666000175100017510000000115313224676052022711 0ustar zuulzuul00000000000000---------- Exceptions ---------- .. currentmodule:: oslo_config.cfg .. autoexception:: Error .. autoexception:: NotInitializedError .. autoexception:: ArgsAlreadyParsedError .. autoexception:: NoSuchOptError .. autoexception:: NoSuchGroupError .. autoexception:: DuplicateOptError .. autoexception:: RequiredOptError .. autoexception:: TemplateSubstitutionError .. autoexception:: ConfigFilesNotFoundError .. autoexception:: ConfigFilesPermissionDeniedError .. autoexception:: ConfigDirNotFoundError .. autoexception:: ConfigFileParseError .. autoexception:: ConfigFileValueError .. autoexception:: DefaultValueError oslo.config-5.2.0/doc/source/reference/sphinxconfiggen.rst0000666000175100017510000000375413224676052023732 0ustar zuulzuul00000000000000==================================== Sphinx Oslo Sample Config Generation ==================================== Included with oslo.config is a sphinx extension to generate a sample config file at the beginning of each sphinx build. To activate the extension add ``oslo_config.sphinxconfiggen`` to the list of extensions in your sphinx ``conf.py``. Then you just need to use the ``config_generator_config_file`` option to point the config generator at the config file which tells it how to generate the sample config. If one isn't specified or it doesn't point to a real file the sample config file generation will be skipped. To generate multiple files, set ``config_generator_config_file`` to a list of tuples containing the input filename and the base name for the output file. The output value can be ``None``, in which case the name is taken from the input value. The input name can be an full path or a value relative to the documentation source directory. For example:: config_generator_config_file = [ ('../../etc/glance-api.conf', 'api'), ('../../etc/glance-cache.conf', 'cache'), ('../../etc/glance-glare.conf', None), ('../../etc/glance-registry.conf', None), ('../../etc/glance-scrubber.conf', None), ] Produces the output files ``api.conf.sample``, ``cache.conf.sample``, ``glance-glare.conf.sample``, ``glance-registry.conf.sample``, and ``glance-scrubber.conf.sample``. Output File Name ---------------- By default the sphinx plugin will generate the sample config file and name the file ``sample.config``. However, if for whatever reason you'd like the name to be more specific to the project name you can use the ``sample_config_basename`` config option to specify the project name. If it's set the output filename will be that value with a ``.conf.sample`` extension. For example if you set the value to "``nova``" the output filename will be "``nova.conf.sample``". You can also include a subdirectory off of the documentation source directory as part of this value. oslo.config-5.2.0/doc/source/reference/index.rst0000666000175100017510000000042613224676052021641 0ustar zuulzuul00000000000000=================== Using oslo.config =================== .. toctree:: :maxdepth: 2 cfg opts types configopts cfgfilter helpers fixture parser exceptions namespaces styleguide mutable builtins sphinxext sphinxconfiggen faq oslo.config-5.2.0/doc/source/reference/mutable.rst0000666000175100017510000001056513224676052022170 0ustar zuulzuul00000000000000Enabling your project for mutable config ======================================== As of OpenStack Newton, config options can be marked as 'mutable'. This means they can be reloaded (usually via SIGHUP) at runtime, without a service restart. However, each project has to be enabled before this will work and some care needs to be taken over how each option is used before it can safely be marked mutable. .. contents:: Table of Contents :local: Calling mutate_config_files --------------------------- Config mutation is triggered by ``ConfigOpts#mutate_config_files`` being called. Services launched with oslo.service get a signal handler on SIGHUP but by default that calls the older ``ConfigOpts#reload_config_files`` method. To get the new behaviour, we have to pass ``restart_method='mutate'``. For example:: service.ProcessLauncher(CONF, restart_method='mutate') An example patch is here: https://review.openstack.org/#/c/280851 Some projects may call ``reload_config_files`` directly, in this case just change that call to ``mutate_config_files``. If there is no signal handler or you want to trigger reload by a different method, maybe via a web UI or watching a file, just ensure your trigger calls ``mutate_config_files``. Making options mutable-safe --------------------------- When options are mutated, they change in the ConfigOpts object but this will not necessarily affect your service immediately. There are three main cases to deal with: * The option is checked every time * The option is cached on the stack * The option affects state The option is checked every time ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This pattern is already safe. Example code:: while True: progress_timeout = CONF.libvirt.live_migration_progress_timeout completion_timeout = int( CONF.libvirt.live_migration_completion_timeout * data_gb) if libvirt_migrate.should_abort(instance, now, progress_time, progress_timeout, completion_timeout): guest.abort_job() The option is cached on the stack ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Just putting the option value in a local variable is enough to cache it. This is tempting to do with loops. Example code:: progress_timeout = CONF.libvirt.live_migration_progress_timeout completion_timeout = int( CONF.libvirt.live_migration_completion_timeout * data_gb) while True: if libvirt_migrate.should_abort(instance, now, progress_time, progress_timeout, completion_timeout): guest.abort_job() The goal is to check the option exactly once every time it could have an effect. Usually this is as simple as checking it every time, for example by moving the locals into the loop. Example patch: https://review.openstack.org/#/c/319203 Sometimes multiple computations have to be performed using the option values and it's important that the result is consistent. In this case, it's necessary to cache the option values in locals. Example patch: https://review.openstack.org/#/c/319254 The option affects state ^^^^^^^^^^^^^^^^^^^^^^^^ An option value can also be cached, after a fashion, by state - either system or external. For example, the 'debug' option of oslo.log is used to set the default log level on startup. The option is not normally checked again, so if it is mutated, the system state will not reflect the new value of the option. In this case we have to use a *mutate hook*:: def _mutate_hook(conf, fresh): if (None, 'debug') in fresh: if conf.debug: log_root.setLevel(logging.DEBUG) def register_options(conf): ... snip ... conf.register_mutate_hook(_mutate_hook) Mutate hook functions will be passed two positional parameters, 'conf' and 'fresh'. 'conf' is a reference to the updated ConfigOpts object. 'fresh' looks like:: { (group, option_name): (old_value, new_value), ... } for example:: { (None, 'debug'): (False, True), ('libvirt', 'live_migration_progress_timeout'): (50, 75) } Hooks may be called in any order. Each project should register one hook, which does whatever is necessary to apply all the new option values. This hook function could grow very large. For good style, modularise the hook using secondary functions rather than accreting a monolith or registering multiple hooks. Example patch: https://review.openstack.org/#/c/254821/ oslo.config-5.2.0/doc/source/contributor/0000775000175100017510000000000013224676374020417 5ustar zuulzuul00000000000000oslo.config-5.2.0/doc/source/contributor/index.rst0000666000175100017510000000012413224676052022250 0ustar zuulzuul00000000000000============== Contributing ============== .. include:: ../../../CONTRIBUTING.rst oslo.config-5.2.0/setup.cfg0000666000175100017510000000211113224676374015616 0ustar zuulzuul00000000000000[metadata] name = oslo.config author = OpenStack author-email = openstack-dev@lists.openstack.org summary = Oslo Configuration API description-file = README.rst home-page = https://docs.openstack.org/oslo.config/latest/ classifier = Development Status :: 5 - Production/Stable Environment :: OpenStack Intended Audience :: Developers Intended Audience :: Information Technology License :: OSI Approved :: Apache Software License Operating System :: OS Independent Programming Language :: Python Programming Language :: Python :: 2 Programming Language :: Python :: 2.7 Programming Language :: Python :: 3 Programming Language :: Python :: 3.5 [files] packages = oslo_config [global] setup-hooks = pbr.hooks.setup_hook [entry_points] console_scripts = oslo-config-generator = oslo_config.generator:main oslo.config.opts = oslo.config = oslo_config._list_opts:list_opts [build_sphinx] all-files = 1 warning-is-error = 1 source-dir = doc/source build-dir = doc/build [upload_sphinx] upload-dir = doc/build/html [wheel] universal = 1 [egg_info] tag_build = tag_date = 0 oslo.config-5.2.0/setup.py0000666000175100017510000000200613224676052015503 0ustar zuulzuul00000000000000# Copyright (c) 2013 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. # THIS FILE IS MANAGED BY THE GLOBAL REQUIREMENTS REPO - DO NOT EDIT import setuptools # In python < 2.7.4, a lazy loading of package `pbr` will break # setuptools if some other modules registered functions in `atexit`. # solution from: http://bugs.python.org/issue15881#msg170215 try: import multiprocessing # noqa except ImportError: pass setuptools.setup( setup_requires=['pbr>=2.0.0'], pbr=True)