| 1 |
import os |
| 2 |
from tempfile import SpooledTemporaryFile |
| 3 |
from unittest.mock import patch |
| 4 |
|
| 5 |
import pytest |
| 6 |
from django.test import TestCase |
| 7 |
|
| 8 |
from dbbackup.db import exceptions |
| 9 |
from dbbackup.db.base import BaseCommandDBConnector, BaseDBConnector, get_connector |
| 10 |
|
| 11 |
|
| 12 |
class GetConnectorTest(TestCase): |
| 13 |
def test_get_connector(self): |
| 14 |
connector = get_connector() |
| 15 |
assert isinstance(connector, BaseDBConnector) |
| 16 |
|
| 17 |
def test_get_connector_sqlite_uses_backup_connector(self): |
| 18 |
"""Test that SQLite database uses SqliteBackupConnector as default.""" |
| 19 |
from unittest.mock import patch |
| 20 |
|
| 21 |
from dbbackup.db.sqlite import SqliteBackupConnector |
| 22 |
|
| 23 |
# Mock the database connection to simulate SQLite |
| 24 |
mock_connection = {"ENGINE": "django.db.backends.sqlite3"} |
| 25 |
|
| 26 |
with patch("django.db.connections") as mock_connections: |
| 27 |
mock_connections.__getitem__.return_value.settings_dict = mock_connection |
| 28 |
connector = get_connector() |
| 29 |
assert isinstance(connector, SqliteBackupConnector) |
| 30 |
|
| 31 |
def test_get_connector_oracle_fallback(self): |
| 32 |
"""Test that Oracle database uses Django connector as fallback.""" |
| 33 |
from unittest.mock import patch |
| 34 |
|
| 35 |
from dbbackup.db.django import DjangoConnector |
| 36 |
|
| 37 |
# Mock the database connection to simulate Oracle |
| 38 |
mock_connection = {"ENGINE": "django.db.backends.oracle"} |
| 39 |
|
| 40 |
with patch("django.db.connections") as mock_connections: |
| 41 |
mock_connections.__getitem__.return_value.settings_dict = mock_connection |
| 42 |
connector = get_connector() |
| 43 |
assert isinstance(connector, DjangoConnector) |
| 44 |
|
| 45 |
def test_get_connector_unmapped_engine_fallback(self): |
| 46 |
"""Test that unmapped database engines use Django connector as fallback.""" |
| 47 |
from unittest.mock import patch |
| 48 |
|
| 49 |
from dbbackup.db.django import DjangoConnector |
| 50 |
|
| 51 |
# Mock the database connection to simulate an unmapped engine |
| 52 |
mock_connection = {"ENGINE": "some.custom.database.backend"} |
| 53 |
|
| 54 |
with patch("django.db.connections") as mock_connections: |
| 55 |
mock_connections.__getitem__.return_value.settings_dict = mock_connection |
| 56 |
connector = get_connector() |
| 57 |
assert isinstance(connector, DjangoConnector) |
| 58 |
|
| 59 |
|
| 60 |
class BaseDBConnectorTest(TestCase): |
| 61 |
def test_init(self): |
| 62 |
BaseDBConnector() |
| 63 |
|
| 64 |
def test_settings(self): |
| 65 |
connector = BaseDBConnector() |
| 66 |
assert isinstance(connector.settings, dict) |
| 67 |
|
| 68 |
def test_generate_filename(self): |
| 69 |
connector = BaseDBConnector() |
| 70 |
connector.generate_filename() |
| 71 |
|
| 72 |
|
| 73 |
class BaseCommandDBConnectorTest(TestCase): |
| 74 |
def test_run_command(self): |
| 75 |
connector = BaseCommandDBConnector() |
| 76 |
stdout, stderr = connector.run_command("echo 123") |
| 77 |
assert stdout.read() == b"123\n" |
| 78 |
assert stderr.read() == b"" |
| 79 |
|
| 80 |
def test_run_command_error(self): |
| 81 |
connector = BaseCommandDBConnector() |
| 82 |
with pytest.raises(exceptions.CommandConnectorError): |
| 83 |
connector.run_command("echa 123") |
| 84 |
|
| 85 |
def test_run_command_error_message_for_missing_command(self): |
| 86 |
"""Test error message when a database command is not found.""" |
| 87 |
connector = BaseCommandDBConnector() |
| 88 |
with pytest.raises(exceptions.CommandConnectorError) as cm: |
| 89 |
connector.run_command("nonexistent_database_command_12345") |
| 90 |
|
| 91 |
error_message = str(cm.value) |
| 92 |
|
| 93 |
# Check that the improved error message contains helpful information |
| 94 |
assert "Database command 'nonexistent_database_command_12345' not found" in error_message |
| 95 |
assert "Please ensure the required database client tools are installed" in error_message |
| 96 |
assert "PostgreSQL: Install postgresql-client" in error_message |
| 97 |
assert "MySQL: Install mysql-client" in error_message |
| 98 |
assert "MongoDB: Install mongodb-tools" in error_message |
| 99 |
assert "DUMP_CMD: Path to the dump command" in error_message |
| 100 |
assert "RESTORE_CMD: Path to the restore command" in error_message |
| 101 |
|
| 102 |
def test_run_command_error_message_for_other_os_errors(self): |
| 103 |
"""Test that non-ENOENT OSErrors still get the original error format.""" |
| 104 |
connector = BaseCommandDBConnector() |
| 105 |
|
| 106 |
# Mock OSError with a different errno to test the else branch |
| 107 |
with patch("dbbackup.db.base.Popen") as mock_popen: |
| 108 |
mock_popen.side_effect = OSError(13, "Permission denied") # EACCES |
| 109 |
|
| 110 |
with pytest.raises(exceptions.CommandConnectorError) as cm: |
| 111 |
connector.run_command("some_command") |
| 112 |
|
| 113 |
error_message = str(cm.value) |
| 114 |
|
| 115 |
# Should get the original error format for non-ENOENT errors |
| 116 |
assert "Error running: some_command" in error_message |
| 117 |
assert "Permission denied" in error_message |
| 118 |
# Should NOT contain the helpful database installation message |
| 119 |
assert "Database command" not in error_message |
| 120 |
assert "client tools are installed" not in error_message |
| 121 |
|
| 122 |
def test_run_command_stdin(self): |
| 123 |
connector = BaseCommandDBConnector() |
| 124 |
stdin = SpooledTemporaryFile() |
| 125 |
stdin.write(b"foo") |
| 126 |
stdin.seek(0) |
| 127 |
# Run |
| 128 |
stdout, stderr = connector.run_command("cat", stdin=stdin) |
| 129 |
assert stdout.read() == b"foo" |
| 130 |
assert not stderr.read() |
| 131 |
|
| 132 |
def test_run_command_with_env(self): |
| 133 |
connector = BaseCommandDBConnector() |
| 134 |
# Empty env |
| 135 |
stdout, _stderr = connector.run_command("env") |
| 136 |
assert stdout.read() |
| 137 |
# env from self.env |
| 138 |
connector.env = {"foo": "bar"} |
| 139 |
stdout, _stderr = connector.run_command("env") |
| 140 |
assert b"foo=bar\n" in stdout.read() |
| 141 |
# method override global env |
| 142 |
stdout, _stderr = connector.run_command("env", env={"foo": "ham"}) |
| 143 |
assert b"foo=ham\n" in stdout.read() |
| 144 |
# get a var from parent env |
| 145 |
os.environ["BAR"] = "foo" |
| 146 |
stdout, _stderr = connector.run_command("env") |
| 147 |
assert b"bar=foo\n" in stdout.read() |
| 148 |
# Conf overrides parendt env |
| 149 |
connector.env = {"bar": "bar"} |
| 150 |
stdout, _stderr = connector.run_command("env") |
| 151 |
assert b"bar=bar\n" in stdout.read() |
| 152 |
# method overrides all |
| 153 |
stdout, _stderr = connector.run_command("env", env={"bar": "ham"}) |
| 154 |
assert b"bar=ham\n" in stdout.read() |
| 155 |
|
| 156 |
def test_run_command_with_parent_env(self): |
| 157 |
connector = BaseCommandDBConnector(use_parent_env=False) |
| 158 |
# Empty env |
| 159 |
stdout, _stderr = connector.run_command("env") |
| 160 |
assert not stdout.read() |
| 161 |
# env from self.env |
| 162 |
connector.env = {"foo": "bar"} |
| 163 |
stdout, _stderr = connector.run_command("env") |
| 164 |
assert stdout.read() == b"foo=bar\n" |
| 165 |
# method override global env |
| 166 |
stdout, _stderr = connector.run_command("env", env={"foo": "ham"}) |
| 167 |
assert stdout.read() == b"foo=ham\n" |
| 168 |
# no var from parent env |
| 169 |
os.environ["BAR"] = "foo" |
| 170 |
stdout, _stderr = connector.run_command("env") |
| 171 |
assert b"bar=foo\n" not in stdout.read() |