Просмотр исходного кода

HUE-8758 [connector] Fix TestCheckConfig suit and make it run all the
time

Instead of only when use connector is enabled.

Romain 5 лет назад
Родитель
Сommit
86d3677c11

+ 7 - 0
apps/useradmin/src/useradmin/organization.py

@@ -156,6 +156,13 @@ class UserManager(BaseUserManager):
     if kwargs.get('username'):
       kwargs['email'] = kwargs.pop('username')
 
+    request = CrequestMiddleware.get_request()
+
+    if request and hasattr(request, 'user') and type(request.user._wrapped) is not object:  # Avoid infinite recursion
+      queryset = queryset.filter(
+        organization=request.user.organization
+      )
+
     return super(UserManager, self).get(*args, **kwargs)
 
   def order_by(self, *args, **kwargs):

+ 38 - 5
desktop/libs/notebook/src/notebook/conf.py

@@ -312,8 +312,6 @@ def _default_interpreters(user):
 
 
 def config_validator(user):
-  from notebook.models import _excute_test_query
-
   res = []
 
   if not has_connectors():
@@ -327,10 +325,10 @@ def config_validator(user):
 
   for interpreter in get_ordered_interpreters(user=user):
     if interpreter.get('is_sql'):
-      connector = interpreter['type']
+      connector_id = interpreter['type']
 
       try:
-        response = _excute_test_query(client, connector)
+        response = _excute_test_query(client, connector_id)
         data = json.loads(response.content)
 
         if data['status'] != 0:
@@ -344,9 +342,44 @@ def config_validator(user):
         LOG.exception(msg)
         res.append(
           (
-            '%(nice_name)s - %(dialect)s (%(type)s)' % interpreter,
+            '%(name)s - %(dialect)s (%(type)s)' % interpreter,
             _(msg) + (' %s' % trace[:100] + ('...' if len(trace) > 50 else ''))
           )
         )
 
   return res
+
+
+def _excute_test_query(client, connector_id):
+  '''
+  Helper utils until the API gets simplified.
+  '''
+  notebook_json = """
+    {
+      "selectedSnippet": "hive",
+      "showHistory": false,
+      "description": "Test Hive Query",
+      "name": "Test Hive Query",
+      "sessions": [
+          {
+              "type": "hive",
+              "properties": [],
+              "id": null
+          }
+      ],
+      "type": "hive",
+      "id": null,
+      "snippets": [{"id":"2b7d1f46-17a0-30af-efeb-33d4c29b1055","type":"%(connector_id)s","status":"running","statement":"select * from web_logs","properties":{"settings":[],"variables":[],"files":[],"functions":[]},"result":{"id":"b424befa-f4f5-8799-a0b4-79753f2552b1","type":"table","handle":{"log_context":null,"statements_count":1,"end":{"column":21,"row":0},"statement_id":0,"has_more_statements":false,"start":{"column":0,"row":0},"secret":"rVRWw7YPRGqPT7LZ/TeFaA==an","has_result_set":true,"statement":"select * from web_logs","operation_type":0,"modified_row_count":null,"guid":"7xm6+epkRx6dyvYvGNYePA==an"}},"lastExecuted": 1462554843817,"database":"default"}],
+      "uuid": "d9efdee1-ef25-4d43-b8f9-1a170f69a05a"
+  }
+  """ % {
+    'connector_id': connector_id
+  }
+
+  return client.post(
+    reverse('notebook:api_sample_data', kwargs={'database': 'default', 'table': 'default'}), {
+      'notebook': notebook_json,
+      'snippet': json.dumps(json.loads(notebook_json)['snippets'][0]),
+      'is_async': json.dumps(True),
+      'operation': json.dumps('hello')
+  })

+ 24 - 31
desktop/libs/notebook/src/notebook/conf_tests.py

@@ -79,10 +79,7 @@ class TestInterpreterConfig(unittest.TestCase):
       assert_true(interpreters, interpreters)
       assert_true(all(['dialect_properties' in interpreter for interpreter in interpreters]), interpreters)
       assert_true(
-        any([
-          interpreter.get('dialect_properties').get('sql_identifier_quote')
-          for interpreter in interpreters
-        ]),
+        any([interpreter.get('dialect_properties').get('sql_identifier_quote') for interpreter in interpreters]),
         interpreters
       )
 
@@ -101,9 +98,6 @@ class TestCheckConfig():
 
   @classmethod
   def setUpClass(cls):
