Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation for IPv6 in ipsubnet filter. Add unit tests #131

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
ed98b1e
fix typo. Add example for IPV6
sebastien-rosset Jan 24, 2022
6bec94e
add unit tests for IPv6
sebastien-rosset Jan 24, 2022
8309327
move unit test with /120 case to a separate PR
sebastien-rosset Jan 31, 2022
acf52be
fix linter issues, add changelog
sebastien-rosset Feb 1, 2022
db31e44
reformat with black
sebastien-rosset Feb 6, 2022
87cd0bb
revert black formatting
sebastien-rosset Feb 16, 2022
112a5d2
revert black formatting
sebastien-rosset Feb 16, 2022
8c47d6c
revert black formatting
sebastien-rosset Feb 16, 2022
546e2dd
revert black formatting
sebastien-rosset Feb 16, 2022
4051ad2
revert black formatting
sebastien-rosset Feb 16, 2022
f039bed
revert black formatting
sebastien-rosset Feb 16, 2022
58b04a0
revert black formatting
sebastien-rosset Feb 16, 2022
053518e
revert black formatting
sebastien-rosset Feb 16, 2022
efc59f7
revert black formatting
sebastien-rosset Feb 16, 2022
93834e6
revert black formatting
sebastien-rosset Feb 16, 2022
2ccc63a
revert black formatting
sebastien-rosset Feb 16, 2022
ae891d0
revert black formatting
sebastien-rosset Feb 16, 2022
1eaf561
revert black formatting
sebastien-rosset Feb 16, 2022
95471aa
revert black formatting
sebastien-rosset Feb 16, 2022
f3ea75b
revert black formatting
sebastien-rosset Feb 16, 2022
ba3bc95
revert black formatting
sebastien-rosset Feb 16, 2022
e4e1304
revert black formatting
sebastien-rosset Feb 16, 2022
3214973
revert black formatting
sebastien-rosset Feb 16, 2022
0682294
revert black formatting
sebastien-rosset Feb 16, 2022
a65b5ba
revert black formatting
sebastien-rosset Feb 16, 2022
efa06d4
revert black formatting
sebastien-rosset Feb 16, 2022
c448f42
revert black formatting
sebastien-rosset Feb 16, 2022
84d8535
revert black formatting
sebastien-rosset Feb 16, 2022
c8c9c79
revert black formatting
sebastien-rosset Feb 16, 2022
d520aa7
revert black formatting
sebastien-rosset Feb 16, 2022
fb89cc6
revert black formatting
sebastien-rosset Feb 16, 2022
ea256bd
revert black formatting
sebastien-rosset Feb 16, 2022
7a0a18d
revert black formatting
sebastien-rosset Feb 16, 2022
fa946a3
revert black formatting
sebastien-rosset Feb 16, 2022
7b0987b
revert black formatting
sebastien-rosset Feb 16, 2022
398707a
revert black formatting
sebastien-rosset Feb 16, 2022
3fdf806
revert black formatting
sebastien-rosset Feb 16, 2022
2b19393
revert black formatting
sebastien-rosset Feb 16, 2022
9136f1c
revert black formatting
sebastien-rosset Feb 16, 2022
2a5c610
revert black formatting
sebastien-rosset Feb 16, 2022
42ae452
revert black formatting
sebastien-rosset Feb 16, 2022
025d2a6
revert black formatting
sebastien-rosset Feb 16, 2022
d338441
revert black formatting
sebastien-rosset Feb 16, 2022
8dcfef6
revert black formatting
sebastien-rosset Feb 16, 2022
ae204f3
reformat
sebastien-rosset Feb 16, 2022
0182ad9
Update changelogs/fragments/add_ipfilter_doc_ut.yaml
sebastien-rosset Apr 25, 2022
66ab491
Merge branch 'main' into ipv6_ipsubnet
sebastien-rosset Apr 25, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelogs/fragments/add_ipfilter_doc_ut.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
trivial:
- Add example for IPv6 in ipsubnet filter documentation.
- Fix typo in ipsubnet documentation.
- Add unit tests for ipsubnet filter.
14 changes: 13 additions & 1 deletion plugins/filter/ipsubnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ class mac_linux(netaddr.mac_unix):
vars:
address: '192.168.144.5'
subnet: '192.168.0.0/16'
ipv6_address: '2001:4860:4860::8888'
ipv6_subnet: '2600:1f1c:1b3:8f00::/56'
tasks:
# If the given string is an IP address, it will be converted into a subnet.
- name: convert IP address to subnet
Expand All @@ -101,6 +103,16 @@ class mac_linux(netaddr.mac_unix):
debug:
msg: "{{ subnet | ansible.utils.ipsubnet(20, -1) }}"

