Skip to content

Commit 5612fe9

Browse files
authored
Add object-storage integration tests (#750)
1 parent fe7d229 commit 5612fe9

File tree

4 files changed

+435
-246
lines changed

4 files changed

+435
-246
lines changed

tests/integration/obj/conftest.py

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import json
2+
from dataclasses import dataclass
3+
from typing import Callable, Optional
4+
5+
import pytest
6+
from pytest import MonkeyPatch
7+
8+
from linodecli.plugins.obj import ENV_ACCESS_KEY_NAME, ENV_SECRET_KEY_NAME
9+
from tests.integration.helpers import exec_test_command, get_random_text
10+
11+
REGION = "us-southeast-1"
12+
CLI_CMD = ["linode-cli", "object-storage"]
13+
BASE_CMD = ["linode-cli", "obj", "--cluster", REGION]
14+
15+
16+
@dataclass
17+
class Keys:
18+
access_key: str
19+
secret_key: str
20+
21+
22+
def patch_keys(keys: Keys, monkeypatch: MonkeyPatch):
23+
assert keys.access_key is not None
24+
assert keys.secret_key is not None
25+
monkeypatch.setenv(ENV_ACCESS_KEY_NAME, keys.access_key)
26+
monkeypatch.setenv(ENV_SECRET_KEY_NAME, keys.secret_key)
27+
28+
29+
def delete_bucket(bucket_name: str, force: bool = True):
30+
args = BASE_CMD + ["rb", bucket_name]
31+
if force:
32+
args.append("--recursive")
33+
exec_test_command(args)
34+
return bucket_name
35+
36+
37+
@pytest.fixture
38+
def create_bucket(
39+
name_generator: Callable, keys: Keys, monkeypatch: MonkeyPatch
40+
):
41+
created_buckets = set()
42+
patch_keys(keys, monkeypatch)
43+
44+
def _create_bucket(bucket_name: Optional[str] = None):
45+
if not bucket_name:
46+
bucket_name = name_generator("test-bk")
47+
48+
exec_test_command(BASE_CMD + ["mb", bucket_name])
49+
created_buckets.add(bucket_name)
50+
return bucket_name
51+
52+
yield _create_bucket
53+
for bk in created_buckets:
54+
try:
55+
delete_bucket(bk)
56+
except Exception as e:
57+
logging.exception(f"Failed to cleanup bucket: {bk}, {e}")
58+
59+
60+
@pytest.fixture
61+
def static_site_index():
62+
return (
63+
"<!DOCTYPE html>"
64+
"<html><head>"
65+
"<title>Hello World</title>"
66+
"</head><body>"
67+
"<p>Hello, World!</p>"
68+
"</body></html>"
69+
)
70+
71+
72+
@pytest.fixture
73+
def static_site_error():
74+
return (
75+
"<!DOCTYPE html>"
76+
"<html><head>"
77+
"<title>Error</title>"
78+
"</head><body>"
79+
"<p>Error!</p>"
80+
"</body></html>"
81+
)
82+
83+
84+
@pytest.fixture(scope="session")
85+
def keys():
86+
response = json.loads(
87+
exec_test_command(
88+
CLI_CMD
89+
+ [
90+
"keys-create",
91+
"--label",
92+
"cli-integration-test-obj-key",
93+
"--json",
94+
],
95+
).stdout.decode()
96+
)[0]
97+
_keys = Keys(
98+
access_key=response.get("access_key"),
99+
secret_key=response.get("secret_key"),
100+
)
101+
yield _keys
102+
exec_test_command(CLI_CMD + ["keys-delete", str(response.get("id"))])
103+
104+
105+
@pytest.fixture(scope="session")
106+
def test_key():
107+
label = get_random_text(10)
108+
key = (
109+
exec_test_command(
110+
CLI_CMD
111+
+ [
112+
"keys-create",
113+
"--label",
114+
label,
115+
"--text",
116+
"--no-headers",
117+
"--format=id",
118+
]
119+
)
120+
.stdout.decode()
121+
.strip()
122+
)
123+
124+
yield key
125+
126+
exec_test_command(CLI_CMD + ["keys-delete", key])

tests/integration/obj/test_obj_bucket.py

Lines changed: 0 additions & 148 deletions
This file was deleted.

tests/integration/obj/test_obj_plugin.py

Lines changed: 2 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,14 @@
1-
import json
2-
import logging
31
from concurrent.futures import ThreadPoolExecutor, wait
4-
from dataclasses import dataclass
52
from typing import Callable, Optional
63

74
import pytest
85
import requests
96
from pytest import MonkeyPatch
107

11-
from linodecli.plugins.obj import ENV_ACCESS_KEY_NAME, ENV_SECRET_KEY_NAME
128
from linodecli.plugins.obj.list import TRUNCATED_MSG
139
from tests.integration.fixture_types import GetTestFilesType, GetTestFileType
1410
from tests.integration.helpers import count_lines, exec_test_command
15-
16-
REGION = "us-southeast-1"
17-
CLI_CMD = ["linode-cli", "object-storage"]
18-
BASE_CMD = ["linode-cli", "obj", "--cluster", REGION]
19-
20-
21-
@dataclass
22-
class Keys:
23-
access_key: str
24-
secret_key: str
25-
26-
27-
@pytest.fixture
28-
def static_site_index():
29-
return (
30-
"<!DOCTYPE html>"
31-
"<html><head>"
32-
"<title>Hello World</title>"
33-
"</head><body>"
34-
"<p>Hello, World!</p>"
35-
"</body></html>"
36-
)
37-
38-
39-
@pytest.fixture
40-
def static_site_error():
41-
return (
42-
"<!DOCTYPE html>"
43-
"<html><head>"
44-
"<title>Error</title>"
45-
"</head><body>"
46-
"<p>Error!</p>"
47-
"</body></html>"
48-
)
49-
50-
51-
@pytest.fixture(scope="session")
52-
def keys():
53-
response = json.loads(
54-
exec_test_command(
55-
CLI_CMD
56-
+ [
57-
"keys-create",
58-
"--label",
59-
"cli-integration-test-obj-key",
60-
"--json",
61-
],
62-
).stdout.decode()
63-
)[0]
64-
_keys = Keys(
65-
access_key=response.get("access_key"),
66-
secret_key=response.get("secret_key"),
67-
)
68-
yield _keys
69-
exec_test_command(CLI_CMD + ["keys-delete", str(response.get("id"))])
70-
71-
72-
def patch_keys(keys: Keys, monkeypatch: MonkeyPatch):
73-
assert keys.access_key is not None
74-
assert keys.secret_key is not None
75-
monkeypatch.setenv(ENV_ACCESS_KEY_NAME, keys.access_key)
76-
monkeypatch.setenv(ENV_SECRET_KEY_NAME, keys.secret_key)
77-
78-
79-
@pytest.fixture
80-
def create_bucket(
81-
name_generator: Callable, keys: Keys, monkeypatch: MonkeyPatch
82-
):
83-
created_buckets = set()
84-
patch_keys(keys, monkeypatch)
85-
86-
def _create_bucket(bucket_name: Optional[str] = None):
87-
if not bucket_name:
88-
bucket_name = name_generator("test-bk")
89-
90-
exec_test_command(BASE_CMD + ["mb", bucket_name])
91-
created_buckets.add(bucket_name)
92-
return bucket_name
93-
94-
yield _create_bucket
95-
for bk in created_buckets:
96-
try:
97-
delete_bucket(bk)
98-
except Exception as e:
99-
logging.exception(f"Failed to cleanup bucket: {bk}, {e}")
100-
101-
102-
def delete_bucket(bucket_name: str, force: bool = True):
103-
args = BASE_CMD + ["rb", bucket_name]
104-
if force:
105-
args.append("--recursive")
106-
exec_test_command(args)
107-
return bucket_name
11+
from tests.integration.obj.conftest import BASE_CMD, REGION, Keys, patch_keys
10812

10913

11014
def test_obj_single_file_single_bucket(
@@ -452,6 +356,6 @@ def test_generate_url(
452356
BASE_CMD + ["signurl", bucket, test_file.name, "+300"]
453357
)
454358
url = process.stdout.decode()
455-
response = requests.get(url)
359+
response = requests.get(url.strip("\n"))
456360
assert response.text == content
457361
assert response.status_code == 200

0 commit comments

Comments
 (0)