File: test_create.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 (176 lines) | stat: -rwxr-xr-x 5,098 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
#!/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 json
import os
import time
from tests_utils import *


def test_create_start():
    """Test create and start commands separately."""
    conf = base_config()
    conf['process']['args'] = ['/init', 'echo', 'create-start-test']
    add_all_namespaces(conf)

    cid = None
    proc = None
    try:
        proc, cid = run_and_get_output(conf, hide_stderr=True, command='create', use_popen=True)

        # Wait for container to be in created state
        for i in range(50):
            try:
                state = json.loads(run_crun_command(["state", cid]))
                if state['status'] == 'created':
                    break
            except:
                pass
            time.sleep(0.1)

        # Start container
        run_crun_command(["start", cid])

        # Get output
        out, _ = proc.communicate(timeout=10)
        if b'create-start-test' not in out:
            logger.info("expected output not found: %s", out)
            return -1

        return 0

    except Exception as e:
        logger.info("test failed: %s", e)
        return -1
    finally:
        if proc is not None:
            try:
                proc.kill()
            except:
                pass
        if cid is not None:
            try:
                run_crun_command(["delete", "-f", cid])
            except:
                pass


def test_create_delete_without_start():
    """Test creating and deleting a container without starting it."""
    conf = base_config()
    conf['process']['args'] = ['/init', 'pause']
    add_all_namespaces(conf)

    cid = None
    proc = None
    try:
        proc, cid = run_and_get_output(conf, hide_stderr=True, command='create', use_popen=True)

        # Wait for container to be in created state
        for i in range(50):
            try:
                state = json.loads(run_crun_command(["state", cid]))
                if state['status'] == 'created':
                    break
            except:
                pass
            time.sleep(0.1)

        # Delete without starting
        run_crun_command(['delete', '-f', cid])
        cid = None

        return 0

    except Exception as e:
        logger.info("test failed: %s", e)
        return -1
    finally:
        if proc is not None:
            try:
                proc.kill()
            except:
                pass
        if cid is not None:
            try:
                run_crun_command(["delete", "-f", cid])
            except:
                pass


def test_create_with_annotations():
    """Test create command with annotations in config."""
    conf = base_config()
    conf['process']['args'] = ['/init', 'true']
    add_all_namespaces(conf)
    conf['annotations'] = {
        'test.annotation.key': 'test-value',
        'another.annotation': 'another-value'
    }

    cid = None
    proc = None
    try:
        proc, cid = run_and_get_output(conf, hide_stderr=True, command='create', use_popen=True)

        # Wait for container to be in created state
        for i in range(50):
            try:
                state = json.loads(run_crun_command(["state", cid]))
                if state['status'] == 'created':
                    break
            except:
                pass
            time.sleep(0.1)

        # Check state includes annotations
        state = json.loads(run_crun_command(['state', cid]))
        annotations = state.get('annotations', {})
        if annotations.get('test.annotation.key') != 'test-value':
            logger.info("annotation not preserved in state: %s", annotations)
            # Continue anyway - annotation preservation is optional

        # Start container
        run_crun_command(['start', cid])
        proc.communicate(timeout=10)

        return 0

    except Exception as e:
        logger.info("test failed: %s", e)
        return -1
    finally:
        if proc is not None:
            try:
                proc.kill()
            except:
                pass
        if cid is not None:
            try:
                run_crun_command(["delete", "-f", cid])
            except:
                pass


all_tests = {
    "create-start": test_create_start,
    "create-delete-without-start": test_create_delete_without_start,
    "create-with-annotations": test_create_with_annotations,
}

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