Commit Graph

127 Commits

Author SHA1 Message Date
Hervé Beraud 84478d83f8 Make tests backward compat with cliff' older versions
Change the assertion kind to support all versions of cliff.
The previous related fix 78098e6b18
was only compatible with cliff>3.4.0.

Change-Id: I7ac27919b0d58929b4c975ebb308f33124a6060f
2022-09-29 09:59:31 +02:00
Hervé Beraud 7b838f5c31 Disable a config test if ran under the root user
The `test_conf_file_permission_denied` is designed to test
if a config file can't be read due to missing file permissions.

However if we run unit tests with the root user we will beneficiat
from the root privileges and so this test will fails. This can
be reproduced by running `tox` with `sudo`.

These changes propose to skip this test if root privileges are
detected.

Closes-bug: #1983358

Change-Id: Idd124082265032c30f83242b612d002d7093aa61
2022-08-03 16:47:25 +02:00
Hervé Beraud 78098e6b18 fix CLI mismatch in unit tests
Since cliff > 3.4.0 we can see mismatch errors with unit tests, this
patch fix this error.

Also this patch fix some issues with deprecation messages.

All these issues needs to be fixed together to unlock our gates.

Closes-bug: #1908934
Change-Id: I8396b4f3ba74a9ea023bff2e3027710f16a74af4
2022-08-03 16:47:25 +02:00
Cyril Roelandt 88c831cfad Fix tests for Python3.
Change-Id: Ifbf6f52a312929ac07154db8eeee976b0ab0d46a
2021-08-19 00:42:44 +00:00
dengzhaosen 934fba53e4 Remove references to 'sys.version_info'
We support Python 3.6 as a minimum now, making these checks no-ops.

Change-Id: I668c1bddba29a5e823734b1cad5116ba2fdb5f75
2021-04-25 16:06:39 +08:00
Stephen Finucane 068c13547e tests: Add test for config file priority order
This was documented in the docstring but we had nothing to assert it in
tests. Correct that gap.

Change-Id: I4002e7fe18c43eb5f9ba2eae16022d7da87f790d
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
2021-04-14 13:16:39 +01:00
Moisés Guimarães de Medeiros 05b6a33336 inherit from object is not required for py3
Change-Id: I3f3b1dca78e2e9b2515231ccc6600b64f31039fb
Signed-off-by: Moisés Guimarães de Medeiros <moguimar@redhat.com>
2021-01-12 17:09:25 +00:00
Sean McGinnis 66b3da9689
Use unittest.mock instead of third party mock
Now that we no longer support py27, we can use the standard library
unittest.mock module instead of the third party mock lib.

Change-Id: I2f8067eeb2aec62f098bfbb3770a67f1876ce7a2
Signed-off-by: Sean McGinnis <sean.mcginnis@gmail.com>
2020-03-31 14:40:09 -05:00
Stephen Finucane 81589565ba Switch to hacking 2.x
This bumps the version of flake8 and pycodestyle to something much
newer, which resolves a long-standing warning about nested sets and
allows us to use new fangled features like f-strings if we so choose.

Note that this requires some minor modifications to keep the 'pep8' task
passing.

Change-Id: I3af8eb490345a6152372730ca3e0a6702bf482ea
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
2020-02-12 09:56:58 +00:00
Stephen Finucane 20a7cee3e3 Remove six
We don't need this in a Python 3-only world. We can't remove mock yet
since there's an issue with the stdlib variant in python3.6, but that is
called out.

Change-Id: I9657b1fd4409be90d645175a6e177d7e1d2ebac2
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
2020-02-12 09:56:55 +00:00
Dolph Mathews a84b5d73f1 Support hyphens in positional argument names
Currently, when you specify a positional argument with a hyphen in the
name, oslo.config is not able to retrieve a value back for that
argument, even if one is parsed by argparse, because there's a mismatch
between where oslo.config expects to find the value (in the dest, with
underscores, which positional arguments do not have), versus where
argparse puts it in the namespace (using the name, with hyphens).

Change-Id: Ibc44a880ffddfaeffca682ccf3b34525f3f0fe27
2017-03-30 20:17:16 +00:00
Dolph Mathews 18d1617caa Assume positional arguments are required
The 'positional' keyword specifically applies to oslo.config's argparse
support. Unlike oslo.config, argparse assumes that all positional
arguments are required by default, and you have to explicitly tell it
that a positional argument is optional if you'd like to opt into that
behavior.

