Add test framework and some tests.
[nbdkit/ericb.git] / tests / test.c
blob2c0de55fbb411bd6fa712e8c382eb6298db65b7a
1 /* nbdkit
2 * Copyright (C) 2013 Red Hat Inc.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * * Neither the name of Red Hat nor the names of its contributors may be
17 * used to endorse or promote products derived from this software without
18 * specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
27 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include <config.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <stdarg.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include <sys/types.h>
42 #include <sys/wait.h>
43 #include <signal.h>
44 #include <errno.h>
46 #include "test.h"
48 static char tmpdir[] = "/tmp/nbdkitXXXXXX";
49 static char sockpath[] = "/tmp/nbdkitXXXXXX/sock";
50 static char unixsockpath[] = "unix:/tmp/nbdkitXXXXXX/sock";
51 static char pidpath[] = "/tmp/nbdkitXXXXXX/pid";
53 pid_t pid = 0;
54 const char *server[2] = { unixsockpath, NULL };
56 static void
57 cleanup (void)
59 if (pid > 0)
60 kill (pid, SIGTERM);
62 unlink (pidpath);
63 unlink (sockpath);
64 rmdir (tmpdir);
67 int
68 test_start_nbdkit (const char *plugin, ...)
70 size_t i, len;
72 if (mkdtemp (tmpdir) == NULL) {
73 perror ("mkdtemp");
74 return -1;
76 len = strlen (tmpdir);
77 memcpy (sockpath, tmpdir, len);
78 memcpy (unixsockpath+5, tmpdir, len);
79 memcpy (pidpath, tmpdir, len);
81 pid = fork ();
82 if (pid == 0) { /* Child (nbdkit). */
83 const char *p;
84 const int MAX_ARGS = 64;
85 const char *argv[MAX_ARGS+1];
86 va_list args;
88 argv[0] = "nbdkit";
89 argv[1] = "-U";
90 argv[2] = sockpath;
91 argv[3] = "-P";
92 argv[4] = pidpath;
93 argv[5] = "-f";
94 argv[6] = plugin;
95 i = 7;
97 va_start (args, plugin);
98 while ((p = va_arg (args, const char *)) != NULL) {
99 if (i >= MAX_ARGS)
100 abort ();
101 argv[i] = p;
102 ++i;
104 va_end (args);
105 argv[i] = NULL;
107 execvp ("../src/nbdkit", (char **) argv);
108 perror ("exec: nbdkit");
109 _exit (EXIT_FAILURE);
112 /* Ensure nbdkit is killed and temporary files are deleted when the
113 * main program exits.
115 atexit (cleanup);
117 /* Wait for the pidfile to turn up, which indicates that nbdkit has
118 * started up successfully and is ready to serve requests. However
119 * if 'pid' exits in this time it indicates a failure to start up.
120 * Also there is a timeout in case nbdkit hangs.
122 for (i = 0; i < NBDKIT_START_TIMEOUT; ++i) {
123 if (waitpid (pid, NULL, WNOHANG) == pid)
124 goto early_exit;
126 if (kill (pid, 0) == -1) {
127 if (errno == ESRCH) {
128 early_exit:
129 fprintf (stderr,
130 "%s FAILED: nbdkit exited before starting to serve files\n",
131 program_name);
132 pid = 0;
133 return -1;
135 perror ("kill");
138 if (access (pidpath, F_OK) == 0)
139 break;
141 sleep (1);
144 return 0;