Skip to content

Commit

Permalink
Handle extraction of Jacoco report by test utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
thelarsjohansson committed Dec 4, 2023
1 parent a349ab1 commit 7c3c097
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 126 deletions.
33 changes: 33 additions & 0 deletions src/test/java/org/phoebus/olog/docker/ITUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,25 @@

package org.phoebus.olog.docker;

import org.junit.jupiter.api.AfterAll;
import org.phoebus.olog.entity.Log;
import org.phoebus.olog.entity.Logbook;
import org.phoebus.olog.entity.Property;
import org.phoebus.olog.entity.Tag;
import org.testcontainers.containers.ComposeContainer;
import org.testcontainers.containers.ContainerState;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.shaded.org.apache.commons.lang3.StringUtils;

import com.github.dockerjava.api.DockerClient;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -123,6 +128,34 @@ public static ComposeContainer defaultComposeContainers() {
.waitingFor(ITUtil.OLOG, Wait.forLogMessage(ITUtil.INTEGRATIONTEST_LOG_MESSAGE, 1));
}

/**
* Extract coverage report from compose container to file system.
*
* @param environment compose container
* @param destinationPath destination path, i.e. where in file system to put coverage report
* that has been extracted from container
*/
public static void extractJacocoReport(ComposeContainer environment, String destinationPath) {
// extract jacoco report from container file system
// stop jvm to make data available

if (!Boolean.FALSE.toString().equals(System.getProperty(ITUtil.JACOCO_SKIPITCOVERAGE))) {
return;
}

Optional<ContainerState> container = environment.getContainerByServiceName(ITUtil.OLOG);
if (container.isPresent()) {
ContainerState cs = container.get();
DockerClient dc = cs.getDockerClient();
dc.stopContainerCmd(cs.getContainerId()).exec();
try {
cs.copyFileFromContainer(ITUtil.JACOCO_EXEC_PATH, destinationPath);
} catch (Exception e) {
// proceed if file cannot be copied
}
}
}

