summaryrefslogtreecommitdiffstats
path: root/test/TestShellWithoutPipefail.py
blob: c0c85457e6d78984ecfb45708995508dcf81260f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# pylint: disable=preferred-module  # FIXME: remove once migrated per GH-725
import unittest

from ansiblelint.rules import RulesCollection
from ansiblelint.rules.ShellWithoutPipefail import ShellWithoutPipefail
from ansiblelint.testing import RunFromText

FAIL_TASKS = '''
---
- hosts: localhost
  become: no
  tasks:
    - name: pipeline without pipefail
      shell: false | cat

    - name: pipeline with or and pipe, no pipefail
      shell: false || true | cat

    - shell: |
        df | grep '/dev'
'''

SUCCESS_TASKS = '''
---
- hosts: localhost
  become: no
  tasks:
    - name: pipeline with pipefail
      shell: set -o pipefail && false | cat

    - name: pipeline with pipefail, multi-line
      shell: |
        set -o pipefail
        false | cat

    - name: pipeline with pipefail, complex set
      shell: |
        set -e -x -o pipefail
        false | cat

    - name: pipeline with pipefail, complex set
      shell: |
        set -e -x -o pipefail
        false | cat

    - name: pipeline with pipefail, complex set
      shell: |
        set -eo pipefail
        false | cat

    - name: pipeline without pipefail, ignoring errors
      shell: false | cat
      ignore_errors: true

    - name: non-pipeline without pipefail
      shell: "true"

    - name: command without pipefail
      command: "true"

    - name: shell with or
      shell:
        false || true

    - shell: |
        set -o pipefail
        df | grep '/dev'
'''


class TestShellWithoutPipeFail(unittest.TestCase):
    collection = RulesCollection()
    collection.register(ShellWithoutPipefail())

    def setUp(self):
        self.runner = RunFromText(self.collection)

    def test_fail(self):
        results = self.runner.run_playbook(FAIL_TASKS)
        self.assertEqual(3, len(results))

    def test_success(self):
        results = self.runner.run_playbook(SUCCESS_TASKS)
        self.assertEqual(0, len(results))