1# Copyright 2015 The Prometheus Authors
2# Licensed under the Apache License, Version 2.0 (the "License");
3# you may not use this file except in compliance with the License.
4# You may obtain a copy of the License at
5#
6# http://www.apache.org/licenses/LICENSE-2.0
7#
8# Unless required by applicable law or agreed to in writing, software
9# distributed under the License is distributed on an "AS IS" BASIS,
10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11# See the License for the specific language governing permissions and
12# limitations under the License.
13
14# Ensure that 'all' is the default target otherwise it will be the first target from Makefile.common.
15all::
16
17# Needs to be defined before including Makefile.common to auto-generate targets
18DOCKER_ARCHS ?= amd64 armv7 arm64 ppc64le s390x
19
20include Makefile.common
21
22PROMTOOL_VERSION ?= 2.18.1
23PROMTOOL_URL     ?= https://github.com/prometheus/prometheus/releases/download/v$(PROMTOOL_VERSION)/prometheus-$(PROMTOOL_VERSION).$(GO_BUILD_PLATFORM).tar.gz
24PROMTOOL         ?= $(FIRST_GOPATH)/bin/promtool
25
26DOCKER_IMAGE_NAME       ?= node-exporter
27MACH                    ?= $(shell uname -m)
28
29STATICCHECK_IGNORE =
30
31ifeq ($(GOHOSTOS), linux)
32	test-e2e := test-e2e
33else
34	test-e2e := skip-test-e2e
35endif
36
37# Use CGO for non-Linux builds.
38ifeq ($(GOOS), linux)
39	PROMU_CONF ?= .promu.yml
40else
41	ifndef GOOS
42		ifeq ($(GOHOSTOS), linux)
43			PROMU_CONF ?= .promu.yml
44		else
45			PROMU_CONF ?= .promu-cgo.yml
46		endif
47	else
48		# Do not use CGO for openbsd/amd64 builds
49		ifeq ($(GOOS), openbsd)
50			ifeq ($(GOARCH), amd64)
51				PROMU_CONF ?= .promu.yml
52			else
53				PROMU_CONF ?= .promu-cgo.yml
54			endif
55		else
56			PROMU_CONF ?= .promu-cgo.yml
57		endif
58	endif
59endif
60
61PROMU := $(FIRST_GOPATH)/bin/promu --config $(PROMU_CONF)
62
63e2e-out = collector/fixtures/e2e-output.txt
64ifeq ($(MACH), ppc64le)
65	e2e-out = collector/fixtures/e2e-64k-page-output.txt
66endif
67ifeq ($(MACH), aarch64)
68	e2e-out = collector/fixtures/e2e-64k-page-output.txt
69endif
70
71# 64bit -> 32bit mapping for cross-checking. At least for amd64/386, the 64bit CPU can execute 32bit code but not the other way around, so we don't support cross-testing upwards.
72cross-test = skip-test-32bit
73define goarch_pair
74	ifeq ($$(GOHOSTOS),linux)
75		ifeq ($$(GOHOSTARCH),$1)
76			GOARCH_CROSS = $2
77			cross-test = test-32bit
78		endif
79	endif
80endef
81
82# By default, "cross" test with ourselves to cover unknown pairings.
83$(eval $(call goarch_pair,amd64,386))
84$(eval $(call goarch_pair,mips64,mips))
85$(eval $(call goarch_pair,mips64el,mipsel))
86
87all:: vet checkmetrics checkrules common-all $(cross-test) $(test-e2e)
88
89.PHONY: test
90test: collector/fixtures/sys/.unpacked
91	@echo ">> running tests"
92	$(GO) test -short $(test-flags) $(pkgs)
93
94.PHONY: test-32bit
95test-32bit: collector/fixtures/sys/.unpacked
96	@echo ">> running tests in 32-bit mode"
97	@env GOARCH=$(GOARCH_CROSS) $(GO) test $(pkgs)
98
99.PHONY: skip-test-32bit
100skip-test-32bit:
101	@echo ">> SKIP running tests in 32-bit mode: not supported on $(GOHOSTOS)/$(GOHOSTARCH)"
102
103%/.unpacked: %.ttar
104	@echo ">> extracting fixtures"
105	if [ -d $(dir $@) ] ; then rm -rf $(dir $@) ; fi
106	./ttar -C $(dir $*) -x -f $*.ttar
107	touch $@
108
109update_fixtures:
110	rm -vf collector/fixtures/sys/.unpacked
111	./ttar -C collector/fixtures -c -f collector/fixtures/sys.ttar sys
112
113.PHONY: test-e2e
114test-e2e: build collector/fixtures/sys/.unpacked
115	@echo ">> running end-to-end tests"
116	./end-to-end-test.sh
117
118.PHONY: skip-test-e2e
119skip-test-e2e:
120	@echo ">> SKIP running end-to-end tests on $(GOHOSTOS)"
121
122.PHONY: checkmetrics
123checkmetrics: $(PROMTOOL)
124	@echo ">> checking metrics for correctness"
125	./checkmetrics.sh $(PROMTOOL) $(e2e-out)
126
127.PHONY: checkrules
128checkrules: $(PROMTOOL)
129	@echo ">> checking rules for correctness"
130	find . -name "*rules*.yml" | xargs -I {} $(PROMTOOL) check rules {}
131
132.PHONY: test-docker
133test-docker:
134	@echo ">> testing docker image"
135	./test_image.sh "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-amd64:$(DOCKER_IMAGE_TAG)" 9100
136
137.PHONY: promtool
138promtool: $(PROMTOOL)
139
140$(PROMTOOL):
141	mkdir -p $(FIRST_GOPATH)/bin
142	curl -fsS -L $(PROMTOOL_URL) | tar -xvzf - -C $(FIRST_GOPATH)/bin --no-anchored --strip 1 promtool
143