|
| 1 | +import json |
| 2 | +import os |
| 3 | +import shutil |
| 4 | +import subprocess |
| 5 | +import tempfile |
| 6 | +import time |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +import pytest |
| 10 | +import requests |
| 11 | +import test_utils |
| 12 | + |
| 13 | +ROOT_DIR = os.path.join(tempfile.gettempdir(), "workspace") |
| 14 | +REPO_ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../../") |
| 15 | +data_file_zero = os.path.join(REPO_ROOT, "test/pytest/test_data/0.png") |
| 16 | +config_file = os.path.join(REPO_ROOT, "test/resources/config_token.properties") |
| 17 | + |
| 18 | + |
| 19 | +# Set up token plugin |
| 20 | +def get_plugin_jar(): |
| 21 | + new_folder_path = os.path.join(ROOT_DIR, "plugins-path") |
| 22 | + plugin_folder = os.path.join(REPO_ROOT, "plugins") |
| 23 | + os.makedirs(new_folder_path, exist_ok=True) |
| 24 | + os.chdir(plugin_folder) |
| 25 | + subprocess.run(["./gradlew", "formatJava"]) |
| 26 | + result = subprocess.run(["./gradlew", "build"]) |
| 27 | + jar_path = os.path.join(plugin_folder, "endpoints/build/libs") |
| 28 | + jar_file = [file for file in os.listdir(jar_path) if file.endswith(".jar")] |
| 29 | + if jar_file: |
| 30 | + shutil.move( |
| 31 | + os.path.join(jar_path, jar_file[0]), |
| 32 | + os.path.join(new_folder_path, jar_file[0]), |
| 33 | + ) |
| 34 | + os.chdir(REPO_ROOT) |
| 35 | + |
| 36 | + |
| 37 | +# Parse json file and return key |
| 38 | +def read_key_file(type): |
| 39 | + json_file_path = os.path.join(REPO_ROOT, "key_file.json") |
| 40 | + with open(json_file_path) as json_file: |
| 41 | + json_data = json.load(json_file) |
| 42 | + |
| 43 | + options = { |
| 44 | + "management": json_data.get("management", {}).get("key", "NOT_PRESENT"), |
| 45 | + "inference": json_data.get("inference", {}).get("key", "NOT_PRESENT"), |
| 46 | + "token": json_data.get("API", {}).get("key", "NOT_PRESENT"), |
| 47 | + } |
| 48 | + key = options.get(type, "Invalid data type") |
| 49 | + return key |
| 50 | + |
| 51 | + |
| 52 | +@pytest.fixture(scope="module") |
| 53 | +def setup_torchserve(): |
| 54 | + get_plugin_jar() |
| 55 | + MODEL_STORE = os.path.join(ROOT_DIR, "model_store/") |
| 56 | + PLUGIN_STORE = os.path.join(ROOT_DIR, "plugins-path") |
| 57 | + |
| 58 | + Path(test_utils.MODEL_STORE).mkdir(parents=True, exist_ok=True) |
| 59 | + |
| 60 | + test_utils.start_torchserve(no_config_snapshots=True, plugin_folder=PLUGIN_STORE) |
| 61 | + |
| 62 | + key = read_key_file("management") |
| 63 | + header = {"Authorization": f"Bearer {key}"} |
| 64 | + |
| 65 | + params = ( |
| 66 | + ("model_name", "mnist"), |
| 67 | + ("url", "mnist.mar"), |
| 68 | + ("initial_workers", "1"), |
| 69 | + ("synchronous", "true"), |
| 70 | + ) |
| 71 | + response = requests.post( |
| 72 | + "http://localhost:8081/models", params=params, headers=header |
| 73 | + ) |
| 74 | + file_content = Path(f"{REPO_ROOT}/key_file.json").read_text() |
| 75 | + print(file_content) |
| 76 | + |
| 77 | + yield "test" |
| 78 | + |
| 79 | + test_utils.stop_torchserve() |
| 80 | + |
| 81 | + |
| 82 | +@pytest.fixture(scope="module") |
| 83 | +def setup_torchserve_expiration(): |
| 84 | + get_plugin_jar() |
| 85 | + MODEL_STORE = os.path.join(ROOT_DIR, "model_store/") |
| 86 | + PLUGIN_STORE = os.path.join(ROOT_DIR, "plugins-path") |
| 87 | + |
| 88 | + Path(test_utils.MODEL_STORE).mkdir(parents=True, exist_ok=True) |
| 89 | + |
| 90 | + test_utils.start_torchserve( |
| 91 | + snapshot_file=config_file, no_config_snapshots=True, plugin_folder=PLUGIN_STORE |
| 92 | + ) |
| 93 | + |
| 94 | + key = read_key_file("management") |
| 95 | + header = {"Authorization": f"Bearer {key}"} |
| 96 | + |
| 97 | + params = ( |
| 98 | + ("model_name", "mnist"), |
| 99 | + ("url", "mnist.mar"), |
| 100 | + ("initial_workers", "1"), |
| 101 | + ("synchronous", "true"), |
| 102 | + ) |
| 103 | + response = requests.post( |
| 104 | + "http://localhost:8081/models", params=params, headers=header |
| 105 | + ) |
| 106 | + file_content = Path(f"{REPO_ROOT}/key_file.json").read_text() |
| 107 | + print(file_content) |
| 108 | + |
| 109 | + yield "test" |
| 110 | + |
| 111 | + test_utils.stop_torchserve() |
| 112 | + |
| 113 | + |
| 114 | +# Test describe model API with token enabled |
| 115 | +def test_managament_api_with_token(setup_torchserve): |
| 116 | + key = read_key_file("management") |
| 117 | + header = {"Authorization": f"Bearer {key}"} |
| 118 | + response = requests.get("http://localhost:8081/models/mnist", headers=header) |
| 119 | + |
| 120 | + assert response.status_code == 200, "Token check failed" |
| 121 | + |
| 122 | + |
| 123 | +# Test describe model API with incorrect token and no token |
| 124 | +def test_managament_api_with_incorrect_token(setup_torchserve): |
| 125 | + # Using random key |
| 126 | + header = {"Authorization": "Bearer abcd1234"} |
| 127 | + response = requests.get(f"http://localhost:8081/models/mnist", headers=header) |
| 128 | + |
| 129 | + assert response.status_code == 400, "Token check failed" |
| 130 | + |
| 131 | + |
| 132 | +# Test inference API with token enabled |
| 133 | +def test_inference_api_with_token(setup_torchserve): |
| 134 | + key = read_key_file("inference") |
| 135 | + header = {"Authorization": f"Bearer {key}"} |
| 136 | + |
| 137 | + response = requests.post( |
| 138 | + url="http://localhost:8080/predictions/mnist", |
| 139 | + files={"data": open(data_file_zero, "rb")}, |
| 140 | + headers=header, |
| 141 | + ) |
| 142 | + |
| 143 | + assert response.status_code == 200, "Token check failed" |
| 144 | + |
| 145 | + |
| 146 | +# Test inference API with incorrect token |
| 147 | +def test_inference_api_with_incorrect_token(setup_torchserve): |
| 148 | + # Using random key |
| 149 | + header = {"Authorization": "Bearer abcd1234"} |
| 150 | + |
| 151 | + response = requests.post( |
| 152 | + url="http://localhost:8080/predictions/mnist", |
| 153 | + files={"data": open(data_file_zero, "rb")}, |
| 154 | + headers=header, |
| 155 | + ) |
| 156 | + |
| 157 | + assert response.status_code == 400, "Token check failed" |
| 158 | + |
| 159 | + |
| 160 | +# Test Token API for regenerating new inference key |
| 161 | +def test_token_inference_api(setup_torchserve): |
| 162 | + token_key = read_key_file("token") |
| 163 | + inference_key = read_key_file("inference") |
| 164 | + header_inference = {"Authorization": f"Bearer {inference_key}"} |
| 165 | + header_token = {"Authorization": f"Bearer {token_key}"} |
| 166 | + params = {"type": "inference"} |
| 167 | + |
| 168 | + # check inference works with current token |
| 169 | + response = requests.post( |
| 170 | + url="http://localhost:8080/predictions/mnist", |
| 171 | + files={"data": open(data_file_zero, "rb")}, |
| 172 | + headers=header_inference, |
| 173 | + ) |
| 174 | + assert response.status_code == 200, "Token check failed" |
| 175 | + |
| 176 | + # generate new inference token and check it is different |
| 177 | + response = requests.get( |
| 178 | + url="http://localhost:8081/token", params=params, headers=header_token |
| 179 | + ) |
| 180 | + assert response.status_code == 200, "Token check failed" |
| 181 | + assert inference_key != read_key_file("inference"), "Key file not updated" |
| 182 | + |
| 183 | + # check inference does not works with original token |
| 184 | + response = requests.post( |
| 185 | + url="http://localhost:8080/predictions/mnist", |
| 186 | + files={"data": open(data_file_zero, "rb")}, |
| 187 | + headers=header_inference, |
| 188 | + ) |
| 189 | + assert response.status_code == 400, "Token check failed" |
| 190 | + |
| 191 | + |
| 192 | +# Test Token API for regenerating new management key |
| 193 | +def test_token_management_api(setup_torchserve): |
| 194 | + token_key = read_key_file("token") |
| 195 | + management_key = read_key_file("management") |
| 196 | + header = {"Authorization": f"Bearer {token_key}"} |
| 197 | + params = {"type": "management"} |
| 198 | + |
| 199 | + response = requests.get( |
| 200 | + url="http://localhost:8081/token", params=params, headers=header |
| 201 | + ) |
| 202 | + |
| 203 | + assert management_key != read_key_file("management"), "Key file not updated" |
| 204 | + assert response.status_code == 200, "Token check failed" |
| 205 | + |
| 206 | + |
| 207 | +# Test expiration time |
| 208 | +@pytest.mark.module2 |
| 209 | +def test_token_expiration_time(setup_torchserve_expiration): |
| 210 | + key = read_key_file("management") |
| 211 | + header = {"Authorization": f"Bearer {key}"} |
| 212 | + response = requests.get("http://localhost:8081/models/mnist", headers=header) |
| 213 | + assert response.status_code == 200, "Token check failed" |
| 214 | + |
| 215 | + time.sleep(15) |
| 216 | + |
| 217 | + response = requests.get("http://localhost:8081/models/mnist", headers=header) |
| 218 | + assert response.status_code == 400, "Token check failed" |
0 commit comments