# test code for the win_service module
# (c) 2014, Chris Church <chris@ninemoreminutes.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: 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 }}'
  register: download_res
  until: download_res is successful
  retries: 3
  delay: 5

- name: remove the dummy test services if it is left over from previous tests
  win_service:
    name: '{{ item }}'
    force_dependent_services: True
    state: absent
  become: yes
  become_method: runas
  become_user: SYSTEM
  with_items:
  - '{{ test_win_service_name }}'
  - TestServiceParent2
  - TestServiceDependency
  - TestRestrictedService

- block:
  - name: create new dummy test service
    win_service:
      name: "{{test_win_service_name}}"
      path: "{{test_win_service_path}}"
      display_name: "{{test_win_service_display_name}}"
      description: "{{test_win_service_description}}"
    register: win_service_added

  - name: check that creating a new service succeeds with a change
    assert:
      that:
      - win_service_added is changed
      - win_service_added.name == test_win_service_name
      - win_service_added.can_pause_and_continue == False
      - win_service_added.display_name == test_win_service_display_name
      - win_service_added.description == test_win_service_description
      - win_service_added.path == test_win_service_path
      - win_service_added.state == 'stopped'
      - win_service_added.start_mode == 'auto'
      - win_service_added.username == 'LocalSystem'
      - win_service_added.desktop_interact == False
      - win_service_added.dependencies == []
      - win_service_added.depended_by == []
      - win_service_added.exists == True

  - include_tasks: tests.yml
  - include_tasks: error_control.yml
  - include_tasks: failure_actions.yml
  - include_tasks: load_order_group.yml
  - include_tasks: pre_shutdown_timeout_ms.yml
  - include_tasks: required_privileges.yml
  - include_tasks: service_type.yml
  - include_tasks: sid_info.yml
  - include_tasks: driver.yml

  always:
  - name: remove test services
    win_service:
      name: '{{ item }}'
      force_dependent_services: True
      state: absent
    become: yes
    become_method: runas
    become_user: SYSTEM
    with_items:
    - '{{ test_win_service_name }}'
    - TestServiceParent2
    - TestServiceDependency
    - TestRestrictedService

  - name: remove test directory
    win_file:
      path: '{{ test_win_service_dir }}'
      state: absent
