]> git.proxmox.com Git - mirror_lxc.git/blob - src/tests/state_server.c
tests: include config.h
[mirror_lxc.git] / src / tests / state_server.c
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
19 #include "config.h"
20
21 #include <alloca.h>
22 #include <errno.h>
23 #include <pthread.h>
24 #include <sched.h>
25 #include <signal.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <sys/reboot.h>
30 #include <sys/types.h>
31 #include <sys/wait.h>
32
33 #include "lxc/lxccontainer.h"
34 #include "lxctest.h"
35 #include "../lxc/compiler.h"
36
37 struct thread_args {
38 int thread_id;
39 int timeout;
40 bool success;
41 struct lxc_container *c;
42 };
43
44 __noreturn static void *state_wrapper(void *data)
45 {
46 struct thread_args *args = data;
47
48 lxc_debug("Starting state server thread %d\n", args->thread_id);
49
50 args->success = args->c->shutdown(args->c, args->timeout);
51
52 lxc_debug("State server thread %d with shutdown timeout %d returned \"%s\"\n",
53 args->thread_id, args->timeout, args->success ? "SUCCESS" : "FAILED");
54
55 pthread_exit(NULL);
56 }
57
58 int main(int argc, char *argv[])
59 {
60 int i, j;
61 pthread_attr_t attr;
62 pthread_t threads[10];
63 struct thread_args args[10];
64 struct lxc_container *c;
65 int ret = EXIT_FAILURE;
66
67 c = lxc_container_new("state-server", NULL);
68 if (!c) {
69 lxc_error("%s", "Failed to create container \"state-server\"");
70 exit(ret);
71 }
72
73 if (c->is_defined(c)) {
74 lxc_error("%s\n", "Container \"state-server\" is defined");
75 goto on_error_put;
76 }
77
78 if (!c->createl(c, "busybox", NULL, NULL, 0, NULL)) {
79 lxc_error("%s\n", "Failed to create busybox container \"state-server\"");
80 goto on_error_put;
81 }
82
83 if (!c->is_defined(c)) {
84 lxc_error("%s\n", "Container \"state-server\" is not defined");
85 goto on_error_put;
86 }
87
88 c->clear_config(c);
89
90 if (!c->load_config(c, NULL)) {
91 lxc_error("%s\n", "Failed to load config for container \"state-server\"");
92 goto on_error_stop;
93 }
94
95 if (!c->want_daemonize(c, true)) {
96 lxc_error("%s\n", "Failed to mark container \"state-server\" daemonized");
97 goto on_error_stop;
98 }
99
100 pthread_attr_init(&attr);
101
102 for (j = 0; j < 10; j++) {
103 lxc_debug("Starting state server test iteration %d\n", j);
104
105 if (!c->startl(c, 0, NULL)) {
106 lxc_error("%s\n", "Failed to start container \"state-server\" daemonized");
107 goto on_error_stop;
108 }
109
110 sleep(5);
111
112 for (i = 0; i < 10; i++) {
113 args[i].thread_id = i;
114 args[i].c = c;
115 args[i].timeout = -1;
116 /* test non-blocking shutdown request */
117 if (i == 0)
118 args[i].timeout = 0;
119
120 ret = pthread_create(&threads[i], &attr, state_wrapper, (void *) &args[i]);
121 if (ret != 0)
122 goto on_error_stop;
123 }
124
125 for (i = 0; i < 10; i++) {
126 ret = pthread_join(threads[i], NULL);
127 if (ret != 0)
128 goto on_error_stop;
129
130 if (!args[i].success) {
131 lxc_error("State server thread %d failed\n", args[i].thread_id);
132 goto on_error_stop;
133 }
134 }
135 }
136
137 ret = EXIT_SUCCESS;
138
139 on_error_stop:
140 if (c->is_running(c) && !c->stop(c))
141 lxc_error("%s\n", "Failed to stop container \"state-server\"");
142
143 if (!c->destroy(c))
144 lxc_error("%s\n", "Failed to destroy container \"state-server\"");
145
146 on_error_put:
147 lxc_container_put(c);
148 if (ret == EXIT_SUCCESS)
149 lxc_debug("%s\n", "All state server tests passed");
150
151 exit(ret);
152 }