From 2409ae5f2781d2542cbe10f04231e0cc5ce22e10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Subileau?= Date: Mon, 27 Aug 2018 01:36:47 +0200 Subject: [PATCH] win_nssm: add more failing tests These tests highlight several issues with this module: * Service not started when state=started * Errors with app_parameters (see #25265) * Exception when passing several dependencies separated by comma as specified in doc (cherry picked from commit e50234bdb32304703fd1ce1d4f6067a0d551049e) --- .../targets/win_nssm/defaults/main.yml | 5 +- .../targets/win_nssm/tasks/main.yml | 23 +++ .../targets/win_nssm/tasks/tests.yml | 140 +++++++++++++++++- 3 files changed, 160 insertions(+), 8 deletions(-) diff --git a/test/integration/targets/win_nssm/defaults/main.yml b/test/integration/targets/win_nssm/defaults/main.yml index 1c7900f727d..6ebafb24a12 100644 --- a/test/integration/targets/win_nssm/defaults/main.yml +++ b/test/integration/targets/win_nssm/defaults/main.yml @@ -1 +1,4 @@ -test_service_name: ansible_nssm_test \ No newline at end of file +test_service_name: ansible_nssm_test +test_win_nssm_path: '{{win_output_dir}}\win_nssm' +test_win_nssm_username: testnssmuser +test_win_nssm_password: Password123! \ No newline at end of file diff --git a/test/integration/targets/win_nssm/tasks/main.yml b/test/integration/targets/win_nssm/tasks/main.yml index 9b3769e645e..49368cf1a46 100644 --- a/test/integration/targets/win_nssm/tasks/main.yml +++ b/test/integration/targets/win_nssm/tasks/main.yml @@ -4,6 +4,19 @@ name: NSSM state: present +- name: ensure testing folder exists + win_file: + path: '{{test_win_nssm_path}}' + state: directory + +- name: create test user for service execution + win_user: + name: '{{test_win_nssm_username}}' + password: '{{test_win_nssm_password}}' + state: present + groups: + - Users + # Run actual tests - block: - include_tasks: tests.yml @@ -14,6 +27,16 @@ name: '{{ test_service_name }}' state: absent + - name: remove test user + win_user: + name: '{{test_win_nssm_username}}' + state: absent + + - name: cleanup test folder + win_file: + path: '{{test_win_nssm_path}}' + state: absent + - name: uninstall NSSM win_chocolatey: name: NSSM diff --git a/test/integration/targets/win_nssm/tasks/tests.yml b/test/integration/targets/win_nssm/tasks/tests.yml index d70e83320c1..1ce69a11e71 100644 --- a/test/integration/targets/win_nssm/tasks/tests.yml +++ b/test/integration/targets/win_nssm/tasks/tests.yml @@ -1,21 +1,147 @@ --- +- name: get register cmd that will get service info + set_fact: + test_service_cmd: | + $res = @{} + $srvobj = Get-WmiObject Win32_Service -Filter "Name=""$service""" | Select Name,PathName,StartMode,StartName,State + if ($srvobj) { + $srvobj | Get-Member -MemberType *Property | % { $res.($_.name) = $srvobj.($_.name) } + $res.Exists = $true + $res.Dependencies = Get-WmiObject -Query "Associators of {Win32_Service.Name=""$service""} Where AssocClass=Win32_DependentService" | select -ExpandProperty Name + $res.Parameters = @{} + $srvkey = "HKLM:\SYSTEM\CurrentControlSet\Services\$service\Parameters" + Get-Item "$srvkey" | Select-Object -ExpandProperty property | % { $res.Parameters.$_ = (Get-ItemProperty -Path "$srvkey" -Name $_).$_} + } else { + $res.Exists = $false + } + ConvertTo-Json -InputObject $res -Compress + - name: install service win_nssm: name: '{{ test_service_name }}' - application: 'C:\Windows\System32\cmd.exe' - app_parameters_free_form: '-Dcom.test.string=value' + application: C:\Windows\System32\cmd.exe state: present register: install_service - name: get result of install service - win_shell: | - Write-Host ([bool](Get-Service "{{ test_service_name }}" -ErrorAction SilentlyContinue)) - nssm get "{{ test_service_name }}" AppParameters + win_shell: '$service = ''{{ test_service_name }}''; {{ test_service_cmd }}' register: install_service_actual - name: assert results of install service assert: that: - install_service.changed == true - - install_service_actual.stdout_lines[0] == "True" - - install_service_actual.stdout_lines[1] == "-Dcom.test.string=value" \ No newline at end of file + - (install_service_actual.stdout|from_json).Exists == true + - (install_service_actual.stdout|from_json).State == 'Stopped' + - (install_service_actual.stdout|from_json).StartMode == 'Auto' + - (install_service_actual.stdout|from_json).Parameters.Application == "C:\Windows\System32\cmd.exe" + +- name: install and start service + win_nssm: + name: '{{ test_service_name }}' + application: C:\Windows\System32\cmd.exe + state: started + register: install_start_service + +- name: get result of install and start service + win_shell: '$service = ''{{ test_service_name }}''; {{ test_service_cmd }}' + register: install_start_service_actual + +- name: assert results of install and start service + assert: + that: + - install_start_service.changed == true + - (install_start_service_actual.stdout|from_json).Exists == true + - (install_start_service_actual.stdout|from_json).State == 'Running' + - (install_service_actual.stdout|from_json).StartMode == 'Auto' + - (install_start_service_actual.stdout|from_json).Parameters.Application == "C:\Windows\System32\cmd.exe" + +- name: install and start service with more parameters + win_nssm: + name: '{{ test_service_name }}' + application: C:\Windows\System32\cmd.exe + start_mode: manual + dependencies: 'tcpip,dnscache' + user: '{{ test_win_nssm_username }}' + password: '{{ test_win_nssm_password }}' + stdout_file: '{{ test_win_nssm_path }}\log.txt' + stderr_file: '{{ test_win_nssm_path }}\error.txt' + state: started + register: install_service_complex + +- name: get result of install and start service with more parameters + win_shell: '$service = ''{{ test_service_name }}''; {{ test_service_cmd }}' + register: install_service_complex_actual + +- name: assert results of install and start service with more parameters + assert: + that: + - install_service_complex.changed == true + - (install_service_complex_actual.stdout|from_json).Exists == true + - (install_service_complex_actual.stdout|from_json).State == 'Running' + - (install_service_complex_actual.stdout|from_json).StartMode == 'Manual' + - (install_service_complex_actual.stdout|from_json).StartName == '.\\' + test_win_nssm_username + - (install_service_complex_actual.stdout|from_json).Parameters.Application == "C:\Windows\System32\cmd.exe" + - (install_service_complex_actual.stdout|from_json).Parameters.AppStdout == test_win_nssm_path + '\\log.txt' + - (install_service_complex_actual.stdout|from_json).Parameters.AppStderr == test_win_nssm_path + '\\error.txt' + - (install_service_complex_actual.stdout|from_json).Dependencies|length == 2 + - '"Tcpip" in (install_service_complex_actual.stdout|from_json).Dependencies' + - '"Dnscache" in (install_service_complex_actual.stdout|from_json).Dependencies' + +- name: install service with free form parameters + win_nssm: + name: '{{ test_service_name }}' + application: C:\Windows\System32\cmd.exe + app_parameters_free_form: '-v -Dcom.test.string=value test' + state: present + register: free_params + +- name: get result of install service with free form parameters + win_shell: '$service = ''{{ test_service_name }}''; {{ test_service_cmd }}' + register: free_params_actual + +- name: assert results of install service with free form parameters + assert: + that: + - free_params.changed == true + - (free_params_actual.stdout|from_json).Exists == true + - (free_params_actual.stdout|from_json).Parameters.Application == "C:\Windows\System32\cmd.exe" + - (free_params_actual.stdout|from_json).Parameters.AppParameters == "-v -Dcom.test.string=value test" + +- name: install service with dict parameters + win_nssm: + name: '{{ test_service_name }}' + application: C:\Windows\System32\cmd.exe + app_parameters: + foo: true + '-file.out': 'output.bat' + _: bar + register: mixed_params + +- name: get result of install service with dict parameters + win_shell: '$service = ''{{ test_service_name }}''; {{ test_service_cmd }}' + register: mixed_params_actual + +- name: assert results of install service with dict parameters + assert: + that: + - mixed_params.changed == true + - (mixed_params_actual.stdout|from_json).Exists == true + - (mixed_params_actual.stdout|from_json).Parameters.Application == "C:\Windows\System32\cmd.exe" + - (mixed_params_actual.stdout|from_json).Parameters.AppParameters == "bar -file.out output.bat foo True" + +- name: remove service + win_nssm: + name: '{{ test_service_name }}' + state: absent + register: remove_service + +- name: get result of remove service + win_shell: '$service = ''{{ test_service_name }}''; {{ test_service_cmd }}' + register: remove_service_actual + +- name: assert results of remove service + assert: + that: + - remove_service.changed == true + - (remove_service_actual.stdout|from_json).Exists == false \ No newline at end of file