Skip to content

Commit

Permalink
test: re-enable and fix LinuxNetworkUtilTest (#5626)
Browse files Browse the repository at this point in the history
* refactor: add overloads exposing search paths

* test: fix LinuxNetworkUtilTest

* test: skip test

* docs: add explaination

* build: remove unused imports

* refactor: make sonar happy
  • Loading branch information
mattdibi authored Dec 19, 2024
1 parent e060bbe commit 2cc5978
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -260,15 +260,23 @@ private boolean isWifiLinkUpInternal(String ifaceName) throws KuraException {
}

public static boolean toolExists(String tool) {
return getToolPath(tool).isPresent();
return toolExists(tool, DEFAULT_PATH);
}

public static boolean toolExists(String tool, String[] searchPaths) {
return getToolPath(tool, searchPaths).isPresent();
}

public static Optional<String> getToolPath(String tool) {
return getToolPath(tool, DEFAULT_PATH);
}

public static Optional<String> getToolPath(String tool, String[] searchPaths) {
Optional<String> optionalToolPath = getCachedTool(tool);
if (optionalToolPath.isPresent()) {
return optionalToolPath;
} else {
for (String folder : DEFAULT_PATH) {
for (String folder : searchPaths) {
String toolPath = folder + tool;
File toolFile = new File(toolPath);
if (toolFile.exists()) {
Expand All @@ -294,7 +302,21 @@ private static Optional<String> getCachedTool(String tool) {
* @return true if the unit is installed
*/
public static boolean systemdSystemUnitExists(String unitName) {
Optional<String> optionalSystemctlPath = getToolPath("systemctl");
return systemdSystemUnitExists(unitName, DEFAULT_PATH);
}
/**
* Checks if the given Systemd system unit is installed.
* The result is based on the exit code of "systemctl status <unitName>"
* as presented in https://www.man7.org/linux/man-pages/man1/systemctl.1.html#EXIT_STATUS
*
* @param unitName
* the name of the Systemd system unit
* @param searchPaths
* the paths to search for the systemctl command
* @return true if the unit is installed
*/
public static boolean systemdSystemUnitExists(String unitName, String[] searchPaths) {
Optional<String> optionalSystemctlPath = getToolPath("systemctl", searchPaths);
if (!optionalSystemctlPath.isPresent()) {
logger.debug("Systemctl command not found in default paths");
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
Expand All @@ -37,7 +34,6 @@
import org.junit.Ignore;
import org.junit.Test;

@Ignore
public class LinuxNetworkUtilTest {

private LinuxNetworkUtil linuxNetworkUtil;
Expand Down Expand Up @@ -91,60 +87,51 @@ public void setNetworkInterfaceLinkDown() {
}

@Test
public void shouldCheckToolExistence() throws NoSuchFieldException, IllegalAccessException, IOException {
public void shouldCheckToolExistence() throws IOException {
givenLinuxNetworkUtil();
givenToolPaths("/tmp/");
givenTool("/tmp/dhcpd");

whenCheckTool("dhcpd");
whenCheckTool("dhcpd", "/tmp/");

thenToolExists();
}

@Test
public void shouldCheckSystemdUnitExistence()
throws NoSuchFieldException, IllegalAccessException, IOException, InterruptedException {
@Ignore("Given the current implementation, we cannot inject the exit code of the command execution and therefore this test will fail.")
public void shouldCheckSystemdUnitExistence() throws IOException {
givenLinuxNetworkUtil();
givenToolPaths("/tmp/");
givenTool("/tmp/systemctl");
givenProcessBuilder(0);

whenCheckSystemdUnit("dnsmask.service");
whenCheckSystemdUnit("dnsmask.service", "/tmp/");

thenSystemdUnitExists();
}

@Test
public void shouldNotCheckSystemdUnitExistence()
throws NoSuchFieldException, IllegalAccessException, IOException, InterruptedException {
public void shouldNotCheckSystemdUnitExistence() throws IOException {
givenLinuxNetworkUtil();
givenToolPaths("/tmp/");
givenTool("/tmp/systemctl");
givenProcessBuilder(4);

whenCheckSystemdUnit("dnsmask.service");
whenCheckSystemdUnit("dnsmask.service", "/tmp/");

thenSystemdUnitNotExist();
}

@Test
public void shouldNotCheckSystemdUnitExistenceIfSystemctlNotExist()
throws NoSuchFieldException, IllegalAccessException, IOException, InterruptedException {
public void shouldNotCheckSystemdUnitExistenceIfSystemctlNotExist() {
givenLinuxNetworkUtil();

whenCheckSystemdUnit("dnsmask.service");
whenCheckSystemdUnit("dnsmask.service", "/tmp/");

thenSystemdUnitNotExist();
}

@Test
public void shouldGetToolPath()
throws NoSuchFieldException, IllegalAccessException, IOException, InterruptedException {
public void shouldGetToolPath() throws IOException {
givenLinuxNetworkUtil();
givenToolPaths("/tmp/");
givenTool("/tmp/myAwesomeCommand");

whenGetTool("myAwesomeCommand");
whenGetTool("myAwesomeCommand", "/tmp/");

thenToolIsRetrieved("/tmp/myAwesomeCommand");
}
Expand All @@ -155,10 +142,6 @@ private void givenLinuxNetworkUtil() {
this.linuxNetworkUtil = new LinuxNetworkUtil(this.commandExecutorServiceStub);
}

private void givenToolPaths(String path) throws NoSuchFieldException, IllegalAccessException {
setFinalStaticField(LinuxNetworkUtil.class, "DEFAULT_PATH", new String[] { path });
}

private void givenTool(String tool) throws IOException {
Path newFilePath = Paths.get(tool);
try {
Expand All @@ -180,30 +163,20 @@ private void givenLinkStatus(String linkStatus) {
this.linkStatus = linkStatus;
}

private void givenProcessBuilder(int returnCode)
throws NoSuchFieldException, IOException, InterruptedException, IllegalAccessException {
Process mockedProcess = mock(Process.class);
when(mockedProcess.waitFor()).thenReturn(returnCode);
when(mockedProcess.getInputStream()).thenReturn(new ByteArrayInputStream(new byte[] {}));
ProcessBuilder mockedProcessBuilder = mock(ProcessBuilder.class);
when(mockedProcessBuilder.start()).thenReturn(mockedProcess);
setFinalStaticField(LinuxNetworkUtil.class, "PROCESS_BUILDER", mockedProcessBuilder);
}

private void whenDedicatedInterfaceName(String dedicatedInterfaceName) {
this.dedicatedInterfaceName = dedicatedInterfaceName;
}

private void whenCheckTool(String toolName) {
this.toolExists = LinuxNetworkUtil.toolExists(toolName);
private void whenCheckTool(String toolName, String searchPath) {
this.toolExists = LinuxNetworkUtil.toolExists(toolName, new String[] { searchPath });
}

private void whenCheckSystemdUnit(String unitName) {
this.systemdUnitExists = LinuxNetworkUtil.systemdSystemUnitExists(unitName);
private void whenCheckSystemdUnit(String unitName, String searchPath) {
this.systemdUnitExists = LinuxNetworkUtil.systemdSystemUnitExists(unitName, new String[] { searchPath });
}

private void whenGetTool(String tool) {
this.toolPath = LinuxNetworkUtil.getToolPath(tool);
private void whenGetTool(String tool, String searchPath) {
this.toolPath = LinuxNetworkUtil.getToolPath(tool, new String[] { searchPath });
}

private void thenApNetworkInterfaceIsCreated() {
Expand Down Expand Up @@ -264,14 +237,4 @@ private void thenToolIsRetrieved(String expectedToolPath) {
assertEquals(expectedToolPath, this.toolPath.get());
}

static void setFinalStaticField(Class clazz, String fieldName, Object value)
throws NoSuchFieldException, IllegalAccessException {
Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
Field modifiers = field.getClass().getDeclaredField("modifiers");
modifiers.setAccessible(true);
modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(null, value);
}

}

0 comments on commit 2cc5978

Please sign in to comment.