Adding first pass at win_copy, win_file and win_template modules.

This commit is contained in:
root 2014-11-24 18:03:32 +00:00 committed by Jon Hawkesworth
commit bf916fb58a
27 changed files with 1387 additions and 5 deletions

View file

@ -1,4 +1,5 @@
---
win_output_dir: 'C:/temp/'
output_dir: ~/ansible_testing
non_root_test_user: ansible
pip_test_package: epdb

View file

@ -0,0 +1,30 @@
# test code for the windows versions of copy, file and template module
# originally
# (c) 2014, Michael DeHaan <michael.dehaan@gmail.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: clean out the test directory
win_file: name={{win_output_dir|mandatory}} state=absent
tags:
- prepare
- name: create the test directory
win_file: name={{win_output_dir}} state=directory
tags:
- prepare

View file

@ -0,0 +1 @@
foo.txt

View file

@ -0,0 +1 @@
baz

View file

@ -0,0 +1 @@
baz

View file

@ -0,0 +1,3 @@
dependencies:
- prepare_win_tests

View file

@ -0,0 +1,259 @@
# test code for the copy module and action plugin
# (c) 2014, Michael DeHaan <michael.dehaan@gmail.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 output directory
set_fact: output_file={{win_output_dir}}/foo.txt
- name: initiate a basic copy
#- name: initiate a basic copy, and also test the mode
# win_copy: src=foo.txt dest={{output_file}} mode=0444
win_copy: src=foo.txt dest={{output_file}}
register: copy_result
- debug: var=copy_result
#- name: check the presence of the output file
- name: check the mode of the output file
win_file: name={{output_file}} state=file
register: file_result_check
- debug: var=file_result_check
#- name: assert the mode is correct
# assert:
# that:
# - "file_result_check.mode == '0444'"
- name: assert basic copy worked
assert:
that:
- "'changed' in copy_result"
# - "'dest' in copy_result"
# - "'group' in copy_result"
# - "'gid' in copy_result"
- "'checksum' in copy_result"
# - "'owner' in copy_result"
# - "'size' in copy_result"
# - "'src' in copy_result"
# - "'state' in copy_result"
# - "'uid' in copy_result"
- name: verify that the file was marked as changed
assert:
that:
- "copy_result.changed == true"
- name: verify that the file checksum is correct
assert:
that:
- "copy_result.checksum[0] == 'c47397529fe81ab62ba3f85e9f4c71f2'"
- name: check the stat results of the file
win_stat: path={{output_file}}
register: stat_results
- name: assert the stat results are correct
assert:
that:
- "stat_results.stat.exists == true"
# - "stat_results.stat.isblk == false"
# - "stat_results.stat.isfifo == false"
# - "stat_results.stat.isreg == true"
# - "stat_results.stat.issock == false"
- "stat_results.stat.md5[0] == 'c47397529fe81ab62ba3f85e9f4c71f2'"
- name: overwrite the file via same means
win_copy: src=foo.txt dest={{output_file}}
register: copy_result2
- name: assert that the file was not changed
assert:
that:
- "not copy_result2|changed"
# content system not available in win_copy right now
#- name: overwrite the file using the content system
# win_copy: content="modified" dest={{output_file}}
# register: copy_result3
#
#- name: assert that the file has changed
# assert:
# that:
# - "copy_result3|changed"
# - "'content' not in copy_result3"
# test recursive copy
- name: set the output subdirectory
set_fact: output_subdir={{win_output_dir}}/sub/
- name: make an output subdirectory
win_file: name={{output_subdir}} state=directory
- name: test recursive copy to directory
# win_copy: src=subdir dest={{output_subdir}} directory_mode=0700
win_copy: src=subdir dest={{output_subdir}}
register: recursive_copy_result
- debug: var=recursive_copy_result
- name: check that a file in a directory was transferred
win_stat: path={{win_output_dir}}/sub/subdir/bar.txt
register: stat_bar
- name: check that a file in a deeper directory was transferred
win_stat: path={{win_output_dir}}/sub/subdir/subdir2/baz.txt
register: stat_bar2
- name: check that a file in a directory whose parent contains a directory alone was transferred
win_stat: path={{win_output_dir}}/sub/subdir/subdir2/subdir3/subdir4/qux.txt
register: stat_bar3
- name: assert recursive copy things
assert:
that:
- "stat_bar.stat.exists"
- "stat_bar2.stat.exists"
- "stat_bar3.stat.exists"
- name: stat the recursively copied directories
win_stat: path={{win_output_dir}}/sub/{{item}}
register: dir_stats
with_items:
- "subdir"
- "subdir/subdir2"
- "subdir/subdir2/subdir3"
- "subdir/subdir2/subdir3/subdir4"
# can't check file mode on windows so commenting this one out.
#- name: assert recursive copied directories mode
# assert:
# that:
# - "{{item.stat.mode}} == 0700"
# with_items: dir_stats.results
# errors on this aren't presently ignored so this test is commented out. But it would be nice to fix.
#
# content param not available in win_copy
#- name: overwrite the file again using the content system, also passing along file params
# win_copy: content="modified" dest={{output_file}}
# register: copy_result4
#- name: assert invalid copy input location fails
# win_copy: src=invalid_file_location_does_not_exist dest={{win_output_dir}}/file.txt
# ignore_errors: True
# register: failed_copy
# owner not available in win_copy, commenting out
#- name: copy already copied directory again
# win_copy: src=subdir dest={{output_subdir | expanduser}} owner={{ansible_ssh_user}}
# register: copy_result5
#- name: assert that the directory was not changed
# assert:
# that:
# - "not copy_result5|changed"
# content not available in win_copy, commenting out.
# issue 8394
#- name: create a file with content and a literal multiline block
# win_copy: |
# content='this is the first line
# this is the second line
#
# this line is after an empty line
# this line is the last line
# '
# dest={{win_output_dir}}/multiline.txt
# register: copy_result6
#- debug: var=copy_result6
#- name: assert the multiline file was created correctly
# assert:
# that:
# - "copy_result6.changed"
# - "copy_result6.dest == '{{win_output_dir|expanduser}}/multiline.txt'"
# - "copy_result6.checksum == '1627d51e7e607c92cf1a502bf0c6cce3'"
# test overwriting a file as an unprivileged user (pull request #8624)
# this can't be relative to {{win_output_dir}} as ~root usually has mode 700
#- name: create world writable directory
#win_file: dest=/tmp/worldwritable state=directory mode=0777
#- name: create world writable file
# win_copy: dest=/tmp/worldwritable/file.txt content="bar" mode=0666
#- name: overwrite the file as user nobody
# win_copy: dest=/tmp/worldwritable/file.txt content="baz"
# sudo: yes
# sudo_user: nobody
# register: copy_result7
#- name: assert the file was overwritten
# assert:
# that:
# - "copy_result7.changed"
# - "copy_result7.dest == '/tmp/worldwritable/file.txt'"
# - "copy_result7.checksum == '73feffa4b7f6bb68e44cf984c85f6e88'"
#- name: clean up
# win_file: dest=/tmp/worldwritable state=absent
# test overwritting a link using "follow=yes" so that the link
# is preserved and the link target is updated
#- name: create a test file to symlink to
# win_copy: dest={{win_output_dir}}/follow_test content="this is the follow test file\n"
#
#- name: create a symlink to the test file
# win_file: path={{win_output_dir}}/follow_link src='./follow_test' state=link
#
#- name: update the test file using follow=True to preserve the link
# win_copy: dest={{win_output_dir}}/follow_link content="this is the new content\n" follow=yes
# register: replace_follow_result
#- name: stat the link path
# win_stat: path={{win_output_dir}}/follow_link
# register: stat_link_result
#
#- name: assert that the link is still a link
# assert:
# that:
# - stat_link_result.stat.islnk
#
#- name: get the md5 of the link target
# shell: checksum {{win_output_dir}}/follow_test | cut -f1 -sd ' '
# register: target_file_result
#- name: assert that the link target was updated
# assert:
# that:
# - replace_follow_result.checksum == target_file_result.stdout
- name: clean up sub
win_file: path={{win_output_dir}}/sub state=absent
- name: clean up foo.txt
win_file: path={{win_output_dir}}/foo.txt state=absent

