Skip to content

Commit

Permalink
NA: Reuse test containers
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagohora committed Sep 2, 2024
1 parent a966fd5 commit 027f37a
Show file tree
Hide file tree
Showing 10 changed files with 738 additions and 530 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@
import org.junit.jupiter.params.provider.ValueSource;
import org.testcontainers.containers.ClickHouseContainer;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import ru.vyarus.dropwizard.guice.test.ClientSupport;
import ru.vyarus.dropwizard.guice.test.jupiter.ext.TestDropwizardAppExtension;
import uk.co.jemos.podam.api.PodamFactory;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -74,9 +74,7 @@
import java.util.stream.Stream;

import static com.comet.opik.api.DatasetItem.DatasetItemPage;
import static com.comet.opik.api.resources.utils.ClickHouseContainerUtils.DATABASE_NAME;
import static com.comet.opik.api.resources.utils.MigrationUtils.CLICKHOUSE_CHANGELOG_FILE;
import static com.comet.opik.api.resources.utils.WireMockUtils.WireMockRuntime;
import static com.comet.opik.infrastructure.auth.RequestContext.SESSION_COOKIE;
import static com.comet.opik.infrastructure.auth.RequestContext.WORKSPACE_HEADER;
import static com.comet.opik.infrastructure.auth.TestHttpClientUtils.UNAUTHORIZED_RESPONSE;
Expand All @@ -92,7 +90,7 @@
@Testcontainers(parallel = true)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@DisplayName("Dataset Resource Test")
class DatasetsResourceTest {
class DatasetsResourceTest extends AbstractDatasetsContainerBaseTest {

private static final String BASE_RESOURCE_URI = "%s/v1/private/datasets";
private static final String EXPERIMENT_RESOURCE_URI = "%s/v1/private/experiments";
Expand All @@ -104,87 +102,15 @@ class DatasetsResourceTest {
public static final String[] IGNORED_FIELDS_LIST = {"feedbackScores", "createdAt", "lastUpdatedAt", "createdBy",
"lastUpdatedBy"};
public static final String[] IGNORED_FIELDS_DATA_ITEM = {"createdAt", "lastUpdatedAt", "experimentItems",
"createdBy",
"lastUpdatedBy"};

public static final String API_KEY = UUID.randomUUID().toString();
private static final String USER = UUID.randomUUID().toString();
private static final String WORKSPACE_ID = UUID.randomUUID().toString();
private static final String TEST_WORKSPACE = UUID.randomUUID().toString();
"createdBy", "lastUpdatedBy"};

private static final TimeBasedEpochGenerator GENERATOR = Generators.timeBasedEpochGenerator();

@Container
private static final RedisContainer REDIS = RedisContainerUtils.newRedisContainer();

@Container
private static final MySQLContainer<?> MYSQL = MySQLContainerUtils.newMySQLContainer();

@Container
private static final ClickHouseContainer CLICKHOUSE = ClickHouseContainerUtils.newClickHouseContainer();

@RegisterExtension
private static final TestDropwizardAppExtension app;

private static final WireMockRuntime wireMock;
public static final String[] DATASET_IGNORED_FIELDS = {"id", "createdAt", "lastUpdatedAt", "createdBy",
"lastUpdatedBy", "experimentCount", "mostRecentExperimentAt", "experimentCount"};

static {
MYSQL.start();
CLICKHOUSE.start();
REDIS.start();

wireMock = WireMockUtils.startWireMock();

var databaseAnalyticsFactory = ClickHouseContainerUtils.newDatabaseAnalyticsFactory(
CLICKHOUSE, DATABASE_NAME);

app = TestDropwizardAppExtensionUtils.newTestDropwizardAppExtension(
MYSQL.getJdbcUrl(),
databaseAnalyticsFactory,
wireMock.runtimeInfo(),
REDIS.getRedisURI());
}

private final PodamFactory factory = PodamFactoryUtils.newPodamFactory();

private String baseURI;
private ClientSupport client;

@BeforeAll
void setUpAll(ClientSupport client, Jdbi jdbi) throws Exception {

MigrationUtils.runDbMigration(jdbi, MySQLContainerUtils.migrationParameters());

try (var connection = CLICKHOUSE.createConnection("")) {
MigrationUtils.runDbMigration(connection, CLICKHOUSE_CHANGELOG_FILE,
ClickHouseContainerUtils.migrationParameters());
}

this.baseURI = "http://localhost:%d".formatted(client.getPort());
this.client = client;

ClientSupportUtils.config(client);

mockTargetWorkspace(API_KEY, TEST_WORKSPACE, WORKSPACE_ID);
}

@AfterAll
void tearDownAll() {
wireMock.server().stop();
}

private static void mockTargetWorkspace(String apiKey, String workspaceName, String workspaceId) {
AuthTestUtils.mockTargetWorkspace(wireMock.server(), apiKey, workspaceName, workspaceId, USER);
}

private static void mockSessionCookieTargetWorkspace(String sessionToken, String workspaceName,
String workspaceId) {
AuthTestUtils.mockSessionCookieTargetWorkspace(wireMock.server(), sessionToken, workspaceName, workspaceId,
USER);
}

private UUID createAndAssert(Dataset dataset) {
return createAndAssert(dataset, API_KEY, TEST_WORKSPACE);
}
Expand Down Expand Up @@ -3367,3 +3293,75 @@ private List<DatasetItem> getStreamedItems(Response response) {
}

}

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
abstract class AbstractDatasetsContainerBaseTest {

protected static final String USER = UUID.randomUUID().toString();
protected static final String API_KEY = UUID.randomUUID().toString();
protected static final String WORKSPACE_ID = UUID.randomUUID().toString();
protected static final String TEST_WORKSPACE = UUID.randomUUID().toString();

protected static final RedisContainer REDIS = RedisContainerUtils.newRedisContainer();

protected static final MySQLContainer<?> MYSQL = MySQLContainerUtils.newMySQLContainer();

private static final ClickHouseContainer CLICKHOUSE = ClickHouseContainerUtils.newClickHouseContainer();

@RegisterExtension
protected static final TestDropwizardAppExtension app;

protected static final WireMockUtils.WireMockRuntime wireMock;

static {
MYSQL.start();
REDIS.start();
CLICKHOUSE.start();

var databaseAnalyticsFactory = ClickHouseContainerUtils.newDatabaseAnalyticsFactory(CLICKHOUSE,
ClickHouseContainerUtils.DATABASE_NAME);

wireMock = WireMockUtils.startWireMock();

app = TestDropwizardAppExtensionUtils.newTestDropwizardAppExtension(MYSQL.getJdbcUrl(),
databaseAnalyticsFactory,
wireMock.runtimeInfo(), REDIS.getRedisURI());
}

protected String baseURI;
protected ClientSupport client;

@BeforeAll
protected void setUpAll(ClientSupport client, Jdbi jdbi) throws SQLException {

MigrationUtils.runDbMigration(jdbi, MySQLContainerUtils.migrationParameters());

try (var connection = CLICKHOUSE.createConnection("")) {
MigrationUtils.runDbMigration(connection, CLICKHOUSE_CHANGELOG_FILE,
ClickHouseContainerUtils.migrationParameters());
}

this.baseURI = "http://localhost:%d".formatted(client.getPort());
this.client = client;

ClientSupportUtils.config(client);

mockTargetWorkspace(API_KEY, TEST_WORKSPACE, WORKSPACE_ID);
}

@AfterAll
protected void tearDownAll() {
wireMock.server().stop();
}

protected void mockTargetWorkspace(String apiKey, String workspaceName, String workspaceId) {
AuthTestUtils.mockTargetWorkspace(wireMock.server(), apiKey, workspaceName, workspaceId, USER);
}

protected void mockSessionCookieTargetWorkspace(String sessionToken, String workspaceName,
String workspaceId) {
AuthTestUtils.mockSessionCookieTargetWorkspace(wireMock.server(), sessionToken, workspaceName, workspaceId,
USER);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import org.junit.jupiter.params.provider.MethodSource;
import org.testcontainers.containers.ClickHouseContainer;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import ru.vyarus.dropwizard.guice.test.ClientSupport;
import ru.vyarus.dropwizard.guice.test.jupiter.ext.TestDropwizardAppExtension;
Expand All @@ -64,7 +63,6 @@
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static com.comet.opik.api.resources.utils.ClickHouseContainerUtils.DATABASE_NAME;
import static com.comet.opik.api.resources.utils.MigrationUtils.CLICKHOUSE_CHANGELOG_FILE;
import static com.comet.opik.infrastructure.auth.RequestContext.SESSION_COOKIE;
import static com.comet.opik.infrastructure.auth.RequestContext.WORKSPACE_HEADER;
Expand All @@ -81,92 +79,23 @@

@Testcontainers(parallel = true)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class ExperimentsResourceTest {
class ExperimentsResourceTest extends AbstractExperimentsContainerBaseTest {

private static final String URL_TEMPLATE = "%s/v1/private/experiments";
private static final String ITEMS_PATH = "/items";
private static final String URL_TEMPLATE_TRACES = "%s/v1/private/traces";

private static final String API_KEY = UUID.randomUUID().toString();

private static final String[] EXPERIMENT_IGNORED_FIELDS = new String[]{
"id", "datasetName", "datasetId", "feedbackScores", "traceCount", "createdAt", "lastUpdatedAt", "createdBy",
"lastUpdatedBy"};

public static final String[] IGNORED_FIELDS = {"input", "output", "feedbackScores", "createdAt", "lastUpdatedAt",
"createdBy", "lastUpdatedBy"};

private static final String WORKSPACE_ID = UUID.randomUUID().toString();
private static final String USER = UUID.randomUUID().toString();
private static final String TEST_WORKSPACE = UUID.randomUUID().toString();

private static final TimeBasedEpochGenerator GENERATOR = Generators.timeBasedEpochGenerator();

@Container
private static final RedisContainer REDIS = RedisContainerUtils.newRedisContainer();

@Container
private static final MySQLContainer<?> MY_SQL_CONTAINER = MySQLContainerUtils.newMySQLContainer();

@Container
private static final ClickHouseContainer CLICK_HOUSE_CONTAINER = ClickHouseContainerUtils.newClickHouseContainer();

@RegisterExtension
private static final TestDropwizardAppExtension app;

private static final WireMockUtils.WireMockRuntime wireMock;

static {
MY_SQL_CONTAINER.start();
CLICK_HOUSE_CONTAINER.start();
REDIS.start();

wireMock = WireMockUtils.startWireMock();

var databaseAnalyticsFactory = ClickHouseContainerUtils.newDatabaseAnalyticsFactory(
CLICK_HOUSE_CONTAINER, DATABASE_NAME);

app = TestDropwizardAppExtensionUtils.newTestDropwizardAppExtension(
MY_SQL_CONTAINER.getJdbcUrl(), databaseAnalyticsFactory, wireMock.runtimeInfo(), REDIS.getRedisURI());
}

private final PodamFactory podamFactory = PodamFactoryUtils.newPodamFactory();

private String baseURI;
private ClientSupport client;

@BeforeAll
void beforeAll(ClientSupport client, Jdbi jdbi) throws SQLException {
MigrationUtils.runDbMigration(jdbi, MySQLContainerUtils.migrationParameters());

try (var connection = CLICK_HOUSE_CONTAINER.createConnection("")) {
MigrationUtils.runDbMigration(connection, CLICKHOUSE_CHANGELOG_FILE,
ClickHouseContainerUtils.migrationParameters());
}

baseURI = "http://localhost:%d".formatted(client.getPort());
this.client = client;

ClientSupportUtils.config(client);

mockTargetWorkspace(API_KEY, TEST_WORKSPACE, WORKSPACE_ID);
}

private static void mockTargetWorkspace(String apiKey, String workspaceName, String workspaceId) {
AuthTestUtils.mockTargetWorkspace(wireMock.server(), apiKey, workspaceName, workspaceId, USER);
}

private static void mockSessionCookieTargetWorkspace(String sessionToken, String workspaceName,
String workspaceId) {
AuthTestUtils.mockSessionCookieTargetWorkspace(wireMock.server(), sessionToken, workspaceName, workspaceId,
USER);
}

@AfterAll
void tearDownAll() {
wireMock.server().stop();
}

@Nested
@DisplayName("Api Key Authentication:")
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
Expand Down Expand Up @@ -1883,3 +1812,75 @@ private String getExperimentItemsPath() {
return URL_TEMPLATE.formatted(baseURI) + ITEMS_PATH;
}
}

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
abstract class AbstractExperimentsContainerBaseTest {

protected static final String USER = UUID.randomUUID().toString();
protected static final String API_KEY = UUID.randomUUID().toString();
protected static final String WORKSPACE_ID = UUID.randomUUID().toString();
protected static final String TEST_WORKSPACE = UUID.randomUUID().toString();

protected static final RedisContainer REDIS = RedisContainerUtils.newRedisContainer();

protected static final MySQLContainer<?> MYSQL = MySQLContainerUtils.newMySQLContainer();

private static final ClickHouseContainer CLICKHOUSE = ClickHouseContainerUtils.newClickHouseContainer();

@RegisterExtension
protected static final TestDropwizardAppExtension app;

protected static final WireMockUtils.WireMockRuntime wireMock;

static {
MYSQL.start();
REDIS.start();
CLICKHOUSE.start();

var databaseAnalyticsFactory = ClickHouseContainerUtils.newDatabaseAnalyticsFactory(CLICKHOUSE,
ClickHouseContainerUtils.DATABASE_NAME);

wireMock = WireMockUtils.startWireMock();

app = TestDropwizardAppExtensionUtils.newTestDropwizardAppExtension(MYSQL.getJdbcUrl(),
databaseAnalyticsFactory,
wireMock.runtimeInfo(), REDIS.getRedisURI());
}

protected String baseURI;
protected ClientSupport client;

@BeforeAll
protected void setUpAll(ClientSupport client, Jdbi jdbi) throws SQLException {

MigrationUtils.runDbMigration(jdbi, MySQLContainerUtils.migrationParameters());

try (var connection = CLICKHOUSE.createConnection("")) {
MigrationUtils.runDbMigration(connection, CLICKHOUSE_CHANGELOG_FILE,
ClickHouseContainerUtils.migrationParameters());
}

this.baseURI = "http://localhost:%d".formatted(client.getPort());
this.client = client;

ClientSupportUtils.config(client);

mockTargetWorkspace(API_KEY, TEST_WORKSPACE, WORKSPACE_ID);
}

@AfterAll
protected void tearDownAll() {
wireMock.server().stop();
}

protected void mockTargetWorkspace(String apiKey, String workspaceName, String workspaceId) {
AuthTestUtils.mockTargetWorkspace(wireMock.server(), apiKey, workspaceName, workspaceId, USER);
}

protected void mockSessionCookieTargetWorkspace(String sessionToken, String workspaceName,
String workspaceId) {
AuthTestUtils.mockSessionCookieTargetWorkspace(wireMock.server(), sessionToken, workspaceName, workspaceId,
USER);
}

}
Loading

0 comments on commit 027f37a

Please sign in to comment.