Skip to content

Commit f156664

Browse files
authored
Merge pull request #2528 from ClickHouse/fix_cloud_test_get_client_info
[repo] Fix tests reading `query_log` by closing query before flushing logs.
2 parents e7cf09c + 1cb243f commit f156664

File tree

4 files changed

+117
-101
lines changed

4 files changed

+117
-101
lines changed

client-v2/src/test/java/com/clickhouse/client/ClientTests.java

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.clickhouse.client.api.ClientException;
66
import com.clickhouse.client.api.ClientFaultCause;
77
import com.clickhouse.client.api.ConnectionReuseStrategy;
8+
import com.clickhouse.client.api.command.CommandResponse;
89
import com.clickhouse.client.api.enums.Protocol;
910
import com.clickhouse.client.api.internal.ClickHouseLZ4OutputStream;
1011
import com.clickhouse.client.api.metadata.DefaultColumnToMethodMatchingStrategy;
@@ -13,16 +14,25 @@
1314
import com.clickhouse.client.api.query.QuerySettings;
1415
import com.clickhouse.client.api.query.Records;
1516
import com.clickhouse.client.config.ClickHouseClientOption;
17+
import com.clickhouse.client.query.QueryTests;
18+
import com.clickhouse.data.ClickHouseVersion;
19+
import org.apache.commons.lang3.RandomStringUtils;
1620
import org.slf4j.Logger;
1721
import org.slf4j.LoggerFactory;
1822
import org.testng.Assert;
1923
import org.testng.annotations.DataProvider;
2024
import org.testng.annotations.Test;
25+
import org.testng.util.Strings;
2126

2227
import java.net.ConnectException;
28+
import java.util.Arrays;
2329
import java.util.HashMap;
30+
import java.util.HashSet;
31+
import java.util.List;
2432
import java.util.Map;
2533
import java.util.Optional;
34+
import java.util.Set;
35+
import java.util.UUID;
2636
import java.util.concurrent.ExecutorService;
2737
import java.util.concurrent.Executors;
2838
import java.util.concurrent.TimeUnit;
@@ -310,6 +320,104 @@ public void testWithOldDefaults() {
310320
}
311321
}
312322

