Skip to content

Commit

Permalink
Ensure that all test container images are locally provided
Browse files Browse the repository at this point in the history
  • Loading branch information
jordansimsmith committed Jul 15, 2024
1 parent d67fa5b commit 50630c5
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 18 deletions.
13 changes: 12 additions & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,25 @@ bazel_dep(name = "rules_oci", version = "2.0.0-beta1")
oci = use_extension("@rules_oci//oci:extensions.bzl", "oci")
oci.pull(
name = "dynamodb",
digest = "sha256:7fb71c1bca230a7eb97943a836b3ea5995dfff155cf3fccaf53f94104e5ea3ee",
image = "docker.io/amazon/dynamodb-local",
platforms = [
"linux/amd64",
"linux/arm64",
],
tag = "2.5.2",
)
use_repo(oci, "dynamodb", "dynamodb_linux_amd64", "dynamodb_linux_arm64")
oci.pull(
name = "ryuk",
digest = "sha256:580a40fdb56f8a9316d0cfcd51f8a213297760219ed548f94f8e718c4387ba01",
image = "docker.io/testcontainers/ryuk",
platforms = [
"linux/amd64",
"linux/arm64",
],
tag = "0.6.0",
)
use_repo(oci, "dynamodb", "dynamodb_linux_amd64", "dynamodb_linux_arm64", "ryuk", "ryuk_linux_amd64", "ryuk_linux_arm64")

bazel_dep(name = "aspect_rules_lint", version = "0.21.0")

Expand Down
56 changes: 51 additions & 5 deletions MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions lib/testcontainers/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
load("@rules_oci//oci:defs.bzl", "oci_image", "oci_load")

oci_image(
name = "ryuk-image",
base = "@ryuk//:ryuk",
)

oci_load(
name = "ryuk-load",
image = ":ryuk-image",
repo_tags = [
"bazel/ryuk:ryuk",
],
)

oci_image(
name = "dynamodb-image",
base = "@dynamodb//:dynamodb",
Expand All @@ -18,6 +31,7 @@ java_library(
srcs = glob(["src/main/java/**/*.java"]),
data = [
":dynamodb-load",
":ryuk-load",
],
resources = glob([
"src/main/resources/**/*.properties",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

import java.net.URI;
import java.net.URISyntaxException;
import org.testcontainers.containers.GenericContainer;

public class DynamoDbContainer extends LoadedContainer<DynamoDbContainer> {
public class DynamoDbContainer extends GenericContainer<DynamoDbContainer> {
private static final int DYNAMODB_PORT = 8000;

public DynamoDbContainer() {
super("dynamodb.image.name", "dynamodb.image.loader");
super(LoadedImage.loadImage("dynamodb.image.name", "dynamodb.image.loader"));

this.withExposedPorts(DYNAMODB_PORT);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,11 @@

import com.google.common.base.Preconditions;
import java.io.IOException;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.utility.DockerImageName;
import org.testcontainers.utility.TestcontainersConfiguration;

class LoadedContainer<SELF extends LoadedContainer<SELF>> extends GenericContainer<SELF> {

public LoadedContainer(String imageKey, String loaderKey) {
super(loadImage(imageKey, loaderKey));

this.withImagePullPolicy(image -> false);
}

private static DockerImageName loadImage(String imageKey, String loaderKey) {
public class LoadedImage {
public static DockerImageName loadImage(String imageKey, String loaderKey) {
var image =
TestcontainersConfiguration.getInstance().getClasspathProperties().getProperty(imageKey);
var loader =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.jordansimsmith.testcontainers;

import com.google.common.base.Preconditions;
import org.testcontainers.utility.DockerImageName;
import org.testcontainers.utility.ImageNameSubstitutor;
import org.testcontainers.utility.TestcontainersConfiguration;

public class LoadedImageNameSubstitutor extends ImageNameSubstitutor {

@Override
public DockerImageName apply(DockerImageName original) {
if (original.toString().equals("testcontainers/ryuk:0.7.0")) {
return LoadedImage.loadImage("ryuk.image.name", "ryuk.image.loader");
}

var loadedPrefix =
TestcontainersConfiguration.getInstance()
.getClasspathProperties()
.getProperty("loaded.image.prefix");
Preconditions.checkNotNull(loadedPrefix);
if (original.toString().startsWith(loadedPrefix)) {
return original;
}

throw new IllegalArgumentException(
"All images must be loaded, refusing to resolve image:" + original);
}

@Override
protected String getDescription() {
return this.getClass().toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
image.substitutor=com.jordansimsmith.testcontainers.LoadedImageNameSubstitutor
loaded.image.prefix=bazel/

ryuk.image.name=bazel/ryuk:ryuk
ryuk.image.loader=lib/testcontainers/ryuk-load.sh

dynamodb.image.name=bazel/dynamodb:dynamodb
dynamodb.image.loader=lib/testcontainers/dynamodb-load.sh

0 comments on commit 50630c5

Please sign in to comment.