blob: d6f43f2c6aef71500ecf2c55df5eba1c6fa4dc2b (
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
# test code for the ping module
# (c) 2014, James Cammarata <jcammarata@ansible.com>
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
- name: Install test requirements
import_role:
name: setup_pexpect
- name: record the test_command file
set_fact: test_command_file={{remote_tmp_dir | expanduser}}/test_command.py
- name: copy script into output directory
copy: src=test_command.py dest={{test_command_file}} mode=0444
- name: record the output file
set_fact: output_file={{remote_tmp_dir}}/foo.txt
- copy:
content: "foo"
dest: "{{output_file}}"
- name: test expect
expect:
command: "{{ansible_python_interpreter}} {{test_command_file}}"
responses:
foo: bar
register: expect_result
- name: assert expect worked
assert:
that:
- "expect_result.changed == true"
- "expect_result.stdout_lines|last == 'foobar'"
- name: test creates option
expect:
command: "{{ansible_python_interpreter}} {{test_command_file}}"
responses:
foo: bar
creates: "{{output_file}}"
register: creates_result
- name: assert when creates is provided command is not run
assert:
that:
- "creates_result.changed == false"
- "'skipped' in creates_result.stdout"
- name: test creates option (missing)
expect:
command: "{{ansible_python_interpreter}} {{test_command_file}}"
responses:
foo: bar
creates: "{{output_file}}.does.not.exist"
register: creates_result
- name: assert when missing creates is provided command is run
assert:
that:
- "creates_result.changed == true"
- "creates_result.stdout_lines|last == 'foobar'"
- name: test removes option
expect:
command: "{{ansible_python_interpreter}} {{test_command_file}}"
responses:
foo: bar
removes: "{{output_file}}"
register: removes_result
- name: assert when removes is provided command is run
assert:
that:
- "removes_result.changed == true"
- "removes_result.stdout_lines|last == 'foobar'"
- name: test removes option (missing)
expect:
command: "{{ansible_python_interpreter}} {{test_command_file}}"
responses:
foo: bar
removes: "{{output_file}}.does.not.exist"
register: removes_result
- name: assert when missing removes is provided command is not run
assert:
that:
- "removes_result.changed == false"
- "'skipped' in removes_result.stdout"
- name: test chdir
expect:
command: "/bin/sh -c 'pwd && sleep 1'"
chdir: "{{remote_tmp_dir}}"
responses:
foo: bar
register: chdir_result
- name: get remote_tmp_dir real path
raw: >
{{ ansible_python_interpreter }} -c 'import os; os.chdir("{{remote_tmp_dir}}"); print(os.getcwd())'
register: remote_tmp_dir_real_path
- name: assert chdir works
assert:
that:
- "'{{chdir_result.stdout | trim}}' == '{{remote_tmp_dir_real_path.stdout | trim}}'"
- name: test timeout option
expect:
command: "sleep 10"
responses:
foo: bar
timeout: 1
ignore_errors: true
register: timeout_result
- name: assert failure message when timeout
assert:
that:
- "timeout_result.msg == 'command exceeded timeout'"
- name: test echo option
expect:
command: "{{ansible_python_interpreter}} {{test_command_file}}"
responses:
foo: bar
echo: true
register: echo_result
- name: assert echo works
assert:
that:
- "echo_result.stdout_lines|length == 7"
- "echo_result.stdout_lines[-2] == 'foobar'"
- "echo_result.stdout_lines[-1] == 'bar'"
- name: test response list
expect:
command: "{{ansible_python_interpreter}} {{test_command_file}} foo foo"
responses:
foo:
- bar
- baz
register: list_result
- name: assert list response works
assert:
that:
- "list_result.stdout_lines|length == 7"
- "list_result.stdout_lines[-2] == 'foobar'"
- "list_result.stdout_lines[-1] == 'foobaz'"
- name: test no remaining responses
expect:
command: "{{ansible_python_interpreter}} {{test_command_file}} foo foo"
responses:
foo:
- bar
register: list_result
ignore_errors: yes
- name: assert no remaining responses
assert:
that:
- "list_result.failed"
- "'No remaining responses' in list_result.msg"
- name: test no command
expect:
command: ""
responses:
foo: bar
register: no_command_result
ignore_errors: yes
- name: assert no command
assert:
that:
- "no_command_result.failed"
- "no_command_result.msg == 'no command given'"
- "no_command_result.rc == 256"
- name: test non-zero return code
expect:
command: "ls /does-not-exist"
responses:
foo: bar
register: non_zero_result
ignore_errors: yes
- name: assert non-zero return code
assert:
that:
- "non_zero_result.failed"
- "non_zero_result.msg == 'non-zero return code'"
|