summaryrefslogtreecommitdiffstats
path: root/test/TestEnvVarsInCommand.py
blob: e3a1d847ab7a2a73ac79e7776808d0c900f3112a (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
# pylint: disable=preferred-module  # FIXME: remove once migrated per GH-725
import unittest

from ansiblelint.rules import RulesCollection
from ansiblelint.rules.EnvVarsInCommandRule 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
'''


class TestEnvVarsInCommand(unittest.TestCase):
    collection = RulesCollection()
    collection.register(EnvVarsInCommandRule())

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

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

    def test_fail(self):
        results = self.runner.run_playbook(FAIL_PLAY_TASKS)
        self.assertEqual(2, len(results))