From 916fa25f45388755724966635cd23f31d73bca4e Mon Sep 17 00:00:00 2001 From: pierantoniomerlino Date: Fri, 22 Nov 2024 16:54:58 +0100 Subject: [PATCH] Improved logging Signed-off-by: pierantoniomerlino --- .../linux/net/dhcp/server/DnsmasqTool.java | 26 ++++++++++++------- .../linux/net/dhcp/server/DhcpdToolTest.java | 13 ++++++---- .../net/dhcp/server/DnsmasqToolTest.java | 16 +++++++----- 3 files changed, 35 insertions(+), 20 deletions(-) diff --git a/kura/org.eclipse.kura.linux.net/src/main/java/org/eclipse/kura/linux/net/dhcp/server/DnsmasqTool.java b/kura/org.eclipse.kura.linux.net/src/main/java/org/eclipse/kura/linux/net/dhcp/server/DnsmasqTool.java index 49da7a1a9c6..6f62abdecba 100644 --- a/kura/org.eclipse.kura.linux.net/src/main/java/org/eclipse/kura/linux/net/dhcp/server/DnsmasqTool.java +++ b/kura/org.eclipse.kura.linux.net/src/main/java/org/eclipse/kura/linux/net/dhcp/server/DnsmasqTool.java @@ -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; @@ -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 configsLastHash = Collections.synchronizedMap(new HashMap<>()); @@ -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 { @@ -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; @@ -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(); } diff --git a/kura/test/org.eclipse.kura.linux.net.test/src/test/java/org/eclipse/kura/linux/net/dhcp/server/DhcpdToolTest.java b/kura/test/org.eclipse.kura.linux.net.test/src/test/java/org/eclipse/kura/linux/net/dhcp/server/DhcpdToolTest.java index 601714f3fe3..8e1d3795ed4 100644 --- a/kura/test/org.eclipse.kura.linux.net.test/src/test/java/org/eclipse/kura/linux/net/dhcp/server/DhcpdToolTest.java +++ b/kura/test/org.eclipse.kura.linux.net.test/src/test/java/org/eclipse/kura/linux/net/dhcp/server/DhcpdToolTest.java @@ -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; @@ -160,7 +161,7 @@ public void shouldReturnFalseWhenDisablingInterfaceandNoRunningTool() throws Exc thenDisableInterfaceReturned(false); } - + /* * Steps */ @@ -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); @@ -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 { @@ -212,7 +215,7 @@ private void givenRunningPids() { public int getPid() { return 1234; } - + }); when(this.mockExecutor.getPids(any())).thenReturn(this.runningPids); diff --git a/kura/test/org.eclipse.kura.linux.net.test/src/test/java/org/eclipse/kura/linux/net/dhcp/server/DnsmasqToolTest.java b/kura/test/org.eclipse.kura.linux.net.test/src/test/java/org/eclipse/kura/linux/net/dhcp/server/DnsmasqToolTest.java index 2e8be8ead44..077fb8e257c 100644 --- a/kura/test/org.eclipse.kura.linux.net.test/src/test/java/org/eclipse/kura/linux/net/dhcp/server/DnsmasqToolTest.java +++ b/kura/test/org.eclipse.kura.linux.net.test/src/test/java/org/eclipse/kura/linux/net/dhcp/server/DnsmasqToolTest.java @@ -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; @@ -47,7 +48,7 @@ public class DnsmasqToolTest { private CommandStatus startInterfaceStatus; private boolean interfaceDisabled; private Exception occurredException; - + /* * Scenarios */ @@ -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); } @@ -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 { @@ -196,7 +200,7 @@ private void whenDisableInterface(String interfaceName) throws KuraProcessExecut this.interfaceDisabled = this.tool.disableInterface(interfaceName); } catch (Exception e) { this.occurredException = e; - } + } } /*