Skip to content

Commit

Permalink
JAMES-2525 enforce singleton swift container for test
Browse files Browse the repository at this point in the history
  • Loading branch information
jeantil authored and chibenwa committed Oct 11, 2018
1 parent 5dff052 commit 7c57ae1
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 49 deletions.
@@ -0,0 +1,134 @@
/****************************************************************
* Licensed to the Apache Software Foundation (ASF) under one *
* or more contributor license agreements. See the NOTICE file *
* distributed with this work for additional information *
* regarding copyright ownership. The ASF licenses this file *
* to you under the Apache License, Version 2.0 (the *
* "License"); you may not use this file except in compliance *
* with the License. You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, *
* software distributed under the License is distributed on an *
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
* KIND, either express or implied. See the License for the *
* specific language governing permissions and limitations *
* under the License. *
****************************************************************/

package org.apache.james.blob.objectstorage;

import static org.testcontainers.containers.wait.strategy.Wait.forHttp;

import java.net.URI;

import org.apache.james.util.Host;
import org.apache.james.util.docker.RateLimiters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.Container;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.output.OutputFrame;
import org.testcontainers.containers.wait.strategy.WaitAllStrategy;

import com.github.fge.lambdas.Throwing;

public class DockerSwiftContainer {
private static final Logger LOGGER = LoggerFactory.getLogger(DockerSwiftContainer.class);
private static final String SWIFT_DOCKER_IMAGE = "jeantil/openstack-keystone-swift:pike";
private static final int SWIFT_PORT = 8080;
private static final int KEYSTONE_ADMIN_PORT = 35357;
private final GenericContainer<?> swiftContainer;
private DockerSwift dockerSwift;

public DockerSwiftContainer() {
this.swiftContainer = new GenericContainer<>(SWIFT_DOCKER_IMAGE)
.withExposedPorts(KEYSTONE_ADMIN_PORT)
.withExposedPorts(SWIFT_PORT)
.withLogConsumer(DockerSwiftContainer::displayDockerLog)
.waitingFor(
new WaitAllStrategy()
.withStrategy(
forHttp("/v3")
.forPort(KEYSTONE_ADMIN_PORT)
.forStatusCode(200)
.withRateLimiter(RateLimiters.DEFAULT)
).withStrategy(
forHttp("/info")
.forPort(SWIFT_PORT)
.forStatusCode(200)
.withRateLimiter(RateLimiters.DEFAULT)
)
);

}

public void start() {
swiftContainer.start();
Integer swiftPort = swiftContainer.getMappedPort(SWIFT_PORT);
String containerIpAddress = swiftContainer.getContainerIpAddress();
Container.ExecResult execResult =
Throwing.supplier(() ->
swiftContainer.execInContainer(
"/swift/bin/register-swift-endpoint.sh",
"http://" + containerIpAddress + ":" + swiftPort))
.sneakyThrow()
.get();
if (!execResult.getStdout().isEmpty()) {
LOGGER.debug(execResult.getStdout());
}
if (!execResult.getStderr().isEmpty()) {
LOGGER.error(execResult.getStderr());
}
URI keystoneV2Endpoint =
URI.create("http://" + getKeystoneHost() + "/v2.0");
URI keystoneV3Endpoint =
URI.create("http://" + getKeystoneHost() + "/v3");
URI swiftEndpoint =
URI.create("http://" + getSwiftHost() + "/auth/v1.0");
dockerSwift = new DockerSwift(keystoneV2Endpoint, keystoneV3Endpoint, swiftEndpoint);

}

public void stop() {
swiftContainer.stop();
}

public Host getKeystoneHost() {
return Host.from(
getIp(),
getKeystonePort());
}

public Host getSwiftHost() {
return Host.from(
getIp(),
getSwiftPort());
}

public String getIp() {
return swiftContainer.getContainerIpAddress();
}

public int getKeystonePort() {
return swiftContainer.getMappedPort(KEYSTONE_ADMIN_PORT);
}

public int getSwiftPort() {
return swiftContainer.getMappedPort(SWIFT_PORT);
}

public DockerSwift dockerSwift() {
return dockerSwift;
}

public GenericContainer<?> getRawContainer() {
return swiftContainer;
}

private static void displayDockerLog(OutputFrame outputFrame) {
LOGGER.info(outputFrame.getUtf8String());
}

}
Expand Up @@ -19,73 +19,27 @@

package org.apache.james.blob.objectstorage;

import static org.testcontainers.containers.wait.strategy.Wait.forHttp;

import java.net.URI;

