yaml modules for working towards troubleshooting easement (#61183)

* create a yamllint utils module. as well as a yaml module that will
eventually be yaml tools to help in troubleshooting yaml
functionality.

* included unit tests for the yaml module. also not happy about moving
from __salt__.module.function to __salt__["module.function"] for the
test to work

* added pre_render test and minor correct to heredoc in yamllint

* changelog

* fix tests running in py3.5, also try and fix pre-commit.

* attempt to fix pre-commit

* still trying to fix pre-commit.

* forgot freebsd, added yamllint to freebsd

* update adding versionadded and depends

* fix pre-commit?

* fix pre-commit

* attempt to fix pre-commit again

Co-authored-by: Megan Wilhite <mwilhite@vmware.com>
This commit is contained in:
Thomas Phipps 2022-04-06 11:53:21 -07:00 committed by GitHub
parent cbd689f106
commit 062637a17e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
42 changed files with 398 additions and 1 deletions

1
changelog/61182.added Normal file
View file

@ -0,0 +1 @@
added yamllint utils module and yaml execution modules

View file

@ -542,6 +542,7 @@ execution modules
xfs
xml
xmpp
yaml
yumpkg
zabbix
zcbuildout

View file

@ -0,0 +1,5 @@
salt.modules.yaml
=================
.. automodule:: salt.modules.yaml
:members:

View file

@ -4,5 +4,6 @@
# pylxd>=2.2.5
yamlordereddictloader
pygit2>=1.2.0; python_version >= '3.7'
yamllint
mercurial
hglib

View file

@ -1,4 +1,5 @@
# FreeBSD static CI requirements
pygit2==1.8.0; python_version >= '3.7'
yamllint
mercurial
hglib

View file

@ -9,5 +9,6 @@ ansible
twilio
python-telegram-bot==12.8; python_version <= '3.5'
python-telegram-bot>=13.7; python_version > '3.5'
yamllint
mercurial
hglib

View file

@ -602,6 +602,8 @@ paramiko==2.7.1
# scp
passlib==1.7.2 ; sys_platform != "win32"
# via -r requirements/static/ci/common.in
pathspec==0.9.0
# via yamllint
pathtools==0.1.2
# via watchdog
platformdirs==2.2.0
@ -701,6 +703,7 @@ pyyaml==5.4.1
# clustershell
# junos-eznc
# kubernetes
# yamllint
# yamlordereddictloader
pyzmq==21.0.2 ; python_version >= "3.9"
# via
@ -811,6 +814,8 @@ werkzeug==2.0.3
# pytest-httpserver
xmltodict==0.12.0
# via moto
yamllint==1.26.3
# via -r requirements/static/ci/darwin.in
yamlordereddictloader==0.4.0
# via
# -r requirements/static/ci/darwin.in

View file

@ -605,6 +605,8 @@ paramiko==2.7.2 ; sys_platform != "win32" and sys_platform != "darwin"
# scp
passlib==1.7.4 ; sys_platform != "win32"
# via -r requirements/static/ci/common.in
pathspec==0.9.0
# via yamllint
platformdirs==2.2.0
# via virtualenv
portend==2.4
@ -683,6 +685,7 @@ pyyaml==5.4.1
# clustershell
# junos-eznc
# kubernetes
# yamllint
# yamlordereddictloader
pyzmq==21.0.2 ; python_version >= "3.9"
# via -r requirements/zeromq.txt
@ -810,6 +813,8 @@ werkzeug==2.0.1
# via moto
xmltodict==0.12.0
# via moto
yamllint==1.26.3
# via -r requirements/static/ci/linux.in
yamlordereddictloader==0.4.0
# via junos-eznc
zc.lockfile==1.4

View file

@ -595,6 +595,8 @@ paramiko==2.7.1 ; sys_platform != "win32" and sys_platform != "darwin"
# scp
passlib==1.7.2 ; sys_platform != "win32"
# via -r requirements/static/ci/common.in
pathspec==0.9.0
# via yamllint
pathtools==0.1.2
# via watchdog
platformdirs==2.2.0
@ -693,6 +695,7 @@ pyyaml==5.4.1
# clustershell
# junos-eznc
# kubernetes
# yamllint
# yamlordereddictloader
pyzmq==21.0.2 ; python_version >= "3.9"
# via
@ -802,6 +805,8 @@ werkzeug==2.0.3
# pytest-httpserver
xmltodict==0.12.0
# via moto
yamllint==1.26.3
# via -r requirements/static/ci/freebsd.in
yamlordereddictloader==0.4.0
# via junos-eznc
zc.lockfile==1.4

View file

@ -601,6 +601,8 @@ paramiko==2.7.2 ; sys_platform != "win32" and sys_platform != "darwin"
# scp
passlib==1.7.4 ; sys_platform != "win32"
# via -r requirements/static/ci/common.in
pathspec==0.9.0
# via yamllint
platformdirs==2.2.0
# via virtualenv
portend==2.7.1
@ -682,6 +684,7 @@ pyyaml==5.4.1
# clustershell
# junos-eznc
# kubernetes
# yamllint
# yamlordereddictloader
pyzmq==21.0.2 ; python_version >= "3.9"
# via -r requirements/zeromq.txt
@ -797,6 +800,8 @@ wrapt==1.11.1
# via astroid
xmltodict==0.12.0
# via moto
yamllint==1.26.3
# via -r requirements/static/ci/linux.in
yamlordereddictloader==0.4.0
# via junos-eznc
zc.lockfile==2.0

View file

@ -611,6 +611,8 @@ paramiko==2.7.1 ; sys_platform != "win32" and sys_platform != "darwin"
# scp
passlib==1.7.2 ; sys_platform != "win32"
# via -r requirements/static/ci/common.in
pathspec==0.9.0
# via yamllint
pathtools==0.1.2
# via watchdog
platformdirs==2.2.0
@ -722,6 +724,7 @@ pyyaml==5.4.1
# clustershell
# junos-eznc
# kubernetes
# yamllint
# yamlordereddictloader
pyzmq==21.0.2 ; python_version >= "3.9"
# via
@ -843,6 +846,8 @@ werkzeug==2.0.3
# pytest-httpserver
xmltodict==0.12.0
# via moto
yamllint==1.26.3
# via -r requirements/static/ci/linux.in
yamlordereddictloader==0.4.0
# via junos-eznc
zc.lockfile==1.4

View file

@ -435,3 +435,4 @@ zipp==0.6.0
# The following packages are considered to be unsafe in a requirements file:
# setuptools
yamllint

View file

@ -670,6 +670,8 @@ passlib==1.7.2 ; sys_platform != "win32"
# via -r requirements/static/ci/common.in
pathlib2==2.3.5
# via pytest
pathspec==0.9.0
# via yamllint
pathtools==0.1.2
# via watchdog
platformdirs==2.0.2
@ -783,6 +785,7 @@ pyyaml==5.3.1
# junos-eznc
# kubernetes
# moto
# yamllint
# yamlordereddictloader
pyzmq==18.0.1 ; python_version < "3.9"
# via
@ -924,6 +927,8 @@ wrapt==1.11.1
# via aws-xray-sdk
xmltodict==0.12.0
# via moto
yamllint==1.26.3
# via -r requirements/static/ci/linux.in
yamlordereddictloader==0.4.0
# via junos-eznc
zc.lockfile==1.4

View file

@ -625,6 +625,8 @@ paramiko==2.7.2 ; sys_platform != "win32" and sys_platform != "darwin"
# scp
passlib==1.7.4 ; sys_platform != "win32"
# via -r requirements/static/ci/common.in
pathspec==0.9.0
# via yamllint
platformdirs==2.2.0
# via virtualenv
portend==2.4
@ -703,6 +705,7 @@ pyyaml==5.4.1
# clustershell
# junos-eznc
# kubernetes
# yamllint
# yamlordereddictloader
pyzmq==18.0.1 ; python_version < "3.9"
# via -r requirements/zeromq.txt
@ -836,6 +839,8 @@ werkzeug==2.0.1
# via moto
xmltodict==0.12.0
# via moto
yamllint==1.26.3
# via -r requirements/static/ci/linux.in
yamlordereddictloader==0.4.0
# via junos-eznc
zc.lockfile==1.4

View file

@ -623,6 +623,8 @@ paramiko==2.7.2 ; sys_platform != "win32" and sys_platform != "darwin"
# scp
passlib==1.7.4 ; sys_platform != "win32"
# via -r requirements/static/ci/common.in
pathspec==0.9.0
# via yamllint
platformdirs==2.2.0
# via virtualenv
portend==2.7.1
@ -704,6 +706,7 @@ pyyaml==5.4.1
# clustershell
# junos-eznc
# kubernetes
# yamllint
# yamlordereddictloader
pyzmq==21.0.2 ; python_version < "3.9"
# via -r requirements/zeromq.txt
@ -829,6 +832,8 @@ wrapt==1.11.1
# via astroid
xmltodict==0.12.0
# via moto
yamllint==1.26.3
# via -r requirements/static/ci/linux.in
yamlordereddictloader==0.4.0
# via junos-eznc
zc.lockfile==2.0

View file

@ -631,6 +631,8 @@ paramiko==2.7.1 ; sys_platform != "win32" and sys_platform != "darwin"
# scp
passlib==1.7.2 ; sys_platform != "win32"
# via -r requirements/static/ci/common.in
pathspec==0.9.0
# via yamllint
pathtools==0.1.2
# via watchdog
platformdirs==2.2.0
@ -742,6 +744,7 @@ pyyaml==5.4.1
# clustershell
# junos-eznc
# kubernetes
# yamllint
# yamlordereddictloader
pyzmq==18.0.1 ; python_version < "3.9"
# via
@ -870,6 +873,8 @@ werkzeug==2.0.3
# pytest-httpserver
xmltodict==0.12.0
# via moto
yamllint==1.26.3
# via -r requirements/static/ci/linux.in
yamlordereddictloader==0.4.0
# via junos-eznc
zc.lockfile==1.4

View file

@ -638,6 +638,8 @@ passlib==1.7.2 ; sys_platform != "win32"
# via
# -r requirements/static/ci/common.in
# ciscoconfparse
pathspec==0.9.0
# via yamllint
pathtools==0.1.2
# via watchdog
platformdirs==2.2.0
@ -742,6 +744,7 @@ pyyaml==5.4.1
# junos-eznc
# kubernetes
# napalm
# yamllint
# yamlordereddictloader
pyzmq==18.0.1 ; python_version < "3.9"
# via
@ -866,6 +869,8 @@ werkzeug==2.0.3
# pytest-httpserver
xmltodict==0.12.0
# via moto
yamllint==1.26.3
# via -r requirements/static/ci/darwin.in
yamlordereddictloader==0.4.0
# via
# -r requirements/static/ci/darwin.in

View file

@ -644,6 +644,8 @@ passlib==1.7.4 ; sys_platform != "win32"
# via
# -r requirements/static/ci/common.in
# ciscoconfparse
pathspec==0.9.0
# via yamllint
platformdirs==2.2.0
# via virtualenv
portend==2.4
@ -727,6 +729,7 @@ pyyaml==5.4.1
# junos-eznc
# kubernetes
# napalm
# yamllint
# yamlordereddictloader
pyzmq==18.0.1 ; python_version < "3.9"
# via -r requirements/zeromq.txt
@ -868,6 +871,8 @@ werkzeug==2.0.1
# via moto
xmltodict==0.12.0
# via moto
yamllint==1.26.3
# via -r requirements/static/ci/linux.in
yamlordereddictloader==0.4.0
# via junos-eznc
zc.lockfile==1.4

View file

@ -631,6 +631,8 @@ passlib==1.7.2 ; sys_platform != "win32"
# via
# -r requirements/static/ci/common.in
# ciscoconfparse
pathspec==0.9.0
# via yamllint
pathtools==0.1.2
# via watchdog
platformdirs==2.2.0
@ -734,6 +736,7 @@ pyyaml==5.4.1
# junos-eznc
# kubernetes
# napalm
# yamllint
# yamlordereddictloader
pyzmq==18.0.1 ; python_version < "3.9"
# via
@ -857,6 +860,8 @@ werkzeug==2.0.3
# pytest-httpserver
xmltodict==0.12.0
# via moto
yamllint==1.26.3
# via -r requirements/static/ci/freebsd.in
yamlordereddictloader==0.4.0
# via junos-eznc
zc.lockfile==1.4

View file

@ -642,6 +642,8 @@ passlib==1.7.4 ; sys_platform != "win32"
# via
# -r requirements/static/ci/common.in
# ciscoconfparse
pathspec==0.9.0
# via yamllint
platformdirs==2.2.0
# via virtualenv
portend==2.7.1
@ -728,6 +730,7 @@ pyyaml==5.4.1
# junos-eznc
# kubernetes
# napalm
# yamllint
# yamlordereddictloader
pyzmq==21.0.2 ; python_version < "3.9"
# via -r requirements/zeromq.txt
@ -861,6 +864,8 @@ wrapt==1.11.1
# via astroid
xmltodict==0.12.0
# via moto
yamllint==1.26.3
# via -r requirements/static/ci/linux.in
yamlordereddictloader==0.4.0
# via junos-eznc
zc.lockfile==2.0

View file

@ -645,6 +645,8 @@ passlib==1.7.2 ; sys_platform != "win32"
# via
# -r requirements/static/ci/common.in
# ciscoconfparse
pathspec==0.9.0
# via yamllint
pathtools==0.1.2
# via watchdog
platformdirs==2.2.0
@ -761,6 +763,7 @@ pyyaml==5.4.1
# junos-eznc
# kubernetes
# napalm
# yamllint
# yamlordereddictloader
pyzmq==18.0.1 ; python_version < "3.9"
# via
@ -896,6 +899,8 @@ werkzeug==2.0.3
# pytest-httpserver
xmltodict==0.12.0
# via moto
yamllint==1.26.3
# via -r requirements/static/ci/linux.in
yamlordereddictloader==0.4.0
# via junos-eznc
zc.lockfile==1.4

View file

@ -178,6 +178,8 @@ packaging==19.2
# via pytest
patch==1.16
# via -r requirements/static/ci/windows.in
pathspec==0.9.0
# via yamllint
pathtools==0.1.2
# via watchdog
platformdirs==2.2.0
@ -280,6 +282,7 @@ pyyaml==5.4.1
# -r requirements/base.txt
# clustershell
# kubernetes
# yamllint
pyzmq==18.0.1 ; python_version < "3.9"
# via
# -r requirements/zeromq.txt
@ -376,6 +379,8 @@ xmltodict==0.12.0
# via
# moto
# pywinrm
yamllint==1.26.3
# via -r requirements/static/ci/windows.in
zc.lockfile==2.0
# via cherrypy
zipp==3.5.0

View file

@ -631,6 +631,8 @@ passlib==1.7.2 ; sys_platform != "win32"
# via
# -r requirements/static/ci/common.in
# ciscoconfparse
pathspec==0.9.0
# via yamllint
pathtools==0.1.2
# via watchdog
platformdirs==2.2.0
@ -735,6 +737,7 @@ pyyaml==5.4.1
# junos-eznc
# kubernetes
# napalm
# yamllint
# yamlordereddictloader
pyzmq==19.0.0 ; python_version < "3.9"
# via
@ -857,6 +860,8 @@ werkzeug==2.0.3
# pytest-httpserver
xmltodict==0.12.0
# via moto
yamllint==1.26.3
# via -r requirements/static/ci/darwin.in
yamlordereddictloader==0.4.0
# via
# -r requirements/static/ci/darwin.in

View file

@ -638,6 +638,8 @@ passlib==1.7.4 ; sys_platform != "win32"
# via
# -r requirements/static/ci/common.in
# ciscoconfparse
pathspec==0.9.0
# via yamllint
platformdirs==2.2.0
# via virtualenv
portend==2.4
@ -721,6 +723,7 @@ pyyaml==5.4.1
# junos-eznc
# kubernetes
# napalm
# yamllint
# yamlordereddictloader
pyzmq==19.0.0 ; python_version < "3.9"
# via -r requirements/zeromq.txt
@ -859,6 +862,8 @@ werkzeug==2.0.1
# via moto
xmltodict==0.12.0
# via moto
yamllint==1.26.3
# via -r requirements/static/ci/linux.in
yamlordereddictloader==0.4.0
# via junos-eznc
zc.lockfile==1.4

View file

@ -624,6 +624,8 @@ passlib==1.7.2 ; sys_platform != "win32"
# via
# -r requirements/static/ci/common.in
# ciscoconfparse
pathspec==0.9.0
# via yamllint
pathtools==0.1.2
# via watchdog
platformdirs==2.2.0
@ -727,6 +729,7 @@ pyyaml==5.4.1
# junos-eznc
# kubernetes
# napalm
# yamllint
# yamlordereddictloader
pyzmq==19.0.0 ; python_version < "3.9"
# via
@ -848,6 +851,8 @@ werkzeug==2.0.3
# pytest-httpserver
xmltodict==0.12.0
# via moto
yamllint==1.26.3
# via -r requirements/static/ci/freebsd.in
yamlordereddictloader==0.4.0
# via junos-eznc
zc.lockfile==1.4

View file

@ -636,6 +636,8 @@ passlib==1.7.4 ; sys_platform != "win32"
# via
# -r requirements/static/ci/common.in
# ciscoconfparse
pathspec==0.9.0
# via yamllint
platformdirs==2.2.0
# via virtualenv
portend==2.7.1
@ -722,6 +724,7 @@ pyyaml==5.4.1
# junos-eznc
# kubernetes
# napalm
# yamllint
# yamlordereddictloader
pyzmq==21.0.2 ; python_version < "3.9"
# via -r requirements/zeromq.txt
@ -848,6 +851,8 @@ wrapt==1.11.1
# via astroid
xmltodict==0.12.0
# via moto
yamllint==1.26.3
# via -r requirements/static/ci/linux.in
yamlordereddictloader==0.4.0
# via junos-eznc
zc.lockfile==2.0

View file

@ -638,6 +638,8 @@ passlib==1.7.2 ; sys_platform != "win32"
# via
# -r requirements/static/ci/common.in
# ciscoconfparse
pathspec==0.9.0
# via yamllint
pathtools==0.1.2
# via watchdog
platformdirs==2.2.0
@ -754,6 +756,7 @@ pyyaml==5.4.1
# junos-eznc
# kubernetes
# napalm
# yamllint
# yamlordereddictloader
pyzmq==19.0.0 ; python_version < "3.9"
# via
@ -887,6 +890,8 @@ werkzeug==2.0.3
# pytest-httpserver
xmltodict==0.12.0
# via moto
yamllint==1.26.3
# via -r requirements/static/ci/linux.in
yamlordereddictloader==0.4.0
# via junos-eznc
zc.lockfile==1.4

View file

@ -171,6 +171,8 @@ packaging==19.2
# via pytest
patch==1.16
# via -r requirements/static/ci/windows.in
pathspec==0.9.0
# via yamllint
pathtools==0.1.2
# via watchdog
platformdirs==2.2.0
@ -273,6 +275,7 @@ pyyaml==5.4.1
# -r requirements/base.txt
# clustershell
# kubernetes
# yamllint
pyzmq==19.0.0 ; python_version < "3.9"
# via
# -r requirements/zeromq.txt
@ -365,6 +368,8 @@ xmltodict==0.12.0
# via
# moto
# pywinrm
yamllint==1.26.3
# via -r requirements/static/ci/windows.in
zc.lockfile==2.0
# via cherrypy
zipp==3.5.0

View file

@ -631,6 +631,8 @@ passlib==1.7.2 ; sys_platform != "win32"
# via
# -r requirements/static/ci/common.in
# ciscoconfparse
pathspec==0.9.0
# via yamllint
pathtools==0.1.2
# via watchdog
platformdirs==2.2.0
@ -735,6 +737,7 @@ pyyaml==5.4.1
# junos-eznc
# kubernetes
# napalm
# yamllint
# yamlordereddictloader
pyzmq==21.0.2 ; python_version >= "3.9"
# via
@ -857,6 +860,8 @@ werkzeug==2.0.3
# pytest-httpserver
xmltodict==0.12.0
# via moto
yamllint==1.26.3
# via -r requirements/static/ci/darwin.in
yamlordereddictloader==0.4.0
# via
# -r requirements/static/ci/darwin.in

View file

@ -636,6 +636,8 @@ passlib==1.7.4 ; sys_platform != "win32"
# via
# -r requirements/static/ci/common.in
# ciscoconfparse
pathspec==0.9.0
# via yamllint
platformdirs==2.2.0
# via virtualenv
portend==2.4
@ -719,6 +721,7 @@ pyyaml==5.4.1
# junos-eznc
# kubernetes
# napalm
# yamllint
# yamlordereddictloader
pyzmq==21.0.2 ; python_version >= "3.9"
# via -r requirements/zeromq.txt
@ -857,6 +860,8 @@ werkzeug==2.0.1
# via moto
xmltodict==0.12.0
# via moto
yamllint==1.26.3
# via -r requirements/static/ci/linux.in
yamlordereddictloader==0.4.0
# via junos-eznc
zc.lockfile==1.4

View file

@ -624,6 +624,8 @@ passlib==1.7.2 ; sys_platform != "win32"
# via
# -r requirements/static/ci/common.in
# ciscoconfparse
pathspec==0.9.0
# via yamllint
pathtools==0.1.2
# via watchdog
platformdirs==2.2.0
@ -727,6 +729,7 @@ pyyaml==5.4.1
# junos-eznc
# kubernetes
# napalm
# yamllint
# yamlordereddictloader
pyzmq==21.0.2 ; python_version >= "3.9"
# via
@ -848,6 +851,8 @@ werkzeug==2.0.3
# pytest-httpserver
xmltodict==0.12.0
# via moto
yamllint==1.26.3
# via -r requirements/static/ci/freebsd.in
yamlordereddictloader==0.4.0
# via junos-eznc
zc.lockfile==1.4

View file

@ -634,6 +634,8 @@ passlib==1.7.4 ; sys_platform != "win32"
# via
# -r requirements/static/ci/common.in
# ciscoconfparse
pathspec==0.9.0
# via yamllint
platformdirs==2.2.0
# via virtualenv
portend==2.7.1
@ -720,6 +722,7 @@ pyyaml==5.4.1
# junos-eznc
# kubernetes
# napalm
# yamllint
# yamlordereddictloader
pyzmq==21.0.2 ; python_version >= "3.9"
# via -r requirements/zeromq.txt
@ -846,6 +849,8 @@ wrapt==1.11.1
# via astroid
xmltodict==0.12.0
# via moto
yamllint==1.26.3
# via -r requirements/static/ci/linux.in
yamlordereddictloader==0.4.0
# via junos-eznc
zc.lockfile==2.0

View file

@ -640,6 +640,8 @@ passlib==1.7.2 ; sys_platform != "win32"
# via
# -r requirements/static/ci/common.in
# ciscoconfparse
pathspec==0.9.0
# via yamllint
pathtools==0.1.2
# via watchdog
platformdirs==2.2.0
@ -756,6 +758,7 @@ pyyaml==5.4.1
# junos-eznc
# kubernetes
# napalm
# yamllint
# yamlordereddictloader
pyzmq==21.0.2 ; python_version >= "3.9"
# via
@ -889,6 +892,8 @@ werkzeug==2.0.3
# pytest-httpserver
xmltodict==0.12.0
# via moto
yamllint==1.26.3
# via -r requirements/static/ci/linux.in
yamlordereddictloader==0.4.0
# via junos-eznc
zc.lockfile==1.4

View file

@ -171,6 +171,8 @@ packaging==19.2
# via pytest
patch==1.16
# via -r requirements/static/ci/windows.in
pathspec==0.9.0
# via yamllint
pathtools==0.1.2
# via watchdog
platformdirs==2.2.0
@ -273,6 +275,7 @@ pyyaml==5.4.1
# -r requirements/base.txt
# clustershell
# kubernetes
# yamllint
pyzmq==20.0.0 ; python_version >= "3.9"
# via
# -r requirements/zeromq.txt
@ -365,6 +368,8 @@ xmltodict==0.12.0
# via
# moto
# pywinrm
yamllint==1.26.3
# via -r requirements/static/ci/windows.in
zc.lockfile==2.0
# via cherrypy
zipp==3.5.0

View file

@ -4,4 +4,4 @@ patch
pygit2>=1.2.0; python_version >= '3.7'
sed
pywinrm>=0.4.1
yamllint

56
salt/modules/yaml.py Normal file
View file

@ -0,0 +1,56 @@
"""
Yaml helper module for troubleshooting yaml
.. versionadded:: 3005
:depends: yamllint
"""
import logging
log = logging.getLogger(__name__)
__virtualname__ = "yaml"
def __virtual__():
return __virtualname__
def lint(source, saltenv=None, pre_render=None, **kwargs):
"""
lint the output after detecting a sucsessful render.
:param str source: managed source file
:param str saltenv: the saltenv to use, defaults
to minions enviroment or base if not set
:param str pre_render: The render options passed to
slsutil.renderer other wise file is cached and loaded as stream
CLI Example:
.. code-block:: bash
salt '*' yamllint.lint salt://example/bad_yaml.sls
"""
if saltenv is None:
saltenv = __salt__["config.get"]("saltenv", "base")
if saltenv is None:
saltenv = "base"
if pre_render is None:
cache = __salt__["cp.cache_file"](source, saltenv)
if cache is False:
return (False, "Template was unable to be cached")
with __utils__["files.fopen"](cache, "r") as yaml_stream:
yaml_out = yaml_stream.read(-1)
else:
kwargs.update({"saltenv": saltenv})
yaml_out = __salt__["slsutil.renderer"](
path=source, default_renderer=pre_render, **kwargs
)
return __utils__["yamllint.lint"](yaml_out)

63
salt/utils/yamllint.py Normal file
View file

@ -0,0 +1,63 @@
import logging
import salt.utils.stringutils
HAS_YAMLLINT = True
try:
from yamllint import linter
from yamllint.config import YamlLintConfig
except ImportError:
HAS_YAMLLINT = False
log = logging.getLogger(__name__)
__virtualname__ = "yamllint"
def __virtual__():
if HAS_YAMLLINT:
return __virtualname__
else:
return (False, "YAMLLint Not installed")
def lint(
source,
yamlconf=None,
):
"""
lint yaml and return result.
source (required)
yaml as str
yamlconf (optional)
yamllint config file to use, if not set will default to a salty version of realaxed.
"""
if yamlconf is not None:
conf = YamlLintConfig(file=yamlconf)
else:
yamlconf = """
extends: relaxed
rules:
line-length: { max: 256, level: warning }
empty-lines: disable
empty-values: {forbid-in-block-mappings: false, forbid-in-flow-mappings: true}
trailing-spaces: disable
key-ordering: disable
truthy: {level: warning, check-keys: false }
"""
conf = YamlLintConfig(yamlconf)
yaml_out = salt.utils.stringutils.to_str(source)
problems = []
for problem in linter.run(yaml_out, conf):
problems.append(
{
"line": problem.line,
"column": problem.column,
"level": problem.level,
"comment": problem.message,
}
)
output = {"source": yaml_out, "problems": problems}
return output

View file

@ -0,0 +1,56 @@
from pathlib import Path
import pytest
import salt.loader
import salt.modules.config
import salt.modules.cp
import salt.modules.slsutil
import salt.modules.yaml
import salt.utils.files
import salt.utils.yamllint
from tests.support.mock import MagicMock
@pytest.fixture
def configure_loader_modules(minion_opts):
cached_file = str(Path(__file__).parent / "testyaml.yaml")
return {
salt.modules.yaml: {
"__salt__": {
"config.get": salt.modules.config.get,
"cp.cache_file": MagicMock(
salt.modules.cp.cache_file, autospec=True, return_value=cached_file
),
"slsutil.renderer": MagicMock(
salt.modules.slsutil.renderer,
autospec=True,
return_value="key: value\n",
),
},
"__opts__": minion_opts,
"__utils__": {
"files.fopen": salt.utils.files.fopen,
"yamllint.lint": salt.utils.yamllint.lint,
},
},
salt.modules.config: {
"__opts__": minion_opts,
},
}
def test_lint_yaml():
"""
ensure that we can lint from the yaml lint utils
"""
assert salt.modules.yaml.lint("salt://test/test.sls") == {
"problems": [],
"source": "key:\n value\n",
}
def test_lint_pre_render():
assert salt.modules.yaml.lint("salt://test.test.sls", pre_render="jinja") == {
"problems": [],
"source": "key: value\n",
}

View file

@ -0,0 +1,2 @@
key:
value

View file

@ -0,0 +1,28 @@
---
extends: default
rules:
braces:
level: warning
max-spaces-inside: 1
brackets:
level: warning
max-spaces-inside: 1
colons:
level: warning
commas:
level: warning
comments: disable
comments-indentation: disable
document-start: disable
empty-lines:
level: warning
hyphens:
level: warning
indentation:
level: warning
indent-sequences: consistent
line-length:
max: 4
truthy: disable

View file

@ -0,0 +1,46 @@
from pathlib import Path
import salt.utils.yamllint as yamllint
def test_good_yaml():
good_yaml = "key: value\n"
assert yamllint.lint(good_yaml) == {"source": good_yaml, "problems": []}
def test_bad_yaml():
bad_yaml = "key: value"
assert yamllint.lint(bad_yaml) == {
"source": bad_yaml,
"problems": [
{
"column": 11,
"comment": "no new line character at the end of file (new-line-at-end-of-file)",
"level": "error",
"line": 1,
}
],
}
def test_input_bytes():
good_yaml = "key: 😳\n"
assert yamllint.lint(good_yaml) == {"source": good_yaml, "problems": []}
def test_config():
good_yaml = "key: this line is long according to config\n"
config_file = str(Path(__file__).parent / "relaxed.yaml")
assert yamllint.lint(good_yaml) == {"source": good_yaml, "problems": []}
assert yamllint.lint(good_yaml, config_file) == {
"source": good_yaml,
"problems": [
{
"column": 5,
"comment": "line too long (42 > 4 characters) (line-length)",
"level": "error",
"line": 1,
}
],
}