|
| 1 | +# Copyright 2024 IBM, Red Hat |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from codeflare_sdk.common.kubernetes_cluster import ( |
| 16 | + Authentication, |
| 17 | + KubeConfigFileAuthentication, |
| 18 | + TokenAuthentication, |
| 19 | + config_check, |
| 20 | +) |
| 21 | +from kubernetes import client, config |
| 22 | +import os |
| 23 | +from pathlib import Path |
| 24 | +import pytest |
| 25 | + |
| 26 | +parent = Path(__file__).resolve().parents[4] # project directory |
| 27 | + |
| 28 | + |
| 29 | +def test_token_auth_creation(): |
| 30 | + token_auth = TokenAuthentication(token="token", server="server") |
| 31 | + assert token_auth.token == "token" |
| 32 | + assert token_auth.server == "server" |
| 33 | + assert token_auth.skip_tls == False |
| 34 | + assert token_auth.ca_cert_path == None |
| 35 | + |
| 36 | + token_auth = TokenAuthentication(token="token", server="server", skip_tls=True) |
| 37 | + assert token_auth.token == "token" |
| 38 | + assert token_auth.server == "server" |
| 39 | + assert token_auth.skip_tls == True |
| 40 | + assert token_auth.ca_cert_path == None |
| 41 | + |
| 42 | + os.environ["CF_SDK_CA_CERT_PATH"] = "/etc/pki/tls/custom-certs/ca-bundle.crt" |
| 43 | + token_auth = TokenAuthentication(token="token", server="server", skip_tls=False) |
| 44 | + assert token_auth.token == "token" |
| 45 | + assert token_auth.server == "server" |
| 46 | + assert token_auth.skip_tls == False |
| 47 | + assert token_auth.ca_cert_path == "/etc/pki/tls/custom-certs/ca-bundle.crt" |
| 48 | + os.environ.pop("CF_SDK_CA_CERT_PATH") |
| 49 | + |
| 50 | + token_auth = TokenAuthentication( |
| 51 | + token="token", |
| 52 | + server="server", |
| 53 | + skip_tls=False, |
| 54 | + ca_cert_path=f"{parent}/tests/auth-test.crt", |
| 55 | + ) |
| 56 | + assert token_auth.token == "token" |
| 57 | + assert token_auth.server == "server" |
| 58 | + assert token_auth.skip_tls == False |
| 59 | + assert token_auth.ca_cert_path == f"{parent}/tests/auth-test.crt" |
| 60 | + |
| 61 | + |
| 62 | +def test_token_auth_login_logout(mocker): |
| 63 | + mocker.patch.object(client, "ApiClient") |
| 64 | + |
| 65 | + token_auth = TokenAuthentication( |
| 66 | + token="testtoken", server="testserver:6443", skip_tls=False, ca_cert_path=None |
| 67 | + ) |
| 68 | + assert token_auth.login() == ("Logged into testserver:6443") |
| 69 | + assert token_auth.logout() == ("Successfully logged out of testserver:6443") |
| 70 | + |
| 71 | + |
| 72 | +def test_token_auth_login_tls(mocker): |
| 73 | + mocker.patch.object(client, "ApiClient") |
| 74 | + |
| 75 | + token_auth = TokenAuthentication( |
| 76 | + token="testtoken", server="testserver:6443", skip_tls=True, ca_cert_path=None |
| 77 | + ) |
| 78 | + assert token_auth.login() == ("Logged into testserver:6443") |
| 79 | + token_auth = TokenAuthentication( |
| 80 | + token="testtoken", server="testserver:6443", skip_tls=False, ca_cert_path=None |
| 81 | + ) |
| 82 | + assert token_auth.login() == ("Logged into testserver:6443") |
| 83 | + token_auth = TokenAuthentication( |
| 84 | + token="testtoken", |
| 85 | + server="testserver:6443", |
| 86 | + skip_tls=False, |
| 87 | + ca_cert_path=f"{parent}/tests/auth-test.crt", |
| 88 | + ) |
| 89 | + assert token_auth.login() == ("Logged into testserver:6443") |
| 90 | + |
| 91 | + os.environ["CF_SDK_CA_CERT_PATH"] = f"{parent}/tests/auth-test.crt" |
| 92 | + token_auth = TokenAuthentication( |
| 93 | + token="testtoken", |
| 94 | + server="testserver:6443", |
| 95 | + skip_tls=False, |
| 96 | + ) |
| 97 | + assert token_auth.login() == ("Logged into testserver:6443") |
| 98 | + |
| 99 | + |
| 100 | +def test_config_check_no_config_file(mocker): |
| 101 | + mocker.patch("os.path.expanduser", return_value="/mock/home/directory") |
| 102 | + mocker.patch("os.path.isfile", return_value=False) |
| 103 | + mocker.patch("codeflare_sdk.common.kubernetes_cluster.auth.config_path", None) |
| 104 | + mocker.patch("codeflare_sdk.common.kubernetes_cluster.auth.api_client", None) |
| 105 | + |
| 106 | + with pytest.raises(PermissionError): |
| 107 | + config_check() |
| 108 | + |
| 109 | + |
| 110 | +def test_config_check_with_incluster_config(mocker): |
| 111 | + mocker.patch("os.path.expanduser", return_value="/mock/home/directory") |
| 112 | + mocker.patch("os.path.isfile", return_value=False) |
| 113 | + mocker.patch.dict(os.environ, {"KUBERNETES_PORT": "number"}) |
| 114 | + mocker.patch("kubernetes.config.load_incluster_config", side_effect=None) |
| 115 | + mocker.patch("codeflare_sdk.common.kubernetes_cluster.auth.config_path", None) |
| 116 | + mocker.patch("codeflare_sdk.common.kubernetes_cluster.auth.api_client", None) |
| 117 | + |
| 118 | + result = config_check() |
| 119 | + assert result == None |
| 120 | + |
| 121 | + |
| 122 | +def test_config_check_with_existing_config_file(mocker): |
| 123 | + mocker.patch("os.path.expanduser", return_value="/mock/home/directory") |
| 124 | + mocker.patch("os.path.isfile", return_value=True) |
| 125 | + mocker.patch("kubernetes.config.load_kube_config", side_effect=None) |
| 126 | + mocker.patch("codeflare_sdk.common.kubernetes_cluster.auth.config_path", None) |
| 127 | + mocker.patch("codeflare_sdk.common.kubernetes_cluster.auth.api_client", None) |
| 128 | + |
| 129 | + result = config_check() |
| 130 | + assert result == None |
| 131 | + |
| 132 | + |
| 133 | +def test_config_check_with_config_path_and_no_api_client(mocker): |
| 134 | + mocker.patch( |
| 135 | + "codeflare_sdk.common.kubernetes_cluster.auth.config_path", "/mock/config/path" |
| 136 | + ) |
| 137 | + mocker.patch("codeflare_sdk.common.kubernetes_cluster.auth.api_client", None) |
| 138 | + result = config_check() |
| 139 | + assert result == "/mock/config/path" |
| 140 | + |
| 141 | + |
| 142 | +def test_load_kube_config(mocker): |
| 143 | + mocker.patch.object(config, "load_kube_config") |
| 144 | + kube_config_auth = KubeConfigFileAuthentication( |
| 145 | + kube_config_path="/path/to/your/config" |
| 146 | + ) |
| 147 | + response = kube_config_auth.load_kube_config() |
| 148 | + |
| 149 | + assert ( |
| 150 | + response |
| 151 | + == "Loaded user config file at path %s" % kube_config_auth.kube_config_path |
| 152 | + ) |
| 153 | + |
| 154 | + kube_config_auth = KubeConfigFileAuthentication(kube_config_path=None) |
| 155 | + response = kube_config_auth.load_kube_config() |
| 156 | + assert response == "Please specify a config file path" |
| 157 | + |
| 158 | + |
| 159 | +def test_auth_coverage(): |
| 160 | + abstract = Authentication() |
| 161 | + abstract.login() |
| 162 | + abstract.logout() |
0 commit comments