summaryrefslogtreecommitdiffstats
path: root/test/rules/test_inline_env_var.py
blob: 98f337e7d271d2ba4fc0741721d30757a163f443 (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
85
86
87
88
89
90
"""Tests for inline-env-var rule."""
from ansiblelint.rules import RulesCollection
from ansiblelint.rules.inline_env_var import EnvVarsInCommandRule
from ansiblelint.testing import RunFromText

SUCCESS_PLAY_TASKS = """
- hosts: localhost

  tasks:
  - name: Actual use of environment
    shell: echo $HELLO
    environment:
      HELLO: hello

  - name: Use some key-value pairs
    command: chdir=/tmp creates=/tmp/bobbins warn=no touch bobbins

  - name: Commands can have flags
    command: abc --xyz=def blah

  - name: Commands can have equals in them
    command: echo "==========="

  - name: Commands with cmd
    command:
      cmd:
        echo "-------"

  - name: Command with stdin (ansible > 2.4)
    command: /bin/cat
    args:
      stdin: "Hello, world!"

  - name: Use argv to send the command as a list
    command:
      argv:
        - /bin/echo
        - Hello
        - World

  - name: Another use of argv
    command:
    args:
      argv:
        - echo
        - testing

  - name: Environment variable with shell
    shell: HELLO=hello echo $HELLO

  - name: Command with stdin_add_newline (ansible > 2.8)
    command: /bin/cat
    args:
      stdin: "Hello, world!"
      stdin_add_newline: false

  - name: Command with strip_empty_ends (ansible > 2.8)
    command: echo
    args:
      strip_empty_ends: false
"""

FAIL_PLAY_TASKS = """
- hosts: localhost

  tasks:
  - name: Environment variable with command
    command: HELLO=hello echo $HELLO

  - name: Typo some stuff
    command: cerates=/tmp/blah warn=no touch /tmp/blah
"""


def test_success() -> None:
    """Positive test for inline-env-var."""
    collection = RulesCollection()
    collection.register(EnvVarsInCommandRule())
    runner = RunFromText(collection)
    results = runner.run_playbook(SUCCESS_PLAY_TASKS)
    assert len(results) == 0


def test_fail() -> None:
    """Negative test for inline-env-var."""
    collection = RulesCollection()
    collection.register(EnvVarsInCommandRule())
    runner = RunFromText(collection)
    results = runner.run_playbook(FAIL_PLAY_TASKS)
    assert len(results) == 2