2525import io .trino .spi .catalog .CatalogProperties ;
2626import io .trino .spi .catalog .CatalogStore ;
2727import io .trino .spi .catalog .CatalogStoreFactory ;
28+ import io .trino .spi .connector .CatalogVersion ;
2829import io .trino .spi .connector .ConnectorName ;
2930import io .trino .testing .DistributedQueryRunner ;
3031import io .trino .testing .H2QueryRunner ;
4243import static io .trino .connector .FileCatalogStore .computeCatalogVersion ;
4344import static io .trino .testing .QueryAssertions .assertQuery ;
4445import static io .trino .testing .QueryAssertions .assertQueryFails ;
46+ import static io .trino .testing .QueryAssertions .assertQueryReturnsEmptyResult ;
4547import static io .trino .testing .QueryAssertions .assertUpdate ;
4648import static io .trino .testing .TestingNames .randomNameSuffix ;
4749import static io .trino .testing .TestingSession .testSession ;
50+ import static java .util .Objects .requireNonNull ;
4851import static org .junit .jupiter .api .parallel .ExecutionMode .SAME_THREAD ;
4952
5053@ Execution (SAME_THREAD )
5154public class TestDynamicCatalogs
5255{
5356 private static final String BROKEN_CATALOG = "broken_catalog" ;
57+ private static final String PREPOPULATED_CATALOG = "prepopulated_catalog" ;
5458 private static final CatalogName BROKEN_CATALOG_NAME = new CatalogName (BROKEN_CATALOG );
59+ private static final CatalogName PREPOPULATED_CATALOG_NAME = new CatalogName (PREPOPULATED_CATALOG );
5560 private static final ConnectorName MEMORY_CONNECTOR_NAME = new ConnectorName ("memory" );
5661
5762 @ Test
@@ -81,16 +86,21 @@ public void testNewHealthyCatalog()
8186 }
8287
8388 @ Test
84- public void testNewUnhealthyCatalog ()
89+ public void testPrepopulatedUnhealthyCatalog ()
8590 throws Exception
8691 {
8792 Session session = testSession ();
93+ ImmutableMap <String , String > properties = ImmutableMap .of ("non_existing" , "false" );
8894 QueryRunner queryRunner = DistributedQueryRunner .builder (session )
89- .setAdditionalModule (new TestCatalogStoreModule ())
95+ .setAdditionalModule (new TestCatalogStoreModule (ImmutableMap .of (BROKEN_CATALOG_NAME , new CatalogProperties (
96+ BROKEN_CATALOG_NAME ,
97+ computeCatalogVersion (BROKEN_CATALOG_NAME , MEMORY_CONNECTOR_NAME , properties ),
98+ MEMORY_CONNECTOR_NAME ,
99+ properties ))))
100+ .setAdditionalSetup (runner -> runner .installPlugin (new MemoryPlugin ()))
90101 .setCoordinatorProperties (ImmutableMap .of ("catalog.store" , "prepopulated_memory" ))
91102 .setWorkerCount (0 )
92103 .build ();
93- queryRunner .installPlugin (new MemoryPlugin ());
94104 queryRunner .createCatalog ("healthy_catalog" , "memory" , ImmutableMap .of ("memory.max-data-per-node" , "128MB" ));
95105 H2QueryRunner h2QueryRunner = new H2QueryRunner ();
96106
@@ -103,29 +113,71 @@ public void testNewUnhealthyCatalog()
103113 assertQuery (queryRunner , session , "SHOW CATALOGS" , h2QueryRunner , "VALUES 'healthy_catalog', 'system'" , false , false );
104114 }
105115
116+ @ Test
117+ public void testPrepopulatedHealthyCatalog ()
118+ throws Exception
119+ {
120+ Session session = testSession ();
121+ ImmutableMap <String , String > properties = ImmutableMap .of ("memory.max-data-per-node" , "128MB" );
122+ QueryRunner queryRunner = DistributedQueryRunner .builder (session )
123+ .setAdditionalModule (new TestCatalogStoreModule (ImmutableMap .of (PREPOPULATED_CATALOG_NAME , new CatalogProperties (
124+ PREPOPULATED_CATALOG_NAME ,
125+ new CatalogVersion ("abc123" ),
126+ MEMORY_CONNECTOR_NAME ,
127+ properties ))))
128+ .setAdditionalSetup (runner -> runner .installPlugin (new MemoryPlugin ()))
129+ .setCoordinatorProperties (ImmutableMap .of ("catalog.store" , "prepopulated_memory" ))
130+ .setWorkerCount (0 )
131+ .build ();
132+ queryRunner .createCatalog ("healthy_catalog" , "memory" , ImmutableMap .of ("memory.max-data-per-node" , "128MB" ));
133+ H2QueryRunner h2QueryRunner = new H2QueryRunner ();
134+
135+ assertQuery (queryRunner , session , "SHOW CATALOGS" , h2QueryRunner , "VALUES 'healthy_catalog', '" + PREPOPULATED_CATALOG + "', 'system'" , false , false );
136+ assertUpdate (queryRunner , session , "CREATE TABLE %s.default.test_table (age INT)" .formatted (PREPOPULATED_CATALOG ), OptionalLong .empty (), Optional .empty ());
137+ assertQueryReturnsEmptyResult (queryRunner , session , "SELECT * FROM %s.default.test_table" .formatted (PREPOPULATED_CATALOG ));
138+ assertQueryFails (queryRunner , session , "CREATE CATALOG %s USING memory WITH (\" memory.max-data-per-node\" = '128MB')" .formatted (PREPOPULATED_CATALOG ), ".*Catalog '%s' already exists.*" .formatted (PREPOPULATED_CATALOG ));
139+
140+ assertUpdate (queryRunner , session , "DROP CATALOG " + PREPOPULATED_CATALOG , OptionalLong .empty (), Optional .empty ());
141+ assertQuery (queryRunner , session , "SHOW CATALOGS" , h2QueryRunner , "VALUES 'healthy_catalog', 'system'" , false , false );
142+ }
143+
106144 public static class TestCatalogStoreModule
107145 extends AbstractConfigurationAwareModule
108146 {
147+ private final Map <CatalogName , CatalogProperties > prepopulatedCatalogs ;
148+
149+ public TestCatalogStoreModule (Map <CatalogName , CatalogProperties > prepopulatedCatalogs )
150+ {
151+ this .prepopulatedCatalogs = requireNonNull (prepopulatedCatalogs , "prepopulatedCatalogs is null" );
152+ }
153+
109154 @ Override
110155 protected void setup (Binder binder )
111156 {
112157 if (buildConfigObject (ServerConfig .class ).isCoordinator ()) {
113- install (new PrepopulatedInMemoryCatalogStoreModule ());
158+ install (new PrepopulatedInMemoryCatalogStoreModule (prepopulatedCatalogs ));
114159 }
115160 }
116161 }
117162
118163 private static class PrepopulatedInMemoryCatalogStoreModule
119164 extends AbstractConfigurationAwareModule
120165 {
166+ private final Map <CatalogName , CatalogProperties > prepopulatedCatalogs ;
167+
168+ public PrepopulatedInMemoryCatalogStoreModule (Map <CatalogName , CatalogProperties > prepopulatedCatalogs )
169+ {
170+ this .prepopulatedCatalogs = requireNonNull (prepopulatedCatalogs , "prepopulatedCatalogs is null" );
171+ }
172+
121173 @ Override
122174 protected void setup (Binder binder ) {}
123175
124176 @ Provides
125177 @ Singleton
126178 public PrepopulatedInMemoryCatalogStoreFactory createDbCatalogStoreFactory (CatalogStoreManager catalogStoreManager )
127179 {
128- PrepopulatedInMemoryCatalogStoreFactory factory = new PrepopulatedInMemoryCatalogStoreFactory ();
180+ PrepopulatedInMemoryCatalogStoreFactory factory = new PrepopulatedInMemoryCatalogStoreFactory (prepopulatedCatalogs );
129181 catalogStoreManager .addCatalogStoreFactory (factory );
130182 return factory ;
131183 }
@@ -134,6 +186,13 @@ public PrepopulatedInMemoryCatalogStoreFactory createDbCatalogStoreFactory(Catal
134186 private static class PrepopulatedInMemoryCatalogStoreFactory
135187 implements CatalogStoreFactory
136188 {
189+ private final Map <CatalogName , CatalogProperties > prepopulatedCatalogs ;
190+
191+ public PrepopulatedInMemoryCatalogStoreFactory (Map <CatalogName , CatalogProperties > prepopulatedCatalogs )
192+ {
193+ this .prepopulatedCatalogs = requireNonNull (prepopulatedCatalogs , "prepopulatedCatalogs is null" );
194+ }
195+
137196 @ Override
138197 public String getName ()
139198 {
@@ -143,36 +202,40 @@ public String getName()
143202 @ Override
144203 public CatalogStore create (Map <String , String > config )
145204 {
146- return new PrepopulatedInMemoryCatalogStore ();
205+ return new PrepopulatedInMemoryCatalogStore (prepopulatedCatalogs );
147206 }
148207 }
149208
150209 private static class PrepopulatedInMemoryCatalogStore
151210 extends InMemoryCatalogStore
152211 {
212+ private final Map <CatalogName , CatalogProperties > prepopulatedCatalogs ;
213+
214+ public PrepopulatedInMemoryCatalogStore (Map <CatalogName , CatalogProperties > prepopulatedCatalogs )
215+ {
216+ this .prepopulatedCatalogs = requireNonNull (prepopulatedCatalogs , "prepopulatedCatalogs is null" );
217+ }
218+
153219 @ Override
154220 public Collection <StoredCatalog > getCatalogs ()
155221 {
156222 Collection <StoredCatalog > catalogs = super .getCatalogs ();
157223 List <StoredCatalog > catalogsCopy = new ArrayList <>(catalogs );
158- catalogsCopy .add (new StoredCatalog ()
159- {
160- @ Override
161- public CatalogName name ()
162- {
163- return new CatalogName ("broken_catalog" );
164- }
165-
166- @ Override
167- public CatalogProperties loadProperties ()
224+ prepopulatedCatalogs .forEach ((catalogName , catalogProperties ) -> {
225+ catalogsCopy .add (new StoredCatalog ()
168226 {
169- ImmutableMap <String , String > properties = ImmutableMap .of ("non_existing" , "false" );
170- return new CatalogProperties (
171- BROKEN_CATALOG_NAME ,
172- computeCatalogVersion (BROKEN_CATALOG_NAME , MEMORY_CONNECTOR_NAME , properties ),
173- MEMORY_CONNECTOR_NAME ,
174- properties );
175- }
227+ @ Override
228+ public CatalogName name ()
229+ {
230+ return catalogName ;
231+ }
232+
233+ @ Override
234+ public CatalogProperties loadProperties ()
235+ {
236+ return catalogProperties ;
237+ }
238+ });
176239 });
177240 return catalogsCopy ;
178241 }
0 commit comments