This patch adopts that same behavior for oslo.config. When you define an
option to be non-positional (oslo.config's default, designed for config
files), then oslo.config makes that option optional:

However, when you define an option to be positional, oslo.config assumes
that the option is primarily going to be used on the CLI and thus sets
it as required, by default.

This change in behavior has the side effect of allowing argparse to
enforce required arguments on the CLI *while* parsing arguments, instead
of depending on oslo.config to detect the condition *after* argparse has
been allowed to parse "invalid" arguments. argparse correctly raises a
SystemExit in this case, and prints the actual command usage and a "hey,
you forgot this required argument", instead of allowing oslo.config to
dump a backtrace to the CLI with a context-less error message
("context-less" in that no useful CLI usage information is dumped along
with the crash to help you correct the condition).

Change-Id: Ifdc6918444fe72f7e1649483c237cce64b4c72d8
Partial-Bug: 1676989
2017-03-30 18:24:31 +00:00
Dolph Mathews ec84eeda52 Refactor unit tests for CLI usage
Prior to this change, CLI usage was hardcoded into the config opt
fixture used for all tests. That CLI usage described two required
positional arguments that were not actually part of the argument parser
(FOO and BAR), but were instead just made up to have a valid-looking
usage description.

The trouble is that you can't then extend that argument parser with
additional real arguments, and then test the configuration of those
arguments by inspecting the *real* usage and --help output from
argparse. For example, a unit test could not assert whether argparse was
configured correctly by oslo.config, because there's simply no way to
know when the usage was just statically set to the arbitrary values of
"FOO BAR."

This patch moves that specific unit test coverage (overriding usage with
something arbitrary) into a dedicated unit test, while removing the
arbitrary usage from all other unit tests. Instead, those unit tests now
make assertions against the argparser's real configuration, hence
options like --config-dir and --config-file now appear in those tests,
because those options are included in every instance of
cfg.ConfigOpts().

Change-Id: I54ba989768d074a5f24897299c85bd35fa1cbd1a
Related-Bug: 1676989
2017-03-30 17:56:13 +00:00
Dolph Mathews b5f76a2ce8 Unit tests to illustrate positional argument bug
This patch does not provide a fix, but instead serves to illustrate
several use cases where positional arguments do not behave correctly on
the CLI.

Change-Id: Ibdb05066b95a285f6618c861eb4d38465dbf0d02
Related-Bug: 1676989
2018-10-08 11:17:49 +01:00
Chuck Short 7072a7552d Replace assertRaisesRegexp with assertRaisesRegex
This replaces the deprecated (in python 3.2) unittest.TestCase method
assertRaisesRegexp() with assertRaisesRegex().

Change-Id: Iba92ef7a76d9392750a95e3bd603f364de4ec890
Signed-off-by: Chuck Short <chucks@redhat.com>
2018-08-27 09:48:29 -04:00
Zuul 084ac31f4c Merge "Add config_source option" 2018-07-13 16:08:32 +00:00
Doug Hellmann e233fc58da Add config_source option
Define a config_source option that can be used to specify the
alternative sources that require drivers to load configuration
settings.

Co-Authored-By: Moises Guimaraes de Medeiros <moguimar@redhat.com>
Change-Id: Ibd5a6d306bb98d30d973dfe3604dcc0691d2e369
Blueprint: oslo-config-drivers
Signed-off-by: Doug Hellmann <doug@doughellmann.com>
2018-06-25 10:17:12 +02:00
Ben Nemec 5f8b0e0185 Optionally use oslo.log for deprecated opt logging
While we can't add a hard dependency on oslo.log because it uses
oslo.config, in most cases oslo.log will be installed anyway.  In
the interest of being able to make use of features like
fatal_deprecations in oslo.log, let's use it if it's available.

Change-Id: If9499aa6fc28a6b92447b3825d3ca1957cb2255a
2018-06-21 14:50:47 +00:00
Ben Nemec 3a9d653b09 Remove MultiConfigParser
This has been deprecated for quite a while and some of the upcoming
work to be done in oslo.config will be easier if we don't have to
deal with this unused parser too.

The class is still currently in use in networking-cisco, but in
change https://review.openstack.org/554617 we have provided a fix
that removes their reliance on it.  They are planning to remove it
entirely in Rocky and are only testing against a capped version in
Queens so we are not going to wait on them.

