|
| 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.gateway.ha.router; |
| 15 | + |
| 16 | +import com.fasterxml.jackson.databind.JsonNode; |
| 17 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 18 | +import io.airlift.json.JsonCodec; |
| 19 | +import io.trino.gateway.ha.HaGatewayLauncher; |
| 20 | +import io.trino.gateway.ha.HaGatewayTestUtils; |
| 21 | +import io.trino.gateway.ha.config.UIConfiguration; |
| 22 | +import io.trino.gateway.ha.domain.RoutingRule; |
| 23 | +import okhttp3.MediaType; |
| 24 | +import okhttp3.OkHttpClient; |
| 25 | +import okhttp3.Request; |
| 26 | +import okhttp3.RequestBody; |
| 27 | +import okhttp3.Response; |
| 28 | +import org.junit.jupiter.api.BeforeAll; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | +import org.junit.jupiter.api.TestInstance; |
| 31 | +import org.testcontainers.containers.TrinoContainer; |
| 32 | + |
| 33 | +import java.util.List; |
| 34 | + |
| 35 | +import static org.assertj.core.api.Assertions.assertThat; |
| 36 | +import static org.testcontainers.utility.MountableFile.forClasspathResource; |
| 37 | + |
| 38 | +@TestInstance(TestInstance.Lifecycle.PER_CLASS) |
| 39 | +final class TestRoutingAPI |
| 40 | +{ |
| 41 | + private final OkHttpClient httpClient = new OkHttpClient(); |
| 42 | + private TrinoContainer trino; |
| 43 | + int routerPort = 21001 + (int) (Math.random() * 1000); |
| 44 | + int backendPort; |
| 45 | + |
| 46 | + @BeforeAll |
| 47 | + void setup() |
| 48 | + throws Exception |
| 49 | + { |
| 50 | + trino = new TrinoContainer("trinodb/trino"); |
| 51 | + trino.withCopyFileToContainer(forClasspathResource("trino-config.properties"), "/etc/trino/config.properties"); |
| 52 | + trino.start(); |
| 53 | + |
| 54 | + backendPort = trino.getMappedPort(8080); |
| 55 | + |
| 56 | + // seed database |
| 57 | + HaGatewayTestUtils.TestConfig testConfig = |
| 58 | + HaGatewayTestUtils.buildGatewayConfigAndSeedDb(routerPort, "test-config-with-routing-rules-api.yml"); |
| 59 | + // Start Gateway |
| 60 | + String[] args = {testConfig.configFilePath()}; |
| 61 | + HaGatewayLauncher.main(args); |
| 62 | + // Now populate the backend |
| 63 | + HaGatewayTestUtils.setUpBackend( |
| 64 | + "trino1", "http://localhost:" + backendPort, "externalUrl", true, "adhoc", routerPort); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void testGetRoutingRulesAPI() |
| 69 | + throws Exception |
| 70 | + { |
| 71 | + Request request = |
| 72 | + new Request.Builder() |
| 73 | + .url("http://localhost:" + routerPort + "/webapp/getRoutingRules") |
| 74 | + .get() |
| 75 | + .build(); |
| 76 | + Response response = httpClient.newCall(request).execute(); |
| 77 | + |
| 78 | + String responseBody = response.body().string(); |
| 79 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 80 | + JsonNode rootNode = objectMapper.readTree(responseBody); |
| 81 | + JsonNode dataNode = rootNode.path("data"); |
| 82 | + |
| 83 | + JsonCodec<RoutingRule[]> responseCodec = JsonCodec.jsonCodec(RoutingRule[].class); |
| 84 | + RoutingRule[] routingRules = responseCodec.fromJson(dataNode.toString()); |
| 85 | + |
| 86 | + assertThat(response.code()).isEqualTo(200); |
| 87 | + assertThat(routingRules[0].name()).isEqualTo("airflow"); |
| 88 | + assertThat(routingRules[0].description()).isEqualTo("if query from airflow, route to etl group"); |
| 89 | + assertThat(routingRules[0].priority()).isEqualTo(0); |
| 90 | + assertThat(routingRules[0].condition()).isEqualTo("request.getHeader(\"X-Trino-Source\") == \"airflow\""); |
| 91 | + assertThat(routingRules[0].actions()).first().isEqualTo("result.put(\"routingGroup\", \"etl\")"); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + void testUpdateRoutingRulesAPI() |
| 96 | + throws Exception |
| 97 | + { |
| 98 | + //Update routing rules with a new rule |
| 99 | + RoutingRule updatedRoutingRules = new RoutingRule("airflow", "if query from airflow, route to adhoc group", 0, List.of("result.put(\"routingGroup\", \"adhoc\")"), "request.getHeader(\"X-Trino-Source\") == \"JDBC\""); |
| 100 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 101 | + RequestBody requestBody = RequestBody.create(objectMapper.writeValueAsString(updatedRoutingRules), MediaType.parse("application/json; charset=utf-8")); |
| 102 | + Request request = new Request.Builder() |
| 103 | + .url("http://localhost:" + routerPort + "/webapp/updateRoutingRules") |
| 104 | + .addHeader("Content-Type", "application/json") |
| 105 | + .post(requestBody) |
| 106 | + .build(); |
| 107 | + Response response = httpClient.newCall(request).execute(); |
| 108 | + |
| 109 | + assertThat(response.code()).isEqualTo(200); |
| 110 | + |
| 111 | + //Fetch the routing rules to see if the update was successful |
| 112 | + Request request2 = |
| 113 | + new Request.Builder() |
| 114 | + .url("http://localhost:" + routerPort + "/webapp/getRoutingRules") |
| 115 | + .get() |
| 116 | + .build(); |
| 117 | + Response response2 = httpClient.newCall(request2).execute(); |
| 118 | + |
| 119 | + String responseBody = response2.body().string(); |
| 120 | + ObjectMapper objectMapper2 = new ObjectMapper(); |
| 121 | + JsonNode rootNode = objectMapper2.readTree(responseBody); |
| 122 | + JsonNode dataNode = rootNode.path("data"); |
| 123 | + |
| 124 | + JsonCodec<RoutingRule[]> responseCodec = JsonCodec.jsonCodec(RoutingRule[].class); |
| 125 | + RoutingRule[] routingRules = responseCodec.fromJson(dataNode.toString()); |
| 126 | + |
| 127 | + assertThat(response.code()).isEqualTo(200); |
| 128 | + assertThat(routingRules[0].name()).isEqualTo("airflow"); |
| 129 | + assertThat(routingRules[0].description()).isEqualTo("if query from airflow, route to adhoc group"); |
| 130 | + assertThat(routingRules[0].priority()).isEqualTo(0); |
| 131 | + assertThat(routingRules[0].condition()).isEqualTo("request.getHeader(\"X-Trino-Source\") == \"JDBC\""); |
| 132 | + assertThat(routingRules[0].actions()).first().isEqualTo("result.put(\"routingGroup\", \"adhoc\")"); |
| 133 | + |
| 134 | + //Revert back to old routing rules to avoid any test failures |
| 135 | + RoutingRule revertRoutingRules = new RoutingRule("airflow", "if query from airflow, route to etl group", 0, List.of("result.put(\"routingGroup\", \"etl\")"), "request.getHeader(\"X-Trino-Source\") == \"airflow\""); |
| 136 | + ObjectMapper objectMapper3 = new ObjectMapper(); |
| 137 | + RequestBody requestBody3 = RequestBody.create(objectMapper3.writeValueAsString(revertRoutingRules), MediaType.parse("application/json; charset=utf-8")); |
| 138 | + Request request3 = new Request.Builder() |
| 139 | + .url("http://localhost:" + routerPort + "/webapp/updateRoutingRules") |
| 140 | + .addHeader("Content-Type", "application/json") |
| 141 | + .post(requestBody3) |
| 142 | + .build(); |
| 143 | + httpClient.newCall(request3).execute(); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + void testUIConfigurationAPI() |
| 148 | + throws Exception |
| 149 | + { |
| 150 | + Request request = new Request.Builder() |
| 151 | + .url("http://localhost:" + routerPort + "/webapp/getUIConfiguration") |
| 152 | + .get() |
| 153 | + .build(); |
| 154 | + |
| 155 | + Response response = httpClient.newCall(request).execute(); |
| 156 | + String responseBody = response.body().string(); |
| 157 | + |
| 158 | + ObjectMapper objectMapper2 = new ObjectMapper(); |
| 159 | + JsonNode rootNode = objectMapper2.readTree(responseBody); |
| 160 | + JsonNode dataNode = rootNode.path("data"); |
| 161 | + |
| 162 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 163 | + UIConfiguration uiConfiguration = objectMapper.readValue(dataNode.toString(), UIConfiguration.class); |
| 164 | + |
| 165 | + assertThat(response.code()).isEqualTo(200); |
| 166 | + assertThat(uiConfiguration.getDisablePages()).contains("routing-rules"); |
| 167 | + } |
| 168 | +} |
0 commit comments