Skip to content

Commit d00f8c5

Browse files
committed
test: add happy-path container tests for mysql-innodb-buffer-pool-size, -instances, mysql-query
Three new integration tests, each spinning up quay.io/sclorg/mariadb-1011-c10s and running the plugin from the host against the container. - mysql-innodb-buffer-pool-size: boot MariaDB with innodb_buffer_pool_size pinned to 32 MiB so the assertion can target a deterministic size. - mysql-innodb-buffer-pool-instances: MariaDB 10.6+ removed the variable, so the plugin short-circuits to "Everything is ok (although nothing checked)." on current sclorg images. That is the happy path on modern MariaDB; a test for the legacy branch belongs in a follow-up against an older image. - mysql-query: run with --critical-query='SELECT 1' and assert "1 result from critical query".
1 parent e6a5991 commit d00f8c5

File tree

3 files changed

+357
-0
lines changed
  • check-plugins
    • mysql-innodb-buffer-pool-instances/unit-test
    • mysql-innodb-buffer-pool-size/unit-test
    • mysql-query/unit-test

3 files changed

+357
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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/monitoring-plugins/blob/main/CONTRIBUTING.md
10+
11+
"""Integration tests for the mysql-innodb-buffer-pool-instances plugin.
12+
13+
The plugin opens a real `pymysql` connection and reads
14+
`innodb_buffer_pool_instances` from `SHOW VARIABLES`. The variable
15+
was removed in MariaDB 10.6.0, which is the short-circuit branch
16+
the plugin explicitly supports: "Everything is ok (although
17+
nothing checked).". sclorg MariaDB 10.11 exercises exactly that
18+
branch, so it is the canonical happy path on modern MariaDB.
19+
20+
A fixture-based test for the older branch (instance count from
21+
a 10.5-era server) belongs in a separate pass against an older
22+
MariaDB image once we add one to IMAGES.
23+
24+
Container source: `quay.io/sclorg/mariadb-1011-c10s` - the Red Hat /
25+
CentOS Stream 10 sclorg packaging of MariaDB 10.11 LTS.
26+
27+
Requirements:
28+
- podman (or docker) with a reachable socket
29+
- `pip install testcontainers`
30+
- `tools/run-unit-tests` auto-sets `CONTAINER_HOST` and
31+
`TESTCONTAINERS_RYUK_DISABLED`
32+
"""
33+
34+
import os
35+
import subprocess
36+
import sys
37+
import tempfile
38+
import unittest
39+
40+
sys.path.insert(0, '..')
41+
42+
import lib.lftest
43+
from lib.globals import STATE_OK
44+
45+
46+
IMAGES = [
47+
('quay.io/sclorg/mariadb-1011-c10s', 'MariaDB 10.11'),
48+
]
49+
50+
51+
class TestCheck(unittest.TestCase):
52+
pass
53+
54+
55+
def _check_image(test, image_pair):
56+
image, label = image_pair
57+
print(f'\n=== Testing {label} ({image}) ===', flush=True)
58+
with lib.lftest.run_container(
59+
image,
60+
env={
61+
'MYSQL_ROOT_PASSWORD': 'test',
62+
'MYSQL_USER': 'test',
63+
'MYSQL_PASSWORD': 'test',
64+
'MYSQL_DATABASE': 'test',
65+
},
66+
ports=[3306],
67+
command='run-mysqld --port=3306',
68+
wait_log='port: 3306',
69+
wait_log_timeout=180,
70+
) as container:
71+
host = container.get_container_host_ip()
72+
port = container.get_exposed_port(3306)
73+
74+
with tempfile.NamedTemporaryFile(
75+
mode='w',
76+
suffix='.cnf',
77+
delete=False,
78+
) as cnf:
79+
cnf.write(
80+
f'[client]\n'
81+
f'host={host}\n'
82+
f'port={port}\n'
83+
f'user=test\n'
84+
f'password=test\n'
85+
f'database=test\n'
86+
)
87+
defaults_file = cnf.name
88+
89+
try:
90+
cmd = [
91+
'python3', '../mysql-innodb-buffer-pool-instances',
92+
f'--defaults-file={defaults_file}',
93+
]
94+
print(f'Run plugin: {" ".join(cmd)}', flush=True)
95+
result = subprocess.run(
96+
cmd,
97+
cwd=os.path.dirname(os.path.abspath(__file__)),
98+
capture_output=True,
99+
text=True,
100+
)
101+
finally:
102+
os.unlink(defaults_file)
103+
104+
combined = result.stdout + result.stderr
105+
print(f'Script output:\n{combined.strip()}', flush=True)
106+
107+
test.assertEqual(result.returncode, STATE_OK)
108+
test.assertIn('Everything is ok', combined)
109+
110+
111+
lib.lftest.attach_each(TestCheck, IMAGES, _check_image, id_func=lambda it: it[1])
112+
113+
114+
if __name__ == '__main__':
115+
unittest.main()
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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/monitoring-plugins/blob/main/CONTRIBUTING.md
10+
11+
"""Integration tests for the mysql-innodb-buffer-pool-size check plugin.
12+
13+
The plugin opens a real `pymysql` connection and reads
14+
`innodb_buffer_pool_size` / `innodb_log_file_size` from
15+
`SHOW VARIABLES`, plus InnoDB data length from
16+
`information_schema.engine_status`. A static fixture cannot drive
17+
it - we spin up an actual MariaDB server, boot it with a pinned
18+
`innodb_buffer_pool_size=32M` so the assertion can target a
19+
deterministic size, and let the plugin talk to it over TCP.
20+
21+
Container source: `quay.io/sclorg/mariadb-1011-c10s` - the Red Hat /
22+
CentOS Stream 10 sclorg packaging of MariaDB 10.11 LTS.
23+
24+
Per CONTRIBUTING's "Combine container tests with fixtures" rule,
25+
this file holds only the happy-path integration test. A freshly
26+
booted MariaDB with 32 MiB buffer pool and no user data is far
27+
below the `buffer_pool_size <= data size` warning threshold, so the
28+
plugin returns STATE_OK.
29+
30+
Requirements:
31+
- podman (or docker) with a reachable socket
32+
- `pip install testcontainers`
33+
- `tools/run-unit-tests` auto-sets `CONTAINER_HOST` and
34+
`TESTCONTAINERS_RYUK_DISABLED`
35+
"""
36+
37+
import os
38+
import subprocess
39+
import sys
40+
import tempfile
41+
import unittest
42+
43+
sys.path.insert(0, '..')
44+
45+
import lib.lftest
46+
from lib.globals import STATE_OK
47+
48+
49+
IMAGES = [
50+
('quay.io/sclorg/mariadb-1011-c10s', 'MariaDB 10.11'),
51+
]
52+
53+
54+
class TestCheck(unittest.TestCase):
55+
pass
56+
57+
58+
def _check_image(test, image_pair):
59+
image, label = image_pair
60+
print(f'\n=== Testing {label} ({image}) ===', flush=True)
61+
with lib.lftest.run_container(
62+
image,
63+
env={
64+
'MYSQL_ROOT_PASSWORD': 'test',
65+
'MYSQL_USER': 'test',
66+
'MYSQL_PASSWORD': 'test',
67+
'MYSQL_DATABASE': 'test',
68+
},
69+
ports=[3306],
70+
# Pin innodb_buffer_pool_size to exactly 32 MiB so the plugin
71+
# reports a deterministic value we can assert on.
72+
command=(
73+
'run-mysqld --port=3306 '
74+
'--innodb-buffer-pool-size=33554432'
75+
),
76+
wait_log='port: 3306',
77+
wait_log_timeout=180,
78+
) as container:
79+
host = container.get_container_host_ip()
80+
port = container.get_exposed_port(3306)
81+
82+
with tempfile.NamedTemporaryFile(
83+
mode='w',
84+
suffix='.cnf',
85+
delete=False,
86+
) as cnf:
87+
cnf.write(
88+
f'[client]\n'
89+
f'host={host}\n'
90+
f'port={port}\n'
91+
f'user=test\n'
92+
f'password=test\n'
93+
f'database=test\n'
94+
)
95+
defaults_file = cnf.name
96+
97+
try:
98+
cmd = [
99+
'python3', '../mysql-innodb-buffer-pool-size',
100+
f'--defaults-file={defaults_file}',
101+
]
102+
print(f'Run plugin: {" ".join(cmd)}', flush=True)
103+
result = subprocess.run(
104+
cmd,
105+
cwd=os.path.dirname(os.path.abspath(__file__)),
106+
capture_output=True,
107+
text=True,
108+
)
109+
finally:
110+
os.unlink(defaults_file)
111+
112+
combined = result.stdout + result.stderr
113+
print(f'Script output:\n{combined.strip()}', flush=True)
114+
115+
# The plugin reports "Data size: <x>, innodb_buffer_pool_size: <y>"
116+
# followed by a ratio line against innodb_log_file_size.
117+
test.assertEqual(result.returncode, STATE_OK)
118+
test.assertRegex(combined, r'innodb_buffer_pool_size:\s+32\.0+\s*MiB')
119+
120+
121+
lib.lftest.attach_each(TestCheck, IMAGES, _check_image, id_func=lambda it: it[1])
122+
123+
124+
if __name__ == '__main__':
125+
unittest.main()
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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/monitoring-plugins/blob/main/CONTRIBUTING.md
10+
11+
"""Integration tests for the mysql-query check plugin.
12+
13+
The plugin opens a real `pymysql` connection and runs an arbitrary
14+
user-supplied SELECT statement for the warning and/or critical
15+
branch. A static fixture cannot drive it - we spin up an actual
16+
MariaDB server and let the plugin run `SELECT 1` against it over
17+
TCP as the simplest happy-path sanity check.
18+
19+
Container source: `quay.io/sclorg/mariadb-1011-c10s` - the Red Hat /
20+
CentOS Stream 10 sclorg packaging of MariaDB 10.11 LTS.
21+
22+
Per CONTRIBUTING's "Combine container tests with fixtures" rule,
23+
this file holds only the happy-path integration test. The plugin
24+
is given no `-c` threshold, so the single returned value `1` from
25+
`SELECT 1` is never compared against anything and the result is
26+
STATE_OK by default.
27+
28+
Requirements:
29+
- podman (or docker) with a reachable socket
30+
- `pip install testcontainers`
31+
- `tools/run-unit-tests` auto-sets `CONTAINER_HOST` and
32+
`TESTCONTAINERS_RYUK_DISABLED`
33+
"""
34+
35+
import os
36+
import subprocess
37+
import sys
38+
import tempfile
39+
import unittest
40+
41+
sys.path.insert(0, '..')
42+
43+
import lib.lftest
44+
from lib.globals import STATE_OK
45+
46+
47+
IMAGES = [
48+
('quay.io/sclorg/mariadb-1011-c10s', 'MariaDB 10.11'),
49+
]
50+
51+
52+
class TestCheck(unittest.TestCase):
53+
pass
54+
55+
56+
def _check_image(test, image_pair):
57+
image, label = image_pair
58+
print(f'\n=== Testing {label} ({image}) ===', flush=True)
59+
with lib.lftest.run_container(
60+
image,
61+
env={
62+
'MYSQL_ROOT_PASSWORD': 'test',
63+
'MYSQL_USER': 'test',
64+
'MYSQL_PASSWORD': 'test',
65+
'MYSQL_DATABASE': 'test',
66+
},
67+
ports=[3306],
68+
command='run-mysqld --port=3306',
69+
wait_log='port: 3306',
70+
wait_log_timeout=180,
71+
) as container:
72+
host = container.get_container_host_ip()
73+
port = container.get_exposed_port(3306)
74+
75+
with tempfile.NamedTemporaryFile(
76+
mode='w',
77+
suffix='.cnf',
78+
delete=False,
79+
) as cnf:
80+
cnf.write(
81+
f'[client]\n'
82+
f'host={host}\n'
83+
f'port={port}\n'
84+
f'user=test\n'
85+
f'password=test\n'
86+
f'database=test\n'
87+
)
88+
defaults_file = cnf.name
89+
90+
try:
91+
cmd = [
92+
'python3', '../mysql-query',
93+
f'--defaults-file={defaults_file}',
94+
'--critical-query=SELECT 1',
95+
]
96+
print(f'Run plugin: {" ".join(cmd)}', flush=True)
97+
result = subprocess.run(
98+
cmd,
99+
cwd=os.path.dirname(os.path.abspath(__file__)),
100+
capture_output=True,
101+
text=True,
102+
)
103+
finally:
104+
os.unlink(defaults_file)
105+
106+
combined = result.stdout + result.stderr
107+
print(f'Script output:\n{combined.strip()}', flush=True)
108+
109+
test.assertEqual(result.returncode, STATE_OK)
110+
test.assertRegex(combined, r'1\s+result\s+from\s+critical\s+query')
111+
112+
113+
lib.lftest.attach_each(TestCheck, IMAGES, _check_image, id_func=lambda it: it[1])
114+
115+
116+
if __name__ == '__main__':
117+
unittest.main()

0 commit comments

Comments
 (0)