1#!/usr/bin/env bats
2
3load helpers
4
5function setup() {
6	teardown_busybox
7	setup_busybox
8}
9
10function teardown() {
11	teardown_busybox
12}
13
14@test "runc run [tty ptsname]" {
15	# Replace sh script with readlink.
16    sed -i 's|"sh"|"sh", "-c", "for file in /proc/self/fd/[012]; do readlink $file; done"|' config.json
17
18	# run busybox
19	runc run test_busybox
20	[ "$status" -eq 0 ]
21	[[ ${lines[0]} =~ /dev/pts/+ ]]
22	[[ ${lines[1]} =~ /dev/pts/+ ]]
23	[[ ${lines[2]} =~ /dev/pts/+ ]]
24}
25
26@test "runc run [tty owner]" {
27	# tty chmod is not doable in rootless containers without idmap.
28	# TODO: this can be made as a change to the gid test.
29	[[ "$ROOTLESS" -ne 0 ]] && requires rootless_idmap
30
31	# Replace sh script with stat.
32	sed -i 's/"sh"/"sh", "-c", "stat -c %u:%g $(tty) | tr : \\\\\\\\n"/' config.json
33
34	# run busybox
35	runc run test_busybox
36	[ "$status" -eq 0 ]
37	[[ ${lines[0]} =~ 0 ]]
38	# This is set by the default config.json (it corresponds to the standard tty group).
39	[[ ${lines[1]} =~ 5 ]]
40}
41
42@test "runc run [tty owner] ({u,g}id != 0)" {
43	# tty chmod is not doable in rootless containers without idmap.
44	[[ "$ROOTLESS" -ne 0 ]] && requires rootless_idmap
45
46	# replace "uid": 0 with "uid": 1000
47	# and do a similar thing for gid.
48	sed -i 's;"uid": 0;"uid": 1000;g' config.json
49	sed -i 's;"gid": 0;"gid": 100;g' config.json
50
51	# Replace sh script with stat.
52	sed -i 's/"sh"/"sh", "-c", "stat -c %u:%g $(tty) | tr : \\\\\\\\n"/' config.json
53
54	# run busybox
55	runc run test_busybox
56	[ "$status" -eq 0 ]
57	[[ ${lines[0]} =~ 1000 ]]
58	# This is set by the default config.json (it corresponds to the standard tty group).
59	[[ ${lines[1]} =~ 5 ]]
60}
61
62@test "runc exec [tty ptsname]" {
63	# run busybox detached
64	runc run -d --console-socket $CONSOLE_SOCKET test_busybox
65	[ "$status" -eq 0 ]
66
67	# make sure we're running
68	testcontainer test_busybox running
69
70	# run the exec
71    runc exec test_busybox sh -c 'for file in /proc/self/fd/[012]; do readlink $file; done'
72	[ "$status" -eq 0 ]
73	[[ ${lines[0]} =~ /dev/pts/+ ]]
74	[[ ${lines[1]} =~ /dev/pts/+ ]]
75	[[ ${lines[2]} =~ /dev/pts/+ ]]
76}
77
78@test "runc exec [tty owner]" {
79	# tty chmod is not doable in rootless containers without idmap.
80	# TODO: this can be made as a change to the gid test.
81	[[ "$ROOTLESS" -ne 0 ]] && requires rootless_idmap
82
83	# run busybox detached
84	runc run -d --console-socket $CONSOLE_SOCKET test_busybox
85	[ "$status" -eq 0 ]
86
87	# make sure we're running
88	testcontainer test_busybox running
89
90	# run the exec
91    runc exec test_busybox sh -c 'stat -c %u:%g $(tty) | tr : \\n'
92	[ "$status" -eq 0 ]
93	[[ ${lines[0]} =~ 0 ]]
94	[[ ${lines[1]} =~ 5 ]]
95}
96
97@test "runc exec [tty owner] ({u,g}id != 0)" {
98	# tty chmod is not doable in rootless containers without idmap.
99	[[ "$ROOTLESS" -ne 0 ]] && requires rootless_idmap
100
101	# replace "uid": 0 with "uid": 1000
102	# and do a similar thing for gid.
103	sed -i 's;"uid": 0;"uid": 1000;g' config.json
104	sed -i 's;"gid": 0;"gid": 100;g' config.json
105
106	# run busybox detached
107	runc run -d --console-socket $CONSOLE_SOCKET test_busybox
108	[ "$status" -eq 0 ]
109
110	# make sure we're running
111	testcontainer test_busybox running
112
113	# run the exec
114	runc exec test_busybox sh -c 'stat -c %u:%g $(tty) | tr : \\n'
115	[ "$status" -eq 0 ]
116	[[ ${lines[0]} =~ 1000 ]]
117	[[ ${lines[1]} =~ 5 ]]
118}
119
120@test "runc exec [tty consolesize]" {
121	# allow writing to filesystem
122	sed -i 's/"readonly": true/"readonly": false/' config.json
123
124	# run busybox detached
125	runc run -d --console-socket $CONSOLE_SOCKET test_busybox
126	[ "$status" -eq 0 ]
127
128	# make sure we're running
129	testcontainer test_busybox running
130
131	tty_info_with_consize_size=$( cat <<EOF
132{
133    "terminal": true,
134    "consoleSize": {
135	    "height": 10,
136	    "width": 110
137    },
138    "args": [
139	    "/bin/sh",
140	    "-c",
141	    "/bin/stty -a > /tmp/tty-info"
142    ],
143    "cwd": "/"
144}
145EOF
146	)
147
148	# run the exec
149	runc exec --pid-file pid.txt -d --console-socket $CONSOLE_SOCKET -p <( echo $tty_info_with_consize_size ) test_busybox
150	[ "$status" -eq 0 ]
151
152	# check the pid was generated
153	[ -e pid.txt ]
154
155	#wait user process to finish
156	timeout 1 tail --pid=$(head -n 1 pid.txt) -f /dev/null
157
158	tty_info=$( cat <<EOF
159{
160    "args": [
161	"/bin/cat",
162	"/tmp/tty-info"
163    ],
164    "cwd": "/"
165}
166EOF
167	)
168
169	# run the exec
170	runc exec -p <( echo $tty_info ) test_busybox
171	[ "$status" -eq 0 ]
172
173	# test tty width and height against original process.json
174	[[ ${lines[0]} =~ "rows 10; columns 110" ]]
175}
176
177@test "runc create [terminal=false]" {
178	# Disable terminal creation.
179	sed -i 's|"terminal": true,|"terminal": false,|g' config.json
180	# Replace sh script with sleep.
181    sed -i 's|"sh"|"sleep", "1000s"|' config.json
182
183	# Make sure that the handling of detached IO is done properly. See #1354.
184	__runc create test_busybox
185
186	# Start the command.
187	runc start test_busybox
188	[ "$status" -eq 0 ]
189
190	testcontainer test_busybox running
191
192	# Kill the container.
193	runc kill test_busybox KILL
194	[ "$status" -eq 0 ]
195}
196
197@test "runc run [terminal=false]" {
198	# Disable terminal creation.
199	sed -i 's|"terminal": true,|"terminal": false,|g' config.json
200	# Replace sh script with sleep.
201    sed -i 's|"sh"|"sleep", "1000s"|' config.json
202
203	# Make sure that the handling of non-detached IO is done properly. See #1354.
204	(
205		__runc run test_busybox
206	) &
207
208	wait_for_container 15 1 test_busybox
209	testcontainer test_busybox running
210
211	# Kill the container.
212	runc kill test_busybox KILL
213	[ "$status" -eq 0 ]
214}
215
216@test "runc run -d [terminal=false]" {
217	# Disable terminal creation.
218	sed -i 's|"terminal": true,|"terminal": false,|g' config.json
219	# Replace sh script with sleep.
220    sed -i 's|"sh"|"sleep", "1000s"|' config.json
221
222	# Make sure that the handling of detached IO is done properly. See #1354.
223	__runc run -d test_busybox
224
225	testcontainer test_busybox running
226
227	# Kill the container.
228	runc kill test_busybox KILL
229	[ "$status" -eq 0 ]
230}
231