Add support for ldap, mysql and postgres tests through the use of an external VM.
[freeradius.git] / src / tests / modules / test.mk
1 #
2 #  Add the module tests to the overall dependencies
3 #
4 tests.modules: tests.unit tests.keywords tests.auth
5
6 # If module requires test server, make sure TEST_SERVER of <MODULE>_TEST_SERVER variables are defined
7 # If TEST_SERVER is defined, define <MODULE>_TEST_SERVER for all modules that have CHECK_MODULE_TEST_CAN_BE_RUN
8 define CHECK_MODULE_TEST_CAN_BE_RUN
9   ifndef ${1}_require_test_server
10     tests.modules: ${1}.test
11   else
12     ifdef TEST_SERVER
13       tests.modules: ${1}.test
14       export $(shell echo ${1} | tr a-z A-Z)_TEST_SERVER := $(TEST_SERVER)
15     endif
16     ifdef $(shell echo ${1} | tr a-z A-Z)_TEST_SERVER
17       tests.modules: ${1}.test
18     endif
19   endif
20 endef
21 $(foreach x,$(TEST_BUILT) $(TEST_SUBBUILT),$(eval $(call CHECK_MODULE_TEST_CAN_BE_RUN,$x)))
22
23 ######################################################################
24 #
25 #  And now more makefile magic to automatically run the tests
26 #  for each module.
27 #
28
29 define DEFAULT_ATTRS
30 ifeq "$(wildcard ${1}.attrs)"
31 ${1}.attrs
32 else
33 src/tests/modules/default-input.attrs
34 endif
35 endef
36
37 #
38 #  Files in the output dir depend on the unit tests
39 #
40 #       src/tests/$(MODULE_DIR)/FOO.unlang      unlang for the test
41 #       src/tests/$(MODULE_DIR)/FOO.attrs       input RADIUS and output filter
42 #       build/tests/$(MODULE_DIR)/FOO.out       updated if the test succeeds
43 #       build/tests/$(MODULE_DIR)/FOO.log       debug output for the test
44 #
45 #  If the test fails, then look for ERROR in the input.  No error
46 #  means it's unexpected, so we die.
47 #
48 #  Otherwise, check the log file for a parse error which matches the
49 #  ERROR line in the input.
50 #
51 $(BUILD_DIR)/tests/modules/%: src/tests/modules/%.unlang $(BUILD_DIR)/tests/modules/%.attrs $(TESTBINDIR)/unittest | build.raddb
52         @mkdir -p $(dir $@)
53         @echo MODULE-TEST $(lastword $(subst /, ,$(dir $@))) $(basename $(notdir $@))
54         @if ! MODULE_TEST_DIR=$(dir $<) MODULE_TEST_UNLANG=$< $(TESTBIN)/unittest -D share -d src/tests/modules/ -i $@.attrs -f $@.attrs -xxx > $@.log 2>&1; then \
55                 if ! grep ERROR $< 2>&1 > /dev/null; then \
56                         cat $@.log; \
57                         echo "# $@.log"; \
58                         echo MODULE_TEST_DIR=$(dir $<) MODULE_TEST_UNLANG=$< $(TESTBIN)/unittest -D share -d src/tests/modules/ -i $@.attrs -f $@.attrs -xx; \
59                         exit 1; \
60                 fi; \
61                 FOUND=$$(grep ^$< $@.log | head -1 | sed 's/:.*//;s/.*\[//;s/\].*//'); \
62                 EXPECTED=$$(grep -n ERROR $< | sed 's/:.*//'); \
63                 if [ "$$EXPECTED" != "$$FOUND" ]; then \
64                         cat $@.log; \
65                         echo "# $@.log"; \
66                         echo MODULE_TEST_DIR=$(dir $<) MODULE_TEST_UNLANG=$< $(TESTBIN)/unittest -D share -d src/tests/modules/ -i $@.attrs -f $@.attrs -xx; \
67                         exit 1; \
68                 fi \
69         fi
70         @touch $@
71
72 #
73 #  Sometimes we have a default input.  So use that.  Otherwise, use
74 #  the input specific to the test.
75 #
76 MODULE_UNLANG           := $(wildcard src/tests/modules/*/*.unlang src/tests/modules/*/*/*.unlang)
77 MODULE_ATTRS_REQUIRES   := $(patsubst %.unlang,%.attrs,$(MODULE_UNLANG))
78 MODULE_ATTRS_EXISTS     := $(wildcard src/tests/modules/*/*.attrs src/tests/modules/*/*/*.attrs)
79 MODULE_ATTRS_NEEDS      := $(filter-out $(MODULE_ATTRS_EXISTS),$(MODULE_ATTRS_REQUIRES))
80
81 MODULE_CONF_REQUIRES    := $(patsubst %.unlang,%.conf,$(MODULE_UNLANG))
82 MODULE_CONF_EXISTS      := $(wildcard src/tests/modules/*/*.conf src/tests/modules/*/*/*.attrs)
83 MODULE_CONF_NEEDS       := $(filter-out $(MODULE_CONF_EXISTS),$(MODULE_CONF_REQUIRES))
84
85 #
86 #  The complete list of tests which are to be run
87 #
88 MODULE_TESTS            := $(patsubst src/tests/modules/%/all.mk,%,$(wildcard src/tests/modules/*/all.mk))
89
90
91 #
92 #  Target-specific rules
93 #
94 define MODULE_COPY_FILE
95 $(BUILD_DIR)/${1}: src/${1}
96         @mkdir -p $$(@D)
97         @cp $$< $$@
98
99 endef
100
101 #
102 #  Default rules
103 #
104 define MODULE_COPY_ATTR
105 $(BUILD_DIR)/${1}: src/tests/modules/default-input.attrs
106         mkdir -p $$(@D)
107         @cp $$< $$@
108 endef
109
110 #
111 #  FIXME: get this working
112 #
113 define MODULE_COPY_CONF
114 $(BUILD_DIR)/${1}: src/tests/modules/${2}/module.conf
115         @mkdir -p $$(@D)
116         @cp $$< $$@
117 endef
118
119 define MODULE_FILE_TARGET
120 $(BUILD_DIR)/${1}: src/${1}.unlang $(BUILD_DIR)/${1}.attrs
121
122 endef
123
124 define MODULE_TEST_TARGET
125 ${1}.test: $(patsubst %.unlang,%,$(subst src,$(BUILD_DIR),$(filter src/tests/modules/${1}/%,$(MODULE_UNLANG))))
126
127 endef
128
129 #
130 #  Create the rules from the list of input files
131 #
132 $(foreach x,$(MODULE_ATTRS_EXISTS),$(eval $(call MODULE_COPY_FILE,$(subst src/,,$x))))
133 $(foreach x,$(MODULE_CONF_EXISTS),$(eval $(call MODULE_COPY_FILE,$(subst src/,,$x))))
134
135 $(foreach x,$(MODULE_ATTRS_NEEDS),$(eval $(call MODULE_COPY_ATTR,$(subst src/,,$x))))
136 # FIXME: copy src/tests/modules/*/module.conf to the right place, too
137
138 $(foreach x,$(MODULE_UNLANG),$(eval $(call MODULE_FILE_TARGET,$(patsubst %.unlang,%,$(subst src/,,$x)))))
139 $(foreach x,$(MODULE_TESTS),$(eval $(call MODULE_TEST_TARGET,$x)))
140
141 .PHONY: clean.modules.test
142 clean.modules.test:
143         @rm -rf $(BUILD_DIR)/tests/modules/
144
145 #
146 #  For each file, look for precursor test.
147 #  Ensure that each test depends on its precursors.
148 #
149 -include $(BUILD_DIR)/tests/modules/depends.mk
150
151 $(BUILD_DIR)/tests/modules/depends.mk: $(MODULE_UNLANG) | $(BUILD_DIR)/tests/modules
152         @rm -f $@
153         @for x in $^; do \
154                 y=`grep PRE $$x | awk '{ print $$3 }'`; \
155                 if [ "$$y" != "" ]; then \
156                         z=`echo $$x | sed 's,src/,$(BUILD_DIR)/', | sed 's/.unlang//'`; \
157                         d=$$(basename $$(dirname $$x)); \
158                         echo "$$z: $(BUILD_DIR)/tests/modules/$$d/$$y" >> $@; \
159                         echo "" >> $@; \
160                 fi \
161         done