-
Notifications
You must be signed in to change notification settings - Fork 14.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create GKEStartKueueJobOperator operator
- Loading branch information
1 parent
01e40ab
commit 9d455b2
Showing
4 changed files
with
261 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,7 @@ | |
GKEDescribeJobOperator, | ||
GKEStartJobOperator, | ||
GKEStartKueueInsideClusterOperator, | ||
GKEStartKueueJobOperator, | ||
GKEStartPodOperator, | ||
) | ||
from airflow.providers.google.cloud.triggers.kubernetes_engine import GKEStartPodTrigger | ||
|
@@ -51,6 +52,7 @@ | |
PROJECT_LOCATION = "test-location" | ||
PROJECT_TASK_ID = "test-task-id" | ||
CLUSTER_NAME = "test-cluster-name" | ||
QUEUE_NAME = "test-queue-name" | ||
|
||
PROJECT_BODY = {"name": "test-name"} | ||
PROJECT_BODY_CREATE_DICT = {"name": "test-name", "initial_node_count": 1} | ||
|
@@ -1009,3 +1011,122 @@ def test_execute_with_impersonation_service_chain_one_element( | |
self.gke_op.execute(context=mock.MagicMock()) | ||
|
||
fetch_cluster_info_mock.assert_called_once() | ||
|
||
|
||
class TestGKEStartKueueJobOperator: | ||
def setup_method(self): | ||
self.gke_op = GKEStartKueueJobOperator( | ||
project_id=TEST_GCP_PROJECT_ID, | ||
location=PROJECT_LOCATION, | ||
cluster_name=CLUSTER_NAME, | ||
task_id=PROJECT_TASK_ID, | ||
name=TASK_NAME, | ||
namespace=NAMESPACE, | ||
image=IMAGE, | ||
queue_name=QUEUE_NAME, | ||
) | ||
self.gke_op.job = mock.MagicMock( | ||
name=TASK_NAME, | ||
namespace=NAMESPACE, | ||
) | ||
|
||
def test_template_fields(self): | ||
assert set(GKEStartJobOperator.template_fields).issubset(GKEStartKueueJobOperator.template_fields) | ||
|
||
@mock.patch.dict(os.environ, {}) | ||
@mock.patch(KUB_JOB_OPERATOR_EXEC) | ||
@mock.patch(TEMP_FILE) | ||
@mock.patch(f"{GKE_CLUSTER_AUTH_DETAILS_PATH}.fetch_cluster_info") | ||
@mock.patch(GKE_HOOK_PATH) | ||
def test_execute(self, mock_hook, fetch_cluster_info_mock, file_mock, exec_mock): | ||
fetch_cluster_info_mock.return_value = (CLUSTER_URL, SSL_CA_CERT) | ||
self.gke_op.execute(context=mock.MagicMock()) | ||
fetch_cluster_info_mock.assert_called_once() | ||
|
||
def test_config_file_throws_error(self): | ||
with pytest.raises(AirflowException): | ||
GKEStartKueueJobOperator( | ||
project_id=TEST_GCP_PROJECT_ID, | ||
location=PROJECT_LOCATION, | ||
cluster_name=CLUSTER_NAME, | ||
task_id=PROJECT_TASK_ID, | ||
name=TASK_NAME, | ||
namespace=NAMESPACE, | ||
image=IMAGE, | ||
config_file="/path/to/alternative/kubeconfig", | ||
) | ||
|
||
@mock.patch.dict(os.environ, {}) | ||
@mock.patch( | ||
"airflow.hooks.base.BaseHook.get_connections", | ||
return_value=[Connection(extra=json.dumps({"keyfile_dict": '{"private_key": "r4nd0m_k3y"}'}))], | ||
) | ||
@mock.patch(KUB_JOB_OPERATOR_EXEC) | ||
@mock.patch(TEMP_FILE) | ||
@mock.patch(f"{GKE_CLUSTER_AUTH_DETAILS_PATH}.fetch_cluster_info") | ||
@mock.patch(GKE_HOOK_PATH) | ||
def test_execute_with_impersonation_service_account( | ||
self, mock_hook, fetch_cluster_info_mock, file_mock, exec_mock, get_con_mock | ||
): | ||
fetch_cluster_info_mock.return_value = (CLUSTER_URL, SSL_CA_CERT) | ||
self.gke_op.impersonation_chain = "[email protected]" | ||
self.gke_op.execute(context=mock.MagicMock()) | ||
fetch_cluster_info_mock.assert_called_once() | ||
|
||
@mock.patch.dict(os.environ, {}) | ||
@mock.patch( | ||
"airflow.hooks.base.BaseHook.get_connections", | ||
return_value=[Connection(extra=json.dumps({"keyfile_dict": '{"private_key": "r4nd0m_k3y"}'}))], | ||
) | ||
@mock.patch(KUB_JOB_OPERATOR_EXEC) | ||
@mock.patch(TEMP_FILE) | ||
@mock.patch(f"{GKE_CLUSTER_AUTH_DETAILS_PATH}.fetch_cluster_info") | ||
@mock.patch(GKE_HOOK_PATH) | ||
def test_execute_with_impersonation_service_chain_one_element( | ||
self, mock_hook, fetch_cluster_info_mock, file_mock, exec_mock, get_con_mock | ||
): | ||
fetch_cluster_info_mock.return_value = (CLUSTER_URL, SSL_CA_CERT) | ||
self.gke_op.impersonation_chain = ["[email protected]"] | ||
self.gke_op.execute(context=mock.MagicMock()) | ||
|
||
fetch_cluster_info_mock.assert_called_once() | ||
|
||
@pytest.mark.db_test | ||
def test_default_gcp_conn_id(self): | ||
gke_op = GKEStartKueueJobOperator( | ||
project_id=TEST_GCP_PROJECT_ID, | ||
location=PROJECT_LOCATION, | ||
cluster_name=CLUSTER_NAME, | ||
task_id=PROJECT_TASK_ID, | ||
name=TASK_NAME, | ||
namespace=NAMESPACE, | ||
image=IMAGE, | ||
queue_name=QUEUE_NAME, | ||
) | ||
gke_op._cluster_url = CLUSTER_URL | ||
gke_op._ssl_ca_cert = SSL_CA_CERT | ||
hook = gke_op.hook | ||
|
||
assert hook.gcp_conn_id == "google_cloud_default" | ||
|
||
@mock.patch( | ||
"airflow.providers.google.common.hooks.base_google.GoogleBaseHook.get_connection", | ||
return_value=Connection(conn_id="test_conn"), | ||
) | ||
def test_gcp_conn_id(self, get_con_mock): | ||
gke_op = GKEStartKueueJobOperator( | ||
project_id=TEST_GCP_PROJECT_ID, | ||
location=PROJECT_LOCATION, | ||
cluster_name=CLUSTER_NAME, | ||
task_id=PROJECT_TASK_ID, | ||
name=TASK_NAME, | ||
namespace=NAMESPACE, | ||
image=IMAGE, | ||
gcp_conn_id="test_conn", | ||
queue_name=QUEUE_NAME, | ||
) | ||
gke_op._cluster_url = CLUSTER_URL | ||
gke_op._ssl_ca_cert = SSL_CA_CERT | ||
hook = gke_op.hook | ||
|
||
assert hook.gcp_conn_id == "test_conn" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters