|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +from __future__ import annotations |
| 19 | + |
| 20 | +from unittest.mock import patch |
| 21 | + |
| 22 | +import pytest |
| 23 | +import yaml |
| 24 | + |
| 25 | +from airflow_breeze.utils.kubernetes_utils import ( |
| 26 | + FORWARDED_NODE_PORT, |
| 27 | + SCRIPTS_CI_KUBERNETES_PATH, |
| 28 | + _render_kind_cluster_config, |
| 29 | + get_kubernetes_port_numbers, |
| 30 | +) |
| 31 | + |
| 32 | +# Rendered config of a cluster created before the switch to single-node clusters — |
| 33 | +# port extraction must keep working for clusters that already exist on disk. |
| 34 | +LEGACY_TWO_NODE_CONFIG = """\ |
| 35 | +kind: Cluster |
| 36 | +apiVersion: kind.x-k8s.io/v1alpha4 |
| 37 | +networking: |
| 38 | + ipFamily: ipv4 |
| 39 | + apiServerAddress: "127.0.0.1" |
| 40 | + apiServerPort: 48366 |
| 41 | +nodes: |
| 42 | + - role: control-plane |
| 43 | + - role: worker |
| 44 | + extraPortMappings: |
| 45 | + - containerPort: 30007 |
| 46 | + hostPort: 18150 |
| 47 | + listenAddress: "127.0.0.1" |
| 48 | + protocol: TCP |
| 49 | +""" |
| 50 | + |
| 51 | +NO_FORWARDED_PORT_CONFIG = """\ |
| 52 | +kind: Cluster |
| 53 | +apiVersion: kind.x-k8s.io/v1alpha4 |
| 54 | +networking: |
| 55 | + ipFamily: ipv4 |
| 56 | + apiServerAddress: "127.0.0.1" |
| 57 | + apiServerPort: 48366 |
| 58 | +nodes: |
| 59 | + - role: control-plane |
| 60 | +""" |
| 61 | + |
| 62 | + |
| 63 | +@pytest.mark.parametrize( |
| 64 | + "config", |
| 65 | + [ |
| 66 | + _render_kind_cluster_config(forwarded_port_number=18150, k8s_api_server_port=48366), |
| 67 | + LEGACY_TWO_NODE_CONFIG, |
| 68 | + ], |
| 69 | + ids=["current-template", "legacy-two-node"], |
| 70 | +) |
| 71 | +@patch("airflow_breeze.utils.kubernetes_utils._get_kind_cluster_config_content", autospec=True) |
| 72 | +def test_get_kubernetes_port_numbers_extracts_ports_from_any_node(mock_get_content, config): |
| 73 | + mock_get_content.return_value = yaml.safe_load(config) |
| 74 | + assert get_kubernetes_port_numbers(python="3.10", kubernetes_version="v1.30.13") == (48366, 18150) |
| 75 | + |
| 76 | + |
| 77 | +@patch("airflow_breeze.utils.kubernetes_utils._get_kind_cluster_config_content", autospec=True) |
| 78 | +def test_get_kubernetes_port_numbers_raises_when_forwarded_port_mapping_is_missing(mock_get_content): |
| 79 | + mock_get_content.return_value = yaml.safe_load(NO_FORWARDED_PORT_CONFIG) |
| 80 | + with pytest.raises(ValueError, match="extraPortMappings"): |
| 81 | + get_kubernetes_port_numbers(python="3.10", kubernetes_version="v1.30.13") |
| 82 | + |
| 83 | + |
| 84 | +def test_nodeport_service_exposes_forwarded_node_port(): |
| 85 | + node_port_service = yaml.safe_load((SCRIPTS_CI_KUBERNETES_PATH / "nodeport.yaml").read_text()) |
| 86 | + assert [port["nodePort"] for port in node_port_service["spec"]["ports"]] == [FORWARDED_NODE_PORT] |
0 commit comments