You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sentry.apache.org by co...@apache.org on 2015/02/25 04:31:58 UTC

incubator-sentry git commit: SENTRY-655: Improve test cases in SentryStoreIntegrationBase (Reviewed by Guoquan Shen)

Repository: incubator-sentry
Updated Branches:
  refs/heads/master 3893e22dd -> 5868e65e3


SENTRY-655: Improve test cases in SentryStoreIntegrationBase (Reviewed by Guoquan Shen)


Project: http://git-wip-us.apache.org/repos/asf/incubator-sentry/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-sentry/commit/5868e65e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-sentry/tree/5868e65e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-sentry/diff/5868e65e

Branch: refs/heads/master
Commit: 5868e65e3b0f6d0ac24b9683fb4969415058194b
Parents: 3893e22
Author: Colin Ma <co...@apache.org>
Authored: Wed Feb 25 11:13:50 2015 +0800
Committer: Colin Ma <co...@apache.org>
Committed: Wed Feb 25 11:13:50 2015 +0800

----------------------------------------------------------------------
 .../service/persistent/DelegateSentryStore.java | 20 +++++++++
 .../persistent/SentryStoreIntegrationBase.java  | 47 +++++++++-----------
 .../persistent/TestDelegateSentryStore.java     | 15 +++++--
 .../TestPrivilegeOperatePersistence.java        | 16 +++----
 4 files changed, 57 insertions(+), 41 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-sentry/blob/5868e65e/sentry-provider/sentry-provider-db/src/main/java/org/apache/sentry/provider/db/generic/service/persistent/DelegateSentryStore.java
----------------------------------------------------------------------
diff --git a/sentry-provider/sentry-provider-db/src/main/java/org/apache/sentry/provider/db/generic/service/persistent/DelegateSentryStore.java b/sentry-provider/sentry-provider-db/src/main/java/org/apache/sentry/provider/db/generic/service/persistent/DelegateSentryStore.java
index b81360b..6061ef2 100644
--- a/sentry-provider/sentry-provider-db/src/main/java/org/apache/sentry/provider/db/generic/service/persistent/DelegateSentryStore.java
+++ b/sentry-provider/sentry-provider-db/src/main/java/org/apache/sentry/provider/db/generic/service/persistent/DelegateSentryStore.java
@@ -34,6 +34,7 @@ import org.apache.sentry.provider.db.SentryAlreadyExistsException;
 import org.apache.sentry.provider.db.SentryGrantDeniedException;
 import org.apache.sentry.provider.db.SentryInvalidInputException;
 import org.apache.sentry.provider.db.SentryNoSuchObjectException;
+import org.apache.sentry.provider.db.service.model.MSentryGMPrivilege;
 import org.apache.sentry.provider.db.service.model.MSentryGroup;
 import org.apache.sentry.provider.db.service.model.MSentryRole;
 import org.apache.sentry.provider.db.service.persistent.CommitContext;
@@ -43,6 +44,7 @@ import org.apache.sentry.provider.db.service.thrift.TSentryGroup;
 import org.apache.sentry.provider.db.service.thrift.TSentryRole;
 import org.apache.sentry.service.thrift.ServiceConstants.ServerConfig;
 
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Joiner;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Strings;
@@ -444,4 +446,22 @@ public class DelegateSentryStore implements SentryStoreLayer {
       throws SentryUserException {
     return SentryPolicyStoreProcessor.getGroupsFromUserName(this.conf, userName);
   }
+
+  @VisibleForTesting
+  void clearAllTables() {
+    boolean rollbackTransaction = true;
+    PersistenceManager pm = null;
+    try {
+      pm = openTransaction();
+      pm.newQuery(MSentryRole.class).deletePersistentAll();
+      pm.newQuery(MSentryGroup.class).deletePersistentAll();
+      pm.newQuery(MSentryGMPrivilege.class).deletePersistentAll();
+      commitUpdateTransaction(pm);
+      rollbackTransaction = false;
+    } finally {
+      if (rollbackTransaction) {
+        rollbackTransaction(pm);
+      }
+    }
+  }
 }

