def test_cancel_benchmark_with_cascade_with_not_found_exception( k8s_execution_engine: K8SExecutionEngine, mock_check_output_with_parent_not_found ): """ Tests that when deleting a benchmark with cascade, an attempt to cascade the deletion is still made even in the case that the scheduled benchmark is not found. E.g. if the user deletes a scheduled benchmark without cascading, they should still be able to delete the scheduled job's spawned jobs. """ k8s_execution_engine.cancel(CLIENT_ID, ACTION_ID, cascade=True) expected_calls = [ # Deletes resources with label action-id=<action_id> call( [ KUBECTL, "delete", JOINED_RESOURCE_TYPES, "--selector", K8SExecutionEngine._create_label_selector(CLIENT_ID, ACTION_ID, as_parent=False), ] ), # Also deletes resources with label parent-action-id=<action_id> call( [ KUBECTL, "delete", JOINED_RESOURCE_TYPES, "--selector", K8SExecutionEngine._create_label_selector(CLIENT_ID, ACTION_ID, as_parent=True), ] ), ] mock_check_output_with_parent_not_found.assert_has_calls(expected_calls)
def test_cancel_benchmark_with_cascade(k8s_execution_engine: K8SExecutionEngine, mock_check_output): k8s_execution_engine.cancel(CLIENT_ID, ACTION_ID, cascade=True) expected_calls = [ # Deletes resources with label action-id=<action_id> call( [ KUBECTL, "delete", JOINED_RESOURCE_TYPES, "--selector", K8SExecutionEngine._create_label_selector(CLIENT_ID, ACTION_ID, as_parent=False), ] ), # Also deletes resources with label parent-action-id=<action_id> call( [ KUBECTL, "delete", JOINED_RESOURCE_TYPES, "--selector", K8SExecutionEngine._create_label_selector(CLIENT_ID, ACTION_ID, as_parent=True), ] ), ] mock_check_output.assert_has_calls(expected_calls)
def test_raise_yaml_exception( k8s_execution_engine: K8SExecutionEngine, valid_fetcher_event: FetcherBenchmarkEvent, mock_subprocess_check_output, mock_fail_create_job_yaml_spec, ): with pytest.raises(ExecutionEngineException): k8s_execution_engine.run(valid_fetcher_event)
def test_cancel_benchmark_without_cascade(k8s_execution_engine: K8SExecutionEngine, mock_check_output): k8s_execution_engine.cancel(CLIENT_ID, ACTION_ID, cascade=False) expected_call = [ KUBECTL, "delete", JOINED_RESOURCE_TYPES, "--selector", K8SExecutionEngine._create_label_selector(CLIENT_ID, ACTION_ID, as_parent=False), ] mock_check_output.assert_called_with(expected_call)
def create_executor(common_kafka_cfg: KafkaServiceConfig, executor_config: ExecutorConfig) -> KafkaService: k8s_engine = K8SExecutionEngine(executor_config) execution_engines = {ExecutorEventHandler.DEFAULT_ENGINE: k8s_engine, K8SExecutionEngine.ENGINE_ID: k8s_engine} kafka_service = create_executor_service(SERVICE_NAME, __version__, common_kafka_cfg, execution_engines) # Add Scheduled Benchmark Handler scheduled_benchmark_handler = ScheduledBenchmarkExecutorEventHandler(k8s_engine) kafka_service.add_callback(scheduled_benchmark_handler, common_kafka_cfg.consumer_topic) return kafka_service
def test_k8s_engine_happy_path( k8s_execution_engine: K8SExecutionEngine, valid_fetcher_event: FetcherBenchmarkEvent, mock_subprocess_check_output, mock_create_job_yaml_spec, ): job = k8s_execution_engine.run(valid_fetcher_event) mock_subprocess_check_output.assert_called_once_with([KUBECTL, "apply", "-f", "-"], input=SOME_YAML_ENCODED) assert job == BenchmarkJob( id=K8SExecutionEngine.JOB_ID_PREFIX + valid_fetcher_event.action_id, extras={K8SExecutionEngine.EXTRA_K8S_YAML: SOME_YAML}, )
def k8s_execution_engine(mock_engine_config: ExecutorConfig) -> K8SExecutionEngine: return K8SExecutionEngine(mock_engine_config)
def test_cancel_with_cascade_fails(k8s_execution_engine: K8SExecutionEngine, mock_check_output_no_resource_found): with pytest.raises(NoResourcesFoundException): k8s_execution_engine.cancel(CLIENT_ID, ACTION_ID, cascade=False)