Skip to content

Commit

Permalink
fix(tests): avoid deprecated assertRaisesRegexp for assertRaisesRegex
Browse files Browse the repository at this point in the history
Avoid warnings:

    src/tests/python/firewalld_test.py::TestFirewallD::test_zone_add_get_query_removeForwardPort
      /data/src/firewalld/src/tests/python/firewalld_test.py:226: DeprecationWarning: Please use assertRaisesRegex instead.
        self.assertRaisesRegexp(Exception, 'ALREADY_ENABLED', self.fw_zone.addForwardPort, zone, port, protocol, toport, toaddr, 0)

unittest.TestCase.assertRaisesRegexp is deprecated for assertRaisesRegex since
Python 3.2. It exists since Python 3.1. Even CentOS Linux 8 brings Python 3.6.

https://docs.python.org/3.3/library/unittest.html#unittest.TestCase.assertRaisesRegex
https://bugs.python.org/issue10273
(cherry picked from commit e502fe6)
  • Loading branch information
thom311 authored and erig0 committed Oct 2, 2023
1 parent 0bfffb3 commit ebf398c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 35 deletions.
36 changes: 18 additions & 18 deletions src/tests/python/firewalld_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def test_zones(self):
"""

print ("\nGetting invalid zone")
self.assertRaisesRegexp(Exception, 'INVALID_ZONE', self.fw.config().getZoneByName, "dummyname")
self.assertRaisesRegex(Exception, 'INVALID_ZONE', self.fw.config().getZoneByName, "dummyname")

zone_version = "1.0"
zone_short = "Testing"
Expand All @@ -81,9 +81,9 @@ def test_zones(self):
settings.setForwardPorts(zone_forward_ports)

print ("Adding zone with name that already exists")
self.assertRaisesRegexp(Exception, 'NAME_CONFLICT', self.fw.config().addZone, "home", settings)
self.assertRaisesRegex(Exception, 'NAME_CONFLICT', self.fw.config().addZone, "home", settings)
print ("Adding zone with empty name")
self.assertRaisesRegexp(Exception, 'INVALID_NAME', self.fw.config().addZone, "", settings)
self.assertRaisesRegex(Exception, 'INVALID_NAME', self.fw.config().addZone, "", settings)
zone_name = "test"
print ("Adding proper zone")
self.fw.config().addZone (zone_name, settings)
Expand Down Expand Up @@ -124,13 +124,13 @@ def test_zones(self):

print ("Renaming zone to name that already exists")
config_zone = self.fw.config().getZoneByName(zone_name)
self.assertRaisesRegexp(Exception, 'NAME_CONFLICT', config_zone.rename, "home")
self.assertRaisesRegex(Exception, 'NAME_CONFLICT', config_zone.rename, "home")
new_zone_name = "renamed"
print ("Renaming zone '%s' to '%s'" % (zone_name, new_zone_name))
config_zone.rename(new_zone_name)

print ("Checking whether the zone '%s' is accessible (it shouldn't be)" % zone_name)
self.assertRaisesRegexp(Exception, 'INVALID_ZONE', self.fw.config().getZoneByName, zone_name)
self.assertRaisesRegex(Exception, 'INVALID_ZONE', self.fw.config().getZoneByName, zone_name)
print ("Checking whether the zone '%s' is accessible" % new_zone_name)
config_zone = self.fw.config().getZoneByName(new_zone_name)
zone_settings = config_zone.getSettings()
Expand All @@ -147,7 +147,7 @@ def test_zones(self):
print ("Removing the zone '%s'" % new_zone_name)
config_zone.remove()
print ("Checking whether the removed zone is accessible (it shouldn't be)")
self.assertRaisesRegexp(Exception, 'INVALID_ZONE', self.fw.config().getZoneByName, new_zone_name)
self.assertRaisesRegex(Exception, 'INVALID_ZONE', self.fw.config().getZoneByName, new_zone_name)

# TODO test loadDefaults() ?

Expand All @@ -167,7 +167,7 @@ def test_services(self):
"""