View file

@ -0,0 +1 @@
foo.txt

View file

@ -0,0 +1 @@
fileA

View file

@ -0,0 +1,3 @@
dependencies:
- prepare_win_tests

View file

@ -0,0 +1,421 @@
# Test code for the file module.
# (c) 2014, Richard Isaacson <richard.c.isaacson@gmail.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/>.
- set_fact: output_file={{win_output_dir}}\\foo.txt
- name: prep with a basic win copy
win_copy: src=foo.txt dest={{output_file}}
- name: verify that we are checking a file and it is present
win_file: path={{output_file}} state=file
register: file_result
- name: verify that the file was marked as changed
assert:
that:
- "file_result.changed == false"
# - "file_result.state == 'file'"
- name: verify that we are checking an absent file
win_file: path={{win_output_dir}}\bar.txt state=absent
register: file2_result
- name: verify that the file was marked as changed
assert:
that:
- "file2_result.changed == false"
# - "file2_result.state == 'absent'"
- name: verify we can touch a file
win_file: path={{win_output_dir}}\baz.txt state=touch
register: file3_result
- name: verify that the file was marked as changed
assert:
that:
- "file3_result.changed == true"
# - "file3_result.state == 'file'"
# - "file3_result.mode == '0644'"
#- name: change file mode
# win_file: path={{win_output_dir}}/baz.txt mode=0600
# register: file4_result
#- name: verify that the file was marked as changed
# assert:
# that:
# - "file4_result.changed == true"
# - "file4_result.mode == '0600'"
#
#- name: change ownership and group
# win_file: path={{win_output_dir}}/baz.txt owner=1234 group=1234
#
#- name: setup a tmp-like directory for ownership test
# win_file: path=/tmp/worldwritable mode=1777 state=directory
#- name: Ask to create a file without enough perms to change ownership
# win_file: path=/tmp/worldwritable/baz.txt state=touch owner=root
# sudo: yes
# sudo_user: nobody
# register: chown_result
# ignore_errors: True
#- name: Ask whether the new file exists
# win_stat: path=/tmp/worldwritable/baz.txt
# register: file_exists_result
#- name: Verify that the file doesn't exist on failure
# assert:
# that:
# - "chown_result.failed == True"
# - "file_exists_result.stat.exists == False"
#
- name: clean up
win_file: path=/tmp/worldwritable state=absent
#- name: create soft link to file
# win_file: src={{output_file}} dest={{win_output_dir}}/soft.txt state=link
# register: file5_result
#- name: verify that the file was marked as changed
# assert:
# that:
# - "file5_result.changed == true"
#
#- name: create hard link to file
# win_file: src={{output_file}} dest={{win_output_dir}}/hard.txt state=hard
# register: file6_result
#
#- name: verify that the file was marked as changed
# assert:
# that:
# - "file6_result.changed == true"
#
- name: create a directory
win_file: path={{win_output_dir}}\foobar state=directory
register: file7_result
- debug: var=file7_result
- name: verify that the file was marked as changed
assert:
that:
- "file7_result.changed == true"
# - "file7_result.state == 'directory'"
# windows and selinux unlikely to ever mix, removing these tests:
#- name: determine if selinux is installed
# shell: which getenforce || exit 0
# register: selinux_installed
#- name: determine if selinux is enabled
# shell: getenforce
# register: selinux_enabled
# when: selinux_installed.stdout != ""
# ignore_errors: true
#- name: decide to include or not include selinux tests
# include: selinux_tests.yml
# when: selinux_installed.stdout != "" and selinux_enabled.stdout != "Disabled"
- name: remote directory foobar
win_file: path={{win_output_dir}}\foobar state=absent
- name: remove file foo.txt
win_file: path={{win_output_dir}}\foo.txt state=absent
- name: remove file bar.txt
win_file: path={{win_output_dir}}\foo.txt state=absent
- name: remove file baz.txt
win_file: path={{win_output_dir}}\foo.txt state=absent
- name: win copy directory structure over
win_copy: src=foobar dest={{win_output_dir}}
- name: remove directory foobar
win_file: path={{win_output_dir}}\foobar state=absent
register: file14_result
- debug: var=file14_result
- name: verify that the directory was removed
assert:
that:
- 'file14_result.changed == true'
# - 'file14_result.state == "absent"'
- name: create a test sub-directory
win_file: dest={{win_output_dir}}/sub1 state=directory
register: file15_result
- name: verify that the new directory was created
assert:
that:
- 'file15_result.changed == true'
# - 'file15_result.state == "directory"'
- name: create test files in the sub-directory
win_file: dest={{win_output_dir}}/sub1/{{item}} state=touch
with_items:
- file1
- file2
- file3
register: file16_result
- name: verify the files were created
assert:
that:
- 'item.changed == true'
# - 'item.state == "file"'
with_items: file16_result.results
#- name: try to force the sub-directory to a link
# win_file: src={{win_output_dir}}/testing dest={{win_output_dir}}/sub1 state=link force=yes
# register: file17_result
# ignore_errors: true
#- name: verify the directory was not replaced with a link
# assert:
# that:
# - 'file17_result.failed == true'
# - 'file17_result.state == "directory"'
#- name: create soft link to directory using absolute path
# win_file: src=/ dest={{win_output_dir}}/root state=link
# register: file18_result
#
#- name: verify that the result was marked as changed
# assert:
# that:
# - "file18_result.changed == true"
#
- name: create another test sub-directory
win_file: dest={{win_output_dir}}/sub2 state=directory
register: file19_result
- name: verify that the new directory was created
assert:
that:
- 'file19_result.changed == true'
# - 'file19_result.state == "directory"'
#- name: create soft link to relative file
# win_file: src=../sub1/file1 dest={{win_output_dir}}/sub2/link1 state=link
# register: file20_result
#
#- name: verify that the result was marked as changed
# assert:
# that:
# - "file20_result.changed == true"
#- name: create soft link to relative directory
# win_file: src=sub1 dest={{win_output_dir}}/sub1-link state=link
# register: file21_result
#
#- name: verify that the result was marked as changed
# assert:
# that:
# - "file21_result.changed == true"
#
#- name: test file creation with symbolic mode
# win_file: dest={{win_output_dir}}/test_symbolic state=touch mode=u=rwx,g=rwx,o=rwx
# register: result
#
#- name: assert file mode
# assert:
# that:
# - result.mode == '0777'
#- name: modify symbolic mode for all
# win_file: dest={{win_output_dir}}/test_symbolic state=touch mode=a=r
# register: result
#
#- name: assert file mode
# assert:
# that:
# - result.mode == '0444'
#- name: modify symbolic mode for owner
# win_file: dest={{win_output_dir}}/test_symbolic state=touch mode=u+w
# register: result
#
#- name: assert file mode
# assert:
# that:
# - result.mode == '0644'
#- name: modify symbolic mode for group
# win_file: dest={{win_output_dir}}/test_symbolic state=touch mode=g+w
# register: result
#
#- name: assert file mode
# assert:
# that:
# - result.mode == '0664'
#
#- name: modify symbolic mode for world
# win_file: dest={{win_output_dir}}/test_symbolic state=touch mode=o+w
# register: result
#
#- name: assert file mode
# assert:
# that:
# - result.mode == '0666'
#
#- name: modify symbolic mode for owner
# win_file: dest={{win_output_dir}}/test_symbolic state=touch mode=u+x
# register: result
#
#- name: assert file mode
# assert:
# that:
# - result.mode == '0766'
##
#- name: modify symbolic mode for group
# win_file: dest={{win_output_dir}}/test_symbolic state=touch mode=g+x
# register: result
#
#- name: assert file mode
# assert:
# that:
# - result.mode == '0776'
#
#- name: modify symbolic mode for world
# win_file: dest={{win_output_dir}}/test_symbolic state=touch mode=o+x
# register: result
#
#- name: assert file mode
# assert:
# that:
# - result.mode == '0777'
#- name: remove symbolic mode for world
# win_file: dest={{win_output_dir}}/test_symbolic state=touch mode=o-wx
# register: result
#
#- name: assert file mode
# assert:
# that:
# - result.mode == '0774'
#
#- name: remove symbolic mode for group
# win_file: dest={{win_output_dir}}/test_symbolic state=touch mode=g-wx
# register: result
#
#- name: assert file mode
### assert:
# that:
# - result.mode == '0744'
#- name: remove symbolic mode for owner
# win_file: dest={{win_output_dir}}/test_symbolic state=touch mode=u-wx
# register: result
#- name: assert file mode
# assert:
# that:
# - result.mode == '0444'
#
#- name: set sticky bit with symbolic mode
# win_file: dest={{win_output_dir}}/test_symbolic state=touch mode=o+t
# register: result
#- name: assert file mode
# assert:
# that:
# - result.mode == '01444'
#
#- name: remove sticky bit with symbolic mode
# win_file: dest={{win_output_dir}}/test_symbolic state=touch mode=o-t
# register: result
#
#- name: assert file mode
# assert:
# that:
# - result.mode == '0444'
#- name: add setgid with symbolic mode
# win_file: dest={{win_output_dir}}/test_symbolic state=touch mode=g+s
# register: result
#
#- name: assert file mode
# assert:
# that:
# - result.mode == '02444'
#
#- name: remove setgid with symbolic mode
# win_file: dest={{win_output_dir}}/test_symbolic state=touch mode=g-s
# register: result
#
#- name: assert file mode
# assert:
# that:
# - result.mode == '0444'
#- name: add setuid with symbolic mode
# win_file: dest={{win_output_dir}}/test_symbolic state=touch mode=u+s
# register: result
#
#- name: assert file mode
# assert:
# that:
# - result.mode == '04444'
#- name: remove setuid with symbolic mode
# win_file: dest={{win_output_dir}}/test_symbolic state=touch mode=u-s
# register: result
#
#- name: assert file mode
# assert:
# that:
# - result.mode == '0444'
# test the file module using follow=yes, so that the target of a
# symlink is modified, rather than the link itself
#- name: create a test file
# win_copy: dest={{win_output_dir}}\test_follow content="this is a test file\n" mode=0666
#- name: create a symlink to the test file
# win_file: path={{win_output_dir}}\test_follow_link src="./test_follow" state=link
#
#- name: modify the permissions on the link using follow=yes
# win_file: path={{win_output_dir}}\test_follow_link mode=0644 follow=yes
# register: result
#- name: assert that the chmod worked
# assert:
# that:
# - result.changed
#
#- name: stat the link target
# win_stat: path={{win_output_dir}}/test_follow
# register: result
#
#- name: assert that the link target was modified correctly
# assert:
# that:
## - result.stat.mode == '0644'
- name: clean up sub1
win_file: path={{win_output_dir}}/sub1 state=absent
- name: clean up sub2
win_file: path={{win_output_dir}}/sub2 state=absent

