Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 126 additions & 0 deletions tests/integration/obj/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import json
from dataclasses import dataclass
from typing import Callable, Optional

import pytest
from pytest import MonkeyPatch

from linodecli.plugins.obj import ENV_ACCESS_KEY_NAME, ENV_SECRET_KEY_NAME
from tests.integration.helpers import exec_test_command, get_random_text

REGION = "us-southeast-1"
CLI_CMD = ["linode-cli", "object-storage"]
BASE_CMD = ["linode-cli", "obj", "--cluster", REGION]


@dataclass
class Keys:
access_key: str
secret_key: str


def patch_keys(keys: Keys, monkeypatch: MonkeyPatch):
assert keys.access_key is not None
assert keys.secret_key is not None
monkeypatch.setenv(ENV_ACCESS_KEY_NAME, keys.access_key)
monkeypatch.setenv(ENV_SECRET_KEY_NAME, keys.secret_key)


def delete_bucket(bucket_name: str, force: bool = True):
args = BASE_CMD + ["rb", bucket_name]
if force:
args.append("--recursive")
exec_test_command(args)
return bucket_name


@pytest.fixture
def create_bucket(
name_generator: Callable, keys: Keys, monkeypatch: MonkeyPatch
):
created_buckets = set()
patch_keys(keys, monkeypatch)

def _create_bucket(bucket_name: Optional[str] = None):
if not bucket_name:
bucket_name = name_generator("test-bk")

exec_test_command(BASE_CMD + ["mb", bucket_name])
created_buckets.add(bucket_name)
return bucket_name

yield _create_bucket
for bk in created_buckets:
try:
delete_bucket(bk)
except Exception as e:
logging.exception(f"Failed to cleanup bucket: {bk}, {e}")


@pytest.fixture
def static_site_index():
return (
"<!DOCTYPE html>"
"<html><head>"
"<title>Hello World</title>"
"</head><body>"
"<p>Hello, World!</p>"
"</body></html>"
)


@pytest.fixture
def static_site_error():
return (
"<!DOCTYPE html>"
"<html><head>"
"<title>Error</title>"
"</head><body>"
"<p>Error!</p>"
"</body></html>"
)


@pytest.fixture(scope="session")
def keys():
response = json.loads(
exec_test_command(
CLI_CMD
+ [
"keys-create",
"--label",
"cli-integration-test-obj-key",
"--json",
],
).stdout.decode()
)[0]
_keys = Keys(
access_key=response.get("access_key"),
secret_key=response.get("secret_key"),
)
yield _keys
exec_test_command(CLI_CMD + ["keys-delete", str(response.get("id"))])


@pytest.fixture(scope="session")
def test_key():
label = get_random_text(10)
key = (
exec_test_command(
CLI_CMD
+ [
"keys-create",
"--label",
label,
"--text",
"--no-headers",
"--format=id",
]
)
.stdout.decode()
.strip()
)

yield key

exec_test_command(CLI_CMD + ["keys-delete", key])
148 changes: 0 additions & 148 deletions tests/integration/obj/test_obj_bucket.py

This file was deleted.

100 changes: 2 additions & 98 deletions tests/integration/obj/test_obj_plugin.py
Original file line number Diff line number Diff line change
@@ -1,110 +1,14 @@
import json
import logging
from concurrent.futures import ThreadPoolExecutor, wait
from dataclasses import dataclass
from typing import Callable, Optional

import pytest
import requests
from pytest import MonkeyPatch

from linodecli.plugins.obj import ENV_ACCESS_KEY_NAME, ENV_SECRET_KEY_NAME
from linodecli.plugins.obj.list import TRUNCATED_MSG
from tests.integration.fixture_types import GetTestFilesType, GetTestFileType
from tests.integration.helpers import count_lines, exec_test_command

REGION = "us-southeast-1"
CLI_CMD = ["linode-cli", "object-storage"]
BASE_CMD = ["linode-cli", "obj", "--cluster", REGION]


@dataclass
class Keys:
access_key: str
secret_key: str


@pytest.fixture
def static_site_index():
return (
"<!DOCTYPE html>"
"<html><head>"
"<title>Hello World</title>"
"</head><body>"
"<p>Hello, World!</p>"
"</body></html>"
)


@pytest.fixture
def static_site_error():
return (
"<!DOCTYPE html>"
"<html><head>"
"<title>Error</title>"
"</head><body>"
"<p>Error!</p>"
"</body></html>"
)


@pytest.fixture(scope="session")
def keys():
response = json.loads(
exec_test_command(
CLI_CMD
+ [
"keys-create",
"--label",
"cli-integration-test-obj-key",
"--json",
],
).stdout.decode()
)[0]
_keys = Keys(
access_key=response.get("access_key"),
secret_key=response.get("secret_key"),
)
yield _keys
exec_test_command(CLI_CMD + ["keys-delete", str(response.get("id"))])


def patch_keys(keys: Keys, monkeypatch: MonkeyPatch):
assert keys.access_key is not None
assert keys.secret_key is not None
monkeypatch.setenv(ENV_ACCESS_KEY_NAME, keys.access_key)
monkeypatch.setenv(ENV_SECRET_KEY_NAME, keys.secret_key)


@pytest.fixture
def create_bucket(
name_generator: Callable, keys: Keys, monkeypatch: MonkeyPatch
):
created_buckets = set()
patch_keys(keys, monkeypatch)

def _create_bucket(bucket_name: Optional[str] = None):
if not bucket_name:
bucket_name = name_generator("test-bk")

exec_test_command(BASE_CMD + ["mb", bucket_name])
created_buckets.add(bucket_name)
return bucket_name

yield _create_bucket
for bk in created_buckets:
try:
delete_bucket(bk)
except Exception as e:
logging.exception(f"Failed to cleanup bucket: {bk}, {e}")


def delete_bucket(bucket_name: str, force: bool = True):
args = BASE_CMD + ["rb", bucket_name]
if force:
args.append("--recursive")
exec_test_command(args)
return bucket_name
from tests.integration.obj.conftest import BASE_CMD, REGION, Keys, patch_keys


def test_obj_single_file_single_bucket(
Expand Down Expand Up @@ -452,6 +356,6 @@ def test_generate_url(
BASE_CMD + ["signurl", bucket, test_file.name, "+300"]
)
url = process.stdout.decode()
response = requests.get(url)
response = requests.get(url.strip("\n"))
assert response.text == content
assert response.status_code == 200
Loading