|
14 | 14 | package io.trino.gateway.ha.handler; |
15 | 15 |
|
16 | 16 | import com.google.common.collect.ImmutableList; |
| 17 | +import jakarta.servlet.ReadListener; |
| 18 | +import jakarta.servlet.ServletInputStream; |
17 | 19 | import jakarta.servlet.http.HttpServletRequest; |
18 | 20 | import org.junit.jupiter.api.Test; |
19 | 21 | import org.junit.jupiter.api.TestInstance; |
20 | 22 | import org.junit.jupiter.api.TestInstance.Lifecycle; |
21 | 23 | import org.mockito.Mockito; |
22 | 24 |
|
| 25 | +import java.io.ByteArrayInputStream; |
23 | 26 | import java.io.IOException; |
24 | 27 | import java.util.List; |
25 | 28 |
|
26 | 29 | import static io.trino.gateway.ha.handler.ProxyUtils.extractQueryIdIfPresent; |
27 | 30 | import static io.trino.gateway.ha.handler.ProxyUtils.getQueryUser; |
28 | 31 | import static io.trino.gateway.ha.handler.QueryIdCachingProxyHandler.AUTHORIZATION; |
29 | 32 | import static io.trino.gateway.ha.handler.QueryIdCachingProxyHandler.USER_HEADER; |
| 33 | +import static java.nio.charset.StandardCharsets.UTF_8; |
30 | 34 | import static org.assertj.core.api.Assertions.assertThat; |
| 35 | +import static org.mockito.Mockito.when; |
31 | 36 |
|
32 | 37 | @TestInstance(Lifecycle.PER_CLASS) |
33 | 38 | public class TestQueryIdCachingProxyHandler |
@@ -66,6 +71,107 @@ public void testExtractQueryIdFromUrl() |
66 | 71 | .isNull(); |
67 | 72 | } |
68 | 73 |
|
| 74 | + @Test |
| 75 | + void testQueryIdFromKill() |
| 76 | + throws IOException |
| 77 | + { |
| 78 | + assertThat( |
| 79 | + extractQueryIdIfPresent( |
| 80 | + prepareMockRequestWithBody("CALL system.runtime.kill_query(query_id => '20200416_160256_03078_6b4yt', message => 'If he dies, he dies')"), |
| 81 | + ImmutableList.of())) |
| 82 | + .isEqualTo("20200416_160256_03078_6b4yt"); |
| 83 | + |
| 84 | + assertThat( |
| 85 | + extractQueryIdIfPresent( |
| 86 | + prepareMockRequestWithBody("CALL system.runtime.kill_query(Query_id => '20200416_160256_03078_6b4yt', Message => 'If he dies, he dies')"), |
| 87 | + ImmutableList.of())) |
| 88 | + .isEqualTo("20200416_160256_03078_6b4yt"); |
| 89 | + |
| 90 | + assertThat( |
| 91 | + extractQueryIdIfPresent( |
| 92 | + prepareMockRequestWithBody("CALL kill_query('20200416_160256_03078_6b4yt', 'If he dies, he dies')"), |
| 93 | + ImmutableList.of())) |
| 94 | + .isEqualTo("20200416_160256_03078_6b4yt"); |
| 95 | + |
| 96 | + assertThat( |
| 97 | + extractQueryIdIfPresent( |
| 98 | + prepareMockRequestWithBody("CALL runtime.kill_query('20200416_160256_03078_6b4yt', '20200416_160256_03078_7n5uy')"), ImmutableList.of())) |
| 99 | + .isEqualTo("20200416_160256_03078_6b4yt"); |
| 100 | + |
| 101 | + assertThat( |
| 102 | + extractQueryIdIfPresent( |
| 103 | + prepareMockRequestWithBody("CALL system.runtime.kill_query('20200416_160256_03078_6b4yt', 'kill_query(''20200416_160256_03078_7n5uy'')')"), |
| 104 | + ImmutableList.of())) |
| 105 | + .isEqualTo("20200416_160256_03078_6b4yt"); |
| 106 | + |
| 107 | + assertThat( |
| 108 | + extractQueryIdIfPresent( |
| 109 | + prepareMockRequestWithBody("CALL system.runtime.kill_query('20200416_160256_03078_6b4yt', '20200416_160256_03078_7n5uy')"), ImmutableList.of())) |
| 110 | + .isEqualTo("20200416_160256_03078_6b4yt"); |
| 111 | + |
| 112 | + assertThat(extractQueryIdIfPresent(prepareMockRequestWithBody("CALL system.runtime.kill_query(query_id=>'20200416_160256_03078_6b4yt')"), ImmutableList.of())) |
| 113 | + .isEqualTo("20200416_160256_03078_6b4yt"); |
| 114 | + |
| 115 | + assertThat(extractQueryIdIfPresent(prepareMockRequestWithBody("CALL system.runtime.kill_query('20200416_160256_03078_6b4yt')"), ImmutableList.of())) |
| 116 | + .isEqualTo("20200416_160256_03078_6b4yt"); |
| 117 | + |
| 118 | + assertThat(extractQueryIdIfPresent(prepareMockRequestWithBody("CALL kill_query('20200416_160256_03078_6b4yt')"), ImmutableList.of())) |
| 119 | + .isEqualTo("20200416_160256_03078_6b4yt"); |
| 120 | + |
| 121 | + assertThat(extractQueryIdIfPresent(prepareMockRequestWithBody("call Kill_Query('20200416_160256_03078_6b4yt')"), ImmutableList.of())) |
| 122 | + .isEqualTo("20200416_160256_03078_6b4yt"); |
| 123 | + |
| 124 | + assertThat(extractQueryIdIfPresent(prepareMockRequestWithBody("select * from postgres.query_logs.queries where sql LIKE '%kill_query(''20200416_160256%' "), |
| 125 | + ImmutableList.of())) |
| 126 | + .isNull(); |
| 127 | + |
| 128 | + assertThat(extractQueryIdIfPresent( |
| 129 | + prepareMockRequestWithBody("select * from postgres.query_logs.queries where sql LIKE '%kill_query(''20200416_160256_03078_6b4yt' "), |
| 130 | + ImmutableList.of())) |
| 131 | + .isNull(); |
| 132 | + |
| 133 | + assertThat(extractQueryIdIfPresent( |
| 134 | + prepareMockRequestWithBody("select * from postgres.query_logs.queries where sql LIKE 'CALL kill_query(_20200416_160256_03078_6b4yt_)' "), |
| 135 | + ImmutableList.of())) |
| 136 | + .isNull(); |
| 137 | + } |
| 138 | + |
| 139 | + private static HttpServletRequest prepareMockRequestWithBody(String query) |
| 140 | + throws IOException |
| 141 | + { |
| 142 | + HttpServletRequest request = Mockito.mock(HttpServletRequest.class); |
| 143 | + |
| 144 | + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(query.getBytes(UTF_8)); |
| 145 | + when(request.getInputStream()).thenReturn(new ServletInputStream() |
| 146 | + { |
| 147 | + @Override |
| 148 | + public boolean isFinished() |
| 149 | + { |
| 150 | + return byteArrayInputStream.available() > 0; |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public boolean isReady() |
| 155 | + { |
| 156 | + return true; |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public void setReadListener(ReadListener readListener) |
| 161 | + {} |
| 162 | + |
| 163 | + public int read() |
| 164 | + throws IOException |
| 165 | + { |
| 166 | + return byteArrayInputStream.read(); |
| 167 | + } |
| 168 | + }); |
| 169 | + |
| 170 | + when(request.getQueryString()).thenReturn(""); |
| 171 | + |
| 172 | + return request; |
| 173 | + } |
| 174 | + |
69 | 175 | @Test |
70 | 176 | public void testUserFromRequest() |
71 | 177 | throws IOException |
|
0 commit comments