Change-Id: Id6ae40311f967e172e45bcb0e61812365a72618b
2018-03-21 14:54:17 +00:00
Doug Hellmann 2fce6fc40a track config file and command line locations for user settings
Keep track of which configuration file a group of settings is in to
try to report that as a detail for user settings.

Track settings from the command line separately from settings in the
configuration files.

It is possible that the configuration file details can be wrong if a
group appears in more than one file. We can be more accurate with this
after we have the backend driver feature implemented, but since most
deployments use a single file so this should be relatively useful.

Change-Id: I04ce72dbc5f676296acfeafc13169177ad815350
Signed-off-by: Doug Hellmann <doug@doughellmann.com>
2018-03-15 10:14:03 -04:00
ChangBo Guo(gcb) aee63832f6 Clean up enforce_type related test method's name
This is follow up of I8a5e868e0adc2c7b6f46f56ee77a8129c34badb9,
need remove enforce_type from test method's name. This commit
also remove test_enforce_type_default_override which is duplicated
with test_default_override.

Change-Id: Ibc7661993247d0510a08c4a890ba7476be8a32b0
2017-11-29 13:18:39 +08:00
ChangBo Guo(gcb) 7e580d30bf Make help message include choices information for StrOpt
Config option `StrOpt` accept choices parameter to indicate
valid values and include comments "# Allowed values:" when generating
sample config file, so it's unnecessary to add allowed values in hep
string of the config option. But it doesn't include choices information
when printing help for the config option. This commit makes consistency
for both of sample config file and help output of command line.

Closes-Bug:1727683

Change-Id: I962e49c81bdf44043a6e1233408a6ee5b883acde
2017-11-01 10:12:38 +08:00
Zuul a3e7f8aa17 Merge "Remove the parameter enforce_type from set_override and set_default" 2017-10-25 17:37:13 +00:00
Sean Pryor 27d781ea45 Remove the parameter enforce_type from set_override and set_default
It's no longer possible to assign a value of a different type in the
config via an override. This behavior was changed in the last release to
default to disallow overrides of different types, and in this release it
is no longer able to be overridden

Change-Id: I8a5e868e0adc2c7b6f46f56ee77a8129c34badb9
2017-10-25 02:44:39 +00:00
James Page 36aded1866 Prefer SNAP_COMMON config files
User provided configuration files in $SNAP_COMMON should always
be preferred over snap provided default config files in $SNAP.

Re-order the target location list in _get_config_dirs to ensure
that files in $SNAP_COMMON are selected over those in $SNAP.

Change-Id: I7cba87ab328edc05b1e6edd1ff512e8fd5192638
2017-10-06 09:03:53 +01:00
Matt Riedemann 0cce0be52a Implement OptGroup.__str__ for log messages
This log message has been around since change
I6e28c25f04273f7def486fadd541babc8cf423cb.

The OptGroup doesn't implement __str__ so the warning
message isn't helpful.

This change implements __str__ to return the group
name so the warning message actually helps people.

Change-Id: Ie6d218e7f6ea846b1f7d7f8452cc07f0be9e32a5
Closes-Bug: #1719032
2017-09-26 10:59:13 -04:00
Doug Hellmann 204faeb1d3 fix an issue with looking up deprecated option names in code
In I6e28c25f04273f7def486fadd541babc8cf423cb we tried to add the
ability to look for a deprecated option using its old name but finding
the value associated with the new name, similar to how the lookup
works when reading values from config files. That patch did not
account for group values that are objects not strings. This fixes the
oversight.

Change-Id: I98e18e88626164b365466ff476125d41cc8641ec
Signed-off-by: Doug Hellmann <doug@doughellmann.com>
2017-07-18 16:24:11 -04:00
Corey Bryant 21e70e28b3 Add snap package paths to default config dirs
With snap packaging (see snapcraft.io) the package is installed into a
read-only squashfs filesystem, which includes the default config. For
example, $SNAP/etc/nova/nova.conf. To override the defaults, a separate
writable directory is used, and this directory is also unique to the snap.
For example, either $SNAP_COMMON/etc/nova/nova.conf, or
$SNAP_COMMON/etc/nova/nova.conf.d/ can be used to override config.

This patch adds these snap directories to the default config paths where
oslo looks for config.

For more details on $SNAP and $SNAP_COMMON please refer to
https://snapcraft.io/docs/reference/env.

