summaryrefslogtreecommitdiffstats
path: root/src/tests/intg/test_confdb.py
blob: 781c36961f941f827bcb28f903f36455d47b01a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#
# Confdb integration tests
#
# Copyright (c) 2022 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

import os
import stat
import signal
import subprocess
import time
import pytest

import config
from util import unindent


def create_conf_file(contents):
    """Create sssd.conf with specified contents"""
    with open(config.CONF_PATH, "w") as conf:
        conf.write(contents)
    os.chmod(config.CONF_PATH, stat.S_IRUSR | stat.S_IWUSR)


def cleanup_conf_file():
    """Remove sssd.conf, if it exists"""
    if os.path.lexists(config.CONF_PATH):
        os.unlink(config.CONF_PATH)


def create_conf_cleanup(request):
    """Add teardown for removing sssd.conf"""
    request.addfinalizer(cleanup_conf_file)


def create_conf_fixture(request, contents):
    """
    Create sssd.conf with specified contents and add teardown for removing it
    """
    create_conf_file(contents)
    create_conf_cleanup(request)


def create_sssd_process():
    """Start the SSSD process"""
    if subprocess.call(["sssd", "-D", "--logger=files"]) != 0:
        raise Exception("sssd start failed")


def get_sssd_pid():
    with open(config.PIDFILE_PATH, "r") as pid_file:
        pid = int(pid_file.read())
    return pid


def cleanup_sssd_process():
    """Stop the SSSD process and remove its state"""
    try:
        pid = get_sssd_pid()
        os.kill(pid, signal.SIGTERM)
        while True:
            try:
                os.kill(pid, signal.SIGCONT)
            except OSError:
                break
            time.sleep(1)
    except OSError:
        # Ignore the error.
        pass
    for path in os.listdir(config.DB_PATH):
        os.unlink(config.DB_PATH + "/" + path)
    for path in os.listdir(config.MCACHE_PATH):
        os.unlink(config.MCACHE_PATH + "/" + path)


def test_domains__domains(request):
    """
    Test that SSSD starts with explicitly configured domain.
    """
    conf = unindent("""\
        [sssd]
        services = nss, sudo
        domains = test

        [domain/test]
        id_provider = proxy
        proxy_lib_name = files
        auth_provider = none
    """)

    create_conf_fixture(request, conf)

    try:
        create_sssd_process()
    except Exception:
        assert False
    finally:
        cleanup_sssd_process()


def test_domains__enabled(request):
    """
    Test that SSSD starts without domains option.
    """
    conf = unindent("""\
        [sssd]
        services = nss, sudo

        [domain/test]
        enabled = true
        id_provider = proxy
        proxy_lib_name = files
        auth_provider = none
    """)

    create_conf_fixture(request, conf)

    try:
        create_sssd_process()
    except Exception:
        assert False
    finally:
        cleanup_sssd_process()


def test_domains__empty(request):
    """
    Test that SSSD fails without any domain enabled.
    """
    conf = unindent("""\
        [sssd]
        services = nss, sudo
        enable_files_domain = false

        [domain/test]
        id_provider = proxy
        proxy_lib_name = files
        auth_provider = none
    """)

    create_conf_fixture(request, conf)
    with pytest.raises(Exception):
        create_sssd_process()

    cleanup_sssd_process()