|
| 1 | +import json |
1 | 2 | import logging |
2 | 3 | from dataclasses import dataclass |
3 | 4 | from typing import Callable, Optional |
|
6 | 7 | import requests |
7 | 8 | from pytest import MonkeyPatch |
8 | 9 |
|
9 | | -from linodecli.configuration.auth import _do_request |
10 | 10 | from linodecli.plugins.obj import ( |
11 | 11 | ENV_ACCESS_KEY_NAME, |
12 | 12 | ENV_SECRET_KEY_NAME, |
13 | 13 | TRUNCATED_MSG, |
14 | 14 | ) |
15 | 15 | from tests.integration.fixture_types import GetTestFilesType, GetTestFileType |
16 | | -from tests.integration.helpers import BASE_URL, count_lines, exec_test_command |
| 16 | +from tests.integration.helpers import count_lines, exec_test_command |
17 | 17 |
|
18 | 18 | REGION = "us-southeast-1" |
| 19 | +CLI_CMD = ["linode-cli", "object-storage"] |
19 | 20 | BASE_CMD = ["linode-cli", "obj", "--cluster", REGION] |
20 | 21 |
|
21 | 22 |
|
@@ -50,27 +51,24 @@ def static_site_error(): |
50 | 51 |
|
51 | 52 |
|
52 | 53 | @pytest.fixture(scope="session") |
53 | | -def keys(token: str): |
54 | | - response = _do_request( |
55 | | - BASE_URL, |
56 | | - requests.post, |
57 | | - "object-storage/keys", |
58 | | - token, |
59 | | - False, |
60 | | - {"label": "cli-integration-test-obj-key"}, |
61 | | - ) |
62 | | - |
| 54 | +def keys(): |
| 55 | + response = json.loads( |
| 56 | + exec_test_command( |
| 57 | + CLI_CMD |
| 58 | + + [ |
| 59 | + "keys-create", |
| 60 | + "--label", |
| 61 | + "cli-integration-test-obj-key", |
| 62 | + "--json", |
| 63 | + ], |
| 64 | + ).stdout.decode() |
| 65 | + )[0] |
63 | 66 | _keys = Keys( |
64 | 67 | access_key=response.get("access_key"), |
65 | 68 | secret_key=response.get("secret_key"), |
66 | 69 | ) |
67 | 70 | yield _keys |
68 | | - _do_request( |
69 | | - BASE_URL, |
70 | | - requests.delete, |
71 | | - f"object-storage/keys/{response['id']}", |
72 | | - token, |
73 | | - ) |
| 71 | + exec_test_command(CLI_CMD + ["keys-delete", str(response.get("id"))]) |
74 | 72 |
|
75 | 73 |
|
76 | 74 | def patch_keys(keys: Keys, monkeypatch: MonkeyPatch): |
@@ -150,6 +148,51 @@ def test_obj_single_file_single_bucket( |
150 | 148 | assert f1.read() == f2.read() |
151 | 149 |
|
152 | 150 |
|
| 151 | +def test_obj_single_file_single_bucket_with_prefix( |
| 152 | + create_bucket: Callable[[Optional[str]], str], |
| 153 | + generate_test_files: GetTestFilesType, |
| 154 | + keys: Keys, |
| 155 | + monkeypatch: MonkeyPatch, |
| 156 | +): |
| 157 | + patch_keys(keys, monkeypatch) |
| 158 | + file_path = generate_test_files()[0] |
| 159 | + bucket_name = create_bucket() |
| 160 | + exec_test_command( |
| 161 | + BASE_CMD + ["put", str(file_path), f"{bucket_name}/prefix"] |
| 162 | + ) |
| 163 | + process = exec_test_command(BASE_CMD + ["la"]) |
| 164 | + output = process.stdout.decode() |
| 165 | + |
| 166 | + assert f"{bucket_name}/prefix/{file_path.name}" in output |
| 167 | + |
| 168 | + file_size = file_path.stat().st_size |
| 169 | + assert str(file_size) in output |
| 170 | + |
| 171 | + process = exec_test_command(BASE_CMD + ["ls"]) |
| 172 | + output = process.stdout.decode() |
| 173 | + assert bucket_name in output |
| 174 | + assert file_path.name not in output |
| 175 | + |
| 176 | + process = exec_test_command(BASE_CMD + ["ls", bucket_name]) |
| 177 | + output = process.stdout.decode() |
| 178 | + assert bucket_name not in output |
| 179 | + assert "prefix" in output |
| 180 | + |
| 181 | + downloaded_file_path = file_path.parent / f"downloaded_{file_path.name}" |
| 182 | + process = exec_test_command( |
| 183 | + BASE_CMD |
| 184 | + + [ |
| 185 | + "get", |
| 186 | + bucket_name, |
| 187 | + "prefix/" + file_path.name, |
| 188 | + str(downloaded_file_path), |
| 189 | + ] |
| 190 | + ) |
| 191 | + output = process.stdout.decode() |
| 192 | + with open(downloaded_file_path) as f2, open(file_path) as f1: |
| 193 | + assert f1.read() == f2.read() |
| 194 | + |
| 195 | + |
153 | 196 | def test_multi_files_multi_bucket( |
154 | 197 | create_bucket: Callable[[Optional[str]], str], |
155 | 198 | generate_test_files: GetTestFilesType, |
|
0 commit comments