File: test_io_priority.py

package info (click to toggle)
crun 1.26-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,356 kB
  • sloc: ansic: 70,844; python: 14,125; sh: 5,122; makefile: 928
file content (177 lines) | stat: -rwxr-xr-x 5,466 bytes parent folder | download
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/bin/env python3
# crun - OCI runtime written in C
#
# Copyright (C) 2017, 2018, 2019 Giuseppe Scrivano <giuseppe@scrivano.org>
# crun 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 2 of the License, or
# (at your option) any later version.
#
# crun 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 crun.  If not, see <http://www.gnu.org/licenses/>.

import subprocess
from tests_utils import *


def test_io_priority_best_effort():
    """Test I/O priority with best effort class."""

    conf = base_config()
    add_all_namespaces(conf)

    # Set I/O priority to best effort class with priority 4
    conf['process']['ioPriority'] = {
        'class': 'IOPRIO_CLASS_BE',
        'priority': 4
    }

    conf['process']['args'] = ['/init', 'true']

    try:
        out, _ = run_and_get_output(conf, hide_stderr=True)
        return 0

    except subprocess.CalledProcessError as e:
        output = e.output.decode('utf-8', errors='ignore') if e.output else ''
        if "ioprio" in output.lower() or "priority" in output.lower():
            return (77, "I/O priority not supported")
        logger.info("test failed: %s", e)
        return -1
    except Exception as e:
        logger.info("test failed: %s", e)
        return -1


def test_io_priority_realtime():
    """Test I/O priority with realtime class."""
    if is_rootless():
        return (77, "IOPRIO_CLASS_RT requires CAP_SYS_ADMIN")

    conf = base_config()
    add_all_namespaces(conf)

    # Set I/O priority to realtime class with priority 2
    conf['process']['ioPriority'] = {
        'class': 'IOPRIO_CLASS_RT',
        'priority': 2
    }

    conf['process']['args'] = ['/init', 'true']

    try:
        out, _ = run_and_get_output(conf, hide_stderr=True)
        return 0

    except subprocess.CalledProcessError as e:
        output = e.output.decode('utf-8', errors='ignore') if e.output else ''
        if "ioprio" in output.lower() or "priority" in output.lower() or "permission" in output.lower():
            return (77, "I/O realtime priority not supported")
        logger.info("test failed: %s", e)
        return -1
    except Exception as e:
        logger.info("test failed: %s", e)
        return -1


def test_io_priority_idle():
    """Test I/O priority with idle class."""

    conf = base_config()
    add_all_namespaces(conf)

    # Set I/O priority to idle class
    conf['process']['ioPriority'] = {
        'class': 'IOPRIO_CLASS_IDLE'
    }

    conf['process']['args'] = ['/init', 'true']

    try:
        out, _ = run_and_get_output(conf, hide_stderr=True)
        return 0

    except subprocess.CalledProcessError as e:
        output = e.output.decode('utf-8', errors='ignore') if e.output else ''
        if "ioprio" in output.lower() or "priority" in output.lower():
            return (77, "I/O priority not supported")
        logger.info("test failed: %s", e)
        return -1
    except Exception as e:
        logger.info("test failed: %s", e)
        return -1


def test_io_priority_best_effort_high():
    """Test I/O priority with high best effort priority."""

    conf = base_config()
    add_all_namespaces(conf)

    # Set I/O priority to best effort with priority 0 (highest)
    conf['process']['ioPriority'] = {
        'class': 'IOPRIO_CLASS_BE',
        'priority': 0
    }

    conf['process']['args'] = ['/init', 'true']

    try:
        out, _ = run_and_get_output(conf, hide_stderr=True)
        return 0

    except subprocess.CalledProcessError as e:
        output = e.output.decode('utf-8', errors='ignore') if e.output else ''
        if "ioprio" in output.lower() or "priority" in output.lower():
            return (77, "I/O priority not supported")
        logger.info("test failed: %s", e)
        return -1
    except Exception as e:
        logger.info("test failed: %s", e)
        return -1


def test_io_priority_best_effort_low():
    """Test I/O priority with low best effort priority."""

    conf = base_config()
    add_all_namespaces(conf)

    # Set I/O priority to best effort with priority 7 (lowest)
    conf['process']['ioPriority'] = {
        'class': 'IOPRIO_CLASS_BE',
        'priority': 7
    }

    conf['process']['args'] = ['/init', 'true']

    try:
        out, _ = run_and_get_output(conf, hide_stderr=True)
        return 0

    except subprocess.CalledProcessError as e:
        output = e.output.decode('utf-8', errors='ignore') if e.output else ''
        if "ioprio" in output.lower() or "priority" in output.lower():
            return (77, "I/O priority not supported")
        logger.info("test failed: %s", e)
        return -1
    except Exception as e:
        logger.info("test failed: %s", e)
        return -1


all_tests = {
    "io-priority-best-effort": test_io_priority_best_effort,
    "io-priority-realtime": test_io_priority_realtime,
    "io-priority-idle": test_io_priority_idle,
    "io-priority-best-effort-high": test_io_priority_best_effort_high,
    "io-priority-best-effort-low": test_io_priority_best_effort_low,
}

if __name__ == "__main__":
    tests_main(all_tests)