Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crons should have an identifier #328

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/rules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Disable recommendation checks using `-x recommendation`

Rule | Description
:-:|:--
[801](recommendations/#801) | Using the `quiet` argument with `cmd.run` is deprecated. Use `output_loglevel: quiet`
[801](recommendations/#801) | cron.present should have an identifier

___

Expand Down
47 changes: 47 additions & 0 deletions saltlint/rules/CronIdRule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2020 Warpnet B.V.

import re
from saltlint.linter.rule import Rule
from saltlint.utils import get_rule_skips_from_text
from saltlint.utils import LANGUAGE_SLS


class CronIdRule(Rule):
id = '801'
shortdesc = 'cron.present should have an identifier'
description = 'cron.present should have an identifier'

severity = 'MEDIUM'
languages = [LANGUAGE_SLS]
tags = ['reccomendation']
version_added = 'v0.9.4'

regex = re.compile(r"""^\s{2}cron\.present:\n(?!^\s{2,4}- identifier:\n)(?:^\s{2,4}- .+\n)+""",
re.MULTILINE)

def matchtext(self, file, text):
results = []

for match in re.finditer(self.regex, text):
# Get the location of the regex match
start = match.start()
end = match.end()

# Get the line number of the last character
lines = text[:end].splitlines()
line_no = len(lines)

# Skip result if noqa for this rule ID is found in section
section = text[start:end]
if self.id in get_rule_skips_from_text(section):
continue

# check if block contains identifier
has_identifier = False
if "\n - identifier:" in section or "\n - identifier:" in section:
has_identifier = True
if not has_identifier:
results.append((line_no, lines[-1], self.shortdesc))

return results
67 changes: 67 additions & 0 deletions tests/unit/TestCronIdRule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2020 Warpnet B.V.

import unittest

from saltlint.linter.collection import RulesCollection
from saltlint.rules.CronIdRule import CronIdRule
from tests import RunFromText


GOOD_CMD_STATE = '''
run_postinstall:
cron.present:
- name: echo hello
- identifier: yes

run_postinstall2:
cron.present:
- name: echo hello
- identifier: yes2

run_do_something:
cron.present:
- name: echo hello
- identifier: yes2
cmd.run:
- name: echo hello

'''

BAD_CMD_STATE = '''
run_postinstall:
cron.present:
- name: echo hello

run_postinstall2:
cron.present:
- name: echo hello

run_do_something:
cron.present:
- name: echo hello
cmd.run:
- name: echo hello

run_do_something:
cron.present:
- name: echo hello
# - identifier: bleh

'''

class TestCmdWaitRecommendRule(unittest.TestCase):
collection = RulesCollection()

def setUp(self):
self.collection.register(CronIdRule())

def test_statement_positive(self):
runner = RunFromText(self.collection)
results = runner.run_state(GOOD_CMD_STATE)
self.assertEqual(0, len(results))

def test_statement_negative(self):
runner = RunFromText(self.collection)
results = runner.run_state(BAD_CMD_STATE)
self.assertEqual(4, len(results))