323+
@DataProvider(name = "sessionRoles")
324+
private static Object[][] sessionRoles() {
325+
return new Object[][]{
326+
{new String[]{"ROL1", "ROL2"}},
327+
{new String[]{"ROL1", "ROL2"}},
328+
{new String[]{"ROL1", "ROL2"}},
329+
{new String[]{"ROL1", "ROL2,☺"}},
330+
{new String[]{"ROL1", "ROL2"}},
331+
};
332+
}
333+
334+
@Test(groups = {"integration"}, dataProvider = "sessionRoles")
335+
public void testOperationCustomRoles(String[] roles) throws Exception {
336+
if (isVersionMatch("(,24.3]", newClient().build())) {
337+
return;
338+
}
339+
340+
String password = "^1A" + RandomStringUtils.random(12, true, true) + "3b$";
341+
final String rolesList = "\"" + Strings.join("\",\"", roles) + "\"";
342+
try (Client client = newClient().build()) {
343+
client.execute("DROP ROLE IF EXISTS " + rolesList).get().close();
344+
client.execute("CREATE ROLE " + rolesList).get().close();
345+
client.execute("DROP USER IF EXISTS some_user").get().close();
346+
client.execute("CREATE USER some_user IDENTIFIED BY '" + password + "'").get().close();
347+
client.execute("GRANT " + rolesList + " TO some_user").get().close();
348+
}
349+
350+
try (Client userClient = newClient().setUsername("some_user").setPassword(password).build()) {
351+
QuerySettings settings = new QuerySettings().setDBRoles(Arrays.asList(roles));
352+
List<GenericRecord> resp = userClient.queryAll("SELECT currentRoles()", settings);
353+
Set<String> roleSet = new HashSet<>(Arrays.asList(roles));
354+
Set<String> currentRoles = new HashSet<String> (resp.get(0).getList(1));
355+
Assert.assertEquals(currentRoles, roleSet, "Roles " + roleSet + " not found in " + currentRoles);
356+
}
357+
}
358+
359+
@DataProvider(name = "clientSessionRoles")
360+
private static Object[][] clientSessionRoles() {
361+
return new Object[][]{
362+
{new String[]{"ROL1", "ROL2"}},
363+
{new String[]{"ROL1", "ROL2,☺"}},
364+
};
365+
}
366+
@Test(groups = {"integration"}, dataProvider = "clientSessionRoles")
367+
public void testClientCustomRoles(String[] roles) throws Exception {
368+
if (isVersionMatch("(,24.3]", newClient().build())) {
369+
return;
370+
}
371+
372+
String password = "^1A" + RandomStringUtils.random(12, true, true) + "3B$";
373+
final String rolesList = "\"" + Strings.join("\",\"", roles) + "\"";
374+
try (Client client = newClient().build()) {
375+
client.execute("DROP ROLE IF EXISTS " + rolesList).get().close();
376+
client.execute("CREATE ROLE " + rolesList).get().close();
377+
client.execute("DROP USER IF EXISTS some_user").get().close();
378+
client.execute("CREATE USER some_user IDENTIFIED WITH sha256_password BY '" + password + "'").get().close();
379+
client.execute("GRANT " + rolesList + " TO some_user").get().close();
380+
}
381+
382+
try (Client userClient = newClient().setUsername("some_user").setPassword(password).build()) {
383+
userClient.setDBRoles(Arrays.asList(roles));
384+
List<GenericRecord> resp = userClient.queryAll("SELECT currentRoles()");
385+
Set<String> roleSet = new HashSet<>(Arrays.asList(roles));
386+
Set<String> currentRoles = new HashSet<String> (resp.get(0).getList(1));
387+
Assert.assertEquals(currentRoles, roleSet, "Roles " + roleSet + " not found in " + currentRoles);
388+
}
389+
}
390+
391+
392+
@Test(groups = {"integration"})
393+
public void testLogComment() throws Exception {
394+
395+
String logComment = "Test log comment";
396+
QuerySettings settings = new QuerySettings()
397+
.setQueryId(UUID.randomUUID().toString())
398+
.logComment(logComment);
399+
400+
try (Client client = newClient().build()) {
401+
402+
try (QueryResponse response = client.query("SELECT 1", settings).get()) {
403+
Assert.assertNotNull(response.getQueryId());
404+
Assert.assertTrue(response.getQueryId().startsWith(settings.getQueryId()));
405+
}
406+
407+
client.execute("SYSTEM FLUSH LOGS").get().close();
408+
409+
List<GenericRecord> logRecords = client.queryAll("SELECT query_id, log_comment FROM clusterAllReplicas('default', system.query_log) WHERE query_id = '" + settings.getQueryId() + "'");
410+
Assert.assertEquals(logRecords.get(0).getString("query_id"), settings.getQueryId());
411+
Assert.assertEquals(logRecords.get(0).getString("log_comment"), logComment);
412+
}
413+
}
414+
415+
public boolean isVersionMatch(String versionExpression, Client client) {
416+
List<GenericRecord> serverVersion = client.queryAll("SELECT version()");
417+
return ClickHouseVersion.of(serverVersion.get(0).getString(1)).check(versionExpression);
418+
}
419+
420+
313421
protected Client.Builder newClient() {
314422
ClickHouseNode node = getServer(ClickHouseProtocol.HTTP);
315423
boolean isSecure = isCloud();

client-v2/src/test/java/com/clickhouse/client/HttpTransportTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ public void testUserAgentHasCompleteProductName(String clientName, Pattern userA
806806
client.execute("SYSTEM FLUSH LOGS").get().close();
807807

808808
List<GenericRecord> logRecords = client.queryAll("SELECT http_user_agent, http_referer, " +
809-
" forwarded_for FROM system.query_log WHERE query_id = '" + q1Id + "'");
809+
" forwarded_for FROM clusterAllReplicas('default', system.query_log) WHERE query_id = '" + q1Id + "'");
810810
Assert.assertFalse(logRecords.isEmpty(), "No records found in query log");
811811

812812
for (GenericRecord record : logRecords) {
@@ -864,7 +864,7 @@ public void testClientName(String clientName, boolean setWithUpdate, String user
864864
client.execute("SYSTEM FLUSH LOGS").get().close();
865865

866866
List<GenericRecord> logRecords = client.queryAll("SELECT query_id, client_name, http_user_agent, http_referer " +
867-
" FROM system.query_log WHERE query_id = '" + settings.getQueryId() + "'");
867+
" FROM clusterAllReplicas('default', system.query_log) WHERE query_id = '" + settings.getQueryId() + "'");
868868
Assert.assertEquals(logRecords.get(0).getString("query_id"), settings.getQueryId());
869869
final String logUserAgent = logRecords.get(0).getString("http_user_agent");
870870
Assert.assertTrue(logUserAgent.startsWith(expectedClientNameStartsWith),
@@ -905,7 +905,7 @@ public void testClientNameThruRawOptions(String property, String value, boolean
905905
client.execute("SYSTEM FLUSH LOGS").get().close();
906906

907907
List<GenericRecord> logRecords = client.queryAll("SELECT query_id, client_name, http_user_agent, http_referer " +
908-
" FROM system.query_log WHERE query_id = '" + settings.getQueryId() + "'");
908+
" FROM clusterAllReplicas('default', system.query_log) WHERE query_id = '" + settings.getQueryId() + "'");
909909
Assert.assertEquals(logRecords.get(0).getString("query_id"), settings.getQueryId());
910910
final String logUserAgent = logRecords.get(0).getString("http_user_agent");
911911
Assert.assertTrue(logUserAgent.startsWith(value),

client-v2/src/test/java/com/clickhouse/client/query/QueryTests.java

Lines changed: 0 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1902,101 +1902,6 @@ public static BigDecimal cropDecimal(BigDecimal value, int scale) {
19021902
return new BigDecimal(bi, scale);
19031903
}
19041904

1905-
@DataProvider(name = "sessionRoles")
1906-
private static Object[][] sessionRoles() {
1907-
return new Object[][]{
1908-
{new String[]{"ROL1", "ROL2"}},
1909-
{new String[]{"ROL1", "ROL2"}},
1910-
{new String[]{"ROL1", "ROL2"}},
1911-
{new String[]{"ROL1", "ROL2,☺"}},
1912-
{new String[]{"ROL1", "ROL2"}},
1913-
};
1914-
}
1915-
1916-
@Test(groups = {"integration"}, dataProvider = "sessionRoles", dataProviderClass = QueryTests.class)
1917-
public void testOperationCustomRoles(String[] roles) throws Exception {
1918-
if (isVersionMatch("(,24.3]")) {
1919-
return;
1920-
}
1921-
1922-
String password = "^1A" + RandomStringUtils.random(12, true, true) + "3b$";
1923-
final String rolesList = "\"" + Strings.join("\",\"", roles) + "\"";
1924-
try (CommandResponse resp = client.execute("DROP ROLE IF EXISTS " + rolesList).get()) {
1925-
}
1926-
try (CommandResponse resp = client.execute("CREATE ROLE " + rolesList).get()) {
1927-
}
1928-
try (CommandResponse resp = client.execute("DROP USER IF EXISTS some_user").get()) {
1929-
}
1930-
try (CommandResponse resp = client.execute("CREATE USER some_user IDENTIFIED BY '" + password + "'" ).get()) {
1931-
}
1932-
try (CommandResponse resp = client.execute("GRANT " + rolesList + " TO some_user").get()) {
1933-
}
1934-
1935-
1936-
try (Client userClient = newClient().setUsername("some_user").setPassword(password).build()) {
1937-
QuerySettings settings = new QuerySettings().setDBRoles(Arrays.asList(roles));
1938-
List<GenericRecord> resp = userClient.queryAll("SELECT currentRoles()", settings);
1939-
Set<String> roleSet = new HashSet<>(Arrays.asList(roles));
1940-
Set<String> currentRoles = new HashSet<String> (resp.get(0).getList(1));
1941-
Assert.assertEquals(currentRoles, roleSet, "Roles " + roleSet + " not found in " + currentRoles);
1942-
}
1943-
}
1944-
1945-
@DataProvider(name = "clientSessionRoles")
1946-
private static Object[][] clientSessionRoles() {
1947-
return new Object[][]{
1948-
{new String[]{"ROL1", "ROL2"}},
1949-
{new String[]{"ROL1", "ROL2,☺"}},
1950-
};
1951-
}
1952-
@Test(groups = {"integration"}, dataProvider = "clientSessionRoles", dataProviderClass = QueryTests.class)
1953-
public void testClientCustomRoles(String[] roles) throws Exception {
1954-
if (isVersionMatch("(,24.3]")) {
1955-
return;
1956-
}
1957-
1958-
String password = "^1A" + RandomStringUtils.random(12, true, true) + "3B$";
1959-
final String rolesList = "\"" + Strings.join("\",\"", roles) + "\"";
1960-
try (CommandResponse resp = client.execute("DROP ROLE IF EXISTS " + rolesList).get()) {
1961-
}
1962-
try (CommandResponse resp = client.execute("CREATE ROLE " + rolesList).get()) {
1963-
}
1964-
try (CommandResponse resp = client.execute("DROP USER IF EXISTS some_user").get()) {
1965-
}
1966-
try (CommandResponse resp = client.execute("CREATE USER some_user IDENTIFIED WITH sha256_password BY '" + password + "'" ).get()) {
1967-
}
1968-
try (CommandResponse resp = client.execute("GRANT " + rolesList + " TO some_user").get()) {
1969-
}
1970-
1971-
try (Client userClient = newClient().setUsername("some_user").setPassword(password).build()) {
1972-
userClient.setDBRoles(Arrays.asList(roles));
1973-
List<GenericRecord> resp = userClient.queryAll("SELECT currentRoles()");
1974-
Set<String> roleSet = new HashSet<>(Arrays.asList(roles));
1975-
Set<String> currentRoles = new HashSet<String> (resp.get(0).getList(1));
1976-
Assert.assertEquals(currentRoles, roleSet, "Roles " + roleSet + " not found in " + currentRoles);
1977-
}
1978-
}
1979-
1980-
1981-
@Test(groups = {"integration"})
1982-
public void testLogComment() throws Exception {
1983-
1984-
String logComment = "Test log comment";
1985-
QuerySettings settings = new QuerySettings()
1986-
.setQueryId(UUID.randomUUID().toString())
1987-
.logComment(logComment);
1988-
try (QueryResponse response = client.query("SELECT 1", settings).get()) {
1989-
Assert.assertNotNull(response.getQueryId());
1990-
Assert.assertTrue(response.getQueryId().startsWith(settings.getQueryId()));
1991-
}
1992-
1993-
try (CommandResponse resp = client.execute("SYSTEM FLUSH LOGS").get()) {
1994-
}
1995-
1996-
List<GenericRecord> logRecords = client.queryAll("SELECT query_id, log_comment FROM system.query_log WHERE query_id = '" + settings.getQueryId() + "'");
1997-
Assert.assertEquals(logRecords.get(0).getString("query_id"), settings.getQueryId());
1998-
Assert.assertEquals(logRecords.get(0).getString("log_comment"), logComment);
1999-
}
20001905
@Test(groups = { "integration" }, enabled = true)
20011906
public void testReadingBitmap() throws Exception {
20021907
final String tableName = "bitmaps_test_table";

jdbc-v2/src/test/java/com/clickhouse/jdbc/ConnectionTest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,10 +306,12 @@ public void setAndGetClientInfoTest(String clientName) throws SQLException {
306306

307307
final String testQuery = "SELECT '" + UUID.randomUUID() + "'";
308308
stmt.execute(testQuery);
309+
String queryId = ((StatementImpl)stmt).getLastQueryId();
310+
stmt.getResultSet().close(); // close result set to finalize request.
309311
stmt.execute("SYSTEM FLUSH LOGS");
310312

311-
final String logQuery ="SELECT http_user_agent " +
312-
" FROM system.query_log WHERE query = '" + testQuery.replaceAll("'", "\\\\'") + "'";
313+
314+
final String logQuery ="SELECT http_user_agent FROM clusterAllReplicas('default', system.query_log) WHERE query_id = " + stmt.enquoteLiteral(queryId);
313315
try (ResultSet rs = stmt.executeQuery(logQuery)) {
314316
Assert.assertTrue(rs.next());
315317
String userAgent = rs.getString("http_user_agent");
@@ -340,10 +342,11 @@ private void influenceUserAgentTest(String clientName, String urlParam) throws S
340342

341343
final String testQuery = "SELECT '" + UUID.randomUUID() + "'";
342344
stmt.execute(testQuery);
345+
stmt.getResultSet().close(); // finalize request
343346
stmt.execute("SYSTEM FLUSH LOGS");
344347

345348
final String logQuery ="SELECT http_user_agent " +
346-
" FROM system.query_log WHERE query = '" + testQuery.replaceAll("'", "\\\\'") + "'";
349+
" FROM clusterAllReplicas('default', system.query_log) WHERE query = '" + testQuery.replaceAll("'", "\\\\'") + "'";
347350
try (ResultSet rs = stmt.executeQuery(logQuery)) {
348351
Assert.assertTrue(rs.next());
349352
String userAgent = rs.getString("http_user_agent");

0 commit comments

Comments
 (0)