Add generic support for running tests as root.

By setting `ASROOT=1` in a unit test makefile a test will now run as
root, using `sudo -n`.

This patch allows us to simplify the pidfilefn() test.

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson
2015-08-23 01:30:29 +02:00
parent e8ff38ad6a
commit 44e787c966
3 changed files with 22 additions and 7 deletions
+10 -3
View File
@@ -120,7 +120,11 @@ int pidfile_signal(const char *pidfile, int signal)
/********************************* UNIT TESTS ************************************/
#ifdef UNITTEST
#define PIDFILE "/tmp/pidfilefn.test.pid"
#include <paths.h>
#include "lite.h"
extern char *__progname;
static char PIDFILE[42];
static void sigterm_handler(int signo)
{
@@ -131,10 +135,15 @@ static void sigalrm_handler(int signo)
{
printf("Testing pidfile() ...\n");
pidfile(NULL);
if (!fexist(PIDFILE))
err(1, "pidfile() failed creating %s", PIDFILE);
}
int main(void)
{
snprintf(PIDFILE, sizeof(PIDFILE), "%s%s.pid", _PATH_VARRUN, __progname);
signal(SIGTERM, sigterm_handler);
signal(SIGALRM, sigalrm_handler);
alarm(1);
@@ -145,8 +154,6 @@ int main(void)
else
printf("We got signal!\n");
system("ls -ld " PIDFILE);
printf("Reading pid file, should be %d ...\n", getpid());
printf("=> %d\n", pidfile_read(PIDFILE));
+3 -3
View File
@@ -1,5 +1,5 @@
OBJS = pidfile.o chomp.o
TARGET = $(OBJS) pidfilefn.test clean
CPPFLAGS = -D_PATHS_H_ -D_PATH_VARRUN="\"/tmp/\""
OBJS = pidfile.o chomp.o fexist.o
ASROOT = 1
TARGET = clean $(OBJS) pidfilefn.test
include rules.mk
+9 -1
View File
@@ -25,6 +25,13 @@ REDIRECT = >/dev/null
MAKEFLAGS = --no-print-directory --silent
endif
# Some tests may need to be run as root, e.g. pidfile()
ifeq ($(ASROOT), 1)
SUDO = sudo -n
else
SUDO =
endif
# Default install paths
prefix ?= /usr/local
libdir ?= $(prefix)/lib
@@ -40,10 +47,11 @@ incdir ?= $(prefix)/include
$(PRINT) " LINK $(subst $(ROOTDIR)/,,$(shell pwd)/)$@\n"
$(Q)$(CC) $(CFLAGS) $(LDFLAGS) -Wl,-Map,$@.map -o $@ $^ $(LDLIBS$(LDLIBS-$(@)))
.PHONY: %.test
%.test: %.c
$(PRINT) " TEST $(subst $(ROOTDIR)/,,$(shell pwd)/)$(@:.test=)\n"
$(Q)$(CC) $(CFLAGS) $(CPPFLAGS) -g -o $@ -DUNITTEST $< $(OBJS)
$(Q)@./$@ $(REDIRECT)
$(Q)$(SUDO) ./$@ $(REDIRECT)
# Default build rules for both main and unit test makefiles