mirror of
https://github.com/troglobit/libite.git
synced 2026-11-19 19:40:45 +00:00
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:
1 parent
e8ff38ad6a
commit
44e787c966
3 files changed
+22
-7
No files matched your search
+10
-3
@@ -120,7 +120,11 @@ int pidfile_signal(const char *pidfile, int signal)
|
|||||||
|
|
||||||
/********************************* UNIT TESTS ************************************/
|
/********************************* UNIT TESTS ************************************/
|
||||||
#ifdef UNITTEST
|
#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)
|
static void sigterm_handler(int signo)
|
||||||
{
|
{
|
||||||
@@ -131,10 +135,15 @@ static void sigalrm_handler(int signo)
|
|||||||
{
|
{
|
||||||
printf("Testing pidfile() ...\n");
|
printf("Testing pidfile() ...\n");
|
||||||
pidfile(NULL);
|
pidfile(NULL);
|
||||||
|
|
||||||
|
if (!fexist(PIDFILE))
|
||||||
|
err(1, "pidfile() failed creating %s", PIDFILE);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
|
snprintf(PIDFILE, sizeof(PIDFILE), "%s%s.pid", _PATH_VARRUN, __progname);
|
||||||
|
|
||||||
signal(SIGTERM, sigterm_handler);
|
signal(SIGTERM, sigterm_handler);
|
||||||
signal(SIGALRM, sigalrm_handler);
|
signal(SIGALRM, sigalrm_handler);
|
||||||
alarm(1);
|
alarm(1);
|
||||||
@@ -145,8 +154,6 @@ int main(void)
|
|||||||
else
|
else
|
||||||
printf("We got signal!\n");
|
printf("We got signal!\n");
|
||||||
|
|
||||||
system("ls -ld " PIDFILE);
|
|
||||||
|
|
||||||
printf("Reading pid file, should be %d ...\n", getpid());
|
printf("Reading pid file, should be %d ...\n", getpid());
|
||||||
printf("=> %d\n", pidfile_read(PIDFILE));
|
printf("=> %d\n", pidfile_read(PIDFILE));
|
||||||
|
|
||||||
|
|||||||
+3
-3
@@ -1,5 +1,5 @@
|
|||||||
OBJS = pidfile.o chomp.o
|
OBJS = pidfile.o chomp.o fexist.o
|
||||||
TARGET = $(OBJS) pidfilefn.test clean
|
ASROOT = 1
|
||||||
CPPFLAGS = -D_PATHS_H_ -D_PATH_VARRUN="\"/tmp/\""
|
TARGET = clean $(OBJS) pidfilefn.test
|
||||||
|
|
||||||
include rules.mk
|
include rules.mk
|
||||||
@@ -25,6 +25,13 @@ REDIRECT = >/dev/null
|
|||||||
MAKEFLAGS = --no-print-directory --silent
|
MAKEFLAGS = --no-print-directory --silent
|
||||||
endif
|
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
|
# Default install paths
|
||||||
prefix ?= /usr/local
|
prefix ?= /usr/local
|
||||||
libdir ?= $(prefix)/lib
|
libdir ?= $(prefix)/lib
|
||||||
@@ -40,10 +47,11 @@ incdir ?= $(prefix)/include
|
|||||||
$(PRINT) " LINK $(subst $(ROOTDIR)/,,$(shell pwd)/)$@\n"
|
$(PRINT) " LINK $(subst $(ROOTDIR)/,,$(shell pwd)/)$@\n"
|
||||||
$(Q)$(CC) $(CFLAGS) $(LDFLAGS) -Wl,-Map,$@.map -o $@ $^ $(LDLIBS$(LDLIBS-$(@)))
|
$(Q)$(CC) $(CFLAGS) $(LDFLAGS) -Wl,-Map,$@.map -o $@ $^ $(LDLIBS$(LDLIBS-$(@)))
|
||||||
|
|
||||||
|
.PHONY: %.test
|
||||||
%.test: %.c
|
%.test: %.c
|
||||||
$(PRINT) " TEST $(subst $(ROOTDIR)/,,$(shell pwd)/)$(@:.test=)\n"
|
$(PRINT) " TEST $(subst $(ROOTDIR)/,,$(shell pwd)/)$(@:.test=)\n"
|
||||||
$(Q)$(CC) $(CFLAGS) $(CPPFLAGS) -g -o $@ -DUNITTEST $< $(OBJS)
|
$(Q)$(CC) $(CFLAGS) $(CPPFLAGS) -g -o $@ -DUNITTEST $< $(OBJS)
|
||||||
$(Q)@./$@ $(REDIRECT)
|
$(Q)$(SUDO) ./$@ $(REDIRECT)
|
||||||
|
|
||||||
|
|
||||||
# Default build rules for both main and unit test makefiles
|
# Default build rules for both main and unit test makefiles
|
||||||
|
|||||||
Reference in new issue
Block a user