Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a simple sanity and some simple test framework. #7

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 38 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
<!-- Dependency versions -->
<version.arquillian_core>1.8.0.Final</version.arquillian_core>
<version.testcontainers>1.19.8</version.testcontainers>
<version.org.junit>5.10.3</version.org.junit>

<!-- Plugin versions -->
<version.formatter.maven.plugin>2.23.0</version.formatter.maven.plugin>
Expand All @@ -95,18 +96,33 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>${version.org.junit}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers-bom</artifactId>
<version>${version.testcontainers}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-container-test-impl-base</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.test</groupId>
<artifactId>arquillian-test-spi</artifactId>
</dependency>
<!-- Required for the OperatesOnDeploymentAwareProvider -->
<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-container-test-impl-base</artifactId>
</dependency>
<!-- To avoid a CVE. This can be removed once TC is updated -->
<dependency>
<groupId>org.apache.commons</groupId>
Expand All @@ -116,7 +132,6 @@
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>${version.testcontainers}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
Expand All @@ -128,6 +143,24 @@
<artifactId>hamcrest</artifactId>
<scope>provided</scope>
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.junit5</groupId>
<artifactId>arquillian-junit5-container</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mockserver</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package org.jboss.arquillian.testcontainers;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import org.jboss.arquillian.core.api.InstanceProducer;
import org.jboss.arquillian.core.api.annotation.Inject;
Expand All @@ -16,8 +17,7 @@
import org.testcontainers.DockerClientFactory;
import org.testcontainers.containers.GenericContainer;

import com.github.dockerjava.api.DockerClient;

@SuppressWarnings("unused")
public class TestContainersObserver {
@Inject
@ClassScoped
Expand Down Expand Up @@ -49,21 +49,34 @@ public void stopContainer(@Observes AfterClass afterClass) {

private void checkForDocker(boolean failIfNoDocker) {
final String detailMessage = "No Docker environment is available.";
if (!isDockerAvailable() && failIfNoDocker) {
throw new AssertionError(detailMessage);
} else {
try {
Class<?> clazz = Class.forName("org.junit.AssumptionViolatedException");
Constructor<?> ctor = clazz.getDeclaredConstructor(String.class);
throw (RuntimeException) ctor.newInstance(detailMessage);
} catch (Exception e) {
// TestNG?
if (!isDockerAvailable()) {
if (failIfNoDocker) {
throw new AssertionError(detailMessage);
} else {
// First attempt to throw a JUnit 5 assumption
throwAssumption("org.opentest4j.TestAbortedException", detailMessage);
// Not found, attempt to throw a JUnit exception
throwAssumption("org.junit.AssumptionViolatedException", detailMessage);
// No supported test platform found. Throw an AssertionError.
throw new AssertionError("Failed to find a support test platform and no Docker environment is available.");
}
}
}

private void throwAssumption(final String type, final String detailMessage) {
try {
Class<?> clazz = Class.forName(type);
Constructor<?> ctor = clazz.getConstructor(String.class);
throw (RuntimeException) ctor.newInstance(detailMessage);
} catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException
| InvocationTargetException ignore) {
}
}

@SuppressWarnings({ "resource", "BooleanMethodIsAlwaysInverted" })
private boolean isDockerAvailable() {
try (DockerClient client = DockerClientFactory.instance().client()) {
try {
DockerClientFactory.instance().client();
return true;
} catch (Throwable ex) {
return false;
Expand Down
42 changes: 42 additions & 0 deletions src/test/java/org/jboss/arquillian/testcontainers/SanityTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright The Arquillian Authors
* SPDX-License-Identifier: Apache-2.0
*/

package org.jboss.arquillian.testcontainers;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.junit5.ArquillianExtension;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.arquillian.testcontainers.test.common.SimpleTestContainer;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

/**
* @author <a href="mailto:[email protected]">James R. Perkins</a>
*/
@ExtendWith(ArquillianExtension.class)
@TestContainer(value = SimpleTestContainer.class, failIfNoDocker = false)
@RunAsClient
public class SanityTest {

@ArquillianResource
private SimpleTestContainer container;

@Deployment
public static JavaArchive createDeployment() {
return ShrinkWrap.create(JavaArchive.class)
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
}

@Test
public void testContainerInjected() {
Assertions.assertNotNull(container, "Expected the container to be injected.");
Assertions.assertTrue(container.isRunning(), "Expected the container to be running");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright The Arquillian Authors
* SPDX-License-Identifier: Apache-2.0
*/

package org.jboss.arquillian.testcontainers.test.common;

import org.testcontainers.containers.MockServerContainer;
import org.testcontainers.utility.DockerImageName;

/**
* @author <a href="mailto:[email protected]">James R. Perkins</a>
*/
public class SimpleTestContainer extends MockServerContainer {

public SimpleTestContainer() {
super(DockerImageName
.parse("mockserver/mockserver")
.withTag("latest"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright The Arquillian Authors
* SPDX-License-Identifier: Apache-2.0
*/

package org.jboss.arquillian.testcontainers.test.common;

import org.jboss.arquillian.container.spi.ConfigurationException;
import org.jboss.arquillian.container.spi.client.container.ContainerConfiguration;
import org.jboss.arquillian.container.spi.client.container.DeployableContainer;
import org.jboss.arquillian.container.spi.client.protocol.ProtocolDescription;
import org.jboss.arquillian.container.spi.client.protocol.metadata.ProtocolMetaData;
import org.jboss.shrinkwrap.api.Archive;

/**
* @author <a href="mailto:[email protected]">James R. Perkins</a>
*/
public class TestDeployableContainer implements DeployableContainer<TestDeployableContainer.EmptyContainerConfiguration> {
@Override
public Class<EmptyContainerConfiguration> getConfigurationClass() {
return EmptyContainerConfiguration.class;
}

@Override
public ProtocolDescription getDefaultProtocol() {
return new ProtocolDescription("TestDeployableContainer");
}

@Override
public ProtocolMetaData deploy(final Archive<?> archive) {
return new ProtocolMetaData();
}

@Override
public void undeploy(final Archive<?> archive) {
}

public static class EmptyContainerConfiguration implements ContainerConfiguration {

@Override
public void validate() throws ConfigurationException {

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright The Arquillian Authors
* SPDX-License-Identifier: Apache-2.0
*/

package org.jboss.arquillian.testcontainers.test.common;

import org.jboss.arquillian.container.spi.client.container.DeployableContainer;
import org.jboss.arquillian.core.spi.LoadableExtension;

/**
* @author <a href="mailto:[email protected]">James R. Perkins</a>
*/
public class TestLoadableExtension implements LoadableExtension {
@Override
public void register(final ExtensionBuilder builder) {
builder.service(DeployableContainer.class, TestDeployableContainer.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.jboss.arquillian.testcontainers.test.common.TestLoadableExtension