Cleanup tests using testcontainers
* Specify tag images * Use getHost() instead of hardcoded `localhost`
This commit is contained in:
@@ -16,7 +16,7 @@
|
||||
|
||||
package org.springframework.ai.autoconfigure.ollama;
|
||||
|
||||
import java.util.Map;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
@@ -30,8 +30,6 @@ import org.testcontainers.junit.jupiter.Testcontainers;
|
||||
import org.springframework.ai.ollama.client.OllamaClient;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@@ -42,18 +40,17 @@ public class OllamaAutoConfigurationIT {
|
||||
private static final Log logger = LogFactory.getLog(OllamaAutoConfigurationIT.class);
|
||||
|
||||
@Container
|
||||
static GenericContainer<?> ollamaContainer = new GenericContainer<>("ollama/ollama").withExposedPorts(11434);
|
||||
static GenericContainer<?> ollamaContainer = new GenericContainer<>("ollama/ollama:0.1.10").withExposedPorts(11434);
|
||||
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withPropertyValues("spring.ai.ollama.baseUrl=http://localhost:" + ollamaContainer.getMappedPort(11434),
|
||||
"spring.ai.ollama.model=orca-mini")
|
||||
.withPropertyValues("spring.ai.ollama.baseUrl=http://" + ollamaContainer.getHost() + ":"
|
||||
+ ollamaContainer.getMappedPort(11434), "spring.ai.ollama.model=orca-mini")
|
||||
.withConfiguration(AutoConfigurations.of(OllamaAutoConfiguration.class));
|
||||
|
||||
@BeforeAll
|
||||
public static void beforeAll() {
|
||||
public static void beforeAll() throws IOException, InterruptedException {
|
||||
logger.info("Start pulling the 'orca-mini' model (3GB) ... would take several minutes ...");
|
||||
new RestTemplate().postForLocation("http://localhost:{port}/api/pull",
|
||||
new HttpEntity<>(Map.of("name", "orca-mini")), ollamaContainer.getMappedPort(11434));
|
||||
ollamaContainer.execInContainer("ollama", "pull", "orca-mini");
|
||||
logger.info("orca-mini pulling competed!");
|
||||
}
|
||||
|
||||
@@ -61,7 +58,8 @@ public class OllamaAutoConfigurationIT {
|
||||
void generate() {
|
||||
contextRunner.run(context -> {
|
||||
OllamaClient client = context.getBean(OllamaClient.class);
|
||||
assertThat(client.getBaseUrl()).isEqualTo("http://localhost:" + ollamaContainer.getMappedPort(11434));
|
||||
assertThat(client.getBaseUrl())
|
||||
.isEqualTo("http://" + ollamaContainer.getHost() + ":" + ollamaContainer.getMappedPort(11434));
|
||||
assertThat(client.getModel()).isEqualTo("orca-mini");
|
||||
|
||||
String response = client.generate("Hello");
|
||||
|
||||
@@ -49,7 +49,7 @@ public class ChromaVectorStoreAutoConfigurationIT {
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(ChromaVectorStoreAutoConfiguration.class))
|
||||
.withUserConfiguration(Config.class)
|
||||
.withPropertyValues("spring.ai.vectorstore.chroma.client.host=http://localhost",
|
||||
.withPropertyValues("spring.ai.vectorstore.chroma.client.host=http://" + chromaContainer.getHost(),
|
||||
"spring.ai.vectorstore.chroma.client.port=" + chromaContainer.getMappedPort(8000),
|
||||
"spring.ai.vectorstore.chroma.store.collectionName=TestCollection");
|
||||
|
||||
|
||||
@@ -48,7 +48,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
public class PgVectorStoreAutoConfigurationIT {
|
||||
|
||||
@Container
|
||||
static GenericContainer<?> postgresContainer = new GenericContainer<>("ankane/pgvector")
|
||||
static GenericContainer<?> postgresContainer = new GenericContainer<>("ankane/pgvector:v0.5.1")
|
||||
.withEnv("POSTGRES_USER", "postgres")
|
||||
.withEnv("POSTGRES_PASSWORD", "postgres")
|
||||
.withExposedPorts(5432);
|
||||
@@ -74,7 +74,7 @@ public class PgVectorStoreAutoConfigurationIT {
|
||||
.withUserConfiguration(Config.class)
|
||||
.withPropertyValues("spring.ai.vectorstore.pgvector.distanceType=CosineDistance",
|
||||
// JdbcTemplate configuration
|
||||
String.format("spring.datasource.url=jdbc:postgresql://localhost:%d/%s",
|
||||
String.format("spring.datasource.url=jdbc:postgresql://%s:%d/%s", postgresContainer.getHost(),
|
||||
postgresContainer.getMappedPort(5432), "postgres"),
|
||||
"spring.datasource.username=postgres", "spring.datasource.password=postgres");
|
||||
|
||||
|
||||
@@ -176,9 +176,10 @@ public class ChromaApiIT {
|
||||
|
||||
@Bean
|
||||
public ChromaApi chromaApi(RestTemplate restTemplate) {
|
||||
|
||||
String host = chromaContainer.getHost();
|
||||
int port = chromaContainer.getMappedPort(8000);
|
||||
return new ChromaApi("http://localhost:" + port, restTemplate);
|
||||
String baseUrl = "http://%s:%d".formatted(host, port);
|
||||
return new ChromaApi(baseUrl, restTemplate);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -58,8 +58,6 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
@Testcontainers
|
||||
public class BasicAuthChromaWhereIT {
|
||||
|
||||
public static String CHROMA_SERVER_URL = "http://localhost:";
|
||||
|
||||
/**
|
||||
* ChromaDB with Basic Authentication:
|
||||
* https://docs.trychroma.com/usage-guide#basic-authentication
|
||||
@@ -113,8 +111,10 @@ public class BasicAuthChromaWhereIT {
|
||||
|
||||
@Bean
|
||||
public ChromaApi chromaApi(RestTemplate restTemplate) {
|
||||
String host = chromaContainer.getHost();
|
||||
int port = chromaContainer.getMappedPort(8000);
|
||||
return new ChromaApi(CHROMA_SERVER_URL + port, restTemplate).withBasicAuthCredentials("admin", "admin");
|
||||
String baseUrl = "http://%s:%d".formatted(host, port);
|
||||
return new ChromaApi(baseUrl, restTemplate).withBasicAuthCredentials("admin", "admin");
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -212,8 +212,10 @@ public class ChromaVectorStoreIT {
|
||||
|
||||
@Bean
|
||||
public ChromaApi chromaApi(RestTemplate restTemplate) {
|
||||
String host = chromaContainer.getHost();
|
||||
int port = chromaContainer.getMappedPort(8000);
|
||||
return new ChromaApi("http://localhost:" + port, restTemplate);
|
||||
String baseUrl = "http://%s:%d".formatted(host, port);
|
||||
return new ChromaApi(baseUrl, restTemplate);
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -57,8 +57,6 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
@Testcontainers
|
||||
public class TokenSecuredChromaWhereIT {
|
||||
|
||||
public static String CHROMA_SERVER_URL = "http://localhost:";
|
||||
|
||||
public static String CHROMA_SERVER_AUTH_CREDENTIALS = "test-token";
|
||||
|
||||
/**
|
||||
@@ -144,8 +142,10 @@ public class TokenSecuredChromaWhereIT {
|
||||
|
||||
@Bean
|
||||
public ChromaApi chromaApi(RestTemplate restTemplate) {
|
||||
String host = chromaContainer.getHost();
|
||||
int port = chromaContainer.getMappedPort(8000);
|
||||
var chromaApi = new ChromaApi(CHROMA_SERVER_URL + port, restTemplate);
|
||||
String baseurl = "http://%s:%d".formatted(host, port);
|
||||
var chromaApi = new ChromaApi(baseurl, restTemplate);
|
||||
chromaApi.withKeyToken(CHROMA_SERVER_AUTH_CREDENTIALS);
|
||||
return chromaApi;
|
||||
}
|
||||
|
||||
@@ -290,7 +290,7 @@ public class MilvusVectorStoreIT {
|
||||
@Bean
|
||||
public MilvusServiceClient milvusClient() {
|
||||
return new MilvusServiceClient(ConnectParam.newBuilder()
|
||||
.withHost("localhost")
|
||||
.withHost(milvusContainer.getServiceHost("standalone", 19530))
|
||||
.withPort(milvusContainer.getServicePort("standalone", 19530))
|
||||
.build());
|
||||
}
|
||||
|
||||
@@ -70,7 +70,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
public class PgVectorStoreIT {
|
||||
|
||||
@Container
|
||||
static GenericContainer<?> postgresContainer = new GenericContainer<>("ankane/pgvector")
|
||||
static GenericContainer<?> postgresContainer = new GenericContainer<>("ankane/pgvector:v0.5.1")
|
||||
.withEnv("POSTGRES_USER", "postgres")
|
||||
.withEnv("POSTGRES_PASSWORD", "postgres")
|
||||
.withExposedPorts(5432);
|
||||
@@ -95,7 +95,7 @@ public class PgVectorStoreIT {
|
||||
.withPropertyValues("test.spring.ai.vectorstore.pgvector.distanceType=CosineDistance",
|
||||
|
||||
// JdbcTemplate configuration
|
||||
String.format("app.datasource.url=jdbc:postgresql://localhost:%d/%s",
|
||||
String.format("app.datasource.url=jdbc:postgresql://%s:%d/%s", postgresContainer.getHost(),
|
||||
postgresContainer.getMappedPort(5432), "postgres"),
|
||||
"app.datasource.username=postgres", "app.datasource.password=postgres",
|
||||
"app.datasource.type=com.zaxxer.hikari.HikariDataSource");
|
||||
|
||||
Reference in New Issue
Block a user