You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ozone.apache.org by ms...@apache.org on 2021/03/25 05:18:12 UTC

[ozone] 20/37: HDDS-2981 Add unit tests for Proto [de]serialization (#1425)

This is an automated email from the ASF dual-hosted git repository.

msingh pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ozone.git

commit d8dfcc0b95b1da712db8d0db9341f5aea94c1246
Author: llemec <ll...@gmail.com>
AuthorDate: Mon Mar 15 19:16:50 2021 +0100

    HDDS-2981 Add unit tests for Proto [de]serialization (#1425)
---
 .../ozone/om/helpers/TestInstanceHelper.java       | 68 ++++++++++++++++++++++
 .../hadoop/ozone/om/helpers/TestOmPrefixInfo.java  | 65 +++++++++++++++++++--
 2 files changed, 127 insertions(+), 6 deletions(-)

diff --git a/hadoop-ozone/interface-storage/src/test/java/org/apache/hadoop/ozone/om/helpers/TestInstanceHelper.java b/hadoop-ozone/interface-storage/src/test/java/org/apache/hadoop/ozone/om/helpers/TestInstanceHelper.java
new file mode 100644
index 0000000..3758122
--- /dev/null
+++ b/hadoop-ozone/interface-storage/src/test/java/org/apache/hadoop/ozone/om/helpers/TestInstanceHelper.java
@@ -0,0 +1,68 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.hadoop.ozone.om.helpers;
+
+import com.google.protobuf.ByteString;
+import org.apache.hadoop.hdds.protocol.proto.HddsProtos;
+import org.apache.hadoop.ozone.OzoneAcl;
+import org.apache.hadoop.ozone.storage.proto.OzoneManagerStorageProtos;
+
+
+/**
+ * Test TestInstanceHelper.
+ *
+ * Utility methods to create test instances of protobuf related classes
+ */
+public final class TestInstanceHelper {
+
+  private TestInstanceHelper(){
+    super();
+  }
+
+  public static OzoneManagerStorageProtos.OzoneAclInfo buildTestOzoneAclInfo(
+      String aclString){
+    OzoneAcl oacl = OzoneAcl.parseAcl(aclString);
+    ByteString rights = ByteString.copyFrom(oacl.getAclBitSet().toByteArray());
+    return OzoneManagerStorageProtos.OzoneAclInfo.newBuilder()
+        .setType(OzoneManagerStorageProtos.OzoneAclInfo.OzoneAclType.USER)
+        .setName(oacl.getName())
+        .setRights(rights)
+        .setAclScope(OzoneManagerStorageProtos.
+            OzoneAclInfo.OzoneAclScope.ACCESS)
+        .build();
+  }
+
+  public static HddsProtos.KeyValue getDefaultTestMetadata(
+      String key, String value) {
+    return HddsProtos.KeyValue.newBuilder()
+        .setKey(key)
+        .setValue(value)
+        .build();
+  }
+
+  public static OzoneManagerStorageProtos.PersistedPrefixInfo
+      getDefaultTestPrefixInfo(String name, String aclString,
+      HddsProtos.KeyValue metadata) {
+    return OzoneManagerStorageProtos.PersistedPrefixInfo.newBuilder()
+        .setName(name)
+        .addAcls(buildTestOzoneAclInfo(aclString))
+        .addMetadata(metadata)
+        .build();
+  }
+}
diff --git a/hadoop-ozone/interface-storage/src/test/java/org/apache/hadoop/ozone/om/helpers/TestOmPrefixInfo.java b/hadoop-ozone/interface-storage/src/test/java/org/apache/hadoop/ozone/om/helpers/TestOmPrefixInfo.java
index c155659..8f00736 100644
--- a/hadoop-ozone/interface-storage/src/test/java/org/apache/hadoop/ozone/om/helpers/TestOmPrefixInfo.java
+++ b/hadoop-ozone/interface-storage/src/test/java/org/apache/hadoop/ozone/om/helpers/TestOmPrefixInfo.java
@@ -18,7 +18,9 @@
 
 package org.apache.hadoop.ozone.om.helpers;
 
+import org.apache.hadoop.hdds.protocol.proto.HddsProtos;
 import org.apache.hadoop.ozone.OzoneAcl;
+import org.apache.hadoop.ozone.storage.proto.OzoneManagerStorageProtos;
 import org.apache.hadoop.ozone.security.acl.IAccessAuthorizer;
 import org.junit.Assert;
 import org.junit.Test;
@@ -26,6 +28,7 @@ import org.junit.Test;
 import java.util.Collections;
 import java.util.HashMap;
 
+
 import static org.apache.hadoop.ozone.OzoneAcl.AclScope.ACCESS;
 
 /**
@@ -33,13 +36,27 @@ import static org.apache.hadoop.ozone.OzoneAcl.AclScope.ACCESS;
  */
 public class TestOmPrefixInfo {
 
-  @Test
-  public void testCopyObject() {
-    OmPrefixInfo omPrefixInfo = new OmPrefixInfo("/path",
+  public OmPrefixInfo getOmPrefixInfoForTest(String path,
+      IAccessAuthorizer.ACLIdentityType identityType,
+      String identityString,
+      IAccessAuthorizer.ACLType aclType,
+      OzoneAcl.AclScope scope) {
+    return new OmPrefixInfo(path,
         Collections.singletonList(new OzoneAcl(
-        IAccessAuthorizer.ACLIdentityType.USER, "user1",
-        IAccessAuthorizer.ACLType.WRITE, ACCESS)), new HashMap<>(), 10, 100);
+            identityType, identityString,
+            aclType, scope)), new HashMap<>(), 10, 100);
+  }
 
+
+  @Test
+  public void testCopyObject() {
+    String testPath = "/my/custom/path";
+    String username = "myuser";
+    OmPrefixInfo omPrefixInfo = getOmPrefixInfoForTest(testPath,
+        IAccessAuthorizer.ACLIdentityType.USER,
+        username,
+        IAccessAuthorizer.ACLType.WRITE,
+        ACCESS);
     OmPrefixInfo clonePrefixInfo = omPrefixInfo.copyObject();
 
     Assert.assertEquals(omPrefixInfo, clonePrefixInfo);
@@ -47,10 +64,46 @@ public class TestOmPrefixInfo {
 
     // Change acls and check.
     omPrefixInfo.addAcl(new OzoneAcl(
-        IAccessAuthorizer.ACLIdentityType.USER, "user1",
+        IAccessAuthorizer.ACLIdentityType.USER, username,
         IAccessAuthorizer.ACLType.READ, ACCESS));
 
     Assert.assertNotEquals(omPrefixInfo, clonePrefixInfo);
 
   }
+
+  @Test
+  public void testgetFromProtobufOneMetadataOneAcl() {
+    String prefixInfoPath = "/mypath/path";
+    String aclString = "user:myuser:rw";
+    String metakey = "metakey";
+    String metaval = "metaval";
+    HddsProtos.KeyValue metadata = TestInstanceHelper
+        .getDefaultTestMetadata(metakey, metaval);
+    OzoneManagerStorageProtos.PersistedPrefixInfo prefixInfo =
+        TestInstanceHelper.getDefaultTestPrefixInfo(prefixInfoPath,
+            aclString, metadata);
+
+    OmPrefixInfo ompri = OmPrefixInfo.getFromProtobuf(prefixInfo);
+
+    Assert.assertEquals(prefixInfoPath, ompri.getName());
+    Assert.assertEquals(1, ompri.getMetadata().size());
+    Assert.assertEquals(metaval, ompri.getMetadata().get(metakey));
+    Assert.assertEquals(1, ompri.getAcls().size());
+  }
+
+  @Test
+  public void testGetProtobuf() {
+    String testPath = "/my/custom/path";
+    String username = "myuser";
+    OmPrefixInfo omPrefixInfo = getOmPrefixInfoForTest(testPath,
+        IAccessAuthorizer.ACLIdentityType.USER,
+        username, IAccessAuthorizer.ACLType.WRITE,
+        ACCESS);
+    omPrefixInfo.getMetadata().put("key", "value");
+    OzoneManagerStorageProtos.PersistedPrefixInfo pi =
+        omPrefixInfo.getProtobuf();
+    Assert.assertEquals(testPath, pi.getName());
+    Assert.assertEquals(1, pi.getAclsCount());
+    Assert.assertEquals(1, pi.getMetadataCount());
+  }
 }

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@ozone.apache.org
For additional commands, e-mail: commits-help@ozone.apache.org