Skip to content

Commit

Permalink
Command module bug fix, tests and cleanup. (ansible#30432)
Browse files Browse the repository at this point in the history
* Add more command integration tests.
* Remove unnecessary command test debug tasks.
* Fix traceback in command module for empty args.
* Remove unreachable code.
  • Loading branch information
mattclay authored Sep 15, 2017
1 parent f128796 commit 4ce13e9
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 10 deletions.
7 changes: 1 addition & 6 deletions lib/ansible/modules/commands/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def main():
module.warn("As of Ansible 2.4, the parameter 'executable' is no longer supported with the 'command' module. Not using '%s'." % executable)
executable = None

if args.strip() == '':
if not args or args.strip() == '':
module.fail_json(rc=256, msg="no command given")

if chdir:
Expand Down Expand Up @@ -187,11 +187,6 @@ def main():
endd = datetime.datetime.now()
delta = endd - startd

if out is None:
out = b''
if err is None:
err = b''

result = dict(
cmd=args,
stdout=out.rstrip(b"\r\n"),
Expand Down
67 changes: 63 additions & 4 deletions test/integration/targets/command_shell/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,69 @@
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.

- name: use command to execute sudo
command: sudo -h
register: become

- name: assert become warning was reported
assert:
that:
- "become.warnings | length() == 1"
- "'Consider using' in become.warnings[0]"

- name: use command to execute sudo without warnings
command: sudo -h warn=no
register: become

- name: assert become warning was not reported
assert:
that:
- "'warnings' not in become"

- name: use command to execute tar
command: tar --help
register: tar

- name: assert tar warning was reported
assert:
that:
- "tar.warnings | length() == 1"
- "'Consider using unarchive module rather than running tar' in tar.warnings[0]"

- name: use command to execute chown
command: chown -h
register: chown
ignore_errors: true

- name: assert chown warning was reported
assert:
that:
- "chown.warnings | length() == 1"
- "'Consider using file module with owner rather than running chown' in chown.warnings[0]"

- name: use command with unsupported executable arg
command: ls /dev/null executable=/bogus
register: executable

- name: assert executable warning was reported
assert:
that:
- "executable.stdout == '/dev/null'"
- "executable.warnings | length() == 1"
- "'no longer supported' in executable.warnings[0]"

- name: use command with no command
command: chdir=/
register: no_command
ignore_errors: true

- name: assert executable fails with no command
assert:
that:
- "no_command.failed == true"
- "no_command.msg == 'no command given'"
- "no_command.rc == 256"

- set_fact: output_dir_test={{output_dir}}/test_command_shell

- name: make sure our testing sub-directory does not exist
Expand Down Expand Up @@ -143,8 +206,6 @@
this line is the last line
register: command_result6

- debug: var=command_result6

- name: assert the multiline input was passed correctly
assert:
that:
Expand Down Expand Up @@ -223,8 +284,6 @@
echo "this is a second line"
register: shell_result5

- debug: var=shell_result5

- name: assert the multiline shell command ran as expected
assert:
that:
Expand Down

0 comments on commit 4ce13e9

Please sign in to comment.