Przeglądaj źródła

Avoid failure if dhcp_end or dhcp_start isn't set

Since dhcp_start and dhcp_end aren't mandatory option, we have to ensure
the validation doesn't fail when they aren't present.

This change introduce a new "default" param to the "ini" module,
allowing to return a success (with the default value) even if the key
isn't found in the file.

Change-Id: Ibd1e54b2d2748b3bf8d2c1cabad3a9faf832e9b2
(cherry picked from commit f84aeec907)
changes/87/692787/4
Cédric Jeanneret 5 miesięcy temu
rodzic
commit
ffd651f782
3 zmienionych plików z 40 dodań i 8 usunięć
  1. +19
    -6
      library/ini.py
  2. +4
    -2
      roles/ctlplane-ip-range/tasks/main.yml
  3. +17
    -0
      tripleo_validations/tests/library/test_ini.py

+ 19
- 6
library/ini.py Wyświetl plik

@@ -52,7 +52,7 @@ def check_file(path, ignore_missing):
return ''


def get_result(path, section, key):
def get_result(path, section, key, default=None):
'''Get value based on section and key'''

msg = ''
@@ -73,10 +73,16 @@ def get_result(path, section, key):
ret = ReturnValue.OK
return (ret, msg, value)
except ConfigParser.Error:
value = None
msg = "There is no key '{}' under the section '{}' in file {}.".format(
key, section, path)
ret = ReturnValue.KEY_NOT_FOUND
if default:
msg = ("There is no key '{}' under section '{}' in file {}. Using"
" default value '{}'".format(key, section, path, default))
ret = ReturnValue.OK
value = default
else:
value = None
msg = "There is no key '{}' under the section '{}' in file {}.".format(
key, section, path)
ret = ReturnValue.KEY_NOT_FOUND
return (ret, msg, value)


@@ -102,6 +108,11 @@ options:
description:
- Section key to look up
type: str
default:
required: false
description:
- Default value if key isn't found
type: str
ignore_missing_file:
required: false
description:
@@ -124,6 +135,7 @@ def main():
section=dict(required=True, type='str'),
key=dict(required=True, type='str'),
ignore_missing_file=dict(required=False, type='bool'),
default=dict(required=False, type='str'),
))

ini_file_path = module.params.get('path')
@@ -142,8 +154,9 @@ def main():
# Try to parse the result from ini file
section = module.params.get('section')
key = module.params.get('key')
default = module.params.get('default')

ret, msg, value = get_result(ini_file_path, section, key)
ret, msg, value = get_result(ini_file_path, section, key, default)

if ret == ReturnValue.INVALID_FORMAT:
module.fail_json(msg=msg)


+ 4
- 2
roles/ctlplane-ip-range/tasks/main.yml Wyświetl plik

@@ -11,6 +11,7 @@
section: ctlplane-subnet
key: dhcp_start
ignore_missing_file: True
default: "192.0.2.5"
register: dhcp_start

- name: Get dhcp_end value from the undercloud.conf file
@@ -20,10 +21,11 @@
section: ctlplane-subnet
key: dhcp_end
ignore_missing_file: True
default: "192.0.2.24"
register: dhcp_end

- name: Check the size of the DHCP range for overcloud nodes
ip_range:
start: "{{ dhcp_start.value|default('192.0.2.5', true) }}"
end: "{{ dhcp_end.value|default('192.0.2.24', true) }}"
start: "{{ dhcp_start.value }}"
end: "{{ dhcp_end.value }}"
min_size: "{{ ctlplane_iprange_min_size }}"

+ 17
- 0
tripleo_validations/tests/library/test_ini.py Wyświetl plik

@@ -101,6 +101,23 @@ class TestIni(base.TestCase):
"in file {}.").format(tmp_name), msg)
self.assertIsNone(value)

def test_get_result_key_not_found_with_default(self):
'''Test ini when key is not found but has a default'''

tmpfile = self.create_tmp_ini()
tmp_name = os.path.relpath(tmpfile.name)
tmpfile.write(valid_content.encode('utf-8'))
tmpfile.seek(0)
ret, msg, value = validation.get_result(tmp_name, 'section', 'key',
'foo')
tmpfile.close()

self.assertEqual(validation.ReturnValue.OK, ret)
self.assertEqual(("There is no key 'key' under section 'section' "
"in file {}. Using default value '{}'"
).format(tmp_name, 'foo'), msg)
self.assertEqual(value, 'foo')

def test_get_result_ok(self):
'''Test ini when key is not found'''



Ładowanie…
Anuluj
Zapisz