|
18 | 18 | import os
|
19 | 19 | import re
|
20 | 20 | from click.testing import CliRunner
|
| 21 | +import pickle |
21 | 22 |
|
22 | 23 | parent = Path(__file__).resolve().parents[1]
|
23 | 24 | sys.path.append(str(parent) + "/src")
|
|
65 | 66 | export_env,
|
66 | 67 | )
|
67 | 68 | from codeflare_sdk.cli.codeflare_cli import cli
|
| 69 | +from codeflare_sdk.cli.cli_utils import load_auth |
| 70 | +import codeflare_sdk.cluster.auth as sdk_auth |
68 | 71 |
|
69 | 72 | import openshift
|
70 | 73 | from openshift.selector import Selector
|
@@ -108,6 +111,65 @@ def test_cluster_definition_cli():
|
108 | 111 | )
|
109 | 112 |
|
110 | 113 |
|
| 114 | +def test_login_cli(mocker): |
| 115 | + runner = CliRunner() |
| 116 | + mocker.patch.object(client, "ApiClient") |
| 117 | + k8s_login_command = """ |
| 118 | + login |
| 119 | + --server=testserver:6443 |
| 120 | + --token=testtoken |
| 121 | + """ |
| 122 | + login_result = runner.invoke(cli, k8s_login_command) |
| 123 | + assert login_result.output == "Logged into 'testserver:6443'\n" |
| 124 | + try: |
| 125 | + auth_file_path = os.path.expanduser("~/.codeflare/auth") |
| 126 | + with open(auth_file_path, "rb") as file: |
| 127 | + auth = pickle.load(file) |
| 128 | + except: |
| 129 | + assert 0 == 1 |
| 130 | + assert auth.server == "testserver:6443" |
| 131 | + assert auth.token == "testtoken" |
| 132 | + assert auth.api_client_config.api_key["authorization"] == "testtoken" |
| 133 | + assert auth.api_client_config.verify_ssl |
| 134 | + assert auth.api_client_config.host == "testserver:6443" |
| 135 | + |
| 136 | + |
| 137 | +def test_login_tls_cli(mocker): |
| 138 | + runner = CliRunner() |
| 139 | + mocker.patch.object(client, "ApiClient") |
| 140 | + k8s_tls_login_command = """ |
| 141 | + login |
| 142 | + --server=testserver:6443 |
| 143 | + --token=testtoken |
| 144 | + --insecure-skip-tls-verify=False |
| 145 | + """ |
| 146 | + k8s_skip_tls_login_command = """ |
| 147 | + login |
| 148 | + --server=testserver:6443 |
| 149 | + --token=testtoken |
| 150 | + --insecure-skip-tls-verify=True |
| 151 | + """ |
| 152 | + tls_result = runner.invoke(cli, k8s_tls_login_command) |
| 153 | + skip_tls_result = runner.invoke(cli, k8s_skip_tls_login_command) |
| 154 | + assert ( |
| 155 | + tls_result.output == skip_tls_result.output == "Logged into 'testserver:6443'\n" |
| 156 | + ) |
| 157 | + |
| 158 | + |
| 159 | +def test_logout_cli(mocker): |
| 160 | + runner = CliRunner() |
| 161 | + mocker.patch.object(client, "ApiClient") |
| 162 | + k8s_logout_command = "logout" |
| 163 | + logout_result = runner.invoke(cli, k8s_logout_command) |
| 164 | + assert logout_result.output == "Successfully logged out of 'testserver:6443'\n" |
| 165 | + assert not os.path.exists(os.path.expanduser("~/.codeflare/auth")) |
| 166 | + |
| 167 | + |
| 168 | +def test_load_auth(): |
| 169 | + load_auth() |
| 170 | + assert sdk_auth.api_client is not None |
| 171 | + |
| 172 | + |
111 | 173 | # For mocking openshift client results
|
112 | 174 | fake_res = openshift.Result("fake")
|
113 | 175 |
|
|
0 commit comments