From bd7001cdb6b6ff3fe363416ccea7963418f200bd Mon Sep 17 00:00:00 2001 From: Jocelyn Jaubert Date: Thu, 16 Aug 2018 20:28:22 +0200 Subject: [PATCH] interfaces_file: Add test for changing ipv4 or ipv6 address through address_family --- .../golden_output/address_family_change_ipv4 | 10 ++ .../address_family_change_ipv4.exceptions.txt | 0 .../address_family_change_ipv4.json | 19 ++++ .../golden_output/address_family_change_ipv6 | 10 ++ .../address_family_change_ipv6.exceptions.txt | 0 .../address_family_change_ipv6.json | 19 ++++ .../golden_output/default_dhcp_change_ipv4 | 7 ++ .../default_dhcp_change_ipv4.exceptions.txt | 0 .../default_dhcp_change_ipv4.json | 18 ++++ .../golden_output/default_dhcp_change_ipv6 | 6 ++ .../default_dhcp_change_ipv6.exceptions.txt | 9 ++ .../default_dhcp_change_ipv6.json | 18 ++++ .../golden_output/servers.com_change_ipv4 | 58 ++++++++++ .../servers.com_change_ipv4.exceptions.txt | 9 ++ .../servers.com_change_ipv4.json | 101 ++++++++++++++++++ .../golden_output/servers.com_change_ipv6 | 58 ++++++++++ .../servers.com_change_ipv6.exceptions.txt | 9 ++ .../servers.com_change_ipv6.json | 101 ++++++++++++++++++ .../interfaces_file/test_interfaces_file.py | 46 ++++++++ 19 files changed, 498 insertions(+) create mode 100644 test/units/modules/system/interfaces_file/fixtures/golden_output/address_family_change_ipv4 create mode 100644 test/units/modules/system/interfaces_file/fixtures/golden_output/address_family_change_ipv4.exceptions.txt create mode 100644 test/units/modules/system/interfaces_file/fixtures/golden_output/address_family_change_ipv4.json create mode 100644 test/units/modules/system/interfaces_file/fixtures/golden_output/address_family_change_ipv6 create mode 100644 test/units/modules/system/interfaces_file/fixtures/golden_output/address_family_change_ipv6.exceptions.txt create mode 100644 test/units/modules/system/interfaces_file/fixtures/golden_output/address_family_change_ipv6.json create mode 100644 test/units/modules/system/interfaces_file/fixtures/golden_output/default_dhcp_change_ipv4 create mode 100644 test/units/modules/system/interfaces_file/fixtures/golden_output/default_dhcp_change_ipv4.exceptions.txt create mode 100644 test/units/modules/system/interfaces_file/fixtures/golden_output/default_dhcp_change_ipv4.json create mode 100644 test/units/modules/system/interfaces_file/fixtures/golden_output/default_dhcp_change_ipv6 create mode 100644 test/units/modules/system/interfaces_file/fixtures/golden_output/default_dhcp_change_ipv6.exceptions.txt create mode 100644 test/units/modules/system/interfaces_file/fixtures/golden_output/default_dhcp_change_ipv6.json create mode 100644 test/units/modules/system/interfaces_file/fixtures/golden_output/servers.com_change_ipv4 create mode 100644 test/units/modules/system/interfaces_file/fixtures/golden_output/servers.com_change_ipv4.exceptions.txt create mode 100644 test/units/modules/system/interfaces_file/fixtures/golden_output/servers.com_change_ipv4.json create mode 100644 test/units/modules/system/interfaces_file/fixtures/golden_output/servers.com_change_ipv6 create mode 100644 test/units/modules/system/interfaces_file/fixtures/golden_output/servers.com_change_ipv6.exceptions.txt create mode 100644 test/units/modules/system/interfaces_file/fixtures/golden_output/servers.com_change_ipv6.json diff --git a/test/units/modules/system/interfaces_file/fixtures/golden_output/address_family_change_ipv4 b/test/units/modules/system/interfaces_file/fixtures/golden_output/address_family_change_ipv4 new file mode 100644 index 00000000000..cb26bf159a9 --- /dev/null +++ b/test/units/modules/system/interfaces_file/fixtures/golden_output/address_family_change_ipv4 @@ -0,0 +1,10 @@ +# The loopback network interface +auto lo eth0 +iface lo inet loopback + +# The primary network interface +iface eth0 inet static + address 192.168.0.42 + +iface eth0 inet6 static + address fc00::1 diff --git a/test/units/modules/system/interfaces_file/fixtures/golden_output/address_family_change_ipv4.exceptions.txt b/test/units/modules/system/interfaces_file/fixtures/golden_output/address_family_change_ipv4.exceptions.txt new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/units/modules/system/interfaces_file/fixtures/golden_output/address_family_change_ipv4.json b/test/units/modules/system/interfaces_file/fixtures/golden_output/address_family_change_ipv4.json new file mode 100644 index 00000000000..bd635949083 --- /dev/null +++ b/test/units/modules/system/interfaces_file/fixtures/golden_output/address_family_change_ipv4.json @@ -0,0 +1,19 @@ +{ + "eth0": { + "address": "fc00::1", + "address_family": "inet6", + "down": [], + "method": "static", + "post-up": [], + "pre-up": [], + "up": [] + }, + "lo": { + "address_family": "inet", + "down": [], + "method": "loopback", + "post-up": [], + "pre-up": [], + "up": [] + } +} \ No newline at end of file diff --git a/test/units/modules/system/interfaces_file/fixtures/golden_output/address_family_change_ipv6 b/test/units/modules/system/interfaces_file/fixtures/golden_output/address_family_change_ipv6 new file mode 100644 index 00000000000..dda68749775 --- /dev/null +++ b/test/units/modules/system/interfaces_file/fixtures/golden_output/address_family_change_ipv6 @@ -0,0 +1,10 @@ +# The loopback network interface +auto lo eth0 +iface lo inet loopback + +# The primary network interface +iface eth0 inet static + address 192.168.0.1 + +iface eth0 inet6 static + address fc00::42 diff --git a/test/units/modules/system/interfaces_file/fixtures/golden_output/address_family_change_ipv6.exceptions.txt b/test/units/modules/system/interfaces_file/fixtures/golden_output/address_family_change_ipv6.exceptions.txt new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/units/modules/system/interfaces_file/fixtures/golden_output/address_family_change_ipv6.json b/test/units/modules/system/interfaces_file/fixtures/golden_output/address_family_change_ipv6.json new file mode 100644 index 00000000000..bd635949083 --- /dev/null +++ b/test/units/modules/system/interfaces_file/fixtures/golden_output/address_family_change_ipv6.json @@ -0,0 +1,19 @@ +{ + "eth0": { + "address": "fc00::1", + "address_family": "inet6", + "down": [], + "method": "static", + "post-up": [], + "pre-up": [], + "up": [] + }, + "lo": { + "address_family": "inet", + "down": [], + "method": "loopback", + "post-up": [], + "pre-up": [], + "up": [] + } +} \ No newline at end of file diff --git a/test/units/modules/system/interfaces_file/fixtures/golden_output/default_dhcp_change_ipv4 b/test/units/modules/system/interfaces_file/fixtures/golden_output/default_dhcp_change_ipv4 new file mode 100644 index 00000000000..6962937416d --- /dev/null +++ b/test/units/modules/system/interfaces_file/fixtures/golden_output/default_dhcp_change_ipv4 @@ -0,0 +1,7 @@ +# The loopback network interface +auto lo eth0 +iface lo inet loopback + +# The primary network interface +iface eth0 inet dhcp + address 192.168.0.42 diff --git a/test/units/modules/system/interfaces_file/fixtures/golden_output/default_dhcp_change_ipv4.exceptions.txt b/test/units/modules/system/interfaces_file/fixtures/golden_output/default_dhcp_change_ipv4.exceptions.txt new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/units/modules/system/interfaces_file/fixtures/golden_output/default_dhcp_change_ipv4.json b/test/units/modules/system/interfaces_file/fixtures/golden_output/default_dhcp_change_ipv4.json new file mode 100644 index 00000000000..bffc17a9897 --- /dev/null +++ b/test/units/modules/system/interfaces_file/fixtures/golden_output/default_dhcp_change_ipv4.json @@ -0,0 +1,18 @@ +{ + "eth0": { + "address_family": "inet", + "down": [], + "method": "dhcp", + "post-up": [], + "pre-up": [], + "up": [] + }, + "lo": { + "address_family": "inet", + "down": [], + "method": "loopback", + "post-up": [], + "pre-up": [], + "up": [] + } +} \ No newline at end of file diff --git a/test/units/modules/system/interfaces_file/fixtures/golden_output/default_dhcp_change_ipv6 b/test/units/modules/system/interfaces_file/fixtures/golden_output/default_dhcp_change_ipv6 new file mode 100644 index 00000000000..bd4522ec09f --- /dev/null +++ b/test/units/modules/system/interfaces_file/fixtures/golden_output/default_dhcp_change_ipv6 @@ -0,0 +1,6 @@ +# The loopback network interface +auto lo eth0 +iface lo inet loopback + +# The primary network interface +iface eth0 inet dhcp diff --git a/test/units/modules/system/interfaces_file/fixtures/golden_output/default_dhcp_change_ipv6.exceptions.txt b/test/units/modules/system/interfaces_file/fixtures/golden_output/default_dhcp_change_ipv6.exceptions.txt new file mode 100644 index 00000000000..04c20891869 --- /dev/null +++ b/test/units/modules/system/interfaces_file/fixtures/golden_output/default_dhcp_change_ipv6.exceptions.txt @@ -0,0 +1,9 @@ +fail_json message: Error: interface eth0 not found +options: +{ + "address_family": "inet6", + "iface": "eth0", + "option": "address", + "state": "present", + "value": "fc00::42" +} \ No newline at end of file diff --git a/test/units/modules/system/interfaces_file/fixtures/golden_output/default_dhcp_change_ipv6.json b/test/units/modules/system/interfaces_file/fixtures/golden_output/default_dhcp_change_ipv6.json new file mode 100644 index 00000000000..bffc17a9897 --- /dev/null +++ b/test/units/modules/system/interfaces_file/fixtures/golden_output/default_dhcp_change_ipv6.json @@ -0,0 +1,18 @@ +{ + "eth0": { + "address_family": "inet", + "down": [], + "method": "dhcp", + "post-up": [], + "pre-up": [], + "up": [] + }, + "lo": { + "address_family": "inet", + "down": [], + "method": "loopback", + "post-up": [], + "pre-up": [], + "up": [] + } +} \ No newline at end of file diff --git a/test/units/modules/system/interfaces_file/fixtures/golden_output/servers.com_change_ipv4 b/test/units/modules/system/interfaces_file/fixtures/golden_output/servers.com_change_ipv4 new file mode 100644 index 00000000000..4356aa47d7e --- /dev/null +++ b/test/units/modules/system/interfaces_file/fixtures/golden_output/servers.com_change_ipv4 @@ -0,0 +1,58 @@ + auto aggi + iface aggi inet static + hwaddress ether 22:44:77:88:D5:96 + address 10.44.15.196 + netmask 255.255.255.248 + mtu 1500 + slaves int1 int2 + bond_mode 4 + bond_miimon 100 + bond_downdelay 200 + bond_updelay 200 + bond_lacp_rate slow + bond_xmit_hash_policy layer3+4 + post-up /sbin/ethtool -K aggi tx off tso off + + auto agge + iface agge inet manual + + auto br0 + iface br0 inet static + bridge_ports agge + hwaddress ether 22:44:77:88:D5:98 + address 188.44.133.76 + netmask 255.255.255.248 + gateway 188.44.133.75 + slaves ext1 ext2 + bond_mode 4 + bond_miimon 100 + bond_downdelay 200 + bond_updelay 200 + bond_lacp_rate slow + bond_xmit_hash_policy layer3+4 + post-up /sbin/ethtool -K agge tx off tso off + + up route add -net 10.0.0.0/8 gw 10.44.15.117 dev aggi + up route add -net 192.168.0.0/16 gw 10.44.15.117 dev aggi + up route add -net 188.44.208.0/21 gw 10.44.15.117 dev aggi + + auto int1 + iface int1 inet manual + bond-master aggi + + auto int2 + iface int2 inet manual + bond-master aggi + + auto ext1 + iface ext1 inet manual + bond-master agge + + auto ext2 + iface ext2 inet manual + bond-master agge + + auto lo + iface lo inet loopback + +source /etc/network/interfaces.d/*.cfg diff --git a/test/units/modules/system/interfaces_file/fixtures/golden_output/servers.com_change_ipv4.exceptions.txt b/test/units/modules/system/interfaces_file/fixtures/golden_output/servers.com_change_ipv4.exceptions.txt new file mode 100644 index 00000000000..a1600d9a67a --- /dev/null +++ b/test/units/modules/system/interfaces_file/fixtures/golden_output/servers.com_change_ipv4.exceptions.txt @@ -0,0 +1,9 @@ +fail_json message: Error: interface eth0 not found +options: +{ + "address_family": "inet", + "iface": "eth0", + "option": "address", + "state": "present", + "value": "192.168.0.42" +} \ No newline at end of file diff --git a/test/units/modules/system/interfaces_file/fixtures/golden_output/servers.com_change_ipv4.json b/test/units/modules/system/interfaces_file/fixtures/golden_output/servers.com_change_ipv4.json new file mode 100644 index 00000000000..0460b552a9d --- /dev/null +++ b/test/units/modules/system/interfaces_file/fixtures/golden_output/servers.com_change_ipv4.json @@ -0,0 +1,101 @@ +{ + "agge": { + "address_family": "inet", + "down": [], + "method": "manual", + "post-up": [], + "pre-up": [], + "up": [] + }, + "aggi": { + "address": "10.44.15.196", + "address_family": "inet", + "bond_downdelay": "200", + "bond_lacp_rate": "slow", + "bond_miimon": "100", + "bond_mode": "4", + "bond_updelay": "200", + "bond_xmit_hash_policy": "layer3+4", + "down": [], + "hwaddress": "ether 22:44:77:88:D5:96", + "method": "static", + "mtu": "1500", + "netmask": "255.255.255.248", + "post-up": [ + "/sbin/ethtool -K aggi tx off tso off" + ], + "pre-up": [], + "slaves": "int1 int2", + "up": [] + }, + "br0": { + "address": "188.44.133.76", + "address_family": "inet", + "bond_downdelay": "200", + "bond_lacp_rate": "slow", + "bond_miimon": "100", + "bond_mode": "4", + "bond_updelay": "200", + "bond_xmit_hash_policy": "layer3+4", + "bridge_ports": "agge", + "down": [], + "gateway": "188.44.133.75", + "hwaddress": "ether 22:44:77:88:D5:98", + "method": "static", + "netmask": "255.255.255.248", + "post-up": [ + "/sbin/ethtool -K agge tx off tso off" + ], + "pre-up": [], + "slaves": "ext1 ext2", + "up": [ + "route add -net 10.0.0.0/8 gw 10.44.15.117 dev aggi", + "route add -net 192.168.0.0/16 gw 10.44.15.117 dev aggi", + "route add -net 188.44.208.0/21 gw 10.44.15.117 dev aggi" + ] + }, + "ext1": { + "address_family": "inet", + "bond-master": "agge", + "down": [], + "method": "manual", + "post-up": [], + "pre-up": [], + "up": [] + }, + "ext2": { + "address_family": "inet", + "bond-master": "agge", + "down": [], + "method": "manual", + "post-up": [], + "pre-up": [], + "up": [] + }, + "int1": { + "address_family": "inet", + "bond-master": "aggi", + "down": [], + "method": "manual", + "post-up": [], + "pre-up": [], + "up": [] + }, + "int2": { + "address_family": "inet", + "bond-master": "aggi", + "down": [], + "method": "manual", + "post-up": [], + "pre-up": [], + "up": [] + }, + "lo": { + "address_family": "inet", + "down": [], + "method": "loopback", + "post-up": [], + "pre-up": [], + "up": [] + } +} \ No newline at end of file diff --git a/test/units/modules/system/interfaces_file/fixtures/golden_output/servers.com_change_ipv6 b/test/units/modules/system/interfaces_file/fixtures/golden_output/servers.com_change_ipv6 new file mode 100644 index 00000000000..4356aa47d7e --- /dev/null +++ b/test/units/modules/system/interfaces_file/fixtures/golden_output/servers.com_change_ipv6 @@ -0,0 +1,58 @@ + auto aggi + iface aggi inet static + hwaddress ether 22:44:77:88:D5:96 + address 10.44.15.196 + netmask 255.255.255.248 + mtu 1500 + slaves int1 int2 + bond_mode 4 + bond_miimon 100 + bond_downdelay 200 + bond_updelay 200 + bond_lacp_rate slow + bond_xmit_hash_policy layer3+4 + post-up /sbin/ethtool -K aggi tx off tso off + + auto agge + iface agge inet manual + + auto br0 + iface br0 inet static + bridge_ports agge + hwaddress ether 22:44:77:88:D5:98 + address 188.44.133.76 + netmask 255.255.255.248 + gateway 188.44.133.75 + slaves ext1 ext2 + bond_mode 4 + bond_miimon 100 + bond_downdelay 200 + bond_updelay 200 + bond_lacp_rate slow + bond_xmit_hash_policy layer3+4 + post-up /sbin/ethtool -K agge tx off tso off + + up route add -net 10.0.0.0/8 gw 10.44.15.117 dev aggi + up route add -net 192.168.0.0/16 gw 10.44.15.117 dev aggi + up route add -net 188.44.208.0/21 gw 10.44.15.117 dev aggi + + auto int1 + iface int1 inet manual + bond-master aggi + + auto int2 + iface int2 inet manual + bond-master aggi + + auto ext1 + iface ext1 inet manual + bond-master agge + + auto ext2 + iface ext2 inet manual + bond-master agge + + auto lo + iface lo inet loopback + +source /etc/network/interfaces.d/*.cfg diff --git a/test/units/modules/system/interfaces_file/fixtures/golden_output/servers.com_change_ipv6.exceptions.txt b/test/units/modules/system/interfaces_file/fixtures/golden_output/servers.com_change_ipv6.exceptions.txt new file mode 100644 index 00000000000..04c20891869 --- /dev/null +++ b/test/units/modules/system/interfaces_file/fixtures/golden_output/servers.com_change_ipv6.exceptions.txt @@ -0,0 +1,9 @@ +fail_json message: Error: interface eth0 not found +options: +{ + "address_family": "inet6", + "iface": "eth0", + "option": "address", + "state": "present", + "value": "fc00::42" +} \ No newline at end of file diff --git a/test/units/modules/system/interfaces_file/fixtures/golden_output/servers.com_change_ipv6.json b/test/units/modules/system/interfaces_file/fixtures/golden_output/servers.com_change_ipv6.json new file mode 100644 index 00000000000..0460b552a9d --- /dev/null +++ b/test/units/modules/system/interfaces_file/fixtures/golden_output/servers.com_change_ipv6.json @@ -0,0 +1,101 @@ +{ + "agge": { + "address_family": "inet", + "down": [], + "method": "manual", + "post-up": [], + "pre-up": [], + "up": [] + }, + "aggi": { + "address": "10.44.15.196", + "address_family": "inet", + "bond_downdelay": "200", + "bond_lacp_rate": "slow", + "bond_miimon": "100", + "bond_mode": "4", + "bond_updelay": "200", + "bond_xmit_hash_policy": "layer3+4", + "down": [], + "hwaddress": "ether 22:44:77:88:D5:96", + "method": "static", + "mtu": "1500", + "netmask": "255.255.255.248", + "post-up": [ + "/sbin/ethtool -K aggi tx off tso off" + ], + "pre-up": [], + "slaves": "int1 int2", + "up": [] + }, + "br0": { + "address": "188.44.133.76", + "address_family": "inet", + "bond_downdelay": "200", + "bond_lacp_rate": "slow", + "bond_miimon": "100", + "bond_mode": "4", + "bond_updelay": "200", + "bond_xmit_hash_policy": "layer3+4", + "bridge_ports": "agge", + "down": [], + "gateway": "188.44.133.75", + "hwaddress": "ether 22:44:77:88:D5:98", + "method": "static", + "netmask": "255.255.255.248", + "post-up": [ + "/sbin/ethtool -K agge tx off tso off" + ], + "pre-up": [], + "slaves": "ext1 ext2", + "up": [ + "route add -net 10.0.0.0/8 gw 10.44.15.117 dev aggi", + "route add -net 192.168.0.0/16 gw 10.44.15.117 dev aggi", + "route add -net 188.44.208.0/21 gw 10.44.15.117 dev aggi" + ] + }, + "ext1": { + "address_family": "inet", + "bond-master": "agge", + "down": [], + "method": "manual", + "post-up": [], + "pre-up": [], + "up": [] + }, + "ext2": { + "address_family": "inet", + "bond-master": "agge", + "down": [], + "method": "manual", + "post-up": [], + "pre-up": [], + "up": [] + }, + "int1": { + "address_family": "inet", + "bond-master": "aggi", + "down": [], + "method": "manual", + "post-up": [], + "pre-up": [], + "up": [] + }, + "int2": { + "address_family": "inet", + "bond-master": "aggi", + "down": [], + "method": "manual", + "post-up": [], + "pre-up": [], + "up": [] + }, + "lo": { + "address_family": "inet", + "down": [], + "method": "loopback", + "post-up": [], + "pre-up": [], + "up": [] + } +} \ No newline at end of file diff --git a/test/units/modules/system/interfaces_file/test_interfaces_file.py b/test/units/modules/system/interfaces_file/test_interfaces_file.py index 70e56e0fb1c..84bae14a741 100644 --- a/test/units/modules/system/interfaces_file/test_interfaces_file.py +++ b/test/units/modules/system/interfaces_file/test_interfaces_file.py @@ -229,3 +229,49 @@ class TestInterfacesFileModule(unittest.TestCase): self.compareInterfacesToFile(ifaces, testfile, "%s_%s.json" % (testfile, testname)) # Restore backup move(backupp, path) + + def test_change_address(self): + testcases = { + "change_ipv4": [ + { + 'iface': 'eth0', + 'address_family': 'inet', + 'option': 'address', + 'value': '192.168.0.42', + 'state': 'present', + } + ], + "change_ipv6": [ + { + 'iface': 'eth0', + 'address_family': 'inet6', + 'option': 'address', + 'value': 'fc00::42', + 'state': 'present', + } + ], + } + for testname, options_list in testcases.items(): + for testfile in self.getTestFiles(): + with tempfile.NamedTemporaryFile() as temp_file: + src_path = os.path.join(fixture_path, testfile) + path = temp_file.name + shutil.copy(src_path, path) + lines, ifaces = interfaces_file.read_interfaces_file(module, path) + backupp = module.backup_local(path) + options = options_list[0] + fail_json_iterations = [] + try: + _, lines = interfaces_file.setInterfaceOption(module, lines, options['iface'], options['option'], + options['value'], options['state'], options['address_family']) + except AnsibleFailJson as e: + fail_json_iterations.append("fail_json message: %s\noptions:\n%s" % + (str(e), json.dumps(options, sort_keys=True, indent=4, separators=(',', ': ')))) + interfaces_file.write_changes(module, [d['line'] for d in lines if 'line' in d], path) + + self.compareStringWithFile("\n=====\n".join(fail_json_iterations), "%s_%s.exceptions.txt" % (testfile, testname)) + + self.compareInterfacesLinesToFile(lines, testfile, "%s_%s" % (testfile, testname)) + self.compareInterfacesToFile(ifaces, testfile, "%s_%s.json" % (testfile, testname)) + # Restore backup + move(backupp, path)