print ("\nGetting invalid service")
self.assertRaisesRegexp(Exception, 'INVALID_SERVICE', self.fw.config().getServiceByName, "dummyname")
self.assertRaisesRegex(Exception, 'INVALID_SERVICE', self.fw.config().getServiceByName, "dummyname")

service_version = "1.0"
service_short = "Testing"
Expand All @@ -184,9 +184,9 @@ def test_services(self):
settings.setDestinations(service_destinations)

print ("Adding service with name that already exists")
self.assertRaisesRegexp(Exception, 'NAME_CONFLICT', self.fw.config().addService, "mdns", settings)
self.assertRaisesRegex(Exception, 'NAME_CONFLICT', self.fw.config().addService, "mdns", settings)
print ("Adding service with empty name")
self.assertRaisesRegexp(Exception, 'INVALID_NAME', self.fw.config().addService, "", settings)
self.assertRaisesRegex(Exception, 'INVALID_NAME', self.fw.config().addService, "", settings)
service_name = "test"
print ("Adding proper service")
self.fw.config().addService (service_name, settings)
Expand All @@ -212,13 +212,13 @@ def test_services(self):

print ("Renaming service to name that already exists")
config_service = self.fw.config().getServiceByName(service_name)
self.assertRaisesRegexp(Exception, 'NAME_CONFLICT', config_service.rename, "mdns")
self.assertRaisesRegex(Exception, 'NAME_CONFLICT', config_service.rename, "mdns")
new_service_name = "renamed"
print ("Renaming service '%s' to '%s'" % (service_name, new_service_name))
config_service.rename(new_service_name)

print ("Checking whether the service '%s' is accessible (it shouldn't be)" % service_name)
self.assertRaisesRegexp(Exception, 'INVALID_SERVICE', self.fw.config().getServiceByName, service_name)
self.assertRaisesRegex(Exception, 'INVALID_SERVICE', self.fw.config().getServiceByName, service_name)
print ("Checking whether the service '%s' is accessible" % new_service_name)
config_service = self.fw.config().getServiceByName(new_service_name)
service_settings = config_service.getSettings()
Expand All @@ -232,7 +232,7 @@ def test_services(self):
print ("Removing the service '%s'" % new_service_name)
config_service.remove()
print ("Checking whether the removed service is accessible (it shouldn't be)")
self.assertRaisesRegexp(Exception, 'INVALID_SERVICE', self.fw.config().getServiceByName, new_service_name)
self.assertRaisesRegex(Exception, 'INVALID_SERVICE', self.fw.config().getServiceByName, new_service_name)

# TODO test loadDefaults() ?