http://git-wip-us.apache.org/repos/asf/incubator-sentry/blob/5868e65e/sentry-provider/sentry-provider-db/src/test/java/org/apache/sentry/provider/db/generic/service/persistent/SentryStoreIntegrationBase.java
----------------------------------------------------------------------
diff --git a/sentry-provider/sentry-provider-db/src/test/java/org/apache/sentry/provider/db/generic/service/persistent/SentryStoreIntegrationBase.java b/sentry-provider/sentry-provider-db/src/test/java/org/apache/sentry/provider/db/generic/service/persistent/SentryStoreIntegrationBase.java
index c65a28f..7951022 100644
--- a/sentry-provider/sentry-provider-db/src/test/java/org/apache/sentry/provider/db/generic/service/persistent/SentryStoreIntegrationBase.java
+++ b/sentry-provider/sentry-provider-db/src/test/java/org/apache/sentry/provider/db/generic/service/persistent/SentryStoreIntegrationBase.java
@@ -21,30 +21,30 @@ import java.io.File;
 
 import org.apache.commons.io.FileUtils;
 import org.apache.hadoop.conf.Configuration;
-import org.apache.sentry.provider.db.generic.service.persistent.SentryStoreLayer;
 import org.apache.sentry.provider.file.PolicyFile;
 import org.apache.sentry.service.thrift.ServiceConstants.ServerConfig;
 import org.junit.After;
