quarkusio / quarkus

Quarkus: Supersonic Subatomic Java.
https://quarkus.io
Apache License 2.0
13.73k stars 2.67k forks source link

set up localstack docker as S3 mock in dev/test mode #18092

Closed liuzqt closed 1 year ago

liuzqt commented 3 years ago

Description

I want to setup localstack docker as S3 mock in dev/test just as mentioned in https://quarkus.io/guides/amazon-s3

I'm not sure whether we already have such built-in support for S3 just like how we use quarkus.datasource.devservices to spawn a temp testcontainer as datasource for dev/test purpose.

If not it's fine I can somehow walkaround it. I suppose I need to manually provision an S3 mock container when quarkus init, and somehow clean that docker when quarkus terminate. I'm new to quarkus, anyone could tell me how to do this?

Implementation ideas

If possible, add dev s3 properties to allow provisioning S3 mock docker during dev/test.

liuzqt commented 3 years ago

I tried to use testcontainer APIs to provision a localstack container when quarkus start, the problem is testcontainer only mapping to random port, which can only be known during runtime, so I can not specify quarkus.s3.endpoint-override in application.properties

corentinarnaud commented 2 years ago

You can declare your container in a QuarkusTestResourceLifecycleManager. The container will be started before quarkus context and populate the properties.

  public class DbContainerRessource implements QuarkusTestResourceLifecycleManager {
    private static final LocalStackContainer LOCAL_STACK_CONTAINER;

    static {
        DockerImageName localstackImage = DockerImageName.parse("localstack/localstack:0.11.3");
        S3_CONTAINER = new LocalStackContainer(localstackImage)
                .withServices(S3, SECRETSMANAGER);
    }

    @Override
    public Map<String, String> start() {
        LOCAL_STACK_CONTAINER.start();
        return Map.ofEntries(
            entry("quarkus.s3.endpoint-override", LOCAL_STACK_CONTAINER.getEndpointOverride(S3).toString()),
            entry("quarkus.s3.aws.region", LOCAL_STACK_CONTAINER.getRegion()),
            entry("quarkus.s3.aws.credentials.type", "static"),
            entry("quarkus.s3.aws.credentials.static-provider.access-key-id", LOCAL_STACK_CONTAINER.getAccessKey()),
            entry("quarkus.s3.aws.credentials.static-provider.secret-access-key", LOCAL_STACK_CONTAINER.getSecretKey()));
    }
  }

And you have to add @QuarkusTestResource(DbContainerRessource.class) on your test

mkouba commented 1 year ago

@liuzqt Have you tried the solution mentioned in https://github.com/quarkusio/quarkus/issues/18092#issuecomment-989744242?

scrocquesel commented 1 year ago

we add dev services to https://github.com/quarkiverse/quarkus-amazon-services/. If you are using the S3 client, you should have a look to the S3 extension

gsmet commented 1 year ago

Ah yes, thanks @scrocquesel , we can close this one.