singhpk234 commented on a change in pull request #4423: URL: https://github.com/apache/iceberg/pull/4423#discussion_r836719802
########## File path: aws/src/integration/java/org/apache/iceberg/aws/lakeformation/TestLakeFormationDML.java ########## @@ -0,0 +1,159 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.iceberg.aws.lakeformation; + +import org.apache.iceberg.AssertHelpers; +import org.apache.iceberg.DataFile; +import org.apache.iceberg.DataFiles; +import org.apache.iceberg.Table; +import org.apache.iceberg.catalog.Namespace; +import org.apache.iceberg.catalog.TableIdentifier; +import org.apache.iceberg.exceptions.CommitStateUnknownException; +import org.junit.Test; +import software.amazon.awssdk.services.glue.model.AccessDeniedException; +import software.amazon.awssdk.services.lakeformation.model.Permission; +import software.amazon.awssdk.services.s3.model.S3Exception; + +public class TestLakeFormationDML extends LakeFormationTestBase { + + @Test + public void testLoadTableWithNoTableAccess() { + String testDbName = getRandomDbName(); + String testTableName = getRandomTableName(); + lfRegisterPathRoleCreateDb(testDbName); + lfRegisterPathRoleCreateTable(testDbName, testTableName); + try { + AssertHelpers.assertThrows("attempt to load a table without SELECT permission should fail", + AccessDeniedException.class, + "Insufficient Lake Formation permission(s)", + () -> glueCatalogPrivilegedRole.loadTable(TableIdentifier.of(Namespace.of(testDbName), testTableName))); + } finally { + lfRegisterPathRoleDeleteTable(testDbName, testTableName); + lfRegisterPathRoleDeleteDb(testDbName); + } + } + + @Test + public void testLoadTableSuccess() { + String testDbName = getRandomDbName(); + String testTableName = getRandomTableName(); + lfRegisterPathRoleCreateDb(testDbName); + lfRegisterPathRoleCreateTable(testDbName, testTableName); + grantTablePrivileges(testDbName, testTableName, Permission.SELECT); + try { + glueCatalogPrivilegedRole.loadTable(TableIdentifier.of(Namespace.of(testDbName), testTableName)); + } finally { + lfRegisterPathRoleDeleteTable(testDbName, testTableName); + lfRegisterPathRoleDeleteDb(testDbName); + } + } + + @Test + public void testUpdateTableWithNoInsertAccess() { + String testDbName = getRandomDbName(); + String testTableName = getRandomTableName(); + lfRegisterPathRoleCreateDb(testDbName); + lfRegisterPathRoleCreateTable(testDbName, testTableName); + grantTablePrivileges(testDbName, testTableName, Permission.SELECT); + try { + Table table = glueCatalogPrivilegedRole.loadTable(TableIdentifier.of(Namespace.of(testDbName), testTableName)); + DataFile dataFile = DataFiles.builder(partitionSpec) + .withPath("/path/to/data-a.parquet") + .withFileSizeInBytes(10) + .withRecordCount(1) + .build(); + AssertHelpers.assertThrows("attempt to insert to a table without INSERT permission should fail", + S3Exception.class, + "Access Denied", + () -> table.newAppend().appendFile(dataFile).commit()); + } finally { + lfRegisterPathRoleDeleteTable(testDbName, testTableName); + lfRegisterPathRoleDeleteDb(testDbName); + } Review comment: [suggestion] try structuring the code in BeforeEach, AfterEach lfRegisterPathRoleDeleteTable(testDbName, testTableName); lfRegisterPathRoleDeleteDb(testDbName); seems to be good candidate for AfterEach, lfRegisterPathRoleCreateDb(testDbName); lfRegisterPathRoleCreateTable(testDbName, testTableName); seems to good candidate for BeforeEach It should remove atleast 6 lines from from each test case can make testDbName / testTableName as class property which you set before each ut execution and similarly you can reference it AfterEach to get rid of it. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
