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

RSE-934: Migrate api new testdeck - config the cluster environment #8844

Closed
wants to merge 8 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ import java.util.function.Consumer
abstract class BaseContainer extends Specification implements ClientProvider {
public static final String PROJECT_NAME = 'test'
private static RdContainer RUNDECK
private static RdClusterDockerContainer RUNDECK_CLUSTER
private static final Object LOCK = new Object()
private static ClientProvider CLIENT_PROVIDER
private static final String DEFAULT_DOCKERFILE_LOCATION = System.getenv("DEFAULT_DOCKERFILE_LOCATION") ?: System.getProperty("DEFAULT_DOCKERFILE_LOCATION")
private static final String DEFAULT_CLUSTER_PATH = System.getenv("COMPOSE_CLUSTER_PATH") ?: System.getProperty("COMPOSE_CLUSTER_PATH")

ClientProvider getClientProvider() {
if (System.getenv("TEST_RUNDECK_URL") != null) {
Expand All @@ -34,15 +36,20 @@ abstract class BaseContainer extends Specification implements ClientProvider {
RdClient clientWithToken(String token) {
return RdClient.create(System.getenv("TEST_RUNDECK_URL"), token)
}

@Override
RdClient getClusterClient() {
return RdClient.create("http://localhost:4440", "admintoken")
}
}
}
} else if (DEFAULT_DOCKERFILE_LOCATION != null && !DEFAULT_DOCKERFILE_LOCATION.isEmpty() && CLIENT_PROVIDER == null){
} else if (DEFAULT_DOCKERFILE_LOCATION != null && !DEFAULT_DOCKERFILE_LOCATION.isEmpty() && CLIENT_PROVIDER == null && DEFAULT_CLUSTER_PATH == null){
synchronized (LOCK) {
RdDockerContainer rdDockerContainer = new RdDockerContainer(getClass().getClassLoader().getResource(DEFAULT_DOCKERFILE_LOCATION).toURI())
rdDockerContainer.start()
CLIENT_PROVIDER = rdDockerContainer
}
} else if (RUNDECK == null && DEFAULT_DOCKERFILE_LOCATION == null) {
} else if (RUNDECK == null && DEFAULT_DOCKERFILE_LOCATION == null && DEFAULT_CLUSTER_PATH == null) {
synchronized (LOCK) {
log.info("Starting testcontainer: ${getClass().getClassLoader().getResource(System.getProperty("COMPOSE_PATH")).toURI()}")
log.info("Starting testcontainer: RUNDECK_IMAGE: ${RdContainer.RUNDECK_IMAGE}")
Expand All @@ -52,6 +59,15 @@ abstract class BaseContainer extends Specification implements ClientProvider {
RUNDECK.start()
CLIENT_PROVIDER = RUNDECK
}
} else if (RUNDECK_CLUSTER == null && DEFAULT_CLUSTER_PATH != null) {
synchronized (LOCK) {
log.info("Starting cluster testcontainer: ${DEFAULT_CLUSTER_PATH}")
log.info("Starting cluster testcontainer: RUNDECK_IMAGE: ${RdClusterDockerContainer.RUNDECK_IMAGE}")
log.info("Starting cluster testcontainer: LICENSE_LOCATION: ${RdClusterDockerContainer.LICENSE_LOCATION}")
RUNDECK_CLUSTER = new RdClusterDockerContainer(getClass().getClassLoader().getResource(DEFAULT_CLUSTER_PATH).toURI())
RUNDECK_CLUSTER.start()
CLIENT_PROVIDER = RUNDECK_CLUSTER
}
}
return CLIENT_PROVIDER
}
Expand Down Expand Up @@ -84,6 +100,7 @@ abstract class BaseContainer extends Specification implements ClientProvider {
}

RdClient _client
RdClient _clientSecond
@Override
RdClient getClient() {
if (null == _client) {
Expand All @@ -95,6 +112,11 @@ abstract class BaseContainer extends Specification implements ClientProvider {
RdClient createClient() {
return clientProvider.getClient()
}

RdClient createSecondClient() {
return clientProvider.getClusterClient()
}

Map<String, RdClient> tokenProviders = [:]

@Override
Expand All @@ -105,6 +127,14 @@ abstract class BaseContainer extends Specification implements ClientProvider {
return tokenProviders[token]
}

@Override
RdClient getClusterClient() {
if (null == _clientSecond) {
_clientSecond = createSecondClient()
}
return _clientSecond
}

void startEnvironment() {
getClientProvider()
}
Expand Down Expand Up @@ -134,6 +164,10 @@ abstract class BaseContainer extends Specification implements ClientProvider {
return client.get(path, clazz)
}

<T> T getSecondCluster(String path, Class<T> clazz) {
return clusterClient.get(path, clazz)
}

Response doPost(String path, Object body = null) {
return client.doPost(path, body)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ interface ClientProvider {
RdClient getClient()

RdClient clientWithToken(String token)

RdClient getClusterClient()
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import okhttp3.Response
import okhttp3.ResponseBody
import org.jetbrains.annotations.NotNull

import java.util.concurrent.TimeUnit
import java.util.function.Consumer

@CompileStatic
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.rundeck.util.container

import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import org.testcontainers.containers.DockerComposeContainer
import org.testcontainers.containers.GenericContainer
import org.testcontainers.containers.output.Slf4jLogConsumer
import org.testcontainers.containers.wait.strategy.Wait
import org.testcontainers.images.builder.ImageFromDockerfile
import org.testcontainers.shaded.com.google.common.collect.ImmutableList

import java.nio.file.Paths
import java.time.Duration

@CompileStatic
@Slf4j
class RdClusterDockerContainer extends DockerComposeContainer<RdClusterDockerContainer> implements ClientProvider {

public static final String DEFAULT_SERVICES_TO_EXPOSE = System.getenv("TEST_RUNDECK_CONTAINERS_SERVICE") ?: 'rundeck-1'
public static final String DEFAULT_SERVICES_TO_EXPOSE_SECOND = System.getenv("TEST_RUNDECK_CONTAINERS_SERVICE_SECOND") ?: 'rundeck-2'
private static final String STATIC_TOKEN = System.getenv("TEST_RUNDECK_CONTAINER_TOKEN") ?: 'admintoken'
private static final Integer DEFAULT_PORT = System.getenv("TEST_RUNDECK_CONTAINER_PORT")?.toInteger() ?: 4440
public static final String RUNDECK_IMAGE = System.getenv("TEST_IMAGE") ?: System.getProperty("TEST_IMAGE")
public static final String LICENSE_LOCATION = System.getenv("LICENSE_LOCATION")

RdClusterDockerContainer(URI dockerFileLocation) {
super(new File(dockerFileLocation))
withExposedService(DEFAULT_SERVICES_TO_EXPOSE, DEFAULT_PORT,
Wait.forListeningPort().withStartupTimeout(Duration.ofMinutes(15)))
withExposedService(DEFAULT_SERVICES_TO_EXPOSE_SECOND, DEFAULT_PORT,
Wait.forListeningPort().withStartupTimeout(Duration.ofMinutes(15)))
withEnv("TEST_IMAGE", RUNDECK_IMAGE)
withEnv("LICENSE_LOCATION", LICENSE_LOCATION)
withLogConsumer(DEFAULT_SERVICES_TO_EXPOSE, new Slf4jLogConsumer(log))
withLogConsumer(DEFAULT_SERVICES_TO_EXPOSE_SECOND, new Slf4jLogConsumer(log))
waitingFor(DEFAULT_SERVICES_TO_EXPOSE,
Wait.forHttp("/api/14/system/info")
.forStatusCodeMatching(it -> it >= 200 && it < 500 && it != 404)
.withStartupTimeout(Duration.ofMinutes(15)))
waitingFor(DEFAULT_SERVICES_TO_EXPOSE_SECOND,
Wait.forHttp("/api/14/system/info")
.forStatusCodeMatching(it -> it >= 200 && it < 500 && it != 404)
.withStartupTimeout(Duration.ofMinutes(15)))
}

@Override
void close() {
super.close()
}

RdClient getClient() {
clientWithToken(STATIC_TOKEN)
}

RdClient clientWithToken(String token) {
RdClient.create("http://${getServiceHost(DEFAULT_SERVICES_TO_EXPOSE, DEFAULT_PORT)}:${getServicePort(DEFAULT_SERVICES_TO_EXPOSE, DEFAULT_PORT)}", token)
}

RdClient getClusterClient() {
RdClient.create("http://${getServiceHost(DEFAULT_SERVICES_TO_EXPOSE_SECOND, DEFAULT_PORT)}:${getServicePort(DEFAULT_SERVICES_TO_EXPOSE_SECOND, DEFAULT_PORT)}", STATIC_TOKEN)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ class RdContainer extends DockerComposeContainer<RdContainer> implements ClientP
RdClient.create("http://${getServiceHost(DEFAULT_SERVICE_TO_EXPOSE,DEFAULT_PORT)}:${getServicePort(DEFAULT_SERVICE_TO_EXPOSE,DEFAULT_PORT)}${CONTEXT_PATH}", token)
}

RdClient getClusterClient() {
RdClient.create("http://localhost:4440", STATIC_TOKEN)
}

@Override
void close() {
super.close()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,8 @@ class RdDockerContainer extends GenericContainer<RdDockerContainer> implements C
RdClient clientWithToken(String token) {
RdClient.create("http://${host}:${firstMappedPort}${CONTEXT_PATH}", token)
}

RdClient getClusterClient() {
RdClient.create("http://localhost:4440", STATIC_TOKEN)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class SeleniumBase extends BaseContainer implements WebDriver, SeleniumContext {
options.addArguments("--disable-extensions")
options.addArguments("--disable-popup-blocking")
options.addArguments("--disable-default-apps")
options.addArguments("--disable-blink-features=AutomationControlled")
_driver = new ChromeDriver(options)
}
return _driver
Expand Down