Change-Id: I83627e0f215382aedc7b32163e0303b39e8bccf8
Closes-Bug: 1696830
2017-06-14 14:35:33 -04:00
Doug Hellmann 12fd34b963 add deprecation_reason to log message
When we log the warning that an option is deprecated for removal,
include the reason if we have one. Previously the deprecation reason was
only visible in the sample configuration file.

Add some tests for the log messages emitted when deprecated options are
used.

Change-Id: I5e309a3651041580fdf529ff31e18bbd90714f35
Signed-off-by: Doug Hellmann <doug@doughellmann.com>
2017-04-25 11:01:42 -04:00
ChangBo Guo(gcb) 04a78cc1d0 Remove usage of enforce_type=True
We deprecated and changed enforce_type's default value in [1],
need clean usage of enforce_type=True in oslo.config firstly.

This commit also makes enforce_type=True in fixture.

[1] https://review.openstack.org/#/c/328692/

Change-Id: I5b62427495179edb7672c6e5c508037f8b9d0b84
2017-04-11 14:27:53 +08:00
ChangBo Guo(gcb) 3a8ba0f8ea Make set_override/set_default with enforce_type=True by default
Consuming projects usually  don't set enforce_type=True by default,
and that will lead some invalid tests which don't honour Config options'
type and value limits. We began to warn user about the change since Sep,
2016 in I438aeb766d663dbfe5dbd263fd166c25814204e8. This change will
notify consuming project to write correct test cases with config
options.

This commit also fixes violations in oslo.config test:
config option 'namespace' is MultiStrOpt, need pass list
to conf.set_default().

Closes-Bug: #1517839

Change-Id: Ifa552de0a994e40388cbc9f7dbaa55700ca276b0
2017-04-05 14:26:40 +08:00
Jenkins b1cf3141b9 Merge "Handle both - and _ forms of deprecated opts" 2017-03-14 08:05:38 +00:00
Ben Nemec 0c6040ef6b Handle both - and _ forms of deprecated opts
While config file opts cannot have a - in their name, CLI opts can
and the current deprecation code doesn't handle that case properly.
Additionally, because oslo.config has some magic to translate one
to the other it is possible we have existing users relying on the
wrong behavior.  For this reason, let's add both forms of the opt
when adding deprecated opts.  This will allow both to work as
expected.

The config generator is updated to ignore the - form of deprecated
opts since they won't work in a config file anyway.

I'm unsure how to filter the extra opts out of the help text, but
since both forms will work in that case it's a minor cosmetic
issue.

Change-Id: I4d2584b33d97fe00a427b992682d9eac391725a2
Closes-Bug: 1279973
2017-02-28 00:22:21 +00:00
Matthew Treinish 29d3345bb5
Handle getattrs to ConfigOpts for deprecated opts
There are libraries and tools which offer their config interface as
a stable backwards compatible interface both in code and as user
facing interface. A vital part of being able to make changes to this
is to handle deprecation and moving options. While oslo.config offers
a fairly rich deprecation mechanism it only exposes itself on the
user facing side. If a project wishes to deprecate an option for
code consumers who wish to use the config definitions externally they
will break by any change that deprecates an option. This commit fixes
this issue by doing a lookup on the deprecated options as a fallback
on a getattr. This way if an option is not found all the deprecated
options are checked, if a matching deprecated opt is found a warning
is logged and it's value is used.

Change-Id: I6e28c25f04273f7def486fadd541babc8cf423cb
2017-02-22 14:25:30 -05:00
Jenkins 3ffb18ea60 Merge "fix interpolation of None value" 2017-01-09 20:11:05 +00:00
Mehdi Abaakouk 71075cec20 fix interpolation of None value
interpolation use string.Template that always return str.
In most case, the caller convert the value to the option type.
But that doesn't work if the value is None.

This change make it works for None by replacing it by an empty string.

Closes-bug: #1654621
Change-Id: I9e318050d26277df99c9352dca70dc4ef4fbe13b
2017-01-09 12:50:02 +01:00
Alexis Lee e5269392d8 config: Pass description and epilog through
We can already pass a usage string through, we should also be able to
pass the description and epilog through. Often some explanatory text
about how to combine the options productively is very useful.

Change-Id: I0cea0a22284ca8338658534c8c300bb9b7c93dd8
2016-12-15 14:18:56 +00:00
Thomas Bechtold f24b04ea87 Add defaults for config-dir
If no --config-dir switches are given on the command
line, use default directories to search for config
snippets.
This is similar to the default config-file support
oslo.config already includes. It is useful in environments
where command line arguments can not easily be added, like
mod_wsgi Apache envs.

Change-Id: I4df977911539777d1510e8b579375aca5b5f15f4
2016-11-30 06:58:12 +01:00
howardlee 33018edc39 [TrivialFix] Replace 'assertTrue(a in b)' with 'assertIn(a, b)'
Change-Id: I35e5895dc965a2e8cb4bcdf1f316c34b08aeee40
2016-10-19 20:12:05 +08:00
Stephen Finucane 2966f314c6 Add 'schemes' argument to URIOpt
Some configuration options only allow a specific scheme, like 'http'
and 'https'. Enable validation of this at the config file level.

Change-Id: Iedf808f1809845cd230181c6e335f71aad089922
2016-09-14 13:09:15 +01:00
Mehdi Abaakouk 539e54e5fe test: add enforce_type test
This adds a negative test to ensure we do not touch the type of the
overrided value when the application do not provide a valid type. This
ensures that we do break them by accidentally convert the value to the
right type.

Change-Id: Ie00e066fa962dfa8114730113ce581391fad1228
2016-09-08 13:54:45 +02:00
Jenkins 5f26113389 Merge "Ensure test_config_dir_doesnt_exist() dir doesn't exist" 2016-09-07 13:34:11 +00:00
Mehdi Abaakouk fcc344d954 Warn user about enforce_type default change
We plan to change the enforce_type default to True.
https://review.openstack.org/#/c/328692

But this is going to break some project. This change
adds a deprecation warning to help them to track their broken
tests.

This should avoid people to change ton of tests file in all projects for
no real value. Specially for an option that in this end will be removed
from oslo.config when everybody have switched to the enforcing mode.

Related-Bug: #1517839

Change-Id: I438aeb766d663dbfe5dbd263fd166c25814204e8
2016-09-06 18:03:00 +02:00
Corey Bryant 0dad997bca Ensure test_config_dir_doesnt_exist() dir doesn't exist
This ensures that the directory used by test_config_dir_doesnt_exist()
doesn't exist.

Change-Id: I87b4dc4fa6c8edde5d037ff7241c868318c8d177
Closes-Bug: #1619800
2016-09-02 17:51:50 -04:00
Jenkins d2c2b3a611 Merge "Add set_override method test with ListOpt" 2016-08-12 21:49:06 +00:00
Adit Sarfaty 14ec2c67eb Add Port type to allow configuration of a list of tcp/ip ports
There was already a PortOpt, but this is not composable with ListOpt.
The new type inherits from Integer and therefore supports min/max
value and choices. For example, openstack/vmware-nsx wants to use this
to configure a default list of ports for a firewall rule.

Change-Id: I83bbec6add8ce2951e94e69ec14bb6d8137d4f0c
2016-08-11 12:05:59 +03:00
ChangBo Guo(gcb) ae8e56ba4b Add set_override method test with ListOpt
We use method set_override to override ListOpt type option in Nova,
so we'd better include test with this type.

Change-Id: I492b026c16981be50231fb70d702fa27ac794a39
2016-07-31 20:10:23 +08:00
ChangBo Guo(gcb) c11571933e Make error message more clear
This commit adjusts sentence about option name and group in
error messages to make it more clear and friendly.

TrivialFix

Change-Id: I0f23e9e0ff0499509621c5ae7b1fc4573d6037a7
2016-07-25 17:33:57 +08:00
Adit Sarfaty 15d3ab88f2 Add min and max values to Float type and Opt
Just like Integers, Floats configuration should also have a minimum and
maximum possible values.
For example, the vmware-nsx plugin needs it for the QoS support.
See https://review.openstack.org/#/c/344763/

Change-Id: If1c47444e0c12b68d9d9cb645b8251e4462cfd49
2016-07-21 14:38:23 +03:00
yan.haifeng 61224ce932 Fix parameters of assertEqual are misplaced
Many assertEqual sentences don't follow assertEqual(expected, actual),
this patch fix all of them.

Change-Id: Ia2fbad7a952039fbba4cddc3308721a19dd099d1
Closes-Bug: 1604213
2016-07-19 11:52:55 +08:00