cli: merge branch 'th/cli-sort-connected-externally'

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1784
This commit is contained in:
Thomas Haller 2023-11-15 09:35:13 +01:00
commit b853a3021c
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
5 changed files with 6277 additions and 6084 deletions

View file

@ -291,65 +291,91 @@ connection_type_to_display(const char *type, NMMetaAccessorGetType get_type)
static int
active_connection_get_state_ord(NMActiveConnection *active)
{
static const NMActiveConnectionState ordered_states[] = {
NM_ACTIVE_CONNECTION_STATE_UNKNOWN,
NM_ACTIVE_CONNECTION_STATE_DEACTIVATED,
NM_ACTIVE_CONNECTION_STATE_DEACTIVATING,
NM_ACTIVE_CONNECTION_STATE_ACTIVATING,
NM_ACTIVE_CONNECTION_STATE_ACTIVATED,
};
NMActiveConnectionState state;
int i;
gboolean is_external;
/* returns an integer related to @active's state, that can be used for sorting
* active connections based on their activation state. */
if (!active)
return -2;
switch (nm_active_connection_get_state(active)) {
case NM_ACTIVE_CONNECTION_STATE_UNKNOWN:
return 0;
case NM_ACTIVE_CONNECTION_STATE_DEACTIVATED:
return 1;
case NM_ACTIVE_CONNECTION_STATE_DEACTIVATING:
return 2;
case NM_ACTIVE_CONNECTION_STATE_ACTIVATING:
return 3;
case NM_ACTIVE_CONNECTION_STATE_ACTIVATED:
return 4;
if (!active)
return -10;
state = nm_active_connection_get_state(active);
is_external = NM_FLAGS_HAS(nm_active_connection_get_state_flags(active),
NM_ACTIVATION_STATE_FLAG_EXTERNAL);
for (i = 0; i < (int) G_N_ELEMENTS(ordered_states); i++) {
if (state == ordered_states[i]) {
if (!is_external)
i += G_N_ELEMENTS(ordered_states);
return i;
}
}
return -1;
return is_external ? -2 : -1;
}
int
nmc_active_connection_cmp(NMActiveConnection *ac_a, NMActiveConnection *ac_b)
{
NMSettingIPConfig *s_ip;
NMRemoteConnection *conn;
NMSettingIPConfig *s_ip4_a;
NMSettingIPConfig *s_ip4_b;
NMSettingIPConfig *s_ip6_a;
NMSettingIPConfig *s_ip6_b;
NMRemoteConnection *conn_a;
NMRemoteConnection *conn_b;
NMIPConfig *da_ip;
NMIPConfig *db_ip;
int da_num_addrs;
int db_num_addrs;
int cmp = 0;
gint64 da_num_addrs;
gint64 db_num_addrs;
gboolean bool_a;
gboolean bool_b;
/* Non-active sort last. */
/* nmc_active_connection_cmp() sorts more-important ACs later. That means,
* - NULL comes first
* - then sorting by state (active_connection_get_state_ord()), with "activated" sorted last.
* - various properties of the AC.
*
* This is basically the inverse order of `nmcli connection`.
*/
/* Non-active (and NULL) sort first! */
NM_CMP_SELF(ac_a, ac_b);
NM_CMP_DIRECT(active_connection_get_state_ord(ac_b), active_connection_get_state_ord(ac_a));
NM_CMP_DIRECT(active_connection_get_state_ord(ac_a), active_connection_get_state_ord(ac_b));
conn_a = nm_active_connection_get_connection(ac_a);
conn_b = nm_active_connection_get_connection(ac_b);
s_ip6_a = conn_a ? nm_connection_get_setting_ip6_config(NM_CONNECTION(conn_a)) : NULL;
s_ip6_b = conn_b ? nm_connection_get_setting_ip6_config(NM_CONNECTION(conn_b)) : NULL;
/* Shared connections (likely hotspots) go on the top if possible */
conn = nm_active_connection_get_connection(ac_a);
s_ip = conn ? nm_connection_get_setting_ip6_config(NM_CONNECTION(conn)) : NULL;
if (s_ip
&& nm_streq(nm_setting_ip_config_get_method(s_ip), NM_SETTING_IP6_CONFIG_METHOD_SHARED))
cmp++;
conn = nm_active_connection_get_connection(ac_b);
s_ip = conn ? nm_connection_get_setting_ip6_config(NM_CONNECTION(conn)) : NULL;
if (s_ip
&& nm_streq(nm_setting_ip_config_get_method(s_ip), NM_SETTING_IP6_CONFIG_METHOD_SHARED))
cmp--;
NM_CMP_RETURN(cmp);
bool_a = (s_ip6_a
&& nm_streq(nm_setting_ip_config_get_method(s_ip6_a),
NM_SETTING_IP6_CONFIG_METHOD_SHARED));
bool_b = (s_ip6_b
&& nm_streq(nm_setting_ip_config_get_method(s_ip6_b),
NM_SETTING_IP6_CONFIG_METHOD_SHARED));
NM_CMP_DIRECT(bool_a, bool_b);
conn = nm_active_connection_get_connection(ac_a);
s_ip = conn ? nm_connection_get_setting_ip4_config(NM_CONNECTION(conn)) : NULL;
if (s_ip
&& nm_streq(nm_setting_ip_config_get_method(s_ip), NM_SETTING_IP4_CONFIG_METHOD_SHARED))
cmp++;
conn = nm_active_connection_get_connection(ac_b);
s_ip = conn ? nm_connection_get_setting_ip4_config(NM_CONNECTION(conn)) : NULL;
if (s_ip
&& nm_streq(nm_setting_ip_config_get_method(s_ip), NM_SETTING_IP4_CONFIG_METHOD_SHARED))
cmp--;
NM_CMP_RETURN(cmp);
s_ip4_a = conn_a ? nm_connection_get_setting_ip4_config(NM_CONNECTION(conn_a)) : NULL;
s_ip4_b = conn_b ? nm_connection_get_setting_ip4_config(NM_CONNECTION(conn_b)) : NULL;
bool_a = (s_ip4_a
&& nm_streq(nm_setting_ip_config_get_method(s_ip4_a),
NM_SETTING_IP4_CONFIG_METHOD_SHARED));
bool_b = (s_ip4_b
&& nm_streq(nm_setting_ip_config_get_method(s_ip4_b),
NM_SETTING_IP4_CONFIG_METHOD_SHARED));
NM_CMP_DIRECT(bool_a, bool_b);
/* VPNs go next */
NM_CMP_DIRECT(!!nm_active_connection_get_vpn(ac_a), !!nm_active_connection_get_vpn(ac_b));
@ -367,9 +393,9 @@ nmc_active_connection_cmp(NMActiveConnection *ac_a, NMActiveConnection *ac_b)
db_num_addrs = db_ip ? nm_ip_config_get_addresses(db_ip)->len : 0;
da_ip = nm_active_connection_get_ip6_config(ac_a);
da_num_addrs += da_ip ? nm_ip_config_get_addresses(da_ip)->len : 0;
da_num_addrs += (gint64) (da_ip ? nm_ip_config_get_addresses(da_ip)->len : 0u);
db_ip = nm_active_connection_get_ip6_config(ac_b);
db_num_addrs += db_ip ? nm_ip_config_get_addresses(db_ip)->len : 0;
db_num_addrs += (gint64) (db_ip ? nm_ip_config_get_addresses(db_ip)->len : 0u);
NM_CMP_DIRECT(da_num_addrs, db_num_addrs);
@ -1447,7 +1473,7 @@ get_ac_for_connection_cmp(gconstpointer pa, gconstpointer pb)
NMActiveConnection *ac_a = *((NMActiveConnection *const *) pa);
NMActiveConnection *ac_b = *((NMActiveConnection *const *) pb);
NM_CMP_RETURN(nmc_active_connection_cmp(ac_a, ac_b));
NM_CMP_RETURN(nmc_active_connection_cmp(ac_b, ac_a));
NM_CMP_DIRECT_STRCMP0(nm_active_connection_get_id(ac_a), nm_active_connection_get_id(ac_b));
NM_CMP_DIRECT_STRCMP0(nm_active_connection_get_connection_type(ac_a),
nm_active_connection_get_connection_type(ac_b));

View file

@ -1082,8 +1082,8 @@ compare_devices(const void *a, const void *b)
NMActiveConnection *da_ac = nm_device_get_active_connection(da);
NMActiveConnection *db_ac = nm_device_get_active_connection(db);
NM_CMP_DIRECT(nm_device_get_state(db), nm_device_get_state(da));
NM_CMP_RETURN(nmc_active_connection_cmp(db_ac, da_ac));
NM_CMP_DIRECT(nm_device_get_state(db), nm_device_get_state(da));
NM_CMP_DIRECT_STRCMP0(nm_device_get_type_description(da), nm_device_get_type_description(db));
NM_CMP_DIRECT_STRCMP0(nm_device_get_iface(da), nm_device_get_iface(db));
NM_CMP_DIRECT_STRCMP0(nm_object_get_path(NM_OBJECT(da)), nm_object_get_path(NM_OBJECT(db)));

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -201,7 +201,6 @@ _UNSTABLE_OUTPUT = object()
class Util:
_signal_no_lookup = {
1: "SIGHUP",
2: "SIGINT",
@ -658,7 +657,6 @@ class Util:
@staticmethod
def cmd_create_argv(cmd_path, args, with_valgrind=None):
if with_valgrind is None:
with_valgrind = conf.get(ENV_NM_TEST_VALGRIND)
@ -1092,9 +1090,7 @@ class NMTestContext:
srv.shutdown()
def async_start(self, wait_all=False):
while True:
while True:
for async_job in list(self._async_jobs[0 : self.MAX_JOBS]):
async_job.start()
@ -1134,7 +1130,6 @@ class NMTestContext:
self._async_jobs.append(async_job)
def run_post(self):
self.async_wait()
self.srv_shutdown()
@ -1302,7 +1297,6 @@ class TestNmcli(unittest.TestCase):
extra_env=None,
sync_barrier=None,
):
frame = sys._getframe(1)
if langs is not None:
@ -1350,7 +1344,6 @@ class TestNmcli(unittest.TestCase):
sync_barrier,
frame,
):
if sync_barrier:
self.ctx.async_wait()
@ -1428,7 +1421,6 @@ class TestNmcli(unittest.TestCase):
self.ctx.ctx_results.append(None)
def complete_cb(async_job, returncode, stdout, stderr):
if expected_stdout is _UNSTABLE_OUTPUT:
stdout = "<UNSTABLE OUTPUT>".encode("utf-8")
else:
@ -1559,7 +1551,6 @@ class TestNmcli(unittest.TestCase):
@nm_test
def test_001(self):
self.call_nmcli_l([])
self.call_nmcli_l(
@ -1626,7 +1617,6 @@ class TestNmcli(unittest.TestCase):
self.call_nmcli_l(["c", "s"], replace_stdout=replace_uuids)
for con_name, apn in con_gsm_list:
replace_uuids.append(
self.ctx.srv.ReplaceTextConUuid(
con_name, "UUID-" + con_name + "-REPLACED-REPLACED-REPL"
@ -1753,6 +1743,8 @@ class TestNmcli(unittest.TestCase):
dbus.UInt32(NM.ActiveConnectionState.DEACTIVATING),
)
self.call_nmcli_l(["-f", "all", "d"], replace_stdout=replace_uuids)
self.call_nmcli_l([], replace_stdout=replace_uuids)
for i in [0, 1]:
@ -1886,8 +1878,17 @@ class TestNmcli(unittest.TestCase):
self.call_nmcli_l([], replace_stdout=replace_uuids)
for mode in Util.iter_nmcli_output_modes():
self.call_nmcli(
["-f", "all", "connection", "show", "--order", "na:-active"],
replace_stdout=replace_uuids,
)
self.call_nmcli(
["-f", "all", "connection", "show", "--order", "active:-na"],
replace_stdout=replace_uuids,
)
for mode in Util.iter_nmcli_output_modes():
self.call_nmcli_l(
mode + ["con", "s", "con-vpn-1"], replace_stdout=replace_uuids
)
@ -2051,9 +2052,34 @@ class TestNmcli(unittest.TestCase):
replace_cmd=replace_uuids,
)
replace_uuids.append(
self.ctx.srv.ReplaceTextConUuid(
"con-xx2", "UUID-con-xx2-REPLACED-REPLACED-REPLA"
)
)
self.call_nmcli(
["c", "add", "type", "ethernet", "con-name", "con-xx2", "ifname", "eth1"],
replace_stdout=replace_uuids,
)
self.ctx.srv.op_SetActiveConnectionStateChangedDelay(
"/org/freedesktop/NetworkManager/Devices/2", 50000
)
self.call_nmcli(["-wait", "0", "con", "up", "con-xx2"])
self.call_nmcli(["con", "up", "con-xx1"])
self.call_nmcli_l(
["-f", "all", "device", "status"],
replace_stdout=replace_uuids,
)
self.call_nmcli_l(
["-f", "all", "connection", "show"],
replace_stdout=replace_uuids,
)
@nm_test_no_dbus
def test_offline(self):
# Make sure we're not using D-Bus
no_dbus_env = {
"DBUS_SYSTEM_BUS_ADDRESS": "very:invalid",