From 0c938562a72f4fa43e85f9ca69234104fcd9da49 Mon Sep 17 00:00:00 2001 From: Chris Church Date: Tue, 17 Jun 2014 23:04:17 -0500 Subject: [PATCH] Add winrm integration tests for raw, script and ping modules. --- test/integration/Makefile | 3 + test/integration/README.md | 20 ++++++ test/integration/inventory.winrm.template | 7 ++ .../roles/test_win_ping/tasks/main.yml | 66 +++++++++++++++++++ .../roles/test_win_raw/tasks/main.yml | 56 ++++++++++++++++ .../test_win_script/files/test_script.bat | 2 + .../test_win_script/files/test_script.ps1 | 2 + .../files/test_script_with_args.ps1 | 7 ++ .../files/test_script_with_errors.ps1 | 11 ++++ .../roles/test_win_script/tasks/main.yml | 59 +++++++++++++++++ test/integration/test_winrm.yml | 8 +++ 11 files changed, 241 insertions(+) create mode 100644 test/integration/inventory.winrm.template create mode 100644 test/integration/roles/test_win_ping/tasks/main.yml create mode 100644 test/integration/roles/test_win_raw/tasks/main.yml create mode 100644 test/integration/roles/test_win_script/files/test_script.bat create mode 100644 test/integration/roles/test_win_script/files/test_script.ps1 create mode 100644 test/integration/roles/test_win_script/files/test_script_with_args.ps1 create mode 100644 test/integration/roles/test_win_script/files/test_script_with_errors.ps1 create mode 100644 test/integration/roles/test_win_script/tasks/main.yml create mode 100644 test/integration/test_winrm.yml diff --git a/test/integration/Makefile b/test/integration/Makefile index 758c150f7ca..aefb27dbb83 100644 --- a/test/integration/Makefile +++ b/test/integration/Makefile @@ -46,6 +46,9 @@ test_vault: ansible-playbook test_vault.yml -i $(INVENTORY) $(CREDENTIALS_ARG) -v $(TEST_FLAGS) --vault-password-file $(VAULT_PASSWORD_FILE) --syntax-check ansible-playbook test_vault.yml -i $(INVENTORY) $(CREDENTIALS_ARG) -v $(TEST_FLAGS) --vault-password-file $(VAULT_PASSWORD_FILE) +test_winrm: + ansible-playbook test_winrm.yml -i inventory.winrm -e @$(VARS_FILE) $(CREDENTIALS_ARG) -v $(TEST_FLAGS) + cloud: amazon rackspace cloud_cleanup: amazon_cleanup rackspace_cleanup diff --git a/test/integration/README.md b/test/integration/README.md index e05f843ac2f..f30697b497b 100644 --- a/test/integration/README.md +++ b/test/integration/README.md @@ -70,3 +70,23 @@ resources. Running these tests may result in additional fees associated with your cloud account. Care is taken to ensure that created resources are removed. However, it is advisable to inspect your AWS console to ensure no unexpected resources are running. + +Windows Tests +============= + +These tests exercise the winrm connection plugin and Windows modules. You'll +need to define an inventory with a remote Windows 2008 or 2012 Server to use +for testing, and enable PowerShell Remoting to continue. + +Running these tests may result in changes to your Windows host, so don't run +them against a production/critical Windows environment. + +Enable PowerShell Remoting (run on the Windows host via Remote Desktop): + Enable-PSRemoting -Force + +Define Windows inventory: + cp inventory.winrm.template inventory.winrm + ${EDITOR:-vi} inventory.winrm + +Run the tests: + make test_winrm diff --git a/test/integration/inventory.winrm.template b/test/integration/inventory.winrm.template new file mode 100644 index 00000000000..088e3cc1446 --- /dev/null +++ b/test/integration/inventory.winrm.template @@ -0,0 +1,7 @@ +[windows] +server ansible_ssh_host=10.10.10.10 ansible_ssh_user=Administrator ansible_ssh_pass=ShhhDontTellAnyone + +[windows:vars] +ansible_connection=winrm +# HTTPS uses 5986, HTTP uses 5985 +ansible_ssh_port=5985 diff --git a/test/integration/roles/test_win_ping/tasks/main.yml b/test/integration/roles/test_win_ping/tasks/main.yml new file mode 100644 index 00000000000..f3b5fcc4f42 --- /dev/null +++ b/test/integration/roles/test_win_ping/tasks/main.yml @@ -0,0 +1,66 @@ +--- + +- name: test win_ping + action: win_ping + register: win_ping_result + +- name: check win_ping result + assert: + that: + - "not win_ping_result|failed" + - "not win_ping_result|changed" + - "win_ping_result.ping == 'pong'" + +- name: test win_ping with data + win_ping: data=blah + register: win_ping_with_data_result + +- name: check win_ping result with data + assert: + that: + - "not win_ping_with_data_result|failed" + - "not win_ping_with_data_result|changed" + - "win_ping_with_data_result.ping == 'blah'" + +- name: test ping.ps1 with data + ping.ps1: data=bleep + register: ping_ps1_result + +- name: check ping.ps1 result + assert: + that: + - "not ping_ps1_result|failed" + - "not ping_ps1_result|changed" + - "ping_ps1_result.ping == 'bleep'" + +#- name: test ping.ps1 with invalid args +# ping.ps1: arg=invalid +# register: ping_ps1_invalid_args_result + +#- name: check that ping.ps1 with invalid args fails +# assert: +# that: +# - "ping_ps1_invalid_args_result|failed" +# - "ping_ps1_invalid_args_result.msg" + +- name: test local ping (should use default ping) + local_action: ping + register: local_ping_result + +- name: check local ping result + assert: + that: + - "not local_ping_result|failed" + - "not local_ping_result|changed" + - "local_ping_result.ping == 'pong'" + +- name: test ping (should use ping.ps1) + action: ping + register: ping_result + +- name: check ping result + assert: + that: + - "not ping_result|failed" + - "not ping_result|changed" + - "ping_result.ping == 'pong'" diff --git a/test/integration/roles/test_win_raw/tasks/main.yml b/test/integration/roles/test_win_raw/tasks/main.yml new file mode 100644 index 00000000000..a59ea3b624c --- /dev/null +++ b/test/integration/roles/test_win_raw/tasks/main.yml @@ -0,0 +1,56 @@ +--- + +- name: run getmac + raw: getmac + register: getmac_result + +- name: assert that getmac ran + assert: + that: + - "getmac_result.rc == 0" + - "getmac_result.stdout" + - "not getmac_result.stderr" + - "not getmac_result|failed" + - "not getmac_result|changed" + +- name: run ipconfig with /all argument + raw: ipconfig /all + register: ipconfig_result + +- name: assert that ipconfig ran with /all argument + assert: + that: + - "ipconfig_result.rc == 0" + - "ipconfig_result.stdout" + - "'Physical Address' in ipconfig_result.stdout" + - "not ipconfig_result.stderr" + - "not ipconfig_result|failed" + - "not ipconfig_result|changed" + +- name: run ipconfig with invalid argument + raw: ipconfig /badswitch + register: ipconfig_invalid_result + ignore_errors: true + +- name: assert that ipconfig with invalid argument failed + assert: + that: + - "ipconfig_invalid_result.rc != 0" + - "ipconfig_invalid_result.stdout" # ipconfig displays errors on stdout. + - "not ipconfig_invalid_result.stderr" + - "ipconfig_invalid_result|failed" + - "not ipconfig_invalid_result|changed" + +- name: run an unknown command + raw: uname -a + register: unknown_result + ignore_errors: true + +- name: assert that an unknown command failed + assert: + that: + - "unknown_result.rc != 0" + - "not unknown_result.stdout" + - "unknown_result.stderr" # An unknown command displays error on stderr. + - "unknown_result|failed" + - "not unknown_result|changed" diff --git a/test/integration/roles/test_win_script/files/test_script.bat b/test/integration/roles/test_win_script/files/test_script.bat new file mode 100644 index 00000000000..05cc2d19ecf --- /dev/null +++ b/test/integration/roles/test_win_script/files/test_script.bat @@ -0,0 +1,2 @@ +@ECHO OFF +ECHO We can even run a batch file! diff --git a/test/integration/roles/test_win_script/files/test_script.ps1 b/test/integration/roles/test_win_script/files/test_script.ps1 new file mode 100644 index 00000000000..9978f363418 --- /dev/null +++ b/test/integration/roles/test_win_script/files/test_script.ps1 @@ -0,0 +1,2 @@ +# Test script to make sure the Ansible script module works. +Write-Host "Woohoo! We can run a PowerShell script via Ansible!" diff --git a/test/integration/roles/test_win_script/files/test_script_with_args.ps1 b/test/integration/roles/test_win_script/files/test_script_with_args.ps1 new file mode 100644 index 00000000000..520aafa395f --- /dev/null +++ b/test/integration/roles/test_win_script/files/test_script_with_args.ps1 @@ -0,0 +1,7 @@ +# Test script to make sure the Ansible script module works when arguments are +# passed to the script. + +foreach ($i in $args) +{ + Write-Host $i; +} diff --git a/test/integration/roles/test_win_script/files/test_script_with_errors.ps1 b/test/integration/roles/test_win_script/files/test_script_with_errors.ps1 new file mode 100644 index 00000000000..ad80f68f8b1 --- /dev/null +++ b/test/integration/roles/test_win_script/files/test_script_with_errors.ps1 @@ -0,0 +1,11 @@ +# http://stackoverflow.com/questions/9948517/how-to-stop-a-powershell-script-on-the-first-error +#$ErrorActionPreference = "Stop"; +# http://stackoverflow.com/questions/15777492/why-are-my-powershell-exit-codes-always-0 + +trap +{ + Write-Error -ErrorRecord $_ + exit 1; +} + +throw "Oh noes I has an error" diff --git a/test/integration/roles/test_win_script/tasks/main.yml b/test/integration/roles/test_win_script/tasks/main.yml new file mode 100644 index 00000000000..99d94387f6e --- /dev/null +++ b/test/integration/roles/test_win_script/tasks/main.yml @@ -0,0 +1,59 @@ +--- + +- name: run simple test script + script: test_script.ps1 + register: test_script_result + +- name: check that script ran + assert: + that: + - "test_script_result.rc == 0" + - "test_script_result.stdout" + - "'Woohoo' in test_script_result.stdout" + - "not test_script_result.stderr" + - "not test_script_result|failed" + - "test_script_result|changed" + +- name: run test script that takes arguments + script: test_script_with_args.ps1 /this /that /other + register: test_script_with_args_result + +- name: check that script ran and received arguments + assert: + that: + - "test_script_with_args_result.rc == 0" + - "test_script_with_args_result.stdout" + - "test_script_with_args_result.stdout_lines[0] == '/this'" + - "test_script_with_args_result.stdout_lines[1] == '/that'" + - "test_script_with_args_result.stdout_lines[2] == '/other'" + - "not test_script_with_args_result.stderr" + - "not test_script_with_args_result|failed" + - "test_script_with_args_result|changed" + +- name: run test script that has errors + script: test_script_with_errors.ps1 + register: test_script_with_errors_result + ignore_errors: true + +- name: check that script ran but failed with errors + assert: + that: + - "test_script_with_errors_result.rc != 0" + - "not test_script_with_errors_result.stdout" + - "test_script_with_errors_result.stderr" + - "test_script_with_errors_result|failed" + - "test_script_with_errors_result|changed" + +- name: run simple batch file + script: test_script.bat + register: test_batch_result + +- name: check that batch file ran + assert: + that: + - "test_batch_result.rc == 0" + - "test_batch_result.stdout" + - "'batch' in test_batch_result.stdout" + - "not test_batch_result.stderr" + - "not test_batch_result|failed" + - "test_batch_result|changed" diff --git a/test/integration/test_winrm.yml b/test/integration/test_winrm.yml new file mode 100644 index 00000000000..9ce95fc3043 --- /dev/null +++ b/test/integration/test_winrm.yml @@ -0,0 +1,8 @@ +--- + +- hosts: windows + gather_facts: false + roles: + - { role: test_win_raw, tags: test_win_raw } + - { role: test_win_script, tags: test_win_script } + - { role: test_win_ping, tags: test_win_ping }