Skip to content

Commit ad728a4

Browse files
committed
Log computer name on platform detail collection failure
Also adds tests to cover the platform detail collection failure message.
1 parent 9eb9f0d commit ad728a4

File tree

2 files changed

+122
-7
lines changed

2 files changed

+122
-7
lines changed

src/main/java/org/jvnet/hudson/plugins/platformlabeler/NodeLabelCache.java

+10-6
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,24 @@ public class NodeLabelCache extends ComputerListener {
6565
* @param computer agent whose labels will be cached
6666
* @param channel This is the channel object to talk to the agent
6767
* @param root The directory where this agent stores files. The same as Node.getRootPath(), except that method returns null until the agent is connected. So this parameter is passed explicitly instead.
68-
* @param ignored TaskListener that is ignored
68+
* @param listener logging destination for agent that is connecting
6969
* @throws java.io.IOException on IO error
7070
* @throws java.lang.InterruptedException on thread interrupt
7171
*/
7272
@Override
73-
public final void preOnline(final Computer computer, Channel channel, FilePath root, final TaskListener ignored)
73+
public final void preOnline(final Computer computer, Channel channel, FilePath root, final TaskListener listener)
7474
throws IOException, InterruptedException {
7575
try {
7676
cacheAndRefreshModel(computer, channel);
7777
} catch (Exception e) {
78-
LOGGER.log(
79-
Level.WARNING,
80-
"Unable to collect platform details during preOnline phase. Connecting the agent anyway.",
81-
e);
78+
String name = "unnamed agent"; // built-in (and others) may not have a name during preOnline
79+
if (computer != null && !computer.getName().isEmpty()) {
80+
name = computer.getName();
81+
}
82+
83+
listener.getLogger()
84+
.println("Ignored platform detail collection failure for '" + name + "' during preOnline phase. "
85+
+ e);
8286
}
8387
}
8488

src/test/java/org/jvnet/hudson/plugins/platformlabeler/NodeLabelCacheTest.java

+112-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import static org.hamcrest.Matchers.in;
88
import static org.hamcrest.Matchers.is;
99
import static org.hamcrest.Matchers.not;
10+
import static org.hamcrest.Matchers.startsWith;
1011

1112
import hudson.FilePath;
1213
import hudson.Launcher;
@@ -23,13 +24,18 @@
2324
import hudson.slaves.RetentionStrategy;
2425
import hudson.util.ClockDifference;
2526
import hudson.util.DescribableList;
27+
import hudson.util.LogTaskListener;
28+
import hudson.util.RingBufferLogHandler;
29+
import java.io.File;
2630
import java.io.IOException;
2731
import java.nio.charset.Charset;
2832
import java.util.Collection;
2933
import java.util.List;
3034
import java.util.Set;
3135
import java.util.concurrent.Future;
36+
import java.util.logging.Level;
3237
import java.util.logging.LogRecord;
38+
import java.util.logging.Logger;
3339
import javax.servlet.ServletException;
3440
import org.junit.After;
3541
import org.junit.Before;
@@ -152,7 +158,7 @@ public void testGetLabelsForNode_IsNull() throws Exception {
152158

153159
@Test(expected = IOException.class)
154160
public void testRequestComputerPlatformDetails_ChannelThrows() throws Exception {
155-
Computer throwingComputer = new NullingComputer(computer.getNode(), new IOException());
161+
Computer throwingComputer = new NullingComputer(computer.getNode(), new IOException("Oops"));
156162
nodeLabelCache.requestComputerPlatformDetails(throwingComputer, throwingComputer.getChannel());
157163
}
158164

@@ -166,6 +172,111 @@ public void testRequestComputerPlatformDetails_ChannelThrowsOnNullChannel() thro
166172
nodeLabelCache.requestComputerPlatformDetails(computer, null);
167173
}
168174

175+
@Test
176+
public void testPreOnline_ChannelLogsDetailCollectionIgnoredOnInternalException() throws Exception {
177+
// Setup a recorder for agent log
178+
RingBufferLogHandler agentLogHandler = new RingBufferLogHandler(10);
179+
Logger agentLogger = Logger.getLogger(NodeLabelCacheTest.class.getName());
180+
agentLogger.addHandler(agentLogHandler);
181+
TaskListener agentListener = new LogTaskListener(agentLogger, Level.INFO);
182+
183+
nodeLabelCache.preOnline(computer, null, new FilePath(new File(".")), agentListener);
184+
185+
assertThat(
186+
agentLogHandler.getView().get(0).getMessage(),
187+
startsWith("Ignored platform detail collection failure for 'unnamed agent' during preOnline phase."));
188+
}
189+
190+
@Test
191+
public void testPreOnline_ChannelLogsDetailCollectionIgnoredOnInternalExceptionForNullComputer() throws Exception {
192+
// Setup a recorder for agent log
193+
RingBufferLogHandler agentLogHandler = new RingBufferLogHandler(10);
194+
Logger agentLogger = Logger.getLogger(NodeLabelCacheTest.class.getName());
195+
agentLogger.addHandler(agentLogHandler);
196+
TaskListener agentListener = new LogTaskListener(agentLogger, Level.INFO);
197+
198+
nodeLabelCache.preOnline(null, null, new FilePath(new File(".")), agentListener);
199+
200+
assertThat(
201+
agentLogHandler.getView().get(0).getMessage(),
202+
startsWith("Ignored platform detail collection failure for 'unnamed agent' during preOnline phase."));
203+
}
204+
205+
@Test
206+
public void testPreOnline_ChannelLogsDetailCollectionIgnoredOnInternalExceptionForComputer() throws Exception {
207+
// Setup a recorder for agent log
208+
RingBufferLogHandler agentLogHandler = new RingBufferLogHandler(10);
209+
Logger agentLogger = Logger.getLogger(NodeLabelCacheTest.class.getName());
210+
agentLogger.addHandler(agentLogHandler);
211+
TaskListener agentListener = new LogTaskListener(agentLogger, Level.INFO);
212+
213+
Computer minimal = new MinimalComputer(computer.getNode());
214+
String name = minimal.getName();
215+
nodeLabelCache.preOnline(minimal, null, new FilePath(new File(".")), agentListener);
216+
217+
assertThat(
218+
agentLogHandler.getView().get(0).getMessage(),
219+
startsWith("Ignored platform detail collection failure for '" + name + "' during preOnline phase."));
220+
}
221+
222+
/** A minimal Computer class for preOnline test. */
223+
private class MinimalComputer extends Computer {
224+
public MinimalComputer(Node node) {
225+
super(node);
226+
}
227+
228+
@Override
229+
public Node getNode() {
230+
return super.getNode();
231+
}
232+
233+
@Override
234+
public String getName() {
235+
return "computer-test";
236+
}
237+
238+
@Override
239+
public VirtualChannel getChannel() {
240+
return null;
241+
}
242+
243+
@Override
244+
public Charset getDefaultCharset() {
245+
throw new UnsupportedOperationException("Unsupported");
246+
}
247+
248+
@Override
249+
public List<LogRecord> getLogRecords() throws IOException, InterruptedException {
250+
throw new UnsupportedOperationException("Unsupported");
251+
}
252+
253+
@Override
254+
@RequirePOST
255+
public void doLaunchSlaveAgent(StaplerRequest sr, StaplerResponse sr1) throws IOException, ServletException {
256+
throw new UnsupportedOperationException("Unsupported");
257+
}
258+
259+
@Override
260+
protected Future<?> _connect(boolean bln) {
261+
throw new UnsupportedOperationException("Unsupported");
262+
}
263+
264+
@Override
265+
public Boolean isUnix() {
266+
throw new UnsupportedOperationException("Unsupported");
267+
}
268+
269+
@Override
270+
public boolean isConnecting() {
271+
throw new UnsupportedOperationException("Unsupported");
272+
}
273+
274+
@Override
275+
public RetentionStrategy<?> getRetentionStrategy() {
276+
throw new UnsupportedOperationException("Unsupported");
277+
}
278+
}
279+
169280
/** Class that intentionally returns nulls for test purposes. */
170281
private class NullingComputer extends Computer {
171282

0 commit comments

Comments
 (0)