Skip to content

Commit

Permalink
test: vsock: Fix false-positive test failure
Browse files Browse the repository at this point in the history
With some Linux kernels, certain vsock-related tests fail even though
should just "pass with issues" (vsock is unavailable).

This is due to the fact that we didn't catch NotYetConnectedException
with an InvalidSocketException cause.

Observed on Oracle cloud with Linux 5.4.17-2136.305.5.3.el8uek.x86_64
  • Loading branch information
kohlschuetter committed Sep 11, 2024
1 parent 6568075 commit f494e6e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,22 @@ protected final void connectSocket(Socket socket, SocketAddress endpoint) throws

protected final boolean connectSocket(SocketChannel socketChannel, SocketAddress endpoint)
throws IOException {
return asp.connectSocket(socketChannel, endpoint);
try {
return asp.connectSocket(socketChannel, endpoint);
} catch (IllegalStateException | IOException e) {
throw handleConnectSocketException(socketChannel, endpoint, e);
}
}

protected IOException handleConnectSocketException(SocketChannel socketChannel,
SocketAddress endpoint, Exception e) {
if (e instanceof IOException) {
return (IOException) e;
} else if (e instanceof RuntimeException) {
throw (RuntimeException) e;
} else {
throw new IllegalStateException(e);
}
}

protected CloseablePair<? extends Socket> newInterconnectedSockets() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.IOException;
import java.net.SocketAddress;
import java.net.SocketTimeoutException;
import java.nio.channels.NotYetConnectedException;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

Expand Down Expand Up @@ -77,14 +78,26 @@ MessageType.TEST_ABORTED_SHORT_WITH_ISSUES, msg, summaryImportantMessage(msg)).i

@Override
protected boolean handleConnect(SocketChannel sc, SocketAddress sa) throws IOException {
Exception ex;
try {
return super.handleConnect(sc, sa);
} catch (NotYetConnectedException e) {
Throwable cause = e.getCause();
if (cause instanceof InvalidSocketException) {
ex = e;
// continue below
} else {
throw e;
}
} catch (InvalidSocketException e) {
String msg = "Could not connect AF_VSOCK socket to CID=" + ((AFVSOCKSocketAddress) sa)
.getVSOCKCID() + "; check kernel capabilities.";
throw (TestAbortedWithImportantMessageException) new TestAbortedWithImportantMessageException(
MessageType.TEST_ABORTED_SHORT_WITH_ISSUES, msg, summaryImportantMessage(msg)).initCause(
e);
ex = e;
// continue below
}

String msg = "Could not connect AF_VSOCK socket to CID=" + ((AFVSOCKSocketAddress) sa)
.getVSOCKCID() + "; check kernel capabilities.";
throw (TestAbortedWithImportantMessageException) new TestAbortedWithImportantMessageException(
MessageType.TEST_ABORTED_SHORT_WITH_ISSUES, msg, summaryImportantMessage(msg)).initCause(
ex);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,19 @@
*/
package org.newsclub.net.unix.vsock;

import java.io.IOException;
import java.net.SocketAddress;
import java.nio.channels.SocketChannel;

import org.junit.jupiter.api.Test;
import org.newsclub.net.unix.AFSocketCapability;
import org.newsclub.net.unix.AFSocketCapabilityRequirement;
import org.newsclub.net.unix.AFVSOCKSocketAddress;
import org.newsclub.net.unix.InvalidSocketException;

import com.kohlschutter.annotations.compiletime.SuppressFBWarnings;
import com.kohlschutter.testutil.TestAbortedWithImportantMessageException;
import com.kohlschutter.testutil.TestAbortedWithImportantMessageException.MessageType;

@AFSocketCapabilityRequirement(AFSocketCapability.CAPABILITY_VSOCK)
@SuppressFBWarnings("NM_SAME_SIMPLE_NAME_AS_SUPERCLASS")
Expand Down Expand Up @@ -72,4 +79,18 @@ public void testDatagramChannelNonBlocking() throws Exception {
public void testDatagramChannelNonBlockingDirect() throws Exception {
super.testDatagramChannelNonBlockingDirect();
}

@Override
protected IOException handleConnectSocketException(SocketChannel channel, SocketAddress sa,
Exception e) {
if (e instanceof InvalidSocketException || e.getCause() instanceof InvalidSocketException) {
String msg = "Could not connect AF_VSOCK socket to CID=" + ((AFVSOCKSocketAddress) sa)
.getVSOCKCID() + "; check kernel capabilities.";
throw (TestAbortedWithImportantMessageException) new TestAbortedWithImportantMessageException(
MessageType.TEST_ABORTED_SHORT_WITH_ISSUES, msg, summaryImportantMessage(msg)).initCause(
e);

}
return super.handleConnectSocketException(channel, sa, e);
}
}

0 comments on commit f494e6e

Please sign in to comment.