From fcbf5c31851cced6a82a27d7d0b489551e325385 Mon Sep 17 00:00:00 2001 From: Rick Elrod Date: Tue, 27 Apr 2021 10:15:24 -0500 Subject: [PATCH] [module_utils] make to_bits actually return (#74409) Change: - Instead of returning the `str` type, return the value that was calculated. Test Plan: - New unit tests. Signed-off-by: Rick Elrod --- changelogs/fragments/to_bits-does-nothing.yml | 2 ++ lib/ansible/module_utils/common/network.py | 2 +- test/units/module_utils/common/test_network.py | 11 +++++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/to_bits-does-nothing.yml diff --git a/changelogs/fragments/to_bits-does-nothing.yml b/changelogs/fragments/to_bits-does-nothing.yml new file mode 100644 index 00000000000..0cf7bf7eb39 --- /dev/null +++ b/changelogs/fragments/to_bits-does-nothing.yml @@ -0,0 +1,2 @@ +bugfixes: + - network module_utils - fix bug where ``to_bits()`` returned the ``str`` type instead of a useful value. diff --git a/lib/ansible/module_utils/common/network.py b/lib/ansible/module_utils/common/network.py index 9e1c1ab78b3..c3874f89e1d 100644 --- a/lib/ansible/module_utils/common/network.py +++ b/lib/ansible/module_utils/common/network.py @@ -146,7 +146,7 @@ def to_bits(val): bits = '' for octet in val.split('.'): bits += bin(int(octet))[2:].zfill(8) - return str + return bits def is_mac(mac_address): diff --git a/test/units/module_utils/common/test_network.py b/test/units/module_utils/common/test_network.py index 1267d0cef04..27d95032a73 100644 --- a/test/units/module_utils/common/test_network.py +++ b/test/units/module_utils/common/test_network.py @@ -9,6 +9,7 @@ __metaclass__ = type import pytest from ansible.module_utils.common.network import ( + to_bits, to_masklen, to_netmask, to_subnet, @@ -66,3 +67,13 @@ def test_to_ipv6_network(): assert '2001:db8::' == to_ipv6_network('2001:db8::') assert '2001:0db8:85a3::' == to_ipv6_network('2001:0db8:85a3:0000:0000:8a2e:0370:7334') assert '2001:0db8:85a3::' == to_ipv6_network('2001:0db8:85a3:0:0:8a2e:0370:7334') + + +def test_to_bits(): + assert to_bits('0') == '00000000' + assert to_bits('1') == '00000001' + assert to_bits('2') == '00000010' + assert to_bits('1337') == '10100111001' + assert to_bits('127.0.0.1') == '01111111000000000000000000000001' + assert to_bits('255.255.255.255') == '11111111111111111111111111111111' + assert to_bits('255.255.255.0') == '11111111111111111111111100000000'