-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from warpnet/pillar-get
Add checks for pillar.get and grains.get
- Loading branch information
Showing
2 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright (c) 2016, Will Thames and contributors | ||
# Copyright (c) 2018, Ansible Project | ||
# Modified work Copyright (c) 2019 Jeffrey Bouter | ||
|
||
from saltlint.linter import SaltLintRule | ||
import re | ||
|
||
|
||
class JinjaPillarGrainsGetFormatRule(SaltLintRule): | ||
id = '211' | ||
shortdesc = 'pillar.get or grains.get should be formatted differently' | ||
description = "pillar.get and grains.get should always be formatted" \ | ||
" like salt['pillar.get']('item') or grains['item1']" | ||
severity = 'HIGH' | ||
tags = ['formatting', 'jinja'] | ||
version_added = 'develop' | ||
|
||
bracket_regex = re.compile(r"{{( |\-|\+)?.(pillar|grains).get\(.+}}") | ||
|
||
def match(self, file, line): | ||
return self.bracket_regex.search(line) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright (c) 2013-2018 Will Thames <will@thames.id.au> | ||
# Copyright (c) 2018 Ansible by Red Hat | ||
# Modified work Copyright (c) 2019 Jeffrey Bouter | ||
|
||
import unittest | ||
|
||
from saltlint.linter import RulesCollection | ||
from saltlint.rules.JinjaPillarGrainsGetFormatRule import JinjaPillarGrainsGetFormatRule | ||
from tests import RunFromText | ||
|
||
|
||
GOOD_STATEMENT_LINE = ''' | ||
example_test: | ||
file.managed: | ||
- name: /etc/test | ||
- user: root | ||
- group: {{ salt['pillar.get']('item') }} test | ||
- something: {{ grains['item'] }} | ||
- content: | | ||
{{ salt['pillar.get']('test') }} | ||
''' | ||
|
||
BAD_STATEMENT_LINE = ''' | ||
example_test: | ||
file.managed: | ||
- name: /etc/test | ||
- user: root | ||
- group: {{ pillar.get('item') }} test | ||
- something: {{ grains.get('item')}} | ||
- content: | | ||
{{ salt['pillar.get']('test') }} | ||
''' | ||
|
||
class TestJinjaPillarGrainsGetFormatRule(unittest.TestCase): | ||
collection = RulesCollection() | ||
|
||
def setUp(self): | ||
self.collection.register(JinjaPillarGrainsGetFormatRule()) | ||
self.runner = RunFromText(self.collection) | ||
|
||
def test_statement_positive(self): | ||
results = self.runner.run_state(GOOD_STATEMENT_LINE) | ||
self.assertEqual(0, len(results)) | ||
|
||
def test_statement_negative(self): | ||
results = self.runner.run_state(BAD_STATEMENT_LINE) | ||
self.assertEqual(2, len(results)) |