Skip to content

Commit

Permalink
[knox_jwt] Improve unit tests for Knox JWT HA support (#3650)
Browse files Browse the repository at this point in the history
  • Loading branch information
Harshg999 authored Mar 14, 2024
1 parent 57523a1 commit 08037bd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
1 change: 1 addition & 0 deletions desktop/core/src/desktop/lib/sdxaas/knox_jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def handle_knox_ha():
knox_urls_list = knox_urls.split(',')

for k_url in knox_urls_list:
k_url = k_url.strip(' ')
try:
res = requests.get(k_url.rstrip('/') + _KNOX_TOKEN_API, auth=auth_handler, verify=False)
except Exception as e:
Expand Down
38 changes: 27 additions & 11 deletions desktop/core/src/desktop/lib/sdxaas/knox_jwt_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
import unittest

from unittest.mock import patch, Mock
from nose.tools import assert_equal, assert_raises

from desktop.conf import SDXAAS
from desktop.lib.sdxaas.knox_jwt import handle_knox_ha, fetch_jwt
from desktop.lib.exceptions_renderable import PopupException

if sys.version_info[0] > 2:
from unittest.mock import patch, Mock
else:
from mock import patch, Mock


def test_handle_knox_ha():
with patch('desktop.lib.sdxaas.knox_jwt.requests_kerberos.HTTPKerberosAuth') as HTTPKerberosAuth:
Expand All @@ -37,33 +30,56 @@ def test_handle_knox_ha():

# Non-HA mode
reset = SDXAAS.TOKEN_URL.set_for_testing('https://knox-gateway0.gethue.com:8443/dl-name/kt-kerberos/')

try:
knox_url = handle_knox_ha()

assert_equal(knox_url, 'https://knox-gateway0.gethue.com:8443/dl-name/kt-kerberos/')
assert_equal(requests_get.call_count, 0) # Simply returning the URL string
finally:
reset()
requests_get.reset_mock()

# HA mode - where first URL sends 200 status code
# HA mode - When gateway0 is healthy and gateway1 is unhealthy
requests_get.side_effect = [Mock(status_code=200), Mock(status_code=404)]
reset = SDXAAS.TOKEN_URL.set_for_testing(
'https://knox-gateway0.gethue.com:8443/dl-name/kt-kerberos/, https://knox-gateway1.gethue.com:8443/dl-name/kt-kerberos/')

try:
knox_url = handle_knox_ha()

assert_equal(knox_url, 'https://knox-gateway0.gethue.com:8443/dl-name/kt-kerberos/')
assert_equal(requests_get.call_count, 1)
finally:
reset()
requests_get.reset_mock()

# HA mode - When gateway0 is unhealthy and gateway1 is healthy
requests_get.side_effect = [Mock(status_code=404), Mock(status_code=200)]
reset = SDXAAS.TOKEN_URL.set_for_testing(
'https://knox-gateway0.gethue.com:8443/dl-name/kt-kerberos/, https://knox-gateway1.gethue.com:8443/dl-name/kt-kerberos/')

# When no Knox URL is healthy
try:
knox_url = handle_knox_ha()

assert_equal(knox_url, 'https://knox-gateway1.gethue.com:8443/dl-name/kt-kerberos/')
assert_equal(requests_get.call_count, 2)
finally:
reset()
requests_get.reset_mock()

# When both gateway0 and gateway1 are unhealthy
requests_get.return_value = Mock(status_code=404)
reset = SDXAAS.TOKEN_URL.set_for_testing(
'https://knox-gateway0.gethue.com:8443/dl-name/kt-kerberos/, https://knox-gateway1.gethue.com:8443/dl-name/kt-kerberos/')

try:
knox_url = handle_knox_ha()

assert_equal(knox_url, None)
assert_equal(requests_get.call_count, 2)
finally:
reset()
requests_get.reset_mock()


def test_fetch_jwt():
Expand Down

0 comments on commit 08037bd

Please sign in to comment.