]> git.proxmox.com Git - mirror_lxc.git/blame - src/tests/console_log.c
console-log test: make sure container is stopped before restarting
[mirror_lxc.git] / src / tests / console_log.c
CommitLineData
a52c1c68
CB
1/* liblxcapi
2 *
3 * Copyright © 2017 Christian Brauner <christian.brauner@ubuntu.com>.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2, as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
e49c56d6
CB
19#include "config.h"
20
a52c1c68
CB
21#include <errno.h>
22#include <fcntl.h>
23#include <inttypes.h>
24#include <signal.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <unistd.h>
29#include <sys/stat.h>
30#include <sys/types.h>
31#include <sys/wait.h>
32
33#include <lxc/lxccontainer.h>
34
35#include "lxctest.h"
36#include "utils.h"
37
38int main(int argc, char *argv[])
39{
23e0d9af
CB
40 int ret;
41 struct stat st_log_file;
a52c1c68
CB
42 struct lxc_container *c;
43 struct lxc_console_log log;
44 bool do_unlink = false;
45 int fret = EXIT_FAILURE;
46
47 c = lxc_container_new("console-log", NULL);
48 if (!c) {
49 lxc_error("%s", "Failed to create container \"console-log\"");
50 exit(fret);
51 }
52
53 if (c->is_defined(c)) {
54 lxc_error("%s\n", "Container \"console-log\" is defined");
55 goto on_error_put;
56 }
57
58 /* Set console ringbuffer size. */
8a2404e9
CB
59 if (!c->set_config_item(c, "lxc.console.buffer.size", "4096")) {
60 lxc_error("%s\n", "Failed to set config item \"lxc.console.buffer.size\"");
a52c1c68
CB
61 goto on_error_put;
62 }
63
8a2404e9 64 /* Set console log file. */
a52c1c68
CB
65 if (!c->set_config_item(c, "lxc.console.logfile", "/tmp/console-log.log")) {
66 lxc_error("%s\n", "Failed to set config item \"lxc.console.logfile\"");
67 goto on_error_put;
68 }
69
70 if (!c->createl(c, "busybox", NULL, NULL, 0, NULL)) {
71 lxc_error("%s\n", "Failed to create busybox container \"console-log\"");
72 goto on_error_put;
73 }
74
75 if (!c->is_defined(c)) {
76 lxc_error("%s\n", "Container \"console-log\" is not defined");
77 goto on_error_put;
78 }
79
80 c->clear_config(c);
81
82 if (!c->load_config(c, NULL)) {
83 lxc_error("%s\n", "Failed to load config for container \"console-log\"");
84 goto on_error_stop;
85 }
86
87 if (!c->want_daemonize(c, true)) {
88 lxc_error("%s\n", "Failed to mark container \"console-log\" daemonized");
89 goto on_error_stop;
90 }
91
92 if (!c->startl(c, 0, NULL)) {
93 lxc_error("%s\n", "Failed to start container \"console-log\" daemonized");
94 goto on_error_stop;
95 }
96
97 /* Leave some time for the container to write something to the log. */
98 sleep(2);
99
100 /* Retrieve the contents of the ringbuffer. */
101 log.clear = false;
102 log.read_max = &(uint64_t){0};
103 log.read = true;
a52c1c68
CB
104
105 ret = c->console_log(c, &log);
106 if (ret < 0) {
107 lxc_error("%s - Failed to retrieve console log \n", strerror(-ret));
108 goto on_error_stop;
109 } else {
110 lxc_debug("Retrieved %" PRIu64
111 " bytes from console log. Contents are \"%s\"\n",
112 *log.read_max, log.data);
94019577 113 free(log.data);
a52c1c68
CB
114 }
115
116 /* Leave another two seconds to ensure boot is finished. */
117 sleep(2);
118
119 /* Clear the console ringbuffer. */
120 log.read_max = &(uint64_t){0};
121 log.read = false;
a52c1c68
CB
122 log.clear = true;
123 ret = c->console_log(c, &log);
124 if (ret < 0) {
125 if (ret != -ENODATA) {
126 lxc_error("%s - Failed to retrieve console log\n", strerror(-ret));
127 goto on_error_stop;
128 }
129 }
130
131 if (!c->stop(c)) {
132 lxc_error("%s\n", "Failed to stop container \"console-log\"");
133 goto on_error_stop;
134 }
135
5894cf31
SH
136 if (!c->wait(c, "STOPPED", 5)) {
137 lxc_error("%s\n", "Failed waiting for container \"console-log\" to stop");
138 goto on_error_stop;
139 }
140
8a2404e9
CB
141 c->clear_config(c);
142
143 if (!c->load_config(c, NULL)) {
144 lxc_error("%s\n", "Failed to load config for container \"console-log\"");
145 goto on_error_stop;
146 }
147
a52c1c68
CB
148 if (!c->startl(c, 0, NULL)) {
149 lxc_error("%s\n", "Failed to start container \"console-log\" daemonized");
150 goto on_error_destroy;
151 }
a52c1c68
CB
152
153 /* Leave some time for the container to write something to the log. */
154 sleep(2);
155
8a2404e9
CB
156 ret = stat("/tmp/console-log.log", &st_log_file);
157 if (ret < 0) {
158 lxc_error("%s - Failed to stat on-disk logfile\n", strerror(errno));
159 goto on_error_stop;
160 }
161
162 /* Turn on rotation for the console log file. */
163 if (!c->set_config_item(c, "lxc.console.rotate", "1")) {
164 lxc_error("%s\n", "Failed to set config item \"lxc.console.rotate\"");
165 goto on_error_put;
166 }
167
168 if (!c->stop(c)) {
169 lxc_error("%s\n", "Failed to stop container \"console-log\"");
170 goto on_error_stop;
171 }
172
5894cf31
SH
173 if (!c->wait(c, "STOPPED", 5)) {
174 lxc_error("%s\n", "Failed waiting for container \"console-log\" to stop");
175 goto on_error_stop;
176 }
177
8a2404e9
CB
178 if (!c->startl(c, 0, NULL)) {
179 lxc_error("%s\n", "Failed to start container \"console-log\" daemonized");
180 goto on_error_destroy;
181 }
182
183 /* Leave some time for the container to write something to the log. */
184 sleep(2);
185
a52c1c68
CB
186 fret = 0;
187
188on_error_stop:
189 if (c->is_running(c) && !c->stop(c))
190 lxc_error("%s\n", "Failed to stop container \"console-log\"");
191
192on_error_destroy:
193 if (!c->destroy(c))
194 lxc_error("%s\n", "Failed to destroy container \"console-log\"");
195
196on_error_put:
197 lxc_container_put(c);
198 if (do_unlink) {
199 ret = unlink("/tmp/console-log.log");
200 if (ret < 0)
8a2404e9
CB
201 lxc_error("%s - Failed to remove container log file\n",
202 strerror(errno));
203
204 ret = unlink("/tmp/console-log.log.1");
205 if (ret < 0)
206 lxc_error("%s - Failed to remove container log file\n",
207 strerror(errno));
a52c1c68
CB
208 }
209 exit(fret);
210}