Skip to content

Commit

Permalink
Add test_connection method to Airbyte hook (#16236)
Browse files Browse the repository at this point in the history
  • Loading branch information
msumit committed Jun 4, 2021
1 parent 5e09926 commit 75c91b4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
19 changes: 19 additions & 0 deletions airflow/providers/airbyte/hooks/airbyte.py
Expand Up @@ -112,3 +112,22 @@ def get_job(self, job_id: int) -> Any:
json={"id": job_id},
headers={"accept": "application/json"},
)

def test_connection(self):
"""Tests the Airbyte connection by hitting the health API"""
self.method = 'GET'
try:
res = self.run(
endpoint=f"api/{self.api_version}/health",
headers={"accept": "application/json"},
extra_options={'check_response': False},
)

if res.status_code == 200:
return True, 'Connection successfully tested'
else:
return False, res.text
except Exception as e: # noqa pylint: disable=broad-except
return False, str(e)
finally:
self.method = 'POST'
17 changes: 17 additions & 0 deletions tests/providers/airbyte/hooks/test_airbyte.py
Expand Up @@ -38,6 +38,7 @@ class TestAirbyteHook(unittest.TestCase):
job_id = 1
sync_connection_endpoint = 'http://test-airbyte:8001/api/v1/connections/sync'
get_job_endpoint = 'http://test-airbyte:8001/api/v1/jobs/get'
health_endpoint = 'http://test-airbyte:8001/api/v1/health'
_mock_sync_conn_success_response_body = {'job': {'id': 1}}
_mock_job_status_success_response_body = {'job': {'status': 'succeeded'}}

Expand Down Expand Up @@ -124,3 +125,19 @@ def test_wait_for_job_cancelled(self, mock_get_job):

calls = [mock.call(job_id=self.job_id), mock.call(job_id=self.job_id)]
assert mock_get_job.has_calls(calls)

@requests_mock.mock()
def test_connection_success(self, m):
m.get(self.health_endpoint, status_code=200,)

status, msg = self.hook.test_connection()
assert status is True
assert msg == 'Connection successfully tested'

@requests_mock.mock()
def test_connection_failure(self, m):
m.get(self.health_endpoint, status_code=500, json={"message": "internal server error"})

status, msg = self.hook.test_connection()
assert status is False
assert msg == '{"message": "internal server error"}'

0 comments on commit 75c91b4

Please sign in to comment.