Skip to content

Commit

Permalink
Fix test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
lkubb committed Aug 8, 2024
1 parent 3dd79ad commit 469cccc
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 48 deletions.
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ tests = [
"pytest>=7.2.0",
"pytest-salt-factories>=1.0.0; sys_platform == 'win32'",
"pytest-salt-factories[docker]>=1.0.0; sys_platform != 'win32'",
"pytest-subtests",
"pymysql",
# required by pymysql for sha256_password/caching_sha2_password auth method
"cryptography",
"sqlparse",
]

[project.entry-points."salt.loader"]
Expand Down
30 changes: 21 additions & 9 deletions tests/support/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@

import attr
import pytest
from pytestskipmarkers.utils import platform
from saltfactories.utils import random_string

# This `pytest.importorskip` here actually works because this module
# is imported into test modules, otherwise, the skipping would just fail
pytest.importorskip("docker")
import docker.errors # isort:skip
import docker.errors # isort:skip pylint:disable=wrong-import-position

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -39,8 +40,12 @@ class MySQLCombo:

@container_id.default
def _default_container_id(self):
mysql_name = self.mysql_name.replace("/", "-")
return random_string(f"{mysql_name}-{self.mysql_version}-")
return random_string(
"{}-{}-".format( # pylint: disable=consider-using-f-string
self.mysql_name.replace("/", "-"),
self.mysql_version,
)
)

@mysql_root_passwd.default
def _default_mysql_root_user_passwd(self):
Expand All @@ -57,7 +62,7 @@ def get_credentials(self, **kwargs):

def get_test_versions():
test_versions = []
name = "mysql/mysql-server"
name = "mysql-server"
for version in ("5.5", "5.6", "5.7", "8.0"):
test_versions.append(
MySQLImage(
Expand All @@ -67,7 +72,7 @@ def get_test_versions():
)
)
name = "mariadb"
for version in ("10.3", "10.4", "10.5", "10.6"):
for version in ("10.3", "10.4", "10.5"):
test_versions.append(
MySQLImage(
name=name,
Expand All @@ -76,7 +81,7 @@ def get_test_versions():
)
)
name = "percona"
for version in ("5.5", "5.6", "5.7", "8.0"):
for version in ("5.6", "5.7", "8.0"):
test_versions.append(
MySQLImage(
name=name,
Expand All @@ -98,6 +103,10 @@ def mysql_image(request):

@pytest.fixture(scope="module")
def create_mysql_combo(mysql_image):
if platform.is_fips_enabled():
if mysql_image.name in ("mysql-server", "percona") and mysql_image.tag == "8.0":
pytest.skip(f"These tests fail on {mysql_image.name}:{mysql_image.tag}")

return MySQLCombo(
mysql_name=mysql_image.name,
mysql_version=mysql_image.tag,
Expand Down Expand Up @@ -143,8 +152,9 @@ def set_container_name_before_start(container):
This is useful if the container has to be restared and the old
container, under the same name was left running, but in a bad shape.
"""
container_name = container.name.rsplit("-", 1)[0]
container.name = random_string(f"{container_name}-")
container.name = random_string(
"{}-".format(container.name.rsplit("-", 1)[0]) # pylint: disable=consider-using-f-string
)
container.display_name = None
return container

Expand All @@ -163,7 +173,9 @@ def mysql_container(salt_factories, mysql_combo):

container = salt_factories.get_container(
mysql_combo.container_id,
f"ghcr.io/saltstack/salt-ci-containers/{mysql_combo.mysql_name}:{mysql_combo.mysql_version}",
"ghcr.io/saltstack/salt-ci-containers/{}:{}".format( # pylint: disable=consider-using-f-string
mysql_combo.mysql_name, mysql_combo.mysql_version
),
pull_before_start=True,
skip_on_pull_failure=True,
skip_if_docker_client_not_connectable=True,
Expand Down
91 changes: 52 additions & 39 deletions tests/unit/modules/test_mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"""

import logging
from textwrap import dedent
from unittest.mock import MagicMock
from unittest.mock import call
from unittest.mock import mock_open
Expand Down Expand Up @@ -776,48 +777,60 @@ def test_sanitize_comment():
"""
Test comment sanitization
"""
input_data = """/*
multiline
comment
*/
CREATE TABLE test_update (a VARCHAR(25)); # end of line comment
# example comment
insert into test_update values ("some #hash value"); -- ending comment
insert into test_update values ("crazy -- not comment"); -- another ending comment
-- another comment type
"""
expected_response = """CREATE TABLE test_update (a VARCHAR(25));
insert into test_update values ("some #hash value");
insert into test_update values ("crazy -- not comment");
"""
input_data = dedent(
"""
/*
multiline
comment
*/
CREATE TABLE test_update (a VARCHAR(25)); # end of line comment
# example comment
insert into test_update values ("some #hash value"); -- ending comment
insert into test_update values ("crazy -- not comment"); -- another ending comment
-- another comment type
"""
).strip()
expected_response = dedent(
"""\
CREATE TABLE test_update (a VARCHAR(25));
insert into test_update values ("some #hash value");
insert into test_update values ("crazy -- not comment");
"""
)
output = mysql._sanitize_comments(input_data)
assert output == expected_response

input_data = """-- --------------------------------------------------------
-- SQL Commands to set up the pmadb as described in the documentation.
--
-- This file is meant for use with MySQL 5 and above!
--
-- This script expects the user pma to already be existing. If we would put a
-- line here to create them too many users might just use this script and end
-- up with having the same password for the controluser.
--
-- This user "pma" must be defined in config.inc.php (controluser/controlpass)
--
-- Please don't forget to set up the tablenames in config.inc.php
--
-- --------------------------------------------------------
--
CREATE DATABASE IF NOT EXISTS `phpmyadmin`
DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;
USE phpmyadmin;
"""

expected_response = """CREATE DATABASE IF NOT EXISTS `phpmyadmin`
DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;
USE phpmyadmin;"""
input_data = dedent(
"""
-- --------------------------------------------------------
-- SQL Commands to set up the pmadb as described in the documentation.
--
-- This file is meant for use with MySQL 5 and above!
--
-- This script expects the user pma to already be existing. If we would put a
-- line here to create them too many users might just use this script and end
-- up with having the same password for the controluser.
--
-- This user "pma" must be defined in config.inc.php (controluser/controlpass)
--
-- Please don't forget to set up the tablenames in config.inc.php
--
-- --------------------------------------------------------
--
CREATE DATABASE IF NOT EXISTS `phpmyadmin`
DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;
USE phpmyadmin;
"""
).strip()

expected_response = dedent(
"""
CREATE DATABASE IF NOT EXISTS `phpmyadmin`
DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;
USE phpmyadmin;
"""
).strip()

output = mysql._sanitize_comments(input_data)
assert output == expected_response
Expand Down

0 comments on commit 469cccc

Please sign in to comment.