|
| 1 | +from unittest import TestCase, mock |
| 2 | +from unittest.mock import patch |
| 3 | + |
| 4 | +import pytest |
| 5 | +from orjson import orjson |
| 6 | + |
| 7 | +from sync.clients.sync import SyncClient |
| 8 | +from sync.config import APIKey |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture() |
| 12 | +def mock_get_recommendation(request): |
| 13 | + with patch( |
| 14 | + "sync.clients.sync.SyncClient.get_latest_project_recommendation", |
| 15 | + side_effect=get_rec_from_file, |
| 16 | + ): |
| 17 | + yield |
| 18 | + |
| 19 | + |
| 20 | +def get_rec_from_file(): |
| 21 | + with open("tests/test_files/recommendation.json") as rec_in: |
| 22 | + return orjson.loads(rec_in.read()) |
| 23 | + |
| 24 | + |
| 25 | +class TestSync(TestCase): |
| 26 | + @mock.patch("sync.clients.sync.SyncClient._send") |
| 27 | + def test_get_latest_project_config_recommendation(self, mock_send): |
| 28 | + mock_send.return_value = get_rec_from_file() |
| 29 | + test_client = SyncClient("url", APIKey()) |
| 30 | + expected_result = { |
| 31 | + "node_type_id": "i6.xlarge", |
| 32 | + "driver_node_type_id": "i6.xlarge", |
| 33 | + "custom_tags": { |
| 34 | + "sync:project-id": "b9bd7136-7699-4603-9040-c6dc4c914e43", |
| 35 | + "sync:run-id": "e96401da-f64d-4ed0-8ded-db1317f40248", |
| 36 | + "sync:recommendation-id": "e029a220-c6a5-49fd-b7ed-7ea046366741", |
| 37 | + "sync:tenant-id": "352176a7-b605-4cc2-b3b2-ee591715b6b4", |
| 38 | + }, |
| 39 | + "num_workers": 20, |
| 40 | + "spark_conf": {"spark.databricks.isv.product": "sync-gradient"}, |
| 41 | + "spark_version": "13.3.x-scala2.12", |
| 42 | + "runtime_engine": "PHOTON", |
| 43 | + "aws_attributes": { |
| 44 | + "first_on_demand": 1, |
| 45 | + "availability": "SPOT_WITH_FALLBACK", |
| 46 | + "spot_bid_price_percent": 100, |
| 47 | + }, |
| 48 | + } |
| 49 | + |
| 50 | + result = test_client.get_latest_project_config_recommendation("project_id") |
| 51 | + assert result == expected_result |
| 52 | + |
| 53 | + @mock.patch("sync.clients.sync.SyncClient._send") |
| 54 | + def test_get_cluster_definition_and_recommendation(self, mock_send): |
| 55 | + mock_send.return_value = get_rec_from_file() |
| 56 | + test_client = SyncClient("url", APIKey()) |
| 57 | + |
| 58 | + expected_result = { |
| 59 | + "cluster_recommendations": { |
| 60 | + "node_type_id": "i6.xlarge", |
| 61 | + "driver_node_type_id": "i6.xlarge", |
| 62 | + "custom_tags": { |
| 63 | + "sync:project-id": "b9bd7136-7699-4603-9040-c6dc4c914e43", |
| 64 | + "sync:run-id": "e96401da-f64d-4ed0-8ded-db1317f40248", |
| 65 | + "sync:recommendation-id": "e029a220-c6a5-49fd-b7ed-7ea046366741", |
| 66 | + "sync:tenant-id": "352176a7-b605-4cc2-b3b2-ee591715b6b4", |
| 67 | + }, |
| 68 | + "num_workers": 20, |
| 69 | + "spark_conf": {"spark.databricks.isv.product": "sync-gradient"}, |
| 70 | + "spark_version": "13.3.x-scala2.12", |
| 71 | + "runtime_engine": "PHOTON", |
| 72 | + "aws_attributes": { |
| 73 | + "first_on_demand": 1, |
| 74 | + "availability": "SPOT_WITH_FALLBACK", |
| 75 | + "spot_bid_price_percent": 100, |
| 76 | + }, |
| 77 | + }, |
| 78 | + "cluster_definition": { |
| 79 | + "cluster_id": "1234-567890-reef123", |
| 80 | + "spark_context_id": 4020997813441462000, |
| 81 | + "cluster_name": "my-cluster", |
| 82 | + "spark_version": "13.3.x-scala2.12", |
| 83 | + "aws_attributes": { |
| 84 | + "zone_id": "us-west-2c", |
| 85 | + "first_on_demand": 1, |
| 86 | + "availability": "SPOT_WITH_FALLBACK", |
| 87 | + "spot_bid_price_percent": 100, |
| 88 | + "ebs_volume_count": 0, |
| 89 | + }, |
| 90 | + "node_type_id": "i3.xlarge", |
| 91 | + "driver_node_type_id": "i3.xlarge", |
| 92 | + "autotermination_minutes": 120, |
| 93 | + "enable_elastic_disk": False, |
| 94 | + "disk_spec": {"disk_count": 0}, |
| 95 | + "cluster_source": "UI", |
| 96 | + "enable_local_disk_encryption": False, |
| 97 | + "instance_source": {"node_type_id": "i3.xlarge"}, |
| 98 | + "driver_instance_source": {"node_type_id": "i3.xlarge"}, |
| 99 | + "state": "TERMINATED", |
| 100 | + "state_message": "Inactive cluster terminated (inactive for 120 minutes).", |
| 101 | + "start_time": 1618263108824, |
| 102 | + "terminated_time": 1619746525713, |
| 103 | + "last_state_loss_time": 1619739324740, |
| 104 | + "num_workers": 30, |
| 105 | + "default_tags": { |
| 106 | + "Vendor": "Databricks", |
| 107 | + "Creator": "someone@example.com", |
| 108 | + "ClusterName": "my-cluster", |
| 109 | + "ClusterId": "1234-567890-reef123", |
| 110 | + }, |
| 111 | + "creator_user_name": "someone@example.com", |
| 112 | + "termination_reason": { |
| 113 | + "code": "INACTIVITY", |
| 114 | + "parameters": {"inactivity_duration_min": "120"}, |
| 115 | + "type": "SUCCESS", |
| 116 | + }, |
| 117 | + "init_scripts_safe_mode": False, |
| 118 | + "spec": {"spark_version": "13.3.x-scala2.12"}, |
| 119 | + }, |
| 120 | + } |
| 121 | + |
| 122 | + with open("tests/test_files/cluster.json") as cluster_in: |
| 123 | + result = test_client.get_cluster_definition_and_recommendation( |
| 124 | + "project_id", cluster_in.read() |
| 125 | + ) |
| 126 | + assert result == expected_result |
| 127 | + |
| 128 | + @mock.patch("sync.clients.sync.SyncClient.get_latest_project_recommendation") |
| 129 | + def test_get_updated_cluster_defintion(self, mock_send): |
| 130 | + mock_send.return_value = get_rec_from_file() |
| 131 | + test_client = SyncClient("url", APIKey()) |
| 132 | + |
| 133 | + expected_result = { |
| 134 | + "cluster_id": "1234-567890-reef123", |
| 135 | + "spark_context_id": 4020997813441462000, |
| 136 | + "cluster_name": "my-cluster", |
| 137 | + "spark_version": "13.3.x-scala2.12", |
| 138 | + "aws_attributes": { |
| 139 | + "first_on_demand": 1, |
| 140 | + "availability": "SPOT_WITH_FALLBACK", |
| 141 | + "spot_bid_price_percent": 100, |
| 142 | + }, |
| 143 | + "custom_tags": { |
| 144 | + "sync:project-id": "b9bd7136-7699-4603-9040-c6dc4c914e43", |
| 145 | + "sync:run-id": "e96401da-f64d-4ed0-8ded-db1317f40248", |
| 146 | + "sync:recommendation-id": "e029a220-c6a5-49fd-b7ed-7ea046366741", |
| 147 | + "sync:tenant-id": "352176a7-b605-4cc2-b3b2-ee591715b6b4", |
| 148 | + }, |
| 149 | + "node_type_id": "i6.xlarge", |
| 150 | + "driver_node_type_id": "i6.xlarge", |
| 151 | + "autotermination_minutes": 120, |
| 152 | + "enable_elastic_disk": False, |
| 153 | + "disk_spec": {"disk_count": 0}, |
| 154 | + "cluster_source": "UI", |
| 155 | + "enable_local_disk_encryption": False, |
| 156 | + "instance_source": {"node_type_id": "i3.xlarge"}, |
| 157 | + "driver_instance_source": {"node_type_id": "i3.xlarge"}, |
| 158 | + "spark_conf": {"spark.databricks.isv.product": "sync-gradient"}, |
| 159 | + "state": "TERMINATED", |
| 160 | + "state_message": "Inactive cluster terminated (inactive for 120 minutes).", |
| 161 | + "start_time": 1618263108824, |
| 162 | + "terminated_time": 1619746525713, |
| 163 | + "last_state_loss_time": 1619739324740, |
| 164 | + "num_workers": 20, |
| 165 | + "runtime_engine": "PHOTON", |
| 166 | + "default_tags": { |
| 167 | + "Vendor": "Databricks", |
| 168 | + "Creator": "someone@example.com", |
| 169 | + "ClusterName": "my-cluster", |
| 170 | + "ClusterId": "1234-567890-reef123", |
| 171 | + }, |
| 172 | + "creator_user_name": "someone@example.com", |
| 173 | + "termination_reason": { |
| 174 | + "code": "INACTIVITY", |
| 175 | + "parameters": {"inactivity_duration_min": "120"}, |
| 176 | + "type": "SUCCESS", |
| 177 | + }, |
| 178 | + "init_scripts_safe_mode": False, |
| 179 | + "spec": {"spark_version": "13.3.x-scala2.12"}, |
| 180 | + } |
| 181 | + |
| 182 | + with open("tests/test_files/cluster.json") as cluster_in: |
| 183 | + result = test_client.get_updated_cluster_defintion("project_id", cluster_in.read()) |
| 184 | + assert result == expected_result |
0 commit comments