config option 's parameter choices can check value before using it

Bug #1471168 reported by ChangBo Guo(gcb)
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
OpenStack Compute (nova)
Fix Released
Undecided
ChangBo Guo(gcb)

Bug Description

In oslo.config StrOpt provide parameter choices, that means the option's value must be in the choices, otherewise an exception will be raised. The current exception is NoSuchOptError[1], a more meaningful exception should be raised [2].

In other word, if an exception will be raised if we set wrong value(not in choices). So in Nova code, we don't need check the value if is valid. The check code is duplicated , we can remove them.

[1]https://github.com/openstack/oslo.config/blob/master/oslo_config/cfg.py#L1899
[2]https://bugs.launchpad.net/bugs/1471149

Tags: config
Revision history for this message
ChangBo Guo(gcb) (glongwave) wrote :

an example the usage of choices:
[gcb@localhost my_code]$ cat test_oslo_config.py
import sys
from oslo_config import cfg

if __name__ == '__main__':
    opts = [
        cfg.StrOpt('type',
                   default='kvm-x',
                   choices=('kvm', 'lxc', 'qemu', 'uml', 'xen', 'parallels'),
                   help='Libvirt domain type'),
           ]

    cfg.CONF.register_opts(opts)
    cfg.CONF(sys.argv[1:], project='test', version='0.1', default_config_files=None)
    virt_type = cfg.CONF.type
    print(virt_type)

[gcb@localhost my_code]$ python test_oslo_config.py
> /usr/lib/python2.7/site-packages/oslo_config/types.py(57)__call__()
-> if self.quotes and value:
(Pdb) c
Traceback (most recent call last):
  File "test_oslo_config.py", line 14, in <module>
    virt_type = cfg.CONF.type
  File "/usr/lib/python2.7/site-packages/oslo_config/cfg.py", line 1870, in __getattr__
    raise NoSuchOptError(name)
oslo_config.cfg.NoSuchOptError: no such option: type

I'm also fixing bug https://bugs.launchpad.net/bugs/1471149 , that will result like
[gcb@localhost my_code]$ python test_oslo_config.py
Traceback (most recent call last):
  File "test_oslo_config.py", line 14, in <module>
    virt_type = cfg.CONF.type
  File "/usr/lib/python2.7/site-packages/oslo_config/cfg.py", line 1868, in __getattr__
    return self._get(name)
  File "/usr/lib/python2.7/site-packages/oslo_config/cfg.py", line 2240, in _get
    value = self._do_get(name, group, namespace)
  File "/usr/lib/python2.7/site-packages/oslo_config/cfg.py", line 2296, in _do_get
    return convert(opt.default)
  File "/usr/lib/python2.7/site-packages/oslo_config/cfg.py", line 2272, in convert
    self._substitute(value, group, namespace), opt)
  File "/usr/lib/python2.7/site-packages/oslo_config/cfg.py", line 2341, in _convert_value
    return opt.type(value)
  File "/usr/lib/python2.7/site-packages/oslo_config/types.py", line 68, in __call__
    repr(value)))
ValueError: Valid values are [kvm, lxc, qemu, uml, xen, parallels], but found 'kvm-x'

Changed in nova:
assignee: nobody → ChangBo Guo(gcb) (glongwave)
Changed in nova:
status: New → In Progress
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to nova (master)

Fix proposed to branch: master
Review: https://review.openstack.org/200808

Revision history for this message
OpenStack Infra (hudson-openstack) wrote :

Fix proposed to branch: master
Review: https://review.openstack.org/200858

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to nova (master)

Reviewed: https://review.openstack.org/200808
Committed: https://git.openstack.org/cgit/openstack/nova/commit/?id=6d86a3eab6a1ce735b79bbaab87c8da390e3393c
Submitter: Jenkins
Branch: master

commit 6d86a3eab6a1ce735b79bbaab87c8da390e3393c
Author: ChangBo Guo(gcb) <email address hidden>
Date: Sat Jul 11 17:27:30 2015 +0800

    libvirt: Remove dead code path in method clear_volume

    In oslo.config StrOpt provide parameter choices, that means the option's
    value must be in the choices, otherewise an exception will be raised.
    That means volume_clear always in ('none', 'zero', 'shred'), this commit
    removes the code path which never run.

    Change-Id: I88e1034a391230cc781cd66911381be2fdea046d
    Partial-Bug: #1471168

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Change abandoned on nova (master)

Change abandoned by Michael Still (<email address hidden>) on branch: master
Review: https://review.openstack.org/200858
Reason: This patch has been stalled for quite a while, so I am going to abandon it to keep the code review queue sane. Please restore the change when it is ready for review.

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to nova (master)

Reviewed: https://review.openstack.org/198285
Committed: https://git.openstack.org/cgit/openstack/nova/commit/?id=ae3d970db302b48db90a03f32ffa54aa55d9f0e4
Submitter: Jenkins
Branch: master

commit ae3d970db302b48db90a03f32ffa54aa55d9f0e4
Author: ChangBo Guo(gcb) <email address hidden>
Date: Fri Jul 3 17:51:46 2015 +0800

    libvirt:Remove duplicated check code for config option sysinfo_serial

    In oslo.config StrOpt provide parameter choices, that means the option's
    value must be in the choices, otherewise an exception will be raised.
    The current exception is NoSuchOptError.
    sysinfo_serial_funcs.get(CONF.libvirt.sysinfo_serial) always return
    non-None value, so remove duplicatd check code.

    Change-Id: If9ff88a69a70b86dd72916f5926c3e47516b5e3b
    Partial-Bug: #1471168

Revision history for this message
Markus Zoeller (markus_z) (mzoeller) wrote :

@ChangBo Guo: Is there still work to do for this bug? It looks like there is no code review open which would solve this. Is the bug maybe already solved?

tags: added: config
Revision history for this message
ChangBo Guo(gcb) (glongwave) wrote :

There is no code review for this bug. need close it.

Changed in nova:
status: In Progress → Fix Committed
Revision history for this message
Markus Zoeller (markus_z) (mzoeller) wrote :

As we use the "direct-release" model in Nova we don't use the
"fix release" status for merged bug fixes anymore. I'm setting
this manually to "fix-released" to be consistent.

[1] "[openstack-dev] [release][all] bugs will now close automatically
    when patches merge"; Doug Hellmann; 2015-12-07;
    http://lists.openstack.org/pipermail/openstack-dev/2015-December/081612.html

Changed in nova:
status: Fix Committed → Fix Released
Revision history for this message
Markus Zoeller (markus_z) (mzoeller) wrote :

> we don't use the "fix release" status for merged bug fixes anymore

should be:

we don't use the "fix comitted" status for merged bug fixes anymore

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.