diff options
-rw-r--r-- | python/vyos/template.py | 5 | ||||
-rw-r--r-- | src/tests/test_template.py | 10 |
2 files changed, 15 insertions, 0 deletions
diff --git a/python/vyos/template.py b/python/vyos/template.py index 85e4d12b3..7810f5edd 100644 --- a/python/vyos/template.py +++ b/python/vyos/template.py @@ -207,6 +207,11 @@ def network_from_ipv4(address): cidr_prefix = ip_interface(f'{address}/{netmask}').network return address_from_cidr(cidr_prefix) +@register_filter('is_interface') +def is_interface(interface): + """ Check if parameter is a valid local interface name """ + return os.path.exists(f'/sys/class/net/{interface}') + @register_filter('is_ip') def is_ip(addr): """ Check addr if it is an IPv4 or IPv6 address """ diff --git a/src/tests/test_template.py b/src/tests/test_template.py index 7800d007f..67c0fe84a 100644 --- a/src/tests/test_template.py +++ b/src/tests/test_template.py @@ -14,13 +14,23 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. +import os import vyos.template + from unittest import TestCase class TestVyOSTemplate(TestCase): def setUp(self): pass + def test_is_interface(self): + for interface in ['lo', 'eth0']: + if os.path.exists(f'/sys/class/net/{interface}'): + self.assertTrue(vyos.template.is_interface(interface)) + else: + self.assertFalse(vyos.template.is_interface(interface)) + self.assertFalse(vyos.template.is_interface('non-existent')) + def test_is_ip(self): self.assertTrue(vyos.template.is_ip('192.0.2.1')) self.assertTrue(vyos.template.is_ip('2001:db8::1')) |