-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
876 lines (800 loc) · 38.6 KB
/
Makefile
File metadata and controls
876 lines (800 loc) · 38.6 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
.PHONY: test test-fast test-llm build build-safe build-all clean fmt lint help pre-push test-env test-env-up test-env-down test-env-clean test-env-logs test-e2e test-e2e-importer test-all test-all-clean test-scenarios test-config-bootstrap docker-build docker-build-vmgather docker-build-vmimporter manual-env-up manual-env-down manual-env-clean manual-env-logs security-check security-check-go security-check-secrets security-check-dockerfile security-check-images
VERSION ?= $(shell git describe --tags --always --dirty)
PKG_TAG ?= $(shell git describe --tags --abbrev=0 2>/dev/null || echo "latest")
# Platforms supported by VictoriaMetrics (consistent with upstream)
# Platforms supported by VictoriaMetrics (limited by distroless support)
PLATFORMS ?= linux/amd64,linux/arm64,linux/arm
# Go version for build
GO_VERSION ?= 1.25.7
GOVULNCHECK_VERSION ?= v1.1.4
DOCKER_OUTPUT ?= type=docker
DOCKER_COMPOSE := $(shell docker compose version >/dev/null 2>&1 && echo "docker compose" || echo "docker-compose")
TEST_ENV_FILE := local-test-env/.env.dynamic
MANUAL_ENV_FILE := local-test-env/.env.manual
TEST_ENV_PROJECT_FILE_TEST := .compose-project.test
TEST_ENV_PROJECT_FILE_MANUAL := .compose-project.manual
# Docker registries and namespace (standard across VictoriaMetrics)
# GHCR is handled by CI; local `make release` targets public hubs only.
DOCKER_REGISTRIES ?= docker.io quay.io
DOCKER_NAMESPACE ?= victoriametrics
# Default target: show help
.DEFAULT_GOAL := help
# Alias for release publishing (consistent with VM standards)
release: publish-via-docker
# ... (snipped) ...
# Valid target for publish-via-docker
publish-via-docker: publish-vmgather publish-vmimporter
publish-vmgather:
@echo "Building and pushing vmgather:$(PKG_TAG)"
@docker buildx build \
--platform $(PLATFORMS) \
--build-arg GO_VERSION=$(GO_VERSION) \
--provenance=mode=max \
--sbom=true \
--label "org.opencontainers.image.source=https://github.com/VictoriaMetrics/vmgather" \
--label "org.opencontainers.image.vendor=VictoriaMetrics" \
--label "org.opencontainers.image.version=$(PKG_TAG)" \
--label "org.opencontainers.image.created=$(shell date -u +'%Y-%m-%dT%H:%M:%SZ')" \
-f build/docker/Dockerfile.vmgather \
$(foreach registry,$(DOCKER_REGISTRIES), \
--tag $(registry)/$(DOCKER_NAMESPACE)/vmgather:$(PKG_TAG) \
--tag $(registry)/$(DOCKER_NAMESPACE)/vmgather:latest \
) \
--push \
.
publish-vmimporter:
@echo "Building and pushing vmimporter:$(PKG_TAG)"
@docker buildx build \
--platform $(PLATFORMS) \
--build-arg GO_VERSION=$(GO_VERSION) \
--provenance=mode=max \
--sbom=true \
--label "org.opencontainers.image.source=https://github.com/VictoriaMetrics/vmgather" \
--label "org.opencontainers.image.vendor=VictoriaMetrics" \
--label "org.opencontainers.image.version=$(PKG_TAG)" \
--label "org.opencontainers.image.created=$(shell date -u +'%Y-%m-%dT%H:%M:%SZ')" \
-f build/docker/Dockerfile.vmimporter \
$(foreach registry,$(DOCKER_REGISTRIES), \
--tag $(registry)/$(DOCKER_NAMESPACE)/vmimporter:$(PKG_TAG) \
--tag $(registry)/$(DOCKER_NAMESPACE)/vmimporter:latest \
) \
--push \
.
# =============================================================================
# HELP - Display available targets
# =============================================================================
help:
@echo "================================================================================"
@echo "vmgather - Makefile Commands"
@echo "================================================================================"
@echo ""
@echo "BUILD COMMANDS:"
@echo " make build - Build binary (with automatic tests)"
@echo " make build-safe - Build with race detector tests + linting (no Docker)"
@echo " make build-all - Build for all platforms (8 targets)"
@echo " make publish-via-docker - Build & Push multi-arch images to registries"
@echo " make release - Alias for publish-via-docker"
@echo " make clean - Clean build artifacts"
@echo ""
@echo "TEST COMMANDS:"
@echo " make test - Run all tests (fast mode, no race detector)"
@echo " make test-fast - Run tests with -short flag (skip slow tests)"
@echo " make test-unit-full - Run unit tests without -short"
@echo " make test-full - Run complete test suite with race detector"
@echo " make test-llm - Run tests with LLM-friendly structured output"
@echo " make test-coverage - Generate HTML coverage report"
@echo ""
@echo "SPECIFIC TEST TARGETS:"
@echo " make test-vm - Test VM client only"
@echo " make test-obfuscation - Test obfuscation only"
@echo " make test-service - Test services only"
@echo " make test-archive - Test archive writer only"
@echo " make test-builder - Test build system"
@echo ""
@echo "DEVELOPMENT:"
@echo " make fmt - Format code"
@echo " make lint - Run linter"
@echo ""
@echo "TESTS WITHOUT DOCKER:"
@echo " make test - Unit tests only (fast, no Docker required)"
@echo " make test-fast - Even faster (skips slow tests)"
@echo " make test-coverage - Generate coverage report"
@echo ""
@echo "TESTS WITH DOCKER:"
@echo " make test-integration - Binary tests Docker environment (needs Docker)"
@echo " make test-env-up - Start test environment"
@echo " make test-env-down - Stop test environment"
@echo " make test-env-clean - Stop test environment and remove volumes"
@echo " make test-e2e - Run VMGather Playwright suite (requires test-env-up)"
@echo " make test-e2e-importer - Run VMImporter Playwright regression suite"
@echo " make test-all - Everything: test-full + both Playwright suites"
@echo " make test-all-clean - Everything + cleanup (recommended for CI / OrbStack)"
@echo " make security-check - Run CI-equivalent security checks locally"
@echo " make pre-push - Full local gate: test-all-clean + security-check"
@echo ""
@echo "FULL TEST SUITE:"
@echo " make test-full - Everything: unit + Docker scenarios"
@echo ""
@echo "TEST CONFIGURATION:"
@echo " make test-config-bootstrap - Generate dynamic env file"
@echo " make test-config-validate - Validate test configuration"
@echo " make test-config-env - Show config as environment variables"
@echo " make test-config-json - Show config as JSON"
@echo ""
@echo "================================================================================"
# =============================================================================
# TEST TARGETS - Automatic test execution with LLM-friendly output
# =============================================================================
# Fast tests for development (default for build)
test:
@echo "================================================================================"
@echo "TEST SUITE: Fast Mode (no race detector, skip slow tests)"
@echo "================================================================================"
@echo ""
@set -e; \
tmpfile="$$(mktemp)"; \
status=0; \
go test -short -coverprofile=coverage.out ./... >"$$tmpfile" 2>&1 || status=$$?; \
cat "$$tmpfile" | $(MAKE) --no-print-directory format-test-output; \
rm -f "$$tmpfile"; \
exit $$status
@echo ""
@$(MAKE) --no-print-directory test-summary
# Fast tests without coverage
test-fast:
@echo "================================================================================"
@echo "TEST SUITE: Ultra-Fast Mode (no coverage, skip slow tests)"
@echo "================================================================================"
@echo ""
@set -e; \
tmpfile="$$(mktemp)"; \
status=0; \
go test -short ./... >"$$tmpfile" 2>&1 || status=$$?; \
cat "$$tmpfile" | $(MAKE) --no-print-directory format-test-output; \
rm -f "$$tmpfile"; \
exit $$status
@echo ""
@$(MAKE) --no-print-directory test-summary
# Full unit test suite (no -short)
test-unit-full:
@echo "================================================================================"
@echo "TEST SUITE: Unit Full Mode (no -short)"
@echo "================================================================================"
@echo ""
@set -e; \
tmpfile="$$(mktemp)"; \
status=0; \
go test -coverprofile=coverage.out ./... >"$$tmpfile" 2>&1 || status=$$?; \
cat "$$tmpfile" | $(MAKE) --no-print-directory format-test-output; \
rm -f "$$tmpfile"; \
exit $$status
@echo ""
@$(MAKE) --no-print-directory test-summary
# Full test suite with race detector (unit tests only)
test-race:
@echo "================================================================================"
@echo "TEST SUITE: Race detector mode"
@echo "================================================================================"
@echo ""
@set -e; \
tmpfile="$$(mktemp)"; \
status=0; \
go test -v -race -coverprofile=coverage.out ./... >"$$tmpfile" 2>&1 || status=$$?; \
cat "$$tmpfile" | $(MAKE) --no-print-directory format-test-output; \
rm -f "$$tmpfile"; \
exit $$status
@echo ""
@$(MAKE) --no-print-directory test-summary
# LLM-friendly structured output (best for CI and LLM agents)
test-llm:
@echo "+===============================================================================+"
@echo "| LLM-FRIENDLY TEST REPORT |"
@echo "+===============================================================================+"
@echo ""
@echo "+-------------------------------------------------------------------------------+"
@echo "| TEST EXECUTION START |"
@echo "| Timestamp: $$(date '+%Y-%m-%d %H:%M:%S') |"
@echo "+-------------------------------------------------------------------------------+"
@echo ""
@go test -json -short ./... 2>&1 | $(MAKE) --no-print-directory parse-json-output || true
@echo ""
@echo "+-------------------------------------------------------------------------------+"
@echo "| TEST EXECUTION END |"
@echo "+-------------------------------------------------------------------------------+"
@$(MAKE) --no-print-directory test-summary-detailed
# Show test coverage in browser
test-coverage: test
@echo "Opening coverage report in browser..."
@go tool cover -html=coverage.out
# Internal target: Format test output for readability
format-test-output:
@awk '{ \
if ($$0 ~ /^PASS/) { print "[PASS] " $$0; } \
else if ($$0 ~ /^FAIL/) { print "[FAIL] " $$0; } \
else if ($$0 ~ /^ok/) { print "[OK] " $$0; } \
else if ($$0 ~ /--- PASS:/) { print " [OK] " $$0; } \
else if ($$0 ~ /--- FAIL:/) { print " [FAIL] " $$0; } \
else if ($$0 ~ /=== RUN/) { print " [RUN] " $$0; } \
else { print $$0; } \
}'
# Internal target: Parse JSON test output (for -json flag)
parse-json-output:
@python3 -c 'import sys, json; \
test_counts = {"pass": 0, "fail": 0, "skip": 0}; \
failed_tests = []; \
for line in sys.stdin: \
try: \
obj = json.loads(line.strip()); \
if obj.get("Action") == "pass" and "Test" in obj: \
test_counts["pass"] += 1; \
print(f" [PASS] {obj.get(\"Package\", \"\")} :: {obj.get(\"Test\", \"\")} ({obj.get(\"Elapsed\", 0):.3f}s)"); \
elif obj.get("Action") == "fail" and "Test" in obj: \
test_counts["fail"] += 1; \
failed_tests.append(f"{obj.get(\"Package\", \"\")} :: {obj.get(\"Test\", \"\")}"); \
print(f" [FAIL] {obj.get(\"Package\", \"\")} :: {obj.get(\"Test\", \"\")} ({obj.get(\"Elapsed\", 0):.3f}s)"); \
elif obj.get("Action") == "skip" and "Test" in obj: \
test_counts["skip"] += 1; \
print(f" [SKIP] {obj.get(\"Package\", \"\")} :: {obj.get(\"Test\", \"\")}");\
except: pass; \
print(f"\n+-------------------------------------------------------------------------------+"); \
print(f"| SUMMARY: {test_counts[\"pass\"]} passed, {test_counts[\"fail\"]} failed, {test_counts[\"skip\"]} skipped"); \
print(f"+-------------------------------------------------------------------------------+"); \
if failed_tests: \
print("\n[FAIL] FAILED TESTS:"); \
for t in failed_tests: print(f" - {t}"); \
sys.exit(test_counts["fail"])' 2>/dev/null || go test -short ./...
# Internal target: Test summary
test-summary:
@echo "================================================================================"
@if [ -f coverage.out ]; then \
echo "Coverage Summary:"; \
go tool cover -func=coverage.out | tail -1; \
fi
@echo "================================================================================"
# Internal target: Detailed test summary
test-summary-detailed:
@echo ""
@echo "+===============================================================================+"
@echo "| DETAILED TEST SUMMARY |"
@echo "+===============================================================================+"
@if [ -f coverage.out ]; then \
echo ""; \
echo "Coverage by Package:"; \
go tool cover -func=coverage.out | grep -v "total:" | awk '{printf " * %-60s %6s\n", $$1":"$$2, $$3}'; \
echo ""; \
echo "Overall Coverage:"; \
go tool cover -func=coverage.out | grep "total:" | awk '{printf " Total: %s\n", $$3}'; \
fi
@echo ""
# =============================================================================
# SPECIFIC TEST TARGETS
# =============================================================================
test-vm:
@echo "Testing VM Client..."
@go test -v ./internal/infrastructure/vm/... | $(MAKE) --no-print-directory format-test-output
test-obfuscation:
@echo "Testing Obfuscation..."
@go test -v ./internal/infrastructure/obfuscation/... | $(MAKE) --no-print-directory format-test-output
test-service:
@echo "Testing Services..."
@go test -v ./internal/application/services/... | $(MAKE) --no-print-directory format-test-output
test-archive:
@echo "Testing Archive Writer..."
@go test -v ./internal/infrastructure/archive/... | $(MAKE) --no-print-directory format-test-output
test-builder:
@echo "Testing Build System..."
@go test -v ./build/... | $(MAKE) --no-print-directory format-test-output
test-domain:
@echo "Testing Domain Layer..."
@go test -v ./internal/domain/... | $(MAKE) --no-print-directory format-test-output
# =============================================================================
# BUILD TARGETS - With automatic testing
# =============================================================================
# Build with automatic fast tests
build: test-fast
@echo ""
@echo "================================================================================"
@echo "Building binary..."
@echo "================================================================================"
@go build -o vmgather ./cmd/vmgather
@go build -o vmimporter ./cmd/vmimporter
@echo "[OK] Build complete: ./vmgather"
@ls -lh vmgather | awk '{print "Size:", $$5}'
@echo "[OK] Build complete: ./vmimporter"
@ls -lh vmimporter | awk '{print "Size:", $$5}'
# Build with race detector tests and linting (no Docker required)
build-safe: test-race lint
@echo ""
@echo "================================================================================"
@echo "Building binary (safe mode)..."
@echo "================================================================================"
@go build -o vmgather ./cmd/vmgather
@go build -o vmimporter ./cmd/vmimporter
@echo "[OK] Build complete (all checks passed): ./vmgather"
@echo "[OK] Build complete (all checks passed): ./vmimporter"
# Build for all platforms
build-all: test-fast
@echo ""
@echo "================================================================================"
@echo "Building for all platforms..."
@echo "================================================================================"
@go run ./build/builder.go
# =============================================================================
# UTILITY TARGETS
# =============================================================================
# Clean build artifacts
clean:
@echo "Cleaning build artifacts..."
@rm -f vmgather vmimporter coverage.out
@rm -rf dist/
@echo "[OK] Clean complete"
# =============================================================================
# DOCKER TARGETS
# =============================================================================
docker-build: docker-build-vmgather docker-build-vmimporter
docker-build-vmgather:
@set -e; \
platforms="$(PLATFORMS)"; \
platform="$${platforms%%,*}"; \
if [ "$$platform" != "$$platforms" ]; then \
echo "[INFO] docker-build loads a single-platform image; using $$platform from PLATFORMS=$$platforms"; \
fi; \
echo "Building Docker image $(DOCKER_NAMESPACE)/vmgather:local ($$platform)"; \
docker buildx build \
--platform "$$platform" \
--build-arg GO_VERSION=$(GO_VERSION) \
--label "org.opencontainers.image.source=https://github.com/VictoriaMetrics/vmgather" \
--label "org.opencontainers.image.vendor=VictoriaMetrics" \
--label "org.opencontainers.image.version=$(PKG_TAG)" \
--label "org.opencontainers.image.created=$(shell date -u +'%Y-%m-%dT%H:%M:%SZ')" \
-f build/docker/Dockerfile.vmgather \
--tag $(DOCKER_NAMESPACE)/vmgather:local \
--output $(DOCKER_OUTPUT) \
.
docker-build-vmimporter:
@set -e; \
platforms="$(PLATFORMS)"; \
platform="$${platforms%%,*}"; \
if [ "$$platform" != "$$platforms" ]; then \
echo "[INFO] docker-build loads a single-platform image; using $$platform from PLATFORMS=$$platforms"; \
fi; \
echo "Building Docker image $(DOCKER_NAMESPACE)/vmimporter:local ($$platform)"; \
docker buildx build \
--platform "$$platform" \
--build-arg GO_VERSION=$(GO_VERSION) \
--label "org.opencontainers.image.source=https://github.com/VictoriaMetrics/vmgather" \
--label "org.opencontainers.image.vendor=VictoriaMetrics" \
--label "org.opencontainers.image.version=$(PKG_TAG)" \
--label "org.opencontainers.image.created=$(shell date -u +'%Y-%m-%dT%H:%M:%SZ')" \
-f build/docker/Dockerfile.vmimporter \
--tag $(DOCKER_NAMESPACE)/vmimporter:local \
--output $(DOCKER_OUTPUT) \
.
# Format code
fmt:
@echo "Formatting code..."
@go fmt ./...
@echo "[OK] Format complete"
# Lint code (matches CI environment)
lint:
@echo "Running linter..."
@if ! command -v golangci-lint &> /dev/null; then \
echo "[ERROR] golangci-lint not found. Installing..."; \
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $$(go env GOPATH)/bin v1.59.1; \
fi
@golangci-lint run --timeout=5m
@echo "[OK] Lint complete"
# =============================================================================
# TEST ENVIRONMENT - Docker-based VM instances for E2E testing
# =============================================================================
# Build test config utility
LOCAL_TEST_ENV_GO := $(wildcard local-test-env/*.go)
local-test-env/testconfig: $(LOCAL_TEST_ENV_GO)
@cd local-test-env && go build -o testconfig .
# Load and validate test configuration
test-config-validate: local-test-env/testconfig
@echo "Validating test configuration..."
@cd local-test-env && ./testconfig validate
# Export test configuration as environment variables
test-config-env: local-test-env/testconfig
@cd local-test-env && ./testconfig env
# Generate dynamic port environment file
test-config-bootstrap: local-test-env/testconfig
@cd local-test-env && ./testconfig bootstrap
# Show test configuration as JSON
test-config-json: local-test-env/testconfig
@cd local-test-env && ./testconfig json
# Legacy clean-slate test environment cycle (cleans up before and after).
# Keep this target for debugging; `pre-push` is defined later and runs the full suite.
test-env-full:
$(MAKE) test-env-clean
$(MAKE) test-env-up
$(MAKE) test
$(MAKE) test-env-clean
test-env-up: local-test-env/testconfig
@echo "================================================================================"
@echo "Starting Test Environment"
@echo "================================================================================"
@if [ ! -d "local-test-env" ]; then \
echo "[ERROR] Error: local-test-env directory not found!"; \
echo "This directory is gitignored. Please ensure you have it locally."; \
exit 1; \
fi
@echo "Starting Docker test environment (isolated compose project)..."
@set -e; \
prev_proj="$$(cd local-test-env && VMGATHER_PROJECT_FILE=$(TEST_ENV_PROJECT_FILE_TEST) ./testconfig project 2>/dev/null || true)"; \
if [ -n "$$prev_proj" ]; then \
echo " [INFO] stopping existing compose project: $$prev_proj"; \
$(DOCKER_COMPOSE) -p "$$prev_proj" --env-file $(TEST_ENV_FILE) -f local-test-env/docker-compose.test.yml down >/dev/null 2>&1 || true; \
fi; \
started=0; \
attempt=0; \
max_attempts=3; \
while [ "$$attempt" -lt "$$max_attempts" ]; do \
attempt=$$((attempt+1)); \
echo " [INFO] bootstrapping dynamic ports (attempt $$attempt/$$max_attempts)"; \
rm -f "$(TEST_ENV_FILE)"; \
(cd local-test-env && VMGATHER_PREFER_DEFAULT_PORTS=0 ./testconfig bootstrap); \
(cd local-test-env && ./testconfig validate); \
proj="$$(cd local-test-env && VMGATHER_PROJECT_FILE=$(TEST_ENV_PROJECT_FILE_TEST) VMGATHER_PROJECT_PREFIX=vmtest ./testconfig project reset)"; \
echo " [INFO] compose project: $$proj (attempt $$attempt/$$max_attempts)"; \
tmpfile="$$(mktemp)"; \
status=0; \
$(DOCKER_COMPOSE) -p "$$proj" --env-file $(TEST_ENV_FILE) -f local-test-env/docker-compose.test.yml up -d >"$$tmpfile" 2>&1 || status=$$?; \
cat "$$tmpfile"; \
if [ "$$status" -eq 0 ]; then \
rm -f "$$tmpfile"; \
started=1; \
break; \
fi; \
if grep -qi "no space left on device" "$$tmpfile"; then \
echo ""; \
echo "[WARN] Docker reported 'no space left on device'. Running cleanup and retrying..."; \
$(DOCKER_COMPOSE) -p "$$proj" --env-file $(TEST_ENV_FILE) -f local-test-env/docker-compose.test.yml down -v >/dev/null 2>&1 || true; \
docker builder prune -af || true; \
docker system prune -af || true; \
rm -f "$$tmpfile"; \
continue; \
fi; \
if grep -qi "already exists in network" "$$tmpfile"; then \
echo ""; \
echo "[WARN] Docker network has stale endpoints. Retrying with a new compose project..."; \
$(DOCKER_COMPOSE) -p "$$proj" --env-file $(TEST_ENV_FILE) -f local-test-env/docker-compose.test.yml down >/dev/null 2>&1 || true; \
rm -f "$$tmpfile"; \
continue; \
fi; \
if grep -Eqi "port is already allocated|ports are not available|bind for .* failed|address already in use" "$$tmpfile"; then \
echo ""; \
echo "[WARN] Docker reported a host port conflict. Re-picking ports and retrying..."; \
$(DOCKER_COMPOSE) -p "$$proj" --env-file $(TEST_ENV_FILE) -f local-test-env/docker-compose.test.yml down >/dev/null 2>&1 || true; \
rm -f "$$tmpfile"; \
continue; \
fi; \
$(DOCKER_COMPOSE) -p "$$proj" --env-file $(TEST_ENV_FILE) -f local-test-env/docker-compose.test.yml down >/dev/null 2>&1 || true; \
rm -f "$$tmpfile"; \
exit "$$status"; \
done; \
if [ "$$started" -ne 1 ]; then \
echo "[ERROR] Failed to start the Docker test environment after $$max_attempts attempts"; \
exit 1; \
fi
@echo ""
@echo "Waiting for services to be ready (30 seconds)..."
@sleep 30
@echo ""
@echo "Running healthcheck..."
@cd local-test-env && ./testconfig healthcheck
@echo ""
@echo "[OK] Test environment is ready!"
@echo ""
@cd local-test-env && ./testconfig json | jq -r '"Available instances:\n - VMGather: \(.vmgather_url)\n - VMSingle No Auth: \(.vm_single_noauth.url)\n - VMSingle via VMAuth: \(.vm_single_auth.url)\n - VM Cluster: \(.vm_cluster.base_url)\n - VMSelect standalone: \(.vmselect_standalone.base_url)\n - VM Cluster via VMAuth: \(.vmauth_cluster.url)"'
@echo ""
@echo "Run 'make test-scenarios' to test all scenarios"
@echo "Run 'make test-env-logs' to see logs"
@echo "Run 'make test-env-down' to stop"
# Stop test environment
test-env-down:
@echo "Stopping Test Environment..."
@set -e; \
proj="$$(cd local-test-env && VMGATHER_PROJECT_FILE=$(TEST_ENV_PROJECT_FILE_TEST) ./testconfig project 2>/dev/null || true)"; \
if [ -z "$$proj" ]; then \
echo "[WARN] No stored compose project name; nothing to stop"; \
exit 0; \
fi; \
$(DOCKER_COMPOSE) -p "$$proj" --env-file $(TEST_ENV_FILE) -f local-test-env/docker-compose.test.yml down
@echo "[OK] Test environment stopped"
# Stop and remove all data
test-env-clean:
@echo "Cleaning Test Environment (including data)..."
@set -e; \
proj="$$(cd local-test-env && VMGATHER_PROJECT_FILE=$(TEST_ENV_PROJECT_FILE_TEST) ./testconfig project 2>/dev/null || true)"; \
if [ -z "$$proj" ]; then \
echo "[WARN] No stored compose project name; nothing to clean"; \
exit 0; \
fi; \
$(DOCKER_COMPOSE) -p "$$proj" --env-file $(TEST_ENV_FILE) -f local-test-env/docker-compose.test.yml down -v; \
rm -f "local-test-env/$(TEST_ENV_PROJECT_FILE_TEST)" >/dev/null 2>&1 || true
@echo "[OK] Test environment cleaned"
# Show logs from test environment
test-env-logs:
@set -e; \
proj="$$(cd local-test-env && VMGATHER_PROJECT_FILE=$(TEST_ENV_PROJECT_FILE_TEST) ./testconfig project 2>/dev/null || true)"; \
if [ -z "$$proj" ]; then \
echo "[ERROR] No stored compose project name; run 'make test-env-up' first"; \
exit 1; \
fi; \
$(DOCKER_COMPOSE) -p "$$proj" --env-file $(TEST_ENV_FILE) -f local-test-env/docker-compose.test.yml logs -f
# Manual (local) environment for UI testing.
manual-env-up: local-test-env/testconfig
@echo "================================================================================"
@echo "Starting Manual Test Environment"
@echo "================================================================================"
@set -e; \
prev_proj="$$(cd local-test-env && VMGATHER_PROJECT_FILE=$(TEST_ENV_PROJECT_FILE_MANUAL) ./testconfig project 2>/dev/null || true)"; \
if [ -n "$$prev_proj" ]; then \
echo " [INFO] stopping existing compose project: $$prev_proj"; \
$(DOCKER_COMPOSE) -p "$$prev_proj" --env-file $(MANUAL_ENV_FILE) -f local-test-env/docker-compose.test.yml down >/dev/null 2>&1 || true; \
fi; \
started=0; \
attempt=0; \
max_attempts=3; \
while [ "$$attempt" -lt "$$max_attempts" ]; do \
attempt=$$((attempt+1)); \
echo " [INFO] bootstrapping dynamic ports (attempt $$attempt/$$max_attempts)"; \
rm -f "$(MANUAL_ENV_FILE)"; \
(cd local-test-env && VMGATHER_ENV_FILE=.env.manual VMGATHER_PREFER_DEFAULT_PORTS=0 ./testconfig bootstrap); \
(cd local-test-env && VMGATHER_ENV_FILE=.env.manual ./testconfig validate); \
proj="$$(cd local-test-env && VMGATHER_ENV_FILE=.env.manual VMGATHER_PROJECT_FILE=$(TEST_ENV_PROJECT_FILE_MANUAL) VMGATHER_PROJECT_PREFIX=vmmanual ./testconfig project reset)"; \
echo " [INFO] compose project: $$proj (attempt $$attempt/$$max_attempts)"; \
tmpfile="$$(mktemp)"; \
status=0; \
$(DOCKER_COMPOSE) -p "$$proj" --env-file $(MANUAL_ENV_FILE) -f local-test-env/docker-compose.test.yml up -d >"$$tmpfile" 2>&1 || status=$$?; \
cat "$$tmpfile"; \
if [ "$$status" -eq 0 ]; then \
rm -f "$$tmpfile"; \
started=1; \
break; \
fi; \
if grep -qi "no space left on device" "$$tmpfile"; then \
echo ""; \
echo "[WARN] Docker reported 'no space left on device'. Running cleanup and retrying..."; \
$(DOCKER_COMPOSE) -p "$$proj" --env-file $(MANUAL_ENV_FILE) -f local-test-env/docker-compose.test.yml down -v >/dev/null 2>&1 || true; \
docker builder prune -af || true; \
docker system prune -af || true; \
rm -f "$$tmpfile"; \
continue; \
fi; \
if grep -qi "already exists in network" "$$tmpfile"; then \
echo ""; \
echo "[WARN] Docker network has stale endpoints. Retrying with a new compose project..."; \
$(DOCKER_COMPOSE) -p "$$proj" --env-file $(MANUAL_ENV_FILE) -f local-test-env/docker-compose.test.yml down >/dev/null 2>&1 || true; \
rm -f "$$tmpfile"; \
continue; \
fi; \
if grep -Eqi "port is already allocated|ports are not available|bind for .* failed|address already in use" "$$tmpfile"; then \
echo ""; \
echo "[WARN] Docker reported a host port conflict. Re-picking ports and retrying..."; \
$(DOCKER_COMPOSE) -p "$$proj" --env-file $(MANUAL_ENV_FILE) -f local-test-env/docker-compose.test.yml down >/dev/null 2>&1 || true; \
rm -f "$$tmpfile"; \
continue; \
fi; \
$(DOCKER_COMPOSE) -p "$$proj" --env-file $(MANUAL_ENV_FILE) -f local-test-env/docker-compose.test.yml down >/dev/null 2>&1 || true; \
rm -f "$$tmpfile"; \
exit "$$status"; \
done; \
if [ "$$started" -ne 1 ]; then \
echo "[ERROR] Failed to start the Docker manual environment after $$max_attempts attempts"; \
exit 1; \
fi; \
echo ""; \
echo "Waiting for services to be ready (30 seconds)..."; \
sleep 30; \
echo ""; \
echo "Running healthcheck..."; \
(cd local-test-env && VMGATHER_ENV_FILE=.env.manual ./testconfig healthcheck); \
echo ""; \
echo "[OK] Manual test environment is ready!"; \
echo ""; \
(cd local-test-env && VMGATHER_ENV_FILE=.env.manual ./testconfig json) | jq -r '"Available instances:\n - VMSingle No Auth: \(.vm_single_noauth.url)\n - VMSingle via VMAuth: \(.vm_single_auth.url)\n - VM Cluster: \(.vm_cluster.base_url)\n - VMSelect standalone: \(.vmselect_standalone.base_url)\n - VM Cluster via VMAuth: \(.vmauth_cluster.url)"'
@echo ""
@echo "Run 'make manual-env-logs' to see logs"
@echo "Run 'make manual-env-down' to stop"
manual-env-down:
@echo "Stopping Manual Test Environment..."
@set -e; \
proj="$$(cd local-test-env && VMGATHER_PROJECT_FILE=$(TEST_ENV_PROJECT_FILE_MANUAL) ./testconfig project 2>/dev/null || true)"; \
if [ -z "$$proj" ]; then \
echo "[WARN] No stored compose project name; nothing to stop"; \
exit 0; \
fi; \
$(DOCKER_COMPOSE) -p "$$proj" --env-file $(MANUAL_ENV_FILE) -f local-test-env/docker-compose.test.yml down
@echo "[OK] Manual test environment stopped"
manual-env-clean:
@echo "Cleaning Manual Test Environment (including data)..."
@set -e; \
proj="$$(cd local-test-env && VMGATHER_PROJECT_FILE=$(TEST_ENV_PROJECT_FILE_MANUAL) ./testconfig project 2>/dev/null || true)"; \
if [ -z "$$proj" ]; then \
echo "[WARN] No stored compose project name; nothing to clean"; \
exit 0; \
fi; \
$(DOCKER_COMPOSE) -p "$$proj" --env-file $(MANUAL_ENV_FILE) -f local-test-env/docker-compose.test.yml down -v; \
rm -f "local-test-env/$(TEST_ENV_PROJECT_FILE_MANUAL)" >/dev/null 2>&1 || true; \
rm -f "$(MANUAL_ENV_FILE)" >/dev/null 2>&1 || true
@echo "[OK] Manual test environment cleaned"
manual-env-logs:
@set -e; \
proj="$$(cd local-test-env && VMGATHER_PROJECT_FILE=$(TEST_ENV_PROJECT_FILE_MANUAL) ./testconfig project 2>/dev/null || true)"; \
if [ -z "$$proj" ]; then \
echo "[ERROR] No stored compose project name; run 'make manual-env-up' first"; \
exit 1; \
fi; \
$(DOCKER_COMPOSE) -p "$$proj" --env-file $(MANUAL_ENV_FILE) -f local-test-env/docker-compose.test.yml logs -f
manual-vmgather-up:
@echo "Building vmgather..."
@go build -o vmgather ./cmd/vmgather
@echo "Starting vmgather UI on http://localhost:8080 ..."
@./vmgather -addr localhost:8080 -no-browser
manual-vmgather-down:
@echo "Stopping vmgather UI (port 8080)..."
@pid="$$(lsof -tiTCP:8080 -sTCP:LISTEN 2>/dev/null || true)"; \
if [ -z "$$pid" ]; then \
echo "[WARN] No listener found on :8080"; \
exit 0; \
fi; \
cmd="$$(ps -p "$$pid" -o command= 2>/dev/null || true)"; \
if echo "$$cmd" | grep -q "[/]vmgather"; then \
kill "$$pid"; \
else \
echo "[ERROR] Refusing to kill pid=$$pid (not vmgather): $$cmd"; \
exit 1; \
fi
# Integration tests: binary tests Docker environment
test-integration: local-test-env/testconfig
@echo "================================================================================"
@echo "Integration Tests: Binary testing Docker environment"
@echo "================================================================================"
@cd local-test-env && ./testconfig healthcheck
@set -e; \
eval "$$(cd local-test-env && ./testconfig env)"; \
cd local-test-env && ./testconfig scenarios; \
cd ..; \
INTEGRATION_TEST=1 LIVE_VM_URL="$$VM_SINGLE_NOAUTH_URL" go test -tags "integration realdiscovery" ./tests/integration/...
# Alias for backward compatibility
test-scenarios: test-integration
# Full E2E test: start env, test, stop
test-env: test-env-up test-integration
@echo ""
@echo "[OK] All E2E tests completed!"
@echo "Run 'make test-env-down' to stop the environment"
# Playwright tests: require the local docker environment.
test-e2e:
@echo "================================================================================"
@echo "Playwright E2E Suite"
@echo "================================================================================"
@cd local-test-env && ./testconfig healthcheck
@echo ""
@echo "[1/3] Building vmgather..."
@go build -o vmgather ./cmd/vmgather
@echo ""
@echo "[2/3] Ensuring E2E dependencies exist..."
@if [ ! -d "tests/e2e/node_modules" ]; then \
echo " Installing node deps via npm ci..."; \
cd tests/e2e && npm ci; \
fi
@echo ""
@echo "[3/3] Running Playwright (workers=1)..."
@set -e; \
eval "$$(cd local-test-env && ./testconfig env)"; \
E2E_REAL="$${E2E_REAL:-1}"; \
LIVE_VM_URL="$${LIVE_VM_URL:-$$VM_SINGLE_NOAUTH_URL}"; \
export E2E_REAL LIVE_VM_URL; \
if [ -n "$$VMGATHER_PORT" ] && command -v lsof >/dev/null 2>&1; then \
pid="$$(lsof -tiTCP:$$VMGATHER_PORT -sTCP:LISTEN || true)"; \
if [ -n "$$pid" ]; then \
cmd="$$(ps -p "$$pid" -o command= || true)"; \
if echo "$$cmd" | grep -q "[/]vmgather" && echo "$$cmd" | grep -q -- "-no-browser"; then \
echo " [WARN] Killing stale vmgather web server (pid=$$pid, port=$$VMGATHER_PORT)"; \
kill "$$pid" || true; \
sleep 1; \
fi; \
fi; \
fi; \
cd tests/e2e && npx playwright test --workers=1
@echo ""
@echo "================================================================================"
@echo "[OK] Playwright E2E suite passed!"
@echo "================================================================================"
# VMImporter Playwright tests.
test-e2e-importer:
@echo "================================================================================"
@echo "Playwright E2E Suite (VMImporter)"
@echo "================================================================================"
@echo ""
@echo "[1/2] Building vmimporter..."
@go build -o vmimporter ./cmd/vmimporter
@echo ""
@echo "[2/2] Running importer regressions..."
@set -e; \
if [ ! -d "tests/e2e/node_modules" ]; then \
echo " Installing node deps via npm ci..."; \
cd tests/e2e && npm ci; \
fi; \
cd tests/e2e && npx playwright test -c playwright.importer.config.js --workers=1
@echo ""
@echo "================================================================================"
@echo "[OK] Playwright VMImporter suite passed!"
@echo "================================================================================"
# Everything: unit + docker scenarios + Playwright suites.
test-all: test-full test-e2e test-e2e-importer
# Everything + cleanup (CI / disk-constrained environments).
test-all-clean:
@set -e; \
status=0; \
$(MAKE) --no-print-directory test-all || status=$$?; \
echo ""; \
echo "================================================================================"; \
echo "Cleaning Docker test environment (including volumes)..."; \
echo "================================================================================"; \
$(MAKE) --no-print-directory test-env-clean || true; \
exit $$status
# Local mirror of `.github/workflows/security.yml`.
security-check: security-check-go security-check-secrets security-check-dockerfile security-check-images
security-check-go:
@echo "================================================================================"
@echo "Security Check: govulncheck"
@echo "================================================================================"
@docker run --rm -v "$$PWD:/work" -w /work golang:$(GO_VERSION)-alpine@sha256:f6751d823c26342f9506c03797d2527668d095b0a15f1862cddb4d927a7a4ced sh -ec '\
go install golang.org/x/vuln/cmd/govulncheck@$(GOVULNCHECK_VERSION); \
$$(go env GOPATH)/bin/govulncheck ./...'
security-check-secrets:
@echo "================================================================================"
@echo "Security Check: secret scanning (gitleaks + trufflehog verified)"
@echo "================================================================================"
@docker run --rm -v "$$PWD:/work" -w /work zricethezav/gitleaks:v8.24.2 \
detect --source . --no-git --config .gitleaks.toml --redact
@docker run --rm -v "$$PWD:/work" trufflesecurity/trufflehog:3.93.3 \
git file:///work --results=verified --fail
security-check-dockerfile:
@echo "================================================================================"
@echo "Security Check: Dockerfile lint + misconfig"
@echo "================================================================================"
@docker run --rm -i hadolint/hadolint:v2.12.0 < build/docker/Dockerfile.vmgather
@docker run --rm -i hadolint/hadolint:v2.12.0 < build/docker/Dockerfile.vmimporter
@docker run --rm -v "$$PWD:/work" -w /work aquasec/trivy:0.65.0 \
config --severity HIGH,CRITICAL --exit-code 1 build/docker/Dockerfile.vmgather
@docker run --rm -v "$$PWD:/work" -w /work aquasec/trivy:0.65.0 \
config --severity HIGH,CRITICAL --exit-code 1 build/docker/Dockerfile.vmimporter
security-check-images: docker-build
@echo "================================================================================"
@echo "Security Check: container image scan"
@echo "================================================================================"
@docker run --rm -v /var/run/docker.sock:/var/run/docker.sock aquasec/trivy:0.65.0 \
image --severity HIGH,CRITICAL --ignore-unfixed --exit-code 1 $(DOCKER_NAMESPACE)/vmgather:local
@docker run --rm -v /var/run/docker.sock:/var/run/docker.sock aquasec/trivy:0.65.0 \
image --severity HIGH,CRITICAL --ignore-unfixed --exit-code 1 $(DOCKER_NAMESPACE)/vmimporter:local
# Local gate before pushing changes.
pre-push: test-all-clean security-check
# =============================================================================
# FULL TEST SUITE
# =============================================================================
# Complete test cycle: unit tests + integration scenarios
test-full:
@echo "================================================================================"
@echo "Full Test Suite"
@echo "================================================================================"
@echo ""
@echo "[1/3] Unit tests (without Docker)..."
@$(MAKE) test-unit-full
@echo ""
@echo "[2/3] Starting Docker test environment..."
@$(MAKE) test-env-down 2>/dev/null || true
@$(MAKE) test-env-up
@echo ""
@echo "[3/3] Integration tests (binary + Docker)..."
@$(MAKE) test-integration
@echo ""
@echo "================================================================================"
@echo "[OK] All tests passed!"
@echo "================================================================================"
@echo ""
@echo "Note: Docker environment still running. Stop with 'make test-env-down'"