]> git.meshlink.io Git - meshlink/blob - test/blackbox/common/containers.c
Add the blackbox container based test suite.
[meshlink] / test / blackbox / common / containers.c
1 /*
2     containers.h -- Container Management API
3     Copyright (C) 2018  Guus Sliepen <guus@meshlink.io>
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 as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include <stdlib.h>
21 #include <assert.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <pthread.h>
25 #include "containers.h"
26 #include "common_handlers.h"
27
28 char *lxc_path = NULL;
29 char *choose_arch;
30 static char container_ips[10][100];
31
32 /* Return the handle to an existing container after finding it by container name */
33 struct lxc_container *find_container(const char *name) {
34         struct lxc_container **test_containers;
35         char **container_names;
36         int num_containers, i;
37
38         assert((num_containers = list_all_containers(lxc_path, &container_names,
39                                  &test_containers)) != -1);
40
41         for(i = 0; i < num_containers; i++) {
42                 if(strcmp(container_names[i], name) == 0) {
43                         return test_containers[i];
44                 }
45         }
46
47         return NULL;
48 }
49
50 /* Rename a Container */
51 void rename_container(const char *old_name, const char *new_name) {
52         char rename_command[200];
53         int rename_status;
54         struct lxc_container *old_container;
55
56         /* Stop the old container if its still running */
57         assert(old_container = find_container(old_name));
58         old_container->shutdown(old_container, CONTAINER_SHUTDOWN_TIMEOUT);
59         /* Call stop() in case shutdown() fails - one of these two will always succeed */
60         old_container->stop(old_container);
61         /* Rename the Container */
62         /* TO DO: Perform this operation using the LXC API - currently does not work via the API,
63             need to investigate and determine why it doesn't work, and make it work */
64         assert(snprintf(rename_command, sizeof(rename_command),
65                         "%s/" LXC_UTIL_REL_PATH "/" LXC_RENAME_SCRIPT " %s %s %s", meshlink_root_path, lxc_path,
66                         old_name, new_name) >= 0);
67         rename_status = system(rename_command);
68         PRINT_TEST_CASE_MSG("Container '%s' rename status: %d\n", old_name, rename_status);
69         assert(rename_status == 0);
70 }
71
72 /* Run 'cmd' inside the Container created for 'node' and return the first line of the output
73     or NULL if there is no output - useful when, for example, a meshlink invite is generated
74     by a node running inside a Container
75     'cmd' is run as a daemon if 'daemonize' is true - this mode is useful for running node
76     simulations in Containers
77     The caller is responsible for freeing the returned string */
78 char *run_in_container(const char *cmd, const char *node, bool daemonize) {
79         char attach_command[400];
80         char *attach_argv[4];
81         char container_find_name[100];
82         struct lxc_container *container;
83         FILE *attach_fp;
84         char *output = NULL;
85         size_t output_len;
86         int i;
87
88         assert(snprintf(container_find_name, sizeof(container_find_name), "%s_%s",
89                         state_ptr->test_case_name, node) >= 0);
90         assert(container = find_container(container_find_name));
91
92         /* Run the command within the Container, either as a daemon or foreground process */
93         /* TO DO: Perform this operation using the LXC API - currently does not work using the API
94             Need to determine why it doesn't work, and make it work */
95         if(daemonize) {
96                 for(i = 0; i < 3; i++) {
97                         assert(attach_argv[i] = malloc(DAEMON_ARGV_LEN));
98                 }
99
100                 assert(snprintf(attach_argv[0], DAEMON_ARGV_LEN, "%s/" LXC_UTIL_REL_PATH "/" LXC_RUN_SCRIPT,
101                                 meshlink_root_path) >= 0);
102                 strncpy(attach_argv[1], cmd, DAEMON_ARGV_LEN);
103                 strncpy(attach_argv[2], container->name, DAEMON_ARGV_LEN);
104                 attach_argv[3] = NULL;
105
106                 /* To daemonize, create a child process and detach it from its parent (this program) */
107                 if(fork() == 0) {
108                         assert(daemon(1, 0) != -1);    // Detach from the parent process
109                         assert(execv(attach_argv[0], attach_argv) != -1);   // Run exec() in the child process
110                 }
111
112                 for(i = 0; i < 3; i++) {
113                         free(attach_argv[i]);
114                 }
115         } else {
116                 assert(snprintf(attach_command, sizeof(attach_command),
117                                 "%s/" LXC_UTIL_REL_PATH "/" LXC_RUN_SCRIPT " \"%s\" %s", meshlink_root_path, cmd,
118                                 container->name) >= 0);
119                 assert(attach_fp = popen(attach_command, "r"));
120                 /* If the command has an output, strip out any trailing carriage returns or newlines and
121                     return it, otherwise return NULL */
122                 assert(output = malloc(100));
123                 output_len = sizeof(output);
124
125                 if(getline(&output, &output_len, attach_fp) != -1) {
126                         i = strlen(output) - 1;
127
128                         while(output[i] == '\n' || output[i] == '\r') {
129                                 i--;
130                         }
131
132                         output[i + 1] = '\0';
133                 } else {
134                         free(output);
135                         output = NULL;
136                 }
137
138                 assert(pclose(attach_fp) != -1);
139         }
140
141         return output;
142 }
143
144 /* Wait for a starting Container to obtain an IP Address, then save that IP for future use */
145 void container_wait_ip(int node) {
146         char container_name[100], lxcls_command[200];
147         struct lxc_container *test_container;
148         char *ip;
149         size_t ip_len;
150         int i;
151         bool ip_found;
152         FILE *lxcls_fp;
153
154         assert(snprintf(container_name, sizeof(container_name), "%s_%s", state_ptr->test_case_name,
155                         state_ptr->node_names[node]) >= 0);
156         assert(test_container = find_container(container_name));
157         assert(snprintf(lxcls_command, sizeof(lxcls_command),
158                         "lxc-ls -f | grep %s | tr -s ' ' | cut -d ' ' -f 5", test_container->name) >= 0);
159         PRINT_TEST_CASE_MSG("Waiting for Container '%s' to acquire IP\n", test_container->name);
160         assert(ip = malloc(20));
161         ip_len = sizeof(ip);
162         ip_found = false;
163
164         while(!ip_found) {
165                 assert(lxcls_fp = popen(lxcls_command, "r"));   // Run command
166                 assert(getline((char **)&ip, &ip_len, lxcls_fp) != -1); // Read its output
167                 /* Strip newlines and carriage returns from output */
168                 i = strlen(ip) - 1;
169
170                 while(ip[i] == '\n' || ip[i] == '\r') {
171                         i--;
172                 }
173
174                 ip[i + 1] = '\0';
175                 ip_found = (strcmp(ip, "-") != 0);  // If the output is not "-", IP has been acquired
176                 assert(pclose(lxcls_fp) != -1);
177                 sleep(1);
178         }
179
180         strncpy(container_ips[node], ip, sizeof(container_ips[node])); // Save the IP for future use
181         PRINT_TEST_CASE_MSG("Node '%s' has IP Address %s\n", state_ptr->node_names[node],
182                             container_ips[node]);
183
184         free(ip);
185 }
186
187 /* Create all required test containers */
188 void create_containers(const char *node_names[], int num_nodes) {
189         int i;
190         char container_name[100];
191         int create_status, snapshot_status, snap_restore_status;
192         struct lxc_container *first_container;
193
194         for(i = 0; i < num_nodes; i++) {
195                 assert(snprintf(container_name, sizeof(container_name), "run_%s", node_names[i]) >= 0);
196
197                 /* If this is the first Container, create it otherwise restore the snapshot saved
198                     for the first Container to create an additional Container */
199                 if(i == 0) {
200                         assert(first_container = lxc_container_new(container_name, NULL));
201                         assert(!first_container->is_defined(first_container));
202                         create_status = first_container->createl(first_container, "download", NULL, NULL,
203                                         LXC_CREATE_QUIET, "-d", "ubuntu", "-r", "trusty", "-a", choose_arch, NULL);
204                         fprintf(stderr, "Container '%s' create status: %d - %s\n", container_name,
205                                 first_container->error_num, first_container->error_string);
206                         assert(create_status);
207                         snapshot_status = first_container->snapshot(first_container, NULL);
208                         fprintf(stderr, "Container '%s' snapshot status: %d - %s\n", container_name,
209                                 first_container->error_num, first_container->error_string);
210                         assert(snapshot_status != -1);
211                 } else {
212                         snap_restore_status = first_container->snapshot_restore(first_container, "snap0",
213                                               container_name);
214                         fprintf(stderr, "Snapshot restore to Container '%s' status: %d - %s\n", container_name,
215                                 first_container->error_num, first_container->error_string);
216                         assert(snap_restore_status);
217                 }
218         }
219 }
220
221 /* Setup Containers required for a test
222     This function should always be invoked in a CMocka context
223     after setting the state of the test case to an instance of black_box_state_t */
224 void setup_containers(void **state) {
225         black_box_state_t *test_state = (black_box_state_t *)(*state);
226         int i, confbase_del_status;
227         char build_command[200];
228         struct lxc_container *test_container, *new_container;
229         char container_find_name[100];
230         char container_new_name[100];
231         int create_status, build_status;
232
233         PRINT_TEST_CASE_HEADER();
234
235         for(i = 0; i < test_state->num_nodes; i++) {
236                 /* Find the run_<node-name> Container or create it if it doesn't exist */
237                 assert(snprintf(container_find_name, sizeof(container_find_name), "run_%s",
238                                 test_state->node_names[i]) >= 0);
239
240                 if(!(test_container = find_container(container_find_name))) {
241                         assert(test_container = lxc_container_new(container_find_name, NULL));
242                         assert(!test_container->is_defined(test_container));
243                         create_status = test_container->createl(test_container, "download", NULL, NULL,
244                                                                 LXC_CREATE_QUIET, "-d", "ubuntu", "-r", "trusty", "-a", choose_arch, NULL);
245                         PRINT_TEST_CASE_MSG("Container '%s' create status: %d - %s\n", container_find_name,
246                                             test_container->error_num, test_container->error_string);
247                         assert(create_status);
248                 }
249
250                 /* Stop the Container if it's running */
251                 test_container->shutdown(test_container, CONTAINER_SHUTDOWN_TIMEOUT);
252                 /* Call stop() in case shutdown() fails
253                     One of these two calls will always succeed */
254                 test_container->stop(test_container);
255                 /* Rename the Container to make it specific to this test case,
256                     if a Container with the target name already exists, skip this step */
257                 assert(snprintf(container_new_name, sizeof(container_new_name), "%s_%s",
258                                 test_state->test_case_name, test_state->node_names[i]) >= 0);
259
260                 if(!(new_container = find_container(container_new_name))) {
261                         rename_container(test_container->name, container_new_name);
262                         assert(new_container = find_container(container_new_name));
263                 }
264
265                 /* Start the Container */
266                 assert(new_container->start(new_container, 0, NULL));
267                 /* Build the Container by copying required files into it */
268                 assert(snprintf(build_command, sizeof(build_command),
269                                 "%s/" LXC_UTIL_REL_PATH "/" LXC_BUILD_SCRIPT " %s %s %s +x >/dev/null",
270                                 meshlink_root_path, test_state->test_case_name, test_state->node_names[i],
271                                 meshlink_root_path) >= 0);
272                 build_status = system(build_command);
273                 PRINT_TEST_CASE_MSG("Container '%s' build Status: %d\n", new_container->name,
274                                     build_status);
275                 assert(build_status == 0);
276                 /* Restart the Container after building it and wait for it to acquire an IP */
277                 new_container->shutdown(new_container, CONTAINER_SHUTDOWN_TIMEOUT);
278                 new_container->stop(new_container);
279                 new_container->start(new_container, 0, NULL);
280                 container_wait_ip(i);
281         }
282 }
283
284 /* Destroy all Containers with names containing 'run_' - Containers saved for debugging will
285     have names beginning with test_case_ ; 'run_' is reserved for temporary Containers
286     intended to be re-used for the next test */
287 void destroy_containers(void) {
288         struct lxc_container **test_containers;
289         char **container_names;
290         int num_containers, i;
291
292         assert((num_containers = list_all_containers(lxc_path, &container_names,
293                                  &test_containers)) != -1);
294
295         for(i = 0; i < num_containers; i++) {
296                 if(strstr(container_names[i], "run_")) {
297                         fprintf(stderr, "Destroying Container '%s'\n", container_names[i]);
298                         /* Stop the Container - it cannot be destroyed till it is stopped */
299                         test_containers[i]->shutdown(test_containers[i], CONTAINER_SHUTDOWN_TIMEOUT);
300                         /* Call stop() in case shutdown() fails
301                             One of these two calls will always succeed */
302                         test_containers[i]->stop(test_containers[i]);
303                         /* Destroy the Container */
304                         test_containers[i]->destroy(test_containers[i]);
305                         /* call destroy_with_snapshots() in case destroy() fails
306                             one of these two calls will always succeed */
307                         test_containers[i]->destroy_with_snapshots(test_containers[i]);
308                 }
309         }
310 }
311
312 /* Restart all the Containers being used in the current test case i.e. Containers with
313     names beginning with <test-case-name>_<node-name> */
314 void restart_all_containers(void) {
315         char container_name[100];
316         struct lxc_container *test_container;
317         int i;
318
319         for(i = 0; i < state_ptr->num_nodes; i++) {
320                 /* Shutdown, then start the Container, then wait for it to acquire an IP Address */
321                 assert(snprintf(container_name, sizeof(container_name), "%s_%s", state_ptr->test_case_name,
322                                 state_ptr->node_names[i]) >= 0);
323                 assert(test_container = find_container(container_name));
324                 test_container->shutdown(test_container, CONTAINER_SHUTDOWN_TIMEOUT);
325                 test_container->stop(test_container);
326                 test_container->start(test_container, 0, NULL);
327                 container_wait_ip(i);
328         }
329 }
330
331 /* Run the gen_invite command inside the 'inviter' container to generate an invite
332     for 'invitee', and return the generated invite which is output on the terminal */
333 char *invite_in_container(const char *inviter, const char *invitee) {
334         char invite_command[200];
335         char *invite_url;
336
337         assert(snprintf(invite_command, sizeof(invite_command),
338                         "LD_LIBRARY_PATH=/home/ubuntu/test/.libs /home/ubuntu/test/gen_invite %s %s "
339                         "2> gen_invite.log", inviter, invitee) >= 0);
340         assert(invite_url = run_in_container(invite_command, inviter, false));
341         PRINT_TEST_CASE_MSG("Invite Generated from '%s' to '%s': %s\n", inviter,
342                             invitee, invite_url);
343
344         return invite_url;
345 }
346
347 /* Run the node_sim_<nodename> program inside the 'node''s container */
348 void node_sim_in_container(const char *node, const char *device_class, const char *invite_url) {
349         char node_sim_command[200];
350
351         assert(snprintf(node_sim_command, sizeof(node_sim_command),
352                         "LD_LIBRARY_PATH=/home/ubuntu/test/.libs /home/ubuntu/test/node_sim_%s %s %s %s "
353                         "1>&2 2>> node_sim_%s.log", node, node, device_class,
354                         (invite_url) ? invite_url : "", node) >= 0);
355         run_in_container(node_sim_command, node, true);
356         PRINT_TEST_CASE_MSG("node_sim_%s started in Container\n", node);
357 }
358
359 /* Run the node_sim_<nodename> program inside the 'node''s container with event handling capable*/
360 void node_sim_in_container_event(const char *node, const char *device_class,
361                                  const char *invite_url, const char *clientId, const char *import) {
362         char node_sim_command[200];
363
364         assert(snprintf(node_sim_command, sizeof(node_sim_command),
365                         "LD_LIBRARY_PATH=/home/ubuntu/test/.libs /home/ubuntu/test/node_sim_%s %s %s %s %s %s "
366                         "1>&2 2>> node_sim_%s.log", node, node, device_class,
367                         clientId, import, (invite_url) ? invite_url : "", node) >= 0);
368         run_in_container(node_sim_command, node, true);
369         PRINT_TEST_CASE_MSG("node_sim_%s(Client Id :%s) started in Container with event handling\n",
370                             node, clientId);
371         PRINT_TEST_CASE_MSG("node_sim_%s mesh event import string : %s\n",
372                             node, import);
373 }
374
375 /* Run the node_step.sh script inside the 'node''s container to send the 'sig' signal to the
376     node_sim program in the container */
377 void node_step_in_container(const char *node, const char *sig) {
378         char node_step_command[200];
379
380         assert(snprintf(node_step_command, sizeof(node_step_command),
381                         "/home/ubuntu/test/node_step.sh lt-node_sim_%s %s 1>&2 2> node_step.log",
382                         node, sig) >= 0);
383         run_in_container(node_step_command, node, false);
384         PRINT_TEST_CASE_MSG("Signal %s sent to node_sim_%s\n", sig, node);
385 }
386
387 /* Change the IP Address of the Container running 'node'
388     Changes begin from X.X.X.254 and continue iteratively till an available address is found */
389 void change_ip(int node) {
390         char *gateway_addr;
391         char new_ip[20];
392         char *netmask;
393         char *last_dot_in_ip;
394         int last_ip_byte = 254;
395         FILE *if_fp;
396         char copy_command[200];
397         char container_name[100];
398         struct lxc_container *container;
399         int copy_file_stat;
400
401         /* Get IP Address of LXC Bridge Interface - this will be set up as the Gateway Address
402             of the Static IP assigned to the Container */
403         assert(gateway_addr = get_ip(lxc_bridge));
404         /* Get Netmask of LXC Brdige Interface */
405         assert(netmask = get_netmask(lxc_bridge));
406
407         /* Replace last byte of Container's IP with 254 to form the new Container IP */
408         assert(container_ips[node]);
409         strncpy(new_ip, container_ips[node], sizeof(new_ip));
410         assert(last_dot_in_ip = strrchr(new_ip, '.'));
411         assert(snprintf(last_dot_in_ip + 1, 4, "%d", last_ip_byte) >= 0);
412
413         /* Check that the new IP does not match the Container's existing IP
414             if it does, iterate till it doesn't */
415         /* TO DO: Make sure the IP does not conflict with any other running Container */
416         while(strcmp(new_ip, container_ips[node]) == 0) {
417                 last_ip_byte--;
418                 assert(snprintf(last_dot_in_ip + 1, 4, "%d", last_ip_byte) >= 0);
419         }
420
421         /* Create new 'interfaces' file for Container */
422         assert(if_fp = fopen("interfaces", "w"));
423         fprintf(if_fp, "auto lo\n");
424         fprintf(if_fp, "iface lo inet loopback\n");
425         fprintf(if_fp, "\n");
426         fprintf(if_fp, "auto eth0\n");
427         fprintf(if_fp, "iface eth0 inet static\n");
428         fprintf(if_fp, "\taddress %s\n", new_ip);
429         fprintf(if_fp, "\tnetmask %s\n", netmask);
430         fprintf(if_fp, "\tgateway %s\n", gateway_addr);
431         assert(fclose(if_fp) != EOF);
432
433         /* Copy 'interfaces' file into Container's /etc/network path */
434         assert(snprintf(copy_command, sizeof(copy_command),
435                         "%s/" LXC_UTIL_REL_PATH "/" LXC_COPY_SCRIPT " interfaces %s_%s /etc/network/interfaces",
436                         meshlink_root_path, state_ptr->test_case_name, state_ptr->node_names[node]) >= 0);
437         copy_file_stat = system(copy_command);
438         PRINT_TEST_CASE_MSG("Container '%s_%s' 'interfaces' file copy status: %d\n",
439                             state_ptr->test_case_name, state_ptr->node_names[node], copy_file_stat);
440         assert(copy_file_stat == 0);
441
442         /* Restart Container to apply new IP Address */
443         assert(snprintf(container_name, sizeof(container_name), "%s_%s", state_ptr->test_case_name,
444                         state_ptr->node_names[node]) >= 0);
445         assert(container = find_container(container_name));
446         container->shutdown(container, CONTAINER_SHUTDOWN_TIMEOUT);
447         /* Call stop() in case shutdown() fails
448             One of these two calls with always succeed */
449         container->stop(container);
450         assert(container->start(container, 0, NULL));
451
452         strncpy(container_ips[node], new_ip, sizeof(new_ip));   // Save the new IP Addres
453         PRINT_TEST_CASE_MSG("Node '%s' IP Address changed to %s\n", state_ptr->node_names[node],
454                             container_ips[node]);
455 }