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?
Aucun commentaire:
Enregistrer un commentaire