Skip to content

Commit

Permalink
add mysql dump and restore end to end test
Browse files Browse the repository at this point in the history
Signed-off-by: Rafał Safin <rafal.safin@rafsaf.pl>
  • Loading branch information
Rafał Safin committed Mar 14, 2024
1 parent ccc84c7 commit 69c0b57
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 6 deletions.
7 changes: 3 additions & 4 deletions backuper/backup_targets/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class MySQL(BaseBackupTarget):
def __init__(self, target_model: MySQLTargetModel) -> None:
super().__init__(target_model)
self.target_model: MySQLTargetModel = target_model
self.db_name = shlex.quote(self.target_model.db)
self.option_file: Path = self._init_option_file()
self.db_version: str = self._mysql_connection()

Expand Down Expand Up @@ -65,9 +66,8 @@ def _mysql_connection(self) -> str:
raise
log.debug("start mysql connection")
try:
db = shlex.quote(self.target_model.db)
result = core.run_subprocess(
f"mariadb --defaults-file={self.option_file} {db} "
f"mariadb --defaults-file={self.option_file} {self.db_name} "
"--execute='SELECT version();'",
)
except core.CoreSubprocessError as err:
Expand Down Expand Up @@ -98,10 +98,9 @@ def _backup(self) -> Path:

out_file = core.get_new_backup_path(self.env_name, name).with_suffix(".sql")

db = shlex.quote(self.target_model.db)
shell_mysqldump_db = (
f"mariadb-dump --defaults-file={self.option_file} "
f"--result-file={out_file} --verbose {db}"
f"--result-file={out_file} --verbose {self.db_name}"
)
log.debug("start mysqldump in subprocess: %s", shell_mysqldump_db)
core.run_subprocess(shell_mysqldump_db)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_backup_target_mariadb.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def test_end_to_end_successful_restore_after_backup(

result = core.run_subprocess(
f"mariadb --defaults-file={test_db.option_file} {test_db.db_name}"
" --execute='select * from my_table;'",
" --execute='select * from my_table order by id asc;'",
)

assert result == ("id\tname\tage\n" "1\tGeralt z Rivii\t60\n" "2\trafsaf\t24\n")
72 changes: 72 additions & 0 deletions tests/test_backup_target_mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)


import shlex

import pytest
from freezegun import freeze_time
from pydantic import SecretStr

from backuper import config, core
from backuper.backup_targets.mysql import MySQL
Expand Down Expand Up @@ -41,3 +44,72 @@ def test_run_mysqldump(mysql_target: MySQLTargetModel) -> None:
)
out_path = config.CONST_BACKUP_FOLDER_PATH / out_file
assert out_backup == out_path


@pytest.mark.parametrize("mysql_target", ALL_MYSQL_DBS_TARGETS)
def test_end_to_end_successful_restore_after_backup(
mysql_target: MySQLTargetModel,
) -> None:
root_target_model = mysql_target.model_copy(
update={
"user": "root",
"password": SecretStr(f"root-{mysql_target.password.get_secret_value()}"),
}
)

db = MySQL(target_model=root_target_model)
core.run_subprocess(
f"mariadb --defaults-file={db.option_file} {db.db_name} --execute="
"'DROP DATABASE IF EXISTS test_db;'",
)
core.run_subprocess(
f"mariadb --defaults-file={db.option_file} {db.db_name} --execute="
"'CREATE DATABASE test_db;'",
)

test_db_target = root_target_model.model_copy(update={"db": "test_db"})
test_db = MySQL(target_model=test_db_target)

table_query = (
"CREATE TABLE my_table "
"(id SERIAL PRIMARY KEY, "
"name VARCHAR (50) UNIQUE NOT NULL, "
"age INTEGER);"
)
core.run_subprocess(
f"mariadb --defaults-file={test_db.option_file} {test_db.db_name} "
f"--execute='{table_query}'",
)

insert_query = shlex.quote(
"INSERT INTO my_table (name, age) "
"VALUES ('Geralt z Rivii', 60),('rafsaf', 24);"
)

core.run_subprocess(
f"mariadb --defaults-file={test_db.option_file} {test_db.db_name} "
f"--execute={insert_query}",
)

test_db_backup = test_db.make_backup()

core.run_subprocess(
f"mariadb --defaults-file={db.option_file} {db.db_name} --execute="
"'DROP DATABASE test_db;'",
)
core.run_subprocess(
f"mariadb --defaults-file={db.option_file} {db.db_name} --execute="
"'CREATE DATABASE test_db;'",
)

core.run_subprocess(
f"mariadb --defaults-file={test_db.option_file} {test_db.db_name}"
f" < {test_db_backup}",
)

result = core.run_subprocess(
f"mariadb --defaults-file={test_db.option_file} {test_db.db_name}"
" --execute='select * from my_table order by id asc;'",
)

assert result == ("id\tname\tage\n" "1\tGeralt z Rivii\t60\n" "2\trafsaf\t24\n")
3 changes: 2 additions & 1 deletion tests/test_backup_target_postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ def test_end_to_end_successful_restore_after_backup(
)

result = core.run_subprocess(
f"psql -d {test_db.escaped_conn_uri} -w --command 'select * from my_table;'",
f"psql -d {test_db.escaped_conn_uri} -w --command "
"'select * from my_table order by id asc;'",
)

assert result == (
Expand Down

0 comments on commit 69c0b57

Please sign in to comment.