From 44e787c96674194e1dcbfa17e74c32cce48c1a56 Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Sun, 23 Aug 2015 01:30:23 +0200 Subject: [PATCH] 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 --- pidfilefn.c | 13 ++++++++++--- pidfilefn.mk | 6 +++--- rules.mk | 10 +++++++++- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/pidfilefn.c b/pidfilefn.c index 1906895..5ba8af5 100644 --- a/pidfilefn.c +++ b/pidfilefn.c @@ -120,7 +120,11 @@ int pidfile_signal(const char *pidfile, int signal) /********************************* UNIT TESTS ************************************/ #ifdef UNITTEST -#define PIDFILE "/tmp/pidfilefn.test.pid" +#include +#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)); diff --git a/pidfilefn.mk b/pidfilefn.mk index 80ece78..3b5a5c1 100644 --- a/pidfilefn.mk +++ b/pidfilefn.mk @@ -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 diff --git a/rules.mk b/rules.mk index 7a2fc8c..358e14a 100644 --- a/rules.mk +++ b/rules.mk @@ -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