summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkkjang <kevinjang7@gmail.com>2017-05-23 15:51:46 -0700
committerToshio Kuratomi <a.badger@gmail.com>2017-05-23 15:54:42 -0700
commit8611899124b9436d927abbc015220cbdc5409c13 (patch)
tree5543338e7c913185564b658c658ccc259e53cb14
parente5c73a73d4966ef7da6519665a755ba8ef5b65ae (diff)
downloadansible-temp-staging-post-2.3.1.tar.gz
Fix expect for python 3 (#24912)temp-staging-post-2.3.1
* Fix expect for python 3 - Change generator next to python 3 compatible - Added tests for expect * Add pexpect to integration.txt - add pexpect library to requirements for integration tests * Use ansible_python_interpreter in integration tests for expect * Use double-quotes for expect integration tests * Cast user input to string for expect integration tests * Cast user input to string earlier in expect integration tests * Use ansible.module_utils.six.moves input for expect integration tests * Fix yamllint errors in the expect test * Use cat to trigger timeout for expect integration tests * Use realpath filter in expect integration tests (cherry picked from commit daada2000c01641f016f223665894ea595957cc6)
-rw-r--r--lib/ansible/modules/commands/expect.py2
-rw-r--r--test/integration/targets/expect/aliases1
-rw-r--r--test/integration/targets/expect/files/foo.txt1
-rw-r--r--test/integration/targets/expect/files/test_command.py3
-rw-r--r--test/integration/targets/expect/tasks/main.yml96
-rw-r--r--test/runner/requirements/integration.txt1
6 files changed, 103 insertions, 1 deletions
diff --git a/lib/ansible/modules/commands/expect.py b/lib/ansible/modules/commands/expect.py
index 5573d8f46a..405fee8904 100644
--- a/lib/ansible/modules/commands/expect.py
+++ b/lib/ansible/modules/commands/expect.py
@@ -116,7 +116,7 @@ def response_closure(module, question, responses):
def wrapped(info):
try:
- return resp_gen.next()
+ return next(resp_gen)
except StopIteration:
module.fail_json(msg="No remaining responses for '%s', "
"output was '%s'" %
diff --git a/test/integration/targets/expect/aliases b/test/integration/targets/expect/aliases
new file mode 100644
index 0000000000..7af8b7f05b
--- /dev/null
+++ b/test/integration/targets/expect/aliases
@@ -0,0 +1 @@
+posix/ci/group2
diff --git a/test/integration/targets/expect/files/foo.txt b/test/integration/targets/expect/files/foo.txt
new file mode 100644
index 0000000000..7c6ded14ec
--- /dev/null
+++ b/test/integration/targets/expect/files/foo.txt
@@ -0,0 +1 @@
+foo.txt
diff --git a/test/integration/targets/expect/files/test_command.py b/test/integration/targets/expect/files/test_command.py
new file mode 100644
index 0000000000..a1691ea9be
--- /dev/null
+++ b/test/integration/targets/expect/files/test_command.py
@@ -0,0 +1,3 @@
+from ansible.module_utils.six.moves import input
+user_input = input('foo')
+print(user_input)
diff --git a/test/integration/targets/expect/tasks/main.yml b/test/integration/targets/expect/tasks/main.yml
new file mode 100644
index 0000000000..b27ca5bc0f
--- /dev/null
+++ b/test/integration/targets/expect/tasks/main.yml
@@ -0,0 +1,96 @@
+# 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: record the test_command file
+ set_fact: test_command_file={{output_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={{output_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 == 'foobar'"
+
+- name: test creates option
+ expect:
+ command: "echo"
+ 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"
+
+- name: test chdir
+ expect:
+ command: "pwd"
+ chdir: "{{output_dir}}"
+ responses:
+ foo: bar
+ register: chdir_result
+
+- name: assert chdir works
+ assert:
+ that:
+ - "chdir_result.stdout == '{{output_dir | expanduser | realpath}}'"
+
+- name: test timeout option
+ expect:
+ command: "cat"
+ responses:
+ foo: bar
+ timeout: 0
+ 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:
+ - "'bar' in echo_result.stdout_lines"
diff --git a/test/runner/requirements/integration.txt b/test/runner/requirements/integration.txt
index 84ca1ec94a..478d15a13b 100644
--- a/test/runner/requirements/integration.txt
+++ b/test/runner/requirements/integration.txt
@@ -4,5 +4,6 @@ junit-xml
ordereddict ; python_version < '2.7'
paramiko
passlib
+pexpect
pycrypto
pyyaml