Эх сурвалжийг харах

[raz_adls] Improve ADLS mapping and cleaner tests

- Decouple relative_path from access_type mapping
- Separate tests for relative_path conditions
- Cleaner tests with removed redundancy
Harshg999 4 жил өмнө
parent
commit
8119f7d497

+ 24 - 19
desktop/core/src/desktop/lib/raz/raz_client.py

@@ -174,16 +174,15 @@ class RazClient(object):
 
 
   def _make_adls_request(self, request_data, method, path, url_params, resource_path):
-    storage_account = path.netloc.split('.')[0]
     resource_path = resource_path.split('/', 1)
 
+    storage_account = path.netloc.split('.')[0]
     container = resource_path[0]
-    relative_path = "/"
 
-    if len(resource_path) == 2:
-      relative_path += resource_path[1]
+    relative_path = "/"
+    relative_path = self._handle_relative_path(method, url_params, resource_path, relative_path)
 
-    req_params = self.handle_adls_req_mapping(method, url_params, relative_path)
+    access_type = self.handle_adls_req_mapping(method, url_params)
 
     request_data.update({
       "clientType": "adls",
@@ -191,33 +190,39 @@ class RazClient(object):
         "resource": {
           "storageaccount": storage_account,
           "container": container,
-          "relativepath": req_params.get('relative_path'),
+          "relativepath": relative_path,
         },
-        "action": req_params.get('access_type'),
-        "accessTypes": [req_params.get('access_type')]
+        "action": access_type,
+        "accessTypes": [access_type]
       }
     })
 
 
-  def handle_adls_req_mapping(self, method, params, relative_path):
-    if method == 'HEAD':
-      access_type = 'get-status' if params.get('action') == 'getStatus' else ''
+  def _handle_relative_path(self, method, params, resource_path, relative_path,):
+    if len(resource_path) == 2:
+      relative_path += resource_path[1]
 
-    if method == 'PATCH':
-      if params.get('action') == 'append' or params.get('action') == 'flush':
-        access_type = 'write'
+    if relative_path == "/" and method == 'GET' and params.get('resource') == 'filesystem' and params.get('directory'):
+      relative_path += lib_urlunquote(params['directory'])
 
-      if params.get('action') == 'setAccessControl':
-        access_type = 'set-permission'
+    return relative_path
 
 
+  def handle_adls_req_mapping(self, method, params):
+    if method == 'HEAD':
+      access_type = 'get-status' if params.get('action') == 'getStatus' else ''
+
     if method == 'DELETE':
       access_type = 'delete-recursive' if params.get('recursive') == 'true' else 'delete'
 
     if method == 'GET':
       access_type = 'list' if params.get('resource') == 'filesystem' else 'read'
-      if params.get('directory'):
-        relative_path += lib_urlunquote(params['directory'])
+
+    if method == 'PATCH':
+      if params.get('action') in ('append', 'flush'):
+        access_type = 'write'
+      elif params.get('action') == 'setAccessControl':
+        access_type = 'set-permission'
 
     if method == 'PUT':
       if params.get('resource') == 'file':
@@ -227,7 +232,7 @@ class RazClient(object):
       else:
         access_type = 'rename-source'
 
-    return {'access_type': access_type, 'relative_path': relative_path}
+    return access_type
 
 
   def _make_s3_request(self, request_data, request_headers, method, params, headers, url_params, endpoint, resource_path):

+ 49 - 44
desktop/core/src/desktop/lib/raz/raz_client_test.py

@@ -180,7 +180,6 @@ class RazClientTest(unittest.TestCase):
 
 
   def test_handle_adls_action_types_mapping(self):
-
     client = RazClient(self.raz_url, self.raz_token, username=self.username, service="adls", service_name="cm_adls", cluster_name="cl1")
 
     # List directory
@@ -188,110 +187,116 @@ class RazClientTest(unittest.TestCase):
     relative_path = '/'
     url_params = {'directory': 'user%2Fcsso_hueuser', 'resource': 'filesystem', 'recursive': 'false'}
 
-    response = client.handle_adls_req_mapping(method, url_params, relative_path)
-
-    assert_equal(response['access_type'], 'list')
-    assert_equal(response['relative_path'], '/user/csso_hueuser')
+    access_type = client.handle_adls_req_mapping(method, url_params)
+    assert_equal(access_type, 'list')
 
     # Stats
     method = 'HEAD'
     relative_path = '/user/csso_hueuser'
     url_params = {'action': 'getStatus'}
 
-    response = client.handle_adls_req_mapping(method, url_params, relative_path)
-
-    assert_equal(response['access_type'], 'get-status')
-    assert_equal(response['relative_path'], '/user/csso_hueuser')
+    access_type = client.handle_adls_req_mapping(method, url_params)
+    assert_equal(access_type, 'get-status')
 
     # Delete path
     method = 'DELETE'
     relative_path = '/user/csso_hueuser/test_dir/customer.csv'
     url_params = {}
 
-    response = client.handle_adls_req_mapping(method, url_params, relative_path)
-
-    assert_equal(response['access_type'], 'delete')
-    assert_equal(response['relative_path'], '/user/csso_hueuser/test_dir/customer.csv')
+    access_type = client.handle_adls_req_mapping(method, url_params)
+    assert_equal(access_type, 'delete')
 
     # Delete with recursive as true
     method = 'DELETE'
     relative_path = '/user/csso_hueuser/test_dir'
     url_params = {'recursive': 'true'}
 
