Skip to content

Commit

Permalink
fix(nm): cannot configure ethernet interfaces if the cable is disconn…
Browse files Browse the repository at this point in the history
…ected [backport release-5.3.0] (#4617)

fix(nm): cannot configure ethernet interfaces if the cable is disconnected (#4606)

* fix(nm): cannot configure ethernet interfaces if the cable is disconnected

* refactor: remove unnecessary setting

* fix(nm.test): a miracle

* fix: remove leftover debug prints

* feat(nm): catch exception on Activate calls

(cherry picked from commit 7584e50)

Co-authored-by: Mattia Dal Ben <[email protected]>
  • Loading branch information
github-actions[bot] and mattdibi authored Apr 28, 2023
1 parent 29a5fbc commit 4db5765
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import org.eclipse.kura.nm.status.NMStatusConverter;
import org.eclipse.kura.nm.status.SimProperties;
import org.eclipse.kura.nm.status.SupportedChannelsProperties;
import org.freedesktop.AddAndActivateConnectionTuple;
import org.freedesktop.NetworkManager;
import org.freedesktop.dbus.DBusPath;
import org.freedesktop.dbus.connections.impl.DBusConnection;
Expand Down Expand Up @@ -394,16 +393,22 @@ private void enableInterface(String deviceId, NetworkProperties properties, Devi

if (connection.isPresent()) {
connection.get().Update(newConnectionSettings);
this.nm.ActivateConnection(new DBusPath(connection.get().getObjectPath()),
new DBusPath(device.getObjectPath()), new DBusPath("/"));
} else {
AddAndActivateConnectionTuple createdConnectionTuple = this.nm.AddAndActivateConnection(
newConnectionSettings, new DBusPath(device.getObjectPath()), new DBusPath("/"));
Settings settings = this.dbusConnection.getRemoteObject(NM_BUS_NAME, NM_SETTINGS_BUS_PATH, Settings.class);
DBusPath createdConnectionPath = settings.AddConnection(newConnectionSettings);
Connection createdConnection = this.dbusConnection.getRemoteObject(NM_BUS_NAME,
createdConnectionTuple.getPath().getPath(), Connection.class);
createdConnectionPath.getPath(), Connection.class);
connection = Optional.of(createdConnection);
}

try {
this.nm.ActivateConnection(new DBusPath(connection.get().getObjectPath()),
new DBusPath(device.getObjectPath()), new DBusPath("/"));
dsLock.waitForSignal();
} catch (DBusExecutionException e) {
logger.warn("Couldn't complete activation of {} interface, caused by:", deviceId, e);
}

// Housekeeping
List<Connection> availableConnections = getAvaliableConnections(device);
for (Connection availableConnection : availableConnections) {
Expand All @@ -412,8 +417,6 @@ private void enableInterface(String deviceId, NetworkProperties properties, Devi
}
}

dsLock.waitForSignal();

if (deviceType == NMDeviceType.NM_DEVICE_TYPE_MODEM) {
int delayMinutes = properties.get(Integer.class, "net.interface.%s.config.resetTimeout", deviceId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
import org.eclipse.kura.net.status.modem.RegistrationStatus;
import org.eclipse.kura.net.status.modem.Sim;
import org.eclipse.kura.net.status.modem.SimType;
import org.freedesktop.AddAndActivateConnectionTuple;
import org.freedesktop.ModemManager1;
import org.freedesktop.NetworkManager;
import org.freedesktop.dbus.DBusPath;
Expand Down Expand Up @@ -95,6 +94,7 @@ public class NMDbusConnectorTest {
private final DBusConnection dbusConnection = mock(DBusConnection.class, RETURNS_SMART_NULLS);
private final NetworkManager mockedNetworkManager = mock(NetworkManager.class);
private final ModemManager1 mockedModemManager = mock(ModemManager1.class);
private final Settings mockedNetworkManagerSettings = mock(Settings.class);
private NMDbusConnector instanceNMDbusConnector;
private DBusConnection dbusConnectionInternal;
private final CommandExecutorService commandExecutorService = mock(CommandExecutorService.class);
Expand Down Expand Up @@ -918,11 +918,17 @@ public void givenBasicMockedDbusConnector() throws DBusException, IOException {
when(this.dbusConnection.getRemoteObject(eq("org.freedesktop.NetworkManager"),
eq("/org/freedesktop/NetworkManager"), any()))
.thenReturn(this.mockedNetworkManager);

when(this.dbusConnection.getRemoteObject(eq("org.freedesktop.NetworkManager"),
eq("/org/freedesktop/NetworkManager/Settings"), any()))
.thenReturn(this.mockedNetworkManagerSettings);

this.instanceNMDbusConnector = NMDbusConnector.getInstance(this.dbusConnection);

when(this.dbusConnection.getRemoteObject(eq("org.freedesktop.ModemManager1"),
eq("/org/freedesktop/ModemkManager1"), any()))
eq("/org/freedesktop/ModemManager1"), any()))
.thenReturn(this.mockedModemManager);

}

private void givenMockedPermissions() {
Expand Down Expand Up @@ -964,8 +970,7 @@ private void givenMockedDevice(String deviceId, String interfaceId, NMDeviceType
mockedDevice1ConnectionSetting.put("connection",
Collections.singletonMap("uuid", new Variant<>("mock-uuid-123")));

Settings mockedDevice1Settings = mock(Settings.class);
when(mockedDevice1Settings.GetConnectionByUuid("mock-uuid-123")).thenReturn(mockedPath1);
when(this.mockedNetworkManagerSettings.GetConnectionByUuid("mock-uuid-123")).thenReturn(mockedPath1);

GetAppliedConnectionTuple mockedDevice1ConnectionTouple = mock(GetAppliedConnectionTuple.class);
when(mockedDevice1ConnectionTouple.getConnection()).thenReturn(mockedDevice1ConnectionSetting);
Expand Down Expand Up @@ -1000,8 +1005,6 @@ private void givenMockedDevice(String deviceId, String interfaceId, NMDeviceType

doReturn(mockedDevice1).when(this.dbusConnection).getRemoteObject("org.freedesktop.NetworkManager",
"/mock/device/" + interfaceId, Device.class);
doReturn(mockedDevice1Settings).when(this.dbusConnection).getRemoteObject("org.freedesktop.NetworkManager",
"/org/freedesktop/NetworkManager/Settings", Settings.class);
doReturn(mockedProperties1).when(this.dbusConnection).getRemoteObject("org.freedesktop.NetworkManager",
"/mock/device/" + interfaceId, Properties.class);
if (hasAssociatedConnection) {
Expand Down Expand Up @@ -1031,11 +1034,7 @@ public void givenMockToPrepNetworkManagerToAllowDeviceToCreateNewConnection() th
DBusPath newConnectionPath = mock(DBusPath.class);
when(newConnectionPath.getPath()).thenReturn("/mock/Connection/path/newly/created");

AddAndActivateConnectionTuple addAndActivateConnectionTuple = mock(AddAndActivateConnectionTuple.class);
when(addAndActivateConnectionTuple.getPath()).thenReturn(newConnectionPath);

when(this.mockedNetworkManager.AddAndActivateConnection(any(), any(), any()))
.thenReturn(addAndActivateConnectionTuple);
when(this.mockedNetworkManagerSettings.AddConnection(any())).thenReturn(newConnectionPath);

doReturn(mock(Connection.class)).when(this.dbusConnection).getRemoteObject("org.freedesktop.NetworkManager",
"/mock/Connection/path/newly/created", Connection.class);
Expand All @@ -1045,16 +1044,12 @@ public void givenMockedConnection(String connectionId, String connectionUuid, St
String connectionPath) throws DBusException {

if (this.mockedConnectionDbusPathList.isEmpty()) {
Settings settings = mock(Settings.class);
when(settings.ListConnections()).thenReturn(this.mockedConnectionDbusPathList);

doReturn(settings).when(this.dbusConnection).getRemoteObject("org.freedesktop.NetworkManager",
"/org/freedesktop/NetworkManager/Settings", Settings.class);
when(this.mockedNetworkManagerSettings.ListConnections()).thenReturn(this.mockedConnectionDbusPathList);

DBusPath mockUuidPath = mock(DBusPath.class);
when(mockUuidPath.getPath()).thenReturn("/unused/connection/path");

when(settings.GetConnectionByUuid(any())).thenReturn(mockUuidPath);
when(this.mockedNetworkManagerSettings.GetConnectionByUuid(any())).thenReturn(mockUuidPath);

doThrow(DBusExecutionException.class).when(this.dbusConnection)
.getRemoteObject("org.freedesktop.NetworkManager", "/unused/connection/path", Connection.class);
Expand Down Expand Up @@ -1090,16 +1085,12 @@ public void givenMockedAssociatedConnection(String connectionId, String connecti
Connection mockAssociatedConnection = mock(Connection.class);

if (this.mockedConnectionDbusPathList.isEmpty()) {
Settings settings = mock(Settings.class);
when(settings.ListConnections()).thenReturn(this.mockedConnectionDbusPathList);

doReturn(settings).when(this.dbusConnection).getRemoteObject("org.freedesktop.NetworkManager",
"/org/freedesktop/NetworkManager/Settings", Settings.class);
when(this.mockedNetworkManagerSettings.ListConnections()).thenReturn(this.mockedConnectionDbusPathList);

DBusPath mockUuidPath = mock(DBusPath.class);
when(mockUuidPath.getPath()).thenReturn("/path/to/Associated/Connection");

when(settings.GetConnectionByUuid(any())).thenReturn(mockUuidPath);
when(this.mockedNetworkManagerSettings.GetConnectionByUuid(any())).thenReturn(mockUuidPath);

doReturn(mockAssociatedConnection).when(this.dbusConnection).getRemoteObject(
"org.freedesktop.NetworkManager", "/path/to/Associated/Connection", Connection.class);
Expand Down Expand Up @@ -1447,7 +1438,8 @@ private void thenActivateConnectionIsCalledFor(String netInterface) throws DBusE
}

private void thenAddAndActivateConnectionIsCalledFor(String netInterface) throws DBusException {
verify(this.mockedNetworkManager).AddAndActivateConnection(any(), any(), any());
verify(this.mockedNetworkManagerSettings).AddConnection(any());
verify(this.mockedNetworkManager).ActivateConnection(any(), any(), any());
}

private void thenNetworkSettingsDidNotChangeForDevice(String netInterface) throws DBusException {
Expand Down

0 comments on commit 4db5765

Please sign in to comment.