|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.apache.doris.service; |
| 19 | + |
| 20 | +import org.apache.doris.analysis.UserIdentity; |
| 21 | +import org.apache.doris.catalog.DatabaseIf; |
| 22 | +import org.apache.doris.catalog.Env; |
| 23 | +import org.apache.doris.catalog.InfoSchemaDb; |
| 24 | +import org.apache.doris.datasource.CatalogIf; |
| 25 | +import org.apache.doris.datasource.CatalogMgr; |
| 26 | +import org.apache.doris.mysql.privilege.AccessControllerManager; |
| 27 | +import org.apache.doris.mysql.privilege.PrivPredicate; |
| 28 | +import org.apache.doris.qe.GlobalVariable; |
| 29 | +import org.apache.doris.thrift.TGetDbsParams; |
| 30 | +import org.apache.doris.thrift.TGetDbsResult; |
| 31 | + |
| 32 | +import org.apache.thrift.TDeserializer; |
| 33 | +import org.apache.thrift.TSerializer; |
| 34 | +import org.apache.thrift.protocol.TBinaryProtocol; |
| 35 | +import org.junit.jupiter.api.AfterEach; |
| 36 | +import org.junit.jupiter.api.Assertions; |
| 37 | +import org.junit.jupiter.api.BeforeEach; |
| 38 | +import org.junit.jupiter.api.Test; |
| 39 | +import org.mockito.MockedStatic; |
| 40 | +import org.mockito.Mockito; |
| 41 | + |
| 42 | +import java.util.ArrayList; |
| 43 | +import java.util.Arrays; |
| 44 | +import java.util.Collections; |
| 45 | +import java.util.List; |
| 46 | + |
| 47 | +public class FrontendServiceGetDbNamesTest { |
| 48 | + private final Env env = Mockito.mock(Env.class); |
| 49 | + private final CatalogMgr catalogMgr = Mockito.mock(CatalogMgr.class); |
| 50 | + private final AccessControllerManager accessManager = Mockito.mock(AccessControllerManager.class); |
| 51 | + private final CatalogIf catalog = Mockito.mock(CatalogIf.class); |
| 52 | + private final FrontendServiceImpl service = new FrontendServiceImpl(Mockito.mock(ExecuteEnv.class)); |
| 53 | + private MockedStatic<Env> envMock; |
| 54 | + |
| 55 | + @BeforeEach |
| 56 | + public void setUp() { |
| 57 | + envMock = Mockito.mockStatic(Env.class); |
| 58 | + envMock.when(Env::getCurrentEnv).thenReturn(env); |
| 59 | + Mockito.when(env.getCatalogMgr()).thenReturn(catalogMgr); |
| 60 | + Mockito.when(env.getAccessManager()).thenReturn(accessManager); |
| 61 | + Mockito.when(catalogMgr.listCatalogs()).thenReturn(Collections.singletonList(catalog)); |
| 62 | + Mockito.when(catalog.getName()).thenReturn("example_catalog"); |
| 63 | + Mockito.when(catalog.getId()).thenReturn(42L); |
| 64 | + Mockito.when(accessManager.checkDbPriv(Mockito.any(UserIdentity.class), Mockito.anyString(), |
| 65 | + Mockito.anyString(), Mockito.eq(PrivPredicate.SHOW))).thenReturn(true); |
| 66 | + } |
| 67 | + |
| 68 | + @AfterEach |
| 69 | + public void tearDown() { |
| 70 | + envMock.close(); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testNamesOnlyDoesNotMaterializeDatabases() throws Exception { |
| 75 | + List<String> names = new ArrayList<>(); |
| 76 | + for (int i = 0; i < 100_000; i++) { |
| 77 | + names.add("namespace_" + i); |
| 78 | + } |
| 79 | + names.add(InfoSchemaDb.DATABASE_NAME); |
| 80 | + names.add("mysql"); |
| 81 | + Mockito.when(catalog.getDbNames()).thenReturn(names); |
| 82 | + Mockito.when(catalog.getAllDbs()).thenThrow(new AssertionError("Database objects must not be loaded")); |
| 83 | + |
| 84 | + TGetDbsParams params = params(); |
| 85 | + params.setIncludeDbIds(false); |
| 86 | + TGetDbsResult result = service.getDbNames(params); |
| 87 | + |
| 88 | + Assertions.assertEquals(names.size(), result.getDbsSize()); |
| 89 | + Assertions.assertEquals(names.size(), result.getCatalogsSize()); |
| 90 | + Assertions.assertEquals(names.size(), result.getCatalogIdsSize()); |
| 91 | + Assertions.assertTrue(result.getCatalogs().stream().allMatch("example_catalog"::equals)); |
| 92 | + Assertions.assertTrue(result.getCatalogIds().stream().allMatch(id -> id == 42L)); |
| 93 | + Assertions.assertFalse(result.isSetDbIds()); |
| 94 | + Mockito.verify(catalog, Mockito.times(1)).getDbNames(); |
| 95 | + Mockito.verify(catalog, Mockito.never()).getAllDbs(); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testNamesOnlyPreservesPrivilegesPatternAndDisplayNames() throws Exception { |
| 100 | + Mockito.when(catalog.getDbNames()).thenReturn(Arrays.asList("visible_a", "visible_hidden", "other")); |
| 101 | + Mockito.when(accessManager.checkDbPriv(Mockito.any(UserIdentity.class), Mockito.eq("example_catalog"), |
| 102 | + Mockito.eq("visible_hidden"), Mockito.eq(PrivPredicate.SHOW))).thenReturn(false); |
| 103 | + |
| 104 | + boolean original = GlobalVariable.showFullDbNameInInfoSchemaDb; |
| 105 | + try { |
| 106 | + for (boolean showFullName : new boolean[] {false, true}) { |
| 107 | + GlobalVariable.showFullDbNameInInfoSchemaDb = showFullName; |
| 108 | + TGetDbsParams params = params(); |
| 109 | + params.setIncludeDbIds(false); |
| 110 | + params.setPattern(showFullName ? "example_catalog.visible%" : "visible%"); |
| 111 | + TGetDbsResult result = service.getDbNames(params); |
| 112 | + Assertions.assertEquals(Collections.singletonList( |
| 113 | + showFullName ? "example_catalog.visible_a" : "visible_a"), result.getDbs()); |
| 114 | + Assertions.assertFalse(result.isSetDbIds()); |
| 115 | + } |
| 116 | + } finally { |
| 117 | + GlobalVariable.showFullDbNameInInfoSchemaDb = original; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void testExistingCallersKeepDatabaseIds() throws Exception { |
| 123 | + DatabaseIf database = Mockito.mock(DatabaseIf.class); |
| 124 | + Mockito.when(database.getFullName()).thenReturn("visible_a"); |
| 125 | + Mockito.when(database.getId()).thenReturn(123L); |
| 126 | + Mockito.when(catalog.getAllDbs()).thenReturn(Collections.singletonList(database)); |
| 127 | + |
| 128 | + for (boolean explicitlySet : new boolean[] {false, true}) { |
| 129 | + TGetDbsParams params = params(); |
| 130 | + if (explicitlySet) { |
| 131 | + params.setIncludeDbIds(true); |
| 132 | + } |
| 133 | + TGetDbsResult result = service.getDbNames(params); |
| 134 | + Assertions.assertEquals(Collections.singletonList(123L), result.getDbIds()); |
| 135 | + Assertions.assertEquals(Collections.singletonList(42L), result.getCatalogIds()); |
| 136 | + } |
| 137 | + Mockito.verify(catalog, Mockito.never()).getDbNames(); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + public void testAbsentIncludeDbIdsOnWireKeepsLegacyDefault() throws Exception { |
| 142 | + TGetDbsParams legacy = params(); |
| 143 | + legacy.unsetIncludeDbIds(); |
| 144 | + TSerializer serializer = new TSerializer(new TBinaryProtocol.Factory()); |
| 145 | + TDeserializer deserializer = new TDeserializer(new TBinaryProtocol.Factory()); |
| 146 | + TGetDbsParams decoded = new TGetDbsParams(); |
| 147 | + deserializer.deserialize(decoded, serializer.serialize(legacy)); |
| 148 | + Assertions.assertTrue(decoded.isIncludeDbIds()); |
| 149 | + |
| 150 | + legacy.setIncludeDbIds(false); |
| 151 | + deserializer.deserialize(decoded, serializer.serialize(legacy)); |
| 152 | + Assertions.assertFalse(decoded.isIncludeDbIds()); |
| 153 | + } |
| 154 | + |
| 155 | + @Test |
| 156 | + public void testNamesOnlyPreservesEmptyCatalogPlaceholder() throws Exception { |
| 157 | + Mockito.when(catalog.getDbNames()).thenReturn(Collections.emptyList()); |
| 158 | + TGetDbsParams params = params(); |
| 159 | + params.setIncludeDbIds(false); |
| 160 | + params.setGetNullCatalog(true); |
| 161 | + |
| 162 | + TGetDbsResult result = service.getDbNames(params); |
| 163 | + Assertions.assertEquals(Collections.singletonList("NULL"), result.getDbs()); |
| 164 | + Assertions.assertEquals(Collections.singletonList("example_catalog"), result.getCatalogs()); |
| 165 | + Assertions.assertEquals(Collections.singletonList(42L), result.getCatalogIds()); |
| 166 | + Assertions.assertFalse(result.isSetDbIds()); |
| 167 | + } |
| 168 | + |
| 169 | + @Test |
| 170 | + public void testNamesOnlySkipsUnavailableCatalog() throws Exception { |
| 171 | + Mockito.when(catalog.getDbNames()).thenThrow(new IllegalStateException("Metadata service unavailable")); |
| 172 | + TGetDbsParams params = params(); |
| 173 | + params.setIncludeDbIds(false); |
| 174 | + |
| 175 | + TGetDbsResult result = service.getDbNames(params); |
| 176 | + Assertions.assertTrue(result.getDbs().isEmpty()); |
| 177 | + Assertions.assertTrue(result.getCatalogs().isEmpty()); |
| 178 | + Assertions.assertFalse(result.isSetDbIds()); |
| 179 | + } |
| 180 | + |
| 181 | + private TGetDbsParams params() { |
| 182 | + TGetDbsParams params = new TGetDbsParams(); |
| 183 | + params.setCurrentUserIdent(UserIdentity.ROOT.toThrift()); |
| 184 | + return params; |
| 185 | + } |
| 186 | +} |
0 commit comments