Skip to content

Commit dfda5ff

Browse files
committed
Add initial Docker tests
1 parent f5fa6ae commit dfda5ff

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
grafana-client==4.1.*
22
opensearch-py==2.5.*
3+
packaging==24.*
34
prometheus-api-client==0.5.*
5+
pytest-subtests==0.12.*
46
pytest-testinfra==10.1.*
57
requests==2.31.*
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Copyright (c) 2024 StackHPC Ltd.
2+
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
# not use this file except in compliance with the License. You may obtain
5+
# a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.
14+
15+
import json
16+
import os
17+
from packaging.version import parse
18+
import pytest
19+
20+
21+
@pytest.fixture
22+
def docker_info(host, scope="session"):
23+
"""Pytest fixture that provides the output of 'docker info'."""
24+
with host.sudo("stack"):
25+
docker_info = host.check_output("docker info --format json")
26+
return json.loads(docker_info)
27+
28+
29+
def test_docker_version(host):
30+
"""Check that Docker is accessible and optionally check version."""
31+
min_version = os.environ.get("DOCKER_VERSION_MIN")
32+
max_version = os.environ.get("DOCKER_VERSION_MAX")
33+
with host.sudo("stack"):
34+
client_version = parse(host.docker.client_version())
35+
server_version = parse(host.docker.server_version())
36+
if min_version:
37+
min_version = parse(min_version)
38+
assert client_version >= min_version
39+
assert server_version >= min_version
40+
if max_version:
41+
max_version = parse(max_version)
42+
assert client_version <= max_version
43+
assert server_version <= max_version
44+
45+
46+
def test_docker_containers(subtests, host):
47+
"""Check that Docker containers are healthy."""
48+
with host.sudo("stack"):
49+
docker_containers = host.docker.get_containers()
50+
for container in docker_containers:
51+
# Use the subtests fixture to create a dynamically parametrised test
52+
# based on the containers on the system.
53+
with subtests.test(msg="container=" + container.name):
54+
state = container.inspect()["State"]
55+
assert state["Running"]
56+
assert not state["Restarting"]
57+
assert not state["Dead"]
58+
assert not state["OOMKilled"]
59+
if "Health" in state:
60+
assert state["Health"]["Status"] == "healthy"
61+
if "HostConfig" in state:
62+
assert state["HostConfig"]["LogConfig"]["Type"] == "json-file"
63+
assert "max-file" in state["HostConfig"]["LogConfig"]["Config"]
64+
assert "max-size" in state["HostConfig"]["LogConfig"]["Config"]
65+
66+
67+
def test_docker_driver(docker_info):
68+
"""Check that Docker is using the overlay2 storage driver."""
69+
assert docker_info["Driver"] == "overlay2"
70+
71+
72+
def test_no_bridge_network_exists(host):
73+
"""Check that no bridge network exists."""
74+
with host.sudo("stack"):
75+
docker_networks = host.check_output("docker network ls --format json")
76+
for network in docker_networks.splitlines():
77+
network = json.loads(network)
78+
assert network["Name"] != "bridge"
79+
assert network["Driver"] != "bridge"
80+
81+
82+
def test_ip_forwarding_disabled(docker_info):
83+
"""Check that IP forwarding is disabled."""
84+
assert not docker_info["IPv4Forwarding"]
85+
86+
87+
def test_iptables_disabled(docker_info):
88+
"""Check that IPTables manipulation is disabled."""
89+
assert not docker_info["BridgeNfIptables"]
90+
assert not docker_info["BridgeNfIp6tables"]
91+
92+
93+
def test_live_restore_enabled(docker_info):
94+
"""Check that live restore is enabled."""
95+
assert docker_info["LiveRestoreEnabled"]

0 commit comments

Comments
 (0)