Improve volumes format check of yaml-validate.py

Currently, yaml-validate.py cannot detect trailing slashes under
"list_concat" and "if".

  volumes:
    - /example:/example/ # <======= detected

  volumes:
    list_concat:
    - - /example1:/example1/ # <===== not detected
      - /example2:/example2
    - if:
      - {get_param: TestValue}
      - - /example3:/example3/ # <==== not detected
        - /example4:/example4

This fix makes yaml-validate.py detect the these trailing slashes.

Because the current implementation digs "list" twice in one call to
check_volumes(), trailing slashes under "list_concat" are not detected.
Because the current implementation ignores "if", trailing slashes under
"if" are not deleted. check_volumes() doesn't have to ignore "if"
because non-volume-style value is ignored because of IndexError.

Change-Id: I4288738c2c95ab990024845fa43653854e790bd8
(cherry picked from commit d940f58614)
This commit is contained in:
Yamato Tanaka 2022-11-18 10:16:49 +09:00 committed by Takashi Kajinami
parent e675ecb955
commit 346cda59b6
2 changed files with 15 additions and 24 deletions

View File

@ -254,7 +254,7 @@ outputs:
config_image: *mysql_config_image
volumes:
list_concat:
- - /var/lib/mysql:/var/lib/mysql/:rw
- - /var/lib/mysql:/var/lib/mysql:rw
- /var/log/containers/mysql:/var/log/mariadb
- /var/lib/config-data/puppet-generated/mysql/root:/root:rw #provides .my.cnf for puppet, changed on password update
- if:

View File

@ -785,30 +785,21 @@ def validate_ct_volumes(data):
def check_volumes(volumes):
if not volumes:
return 0
result = 0
if isinstance(volumes, list):
for vol in volumes:
if isinstance(vol, dict):
# Avoid 'if', 'get_*' etc
continue
elif isinstance(vol, list):
for item in vol:
result += check_volumes(item)
elif isinstance(vol, str):
vol_def = vol.split(':')
try:
if vol_def[0][-1] == '/' or vol_def[1][-1] == '/':
print('ERROR: trailing "/" detected'
' for {}'.format(vol))
return 1
except IndexError:
# Not a volume definition, ignore it
continue
elif isinstance(volumes, list):
return sum([check_volumes(item) for item in volumes])
elif isinstance(volumes, dict):
# Step into 'list_concat', 'map_*' etc.
for item in volumes.values():
result += check_volumes(item)
return result
return sum([check_volumes(item) for item in volumes.values()])
elif isinstance(volumes, str):
vol_def = volumes.split(':')
try:
if vol_def[0][-1] == '/' or vol_def[1][-1] == '/':
print('ERROR: trailing "/" detected'
' for {}'.format(volumes))
return 1
except IndexError:
# Not a volume definition, ignore it
pass
return 0
if not data:
return 0