Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CHE-4777: Improve logging on an agent start fail #4834

Merged
merged 1 commit into from
Apr 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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