/**
* Refresh Elastic indices and return response code and string.
*
Expand Down
23 changes: 2 additions & 21 deletions src/test/java/org/phoebus/olog/docker/OlogIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,11 @@
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.ComposeContainer;
import org.testcontainers.containers.ContainerState;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import com.github.dockerjava.api.DockerClient;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
Expand Down Expand Up @@ -68,23 +64,8 @@ class OlogIT {
@AfterAll
public static void extractJacocoReport() {
// extract jacoco report from container file system
// stop jvm to make data available

if (!Boolean.FALSE.toString().equals(System.getProperty(ITUtil.JACOCO_SKIPITCOVERAGE))) {
return;
}

Optional<ContainerState> container = ENVIRONMENT.getContainerByServiceName(ITUtil.OLOG);
if (container.isPresent()) {
ContainerState cs = container.get();
DockerClient dc = cs.getDockerClient();
dc.stopContainerCmd(cs.getContainerId()).exec();
try {
cs.copyFileFromContainer(ITUtil.JACOCO_EXEC_PATH, ITUtil.JACOCO_TARGET_PREFIX + OlogIT.class.getSimpleName() + ITUtil.JACOCO_TARGET_SUFFIX);
} catch (Exception e) {
// proceed if file cannot be copied
}
}
ITUtil.extractJacocoReport(ENVIRONMENT,
ITUtil.JACOCO_TARGET_PREFIX + OlogIT.class.getSimpleName() + ITUtil.JACOCO_TARGET_SUFFIX);
}

@Test
Expand Down
23 changes: 2 additions & 21 deletions src/test/java/org/phoebus/olog/docker/OlogLogbooksIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,11 @@
import org.phoebus.olog.entity.Logbook;
import org.phoebus.olog.entity.State;
import org.testcontainers.containers.ComposeContainer;
import org.testcontainers.containers.ContainerState;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import com.github.dockerjava.api.DockerClient;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
Expand Down Expand Up @@ -147,23 +143,8 @@ public static void tearDownObjects() {
@AfterAll
public static void extractJacocoReport() {
// extract jacoco report from container file system
// stop jvm to make data available

if (!Boolean.FALSE.toString().equals(System.getProperty(ITUtil.JACOCO_SKIPITCOVERAGE))) {
return;
}

Optional<ContainerState> container = ENVIRONMENT.getContainerByServiceName(ITUtil.OLOG);
if (container.isPresent()) {
ContainerState cs = container.get();
DockerClient dc = cs.getDockerClient();
dc.stopContainerCmd(cs.getContainerId()).exec();
try {
cs.copyFileFromContainer(ITUtil.JACOCO_EXEC_PATH, ITUtil.JACOCO_TARGET_PREFIX + OlogLogbooksIT.class.getSimpleName() + ITUtil.JACOCO_TARGET_SUFFIX);
} catch (Exception e) {
// proceed if file cannot be copied
}
}
ITUtil.extractJacocoReport(ENVIRONMENT,
ITUtil.JACOCO_TARGET_PREFIX + OlogLogbooksIT.class.getSimpleName() + ITUtil.JACOCO_TARGET_SUFFIX);
}

@Test
Expand Down
23 changes: 2 additions & 21 deletions src/test/java/org/phoebus/olog/docker/OlogLogsIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,11 @@
import org.phoebus.olog.docker.ITUtil.AuthorizationChoice;
import org.phoebus.olog.entity.Log;
import org.testcontainers.containers.ComposeContainer;
import org.testcontainers.containers.ContainerState;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import com.github.dockerjava.api.DockerClient;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
Expand Down Expand Up @@ -89,23 +85,8 @@ class OlogLogsIT {
@AfterAll
public static void extractJacocoReport() {
// extract jacoco report from container file system
// stop jvm to make data available

if (!Boolean.FALSE.toString().equals(System.getProperty(ITUtil.JACOCO_SKIPITCOVERAGE))) {
return;
}

Optional<ContainerState> container = ENVIRONMENT.getContainerByServiceName(ITUtil.OLOG);
if (container.isPresent()) {
ContainerState cs = container.get();
DockerClient dc = cs.getDockerClient();
dc.stopContainerCmd(cs.getContainerId()).exec();
try {
cs.copyFileFromContainer(ITUtil.JACOCO_EXEC_PATH, ITUtil.JACOCO_TARGET_PREFIX + OlogLogsIT.class.getSimpleName() + ITUtil.JACOCO_TARGET_SUFFIX);
} catch (Exception e) {
// proceed if file cannot be copied
}
}
ITUtil.extractJacocoReport(ENVIRONMENT,
ITUtil.JACOCO_TARGET_PREFIX + OlogLogsIT.class.getSimpleName() + ITUtil.JACOCO_TARGET_SUFFIX);
}

@Test
Expand Down
23 changes: 2 additions & 21 deletions src/test/java/org/phoebus/olog/docker/OlogLogsQueryIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,13 @@
import org.junit.jupiter.api.Test;
import org.phoebus.olog.entity.SearchResult;
import org.testcontainers.containers.ComposeContainer;
import org.testcontainers.containers.ContainerState;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import com.github.dockerjava.api.DockerClient;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
Expand Down Expand Up @@ -91,23 +87,8 @@ class OlogLogsQueryIT {
@AfterAll
public static void extractJacocoReport() {
// extract jacoco report from container file system
// stop jvm to make data available

if (!Boolean.FALSE.toString().equals(System.getProperty(ITUtil.JACOCO_SKIPITCOVERAGE))) {
return;
}

Optional<ContainerState> container = ENVIRONMENT.getContainerByServiceName(ITUtil.OLOG);
if (container.isPresent()) {
ContainerState cs = container.get();
DockerClient dc = cs.getDockerClient();
dc.stopContainerCmd(cs.getContainerId()).exec();
try {
cs.copyFileFromContainer(ITUtil.JACOCO_EXEC_PATH, ITUtil.JACOCO_TARGET_PREFIX + OlogLogsQueryIT.class.getSimpleName() + ITUtil.JACOCO_TARGET_SUFFIX);
} catch (Exception e) {
// proceed if file cannot be copied
}
}
ITUtil.extractJacocoReport(ENVIRONMENT,
ITUtil.JACOCO_TARGET_PREFIX + OlogLogsQueryIT.class.getSimpleName() + ITUtil.JACOCO_TARGET_SUFFIX);
}

@Test
Expand Down
23 changes: 2 additions & 21 deletions src/test/java/org/phoebus/olog/docker/OlogPropertiesIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,12 @@
import org.phoebus.olog.entity.Property;
import org.phoebus.olog.entity.State;
import org.testcontainers.containers.ComposeContainer;
import org.testcontainers.containers.ContainerState;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import com.github.dockerjava.api.DockerClient;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.HashSet;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
Expand Down Expand Up @@ -221,23 +217,8 @@ public static void tearDownObjects() {
@AfterAll
public static void extractJacocoReport() {
// extract jacoco report from container file system
// stop jvm to make data available

if (!Boolean.FALSE.toString().equals(System.getProperty(ITUtil.JACOCO_SKIPITCOVERAGE))) {
return;
}

Optional<ContainerState> container = ENVIRONMENT.getContainerByServiceName(ITUtil.OLOG);
if (container.isPresent()) {
ContainerState cs = container.get();
DockerClient dc = cs.getDockerClient();
dc.stopContainerCmd(cs.getContainerId()).exec();
try {
cs.copyFileFromContainer(ITUtil.JACOCO_EXEC_PATH, ITUtil.JACOCO_TARGET_PREFIX + OlogPropertiesIT.class.getSimpleName() + ITUtil.JACOCO_TARGET_SUFFIX);
} catch (Exception e) {
// proceed if file cannot be copied
}
}
ITUtil.extractJacocoReport(ENVIRONMENT,
ITUtil.JACOCO_TARGET_PREFIX + OlogPropertiesIT.class.getSimpleName() + ITUtil.JACOCO_TARGET_SUFFIX);
}

@Test
Expand Down
23 changes: 2 additions & 21 deletions src/test/java/org/phoebus/olog/docker/OlogTagsIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,11 @@
import org.phoebus.olog.entity.State;
import org.phoebus.olog.entity.Tag;
import org.testcontainers.containers.ComposeContainer;
import org.testcontainers.containers.ContainerState;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import com.github.dockerjava.api.DockerClient;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
Expand Down Expand Up @@ -145,23 +141,8 @@ public static void tearDownObjects() {
@AfterAll
public static void extractJacocoReport() {
// extract jacoco report from container file system
// stop jvm to make data available

if (!Boolean.FALSE.toString().equals(System.getProperty(ITUtil.JACOCO_SKIPITCOVERAGE))) {
return;
}

Optional<ContainerState> container = ENVIRONMENT.getContainerByServiceName(ITUtil.OLOG);
if (container.isPresent()) {
ContainerState cs = container.get();
DockerClient dc = cs.getDockerClient();
dc.stopContainerCmd(cs.getContainerId()).exec();
try {
cs.copyFileFromContainer(ITUtil.JACOCO_EXEC_PATH, ITUtil.JACOCO_TARGET_PREFIX + OlogTagsIT.class.getSimpleName() + ITUtil.JACOCO_TARGET_SUFFIX);
} catch (Exception e) {
// proceed if file cannot be copied
}
}
ITUtil.extractJacocoReport(ENVIRONMENT,
ITUtil.JACOCO_TARGET_PREFIX + OlogTagsIT.class.getSimpleName() + ITUtil.JACOCO_TARGET_SUFFIX);
}

@Test
Expand Down

0 comments on commit 7c3c097

Please sign in to comment.