# Get a new subnet with the specified index.
- name: Get first IPv6 subnet that has prefix length /120
debug:
msg: "{{ ipv6_subnet | ansible.utils.ipsubnet(120, 0) }}"

# Get a new subnet with the specified index.
- name: Get last subnet that has prefix length /120
debug:
msg: "{{ ipv6_subnet | ansible.utils.ipsubnet(120, -1) }}"

# If you specify an IP address instead of a subnet, and give a subnet size as the first argument, the ipsubnet() |
# filter will instead return the biggest subnet that contains that given IP address.
- name: Get biggest subnet that contains that given IP address.
Expand Down Expand Up @@ -137,7 +149,7 @@ class mac_linux(netaddr.mac_unix):

# By specifying another subnet as a second argument, if the second subnet includes the first, you can determine |
# the rank of the first subnet in the second.
- name: he fifth subnet /30 in a /24
- name: The fifth subnet /30 in a /24
debug:
msg: "{{ '192.168.144.16/30' | ansible.utils.ipsubnet('192.168.144.0/24') }}"

Expand Down
23 changes: 23 additions & 0 deletions tests/unit/plugins/filter/test_ipsubnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,26 @@ def test_ipvsubnet_filter_rank_address_in_subnet3(self):
args = ["", "192.168.144.16/30", "192.168.144.0/24"]
result = _ipsubnet(*args)
self.assertEqual(result, "5")

def test_ipvsubnet_get_subnet(self):
test_cases = [
[["", "2600:1f1c:1b3:8f00::/56", 64, 0], "2600:1f1c:1b3:8f00::/64"],
[["", "2600:1f1c:1b3:8f00::/56", "64", "1"], "2600:1f1c:1b3:8f01::/64"],
[["", "2600:1f1c:1b3:8f00::/56", "64", "2"], "2600:1f1c:1b3:8f02::/64"],
[["", "2600:1f1c:1b3:8f00::/56", "64", "-2"], "2600:1f1c:1b3:8ffe::/64"],
[["", "2600:1f1c:1b3:8f00::/56", "64", "-1"], "2600:1f1c:1b3:8fff::/64"],
[["", "2600:1f1c:1b3:8f00::/56", "65", "0"], "2600:1f1c:1b3:8f00::/65"],
[["", "2600:1f1c:1b3:8f00::/56", "65", "1"], "2600:1f1c:1b3:8f00:8000::/65"],
[["", "2600:1f1c:1b3:8f00::/56", "65", "2"], "2600:1f1c:1b3:8f01::/65"],
[["", "2600:1f1c:1b3:8f00::/56", "65", "-2"], "2600:1f1c:1b3:8fff::/65"],
[["", "2600:1f1c:1b3:8f00::/56", "65", "-1"], "2600:1f1c:1b3:8fff:8000::/65"],
[["", "2600:1f1c:1b3:8f00::/56", "64", "257"], False], # subnet id too big for /64
[["", "2600:1f1c:1b3:8f00:::/56", "64", "0"], False], # Too many colons
[["", "2600:1f1c:1b3:8f00::/56", "129", "0"], False], # Must be /128 or less
]
for args, expected in test_cases:
self.assertEqual(
_ipsubnet(*args),
expected,
msg="Testing {0}".format(args),
)