|
| 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 os |
| 16 | +import pytest |
| 17 | + |
| 18 | + |
| 19 | +def test_selinux(host): |
| 20 | + """Check that SELinux is enabled and permissive on supported systems.""" |
| 21 | + # Adapted from Kayobe host configure tests: |
| 22 | + # https://opendev.org/openstack/kayobe/src/branch/master/playbooks/kayobe-overcloud-host-configure-base/tests/test_overcloud_host_configure.py |
| 23 | + if host.system_info.distribution in {"debian", "ubuntu"}: |
| 24 | + pytest.skip(reason="SELinux is not supported on Debian or Ubuntu") |
| 25 | + # Desired state: enforcing, permissive or disabled |
| 26 | + expected_state = os.environ["SELINUX_STATE"] |
| 27 | + assert expected_state in {"enforcing", "permissive", "disabled"} |
| 28 | + expected_status = "disabled" if expected_state == "disabled" else "enabled" |
| 29 | + expected_mode = expected_state |
| 30 | + selinux = host.check_output("sestatus") |
| 31 | + selinux = selinux.splitlines() |
| 32 | + # Remove duplicate whitespace characters in output |
| 33 | + selinux = [" ".join(x.split()) for x in selinux] |
| 34 | + |
| 35 | + assert f"SELinux status: {expected_status}" in selinux |
| 36 | + if expected_status == "enabled": |
| 37 | + assert f"Current mode: {expected_mode}" in selinux |
| 38 | + assert f"Mode from config file: {expected_mode}" in selinux |
0 commit comments