|
|
@@ -32,20 +32,18 @@ from desktop.lib.test_utils import add_to_group, grant_access
|
|
|
from hadoop.pseudo_hdfs4 import is_live_cluster
|
|
|
|
|
|
from libsentry import sentry_site
|
|
|
-from libsentry.api import get_api
|
|
|
-from libsentry.api2 import get_api as get_api2
|
|
|
+from libsentry.api import get_api, API_CACHE
|
|
|
+from libsentry.api2 import get_api as get_api2, API_CACHE as API2_CACHE
|
|
|
from libsentry.conf import is_enabled, HOSTNAME, PORT, SENTRY_CONF_DIR
|
|
|
from libsentry.client import SentryClient
|
|
|
|
|
|
|
|
|
-class TestWithSentry:
|
|
|
+class TestWithSentry(object):
|
|
|
|
|
|
requires_hadoop = True
|
|
|
|
|
|
-
|
|
|
@classmethod
|
|
|
def setup_class(cls):
|
|
|
-
|
|
|
if not is_live_cluster():
|
|
|
raise SkipTest('Sentry tests require a live sentry server')
|
|
|
|
|
|
@@ -61,9 +59,26 @@ class TestWithSentry:
|
|
|
cls.config_path = os.path.join(SENTRY_CONF_DIR.get(), 'sentry-site.xml')
|
|
|
|
|
|
|
|
|
- @classmethod
|
|
|
- def teardown_class(cls):
|
|
|
+ def setUp(self):
|
|
|
+ self.rpc_addresses = ''
|
|
|
+ if sentry_site.get_sentry_server_rpc_addresses() is not None:
|
|
|
+ self.rpc_addresses = ','.join(sentry_site.get_sentry_server_rpc_addresses())
|
|
|
+
|
|
|
+ self.tmpdir = tempfile.mkdtemp()
|
|
|
+ self.resets = [
|
|
|
+ SENTRY_CONF_DIR.set_for_testing(self.tmpdir),
|
|
|
+ ]
|
|
|
+ if API_CACHE is not None:
|
|
|
+ self.resets.append(API_CACHE.set_for_testing(None))
|
|
|
+ if API2_CACHE is not None:
|
|
|
+ self.resets.append(API2_CACHE.set_for_testing(None))
|
|
|
+
|
|
|
+
|
|
|
+ def tearDown(self):
|
|
|
sentry_site.reset()
|
|
|
+ for reset in self.resets:
|
|
|
+ reset()
|
|
|
+ shutil.rmtree(self.tmpdir)
|
|
|
|
|
|
|
|
|
def test_get_collections(self):
|
|
|
@@ -75,84 +90,83 @@ class TestWithSentry:
|
|
|
assert_equal(0, resp.status.value, resp)
|
|
|
|
|
|
|
|
|
- def test_ha_failover(self):
|
|
|
- try:
|
|
|
- tmpdir = tempfile.mkdtemp()
|
|
|
- finish = SENTRY_CONF_DIR.set_for_testing(tmpdir)
|
|
|
- rpc_addresses = ','.join(sentry_site.get_sentry_server_rpc_addresses())
|
|
|
-
|
|
|
- # Test with good-host,bad-host-1,bad-host-2
|
|
|
- xml = self._sentry_site_xml(rpc_addresses='%s,bad-host-1,bad-host-2' % rpc_addresses)
|
|
|
- file(os.path.join(tmpdir, 'sentry-site.xml'), 'w').write(xml)
|
|
|
- sentry_site.reset()
|
|
|
-
|
|
|
- api = get_api(self.user)
|
|
|
- assert_equal('%s,bad-host-1,bad-host-2' % rpc_addresses, ','.join(sentry_site.get_sentry_server_rpc_addresses()))
|
|
|
- resp = api.list_sentry_roles_by_group(groupName='*')
|
|
|
- assert_true(isinstance(resp, list))
|
|
|
-
|
|
|
- api2 = get_api2(self.user, 'solr')
|
|
|
- resp = api2.list_sentry_roles_by_group(groupName='*')
|
|
|
- assert_true(isinstance(resp, list))
|
|
|
-
|
|
|
- # Test with bad-host-1,bad-host-2,good-host
|
|
|
- xml = self._sentry_site_xml(rpc_addresses='bad-host-1,bad-host-2,%s' % rpc_addresses)
|
|
|
- file(os.path.join(tmpdir, 'sentry-site.xml'), 'w').write(xml)
|
|
|
- sentry_site.reset()
|
|
|
-
|
|
|
- api = get_api(self.user)
|
|
|
- assert_equal('bad-host-1,bad-host-2,%s' % rpc_addresses, ','.join(sentry_site.get_sentry_server_rpc_addresses()))
|
|
|
- resp = api.list_sentry_roles_by_group(groupName='*')
|
|
|
- assert_true(isinstance(resp, list))
|
|
|
-
|
|
|
- api2 = get_api2(self.user, 'solr')
|
|
|
- resp = api2.list_sentry_roles_by_group(groupName='*')
|
|
|
- assert_true(isinstance(resp, list))
|
|
|
-
|
|
|
- # Test with bad-host-1,good-host,bad-host-2
|
|
|
- xml = self._sentry_site_xml(rpc_addresses='bad-host-1,%s,bad-host-2' % rpc_addresses)
|
|
|
- file(os.path.join(tmpdir, 'sentry-site.xml'), 'w').write(xml)
|
|
|
- sentry_site.reset()
|
|
|
-
|
|
|
- api = get_api(self.user)
|
|
|
- assert_equal('bad-host-1,%s,bad-host-2' % rpc_addresses, ','.join(sentry_site.get_sentry_server_rpc_addresses()))
|
|
|
- resp = api.list_sentry_roles_by_group(groupName='*')
|
|
|
- assert_true(isinstance(resp, list))
|
|
|
-
|
|
|
- api2 = get_api2(self.user, 'solr')
|
|
|
- resp = api2.list_sentry_roles_by_group(groupName='*')
|
|
|
- assert_true(isinstance(resp, list))
|
|
|
-
|
|
|
- # Test with all bad hosts
|
|
|
- xml = self._sentry_site_xml(rpc_addresses='bad-host-1,bad-host-2')
|
|
|
- file(os.path.join(tmpdir, 'sentry-site.xml'), 'w').write(xml)
|
|
|
- sentry_site.reset()
|
|
|
-
|
|
|
- api = get_api(self.user)
|
|
|
- assert_equal('bad-host-1,bad-host-2', ','.join(sentry_site.get_sentry_server_rpc_addresses()))
|
|
|
- assert_raises(PopupException, api.list_sentry_roles_by_group, '*')
|
|
|
-
|
|
|
- api2 = get_api2(self.user, 'solr')
|
|
|
- assert_raises(PopupException, api2.list_sentry_roles_by_group, '*')
|
|
|
-
|
|
|
- # Test with no rpc hosts and fallback to hostname and port
|
|
|
- xml = self._sentry_site_xml(rpc_addresses='')
|
|
|
- file(os.path.join(tmpdir, 'sentry-site.xml'), 'w').write(xml)
|
|
|
- sentry_site.reset()
|
|
|
-
|
|
|
- api = get_api(self.user)
|
|
|
- assert_false(sentry_site.is_ha_enabled(), sentry_site.get_sentry_server_rpc_addresses())
|
|
|
- assert_true(is_enabled() and HOSTNAME.get() and HOSTNAME.get() != 'localhost')
|
|
|
- resp = api.list_sentry_roles_by_group(groupName='*')
|
|
|
- assert_true(isinstance(resp, list))
|
|
|
-
|
|
|
- api2 = get_api2(self.user, 'solr')
|
|
|
- resp = api2.list_sentry_roles_by_group(groupName='*')
|
|
|
- assert_true(isinstance(resp, list))
|
|
|
- finally:
|
|
|
- sentry_site.reset()
|
|
|
- finish()
|
|
|
- shutil.rmtree(tmpdir)
|
|
|
+ def test_ha_failover_good_bad_bad(self):
|
|
|
+ # Test with good-host,bad-host-1,bad-host-2
|
|
|
+ xml = self._sentry_site_xml(rpc_addresses='%s,bad-host-1,bad-host-2' % self.rpc_addresses)
|
|
|
+ file(os.path.join(self.tmpdir, 'sentry-site.xml'), 'w').write(xml)
|
|
|
+ sentry_site.reset()
|
|
|
+
|
|
|
+ api = get_api(self.user)
|
|
|
+ assert_equal('%s,bad-host-1,bad-host-2' % self.rpc_addresses, ','.join(sentry_site.get_sentry_server_rpc_addresses()))
|
|
|
+ resp = api.list_sentry_roles_by_group(groupName='*')
|
|
|
+ assert_true(isinstance(resp, list))
|
|
|
+
|
|
|
+ api2 = get_api2(self.user, 'solr')
|
|
|
+ resp = api2.list_sentry_roles_by_group(groupName='*')
|
|
|
+ assert_true(isinstance(resp, list))
|
|
|
+
|
|
|
+
|
|
|
+ def test_ha_failover_bad_bad_good(self):
|
|
|
+ # Test with bad-host-1,bad-host-2,good-host
|
|
|
+ xml = self._sentry_site_xml(rpc_addresses='bad-host-1,bad-host-2,%s' % self.rpc_addresses)
|
|
|
+ file(os.path.join(self.tmpdir, 'sentry-site.xml'), 'w').write(xml)
|
|
|
+ sentry_site.reset()
|
|
|
+
|
|
|
+ api = get_api(self.user)
|
|
|
+ assert_equal('bad-host-1,bad-host-2,%s' % self.rpc_addresses, ','.join(sentry_site.get_sentry_server_rpc_addresses()))
|
|
|
+ resp = api.list_sentry_roles_by_group(groupName='*')
|
|
|
+ assert_true(isinstance(resp, list))
|
|
|
+
|
|
|
+ api2 = get_api2(self.user, 'solr')
|
|
|
+ resp = api2.list_sentry_roles_by_group(groupName='*')
|
|
|
+ assert_true(isinstance(resp, list))
|
|
|
+
|
|
|
+
|
|
|
+ def test_ha_failover_bad_good_bad(self):
|
|
|
+ # Test with bad-host-1,good-host,bad-host-2
|
|
|
+ xml = self._sentry_site_xml(rpc_addresses='bad-host-1,%s,bad-host-2' % self.rpc_addresses)
|
|
|
+ file(os.path.join(self.tmpdir, 'sentry-site.xml'), 'w').write(xml)
|
|
|
+ sentry_site.reset()
|
|
|
+
|
|
|
+ api = get_api(self.user)
|
|
|
+ assert_equal('bad-host-1,%s,bad-host-2' % self.rpc_addresses, ','.join(sentry_site.get_sentry_server_rpc_addresses()))
|
|
|
+ resp = api.list_sentry_roles_by_group(groupName='*')
|
|
|
+ assert_true(isinstance(resp, list))
|
|
|
+
|
|
|
+ api2 = get_api2(self.user, 'solr')
|
|
|
+ resp = api2.list_sentry_roles_by_group(groupName='*')
|
|
|
+ assert_true(isinstance(resp, list))
|
|
|
+
|
|
|
+
|
|
|
+ def test_ha_failover_all_bad(self):
|
|
|
+ # Test with all bad hosts
|
|
|
+ xml = self._sentry_site_xml(rpc_addresses='bad-host-1,bad-host-2')
|
|
|
+ file(os.path.join(self.tmpdir, 'sentry-site.xml'), 'w').write(xml)
|
|
|
+ sentry_site.reset()
|
|
|
+
|
|
|
+ api = get_api(self.user)
|
|
|
+ assert_equal('bad-host-1,bad-host-2', ','.join(sentry_site.get_sentry_server_rpc_addresses()))
|
|
|
+ assert_raises(PopupException, api.list_sentry_roles_by_group, '*')
|
|
|
+
|
|
|
+ api2 = get_api2(self.user, 'solr')
|
|
|
+ assert_raises(PopupException, api2.list_sentry_roles_by_group, '*')
|
|
|
+
|
|
|
+
|
|
|
+ def test_no_rpc_hosts(self):
|
|
|
+ # Test with no rpc hosts and fallback to hostname and port
|
|
|
+ xml = self._sentry_site_xml(rpc_addresses='')
|
|
|
+ file(os.path.join(self.tmpdir, 'sentry-site.xml'), 'w').write(xml)
|
|
|
+ sentry_site.reset()
|
|
|
+
|
|
|
+ api = get_api(self.user)
|
|
|
+ assert_false(sentry_site.is_ha_enabled(), sentry_site.get_sentry_server_rpc_addresses())
|
|
|
+ assert_true(is_enabled() and HOSTNAME.get() and HOSTNAME.get() != 'localhost')
|
|
|
+ resp = api.list_sentry_roles_by_group(groupName='*')
|
|
|
+ assert_true(isinstance(resp, list))
|
|
|
+
|
|
|
+ api2 = get_api2(self.user, 'solr')
|
|
|
+ resp = api2.list_sentry_roles_by_group(groupName='*')
|
|
|
+ assert_true(isinstance(resp, list))
|
|
|
|
|
|
|
|
|
def _sentry_site_xml(self, rpc_addresses):
|