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

from ansiblelint.rules import RulesCollection
from ansiblelint.rules.UseHandlerRatherThanWhenChangedRule import (
    UseHandlerRatherThanWhenChangedRule,
)
from ansiblelint.testing import RunFromText

SUCCESS_TASKS = '''
- name: print helpful error message
  debug:
    var: result
  when: result.failed

- name: do something when hello is output
  debug:
    msg: why isn't this a handler
  when: result.stdout == "hello"

- name: never actually debug
  debug:
    var: result
  when: False

- name: Dont execute this step
  debug:
    msg: "debug message"
  when:
    - false

- name: check when with a list
  debug:
    var: result
  when:
  - conditionA
  - conditionB
'''


FAIL_TASKS = '''
- name: execute command
  command: echo hello
  register: result

- name: this should be a handler
  debug:
    msg: why isn't this a handler
  when: result.changed

- name: this should be a handler 2
  debug:
    msg: why isn't this a handler
  when: result|changed

- name: this should be a handler 3
  debug:
    msg: why isn't this a handler
  when: result.changed == true

- name: this should be a handler 4
  debug:
    msg: why isn't this a handler
  when: result['changed'] == true

- name: this should be a handler 5
  debug:
    msg: why isn't this a handler
  when:
  - result['changed'] == true
  - another_condition
'''


class TestUseHandlerRatherThanWhenChanged(unittest.TestCase):
    collection = RulesCollection()
    collection.register(UseHandlerRatherThanWhenChangedRule())

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

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

    def test_fail(self):
        results = self.runner.run_role_tasks_main(FAIL_TASKS)
        self.assertEqual(5, len(results))