From 43f294b245c697176c437286b27c83b59f6fac85 Mon Sep 17 00:00:00 2001 From: Yedaya Katsman Date: Mon, 16 Jun 2025 22:56:43 +0300 Subject: [PATCH 1/2] test(interfaces): Test for missing ip or ifconfig We have a fallback to `ifconfig` if `ip` doesn't exist, but it wasn't tested anywhere that does have `ip` (pretty sure that's all of our current distro tests). Introduce a parameterized fixture that "hides" `ip` or `ifconfig` and, use it in tests. Also introduce another test that actually makes sure that we find the correct interfaces. Follow up to https://github.com/scop/bash-completion/pull/1090 --- .../test_unit_compgen_available_interfaces.py | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/test/t/unit/test_unit_compgen_available_interfaces.py b/test/t/unit/test_unit_compgen_available_interfaces.py index f3ebac01cfb..f6548feb1a4 100644 --- a/test/t/unit/test_unit_compgen_available_interfaces.py +++ b/test/t/unit/test_unit_compgen_available_interfaces.py @@ -1,6 +1,6 @@ import pytest -from conftest import assert_bash_exec +from conftest import assert_bash_exec, bash_env_saved @pytest.mark.bashcomp(cmd=None) @@ -16,10 +16,30 @@ def functions(self, bash): '_comp__test_compgen() { local -a arr=(00); _comp_compgen -v arr "$@"; _comp__test_dump; }', ) - def test_1_trailing_colons(self, bash, functions): + # We fallback to ifconfig if ip fails, so we want to check also the scenario without ip. + @pytest.fixture(scope="function", params=["ip", "ifconfig"]) + def remove_one_tool(self, request, bash): + assert_bash_exec(bash, f"{request.param}() {{ false; }}") + yield + assert_bash_exec(bash, f"unset -f {request.param}") + + def test_1_trailing_colons(self, bash, functions, remove_one_tool): output = assert_bash_exec( bash, "_comp__test_compgen available_interfaces", want_output=True, ) assert ":>" not in output.strip() + + def test_2_correct_interfaces(self, bash, functions, remove_one_tool): + with bash_env_saved(bash) as bash_env: + # Using emulated ip and ifconfig commands + bash_env.write_variable( + "PATH", "$PWD/shared/bin:$PATH", quote=False + ) + output = assert_bash_exec( + bash, + "_comp__test_compgen available_interfaces", + want_output=True, + ) + assert all(iface in output for iface in ["", ""]) From d0b6449275076607057543f9d2331b97b13e4b27 Mon Sep 17 00:00:00 2001 From: Yedaya Katsman Date: Tue, 17 Jun 2025 00:14:31 +0300 Subject: [PATCH 2/2] test(interfaces): Test completion of veth peers Follow up to https://github.com/scop/bash-completion/pull/1393 Depends on https://github.com/scop/bash-completion/pull/1394 --- test/fixtures/shared/bin/ifconfig | 19 ++++++++++++++++++- test/fixtures/shared/bin/ip | 12 ++++++++++++ .../test_unit_compgen_available_interfaces.py | 5 ++++- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/test/fixtures/shared/bin/ifconfig b/test/fixtures/shared/bin/ifconfig index 59c9140b9d5..c31481a571f 100755 --- a/test/fixtures/shared/bin/ifconfig +++ b/test/fixtures/shared/bin/ifconfig @@ -1,6 +1,6 @@ #!/bin/sh -# Dummy "ifconfig -a" emulator +# Dummy "ifconfig -a" emulator, from net-tools 1.60, ifconfig 1.42 (2001-04-13) cat < mtu 1500 qdisc noqueue state UP mode DEFAULT group default link/ether 33:33:33:33:33:33 brd ff:ff:ff:ff:ff:ff link-netnsid 0 +5: peer2@peer1: mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000 + link/ether 33:33:33:33:33:33 brd ff:ff:ff:ff:ff:ff +6: peer1@peer2: mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000 + link/ether 33:33:33:33:33:33 brd ff:ff:ff:ff:ff:ff EOF exit 0 ;; @@ -27,6 +31,14 @@ EOF valid_lft forever preferred_lft forever inet6 fe80::000:0000:0000:0000/64 scope link valid_lft forever preferred_lft forever +5: peer2@peer1: mtu 1500 qdisc noqueue state UP group default qlen 1000 + link/ether 33:33:33:33:33:33 brd ff:ff:ff:ff:ff:ff + inet6 fe80::000:0000:0000:0000/64 scope link + valid_lft forever preferred_lft forever +6: peer1@peer2: mtu 1500 qdisc noqueue state UP group default qlen 1000 + link/ether 33:33:33:33:33:33 brd ff:ff:ff:ff:ff:ff + inet6 fe80::000:0000:0000:0000/64 scope link + valid_lft forever preferred_lft forever EOF exit 0 ;; diff --git a/test/t/unit/test_unit_compgen_available_interfaces.py b/test/t/unit/test_unit_compgen_available_interfaces.py index f6548feb1a4..5e487283d10 100644 --- a/test/t/unit/test_unit_compgen_available_interfaces.py +++ b/test/t/unit/test_unit_compgen_available_interfaces.py @@ -42,4 +42,7 @@ def test_2_correct_interfaces(self, bash, functions, remove_one_tool): "_comp__test_compgen available_interfaces", want_output=True, ) - assert all(iface in output for iface in ["", ""]) + assert all( + iface in output + for iface in ["", "", "", ""] + )