Skip to content

Commit

Permalink
Remove third party container + Refactoring (testcontainers#512)
Browse files Browse the repository at this point in the history
  • Loading branch information
oussamabadr committed Dec 11, 2020
1 parent 553fc2d commit d447ffb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void saveRecordingToFile(File file) {

@SneakyThrows
public InputStream streamRecording() {
fixRecordDuration();
reencodeRecording();

TarArchiveInputStream archiveInputStream = new TarArchiveInputStream(
dockerClient.copyArchiveFromContainerCmd(getContainerId(), RECORDING_FILE_NAME).exec()
Expand All @@ -106,7 +106,7 @@ public InputStream streamRecording() {
return archiveInputStream;
}

private void fixRecordDuration() throws IOException, InterruptedException {
private void reencodeRecording() throws IOException, InterruptedException {
String newFlvOutput = "/newScreen.flv";
execInContainer("ffmpeg" , "-i", RECORDING_FILE_NAME, "-vcodec", "libx264", newFlvOutput);
execInContainer("mv" , "-f", newFlvOutput, RECORDING_FILE_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testcontainers.containers.BindMode;
import org.testcontainers.containers.BrowserWebDriverContainer;
import org.testcontainers.containers.DefaultRecordingFileFactory;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.SeleniumUtils;
import org.testcontainers.containers.output.ToStringConsumer;
import org.testcontainers.containers.startupcheck.OneShotStartupCheckStrategy;
import org.testcontainers.lifecycle.TestDescription;
import org.testcontainers.utility.DockerImageName;
import org.testcontainers.utility.MountableFile;
import org.testcontainers.utility.TestcontainersConfiguration;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -67,13 +69,13 @@ public String getFilesystemFriendlyName() {
}

@Test
public void recordingTestThatShouldHaveCorrectDuration() throws IOException {
public void recordingTestThatShouldHaveCorrectDuration() throws IOException, InterruptedException {
final String flvFileTitle = "ChromeThatRecordsAllTests-recordingTestThatShouldBeRecordedAndRetained";
final String flvFileNameRegEx = "PASSED-" + flvFileTitle + ".*\\.flv";

DockerImageName chromeDockerImageName = DockerImageName.parse("selenium/standalone-chrome-debug")
.withTag(SeleniumUtils.determineClasspathSeleniumVersion());
try (
BrowserWebDriverContainer chrome = new BrowserWebDriverContainer()
.withCapabilities(new ChromeOptions())
BrowserWebDriverContainer chrome = new BrowserWebDriverContainer(chromeDockerImageName)
.withRecordingMode(RECORD_ALL, vncRecordingDirectory.getRoot())
.withRecordingFileFactory(new DefaultRecordingFileFactory())
) {
Expand All @@ -95,20 +97,17 @@ public String getFilesystemFriendlyName() {
String recordedFile = vncRecordingDirectory.getRoot().listFiles(new PatternFilenameFilter(flvFileNameRegEx))[0].getCanonicalPath();

ToStringConsumer dockerLogConsumer = new ToStringConsumer();

try( GenericContainer container = new GenericContainer<>("jrottenberg/ffmpeg:3.2-alpine38") ) {
container.withStartupCheckStrategy(new OneShotStartupCheckStrategy())
.withFileSystemBind(recordedFile, "/tmp/chromeTestRecord.flv", BindMode.READ_WRITE )
.withCommand("-i" ,"/tmp/chromeTestRecord.flv")
try( GenericContainer<?> container = new GenericContainer<>(TestcontainersConfiguration.getInstance().getVncDockerImageName()) ) {
String recordFileContainerPath = "/tmp/chromeTestRecord.flv";
container.withCopyFileToContainer(MountableFile.forHostPath(recordedFile), recordFileContainerPath)
.withCreateContainerCmdModifier( createContainerCmd -> createContainerCmd.withEntrypoint("ffmpeg") )
.withCommand("-i" , recordFileContainerPath)
.withLogConsumer(dockerLogConsumer)
.start();
} catch (RuntimeException ex) {
// Container has started successfully but an exception is thrown as we used OneShotStartupCheckStrategy
// But (for now) OneShotStartupCheckStrategy is useful to get Duration from container output
}

String dockerOutput = dockerLogConsumer.toUtf8String();
assertTrue("Duration is incorrect: ", dockerOutput.matches("[\\S\\s]*Duration: (?!00:00:00.00)[\\S\\s]*"));
assertTrue("Duration is incorrect in:\n " + dockerOutput, dockerOutput.matches("[\\S\\s]*Duration: (?!00:00:00.00)[\\S\\s]*"));
}
}
}
Expand Down

0 comments on commit d447ffb

Please sign in to comment.