From 4488b79b049990b3340fbd9426c0997c41fd695a Mon Sep 17 00:00:00 2001 From: Ashwini Mhatre Date: Mon, 24 Jan 2022 16:45:16 +0530 Subject: [PATCH] Add ipaddr Filter plugin (#115) Add ipaddr Filter plugin SUMMARY depend-on: #113 ISSUE TYPE New Module Pull Request COMPONENT NAME ADDITIONAL INFORMATION Reviewed-by: Nathaniel Case Reviewed-by: Ashwini Mhatre Reviewed-by: Sagar Paul Reviewed-by: None --- README.md | 1 + .../fragments/add_ipaddr_filter_plugin.yaml | 3 + docs/ansible.utils.ipaddr_filter.rst | 330 ++++++++++++++++++ plugins/filter/ipaddr.py | 281 +++++++++++++++ .../utils_ipaddr_filter/tasks/ipaddr.yaml | 44 +++ .../utils_ipaddr_filter/vars/main.yaml | 20 ++ tests/unit/plugins/filter/test_ipaddr.py | 183 ++++++++++ 7 files changed, 862 insertions(+) create mode 100644 changelogs/fragments/add_ipaddr_filter_plugin.yaml create mode 100644 docs/ansible.utils.ipaddr_filter.rst create mode 100644 plugins/filter/ipaddr.py create mode 100644 tests/integration/targets/utils_ipaddr_filter/tasks/ipaddr.yaml create mode 100644 tests/unit/plugins/filter/test_ipaddr.py diff --git a/README.md b/README.md index 8cce206..8cff24c 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ Name | Description [ansible.utils.from_xml](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.from_xml_filter.rst)|Convert given XML string to native python dictionary. [ansible.utils.get_path](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.get_path_filter.rst)|Retrieve the value in a variable using a path [ansible.utils.index_of](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.index_of_filter.rst)|Find the indices of items in a list matching some criteria +[ansible.utils.ipaddr](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.ipaddr_filter.rst)|This filter is designed to return the input value if a query is True, else False. [ansible.utils.ipwrap](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.ipwrap_filter.rst)|This filter is designed to Wrap IPv6 addresses in [ ] brackets. [ansible.utils.network_in_usable](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.network_in_usable_filter.rst)|The network_in_usable filter returns whether an address passed as an argument is usable in a network. [ansible.utils.network_in_network](https://github.com/ansible-collections/ansible.utils/blob/main/docs/ansible.utils.network_in_network_filter.rst)|This filter returns whether an address or a network passed as argument is in a network. diff --git a/changelogs/fragments/add_ipaddr_filter_plugin.yaml b/changelogs/fragments/add_ipaddr_filter_plugin.yaml new file mode 100644 index 0000000..f05ca9a --- /dev/null +++ b/changelogs/fragments/add_ipaddr_filter_plugin.yaml @@ -0,0 +1,3 @@ +--- +minor_changes: + - Add ipaddr filter plugin. diff --git a/docs/ansible.utils.ipaddr_filter.rst b/docs/ansible.utils.ipaddr_filter.rst new file mode 100644 index 0000000..69b7934 --- /dev/null +++ b/docs/ansible.utils.ipaddr_filter.rst @@ -0,0 +1,330 @@ +.. _ansible.utils.ipaddr_filter: + + +******************** +ansible.utils.ipaddr +******************** + +**This filter is designed to return the input value if a query is True, else False.** + + +Version added: 2.5.0 + +.. contents:: + :local: + :depth: 1 + + +Synopsis +-------- +- This filter is designed to return the input value if a query is True, and False if a query is False +- This way it can be easily used in chained filters + + + + +Parameters +---------- + +.. raw:: html + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
ParameterChoices/DefaultsConfigurationComments
+
+ alias + +
+ string +
+
+ + +
type of filter. example ipaddr, ipv4, ipv6, ipwrap
+
+
+ query + +
+ string +
+
+ Default:
""
+
+ +
You can provide a single argument to each ipaddr() filter.
+
The filter will then treat it as a query and return values modified by that query.
+
{'Types of queries include': [{'query by name': "ansible.utils.ipaddr('address'), ansible.utils.ipv4('network');"}, {'query by CIDR range': "ansible.utils.ipaddr('192.168.0.0/24'), ansible.utils.ipv6('2001:db8::/32');"}, {'query by index number': "ansible.utils.ipaddr('1'), ansible.utils.ipaddr('-1');"}]}
+
+
+ value + +
+ list + / elements=string + / required +
+
+ + +
list of subnets or individual address or any other values input for ipaddr plugin
+
+
+ version + +
+ integer +
+
+ + +
Ip version 4 or 6
+
+
+ + + + +Examples +-------- + +.. code-block:: yaml + + #### examples + # Ipaddr filter plugin with different queries. + - name: Set value as input list + ansible.builtin.set_fact: + value: + - 192.24.2.1 + - host.fqdn + - ::1 + - '' + - 192.168.32.0/24 + - fe80::100/10 + - 42540766412265424405338506004571095040/64 + - True + - debug: + msg: "{{ value|ansible.utils.ipaddr }}" + + - name: Fetch only those elements that are host IP addresses and not network ranges + debug: + msg: "{{ value|ansible.utils.ipaddr('address') }}" + + - name: | + Fetch only host IP addresses with their correct CIDR prefixes (as is common with IPv6 addressing), you can use + the ipaddr('host') filter. + debug: + msg: "{{ value|ansible.utils.ipaddr('host') }}" + + - name: check if IP addresses or network ranges are accessible on a public Internet and return it. + debug: + msg: "{{ value|ansible.utils.ipaddr('public') }}" + + - name: check if IP addresses or network ranges are accessible on a private Internet and return it. + debug: + msg: "{{ value|ansible.utils.ipaddr('private') }}" + + - name: check which values are values are specifically network ranges and return it. + debug: + msg: "{{ value|ansible.utils.ipaddr('net') }}" + + - name: check how many IP addresses can be in a certain range. + debug: + msg: "{{ value| ansible.utils.ipaddr('net') | ansible.utils.ipaddr('size') }}" + + - name: By specifying a network range as a query, you can check if a given value is in that range. + debug: + msg: "{{ value|ansible.utils.ipaddr('192.0.0.0/8') }}" + + # First IP address (network address) + - name: | + If you specify a positive or negative integer as a query, ipaddr() will treat this as an index and will return + the specific IP address from a network range, in the "host/prefix" format. + debug: + msg: "{{ value| ansible.utils.ipaddr('net') | ansible.utils.ipaddr('0') }}" + + # Second IP address (usually the gateway host) + - debug: + msg: "{{ value| ansible.utils.ipaddr('net') | ansible.utils.ipaddr('1') }}" + + # Last IP address (the broadcast address in IPv4 networks) + - debug: + msg: "{{ value| ansible.utils.ipaddr('net') | ansible.utils.ipaddr('-1') }}" + + + # PLAY [Ipaddr filter plugin with different queries.] ****************************************************************** + # TASK [Set value as input list] *************************************************************************************** + # ok: [localhost] => {"ansible_facts": {"value": ["192.24.2.1", "host.fqdn", "::1", "", "192.168.32.0/24", + # "fe80::100/10", "42540766412265424405338506004571095040/64", true]}, "changed": false} + # + # TASK [debug] ******************************************************************************************************** + # ok: [localhost] => { + # "msg": [ + # "192.24.2.1", + # "::1", + # "192.168.32.0/24", + # "fe80::100/10", + # "2001:db8:32c:faad::/64" + # ] + # } + # + # TASK [Fetch only those elements that are host IP addresses and not network ranges] *********************************** + # ok: [localhost] => { + # "msg": [ + # "192.24.2.1", + # "::1", + # "fe80::100", + # "2001:db8:32c:faad::" + # ] + # } + # + # TASK [Fetch only host IP addresses with their correct CIDR prefixes (as is common with IPv6 addressing), you can use + # the ipaddr('host') filter.] ***************** + # ok: [localhost] => { + # "msg": [ + # "192.24.2.1/32", + # "::1/128", + # "fe80::100/10" + # ] + # } + # + # TASK [check if IP addresses or network ranges are accessible on a public Internet and return it.] ******************** + # ok: [localhost] => { + # "msg": [ + # "192.24.2.1", + # "2001:db8:32c:faad::/64" + # ] + # } + # + # TASK [check if IP addresses or network ranges are accessible on a private Internet and return it.] ******************* + # ok: [localhost] => { + # "msg": [ + # "192.168.32.0/24", + # "fe80::100/10" + # ] + # } + # + # TASK [check which values are values are specifically network ranges and return it.] ********************************** + # ok: [localhost] => { + # "msg": [ + # "192.168.32.0/24", + # "2001:db8:32c:faad::/64" + # ] + # } + # + # TASK [check how many IP addresses can be in a certain range.] ********************************************************* + # ok: [localhost] => { + # "msg": [ + # 256, + # 18446744073709551616 + # ] + # } + # + # TASK [By specifying a network range as a query, you can check if a given value is in that range.] ******************** + # ok: [localhost] => { + # "msg": [ + # "192.24.2.1", + # "192.168.32.0/24" + # ] + # } + # + # TASK [If you specify a positive or negative integer as a query, ipaddr() will treat this as an index and will + # return the specific IP address from a network range, in the "host/prefix" format.] *** + # ok: [localhost] => { + # "msg": [ + # "192.168.32.0/24", + # "2001:db8:32c:faad::/64" + # ] + # } + # + # TASK [debug] ********************************************************************************************************* + # ok: [localhost] => { + # "msg": [ + # "192.168.32.1/24", + # "2001:db8:32c:faad::1/64" + # ] + # } + # + # TASK [debug] ******************************************************************************************************** + # ok: [localhost] => { + # "msg": [ + # "192.168.32.255/24", + # "2001:db8:32c:faad:ffff:ffff:ffff:ffff/64" + # ] + # } + + + +Return Values +------------- +Common return values are documented `here `_, the following are the fields unique to this filter: + +.. raw:: html + + + + + + + + + + + + +
KeyReturnedDescription
+
+ data + +
+ list + / elements=string +
+
+
Returns list with values valid for a particular query.
+
+
+

+ + +Status +------ + + +Authors +~~~~~~~ + +- Ashwini Mhatre (@amhatre) + + +.. hint:: + Configuration entries for each entry type have a low to high priority order. For example, a variable that is lower in the list will override a variable that is higher up. diff --git a/plugins/filter/ipaddr.py b/plugins/filter/ipaddr.py new file mode 100644 index 0000000..60a9cc6 --- /dev/null +++ b/plugins/filter/ipaddr.py @@ -0,0 +1,281 @@ +# -*- coding: utf-8 -*- +# Copyright 2021 Red Hat +# GNU General Public License v3.0+ +# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +""" +filter plugin file for ipaddr filters: cidr_merge +""" +from __future__ import absolute_import, division, print_function +from functools import partial +from ansible_collections.ansible.utils.plugins.plugin_utils.base.ipaddr_utils import ( + ipaddr, + _need_netaddr, +) +from ansible.errors import AnsibleFilterError +from ansible_collections.ansible.utils.plugins.module_utils.common.argspec_validate import ( + AnsibleArgSpecValidator, +) + +__metaclass__ = type + + +try: + from jinja2.filters import pass_environment +except ImportError: + from jinja2.filters import environmentfilter as pass_environment + +try: + import netaddr + + HAS_NETADDR = True +except ImportError: + # in this case, we'll make the filters return error messages (see bottom) + HAS_NETADDR = False +else: + + class mac_linux(netaddr.mac_unix): + pass + + mac_linux.word_fmt = "%.2x" + +DOCUMENTATION = """ + name: ipaddr + author: Ashwini Mhatre (@amhatre) + version_added: "2.5.0" + short_description: This filter is designed to return the input value if a query is True, else False. + description: + - This filter is designed to return the input value if a query is True, and False if a query is False + - This way it can be easily used in chained filters + options: + value: + description: + - list of subnets or individual address or any other values input for ipaddr plugin + type: list + elements: str + required: True + query: + description: + - You can provide a single argument to each ipaddr() filter. + - The filter will then treat it as a query and return values modified by that query. + - Types of queries include: + - query by name: ansible.utils.ipaddr('address'), ansible.utils.ipv4('network'); + - query by CIDR range: ansible.utils.ipaddr('192.168.0.0/24'), ansible.utils.ipv6('2001:db8::/32'); + - query by index number: ansible.utils.ipaddr('1'), ansible.utils.ipaddr('-1'); + type: str + default: '' + version: + type: int + description: Ip version 4 or 6 + alias: + type: str + description: type of filter. example ipaddr, ipv4, ipv6, ipwrap + notes: +""" + +EXAMPLES = r""" +#### examples +# Ipaddr filter plugin with different queries. +- name: Set value as input list + ansible.builtin.set_fact: + value: + - 192.24.2.1 + - host.fqdn + - ::1 + - '' + - 192.168.32.0/24 + - fe80::100/10 + - 42540766412265424405338506004571095040/64 + - True +- debug: + msg: "{{ value|ansible.utils.ipaddr }}" + +- name: Fetch only those elements that are host IP addresses and not network ranges + debug: + msg: "{{ value|ansible.utils.ipaddr('address') }}" + +- name: | + Fetch only host IP addresses with their correct CIDR prefixes (as is common with IPv6 addressing), you can use + the ipaddr('host') filter. + debug: + msg: "{{ value|ansible.utils.ipaddr('host') }}" + +- name: check if IP addresses or network ranges are accessible on a public Internet and return it. + debug: + msg: "{{ value|ansible.utils.ipaddr('public') }}" + +- name: check if IP addresses or network ranges are accessible on a private Internet and return it. + debug: + msg: "{{ value|ansible.utils.ipaddr('private') }}" + +- name: check which values are values are specifically network ranges and return it. + debug: + msg: "{{ value|ansible.utils.ipaddr('net') }}" + +- name: check how many IP addresses can be in a certain range. + debug: + msg: "{{ value| ansible.utils.ipaddr('net') | ansible.utils.ipaddr('size') }}" + +- name: By specifying a network range as a query, you can check if a given value is in that range. + debug: + msg: "{{ value|ansible.utils.ipaddr('192.0.0.0/8') }}" + +# First IP address (network address) +- name: | + If you specify a positive or negative integer as a query, ipaddr() will treat this as an index and will return + the specific IP address from a network range, in the "host/prefix" format. + debug: + msg: "{{ value| ansible.utils.ipaddr('net') | ansible.utils.ipaddr('0') }}" + +# Second IP address (usually the gateway host) +- debug: + msg: "{{ value| ansible.utils.ipaddr('net') | ansible.utils.ipaddr('1') }}" + +# Last IP address (the broadcast address in IPv4 networks) +- debug: + msg: "{{ value| ansible.utils.ipaddr('net') | ansible.utils.ipaddr('-1') }}" + + +# PLAY [Ipaddr filter plugin with different queries.] ****************************************************************** +# TASK [Set value as input list] *************************************************************************************** +# ok: [localhost] => {"ansible_facts": {"value": ["192.24.2.1", "host.fqdn", "::1", "", "192.168.32.0/24", +# "fe80::100/10", "42540766412265424405338506004571095040/64", true]}, "changed": false} +# +# TASK [debug] ******************************************************************************************************** +# ok: [localhost] => { +# "msg": [ +# "192.24.2.1", +# "::1", +# "192.168.32.0/24", +# "fe80::100/10", +# "2001:db8:32c:faad::/64" +# ] +# } +# +# TASK [Fetch only those elements that are host IP addresses and not network ranges] *********************************** +# ok: [localhost] => { +# "msg": [ +# "192.24.2.1", +# "::1", +# "fe80::100", +# "2001:db8:32c:faad::" +# ] +# } +# +# TASK [Fetch only host IP addresses with their correct CIDR prefixes (as is common with IPv6 addressing), you can use +# the ipaddr('host') filter.] ***************** +# ok: [localhost] => { +# "msg": [ +# "192.24.2.1/32", +# "::1/128", +# "fe80::100/10" +# ] +# } +# +# TASK [check if IP addresses or network ranges are accessible on a public Internet and return it.] ******************** +# ok: [localhost] => { +# "msg": [ +# "192.24.2.1", +# "2001:db8:32c:faad::/64" +# ] +# } +# +# TASK [check if IP addresses or network ranges are accessible on a private Internet and return it.] ******************* +# ok: [localhost] => { +# "msg": [ +# "192.168.32.0/24", +# "fe80::100/10" +# ] +# } +# +# TASK [check which values are values are specifically network ranges and return it.] ********************************** +# ok: [localhost] => { +# "msg": [ +# "192.168.32.0/24", +# "2001:db8:32c:faad::/64" +# ] +# } +# +# TASK [check how many IP addresses can be in a certain range.] ********************************************************* +# ok: [localhost] => { +# "msg": [ +# 256, +# 18446744073709551616 +# ] +# } +# +# TASK [By specifying a network range as a query, you can check if a given value is in that range.] ******************** +# ok: [localhost] => { +# "msg": [ +# "192.24.2.1", +# "192.168.32.0/24" +# ] +# } +# +# TASK [If you specify a positive or negative integer as a query, ipaddr() will treat this as an index and will +# return the specific IP address from a network range, in the "host/prefix" format.] *** +# ok: [localhost] => { +# "msg": [ +# "192.168.32.0/24", +# "2001:db8:32c:faad::/64" +# ] +# } +# +# TASK [debug] ********************************************************************************************************* +# ok: [localhost] => { +# "msg": [ +# "192.168.32.1/24", +# "2001:db8:32c:faad::1/64" +# ] +# } +# +# TASK [debug] ******************************************************************************************************** +# ok: [localhost] => { +# "msg": [ +# "192.168.32.255/24", +# "2001:db8:32c:faad:ffff:ffff:ffff:ffff/64" +# ] +# } +""" + +RETURN = """ + data: + type: list + elements: str + description: + - Returns list with values valid for a particular query. +""" + + +@pass_environment +def _ipaddr(*args, **kwargs): + """This filter is designed to return the input value if a query is True, and False if a query is False""" + keys = ["value", "query", "version", "alias"] + data = dict(zip(keys, args[1:])) + data.update(kwargs) + aav = AnsibleArgSpecValidator( + data=data, schema=DOCUMENTATION, name="ipaddr" + ) + valid, errors, updated_data = aav.validate() + if not valid: + raise AnsibleFilterError(errors) + return ipaddr(**updated_data) + + +class FilterModule(object): + """IP address and network manipulation filters + """ + + filter_map = { + # IP addresses and networks + "ipaddr": _ipaddr + } + + def filters(self): + """ ipaddr filter """ + if HAS_NETADDR: + return self.filter_map + else: + return dict( + (f, partial(_need_netaddr, f)) for f in self.filter_map + ) diff --git a/tests/integration/targets/utils_ipaddr_filter/tasks/ipaddr.yaml b/tests/integration/targets/utils_ipaddr_filter/tasks/ipaddr.yaml new file mode 100644 index 0000000..9f172af --- /dev/null +++ b/tests/integration/targets/utils_ipaddr_filter/tasks/ipaddr.yaml @@ -0,0 +1,44 @@ +--- +- name: set ipaddress list + ansible.builtin.set_fact: + value: + - 192.24.2.1 + - host.fqdn + - ::1 + - 192.168.32.0/24 + - fe80::100/10 + - "42540766412265424405338506004571095040/64" + - True + +- name: ipaddr filter with empty string query + ansible.builtin.set_fact: + result1: "{{ value|ansible.utils.ipaddr }}" + +- name: Assert result for ipaddr. + assert: + that: "{{ result1 == ipaddr_result1 }}" + +- name: ipaddr filter with host string query + ansible.builtin.set_fact: + result2: "{{ value|ansible.utils.ipaddr('host') }}" + +- name: Assert result for ipaddr host query + assert: + that: "{{ result2 == ipaddr_result2 }}" + + +- name: ipaddr filter with public network query + ansible.builtin.set_fact: + result3: "{{ value|ansible.utils.ipaddr('public') }}" + +- name: Assert result for ipaddr public network query + assert: + that: "{{ result3== ipaddr_result3 }}" + +- name: ipaddr filter with network query + ansible.builtin.set_fact: + result4: "{{ value|ansible.utils.ipaddr('net') }}" + +- name: Assert result for ipaddr network query + assert: + that: "{{ result4 == ipaddr_result4 }}" diff --git a/tests/integration/targets/utils_ipaddr_filter/vars/main.yaml b/tests/integration/targets/utils_ipaddr_filter/vars/main.yaml index 1f77a9f..39fd97c 100644 --- a/tests/integration/targets/utils_ipaddr_filter/vars/main.yaml +++ b/tests/integration/targets/utils_ipaddr_filter/vars/main.yaml @@ -12,6 +12,26 @@ result2_val: result3_val: - "192.24.2.1" +ipaddr_result1: + - 192.24.2.1 + - ::1 + - 192.168.32.0/24 + - fe80::100/10 + - 2001:db8:32c:faad::/64 + +ipaddr_result2: + - 192.24.2.1/32 + - ::1/128 + - fe80::100/10 + +ipaddr_result3: + - 192.24.2.1 + - 2001:db8:32c:faad::/64 + +ipaddr_result4: + - 192.168.32.0/24 + - 2001:db8:32c:faad::/64 + ipwrap_result1: - "192.24.2.1" - "host.fqdn" diff --git a/tests/unit/plugins/filter/test_ipaddr.py b/tests/unit/plugins/filter/test_ipaddr.py new file mode 100644 index 0000000..ad48cec --- /dev/null +++ b/tests/unit/plugins/filter/test_ipaddr.py @@ -0,0 +1,183 @@ +# -*- coding: utf-8 -*- +# Copyright 2021 Red Hat +# GNU General Public License v3.0+ +# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +""" +Unit test file for cidr_merge filter plugin +""" + +from __future__ import absolute_import, division, print_function + +__metaclass__ = type + +import unittest +from ansible.errors import AnsibleFilterError +from ansible_collections.ansible.utils.plugins.filter.ipaddr import _ipaddr + + +VALID_DATA = [ + "192.24.2.1", + "host.fqdn", + "::1", + "", + "192.168.32.0/24", + "fe80::100/10", + "42540766412265424405338506004571095040/64", + True, +] + +VALID_DATA1 = ["192.168.32.0/24", "2001:db8:32c:faad::/64"] + +VALID_OUTPUT = [ + "192.24.2.1", + "::1", + "192.168.32.0/24", + "fe80::100/10", + "2001:db8:32c:faad::/64", +] + +VALID_OUTPUT1 = ["192.24.2.1", "::1", "fe80::100", "2001:db8:32c:faad::"] + +VALID_OUTPUT2 = ["192.24.2.1/32", "::1/128", "fe80::100/10"] + +VALID_OUTPUT3 = ["192.24.2.1", "2001:db8:32c:faad::/64"] + +VALID_OUTPUT4 = ["192.168.32.0/24", "fe80::100/10"] + +VALID_OUTPUT5 = ["192.168.32.0/24", "2001:db8:32c:faad::/64"] + +VALID_OUTPUT6 = [256, 18446744073709551616] + +VALID_OUTPUT7 = ["192.24.2.1", "192.168.32.0/24"] + +VALID_OUTPUT8 = ["192.168.32.0/24", "2001:db8:32c:faad::/64"] + +# ansible_default_ipv4 = { +# "address": "192.168.0.11", +# "alias": "eth0", +# "broadcast": "192.168.0.255", +# "gateway": "192.168.0.1", +# "interface": "eth0", +# "macaddress": "fa:16:3e:c4:bd:89", +# "mtu": 1500, +# "netmask": "255.255.255.0", +# "network": "192.168.0.0", +# "type": "ether" +# } + +net_mask = "192.168.0.0/255.255.255.0" + +host_prefix = ["2001:db8:deaf:be11::ef3/64", "192.0.2.48/24", "192.168.0.0/16"] + + +class TestIpAddr(unittest.TestCase): + def setUp(self): + pass + + def test_valid_data_empty(self): + """Check passing invalid argspec""" + + args = ["", VALID_DATA, ""] + result = _ipaddr(*args) + self.assertEqual(result, VALID_OUTPUT) + + def test_valid_data_address(self): + """Check passing invalid argspec""" + + args = ["", VALID_DATA, "address"] + result = _ipaddr(*args) + self.assertEqual(result, VALID_OUTPUT1) + + def test_valid_data_host(self): + """Check passing invalid argspec""" + + args = ["", VALID_DATA, "host"] + result = _ipaddr(*args) + self.assertEqual(result, VALID_OUTPUT2) + + def test_valid_data_public(self): + """Check passing invalid argspec""" + + args = ["", VALID_DATA, "public"] + result = _ipaddr(*args) + self.assertEqual(result, VALID_OUTPUT3) + + def test_valid_data_private(self): + """Check passing invalid argspec""" + + args = ["", VALID_DATA, "private"] + result = _ipaddr(*args) + self.assertEqual(result, VALID_OUTPUT4) + + def test_valid_data_net(self): + """Check passing invalid argspec""" + + args = ["", VALID_DATA, "net"] + result = _ipaddr(*args) + self.assertEqual(result, VALID_OUTPUT5) + + def test_valid_data_size(self): + """Check passing invalid argspec""" + + args = ["", VALID_DATA1, "size"] + result = _ipaddr(*args) + self.assertEqual(result, VALID_OUTPUT6) + + def test_valid_data_network_range(self): + """Check passing invalid argspec""" + + args = ["", VALID_DATA, "192.0.0.0/8"] + result = _ipaddr(*args) + self.assertEqual(result, VALID_OUTPUT7) + + def test_valid_data_with_index(self): + """Check passing invalid argspec""" + + args = ["", VALID_DATA1, "0"] + result = _ipaddr(*args) + self.assertEqual(result, VALID_OUTPUT8) + + def test_invalid_data(self): + """Check passing invalid argspec""" + + args = ["", VALID_DATA, "tftftf"] + kwargs = {} + with self.assertRaises(AnsibleFilterError) as error: + _ipaddr(*args, **kwargs) + self.assertIn("unknown filter type: tftftf", str(error.exception)) + + def test_valid_data_with_prefix(self): + """Check passing invalid argspec""" + + args = ["", net_mask, "prefix"] + result = _ipaddr(*args) + self.assertEqual(result, [24]) + + def test_valid_data_with_host_prefix(self): + """Check passing invalid argspec""" + + args = ["", host_prefix, "host/prefix"] + result = _ipaddr(*args) + self.assertEqual( + result, ["2001:db8:deaf:be11::ef3/64", "192.0.2.48/24"] + ) + + def test_valid_data_with_network_prefix(self): + """Check passing invalid argspec""" + + ipaddress = "192.168.0.11/255.255.255.0" + + args = ["", ipaddress, "network/prefix"] + result = _ipaddr(*args) + self.assertEqual(result, ["192.168.0.0/24"]) + + def test_valid_data_with_subnet(self): + """Check passing invalid argspec""" + + args = ["", host_prefix, "host/prefix"] + result1 = _ipaddr(*args) + + args = ["", result1, "subnet"] + result = _ipaddr(*args) + self.assertEqual(result, ["2001:db8:deaf:be11::/64", "192.0.2.0/24"])