|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package io.trino.server; |
| 15 | + |
| 16 | +import com.google.common.collect.ImmutableMap; |
| 17 | +import com.google.common.collect.ImmutableSet; |
| 18 | +import io.airlift.http.client.HttpClient; |
| 19 | +import io.airlift.http.client.Request; |
| 20 | +import io.airlift.http.client.jetty.JettyHttpClient; |
| 21 | +import io.trino.client.QueryResults; |
| 22 | +import io.trino.connector.MockConnectorFactory; |
| 23 | +import io.trino.connector.MockConnectorPlugin; |
| 24 | +import io.trino.server.testing.TestingTrinoServer; |
| 25 | +import org.junit.jupiter.api.AfterAll; |
| 26 | +import org.junit.jupiter.api.BeforeAll; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | +import org.junit.jupiter.api.TestInstance; |
| 29 | +import org.junit.jupiter.api.parallel.Execution; |
| 30 | + |
| 31 | +import java.net.URI; |
| 32 | +import java.util.List; |
| 33 | +import java.util.Optional; |
| 34 | + |
| 35 | +import static com.google.common.base.Preconditions.checkState; |
| 36 | +import static io.airlift.http.client.HttpUriBuilder.uriBuilderFrom; |
| 37 | +import static io.airlift.http.client.JsonResponseHandler.createJsonResponseHandler; |
| 38 | +import static io.airlift.http.client.Request.Builder.prepareGet; |
| 39 | +import static io.airlift.http.client.Request.Builder.preparePost; |
| 40 | +import static io.airlift.http.client.StaticBodyGenerator.createStaticBodyGenerator; |
| 41 | +import static io.airlift.json.JsonCodec.jsonCodec; |
| 42 | +import static io.airlift.testing.Closeables.closeAll; |
| 43 | +import static io.trino.client.ProtocolHeaders.TRINO_HEADERS; |
| 44 | +import static io.trino.testing.TestingNames.randomNameSuffix; |
| 45 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 46 | +import static org.assertj.core.api.Assertions.assertThat; |
| 47 | +import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS; |
| 48 | +import static org.junit.jupiter.api.parallel.ExecutionMode.CONCURRENT; |
| 49 | + |
| 50 | +@TestInstance(PER_CLASS) |
| 51 | +@Execution(CONCURRENT) |
| 52 | +final class TestResourceGroupStateInfoResource |
| 53 | +{ |
| 54 | + private TestingTrinoServer server; |
| 55 | + private HttpClient client; |
| 56 | + |
| 57 | + @BeforeAll |
| 58 | + public void setup() |
| 59 | + { |
| 60 | + client = new JettyHttpClient(); |
| 61 | + server = TestingTrinoServer.builder() |
| 62 | + .setProperties(ImmutableMap.<String, String>builder() |
| 63 | + .put("web-ui.authentication.type", "fixed") |
| 64 | + .put("web-ui.user", "test-user") |
| 65 | + .buildOrThrow()) |
| 66 | + .build(); |
| 67 | + server.installPlugin(new MockConnectorPlugin(MockConnectorFactory.builder() |
| 68 | + .withRedactablePropertyNames(ImmutableSet.of("password")) |
| 69 | + .build())); |
| 70 | + } |
| 71 | + |
| 72 | + @AfterAll |
| 73 | + public void teardown() |
| 74 | + throws Exception |
| 75 | + { |
| 76 | + closeAll(server, client); |
| 77 | + server = null; |
| 78 | + client = null; |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void testGetResourceGroupInfoWithRedactedSecrets() |
| 83 | + { |
| 84 | + String catalog = "catalog_" + randomNameSuffix(); |
| 85 | + startQuery(""" |
| 86 | + CREATE CATALOG %s USING mock |
| 87 | + WITH ( |
| 88 | + "user" = 'bob', |
| 89 | + "password" = '1234' |
| 90 | + )""".formatted(catalog)); |
| 91 | + |
| 92 | + ResourceGroupInfo resourceGroupInfo = getResourceGroupInfo("global"); |
| 93 | + Optional<List<QueryStateInfo>> queryStateInfos = resourceGroupInfo.runningQueries(); |
| 94 | + assertThat(queryStateInfos.isPresent()).isTrue(); |
| 95 | + List<QueryStateInfo> queryStates = queryStateInfos.get(); |
| 96 | + assertThat(queryStates.size()).isEqualTo(1); |
| 97 | + assertThat(queryStates.getFirst().getQuery()).isEqualTo(""" |
| 98 | + CREATE CATALOG %s USING mock |
| 99 | + WITH ( |
| 100 | + "user" = 'bob', |
| 101 | + "password" = '***' |
| 102 | + )""".formatted(catalog)); |
| 103 | + } |
| 104 | + |
| 105 | + private void startQuery(String sql) |
| 106 | + { |
| 107 | + Request request = preparePost() |
| 108 | + .setUri(server.resolve("/v1/statement")) |
| 109 | + .setBodyGenerator(createStaticBodyGenerator(sql, UTF_8)) |
| 110 | + .setHeader(TRINO_HEADERS.requestUser(), "unknown") |
| 111 | + .build(); |
| 112 | + QueryResults queryResults = client.execute(request, createJsonResponseHandler(jsonCodec(QueryResults.class))); |
| 113 | + checkState(queryResults.getNextUri() != null && queryResults.getNextUri().toString().contains("/v1/statement/queued/"), "nextUri should point to /v1/statement/queued/"); |
| 114 | + request = prepareGet() |
| 115 | + .setHeader(TRINO_HEADERS.requestUser(), "unknown") |
| 116 | + .setUri(queryResults.getNextUri()) |
| 117 | + .build(); |
| 118 | + client.execute(request, createJsonResponseHandler(jsonCodec(QueryResults.class))); |
| 119 | + } |
| 120 | + |
| 121 | + private ResourceGroupInfo getResourceGroupInfo(String resourceGroupId) |
| 122 | + { |
| 123 | + URI uri = uriBuilderFrom(server.getBaseUrl()) |
| 124 | + .replacePath("/v1/resourceGroupState") |
| 125 | + .appendPath(resourceGroupId) |
| 126 | + .build(); |
| 127 | + Request request = prepareGet() |
| 128 | + .setUri(uri) |
| 129 | + .setHeader(TRINO_HEADERS.requestUser(), "unknown") |
| 130 | + .build(); |
| 131 | + return client.execute(request, createJsonResponseHandler(jsonCodec(ResourceGroupInfo.class))); |
| 132 | + } |
| 133 | +} |
0 commit comments