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