Closed AndyGee closed 5 years ago
I'm stuck behind a corporate firewall, else I'd submit:
` package org.arquillian.cube.docker.drone;
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; import java.util.logging.Level; import java.util.logging.Logger;
import org.arquillian.cube.docker.drone.event.AfterVideoRecorded; import org.arquillian.cube.docker.drone.util.VideoFileDestination; import org.arquillian.cube.spi.Cube; import org.arquillian.cube.spi.CubeControlException; import org.arquillian.cube.spi.CubeRegistry; import org.jboss.arquillian.core.api.Event; import org.jboss.arquillian.core.api.Instance; import org.jboss.arquillian.core.api.annotation.Inject; import org.jboss.arquillian.core.api.annotation.Observes; import org.jboss.arquillian.test.spi.TestResult; import org.jboss.arquillian.test.spi.event.suite.After; import org.jboss.arquillian.test.spi.event.suite.Before;
/**
started and stopped. */ public class VncRecorderLifecycleManager {
@Inject
Event
@Inject
Instance
Cube vnc;
public void startRecording(@Observes Before beforeTestMethod, CubeDroneConfiguration cubeDroneConfiguration, CubeRegistry cubeRegistry) {
try {
if (cubeDroneConfiguration.isRecording()) {
// lazy init
initVncCube(cubeRegistry);
vnc.create();
vnc.start();
}
} catch (CubeControlException e) {
Logger.getLogger(VncRecorderLifecycleManager.class.getName()).log(Level.SEVERE, "Failed to start vnc", e);
}
}
private void initVncCube(CubeRegistry cubeRegistry) { if (vnc == null) { SeleniumContainers seleniumContainers = seleniumContainersInstance.get();
Cube vncContainer = cubeRegistry.getCube(seleniumContainers.getVncContainerName());
if (vncContainer == null) {
throw new IllegalArgumentException("VNC cube is not present in registry.");
}
this.vnc = vncContainer;
}
}
public void stopRecording(@Observes After afterTestMethod, TestResult testResult, CubeDroneConfiguration cubeDroneConfiguration, SeleniumContainers seleniumContainers) {
try {
if (this.vnc != null) {
vnc.stop();
Path finalLocation = null;
if (shouldRecordOnlyOnFailure(testResult, cubeDroneConfiguration)) {
finalLocation =
moveFromVolumeFolderToBuildDirectory(afterTestMethod, cubeDroneConfiguration, seleniumContainers);
} else {
if (shouldRecordAlways(cubeDroneConfiguration)) {
finalLocation =
moveFromVolumeFolderToBuildDirectory(afterTestMethod, cubeDroneConfiguration, seleniumContainers);
}
}
vnc.destroy();
this.afterVideoRecordedEvent.fire(new AfterVideoRecorded(afterTestMethod, finalLocation));
}
} catch (CubeControlException e) {
Logger.getLogger(VncRecorderLifecycleManager.class.getName()).log(Level.SEVERE, "Failed to stop vnc", e);
}
}
private boolean shouldRecordAlways(CubeDroneConfiguration cubeDroneConfiguration) { return cubeDroneConfiguration.isRecording() && !cubeDroneConfiguration.isRecordOnFailure(); }
private boolean shouldRecordOnlyOnFailure(TestResult testResult, CubeDroneConfiguration cubeDroneConfiguration) { return cubeDroneConfiguration.isRecordOnFailure() && testResult.getStatus() == TestResult.Status.FAILED; }
private Path moveFromVolumeFolderToBuildDirectory(After afterTestMethod, CubeDroneConfiguration cubeDroneConfiguration, SeleniumContainers seleniumContainers) { try { final Path finalLocation = getFinalLocation(cubeDroneConfiguration, afterTestMethod); Files.move(seleniumContainers.getVideoRecordingFile(), finalLocation, StandardCopyOption.REPLACE_EXISTING); return finalLocation; } catch (IOException e) { throw new IllegalArgumentException(e); } }
private Path getFinalLocation(CubeDroneConfiguration cubeDroneConfiguration, After afterTestMethod) { return VideoFileDestination.getFinalLocation(afterTestMethod, cubeDroneConfiguration); } } `
and
´ package org.arquillian.cube.docker.drone;
import org.arquillian.cube.docker.drone.event.AfterConversion; import org.arquillian.cube.spi.Cube; import org.arquillian.cube.spi.CubeControlException; import org.arquillian.cube.spi.CubeRegistry; import org.jboss.arquillian.core.api.Event; import org.jboss.arquillian.core.api.Instance; import org.jboss.arquillian.core.api.annotation.Inject; import org.jboss.arquillian.core.api.annotation.Observes; import org.jboss.arquillian.test.spi.event.suite.AfterSuite;
import java.util.logging.Level; import java.util.logging.Logger;
public class VideoConversionLifecycleManager {
Cube flv2mp4;
@Inject
Event<AfterConversion> afterConversionEvent;
@Inject
Instance<SeleniumContainers> seleniumContainersInstance;
public void startConversion(@Observes AfterSuite afterSuite, CubeDroneConfiguration cubeDroneConfiguration,
CubeRegistry cubeRegistry) {
try {
if (cubeDroneConfiguration.isRecording()) {
initConversionCube(cubeRegistry);
flv2mp4.create();
flv2mp4.start();
afterConversionEvent.fire(new AfterConversion());
}
} catch (CubeControlException e) {
Logger.getLogger(VideoConversionLifecycleManager.class.getName()).log(Level.SEVERE, "Failed to start flv2mp4", e);
}
}
private void initConversionCube(CubeRegistry cubeRegistry) {
if (flv2mp4 == null) {
SeleniumContainers seleniumContainers = seleniumContainersInstance.get();
Cube conversionContainer = cubeRegistry.getCube(seleniumContainers.getVideoConverterContainerName());
if (conversionContainer == null) {
throw new IllegalArgumentException(
"Video conversion cube is not present in the registry.");
}
this.flv2mp4 = conversionContainer;
}
}
public void stopContainer(@Observes AfterConversion afterConversion) {
if (this.flv2mp4 != null) {
try {
flv2mp4.stop();
flv2mp4.destroy();
} catch (CubeControlException e) {
Logger.getLogger(VideoConversionLifecycleManager.class.getName()).log(Level.SEVERE, "Failed to stop flv2mp4", e);
}
}
}
} ´
Issue Overview
VideoConversionLifecycleManager & VncRecorderLifecycleManager both use
@Observes
for lifecycle.As
@Observes
events are handled directly in the event chain then extra care (try/catch) needs to be taken on event methods that may fail. Other observers must still be called even on an error, else cleanups are not run for example.Generally, all
@Observes
methods should be safe to call (try/catch), with their own error handling, else failures break the event chain.Expected Behaviour
At least an error message on failure, but not the failure of the test suite.
Current Behaviour
Any errors in
@Observes
methods cause the test suite to fail, eg:` org.arquillian.cube.spi.CubeControlException: Could not create flv2mp4 at org.arquillian.cube.spi.CubeControlException.failedCreate(CubeControlException.java:15) at org.arquillian.cube.docker.impl.model.DockerCube.create(DockerCube.java:115) at org.arquillian.cube.docker.drone.VideoConversionLifecycleManager.startConversion(VideoConversionLifec
.....
Caused by: com.github.dockerjava.api.exception.InternalServerErrorException: {"message":"Get https://registry-1.docker.io/v2/: dial tcp: lookup registry-1.docker.io on 192.168.65.1:53: no such host"} `
The above example is due to the fact the image is blocked by policy, but it should not break the suite.