salt icon indicating copy to clipboard operation
salt copied to clipboard

Syntax checking

Open j0nes2k opened this issue 14 years ago • 65 comments

When a salt state contains YAML syntax errors, it is quite hard to find these errors at the moment. It would be great to have a basic syntax check (validation agains a schema) and/or references check (e.g. do the references given in require-statements really exist).

j0nes2k avatar Mar 01 '12 09:03 j0nes2k

Yes, the problem is that when we parse the data it is a data structure and we don't know if it came from YAML or some other source. Also that it needs to be passed through the jinja as well. When you find an issue that was difficult to debug please let us know what the error was and what the solution was so we can make the errors better

thatch45 avatar Mar 01 '12 16:03 thatch45

In general, I think a litle tool that allowed you to render sls files locally and pass in the commands via the command line would be really helpful for developing these modules. This tool could easily try and load the yaml, json or any other format supported in the future.

dcolish avatar Mar 03 '12 17:03 dcolish

Thank you for your input! The main problem is that when I create a state, I write the complete file including package installations, config file copying etc. in one go. And of course there will be simple typing errors, like "reqiure" vs. "require" (just an example). When I try to push this state to my machines, I only get empty responses back (e.g. no "{}", just an empty line). Then I have to comment out parts of my state, check whether the highstate now works, comment out other parts until I find this simple error.

It would be great to have a simple check (like: "method 'reqiure' is not known" or something like that) to make it easier to debug things - or am I missing a built-in solution available in salt already?

j0nes2k avatar Mar 06 '12 07:03 j0nes2k

Ahh, yes, we do need better error checking, and maybe some kind of salt-lint would be nice. I am working towards something that will make this better, and there are some places where error checking is still lacking, we will keep this on the radar!

thatch45 avatar Mar 06 '12 07:03 thatch45

I've experienced the same issues, the current workaround/solution is to run "salt-call -l info state.highstate" on a minion as it always reports compile errors properly even though nothing is returned to the master. The output shows issues like typos and source file location errors. Note that it will only report the first compile errors it breaks on, so if there are a few, it'll be necessary to fix them and try again until it passes cleanly.

halberom avatar Mar 06 '12 09:03 halberom

In git the errors should be returned to the master, and yes, there are a few rounds of errors that Salt can't proceed if there are errors early on.

thatch45 avatar Mar 06 '12 16:03 thatch45

It would be nice to have simple YAML syntax checking for errors. e.g checking colon : , indentation etc.

test                                  # missing colon :
  - arg1
  - arg2

syntax error like above, simply pass thru to minions when running state.highstate and crashes minion (need to restart minion process)

mlister2006 avatar Oct 02 '12 12:10 mlister2006

Yes, I have started on a process for this which is called master compiling, but have been unable to finish it, there has been too much demand in too many places! I will get back to it eventually and make this happen!

thatch45 avatar Oct 02 '12 16:10 thatch45

I would very much like to see some better validation...some bugs are currently extremely hard to track down :\

For example, people new to salt are inclined to believe that the top-level directives in an sls file are a list, not a mapping, so they frequently duplicate keys. eg.

ntpd:
  pkg.installed: []
ntpd:
  service.running:
    - require:
      - pkg: ntpd

This silently fails and is difficult to track down.

Additionally, there doesn't seem to be any validation of state names in the top file? If I mistype a state name in the top file, it just gets dropped without error.

A possible solution might be to have two-stage validation:

  1. Per-data-source validators, eg. yaml, json, etc.
  2. The final parsed in-memory data-structure validator.

alecthomas avatar Jan 09 '13 01:01 alecthomas

A state name gets dropped without an error? Can you give me an example? this should generate an error. My plan for a validator is the masterstate approach, but it has been stagnat for a while

thatch45 avatar Jan 09 '13 17:01 thatch45

I'll try to dig up an example, but in the interim I whipped up a quick YAML + Salt validator.

This has two stages:

  1. YAML structural validation - currently primarily disallowing duplicate keys.
  2. Once the YAML is parsed into a Python dictionary, validate the various Salt structures (top, state and pillar) with voluptuous. This is very broad, and has no knowledge of the module-specific structures.

alecthomas avatar Jan 09 '13 21:01 alecthomas

This looks like it will easily get past many of the rendering issues and validate structure, how would you feel about us including this as a runner?

thatch45 avatar Jan 10 '13 16:01 thatch45

That would be fine with me, please use as much of the code as you want.

I tested this on our salt tree and it works quite well, but I have no easy means of testing it after Jinja expansion, so it explodes on a few files.

And of course, it doesn't do source validation on any of the other data sources: json, etc.

alecthomas avatar Jan 10 '13 16:01 alecthomas

Sounds good, at least it is a good starting point!

thatch45 avatar Jan 10 '13 17:01 thatch45

me too !!!

jolestar avatar Apr 15 '13 10:04 jolestar

This should also consider jinja and python states. Maybe not python states but at least jinja states.

fsniper avatar Jun 27 '13 08:06 fsniper

I've been curious about this for a bit, so I went ahead and figured out a simple setup on travis ci with help from @whiteinge and @thatch45. If your states are open source or you use Travis CI Pro, you're :+1:.

.travis.yml

language: python
python:
- '2.7'

before_install:
  - sudo apt-get update
  - curl -L http://bootstrap.saltstack.org | sudo sh -s -- git develop

install:
  # Copy these states
  - sudo mkdir -p /srv/salt/states
  - sudo cp -r . /srv/salt/states
  - sudo cp .travis/minion /etc/salt/minion
  - sudo service salt-minion restart

  # Additional debug help
  - sudo cat /var/log/salt/*

  # See what kind of travis box you're on
  # to help with making your states compatible with travis
  - sudo salt-call grains.items --local

script:
  - sudo salt-call state.show_highstate --local  --retcode-passthrough

.travis/minion

file_client: local
file_roots:
  base:
    - /srv/salt/states

This setup was designed with the simplistic states that nbviewer uses. More about it in blog form.

You'll also want to load your pillar data onto the Travis minion and possibly run a highstate (careful with this as Travis' environment is likely a bit different from your infrastructure).

rgbkrk avatar Jan 29 '14 23:01 rgbkrk

Not sure if still of use, but I've wasted ~30 minutes trying to find an error in pillar's top.sls (simplified for the sake of example):

base:
  'roles:test':
    - match:grain
    - test

Notice the missing space between match: and grain. My pillar data wasn't available to the minion and I was checking for spelling errors and such, but somehow my brain didn't "see" the missing space. Maybe some sort of warning could be added for such things.

lcosmin avatar Feb 04 '14 07:02 lcosmin

+1

HontoNoRoger avatar Oct 23 '14 09:10 HontoNoRoger

Voluptuous would be great for this. It's small, simple, and crazy straight-forward. We could toss a copy of it in salt/utils/validate/ with no impact. I started work writing a schema for the Salt master config and has worked well for validating the external_auth part. I'm not going to have time to put into that for a little while so if someone else wants to take the first stab, please feel free.

See also #15836.

whiteinge avatar Oct 24 '14 15:10 whiteinge