|
| 1 | +package io.weaviate.integration.client.cluster; |
| 2 | + |
| 3 | +import io.weaviate.client.Config; |
| 4 | +import io.weaviate.client.WeaviateClient; |
| 5 | +import io.weaviate.client.base.Result; |
| 6 | +import io.weaviate.client.base.util.TriConsumer; |
| 7 | +import io.weaviate.client.v1.cluster.model.NodesStatusResponse; |
| 8 | +import io.weaviate.integration.client.WeaviateTestGenerics; |
| 9 | +import org.junit.After; |
| 10 | +import org.junit.Before; |
| 11 | +import org.junit.ClassRule; |
| 12 | +import org.junit.Test; |
| 13 | +import org.testcontainers.containers.DockerComposeContainer; |
| 14 | +import org.testcontainers.containers.wait.strategy.Wait; |
| 15 | + |
| 16 | +import java.io.File; |
| 17 | +import java.util.List; |
| 18 | +import java.util.function.Consumer; |
| 19 | + |
| 20 | +import static io.weaviate.integration.client.WeaviateVersion.EXPECTED_WEAVIATE_GIT_HASH; |
| 21 | +import static io.weaviate.integration.client.WeaviateVersion.EXPECTED_WEAVIATE_VERSION; |
| 22 | +import static org.assertj.core.api.Assertions.assertThat; |
| 23 | +import static org.assertj.core.api.InstanceOfAssertFactories.ARRAY; |
| 24 | + |
| 25 | +public class ClientClusterMultiTenancyTest { |
| 26 | + |
| 27 | + private WeaviateClient client; |
| 28 | + private final WeaviateTestGenerics testGenerics = new WeaviateTestGenerics(); |
| 29 | + |
| 30 | + @ClassRule |
| 31 | + public static DockerComposeContainer<?> compose = new DockerComposeContainer<>( |
| 32 | + new File("src/test/resources/docker-compose-test.yaml") |
| 33 | + ).withExposedService("weaviate_1", 8080, Wait.forHttp("/v1/.well-known/ready").forStatusCode(200)); |
| 34 | + |
| 35 | + @Before |
| 36 | + public void before() { |
| 37 | + String host = compose.getServiceHost("weaviate_1", 8080); |
| 38 | + Integer port = compose.getServicePort("weaviate_1", 8080); |
| 39 | + Config config = new Config("http", host + ":" + port); |
| 40 | + |
| 41 | + client = new WeaviateClient(config); |
| 42 | + } |
| 43 | + |
| 44 | + @After |
| 45 | + public void after() { |
| 46 | + testGenerics.cleanupWeaviate(client); |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | + @Test |
| 51 | + public void shouldGetNodeStatusPerClass() { |
| 52 | + String[] pizzaTenants = new String[]{"TenantPizza1", "TenantPizza2"}; |
| 53 | + String[] soupTenants = new String[]{"TenantSoup1", "TenantSoup2", "TenantSoup3"}; |
| 54 | + List<String> pizzaIds = WeaviateTestGenerics.IDS_BY_CLASS.get("Pizza"); |
| 55 | + List<String> soupIds = WeaviateTestGenerics.IDS_BY_CLASS.get("Soup"); |
| 56 | + testGenerics.createSchemaPizzaForTenants(client); |
| 57 | + testGenerics.createTenantsPizza(client, pizzaTenants); |
| 58 | + testGenerics.createDataPizzaForTenants(client, pizzaTenants); |
| 59 | + testGenerics.createSchemaSoupForTenants(client); |
| 60 | + testGenerics.createTenantsSoup(client, soupTenants); |
| 61 | + testGenerics.createDataSoupForTenants(client, soupTenants); |
| 62 | + |
| 63 | + Consumer<Result<NodesStatusResponse>> assertSingleNode = (Result<NodesStatusResponse> result) -> |
| 64 | + assertThat(result).isNotNull() |
| 65 | + .returns(false, Result::hasErrors) |
| 66 | + .extracting(Result::getResult).isNotNull() |
| 67 | + .extracting(NodesStatusResponse::getNodes).asInstanceOf(ARRAY) |
| 68 | + .hasSize(1); |
| 69 | + |
| 70 | + TriConsumer<NodesStatusResponse.NodeStatus, Long, Long> assertCounts = (NodesStatusResponse.NodeStatus nodeStatus, Long shardCount, Long objectCount) -> { |
| 71 | + assertThat(nodeStatus.getName()).isNotBlank(); |
| 72 | + assertThat(nodeStatus) |
| 73 | + .returns(EXPECTED_WEAVIATE_VERSION, NodesStatusResponse.NodeStatus::getVersion) |
| 74 | + .returns(EXPECTED_WEAVIATE_GIT_HASH, NodesStatusResponse.NodeStatus::getGitHash) |
| 75 | + .returns(NodesStatusResponse.Status.HEALTHY, NodesStatusResponse.NodeStatus::getStatus) |
| 76 | + .extracting(NodesStatusResponse.NodeStatus::getStats) |
| 77 | + .returns(shardCount, NodesStatusResponse.Stats::getShardCount) |
| 78 | + .returns(objectCount, NodesStatusResponse.Stats::getObjectCount); |
| 79 | + }; |
| 80 | + |
| 81 | + // ALL |
| 82 | + Result<NodesStatusResponse> resultAll = client.cluster() |
| 83 | + .nodesStatusGetter() |
| 84 | + .run(); |
| 85 | + |
| 86 | + long expectedAllShardCount = pizzaTenants.length + soupTenants.length; |
| 87 | + long expectedAllObjectsCount = pizzaTenants.length * pizzaIds.size() + soupTenants.length * soupIds.size(); |
| 88 | + assertSingleNode.accept(resultAll); |
| 89 | + assertCounts.accept(resultAll.getResult().getNodes()[0], expectedAllShardCount, expectedAllObjectsCount); |
| 90 | + |
| 91 | + // PIZZA |
| 92 | + Result<NodesStatusResponse> resultPizza = client.cluster() |
| 93 | + .nodesStatusGetter() |
| 94 | + .withClassName("Pizza") |
| 95 | + .run(); |
| 96 | + |
| 97 | + long expectedPizzaShardCount = pizzaTenants.length; |
| 98 | + long expectedPizzaObjectsCount = pizzaTenants.length * pizzaIds.size(); |
| 99 | + assertSingleNode.accept(resultPizza); |
| 100 | + assertCounts.accept(resultPizza.getResult().getNodes()[0], expectedPizzaShardCount, expectedPizzaObjectsCount); |
| 101 | + |
| 102 | + // SOUP |
| 103 | + Result<NodesStatusResponse> resultSoup = client.cluster() |
| 104 | + .nodesStatusGetter() |
| 105 | + .withClassName("Soup") |
| 106 | + .run(); |
| 107 | + |
| 108 | + long expectedSoupShardCount = soupTenants.length; |
| 109 | + long expectedSoupObjectsCount = soupTenants.length * soupIds.size(); |
| 110 | + assertSingleNode.accept(resultSoup); |
| 111 | + assertCounts.accept(resultSoup.getResult().getNodes()[0], expectedSoupShardCount, expectedSoupObjectsCount); |
| 112 | + } |
| 113 | +} |
0 commit comments