From 262c341cdab7320393e4daa663bfa3117b143ff1 Mon Sep 17 00:00:00 2001 From: Matt Clay Date: Thu, 24 Mar 2016 10:00:24 -0700 Subject: [PATCH] Add connection tests for winrm connection plugin. These are the same tests used for the other connection plugins, adapted to use winrm modules and Windows friendly paths. --- lib/ansible/plugins/connection/winrm.py | 10 +++--- test/integration/Makefile | 7 ++++ test/integration/inventory.winrm.template | 12 +++++++ test/integration/test_connection_winrm.yml | 37 ++++++++++++++++++++++ 4 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 test/integration/test_connection_winrm.yml diff --git a/lib/ansible/plugins/connection/winrm.py b/lib/ansible/plugins/connection/winrm.py index 1fd72f0c940..4781188a2b5 100644 --- a/lib/ansible/plugins/connection/winrm.py +++ b/lib/ansible/plugins/connection/winrm.py @@ -247,9 +247,9 @@ class Connection(ConnectionBase): # FUTURE: determine buffer size at runtime via remote winrm config? def _put_file_stdin_iterator(self, in_path, out_path, buffer_size=250000): - in_size = os.path.getsize(in_path) + in_size = os.path.getsize(to_bytes(in_path, errors='strict')) offset = 0 - with open(in_path, 'rb') as in_file: + with open(to_bytes(in_path, errors='strict'), 'rb') as in_file: for out_data in iter((lambda:in_file.read(buffer_size)), ''): offset += len(out_data) self._display.vvvvv('WINRM PUT "%s" to "%s" (offset=%d size=%d)' % (in_path, out_path, offset, len(out_data)), host=self._winrm_host) @@ -265,7 +265,7 @@ class Connection(ConnectionBase): super(Connection, self).put_file(in_path, out_path) out_path = self._shell._unquote(out_path) display.vvv('PUT "%s" TO "%s"' % (in_path, out_path), host=self._winrm_host) - if not os.path.exists(in_path): + if not os.path.exists(to_bytes(in_path, errors='strict')): raise AnsibleFileNotFound('file or module does not exist: "%s"' % in_path) script_template = u''' @@ -366,9 +366,9 @@ class Connection(ConnectionBase): else: if not out_file: # If out_path is a directory and we're expecting a file, bail out now. - if os.path.isdir(out_path): + if os.path.isdir(to_bytes(out_path, errors='strict')): break - out_file = open(out_path, 'wb') + out_file = open(to_bytes(out_path, errors='strict'), 'wb') out_file.write(data) if len(data) < buffer_size: break diff --git a/test/integration/Makefile b/test/integration/Makefile index 39f4fd38729..db610c4641e 100644 --- a/test/integration/Makefile +++ b/test/integration/Makefile @@ -102,6 +102,13 @@ test_connection: setup $(call TEST_CONNECTION_CMD) $(call TEST_CONNECTION_CMD, LC_ALL=C LANG=C) +# Connection plugin test command to repeat with each locale setting. WinRM specific version. +TEST_CONNECTION_WINRM_CMD = $(1) ansible-playbook test_connection_winrm.yml -i inventory.winrm $(TEST_FLAGS) + +test_connection_winrm: setup + $(call TEST_CONNECTION_WINRM_CMD) + $(call TEST_CONNECTION_WINRM_CMD, LC_ALL=C LANG=C) + destructive: setup ansible-playbook destructive.yml -i $(INVENTORY) -e outputdir=$(TEST_DIR) -e @$(VARS_FILE) $(CREDENTIALS_ARG) -v $(TEST_FLAGS) diff --git a/test/integration/inventory.winrm.template b/test/integration/inventory.winrm.template index 088e3cc1446..3e91c8b232f 100644 --- a/test/integration/inventory.winrm.template +++ b/test/integration/inventory.winrm.template @@ -5,3 +5,15 @@ server ansible_ssh_host=10.10.10.10 ansible_ssh_user=Administrator ansible_ssh_p ansible_connection=winrm # HTTPS uses 5986, HTTP uses 5985 ansible_ssh_port=5985 + +[winrm] +winrm-pipelining ansible_ssh_pipelining=true +winrm-no-pipelining ansible_ssh_pipelining=false + +[winrm:vars] +ansible_connection=winrm +ansible_host=somehost +ansible_user=someuser +ansible_password=somepassword +ansible_port=5986 +ansible_winrm_server_cert_validation=ignore diff --git a/test/integration/test_connection_winrm.yml b/test/integration/test_connection_winrm.yml new file mode 100644 index 00000000000..76e02a9e2db --- /dev/null +++ b/test/integration/test_connection_winrm.yml @@ -0,0 +1,37 @@ +- hosts: winrm + gather_facts: no + serial: 1 + tasks: + + ### raw with unicode arg and output + + - name: raw with unicode arg and output + raw: echo 汉语 + register: command + - name: check output of raw with unicode arg and output + assert: { that: "'汉语' in command.stdout" } + + ### copy local file with unicode filename and content + + - name: create local file with unicode filename and content + local_action: lineinfile dest=/tmp/ansible-local-汉语/汉语.txt create=true line=汉语 + - name: remove remote file with unicode filename and content + win_file: path=c:/windows/temp/ansible-remote-汉语/汉语.txt state=absent + - name: create remote directory with unicode name + win_file: path=c:/windows/temp/ansible-remote-汉语 state=directory + - name: copy local file with unicode filename and content + win_copy: src=/tmp/ansible-local-汉语/汉语.txt dest=c:/windows/temp/ansible-remote-汉语/汉语.txt + + ### fetch remote file with unicode filename and content + + - name: remove local file with unicode filename and content + local_action: file path=/tmp/ansible-local-汉语/汉语.txt state=absent + - name: fetch remote file with unicode filename and content + fetch: src=c:/windows/temp/ansible-remote-汉语/汉语.txt dest=/tmp/ansible-local-汉语/汉语.txt fail_on_missing=true validate_checksum=true flat=true + + ### remove local and remote temp files + + - name: remove local temp file + local_action: file path=/tmp/ansible-local-汉语 state=absent + - name: remove remote temp file + win_file: path=c:/windows/temp/ansible-remote-汉语 state=absent