Expand All @@ -251,7 +251,7 @@ def test_icmptypes(self):
remove()
"""
print ("\nGetting invalid icmp-type")
self.assertRaisesRegexp(Exception, 'INVALID_ICMPTYPE', self.fw.config().getIcmpTypeByName, "dummyname")
self.assertRaisesRegex(Exception, 'INVALID_ICMPTYPE', self.fw.config().getIcmpTypeByName, "dummyname")

icmptype_version = "1.0"
icmptype_short = "Testing"
Expand All @@ -264,9 +264,9 @@ def test_icmptypes(self):
settings.setDestinations(icmptype_destinations)

print ("Adding icmp type with name that already exists")
self.assertRaisesRegexp(Exception, 'NAME_CONFLICT', self.fw.config().addIcmpType, "echo-reply", settings)
self.assertRaisesRegex(Exception, 'NAME_CONFLICT', self.fw.config().addIcmpType, "echo-reply", settings)
print ("Adding icmp type with empty name")
self.assertRaisesRegexp(Exception, 'INVALID_NAME', self.fw.config().addIcmpType, "", settings)
self.assertRaisesRegex(Exception, 'INVALID_NAME', self.fw.config().addIcmpType, "", settings)
icmptype_name = "test"
print ("Adding proper icmp type")
self.fw.config().addIcmpType (icmptype_name, settings)
Expand All @@ -288,13 +288,13 @@ def test_icmptypes(self):

print ("Renaming icmp type to name that already exists")
config_icmptype = self.fw.config().getIcmpTypeByName(icmptype_name)
self.assertRaisesRegexp(Exception, 'NAME_CONFLICT', config_icmptype.rename, "echo-reply")
self.assertRaisesRegex(Exception, 'NAME_CONFLICT', config_icmptype.rename, "echo-reply")
new_icmptype_name = "renamed"
print ("Renaming icmp type '%s' to '%s'" % (icmptype_name, new_icmptype_name))
config_icmptype.rename(new_icmptype_name)

print ("Checking whether the icmp type '%s' is accessible (it shouldn't be)" % icmptype_name)
self.assertRaisesRegexp(Exception, 'INVALID_ICMPTYPE', self.fw.config().getIcmpTypeByName, icmptype_name)
self.assertRaisesRegex(Exception, 'INVALID_ICMPTYPE', self.fw.config().getIcmpTypeByName, icmptype_name)
print ("Checking whether the icmp type '%s' is accessible" % new_icmptype_name)
config_icmptype = self.fw.config().getIcmpTypeByName(new_icmptype_name)
icmptype_settings = config_icmptype.getSettings()
Expand All @@ -306,7 +306,7 @@ def test_icmptypes(self):
print ("Removing the icmp type '%s'" % new_icmptype_name)
config_icmptype.remove()
print ("Checking whether the removed icmp type is accessible (it shouldn't be)")
self.assertRaisesRegexp(Exception, 'INVALID_ICMPTYPE', self.fw.config().getIcmpTypeByName, new_icmptype_name)
self.assertRaisesRegex(Exception, 'INVALID_ICMPTYPE', self.fw.config().getIcmpTypeByName, new_icmptype_name)

# TODO test loadDefaults() ?

Expand Down
8 changes: 4 additions & 4 deletions src/tests/python/firewalld_direct.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def tearDown(self):
def test_add_removeChain(self):
self.fw_direct.addChain("ipv4", "filter", "direct_foo2")
# Re-adding
self.assertRaisesRegexp(Exception, 'ALREADY_ENABLED',
self.assertRaisesRegex(Exception, 'ALREADY_ENABLED',
self.fw_direct.addChain, "ipv4", "filter", "direct_foo2")
ret = self.fw_direct.getChains("ipv4", "filter")
self.assertTrue(len(ret)==2) # "direct_foo1" and "direct_foo2"
Expand All @@ -66,7 +66,7 @@ def test_add_removeChain(self):

self.fw_direct.removeChain("ipv4", "filter", "direct_foo2")
# Re-removing
self.assertRaisesRegexp(Exception, 'NOT_ENABLED',
self.assertRaisesRegex(Exception, 'NOT_ENABLED',
self.fw_direct.removeChain, "ipv4", "filter", "direct_foo2")
ret = self.fw_direct.getChains("ipv4", "filter")
self.assertTrue(len(ret)==1) # "direct_foo1"
Expand All @@ -81,7 +81,7 @@ def test_add_removeRule(self):
self.fw_direct.addRule("ipv4", "filter", "direct_foo1", -10, [ "-m", "tcp", "-p", "tcp", "--dport", "330", "-j", "ACCEPT" ])
self.fw_direct.addRule("ipv4", "filter", "direct_foo1", -5, [ "-m", "udp", "-p", "udp", "--dport", "331", "-j", "ACCEPT" ])
# Re-adding
self.assertRaisesRegexp(Exception, 'ALREADY_ENABLED',
self.assertRaisesRegex(Exception, 'ALREADY_ENABLED',
self.fw_direct.addRule, "ipv4", "filter", "direct_foo1", -5, [ "-m", "udp", "-p", "udp", "--dport", "331", "-j", "ACCEPT" ])
ret = self.fw_direct.queryRule("ipv4", "filter", "direct_foo1", -5, [ "-m", "udp", "-p", "udp", "--dport", "331", "-j", "ACCEPT" ])
self.assertTrue(dbus_to_python(ret))
Expand All @@ -96,7 +96,7 @@ def test_add_removeRule(self):
self.fw_direct.removeRule("ipv4", "filter", "direct_foo1", 0, [ "-m", "tcp", "-p", "tcp", "--dport", "333", "-j", "ACCEPT" ])
self.fw_direct.removeRule("ipv4", "filter", "direct_foo1", 1, [ "-m", "tcp", "-p", "tcp", "--dport", "334", "-j", "ACCEPT" ])
# Re-removing
self.assertRaisesRegexp(Exception, 'NOT_ENABLED',
self.assertRaisesRegex(Exception, 'NOT_ENABLED',
self.fw_direct.removeRule, "ipv4", "filter", "direct_foo1", 1, [ "-m", "tcp", "-p", "tcp", "--dport", "334", "-j", "ACCEPT" ])
ret = self.fw_direct.queryRule("ipv4", "filter", "direct_foo1", 1, [ "-m", "tcp", "-p", "tcp", "--dport", "334", "-j", "ACCEPT" ])
self.assertFalse(dbus_to_python(ret))
Expand Down
26 changes: 13 additions & 13 deletions src/tests/python/firewalld_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,14 @@ def test_zone_add_remove_queryInterface(self):
self.assertTrue(self.fw_zone.queryInterface(zone, interface))

print ("Re-adding")
self.assertRaisesRegexp(Exception, 'ZONE_ALREADY_SET', self.fw_zone.addInterface, zone, interface)
self.assertRaisesRegex(Exception, 'ZONE_ALREADY_SET', self.fw_zone.addInterface, zone, interface)

zone = "block"
print ("Re-adding interface '%s' to '%s' zone" % (interface, zone))
self.assertRaisesRegexp(Exception, 'ZONE_CONFLICT', self.fw_zone.addInterface, zone, interface)
self.assertRaisesRegex(Exception, 'ZONE_CONFLICT', self.fw_zone.addInterface, zone, interface)

print ("Removing interface '%s' from '%s' zone" % (interface, zone))
self.assertRaisesRegexp(Exception, 'ZONE_CONFLICT', self.fw_zone.removeInterface, zone, interface)
self.assertRaisesRegex(Exception, 'ZONE_CONFLICT', self.fw_zone.removeInterface, zone, interface)

zone = "trusted"
print ("Removing interface '%s' from '%s' zone" % (interface, zone))
Expand Down Expand Up @@ -151,7 +151,7 @@ def test_zone_add_get_query_removeService(self):
ret = self.fw_zone.addService(zone, service, 0)
self.assertEqual(ret, zone)
print ("Re-adding")
self.assertRaisesRegexp(Exception, 'ALREADY_ENABLED', self.fw_zone.addService, zone, service, 0)
self.assertRaisesRegex(Exception, 'ALREADY_ENABLED', self.fw_zone.addService, zone, service, 0)

print ("Get services of zone '%s'" % (zone))
ret = self.fw_zone.getServices(zone)
Expand All @@ -162,7 +162,7 @@ def test_zone_add_get_query_removeService(self):
ret = self.fw_zone.removeService(zone, service)
self.assertEqual(ret, zone)
print ("Re-removing")
self.assertRaisesRegexp(Exception, 'NOT_ENABLED', self.fw_zone.removeService, zone, service)
self.assertRaisesRegex(Exception, 'NOT_ENABLED', self.fw_zone.removeService, zone, service)

zone = "dmz"
timeout = 2
Expand All @@ -182,7 +182,7 @@ def test_zone_add_get_query_removePort(self):
ret = self.fw_zone.addPort(zone, port, protocol, 0)
self.assertEqual(ret, zone)
print ("Re-adding port")
self.assertRaisesRegexp(Exception, 'ALREADY_ENABLED', self.fw_zone.addPort, zone, port, protocol, 0)
self.assertRaisesRegex(Exception, 'ALREADY_ENABLED', self.fw_zone.addPort, zone, port, protocol, 0)

print ("Get ports of zone '%s': " % (zone))
ret = self.fw_zone.getPorts(zone)
Expand All @@ -193,7 +193,7 @@ def test_zone_add_get_query_removePort(self):
ret = self.fw_zone.removePort(zone, port, protocol)
self.assertEqual(ret, zone)
print ("Re-removing")
self.assertRaisesRegexp(Exception, 'NOT_ENABLED', self.fw_zone.removePort, zone, port, protocol)
self.assertRaisesRegex(Exception, 'NOT_ENABLED', self.fw_zone.removePort, zone, port, protocol)

port = "443-445"
protocol="udp"
Expand All @@ -213,7 +213,7 @@ def test_zone_add_query_removeMasquerade(self):
ret = self.fw_zone.addMasquerade(zone, 0)
self.assertEqual(ret, zone)
print ("Re-adding")
self.assertRaisesRegexp(Exception, 'ALREADY_ENABLED', self.fw_zone.addMasquerade, zone, 0)
self.assertRaisesRegex(Exception, 'ALREADY_ENABLED', self.fw_zone.addMasquerade, zone, 0)

print ("Checking if masquerade is added to zone '%s'" % (zone))
self.assertTrue(self.fw_zone.queryMasquerade(zone))
Expand All @@ -222,7 +222,7 @@ def test_zone_add_query_removeMasquerade(self):
ret = self.fw_zone.removeMasquerade(zone)
self.assertEqual(ret, zone)
print ("Re-adding")
self.assertRaisesRegexp(Exception, 'NOT_ENABLED', self.fw_zone.removeMasquerade, zone)
self.assertRaisesRegex(Exception, 'NOT_ENABLED', self.fw_zone.removeMasquerade, zone)

zone = "dmz"
timeout = 2
Expand All @@ -244,7 +244,7 @@ def test_zone_add_get_query_removeForwardPort(self):
ret = self.fw_zone.addForwardPort(zone, port, protocol, toport, toaddr, 0)
self.assertEqual(ret, zone)
print ("Re-adding")
self.assertRaisesRegexp(Exception, 'ALREADY_ENABLED', self.fw_zone.addForwardPort, zone, port, protocol, toport, toaddr, 0)
self.assertRaisesRegex(Exception, 'ALREADY_ENABLED', self.fw_zone.addForwardPort, zone, port, protocol, toport, toaddr, 0)

print ("Get forward ports of zone '%s': " % (zone))
ret = self.fw_zone.getForwardPorts(zone)
Expand All @@ -255,7 +255,7 @@ def test_zone_add_get_query_removeForwardPort(self):
ret = self.fw_zone.removeForwardPort(zone, port, protocol, toport, toaddr)
self.assertEqual(ret, zone)
print ("Re-removing")
self.assertRaisesRegexp(Exception, 'NOT_ENABLED', self.fw_zone.removeForwardPort, zone, port, protocol, toport, toaddr)
self.assertRaisesRegex(Exception, 'NOT_ENABLED', self.fw_zone.removeForwardPort, zone, port, protocol, toport, toaddr)

port = "443-445"
protocol="udp"
Expand All @@ -278,7 +278,7 @@ def test_zone_add_get_query_removeIcmpBlock(self):
ret = self.fw_zone.addIcmpBlock(zone, icmp, 0)
self.assertEqual(ret, zone)
print ("Re-adding")
self.assertRaisesRegexp(Exception, 'ALREADY_ENABLED', self.fw_zone.addIcmpBlock, zone, icmp, 0)
self.assertRaisesRegex(Exception, 'ALREADY_ENABLED', self.fw_zone.addIcmpBlock, zone, icmp, 0)

print ("Get icmp blocks of zone '%s': " % (zone))
ret = self.fw_zone.getIcmpBlocks(zone)
Expand All @@ -289,7 +289,7 @@ def test_zone_add_get_query_removeIcmpBlock(self):
ret = self.fw_zone.removeIcmpBlock(zone, icmp)
self.assertEqual(ret, zone)
print ("Re-removing")
self.assertRaisesRegexp(Exception, 'NOT_ENABLED', self.fw_zone.removeIcmpBlock, zone, icmp)
self.assertRaisesRegex(Exception, 'NOT_ENABLED', self.fw_zone.removeIcmpBlock, zone, icmp)

icmp = "redirect"
zone = "dmz"
Expand Down

0 comments on commit ebf398c

Please sign in to comment.