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