-    response = client.handle_adls_req_mapping(method, url_params, relative_path)
-
-    assert_equal(response['access_type'], 'delete-recursive')
-    assert_equal(response['relative_path'], '/user/csso_hueuser/test_dir')
+    access_type = client.handle_adls_req_mapping(method, url_params)
+    assert_equal(access_type, 'delete-recursive')
 
     # Create directory
     method = 'PUT'
     relative_path = '/user/csso_hueuser/test_dir'
     url_params = {'resource': 'directory'}
 
-    response = client.handle_adls_req_mapping(method, url_params, relative_path)
-
-    assert_equal(response['access_type'], 'create-directory')
-    assert_equal(response['relative_path'], '/user/csso_hueuser/test_dir')
+    access_type = client.handle_adls_req_mapping(method, url_params)
+    assert_equal(access_type, 'create-directory')
 
     # Create file
     method = 'PUT'
     relative_path = '/user/csso_hueuser/customers.csv'
     url_params = {'resource': 'file'}
 
-    response = client.handle_adls_req_mapping(method, url_params, relative_path)
-
-    assert_equal(response['access_type'], 'create-file')
-    assert_equal(response['relative_path'], '/user/csso_hueuser/customers.csv')
+    access_type = client.handle_adls_req_mapping(method, url_params)
+    assert_equal(access_type, 'create-file')
 
     # Append
     method = 'PATCH'
     relative_path = '/user/csso_hueuser/customers.csv'
     url_params = {'action': 'append'}
 
-    response = client.handle_adls_req_mapping(method, url_params, relative_path)
-
-    assert_equal(response['access_type'], 'write')
-    assert_equal(response['relative_path'], '/user/csso_hueuser/customers.csv')
+    access_type = client.handle_adls_req_mapping(method, url_params)
+    assert_equal(access_type, 'write')
 
     # Flush
     method = 'PATCH'
     relative_path = '/user/csso_hueuser/customers.csv'
     url_params = {'action': 'flush'}
 
-    response = client.handle_adls_req_mapping(method, url_params, relative_path)
-
-    assert_equal(response['access_type'], 'write')
-    assert_equal(response['relative_path'], '/user/csso_hueuser/customers.csv')
+    access_type = client.handle_adls_req_mapping(method, url_params)
+    assert_equal(access_type, 'write')
 
     # Chmod
     method = 'PATCH'
     relative_path = '/user/csso_hueuser/customers.csv'
     url_params = {'action': 'setAccessControl'}
 
-    response = client.handle_adls_req_mapping(method, url_params, relative_path)
-
-    assert_equal(response['access_type'], 'set-permission')
-    assert_equal(response['relative_path'], '/user/csso_hueuser/customers.csv')
+    access_type = client.handle_adls_req_mapping(method, url_params)
+    assert_equal(access_type, 'set-permission')
 
     # Rename
     method = 'PUT'
     relative_path = '/user/csso_hueuser/old_dir' # First call to fetch SAS to sign header path
     url_params = {}
 
-    response = client.handle_adls_req_mapping(method, url_params, relative_path)
-
-    assert_equal(response['access_type'], 'rename-source')
-    assert_equal(response['relative_path'], '/user/csso_hueuser/old_dir')
+    access_type = client.handle_adls_req_mapping(method, url_params)
+    assert_equal(access_type, 'rename-source')
 
     method = 'PUT'
     relative_path = '/user/csso_hueuser/new_dir' 
     headers = {'x-ms-rename-source': '/user/csso_hueuser/old_dir?some_sas_token'} # Second call having signed header path
     url_params = {}
 
-    response = client.handle_adls_req_mapping(method, url_params, relative_path)
+    access_type = client.handle_adls_req_mapping(method, url_params)
+    assert_equal(access_type, 'rename-source')
+
+
+  def test_handle_relative_path(self):
+    client = RazClient(self.raz_url, self.raz_token, username=self.username, service="adls", service_name="cm_adls", cluster_name="cl1")
+
+    # No relative path condition
+    method = 'GET'
+    resource_path = ['gethue-container']
+    url_params = {}
+
+    relative_path = client._handle_relative_path(method, url_params, resource_path, "/")
+    assert_equal(relative_path, "/")
+
+    # When relative path present in URL
+    method = 'GET'
+    resource_path = ['gethue-container', 'user/csso_hueuser/customer.csv']
+    url_params = {}
+
+    relative_path = client._handle_relative_path(method, url_params, resource_path, "/")
+    assert_equal(relative_path, "/user/csso_hueuser/customer.csv")
+
+    # When list operation
+    method = 'GET'
+    resource_path = ['gethue-container']
+    url_params = {'directory': 'user%2Fcsso_hueuser', 'resource': 'filesystem', 'recursive': 'false'}
 
-    assert_equal(response['access_type'], 'rename-source')
-    assert_equal(response['relative_path'], '/user/csso_hueuser/new_dir')
+    relative_path = client._handle_relative_path(method, url_params, resource_path, "/")
+    assert_equal(relative_path, "/user/csso_hueuser")
 
 
   def test_get_raz_client_s3(self):