Add "trivial-httpd" builtin, use it in tests

A simple HTTP server implementation is so few lines of code when one
is linking to libsoup anyways, so let's just have one here in ostree
that will be used for the test suite.

This allows us to run the archive tests that previously required
apache even in gnome-ostree.
This commit is contained in:
Colin Walters committed 2013-07-05 16:28:40 -04:00
1 parent 2ed49a3749
commit ec21dc4242
8 files changed
+355 -339

No files matched your search

-35
View File
@@ -1,35 +0,0 @@
# Toplevel tests Makefile
#
# Copyright (C) 2011 Colin Walters <walters@verbum.org>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
TESTS = $(wildcard t[0-9][0-9][0-9][0-9]-*.sh)
all: tmpdir-lifecycle run-apache
tmpdir-lifecycle: tmpdir-lifecycle.c Makefile
gcc $(CFLAGS) `pkg-config --cflags --libs gio-unix-2.0` -o $@ $<
run-apache: run-apache.c Makefile
gcc $(CFLAGS) `pkg-config --cflags --libs gio-unix-2.0` -o $@ $<
check:
@for test in $(TESTS); do \
echo $$test; \
./$$test; \
done
+4 -33
View File
@@ -102,9 +102,6 @@ setup_test_repository () {
}
setup_fake_remote_repo1() {
if ! test -x ${SRCDIR}/run-apache; then
exit 77
fi
mode=$1
shift
oldpwd=`pwd`
@@ -131,36 +128,10 @@ setup_fake_remote_repo1() {
cd ${test_tmpdir}
mkdir ${test_tmpdir}/httpd
cd httpd
cat >httpd.conf <<EOF
ServerRoot ${test_tmpdir}/httpd
PidFile pid
LogLevel crit
ErrorLog log
LockFile lock
ServerName localhost
LoadModule alias_module modules/mod_alias.so
LoadModule cgi_module modules/mod_cgi.so
LoadModule env_module modules/mod_env.so
StartServers 1
# SetEnv OSTREE_REPO_PREFIX ${test_tmpdir}/ostree-srv
Alias /ostree/ ${test_tmpdir}/ostree-srv/
# ScriptAlias /ostree/ ${test_tmpdir}/httpd/ostree-http-backend/
EOF
${SRCDIR}/tmpdir-lifecycle ${SRCDIR}/run-apache `pwd`/httpd.conf ${test_tmpdir}/httpd-address &
for i in $(seq 5); do
if ! test -f ${test_tmpdir}/httpd-address; then
sleep 1
else
break
fi
done
if ! test -f ${test_tmpdir}/httpd-address; then
echo "Error: timed out waiting for httpd-address file"
exit 1
fi
ln -s ${test_tmpdir}/ostree-srv ostree
ostree trivial-httpd --daemonize -p ${test_tmpdir}/httpd-port
port=$(cat ${test_tmpdir}/httpd-port)
echo "http://127.0.0.1:${port}" > ${test_tmpdir}/httpd-address
cd ${oldpwd}
export OSTREE="ostree --repo=repo"
-168
View File
@@ -1,168 +0,0 @@
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
*
* Copyright (C) 2011 Colin Walters <walters@verbum.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Author: Colin Walters <walters@verbum.org>
*/
#include <gio/gio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/stat.h>
/* Taken from gnome-user-share src/httpd.c under the GPLv2 */
static int
get_port (void)
{
int sock;
int saved_errno;
struct sockaddr_in addr;
int reuse;
socklen_t len;
sock = socket (PF_INET, SOCK_STREAM, 0);
if (sock < 0)
{
return -1;
}
memset (&addr, 0, sizeof (addr));
addr.sin_family = AF_INET;
addr.sin_port = 0;
addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
reuse = 1;
setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof (reuse));
if (bind (sock, (struct sockaddr *)&addr, sizeof (addr)) == -1)
{
saved_errno = errno;
close (sock);
errno = saved_errno;
return -1;
}
len = sizeof (addr);
if (getsockname (sock, (struct sockaddr *)&addr, &len) == -1)
{
saved_errno = errno;
close (sock);
errno = saved_errno;
return -1;
}
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__APPLE__)
/* XXX This exposes a potential race condition, but without this,
* httpd will not start on the above listed platforms due to the fact
* that SO_REUSEADDR is also needed when Apache binds to the listening
* socket. At this time, Apache does not support that socket option.
*/
close (sock);
#endif
return ntohs (addr.sin_port);
}
static const char *known_httpd_modules_locations [] = {
"/usr/libexec/apache2",
"/usr/lib/apache2/modules",
"/usr/lib64/httpd/modules",
"/usr/lib/httpd/modules",
NULL
};
static gchar*
get_httpd_modules_path ()
{
int i;
for (i = 0; known_httpd_modules_locations[i]; i++)
{
if (g_file_test (known_httpd_modules_locations[i], G_FILE_TEST_IS_EXECUTABLE)
&& g_file_test (known_httpd_modules_locations[i], G_FILE_TEST_IS_DIR))
{
return g_strdup (known_httpd_modules_locations[i]);
}
}
return NULL;
}
int
main (int argc,
char **argv)
{
int port;
char *listen;
char *address_string;
GError *error = NULL;
GPtrArray *httpd_argv;
char *modules;
if (argc != 3)
{
fprintf (stderr, "usage: run-apache CONF PORTFILE");
return 1;
}
g_type_init ();
port = get_port ();
if (port == -1)
{
perror ("Failed to bind port");
return 1;
}
httpd_argv = g_ptr_array_new ();
g_ptr_array_add (httpd_argv, "httpd");
g_ptr_array_add (httpd_argv, "-DFOREGROUND");
g_ptr_array_add (httpd_argv, "-f");
g_ptr_array_add (httpd_argv, argv[1]);
g_ptr_array_add (httpd_argv, "-C");
listen = g_strdup_printf ("Listen 127.0.0.1:%d", port);
g_ptr_array_add (httpd_argv, listen);
g_ptr_array_add (httpd_argv, NULL);
address_string = g_strdup_printf ("http://127.0.0.1:%d\n", port);
if (!g_file_set_contents (argv[2], address_string, -1, &error))
{
g_printerr ("%s\n", error->message);
return 1;
}
setenv ("LANG", "C", 1);
modules = get_httpd_modules_path ();
if (modules == NULL)
{
g_printerr ("Failed to find httpd modules\n");
return 1;
}
if (symlink (modules, "modules") < 0)
{
perror ("failed to make modules symlink");
return 1;
}
execvp ("httpd", (char**)httpd_argv->pdata);
perror ("Failed to run httpd");
return 1;
}
-103
View File
@@ -1,103 +0,0 @@
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
*
* Kill a child process when the current directory is deleted
*
* Copyright (C) 2011 Colin Walters <walters@verbum.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Author: Colin Walters <walters@verbum.org>
*/
#include <gio/gio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
struct TmpdirLifecyleData {
GMainLoop *loop;
GPid pid;
gboolean exited;
};
static void
on_dir_changed (GFileMonitor *mon,
GFile *file,
GFile *other,
GFileMonitorEvent event,
gpointer user_data)
{
struct TmpdirLifecyleData *data = user_data;
if (event == G_FILE_MONITOR_EVENT_DELETED)
g_main_loop_quit (data->loop);
}
static void
on_child_exited (GPid pid,
int status,
gpointer user_data)
{
struct TmpdirLifecyleData *data = user_data;
data->exited = TRUE;
g_main_loop_quit (data->loop);
}
int
main (int argc,
char **argv)
{
GFileMonitor *monitor;
GFile *curdir;
GError *error = NULL;
GPtrArray *new_argv;
int i;
struct TmpdirLifecyleData data;
g_type_init ();
memset (&data, 0, sizeof (data));
data.loop = g_main_loop_new (NULL, TRUE);
curdir = g_file_new_for_path (".");
monitor = g_file_monitor_directory (curdir, 0, NULL, &error);
if (!monitor)
exit (1);
g_signal_connect (monitor, "changed",
G_CALLBACK (on_dir_changed), &data);
new_argv = g_ptr_array_new ();
for (i = 1; i < argc; i++)
g_ptr_array_add (new_argv, argv[i]);
g_ptr_array_add (new_argv, NULL);
if (!g_spawn_async (NULL, (char**)new_argv->pdata, NULL, G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
NULL, NULL, &data.pid, &error))
{
g_printerr ("%s\n", error->message);
return 1;
}
g_child_watch_add (data.pid, on_child_exited, &data);
g_main_loop_run (data.loop);
if (!data.exited)
kill (data.pid, SIGTERM);
return 0;
}