-    if not ENABLE_CONNECTORS.get():  # Skip for now
-      raise SkipTest
-
     cls._class_resets = [
       ENABLE_CONNECTORS.set_for_testing(True),
     ]
@@ -113,30 +107,29 @@ class TestCheckConfig():
     for reset in cls._class_resets:
       reset()
 
-    update_app_permissions()
-
-
-  @patch('desktop.lib.connectors.models.CONNECTOR_INSTANCES', None)
   @patch('notebook.conf.has_connectors', return_value=True)
   def test_config_validator(self, has_connectors):
 
-    with patch('desktop.lib.connectors.models.CONNECTORS.get') as CONNECTORS:
-      CONNECTORS.return_value = {
-        'hive-1': Mock(
-          NICE_NAME=Mock(get=Mock(return_value='Hive')),
-          DIALECT=Mock(get=Mock(return_value='hive')),
-          INTERFACE=Mock(get=Mock(return_value='hiveserver2')),
-          SETTINGS=Mock(get=Mock(return_value=[{"name": "server_host", "value": "gethue"}, {"name": "server_port", "value": "10000"}])),
-        )
-      }
-
-      update_app_permissions()
-
-      connectors = _get_installed_connectors(user=self.user)
-      assert_true(connectors, connectors)
-
-      warnings = config_validator(user=self.user)
-
-      assert_true(warnings, warnings)
-      assert_equal('Hive - hive (hive-1)', warnings[0][0])
-      assert_true('Testing the connector connection failed' in warnings[0][1], warnings)
+    with patch('desktop.lib.connectors.api._get_installed_connectors') as _get_installed_connectors:
+      with patch('notebook.conf._excute_test_query') as _excute_test_query:
+        _get_installed_connectors.return_value = [{
+            'nice_name': 'Hive',
+            'name': 'hive-1',
+            'dialect': 'hive',
+            'category': 'editor',
+            'is_sql': True,
+            'interface': 'hiveserver2',
+            'settings': {},
+            'dialect_properties': {'sql_identifier_quote': '`',},
+          }
+        ]
+        _excute_test_query.side_effect = Exception('')
+
+        connectors = _get_installed_connectors(user=self.user)
+        assert_true(connectors, connectors)
+
+        warnings = config_validator(user=self.user)
+
+        assert_true(warnings, warnings)
+        assert_equal('Hive - hive (hive-1)', warnings[0][0])
+        assert_true('Testing the connector connection failed' in warnings[0][1], warnings)

+ 0 - 35
desktop/libs/notebook/src/notebook/models.py

@@ -492,41 +492,6 @@ def _get_editor_type(editor_id):
   return document.type.rsplit('-', 1)[-1]
 
 
-def _excute_test_query(client, interpreter):
-  '''
-  Helper utils until the API gets simplified.
-  '''
-  notebook_json = """
-    {
-      "selectedSnippet": "hive",
-      "showHistory": false,
-      "description": "Test Hive Query",
-      "name": "Test Hive Query",
-      "sessions": [
-          {
-              "type": "hive",
-              "properties": [],
-              "id": null
-          }
-      ],
-      "type": "hive",
-      "id": null,
-      "snippets": [{"id":"2b7d1f46-17a0-30af-efeb-33d4c29b1055","type":"%(connector)s","status":"running","statement":"select * from web_logs","properties":{"settings":[],"variables":[],"files":[],"functions":[]},"result":{"id":"b424befa-f4f5-8799-a0b4-79753f2552b1","type":"table","handle":{"log_context":null,"statements_count":1,"end":{"column":21,"row":0},"statement_id":0,"has_more_statements":false,"start":{"column":0,"row":0},"secret":"rVRWw7YPRGqPT7LZ/TeFaA==an","has_result_set":true,"statement":"select * from web_logs","operation_type":0,"modified_row_count":null,"guid":"7xm6+epkRx6dyvYvGNYePA==an"}},"lastExecuted": 1462554843817,"database":"default"}],
-      "uuid": "d9efdee1-ef25-4d43-b8f9-1a170f69a05a"
-  }
-  """ % {
-    'connector': interpreter
-  }
-
-  return client.post(
-    reverse('notebook:api_sample_data', kwargs={'database': 'default', 'table': 'default'}), {
-      'notebook': notebook_json,
-      'snippet': json.dumps(json.loads(notebook_json)['snippets'][0]),
-      'is_async': json.dumps(True),
-      'operation': json.dumps('hello')
-  })
-
-
 class ApiWrapper(object):
   def __init__(self, request, snippet):
     self.request = request