View file

@ -0,0 +1 @@
templated_var_loaded

View file

@ -0,0 +1,3 @@
dependencies:
- prepare_win_tests

View file

@ -0,0 +1,103 @@
# test code for the template module
# (c) 2014, Michael DeHaan <michael.dehaan@gmail.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: fill in a basic template
# win_template: src=foo.j2 dest={{win_output_dir}}/foo.templated mode=0644
win_template: src=foo.j2 dest={{win_output_dir}}/foo.templated
register: template_result
- assert:
that:
- "'changed' in template_result"
# - "'dest' in template_result"
# - "'group' in template_result"
# - "'gid' in template_result"
# - "'checksum' in template_result"
# - "'owner' in template_result"
# - "'size' in template_result"
# - "'src' in template_result"
# - "'state' in template_result"
# - "'uid' in template_result"
- name: verify that the file was marked as changed
assert:
that:
- "template_result.changed == true"
# VERIFY CONTENTS
- name: copy known good into place
win_copy: src=foo.txt dest={{win_output_dir}}\foo.txt
- name: compare templated file to known good
raw: fc.exe {{win_output_dir}}\foo.templated {{win_output_dir}}\foo.txt
register: diff_result
- debug: var=diff_result
- name: verify templated file matches known good
assert:
that:
# - 'diff_result.stdout == ""'
- 'diff_result.stdout_lines[1] == "FC: no differences encountered"'
- "diff_result.rc == 0"
# VERIFY MODE
# can't set file mode on windows so commenting this test out
#- name: set file mode
# win_file: path={{win_output_dir}}/foo.templated mode=0644
# register: file_result
#- name: ensure file mode did not change
# assert:
# that:
# - "file_result.changed != True"
# commenting out all the following tests as expanduser and file modes not windows concepts.
# VERIFY dest as a directory does not break file attributes
# Note: expanduser is needed to go down the particular codepath that was broken before
#- name: setup directory for test
# win_file: state=directory dest={{win_output_dir | expanduser}}/template-dir mode=0755 owner=nobody group=root
#- name: set file mode when the destination is a directory
# win_template: src=foo.j2 dest={{win_output_dir | expanduser}}/template-dir/ mode=0600 owner=root group=root
#- name: set file mode when the destination is a directory
# win_template: src=foo.j2 dest={{win_output_dir | expanduser}}/template-dir/ mode=0600 owner=root group=root
# register: file_result
#
#- name: check that the file has the correct attributes
# win_stat: path={{win_output_dir | expanduser}}/template-dir/foo.j2
# register: file_attrs
#
#- assert:
# that:
# - "file_attrs.stat.uid == 0"
# - "file_attrs.stat.pw_name == 'root'"
# - "file_attrs.stat.mode == '0600'"
#
#- name: check that the containing directory did not change attributes
# win_stat: path={{win_output_dir | expanduser}}/template-dir/
# register: dir_attrs
#
#- assert:
# that:
# - "dir_attrs.stat.uid != 0"
# - "dir_attrs.stat.pw_name == 'nobody'"
# - "dir_attrs.stat.mode == '0755'"

View file

@ -0,0 +1 @@
{{ templated_var }}

View file

@ -0,0 +1 @@
templated_var: templated_var_loaded

View file

@ -30,3 +30,6 @@
- { role: test_win_msi, tags: test_win_msi }
- { role: test_win_service, tags: test_win_service }
- { role: test_win_feature, tags: test_win_feature }
- { role: test_win_file, tags: test_win_file }
- { role: test_win_copy, tags: test_win_copy }
- { role: test_win_template, tags: test_win_template }