Skip to content

Commit

Permalink
add verification test and change param name
Browse files Browse the repository at this point in the history
Signed-off-by: Amit Weinstock <[email protected]>
  • Loading branch information
amitosw15 committed Jan 13, 2025
1 parent ed93928 commit b1bce8a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
6 changes: 3 additions & 3 deletions plugins/modules/win_listen_ports_facts.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@

$spec = @{
options = @{
date_format = @{ type = 'str'; default = '%c' }
date_uformat = @{ type = 'str'; default = '%c' }
tcp_filter = @{ type = 'list'; elements = 'str'; default = 'Listen' }
}
supports_check_mode = $true
}

$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec)

$date_format = $module.Params.date_format
$date_uformat = $module.Params.date_uformat
$tcp_filter = $module.Params.tcp_filter

# Structure of the response the script will return
Expand All @@ -39,7 +39,7 @@ function Format-Date {
)

if ($null -ne $date) {
$date = Get-Date $date -UFormat $date_format
$date = Get-Date $date -UFormat $date_uformat
}

return $date
Expand Down
7 changes: 2 additions & 5 deletions plugins/modules/win_listen_ports_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
DOCUMENTATION = r'''
---
module: win_listen_ports_facts
version_added: '1.10.0'
short_description: Recopilates the facts of the listening ports of the machine
description:
- Recopilates the information of the TCP and UDP ports of the machine and
Expand All @@ -18,7 +17,7 @@
listen_ports_facts, mantaining the format of the said module.
version_added: 2.7.0
options:
date_format:
date_uformat:
description:
- The format of the date when the process that owns the port started.
- The date specification is UFormat
Expand All @@ -39,8 +38,6 @@
the listen_ports_facts schema to achieve compatibility with the said module
output, even though this module if capable of extracting ports with a state
other than Listen
seealso:
- module: community.general.listen_ports_facts
author:
- David Nieto (@david-ns)
'''
Expand All @@ -57,7 +54,7 @@
- name: Get ports facts with only the year within the date field
ansible.windows.win_listen_ports_facts:
date_format: '%Y'
date_uformat: '%Y'
'''

RETURN = r'''
Expand Down
25 changes: 19 additions & 6 deletions tests/integration/targets/win_listen_ports_facts/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
win_listen_ports_facts:
tcp_filter:
- Error
date_format: null
date_uformat: null
register: invalid_state_date_run
ignore_errors: True

Expand All @@ -17,7 +17,7 @@
- Listen
- Closed
- Error
date_format: null
date_uformat: null
register: partial_invalid_state_date_run
ignore_errors: True

Expand All @@ -26,20 +26,33 @@
tcp_filter:
- Listen
- Closed
date_format: yyyy
date_uformat: "%A %m/%d/%Y %R %Z"
register: custom_state_date_run
ignore_errors: True

- name: Gather facts with default filters and date format
win_listen_ports_facts:
register: default_run
ignore_errors: True

- assert:
- name: assert meta data of tasks as expected
assert:
that:
- invalid_state_date_run.failed is defined and invalid_state_date_run.failed
- partial_invalid_state_date_run.failed is defined and partial_invalid_state_date_run.failed
- custom_state_date_run.failed is not defined or not custom_state_date_run.failed
- default_run.failed is not defined or not default_run.failed
- tcp_listen is defined
- udp_listen is defined

- name: assert results are in expected format
assert:
that:
- custom_state_date_run.ansible_facts.tcp_listen[0].address is defined
- custom_state_date_run.ansible_facts.tcp_listen[0].name is defined
- custom_state_date_run.ansible_facts.tcp_listen[0].pid is defined
- custom_state_date_run.ansible_facts.tcp_listen[0].port is defined
- custom_state_date_run.ansible_facts.tcp_listen[0].protocol is defined
- custom_state_date_run.ansible_facts.tcp_listen[0].protocol == 'tcp'
- custom_state_date_run.ansible_facts.tcp_listen[0].stime is defined
- custom_state_date_run.ansible_facts.tcp_listen[0].user is defined
- custom_state_date_run.ansible_facts.udp_listen[0].protocol is defined
- custom_state_date_run.ansible_facts.udp_listen[0].protocol == 'udp'

0 comments on commit b1bce8a

Please sign in to comment.