|
| 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]) |
0 commit comments