|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8; py-indent-offset: 4 -*- |
| 3 | +# |
| 4 | +# Author: Linuxfabrik GmbH, Zurich, Switzerland |
| 5 | +# Contact: info (at) linuxfabrik (dot) ch |
| 6 | +# https://www.linuxfabrik.ch/ |
| 7 | +# License: The Unlicense, see LICENSE file. |
| 8 | + |
| 9 | +# https://github.com/Linuxfabrik/lib/blob/main/CONTRIBUTING.md |
| 10 | + |
| 11 | +"""Tests for lib.db_mysql. |
| 12 | +
|
| 13 | +The version-banner parsing is pure and runs without a database. The |
| 14 | +connection-based helpers are exercised against a real MariaDB LTS server |
| 15 | +in a Red Hat family container (quay.io/sclorg) via lib.lftest, so this is |
| 16 | +a container test (run via tools/run-container-tests; podman required). |
| 17 | +""" |
| 18 | + |
| 19 | +import os |
| 20 | +import sys |
| 21 | +import unittest |
| 22 | + |
| 23 | +sys.path.insert(0, '..') |
| 24 | + |
| 25 | +import lib.db_mysql as db_mysql |
| 26 | +import lib.lftest |
| 27 | + |
| 28 | +HERE = os.path.dirname(os.path.abspath(__file__)) |
| 29 | +CONTAINERFILES_DIR = os.path.join(HERE, 'containerfiles') |
| 30 | +CONTAINERFILES = sorted( |
| 31 | + name |
| 32 | + for name in os.listdir(CONTAINERFILES_DIR) |
| 33 | + if os.path.isfile(os.path.join(CONTAINERFILES_DIR, name)) |
| 34 | +) |
| 35 | + |
| 36 | + |
| 37 | +class TestVersionBanner(unittest.TestCase): |
| 38 | + """Pure version-banner parsing, no database needed.""" |
| 39 | + |
| 40 | + def test_mariadb_distrib_banner(self): |
| 41 | + self.assertEqual( |
| 42 | + db_mysql.get_server_info(banner='mysql Ver 15.1 Distrib 10.11.8-MariaDB, for Linux'), |
| 43 | + {'flavor': 'mariadb', 'version': '10.11.8'}, |
| 44 | + ) |
| 45 | + |
| 46 | + def test_mysql_banner(self): |
| 47 | + self.assertEqual( |
| 48 | + db_mysql.get_server_info( |
| 49 | + banner='mysql Ver 8.0.36 for Linux on x86_64 (MySQL Community Server - GPL)' |
| 50 | + ), |
| 51 | + {'flavor': 'mysql', 'version': '8.0.36'}, |
| 52 | + ) |
| 53 | + |
| 54 | + |
| 55 | +class TestLod2Dict(unittest.TestCase): |
| 56 | + def test_maps_variable_name_to_value(self): |
| 57 | + self.assertEqual( |
| 58 | + db_mysql.lod2dict( |
| 59 | + [{'Variable_name': 'a', 'Value': '1'}, {'Variable_name': 'b', 'Value': '2'}] |
| 60 | + ), |
| 61 | + {'a': '1', 'b': '2'}, |
| 62 | + ) |
| 63 | + |
| 64 | + def test_empty(self): |
| 65 | + self.assertEqual(db_mysql.lod2dict([]), {}) |
| 66 | + |
| 67 | + |
| 68 | +class TestContainer(unittest.TestCase): |
| 69 | + pass |
| 70 | + |
| 71 | + |
| 72 | +def _check_image(test, containerfile): |
| 73 | + path = os.path.join(CONTAINERFILES_DIR, containerfile) |
| 74 | + print(f'\n=== Testing {containerfile} ===', flush=True) |
| 75 | + with lib.lftest.run_mysql_compatible_from_containerfile(path) as (_container, defaults_file): |
| 76 | + ok, conn = db_mysql.connect({'defaults_file': defaults_file}) |
| 77 | + test.assertTrue(ok, conn) |
| 78 | + try: |
| 79 | + # select(): basic round trip. |
| 80 | + ok, rows = db_mysql.select(conn, 'SELECT 1 AS one') |
| 81 | + test.assertTrue(ok, rows) |
| 82 | + test.assertEqual(rows[0]['one'], 1) |
| 83 | + |
| 84 | + # parameterized select (positional %s + list, per db_mysql.select). |
| 85 | + ok, rows = db_mysql.select(conn, 'SELECT %s AS n', data=[42]) |
| 86 | + test.assertTrue(ok, rows) |
| 87 | + test.assertEqual(rows[0]['n'], 42) |
| 88 | + |
| 89 | + # get_all_status() / get_all_variables() return a dict (coe-unwrapped). |
| 90 | + status = db_mysql.get_all_status(conn) |
| 91 | + test.assertIsInstance(status, dict) |
| 92 | + test.assertIn('Uptime', status) |
| 93 | + |
| 94 | + variables = db_mysql.get_all_variables(conn) |
| 95 | + test.assertIsInstance(variables, dict) |
| 96 | + test.assertIn('version', variables) |
| 97 | + |
| 98 | + # get_engines(): InnoDB must be present on a default MariaDB. |
| 99 | + engines = db_mysql.get_engines(conn) |
| 100 | + test.assertTrue(any('innodb' in str(e).lower() for e in engines), engines) |
| 101 | + |
| 102 | + # get_replica_status(): None on a standalone server. |
| 103 | + test.assertIsNone(db_mysql.get_replica_status(conn)) |
| 104 | + |
| 105 | + # has_is_role_column(): returns a definite bool against the live server |
| 106 | + # (the exact value is server/version dependent). |
| 107 | + test.assertIsInstance(db_mysql.has_is_role_column(conn), bool) |
| 108 | + |
| 109 | + # root (via the defaults file) has the SELECT smoke-test privilege. |
| 110 | + ok, _ = db_mysql.check_select_privileges(conn) |
| 111 | + test.assertTrue(ok) |
| 112 | + |
| 113 | + test.assertEqual(db_mysql.commit(conn), (True, None)) |
| 114 | + finally: |
| 115 | + db_mysql.close(conn) |
| 116 | + |
| 117 | + |
| 118 | +lib.lftest.attach_each(TestContainer, CONTAINERFILES, _check_image) |
| 119 | + |
| 120 | + |
| 121 | +if __name__ == '__main__': |
| 122 | + unittest.main() |
0 commit comments