| 1 |
import contextlib |
| 2 |
import os |
| 3 |
import subprocess |
| 4 |
|
| 5 |
from django.conf import settings |
| 6 |
from django.core.files import File |
| 7 |
from django.core.files.storage import Storage |
| 8 |
from django.utils import timezone |
| 9 |
|
| 10 |
from dbbackup.db.base import get_connector |
| 11 |
|
| 12 |
BASE_FILE = os.path.join(settings.BLOB_DIR, "test.txt") |
| 13 |
ENCRYPTED_FILE = os.path.join(settings.BLOB_DIR, "test.txt.gpg") |
| 14 |
COMPRESSED_FILE = os.path.join(settings.BLOB_DIR, "test.txt.gz") |
| 15 |
TARED_FILE = os.path.join(settings.BLOB_DIR, "test.txt.tar") |
| 16 |
ENCRYPTED_COMPRESSED_FILE = os.path.join(settings.BLOB_DIR, "test.txt.gz.gpg") |
| 17 |
_REPO_TMP_DIR = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "tmp")) |
| 18 |
os.makedirs(_REPO_TMP_DIR, exist_ok=True) |
| 19 |
TEST_DATABASE = { |
| 20 |
"ENGINE": "django.db.backends.sqlite3", |
| 21 |
# Store test DB in repo-local tmp directory (ensured above) |
| 22 |
"NAME": os.path.join(_REPO_TMP_DIR, "foo.db"), |
| 23 |
"USER": "foo", |
| 24 |
"PASSWORD": "bar", |
| 25 |
"HOST": "foo", |
| 26 |
"PORT": 122, |
| 27 |
} |
| 28 |
TEST_MONGODB = { |
| 29 |
"ENGINE": "django_mongodb_engine", |
| 30 |
"NAME": "mongo_test", |
| 31 |
"USER": "foo", |
| 32 |
"PASSWORD": "bar", |
| 33 |
"HOST": "foo", |
| 34 |
"PORT": 122, |
| 35 |
} |
| 36 |
TEST_DATABASE = settings.DATABASES["default"] |
| 37 |
|
| 38 |
GPG_PRIVATE_PATH = os.path.join(settings.BLOB_DIR, "gpg/secring.gpg") |
| 39 |
GPG_PUBLIC_PATH = os.path.join(settings.BLOB_DIR, "gpg/pubring.gpg") |
| 40 |
GPG_FINGERPRINT = "7438 8D4E 02AF C011 4E2F 1E79 F7D1 BBF0 1F63 FDE9" |
| 41 |
DEV_NULL = open(os.devnull, "w") |
| 42 |
|
| 43 |
|
| 44 |
class HandledFiles(dict): |
| 45 |
""" |
| 46 |
Dict for gather information about fake storage and clean between tests. |
| 47 |
You should use the constant instance ``HANDLED_FILES`` and clean it |
| 48 |
before tests. |
| 49 |
""" |
| 50 |
|
| 51 |
def __init__(self): |
| 52 |
super().__init__() |
| 53 |
self.clean() |
| 54 |
|
| 55 |
def clean(self): |
| 56 |
self["written_files"] = [] |
| 57 |
self["deleted_files"] = [] |
| 58 |
|
| 59 |
|
| 60 |
HANDLED_FILES = HandledFiles() |
| 61 |
|
| 62 |
|
| 63 |
class FakeStorage(Storage): |
| 64 |
name = "FakeStorage" |
| 65 |
|
| 66 |
def exists(self, name): |
| 67 |
return any(f[0] == name for f in HANDLED_FILES["written_files"]) |
| 68 |
|
| 69 |
def get_available_name(self, name, max_length=None): |
| 70 |
return name[:max_length] |
| 71 |
|
| 72 |
def get_valid_name(self, name): |
| 73 |
return name |
| 74 |
|
| 75 |
def listdir(self, path): |
| 76 |
return ([], [f[0] for f in HANDLED_FILES["written_files"]]) |
| 77 |
|
| 78 |
def accessed_time(self, name): |
| 79 |
return timezone.now() |
| 80 |
|
| 81 |
created_time = modified_time = accessed_time |
| 82 |
|
| 83 |
def _open(self, name, mode="rb"): |
| 84 |
file_ = next(f[1] for f in HANDLED_FILES["written_files"] if f[0] == name) |
| 85 |
file_.seek(0) |
| 86 |
return file_ |
| 87 |
|
| 88 |
def _save(self, name, content): |
| 89 |
HANDLED_FILES["written_files"].append((name, File(content))) |
| 90 |
return name |
| 91 |
|
| 92 |
def delete(self, name): |
| 93 |
HANDLED_FILES["deleted_files"].append(name) |
| 94 |
|
| 95 |
|
| 96 |
class LocationPrefixedFakeStorage(FakeStorage): |
| 97 |
name = "LocationPrefixedFakeStorage" |
| 98 |
location = "dev" |
| 99 |
|
| 100 |
def listdir(self, path): |
| 101 |
return ([], [f"{self.location}/{f[0]}" for f in HANDLED_FILES["written_files"]]) |
| 102 |
|
| 103 |
|
| 104 |
def clean_gpg_keys(): |
| 105 |
with contextlib.suppress(Exception): |
| 106 |
cmd = f"gpg --batch --yes --delete-key '{GPG_FINGERPRINT}'" |
| 107 |
subprocess.call(cmd, stdout=DEV_NULL, stderr=DEV_NULL) |
| 108 |
with contextlib.suppress(Exception): |
| 109 |
cmd = f"gpg --batch --yes --delete-secret-key '{GPG_FINGERPRINT}'" |
| 110 |
subprocess.call(cmd, stdout=DEV_NULL, stderr=DEV_NULL) |
| 111 |
|
| 112 |
|
| 113 |
def add_private_gpg(): |
| 114 |
cmd = f"gpg --import {GPG_PRIVATE_PATH}".split() |
| 115 |
subprocess.call(cmd, stdout=DEV_NULL, stderr=DEV_NULL) |
| 116 |
|
| 117 |
|
| 118 |
def add_public_gpg(): |
| 119 |
cmd = f"gpg --import {GPG_PUBLIC_PATH}".split() |
| 120 |
subprocess.call(cmd, stdout=DEV_NULL, stderr=DEV_NULL) |
| 121 |
|
| 122 |
|
| 123 |
def callable_for_filename_template(datetime, **kwargs): |
| 124 |
return f"{datetime}_foo" |
| 125 |
|
| 126 |
|
| 127 |
def get_dump(database=TEST_DATABASE): |
| 128 |
return get_connector().create_dump() |
| 129 |
|
| 130 |
|
| 131 |
def get_dump_name(database=None): |
| 132 |
database = database or TEST_DATABASE |
| 133 |
return get_connector().generate_filename() |