|
| 1 | +package io.weaviate.integration.client; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import org.junit.rules.TestRule; |
| 7 | +import org.junit.runner.Description; |
| 8 | +import org.junit.runners.model.Statement; |
| 9 | +import org.testcontainers.containers.GenericContainer; |
| 10 | +import org.testcontainers.containers.MinIOContainer; |
| 11 | +import org.testcontainers.containers.Network; |
| 12 | +import org.testcontainers.containers.wait.strategy.Wait; |
| 13 | +import org.testcontainers.weaviate.WeaviateContainer; |
| 14 | + |
| 15 | +public class WeaviateDockerComposeBackup implements TestRule { |
| 16 | + |
| 17 | + public static final String ADMIN_KEY = "admin-key"; |
| 18 | + |
| 19 | + /** Weaviate Docker image to create a container from. */ |
| 20 | + private final String weaviateVersion; |
| 21 | + private final boolean withOffloadS3; |
| 22 | + |
| 23 | + public WeaviateDockerComposeBackup() { |
| 24 | + this.weaviateVersion = WeaviateDockerImage.WEAVIATE_DOCKER_IMAGE; |
| 25 | + this.withOffloadS3 = false; |
| 26 | + } |
| 27 | + |
| 28 | + public WeaviateDockerComposeBackup(String version) { |
| 29 | + this.weaviateVersion = String.format("semitechnologies/weaviate:%s", version); |
| 30 | + this.withOffloadS3 = false; |
| 31 | + } |
| 32 | + |
| 33 | + public WeaviateDockerComposeBackup(String version, boolean withOffloadS3) { |
| 34 | + this.weaviateVersion = String.format("semitechnologies/weaviate:%s", version); |
| 35 | + this.withOffloadS3 = withOffloadS3; |
| 36 | + } |
| 37 | + |
| 38 | + public static class Weaviate extends WeaviateContainer { |
| 39 | + public Weaviate(String dockerImageName, boolean withOffloadS3) { |
| 40 | + super(dockerImageName); |
| 41 | + waitingFor(Wait.forHttp("/v1/.well-known/ready").forPort(8080).forStatusCode(200)); |
| 42 | + withNetwork(Network.SHARED); |
| 43 | + List<String> enableModules = new ArrayList<>(); |
| 44 | + enableModules.add("text2vec-contextionary"); |
| 45 | + enableModules.add("backup-filesystem"); |
| 46 | + enableModules.add("generative-openai"); |
| 47 | + if (withOffloadS3) { |
| 48 | + enableModules.add("offload-s3"); |
| 49 | + withEnv("OFFLOAD_S3_ENDPOINT", "http://minio:9000"); |
| 50 | + withEnv("OFFLOAD_S3_BUCKET_AUTO_CREATE", "true"); |
| 51 | + withEnv("AWS_ACCESS_KEY_ID", MinIO.USER); |
| 52 | + withEnv("AWS_SECRET_KEY", MinIO.PASSWORD); |
| 53 | + } |
| 54 | + withEnv("LOG_LEVEL", "debug"); |
| 55 | + withEnv("CONTEXTIONARY_URL", "contextionary:9999"); |
| 56 | + withEnv("QUERY_DEFAULTS_LIMIT", "25"); |
| 57 | + withEnv("DEFAULT_VECTORIZER_MODULE", "text2vec-contextionary"); |
| 58 | + withEnv("BACKUP_FILESYSTEM_PATH", "/tmp/backups"); |
| 59 | + withEnv("DISABLE_TELEMETRY", "true"); |
| 60 | + withEnv("PERSISTENCE_FLUSH_IDLE_MEMTABLES_AFTER", "1"); |
| 61 | + withEnv("ENABLE_MODULES", String.join(",", enableModules)); |
| 62 | + withCreateContainerCmdModifier(cmd -> cmd.withHostName("weaviate")); |
| 63 | + |
| 64 | + withEnv("AUTHENTICATION_ANONYMOUS_ACCESS_ENABLED", "false"); |
| 65 | + withEnv("AUTHENTICATION_APIKEY_ENABLED", "true"); |
| 66 | + withEnv("AUTHENTICATION_APIKEY_ALLOWED_KEYS", ADMIN_KEY + ",custom-key"); |
| 67 | + withEnv("AUTHENTICATION_APIKEY_USERS", "admin-user,custom-user"); |
| 68 | + withEnv("AUTHORIZATION_ADMIN_USERS", "admin-user"); |
| 69 | + withEnv("PERSISTENCE_DATA_PATH", "./data-weaviate-0"); |
| 70 | + withEnv("CLUSTER_IN_LOCALHOST", "true"); |
| 71 | + withEnv("CLUSTER_GOSSIP_BIND_PORT", "7100"); |
| 72 | + withEnv("CLUSTER_DATA_BIND_PORT", "7101"); |
| 73 | + withEnv("RAFT_BOOTSTRAP_EXPECT", "1"); |
| 74 | + withEnv("AUTHORIZATION_ENABLE_RBAC", "true"); |
| 75 | + withEnv("AUTHENTICATION_DB_USERS_ENABLED", "true"); |
| 76 | + withEnv("AUTHENTICATION_OIDC_ENABLED", "true"); |
| 77 | + withEnv("AUTHENTICATION_OIDC_CLIENT_ID", "wcs"); |
| 78 | + withEnv("AUTHENTICATION_OIDC_ISSUER", "https://auth.wcs.api.weaviate.io/auth/realms/SeMI"); |
| 79 | + withEnv("AUTHENTICATION_OIDC_USERNAME_CLAIM", "email"); |
| 80 | + withEnv("AUTHENTICATION_OIDC_GROUPS_CLAIM", "groups"); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + public static class Contextionary extends GenericContainer<Contextionary> { |
| 85 | + public Contextionary() { |
| 86 | + super("semitechnologies/contextionary:en0.16.0-v1.2.1"); |
| 87 | + withNetwork(Network.SHARED); |
| 88 | + withEnv("OCCURRENCE_WEIGHT_LINEAR_FACTOR", "true"); |
| 89 | + withEnv("PERSISTENCE_DATA_PATH", "/var/lib/weaviate"); |
| 90 | + withEnv("OCCURRENCE_WEIGHT_LINEAR_FACTOR", "0.75"); |
| 91 | + withEnv("EXTENSIONS_STORAGE_MODE", "weaviate"); |
| 92 | + withEnv("EXTENSIONS_STORAGE_ORIGIN", "http://weaviate:8080"); |
| 93 | + withEnv("NEIGHBOR_OCCURRENCE_IGNORE_PERCENTILE", "5"); |
| 94 | + withEnv("ENABLE_COMPOUND_SPLITTING", "'false'"); |
| 95 | + withCreateContainerCmdModifier(cmd -> cmd.withHostName("contextionary")); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + public static class MinIO extends MinIOContainer { |
| 100 | + private static final String USER = "minioadmin"; |
| 101 | + private static final String PASSWORD = "minioadmin"; |
| 102 | + |
| 103 | + public MinIO() { |
| 104 | + super("minio/minio"); |
| 105 | + withNetwork(Network.SHARED); |
| 106 | + withUserName(USER); |
| 107 | + withPassword(PASSWORD); |
| 108 | + withCreateContainerCmdModifier(cmd -> cmd.withHostName("minio")); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + private static Contextionary contextionary; |
| 113 | + private static Weaviate weaviate; |
| 114 | + private static MinIO minio; |
| 115 | + |
| 116 | + public void start() { |
| 117 | + if (withOffloadS3) { |
| 118 | + minio = new MinIO(); |
| 119 | + minio.start(); |
| 120 | + } |
| 121 | + contextionary = new Contextionary(); |
| 122 | + contextionary.start(); |
| 123 | + weaviate = new Weaviate(this.weaviateVersion, this.withOffloadS3); |
| 124 | + weaviate.start(); |
| 125 | + } |
| 126 | + |
| 127 | + public String getHttpHostAddress() { |
| 128 | + return weaviate.getHttpHostAddress(); |
| 129 | + } |
| 130 | + |
| 131 | + public String getGrpcHostAddress() { |
| 132 | + return weaviate.getGrpcHostAddress(); |
| 133 | + } |
| 134 | + |
| 135 | + public void stop() { |
| 136 | + weaviate.stop(); |
| 137 | + contextionary.stop(); |
| 138 | + if (withOffloadS3) { |
| 139 | + minio.stop(); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public Statement apply(Statement base, Description description) { |
| 145 | + return new Statement() { |
| 146 | + @Override |
| 147 | + public void evaluate() throws Throwable { |
| 148 | + try { |
| 149 | + start(); |
| 150 | + base.evaluate(); |
| 151 | + } finally { |
| 152 | + stop(); |
| 153 | + } |
| 154 | + } |
| 155 | + }; |
| 156 | + } |
| 157 | +} |
0 commit comments