import org.apache.james.util.docker.RateLimiters;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolutionException;
import org.junit.jupiter.api.extension.ParameterResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.Container;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.WaitAllStrategy;

public class DockerSwiftExtension implements ParameterResolver, BeforeAllCallback,
AfterAllCallback {
private static final Logger LOGGER = LoggerFactory.getLogger(DockerSwiftExtension.class);
private static final String SWIFT_DOCKER_IMAGE = "jeantil/openstack-keystone-swift:pike";
private static final int SWIFT_PORT = 8080;
private static final int KEYSTONE_ADMIN_PORT = 35357;

private final GenericContainer<?> swiftContainer;

private final DockerSwiftRule swiftContainer;
private DockerSwift dockerSwift;

public DockerSwiftExtension() {
swiftContainer = new GenericContainer<>(SWIFT_DOCKER_IMAGE)
.withExposedPorts(KEYSTONE_ADMIN_PORT)
.withExposedPorts(SWIFT_PORT)
.waitingFor(
new WaitAllStrategy().withStrategy(
forHttp("/v3")
.forPort(KEYSTONE_ADMIN_PORT)
.forStatusCode(200)
.withRateLimiter(RateLimiters.DEFAULT)
).withStrategy(
forHttp("/info")
.forPort(SWIFT_PORT)
.forStatusCode(200)
.withRateLimiter(RateLimiters.DEFAULT)
)
);
swiftContainer = new DockerSwiftRule();
}

@Override
public void beforeAll(ExtensionContext context) throws Exception {
swiftContainer.start();
Integer keystonePort = swiftContainer.getMappedPort(KEYSTONE_ADMIN_PORT);
Integer swiftPort = swiftContainer.getMappedPort(SWIFT_PORT);
String containerIpAddress = swiftContainer.getContainerIpAddress();
Container.ExecResult execResult = swiftContainer.execInContainer("/swift/bin/register-swift-endpoint.sh", "http://" + containerIpAddress + ":" + swiftPort);
if (!execResult.getStdout().isEmpty()) {
LOGGER.debug(execResult.getStdout());
}
if (!execResult.getStderr().isEmpty()) {
LOGGER.error(execResult.getStderr());
}
URI keystoneV2Endpoint =
URI.create("http://" + containerIpAddress + ":" + keystonePort + "/v2.0");
URI keystoneV3Endpoint =
URI.create("http://" + containerIpAddress + ":" + keystonePort + "/v3");
URI swiftEndpoint =
URI.create("http://" + containerIpAddress + ":" + swiftPort + "/auth/v1.0");
dockerSwift = new DockerSwift(keystoneV2Endpoint, keystoneV3Endpoint, swiftEndpoint);
dockerSwift = swiftContainer.dockerSwift();
}

@Override
Expand Down
@@ -0,0 +1,49 @@
/****************************************************************
* Licensed to the Apache Software Foundation (ASF) under one *
* or more contributor license agreements. See the NOTICE file *
* distributed with this work for additional information *
* regarding copyright ownership. The ASF licenses this file *
* to you under the Apache License, Version 2.0 (the *
* "License"); you may not use this file except in compliance *
* with the License. You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, *
* software distributed under the License is distributed on an *
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
* KIND, either express or implied. See the License for the *
* specific language governing permissions and limitations *
* under the License. *
****************************************************************/

package org.apache.james.blob.objectstorage;

import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.testcontainers.containers.GenericContainer;

public class DockerSwiftRule implements TestRule {

@Override
public Statement apply(Statement base, Description description) {
return base;
}

public void start() {

}

public void stop() {

}

public GenericContainer<?> getRawContainer() {
return DockerSwiftSingleton.singleton.getRawContainer();
}

public DockerSwift dockerSwift() {
return DockerSwiftSingleton.singleton.dockerSwift();
}
}
@@ -0,0 +1,31 @@
/****************************************************************
* Licensed to the Apache Software Foundation (ASF) under one *
* or more contributor license agreements. See the NOTICE file *
* distributed with this work for additional information *
* regarding copyright ownership. The ASF licenses this file *
* to you under the Apache License, Version 2.0 (the *
* "License"); you may not use this file except in compliance *
* with the License. You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, *
* software distributed under the License is distributed on an *
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
* KIND, either express or implied. See the License for the *
* specific language governing permissions and limitations *
* under the License. *
****************************************************************/

package org.apache.james.blob.objectstorage;

public class DockerSwiftSingleton {

public static final DockerSwiftContainer singleton = new DockerSwiftContainer();

static {
singleton.start();
}

// Cleanup will be performed by test container resource reaper
}

0 comments on commit 7c57ae1

Please sign in to comment.