Skip to content

Commit

Permalink
CHE-4777: Improve logging on an agent start fail (#4834)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mykola Morhun committed Apr 19, 2017
1 parent 03742e4 commit 4f4c452
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,12 @@ public void writeLine(String line) throws IOException {
Thread.sleep(agentPingDelayMs);
}
}
LOG.error(format("Fail launching agent '%s' in '%s' workspace due to timeout",
agent.getName(), machine.getWorkspaceId()));

process.kill();
} catch (MachineException e) {
logAsErrorAgentStartLogs(agent.getName(), agentLogger.getText());
logAsErrorAgentStartLogs(machine, agent.getName(), agentLogger.getText());
throw new ServerException(e.getServiceError());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
Expand All @@ -110,6 +112,7 @@ public void writeLine(String line) throws IOException {
agentLogger.close();
}

logAsErrorAgentStartLogs(machine, agent.getName(), agentLogger.getText());
throw new AgentStartException(format("Fail launching agent %s. Workspace ID:%s",
agent.getName(), machine.getWorkspaceId()));
}
Expand Down Expand Up @@ -141,13 +144,21 @@ protected InstanceProcess start(Instance machine, Agent agent, LineConsumer line
}

@VisibleForTesting
void logAsErrorAgentStartLogs(String agentName, String logs) {
void logAsErrorAgentStartLogs(Instance machine, String agentName, String logs) {
if (!logs.isEmpty()) {
LOG.error("An error occurs while starting '{}' agent. Detailed log:\n{}",
LOG.error("An error occurs while starting '{}' agent in '{}' workspace in '{}' machine on '{}' node. Detailed log:\n{}",
agentName,
machine.getWorkspaceId(),
machine.getId(),
machine.getNode(),
logs);
} else {
LOG.error("An error occurs while starting '{}' agent. The agent didn't produce any logs.", agentName);
LOG.error("An error occurs while starting '{}' agent in '{}' workspace in '{}' machine on '{}' node. " +
"The agent didn't produce any logs.",
agentName,
machine.getWorkspaceId(),
machine.getId(),
machine.getNode());
}
}

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

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.atLeast;
Expand All @@ -41,6 +42,7 @@
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;

/**
* @author Alexander Garagatyi
Expand Down Expand Up @@ -265,10 +267,15 @@ public void shouldLogAgentStartLogsIfTimeoutReached() throws Exception {
doReturn(process).when(launcher).start(any(Instance.class), any(Agent.class), any(LineConsumer.class));

// when
launcher.launch(machine, agent);

// then
verify(launcher).logAsErrorAgentStartLogs(anyString(), anyString());
try {
launcher.launch(machine, agent);
fail("Should throw AgentStartException");
} catch (AgentStartException e) {
// then
verify(launcher).logAsErrorAgentStartLogs(anyObject(), anyString(), anyString());
// rethrow exception to verify message
throw e;
}
}

@Test(expectedExceptions = ServerException.class, expectedExceptionsMessageRegExp = "An error on agent start")
Expand All @@ -278,10 +285,15 @@ public void shouldLogAgentStartLogsIfMachineExceptionOccurs() throws Exception {
.when(launcher).start(any(Instance.class), any(Agent.class), any(LineConsumer.class));

// when
launcher.launch(machine, agent);

// then
verify(launcher).logAsErrorAgentStartLogs(anyString(), anyString());
try {
launcher.launch(machine, agent);
fail("Should throw ServerException");
} catch (ServerException e) {
// then
verify(launcher).logAsErrorAgentStartLogs(anyObject(), anyString(), anyString());
// rethrow exception to verify message
throw e;
}
}

@Test(expectedExceptions = ServerException.class, expectedExceptionsMessageRegExp = "An error on process kill")
Expand All @@ -296,10 +308,15 @@ public void shouldLogAgentStartLogsIfMachineExceptionOccursAfterAgentStartTimeou
doThrow(new MachineException("An error on process kill")).when(process).kill();

// when
launcher.launch(machine, agent);

// then
verify(launcher).logAsErrorAgentStartLogs(anyString(), anyString());
try {
launcher.launch(machine, agent);
fail("Should throw ServerException");
} catch (ServerException e) {
// then
verify(launcher).logAsErrorAgentStartLogs(anyObject(), anyString(), anyString());
// rethrow exception to verify message
throw e;
}
}

private static class TestAgentLauncher extends AbstractAgentLauncher {
Expand Down

0 comments on commit 4f4c452

Please sign in to comment.