napalm-automation / napalm-base

Apache License 2.0
32 stars 48 forks source link

napalm_validate fails for strings containing regular expression characters #287

Closed ktbyers closed 7 years ago

ktbyers commented 7 years ago

I was doing some testing and I had this string for the os_version returned by get_facts:

"C880 Software (C880DATA-UNIVERSALK9-M), Version 15.4(2)T1, RELEASE SOFTWARE (fc3)"

It would fail the validation even though I had an exact string match specified. Digging into it shows we have a bug here:

https://github.com/napalm-automation/napalm-base/blob/develop/napalm_base/validate.py#L132

We use the string directly in a regular expression so any regular expression characters will be treated as such.

We could add an re.escape(src) to the code, but then we have the question of...what if you want to specify a regular expression.

So do we want to support both strings and regular expressions here? If so, how do we differentiate between the two in the YAML file.

dbarrosop commented 7 years ago

I'd treat that as a regular expression. Otherwise we are going to go down the rabbit hole quite quickly. Maybe we need to make better emphasis on the documentation.

ktbyers commented 7 years ago

Proposed fix for this:

https://github.com/napalm-automation/napalm-base/pull/288

Try regex first and if that fails, test string equality.

dbarrosop commented 7 years ago

Fixed by #288