Skip to content

Commit

Permalink
Improved logging
Browse files Browse the repository at this point in the history
Signed-off-by: pierantoniomerlino <[email protected]>
  • Loading branch information
pierantoniomerlino committed Nov 22, 2024
1 parent a2837bc commit 916fa25
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*******************************************************************************/
package org.eclipse.kura.linux.net.dhcp.server;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -42,10 +43,10 @@ public class DnsmasqTool implements DhcpLinuxTool {
private String globalConfigFilename = "/etc/dnsmasq.d/dnsmasq-globals.conf";
private static final String GLOBAL_CONFIGURATION = "port=0\nbind-dynamic\n";

static final Command IS_ACTIVE_COMMAND = new Command(
new String[] { "systemctl", "is-active", "--quiet", DhcpServerTool.DNSMASQ.getValue() });
static final Command RESTART_COMMAND = new Command(
new String[] { "systemctl", "restart", DhcpServerTool.DNSMASQ.getValue() });
static final String[] IS_ACTIVE_COMMANDLINE = new String[] { "systemctl", "is-active", "--quiet",
DhcpServerTool.DNSMASQ.getValue() };
static final String[] RESTART_COMMANDLINE = new String[] { "systemctl", "restart",
DhcpServerTool.DNSMASQ.getValue() };

private CommandExecutorService executorService;
private Map<String, byte[]> configsLastHash = Collections.synchronizedMap(new HashMap<>());
Expand All @@ -57,7 +58,7 @@ public DnsmasqTool(CommandExecutorService service) {

@Override
public boolean isRunning(String interfaceName) throws KuraProcessExecutionErrorException {
CommandStatus status = this.executorService.execute(IS_ACTIVE_COMMAND);
CommandStatus status = this.executorService.execute(new Command(IS_ACTIVE_COMMANDLINE));

boolean isRunning;
try {
Expand Down Expand Up @@ -85,12 +86,19 @@ public CommandStatus startInterface(String interfaceName) throws KuraProcessExec
"Failed to start DHCP server for interface: " + interfaceName);
}

CommandStatus restartStatus = this.executorService.execute(RESTART_COMMAND);
Command restartCommand = new Command(RESTART_COMMANDLINE);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayOutputStream err = new ByteArrayOutputStream();
restartCommand.setErrorStream(err);
restartCommand.setOutputStream(out);
CommandStatus restartStatus = this.executorService.execute(restartCommand);

if (!restartStatus.getExitStatus().isSuccessful()) {
logger.error("dnsmasq Systemd unit startup failed. Is dnsmasq package installed on the system?");
logger.error("dnsmasq Systemd unit startup failed. Check if the tool is properly installed on the system.");
logger.error("dnsmasq stderr {}", new String(err.toByteArray(), StandardCharsets.UTF_8));
logger.error("dnsmasq stdout {}", new String(out.toByteArray(), StandardCharsets.UTF_8));
removeInterfaceConfig(interfaceName);
restartStatus = this.executorService.execute(RESTART_COMMAND);
restartStatus = this.executorService.execute(restartCommand);
}

return restartStatus;
Expand All @@ -101,7 +109,7 @@ public boolean disableInterface(String interfaceName) throws KuraProcessExecutio
boolean isInterfaceDisabled = true;

if (removeInterfaceConfig(interfaceName)) {
CommandStatus status = this.executorService.execute(RESTART_COMMAND);
CommandStatus status = this.executorService.execute(new Command(RESTART_COMMANDLINE));
isInterfaceDisabled = status.getExitStatus().isSuccessful();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.Map;

import org.eclipse.kura.KuraProcessExecutionErrorException;
import org.eclipse.kura.executor.Command;
import org.eclipse.kura.executor.CommandExecutorService;
import org.eclipse.kura.executor.CommandStatus;
import org.eclipse.kura.executor.ExitStatus;
Expand Down Expand Up @@ -160,7 +161,7 @@ public void shouldReturnFalseWhenDisablingInterfaceandNoRunningTool() throws Exc

thenDisableInterfaceReturned(false);
}

/*
* Steps
*/
Expand All @@ -183,9 +184,10 @@ public int getExitCode() {
public boolean isSuccessful() {
return isSuccessful;
}

};
CommandStatus returnedStatus = new CommandStatus(DnsmasqTool.IS_ACTIVE_COMMAND, returnedExitStatus);
CommandStatus returnedStatus = new CommandStatus(new Command(DnsmasqTool.IS_ACTIVE_COMMANDLINE),
returnedExitStatus);

when(this.mockExecutor.execute(any())).thenReturn(returnedStatus);
when(this.mockExecutor.isRunning(any(String[].class))).thenReturn(isSuccessful);
Expand All @@ -197,7 +199,8 @@ private void givenDhcpdTool(DhcpServerTool tool) {

private void givenDhcpServerManagerReturn(String interfaceName, String returnedConfigFilename) {
mockServerManager = mockStatic(DhcpServerManager.class);
mockServerManager.when(() -> DhcpServerManager.getPidFilename(interfaceName)).thenReturn(returnedConfigFilename);
mockServerManager.when(() -> DhcpServerManager.getPidFilename(interfaceName))
.thenReturn(returnedConfigFilename);
}

private void givenConfigFile(String filename) throws IOException {
Expand All @@ -212,7 +215,7 @@ private void givenRunningPids() {
public int getPid() {
return 1234;
}

});

when(this.mockExecutor.getPids(any())).thenReturn(this.runningPids);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.IOException;

import org.eclipse.kura.KuraProcessExecutionErrorException;
import org.eclipse.kura.executor.Command;
import org.eclipse.kura.executor.CommandExecutorService;
import org.eclipse.kura.executor.CommandStatus;
import org.eclipse.kura.executor.ExitStatus;
Expand All @@ -47,7 +48,7 @@ public class DnsmasqToolTest {
private CommandStatus startInterfaceStatus;
private boolean interfaceDisabled;
private Exception occurredException;

/*
* Scenarios
*/
Expand Down Expand Up @@ -151,9 +152,10 @@ public int getExitCode() {
public boolean isSuccessful() {
return isSuccessful;
}

};
CommandStatus returnedStatus = new CommandStatus(DnsmasqTool.IS_ACTIVE_COMMAND, returnedExitStatus);
CommandStatus returnedStatus = new CommandStatus(new Command(DnsmasqTool.IS_ACTIVE_COMMANDLINE),
returnedExitStatus);

when(this.mockExecutor.execute(any())).thenReturn(returnedStatus);
}
Expand All @@ -166,13 +168,15 @@ private void givenDhcpServerManagerReturn(String interfaceName, String returnedC
private void givenConfigFile(String filename) throws IOException {
try {
this.tmpFolder.newFolder("etc", "dnsmasq.d");
} catch (Exception e) {}
} catch (Exception e) {
}
this.tmpConfigFile = this.tmpFolder.newFile(filename);
}

private void givenDnsmasqTool() throws Exception {
this.tool = new DnsmasqTool(this.mockExecutor);
this.tool.setDnsmasqGlobalConfigFile(this.tmpConfigFile.getAbsoluteFile().getParent() + "/dnsmasq-globals.conf");
this.tool
.setDnsmasqGlobalConfigFile(this.tmpConfigFile.getAbsoluteFile().getParent() + "/dnsmasq-globals.conf");
}

private void givenStartInterface(String interfaceName) throws KuraProcessExecutionErrorException {
Expand All @@ -196,7 +200,7 @@ private void whenDisableInterface(String interfaceName) throws KuraProcessExecut
this.interfaceDisabled = this.tool.disableInterface(interfaceName);
} catch (Exception e) {
this.occurredException = e;
}
}
}

/*
Expand Down

0 comments on commit 916fa25

Please sign in to comment.