Skip to content

Commit

Permalink
fix failed unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ChenSammi committed Nov 25, 2024
1 parent b336b05 commit a3036f1
Show file tree
Hide file tree
Showing 21 changed files with 141 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,6 @@ vol, bucket, key, volOwner, bucketOwner, createUGIForApi(),
*/
@VisibleForTesting
public UserGroupInformation createUGI() throws AuthenticationException {

if (userGroupInformation != null) {
return userGroupInformation;
}
Expand Down Expand Up @@ -413,6 +412,11 @@ public UserGroupInformation createUGIForApi() throws OMException {
return ugi;
}

@VisibleForTesting
public void setUGI(UserGroupInformation ugi) {
this.userGroupInformation = ugi;
}

/**
* Return InetAddress created from OMRequest userInfo. If userInfo is not
* set, returns null.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,14 +325,14 @@ private boolean isECBucket(BucketInfo bucketInfo) {
*/
private void addDefaultAcls(OmBucketInfo omBucketInfo,
OmVolumeArgs omVolumeArgs, OzoneManager ozoneManager) throws OMException {
// Add acls for bucket creator.
List<OzoneAcl> acls = new ArrayList<>();
// Add default acls
acls.addAll(getDefaultAclList(createUGIForApi(), ozoneManager.getConfiguration()));
if (omBucketInfo.getAcls() != null) {
// Add acls for bucket creator.
acls.addAll(omBucketInfo.getAcls());
}

// Add default acls
acls.addAll(getDefaultAclList(createUGIForApi(), ozoneManager.getConfiguration()));
// Link bucket default acl
if (omBucketInfo.getSourceBucket() != null && omBucketInfo.getSourceVolume() != null) {
acls.add(linkBucketDefaultAcl());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,12 +416,11 @@ protected List<OzoneAcl> getAclsForKey(KeyArgs keyArgs,
PrefixManager prefixManager, OzoneConfiguration config) throws OMException {

List<OzoneAcl> acls = new ArrayList<>();
acls.addAll(getDefaultAclList(createUGIForApi(), config));
if (keyArgs.getAclsList() != null) {
acls.addAll(OzoneAclUtil.fromProtobuf(keyArgs.getAclsList()));
}

acls.addAll(getDefaultAclList(createUGIForApi(), config));

// Inherit DEFAULT acls from prefix.
if (prefixManager != null) {
List< OmPrefixInfo > prefixList = prefixManager.getLongestPrefixPath(
Expand Down Expand Up @@ -477,6 +476,8 @@ protected List<OzoneAcl> getAclsForDir(KeyArgs keyArgs, OmBucketInfo bucketInfo,
OMFileRequest.OMPathInfo omPathInfo, OzoneConfiguration config) throws OMException {
// Acls inherited from parent or bucket will convert to DEFAULT scope
List<OzoneAcl> acls = new ArrayList<>();
// add default ACLs
acls.addAll(getDefaultAclList(createUGIForApi(), config));

// Inherit DEFAULT acls from parent-dir
if (omPathInfo != null) {
Expand All @@ -491,7 +492,6 @@ protected List<OzoneAcl> getAclsForDir(KeyArgs keyArgs, OmBucketInfo bucketInfo,

// add acls from clients
acls.addAll(OzoneAclUtil.fromProtobuf(keyArgs.getAclsList()));
acls.addAll(getDefaultAclList(createUGIForApi(), config));
acls = acls.stream().distinct().collect(Collectors.toList());
return acls;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.apache.hadoop.ozone.om.response.OMClientResponse;
import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos;
import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.BucketInfo;
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.ratis.server.protocol.TermIndex;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -107,6 +108,7 @@ public void setup() throws IOException {
when(ozoneManager.getMaxUserVolumeCount()).thenReturn(10L);
auditLogger = mock(AuditLogger.class);
when(ozoneManager.getAuditLogger()).thenReturn(auditLogger);
when(ozoneManager.getConfiguration()).thenReturn(ozoneConfiguration);
doNothing().when(auditLogger).logWrite(any(AuditMessage.class));
doubleBuffer = OzoneManagerDoubleBuffer.newBuilder()
.setOmMetadataManager(omMetadataManager)
Expand Down Expand Up @@ -450,6 +452,11 @@ private OMClientResponse createVolume(String volumeName,

OMVolumeCreateRequest omVolumeCreateRequest =
new OMVolumeCreateRequest(omRequest);
try {
omVolumeCreateRequest.setUGI(UserGroupInformation.getCurrentUser());
} catch (IOException e) {
throw new RuntimeException(e);
}

final TermIndex termIndex = TransactionInfo.getTermIndex(transactionId);
OMClientResponse omClientResponse = omVolumeCreateRequest.validateAndUpdateCache(ozoneManager, termIndex);
Expand All @@ -462,7 +469,7 @@ private OMClientResponse createVolume(String volumeName,
* @return OMBucketCreateResponse
*/
private OMBucketCreateResponse createBucket(String volumeName,
String bucketName, long transactionID) {
String bucketName, long transactionID) {

BucketInfo.Builder bucketInfo =
newBucketInfoBuilder(bucketName, volumeName)
Expand All @@ -472,6 +479,10 @@ private OMBucketCreateResponse createBucket(String volumeName,

OMBucketCreateRequest omBucketCreateRequest =
new OMBucketCreateRequest(omRequest);
try {
omBucketCreateRequest.setUGI(UserGroupInformation.getCurrentUser());
} catch (IOException e) {
}

final TermIndex termIndex = TermIndex.valueOf(term, transactionID);
OMClientResponse omClientResponse = omBucketCreateRequest.validateAndUpdateCache(ozoneManager, termIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.hadoop.ozone.om.OMConfigKeys;
import org.apache.hadoop.ozone.om.exceptions.OMException;
import org.apache.hadoop.ozone.om.helpers.BucketLayout;
import org.apache.hadoop.security.UserGroupInformation;
import org.junit.jupiter.api.Test;

import org.apache.hadoop.ozone.om.OMMetadataManager;
Expand All @@ -41,6 +42,7 @@

import static org.apache.hadoop.ozone.om.request.OMRequestTestUtils.newBucketInfoBuilder;
import static org.apache.hadoop.ozone.om.request.OMRequestTestUtils.newCreateBucketRequest;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand Down Expand Up @@ -336,7 +338,7 @@ protected void doValidateAndUpdateCache(String volumeName, String bucketName,
assertNull(omMetadataManager.getBucketTable().get(bucketKey));
OMBucketCreateRequest omBucketCreateRequest =
new OMBucketCreateRequest(modifiedRequest);

omBucketCreateRequest.setUGI(UserGroupInformation.getCurrentUser());

OMClientResponse omClientResponse =
omBucketCreateRequest.validateAndUpdateCache(ozoneManager, 1);
Expand All @@ -355,8 +357,7 @@ protected void doValidateAndUpdateCache(String volumeName, String bucketName,
dbBucketInfo.getCreationTime());
assertEquals(bucketInfoFromProto.getModificationTime(),
dbBucketInfo.getModificationTime());
assertEquals(bucketInfoFromProto.getAcls(),
dbBucketInfo.getAcls());
assertTrue(dbBucketInfo.getAcls().containsAll(bucketInfoFromProto.getAcls()));
assertEquals(bucketInfoFromProto.getIsVersionEnabled(),
dbBucketInfo.getIsVersionEnabled());
assertEquals(bucketInfoFromProto.getStorageType(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.hadoop.ozone.om.response.OMClientResponse;
import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos;
import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OMRequest;
import org.apache.hadoop.security.UserGroupInformation;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -34,6 +35,7 @@
import static org.apache.hadoop.ozone.om.request.OMRequestTestUtils.newBucketInfoBuilder;
import static org.apache.hadoop.ozone.om.request.OMRequestTestUtils.newCreateBucketRequest;
import static org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.BucketLayoutProto.FILE_SYSTEM_OPTIMIZED;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
Expand Down Expand Up @@ -143,7 +145,7 @@ protected void doValidateAndUpdateCache(String volumeName, String bucketName,
assertNull(omMetadataManager.getBucketTable().get(bucketKey));
OMBucketCreateRequest omBucketCreateRequest =
new OMBucketCreateRequest(modifiedRequest);

omBucketCreateRequest.setUGI(UserGroupInformation.getCurrentUser());

OMClientResponse omClientResponse =
omBucketCreateRequest.validateAndUpdateCache(ozoneManager, 1);
Expand All @@ -166,8 +168,7 @@ protected void doValidateAndUpdateCache(String volumeName, String bucketName,
dbBucketInfo.getCreationTime());
assertEquals(bucketInfoFromProto.getModificationTime(),
dbBucketInfo.getModificationTime());
assertEquals(bucketInfoFromProto.getAcls(),
dbBucketInfo.getAcls());
assertTrue(dbBucketInfo.getAcls().containsAll(bucketInfoFromProto.getAcls()));
assertEquals(bucketInfoFromProto.getIsVersionEnabled(),
dbBucketInfo.getIsVersionEnabled());
assertEquals(bucketInfoFromProto.getStorageType(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.apache.hadoop.ozone.om.response.OMClientResponse;
import org.apache.hadoop.hdds.utils.db.cache.CacheKey;
import org.apache.hadoop.ozone.om.upgrade.OMLayoutVersionManager;
import org.apache.hadoop.security.UserGroupInformation;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -68,6 +69,7 @@
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.doNothing;
Expand Down Expand Up @@ -183,7 +185,7 @@ public void testValidateAndUpdateCache() throws Exception {

omDirectoryCreateRequest =
new OMDirectoryCreateRequest(modifiedOmRequest, getBucketLayout());

omDirectoryCreateRequest.setUGI(UserGroupInformation.getCurrentUser());
OMClientResponse omClientResponse =
omDirectoryCreateRequest.validateAndUpdateCache(ozoneManager, 100L);

Expand Down Expand Up @@ -222,6 +224,7 @@ public void testValidateAndUpdateCacheWithNamespaceQuotaExceed()

omDirectoryCreateRequest =
new OMDirectoryCreateRequest(modifiedOmRequest, getBucketLayout());
omDirectoryCreateRequest.setUGI(UserGroupInformation.getCurrentUser());
OMClientResponse omClientResponse =
omDirectoryCreateRequest.validateAndUpdateCache(ozoneManager, 100L);

Expand Down Expand Up @@ -310,7 +313,7 @@ public void testValidateAndUpdateCacheWithSubDirectoryInPath()

omDirectoryCreateRequest =
new OMDirectoryCreateRequest(modifiedOmRequest, getBucketLayout());

omDirectoryCreateRequest.setUGI(UserGroupInformation.getCurrentUser());
OMClientResponse omClientResponse =
omDirectoryCreateRequest.validateAndUpdateCache(ozoneManager, 100L);

Expand Down Expand Up @@ -430,6 +433,7 @@ public void testCreateDirectoryOMMetric()

omDirectoryCreateRequest =
new OMDirectoryCreateRequest(modifiedOmRequest, getBucketLayout());
omDirectoryCreateRequest.setUGI(UserGroupInformation.getCurrentUser());

assertEquals(0L, omMetrics.getNumKeys());
OMClientResponse omClientResponse =
Expand Down Expand Up @@ -480,7 +484,7 @@ public void testCreateDirectoryInheritParentDefaultAcls() throws Exception {

omDirectoryCreateRequest =
new OMDirectoryCreateRequest(modifiedOmRequest, getBucketLayout());

omDirectoryCreateRequest.setUGI(UserGroupInformation.getCurrentUser());
OMClientResponse omClientResponse =
omDirectoryCreateRequest.validateAndUpdateCache(ozoneManager, 100L);

Expand Down Expand Up @@ -510,7 +514,7 @@ private void verifyDirectoriesInheritAcls(String volumeName,

List<OzoneAcl> omKeyAcls = omKeyInfo.getAcls();

assertEquals(expectedInheritAcls, omKeyAcls, "Failed to inherit parent acls!,");
assertTrue(omKeyAcls.containsAll(expectedInheritAcls), "Failed to inherit parent acls!,");

prefix = dirName + OZONE_URI_DELIMITER;
expectedInheritAcls = omKeyAcls;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.KeyArgs;
import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OMRequest;
import jakarta.annotation.Nonnull;
import org.apache.hadoop.security.UserGroupInformation;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -169,7 +170,7 @@ public void testValidateAndUpdateCache() throws Exception {
omDirCreateRequestFSO =
new OMDirectoryCreateRequestWithFSO(modifiedOmReq,
BucketLayout.FILE_SYSTEM_OPTIMIZED);

omDirCreateRequestFSO.setUGI(UserGroupInformation.getCurrentUser());
OMClientResponse omClientResponse =
omDirCreateRequestFSO.validateAndUpdateCache(ozoneManager, 100L);

Expand Down Expand Up @@ -209,6 +210,7 @@ public void testValidateAndUpdateCacheWithNamespaceQuotaExceeded()
omDirCreateRequestFSO =
new OMDirectoryCreateRequestWithFSO(modifiedOmReq,
BucketLayout.FILE_SYSTEM_OPTIMIZED);
omDirCreateRequestFSO.setUGI(UserGroupInformation.getCurrentUser());
OMClientResponse omClientResponse =
omDirCreateRequestFSO.validateAndUpdateCache(ozoneManager, 100L);
assertSame(omClientResponse.getOMResponse().getStatus(),
Expand Down Expand Up @@ -317,7 +319,7 @@ public void testValidateAndUpdateCacheWithSubDirectoryInPath()

omDirCreateReqFSO = new OMDirectoryCreateRequestWithFSO(modifiedOmReq,
BucketLayout.FILE_SYSTEM_OPTIMIZED);

omDirCreateReqFSO.setUGI(UserGroupInformation.getCurrentUser());
OMClientResponse omClientResponse =
omDirCreateReqFSO.validateAndUpdateCache(ozoneManager, 100L);

Expand Down Expand Up @@ -570,6 +572,7 @@ public void testCreateDirectoryUptoLimitOfMaxLevels255() throws Exception {

omDirCreateReqFSO = new OMDirectoryCreateRequestWithFSO(modifiedOmReq,
BucketLayout.FILE_SYSTEM_OPTIMIZED);
omDirCreateReqFSO.setUGI(UserGroupInformation.getCurrentUser());

assertEquals(0L, omMetrics.getNumKeys());
OMClientResponse omClientResponse =
Expand Down Expand Up @@ -604,7 +607,7 @@ public void testCreateDirectoryExceedLimitOfMaxLevels255() throws Exception {

omDirCreateReqFSO = new OMDirectoryCreateRequestWithFSO(modifiedOmReq,
BucketLayout.FILE_SYSTEM_OPTIMIZED);

omDirCreateReqFSO.setUGI(UserGroupInformation.getCurrentUser());
assertEquals(0L, omMetrics.getNumKeys());
OMClientResponse omClientResponse =
omDirCreateReqFSO.validateAndUpdateCache(ozoneManager, 100L);
Expand Down Expand Up @@ -643,6 +646,7 @@ public void testCreateDirectoryOMMetric() throws Exception {

omDirCreateReqFSO = new OMDirectoryCreateRequestWithFSO(modifiedOmReq,
BucketLayout.FILE_SYSTEM_OPTIMIZED);
omDirCreateReqFSO.setUGI(UserGroupInformation.getCurrentUser());

assertEquals(0L, omMetrics.getNumKeys());
OMClientResponse omClientResponse =
Expand Down Expand Up @@ -695,7 +699,7 @@ public void testCreateDirectoryInheritParentDefaultAcls() throws Exception {

omDirCreateReqFSO = new OMDirectoryCreateRequestWithFSO(modifiedOmReq,
BucketLayout.FILE_SYSTEM_OPTIMIZED);

omDirCreateReqFSO.setUGI(UserGroupInformation.getCurrentUser());
OMClientResponse omClientResponse =
omDirCreateReqFSO.validateAndUpdateCache(ozoneManager, 100L);
assertSame(omClientResponse.getOMResponse().getStatus(),
Expand Down Expand Up @@ -730,7 +734,7 @@ private void verifyDirectoriesInheritAcls(List<String> dirs,
System.out.println(
" subdir acls : " + omDirInfo + " ==> " + omDirAcls);

assertEquals(expectedInheritAcls, omDirAcls,
assertTrue(omDirAcls.containsAll(expectedInheritAcls),
"Failed to inherit parent DEFAULT acls!");

parentID = omDirInfo.getObjectID();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo;
import org.apache.hadoop.ozone.om.request.OMRequestTestUtils;
import jakarta.annotation.Nonnull;
import org.apache.hadoop.security.UserGroupInformation;
import org.junit.jupiter.api.Test;

import org.apache.hadoop.hdds.protocol.proto.HddsProtos;
Expand Down Expand Up @@ -481,7 +482,7 @@ protected void verifyInheritAcls(List<String> dirs, OmKeyInfo omKeyInfo,

System.out.println(
" subdir acls : " + omDirInfo + " ==> " + omDirAcls);
assertEquals(expectedInheritAcls, omDirAcls,
assertTrue(omDirAcls.containsAll(expectedInheritAcls),
"Failed to inherit parent DEFAULT acls!");

parentID = omDirInfo.getObjectID();
Expand Down Expand Up @@ -513,9 +514,9 @@ protected void verifyInheritAcls(List<String> dirs, OmKeyInfo omKeyInfo,

// Should inherit parent DEFAULT acls
// [user:newUser:rw[ACCESS], group:newGroup:rwl[ACCESS]]
assertEquals(parentDefaultAcl.stream()
assertTrue(keyAcls.containsAll(parentDefaultAcl.stream()
.map(acl -> acl.withScope(OzoneAcl.AclScope.ACCESS))
.collect(Collectors.toList()), keyAcls,
.collect(Collectors.toList())),
"Failed to inherit bucket DEFAULT acls!");
// Should not inherit parent ACCESS acls
assertThat(keyAcls).doesNotContain(parentAccessAcl);
Expand All @@ -529,7 +530,7 @@ protected void verifyInheritAcls(List<String> dirs, OmKeyInfo omKeyInfo,
".snapshot/a/b/keyName,Cannot create key under path reserved for snapshot: .snapshot/",
".snapshot,Cannot create key with reserved name: .snapshot"})
public void testPreExecuteWithInvalidKeyPrefix(String invalidKeyName,
String expectedErrorMessage) {
String expectedErrorMessage) throws IOException {

OMRequest omRequest = createFileRequest(volumeName, bucketName,
invalidKeyName, HddsProtos.ReplicationFactor.ONE,
Expand Down Expand Up @@ -644,8 +645,10 @@ protected OMRequest createFileRequest(
* @return OMFileCreateRequest reference
*/
@Nonnull
protected OMFileCreateRequest getOMFileCreateRequest(OMRequest omRequest) {
return new OMFileCreateRequest(omRequest, getBucketLayout());
protected OMFileCreateRequest getOMFileCreateRequest(OMRequest omRequest) throws IOException {
OMFileCreateRequest request = new OMFileCreateRequest(omRequest, getBucketLayout());
request.setUGI(UserGroupInformation.getCurrentUser());
return request;
}

}
Loading

0 comments on commit a3036f1

Please sign in to comment.