-import org.junit.Before;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
 
 import com.google.common.io.Files;
 
 public abstract class SentryStoreIntegrationBase {
-  protected final String[] adminGroups = {"adminGroup"};
-  private File dataDir;
-  protected SentryStoreLayer sentryStore;
-  private PolicyFile policyFile;
-  private File policyFilePath;
+  protected final static String[] adminGroups = { "adminGroup" };
+  private static File dataDir;
+  private static File policyFilePath;
+  private static Configuration conf;
+  protected static DelegateSentryStore sentryStore;
+  protected static PolicyFile policyFile;
 
-  @Before
-  public void setup() throws Exception {
-    Configuration conf = new Configuration(false);
+  @BeforeClass
+  public static void setup() throws Exception {
+    conf = new Configuration(false);
     setup(conf);
-    configure(conf);
-    sentryStore = createSentryStore(conf);
+    sentryStore = new DelegateSentryStore(conf);
   }
 
-  private void setup(Configuration conf) throws Exception {
+  private static void setup(Configuration conf) throws Exception {
     dataDir = new File(Files.createTempDir(), "sentry_policy_db");
     conf.set(ServerConfig.SENTRY_VERIFY_SCHEM_VERSION, "false");
     conf.set(ServerConfig.SENTRY_STORE_JDBC_URL,
@@ -56,14 +56,15 @@ public abstract class SentryStoreIntegrationBase {
     policyFilePath = new File(Files.createTempDir(), "local_policy_file.ini");
     conf.set(ServerConfig.SENTRY_STORE_GROUP_MAPPING_RESOURCE,
         policyFilePath.getPath());
-    policyFile = new PolicyFile();
-    String adminUser = "admin";
-    addGroupsToUser(adminUser, adminGroups);
-    writePolicyFile();
   }
 
   @After
-  public void teardown() {
+  public void clearData() {
+    sentryStore.clearAllTables();
+  }
+
+  @AfterClass
+  public static void teardown() {
     if (sentryStore != null) {
       sentryStore.close();
     }
@@ -75,21 +76,15 @@ public abstract class SentryStoreIntegrationBase {
     }
   }
 
-  public void addGroupsToUser(String user, String... groupNames) {
+  public static void addGroupsToUser(String user, String... groupNames) {
     policyFile.addGroupsToUser(user, groupNames);
   }
 
-  public void writePolicyFile() throws Exception {
+  public static void writePolicyFile() throws Exception {
     policyFile.write(policyFilePath);
   }
 
   public String[] getAdminGroups() {
     return adminGroups;
   }
-
-  public void configure(Configuration conf) throws Exception {
-
-  }
-
-  public abstract SentryStoreLayer createSentryStore(Configuration conf) throws Exception;
 }

http://git-wip-us.apache.org/repos/asf/incubator-sentry/blob/5868e65e/sentry-provider/sentry-provider-db/src/test/java/org/apache/sentry/provider/db/generic/service/persistent/TestDelegateSentryStore.java
----------------------------------------------------------------------
diff --git a/sentry-provider/sentry-provider-db/src/test/java/org/apache/sentry/provider/db/generic/service/persistent/TestDelegateSentryStore.java b/sentry-provider/sentry-provider-db/src/test/java/org/apache/sentry/provider/db/generic/service/persistent/TestDelegateSentryStore.java
index c7c6af7..751bc3f 100644
--- a/sentry-provider/sentry-provider-db/src/test/java/org/apache/sentry/provider/db/generic/service/persistent/TestDelegateSentryStore.java
+++ b/sentry-provider/sentry-provider-db/src/test/java/org/apache/sentry/provider/db/generic/service/persistent/TestDelegateSentryStore.java
@@ -22,18 +22,25 @@ import static junit.framework.Assert.fail;
 
 import java.util.Set;
 
-import org.apache.hadoop.conf.Configuration;
 import org.apache.sentry.provider.db.SentryAlreadyExistsException;
 import org.apache.sentry.provider.db.SentryNoSuchObjectException;
+import org.apache.sentry.provider.file.PolicyFile;
+import org.junit.Before;
 import org.junit.Test;
 
 import com.google.common.collect.Sets;
 
 public class TestDelegateSentryStore extends SentryStoreIntegrationBase{
   private static final String SEARCH = "solr";
-  @Override
-  public SentryStoreLayer createSentryStore(Configuration conf) throws Exception {
-    return new DelegateSentryStore(conf);
+
+  @Before
+  public void configure() throws Exception {
+    /**
+     * add the admin user to admin groups
+     */
+    policyFile = new PolicyFile();
+    addGroupsToUser("admin", getAdminGroups());
+    writePolicyFile();
   }
 
   @Test

http://git-wip-us.apache.org/repos/asf/incubator-sentry/blob/5868e65e/sentry-provider/sentry-provider-db/src/test/java/org/apache/sentry/provider/db/generic/service/persistent/TestPrivilegeOperatePersistence.java
----------------------------------------------------------------------
diff --git a/sentry-provider/sentry-provider-db/src/test/java/org/apache/sentry/provider/db/generic/service/persistent/TestPrivilegeOperatePersistence.java b/sentry-provider/sentry-provider-db/src/test/java/org/apache/sentry/provider/db/generic/service/persistent/TestPrivilegeOperatePersistence.java
index 5dc5fde..8893391 100644
--- a/sentry-provider/sentry-provider-db/src/test/java/org/apache/sentry/provider/db/generic/service/persistent/TestPrivilegeOperatePersistence.java
+++ b/sentry-provider/sentry-provider-db/src/test/java/org/apache/sentry/provider/db/generic/service/persistent/TestPrivilegeOperatePersistence.java
@@ -23,15 +23,14 @@ import static junit.framework.Assert.fail;
 import java.util.Arrays;
 import java.util.List;
 
-import org.apache.hadoop.conf.Configuration;
 import org.apache.sentry.core.common.Authorizable;
 import org.apache.sentry.core.model.search.Collection;
 import org.apache.sentry.core.model.search.Field;
 import org.apache.sentry.core.model.search.SearchConstants;
 import org.apache.sentry.provider.db.SentryGrantDeniedException;
-import org.apache.sentry.provider.db.generic.service.persistent.DelegateSentryStore;
-import org.apache.sentry.provider.db.generic.service.persistent.SentryStoreLayer;
 import org.apache.sentry.provider.db.generic.service.persistent.PrivilegeObject.Builder;
+import org.apache.sentry.provider.file.PolicyFile;
+import org.junit.Before;
 import org.junit.Test;
 
 import com.google.common.collect.Sets;
@@ -54,21 +53,16 @@ public class TestPrivilegeOperatePersistence extends SentryStoreIntegrationBase
   private static final String FIELD_NAME = "field1";
   private static final String NOT_FIELD_NAME = "not_field1";
 
-  @Override
-  public void configure(Configuration conf) throws Exception {
+  @Before
+  public void configure() throws Exception {
     /**
      * add the solr user to admin groups
      */
+    policyFile = new PolicyFile();
     addGroupsToUser(ADMIN_USER, getAdminGroups());
     writePolicyFile();
   }
 
-  @Override
-  public SentryStoreLayer createSentryStore(Configuration conf)
-      throws Exception {
-    return new DelegateSentryStore(conf);
-  }
-
   /**
    * Grant query privilege to role r1
    */