win_service - use custom binary for tests (#51689)
This commit is contained in:
parent
d37386d2c7
commit
2e99dea867
5 changed files with 222 additions and 119 deletions
|
@ -1,9 +1,7 @@
|
|||
---
|
||||
# parameters set here for creating new service in tests
|
||||
test_win_service_binary_url: https://s3.amazonaws.com/ansible-ci-files/test/integration/targets/win_service/SleepService.exe
|
||||
test_win_service_dir: C:\ansible testing\[win_service]
|
||||
test_win_service_path: '{{ test_win_service_dir }}\SleepService.exe'
|
||||
test_win_service_name: TestService [*abc]
|
||||
test_win_service_display_name: Test Service
|
||||
test_win_service_description: Test Service description
|
||||
test_win_service_path: C:\Windows\System32\snmptrap.exe
|
||||
|
||||
# used for the pause tests, need to use an actual service
|
||||
test_win_service_pause_name: TapiSrv
|
||||
|
|
16
test/integration/targets/win_service/files/Program.cs
Normal file
16
test/integration/targets/win_service/files/Program.cs
Normal file
|
@ -0,0 +1,16 @@
|
|||
using System.ServiceProcess;
|
||||
|
||||
namespace SleepService
|
||||
{
|
||||
internal static class Program
|
||||
{
|
||||
private static void Main(string[] args)
|
||||
{
|
||||
ServiceBase.Run(new ServiceBase[1]
|
||||
{
|
||||
(ServiceBase) new SleepService(args)
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
85
test/integration/targets/win_service/files/SleepService.cs
Normal file
85
test/integration/targets/win_service/files/SleepService.cs
Normal file
|
@ -0,0 +1,85 @@
|
|||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using System.Management;
|
||||
using System.ServiceProcess;
|
||||
|
||||
namespace SleepService
|
||||
{
|
||||
public class SleepService : ServiceBase
|
||||
{
|
||||
private IContainer components = null;
|
||||
private string[] serviceArgs;
|
||||
private string displayName;
|
||||
|
||||
public SleepService(string[] args)
|
||||
{
|
||||
CanPauseAndContinue = true;
|
||||
CanShutdown = true;
|
||||
CanStop = true;
|
||||
AutoLog = false;
|
||||
serviceArgs = args;
|
||||
InitializeComponent();
|
||||
|
||||
string eventSource = "Ansible Test";
|
||||
if (!EventLog.SourceExists(eventSource))
|
||||
EventLog.CreateEventSource(eventSource, "Application");
|
||||
EventLog.Source = eventSource;
|
||||
EventLog.Log = "Application";
|
||||
}
|
||||
|
||||
private string GetServiceName()
|
||||
{
|
||||
using (ManagementObjectCollection.ManagementObjectEnumerator enumerator = new ManagementObjectSearcher(string.Format("SELECT * FROM Win32_Service WHERE ProcessId = {0}", (object)Process.GetCurrentProcess().Id)).Get().GetEnumerator())
|
||||
{
|
||||
if (enumerator.MoveNext())
|
||||
return enumerator.Current["Name"].ToString();
|
||||
}
|
||||
return ServiceName;
|
||||
}
|
||||
|
||||
protected override void OnContinue()
|
||||
{
|
||||
EventLog.WriteEntry(string.Format("{0} OnContinue", displayName));
|
||||
}
|
||||
|
||||
protected override void OnCustomCommand(int command)
|
||||
{
|
||||
EventLog.WriteEntry(string.Format("{0} OnCustomCommand {1}", displayName, command.ToString()));
|
||||
}
|
||||
|
||||
protected override void OnPause()
|
||||
{
|
||||
EventLog.WriteEntry(string.Format("{0} OnPause", displayName));
|
||||
}
|
||||
|
||||
protected override void OnStart(string[] args)
|
||||
{
|
||||
displayName = this.GetServiceName();
|
||||
EventLog.WriteEntry(string.Format("{0} OnStart Args:\n{1}", displayName, string.Join("\n", serviceArgs)));
|
||||
}
|
||||
|
||||
protected override void OnShutdown()
|
||||
{
|
||||
EventLog.WriteEntry(string.Format("{0} OnShutdown", displayName));
|
||||
}
|
||||
|
||||
protected override void OnStop()
|
||||
{
|
||||
EventLog.WriteEntry(string.Format("{0} OnStop", displayName));
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing && components != null)
|
||||
components.Dispose();
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
private void InitializeComponent()
|
||||
{
|
||||
components = new Container();
|
||||
ServiceName = nameof(SleepService);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -16,6 +16,17 @@
|
|||
# You should have received a copy of the GNU General Public License
|
||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
- name: create test directory
|
||||
win_file:
|
||||
path: '{{ test_win_service_dir }}'
|
||||
state: directory
|
||||
|
||||
# This binary has been pre-compiled with the code in the files directory of this role
|
||||
- name: download service executable
|
||||
win_get_url:
|
||||
url: '{{ test_win_service_binary_url }}'
|
||||
dest: '{{ test_win_service_path }}'
|
||||
|
||||
- name: remove the dummy test services if it is left over from previous tests
|
||||
win_service:
|
||||
name: '{{ item }}'
|
||||
|
@ -26,23 +37,11 @@
|
|||
- TestServiceParent2
|
||||
- TestServiceDependency
|
||||
|
||||
- name: get details of the {{test_win_service_pause_name}} service
|
||||
win_service:
|
||||
name: '{{test_win_service_pause_name}}'
|
||||
register: pause_service_details
|
||||
|
||||
- block:
|
||||
- include_tasks: tests.yml
|
||||
|
||||
always:
|
||||
- name: ensure the {{test_win_service_pause_name}} is set to stopped if it was stopped
|
||||
win_service:
|
||||
name: '{{test_win_service_pause_name}}'
|
||||
state: stopped
|
||||
force_dependent_services: True
|
||||
when: pause_service_details.state == 'stopped'
|
||||
|
||||
- name: make sure all services are removed in the end
|
||||
- name: remove test services
|
||||
win_service:
|
||||
name: '{{ item }}'
|
||||
force_dependent_services: True
|
||||
|
@ -57,3 +56,8 @@
|
|||
name: TEST_SERVICE_PATH
|
||||
level: machine
|
||||
state: absent
|
||||
|
||||
- name: remove test directory
|
||||
win_file:
|
||||
path: '{{ test_win_service_dir }}'
|
||||
state: absent
|
||||
|
|
|
@ -800,11 +800,12 @@
|
|||
- win_service_removed.state is not defined
|
||||
- win_service_removed.username is not defined
|
||||
|
||||
# only run these tests if TapiSrv is already stopped, don't want to impact an existing server
|
||||
- block:
|
||||
- name: start the pausable service
|
||||
- name: create new pausable dummy test service
|
||||
win_service:
|
||||
name: '{{test_win_service_pause_name}}'
|
||||
name: "{{test_win_service_name}}"
|
||||
path: "{{test_win_service_path}}"
|
||||
display_name: "{{test_win_service_display_name}}"
|
||||
description: "{{test_win_service_description}}"
|
||||
state: started
|
||||
register: stat_pausable_service
|
||||
|
||||
|
@ -815,7 +816,7 @@
|
|||
|
||||
- name: pause a service check
|
||||
win_service:
|
||||
name: '{{test_win_service_pause_name}}'
|
||||
name: '{{test_win_service_name}}'
|
||||
state: paused
|
||||
register: win_service_paused_check
|
||||
check_mode: yes
|
||||
|
@ -828,7 +829,7 @@
|
|||
|
||||
- name: pause a service
|
||||
win_service:
|
||||
name: '{{test_win_service_pause_name}}'
|
||||
name: '{{test_win_service_name}}'
|
||||
state: paused
|
||||
register: win_service_paused
|
||||
|
||||
|
@ -840,7 +841,7 @@
|
|||
|
||||
- name: pause a service again
|
||||
win_service:
|
||||
name: '{{test_win_service_pause_name}}'
|
||||
name: '{{test_win_service_name}}'
|
||||
state: paused
|
||||
register: win_service_paused_again
|
||||
|
||||
|
@ -851,7 +852,7 @@
|
|||
|
||||
- name: start a paused service check
|
||||
win_service:
|
||||
name: '{{test_win_service_pause_name}}'
|
||||
name: '{{test_win_service_name}}'
|
||||
state: started
|
||||
register: start_paused_service_check
|
||||
check_mode: yes
|
||||
|
@ -864,7 +865,7 @@
|
|||
|
||||
- name: start a paused service
|
||||
win_service:
|
||||
name: '{{test_win_service_pause_name}}'
|
||||
name: '{{test_win_service_name}}'
|
||||
state: started
|
||||
register: start_paused_service
|
||||
|
||||
|
@ -876,12 +877,12 @@
|
|||
|
||||
- name: pause service for next test
|
||||
win_service:
|
||||
name: '{{test_win_service_pause_name}}'
|
||||
name: '{{test_win_service_name}}'
|
||||
state: paused
|
||||
|
||||
- name: stop a paused service check
|
||||
win_service:
|
||||
name: '{{test_win_service_pause_name}}'
|
||||
name: '{{test_win_service_name}}'
|
||||
state: stopped
|
||||
force_dependent_services: True
|
||||
register: stop_paused_service_check
|
||||
|
@ -895,7 +896,7 @@
|
|||
|
||||
- name: stop a paused service
|
||||
win_service:
|
||||
name: '{{test_win_service_pause_name}}'
|
||||
name: '{{test_win_service_name}}'
|
||||
state: stopped
|
||||
force_dependent_services: True
|
||||
register: stop_paused_service
|
||||
|
@ -908,8 +909,7 @@
|
|||
|
||||
- name: fail to pause a stopped service check
|
||||
win_service:
|
||||
name: '{{test_win_service_pause_name}}'
|
||||
name: '{{test_win_service_name}}'
|
||||
state: paused
|
||||
register: fail_pause_stopped_service
|
||||
failed_when: "fail_pause_stopped_service.msg != 'failed to pause service ' + test_win_service_pause_name + ': The service does not support pausing'"
|
||||
when: pause_service_details.state == 'stopped'
|
||||
failed_when: "fail_pause_stopped_service.msg != 'failed to pause service ' + test_win_service_name + ': The service does not support pausing'"
|
||||
|
|
Loading…
Reference in a new issue