-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
60 lines (51 loc) · 2.28 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
SHELL := /bin/bash
IMAGE := mistifyio/mistify-os:zfs-stable-api
# Recursive wildcard function
rwildcard=$(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2) $(filter $(subst *,%,$2),$d))
# Generate the test binary name from the directory. Used in combination with
# .SECONDEXPANSION so that the pattern match from the target can be used more
# than once to generate the prerequisite.
testBinFromDir=$(addprefix $(addsuffix /,$(1)), $(addsuffix .test, $(notdir $(1))))
# Determine the list of go packages to be tested based on which have test files.
# Test targets are of the form `pkgdir/pkgname.test`
test_files := $(call rwildcard,,*_test.go)
pkgdirs := $(sort $(dir $(test_files)))
pkgs := $(notdir $(patsubst %/,%,$(pkgdirs)))
testBins := $(join $(pkgdirs), $(addsuffix .test,$(pkgs)))
testOutputs := $(addsuffix test.out,$(pkgdirs))
# Suppress Make output. The relevant test output will be collected and sent to
# stdout
.SILENT:
# Test entry point to run all go package tests.
.PHONY: test
test: $(testOutputs)
# Run a package's test suite in a container and collect the output.
# Use a custom tmp directory so zpool commands work, which require paths
# relative to the host. Mount /tmp for that custom tmp directory to work.
# Mount the cgroup fs for systemd to work. Use SYS_ADMIN cap for zpool mounting.
# Add /dev/zfs device for zfs to work.
.SECONDEXPANSION:
$(testOutputs): %/test.out: $$(call testBinFromDir,%)
flock /dev/stdout -c 'echo "RUN $<"'
tmpdir=/tmp/$(notdir $<); \
rm -rf $$tmpdir && \
mkdir $$tmpdir && \
cid=$(shell docker run -dti -v "$(CURDIR):/mistify:ro" -v /tmp:/tmp -v /sys/fs/cgroup:/sys/fs/cgroup:ro --cap-add=SYS_ADMIN --device /dev/zfs:/dev/zfs --name $(notdir $<) -e "TMPDIR=$(tmpdir)" $(IMAGE)) && \
test -n $(cid) && \
sleep .25 && \
docker exec $$cid sh -c "cd /mistify; cd $(@D); ./$(notdir $<) -test.v" &> $@; \
ret=$$?; \
docker kill $$cid &>/dev/null && \
docker rm -v $$cid &>/dev/null && \
flock /dev/stdout -c 'echo "+++ $< +++"; cat $@'; \
rm -rf $$tmpdir; \
exit $$ret
# Build a package's test binaries. Done outside the container so it can be used
# with the base MistifyOS instead of the SDK. Always for a rebuild.
.SECONDARY: $(testBins)
$(testBins): %.test: FORCE
echo BUILD $@
cd $(dir $@) && flock -s /dev/stdout go test -c
FORCE:
clean:
go clean ./...