0

I'm using junit for a number of integration test that require different openvpn-connections. Within my tests, I start the openvpn-connection with the ProcessBuilder-class:

Process p = new ProcessBuilder("sudo", vpnBinary, clientfile).directory(tempDir.toFile()).inheritIO().start();
vpnProcess = p;

To close the connection at the end of the test, I add a hook during the test setup:

public static void setUpBeforeClass() throws Exception {
    Runtime.getRuntime().addShutdownHook(new Thread() {
            public void run() {
                stopVPNConnection();
            }
}

I tried different versions of the stopVPNConnection method, non of which works in a reliable way...

First:

private static void stopVPNConnection(){

        if(vpnProcess != null){

            LOG.info("Going to shutdown special vpn and start normal vpn");

            vpnProcess.destroy();
            LOG.fine("Destroy signal sent");

        }
}

Second:

private static void stopVPNConnection(){

        if(vpnProcess != null){

            LOG.info("Going to shutdown special vpn and start normal vpn");

            vpnProcess.destroyForcibly();
            LOG.fine("Destroy signal sent");

        }
}

The third made use of Java tool/method to force-kill a child process

The fourth starts another process:

Process p = new ProcessBuilder("sudo", "/usr/bin/killall", "sudo").inheritIO().start();

Up to this point, none of these have stopped the openvpn-connection in a sufficiently reliable way for automated testing.

Is there a better way to stop the process?

How to go about understanding why openvpn is not stopped?

DoRe
  • 1
  • Have you tried manually stopping the process? When you run killall does it stop? It might be that the process is not responding to the kill request. Also, in the forth method have you tried killall -9. It is possible that the VPN process is intercepting the kill request and ignoring it. – James Wilson Oct 03 '19 at 10:15
  • @James Wilson: Thanks for the suggestion. Killing openvpn from the command line works as expected - so it is some problem when starting the process from ProcessBuilder – DoRe Oct 14 '19 at 09:01

0 Answers0