default: build

# Specify which test file to use.
TEST_FILE_NAME?=""

# Specify which test names to filter by.
TEST_FILTER?=""

# Internal vars
TEST_RUNNER=../../docker-utils/run_test_in_docker.sh

build:
	cargo build --all-targets

lint:
	cargo clippy -- -D warnings -A dead_code

fmt:
	cargo fmt --all
	cargo sort --workspace

.PHONY: test
test:
	echo "Running Nomad integration tests"
	DOCKER_TAG=$(DOCKER_TAG) TEST_FILE_NAME="$(TEST_FILE_NAME)" TEST_FILTER="$(TEST_FILTER)" ./$(TEST_RUNNER)
	# Starts up the local testing infrastructure (i.e. Consul/Nomad/Vault)
run-test-env: stop-test-env
	docker network rm persistence || true

	# Create the network with a preset subnet (we just choose any) since we use Consul for DNS which hence
	# needs a static IP address.
	docker network create --subnet 172.16.238.0/24 persistence || true
	mkdir -p /tmp/nomad && mkdir -p /tmp/nomad1

	COMPOSE_PROFILES=$(COMPOSE_PROFILES) docker-compose -f core-compose.yml up --detach --force-recreate


ifndef DISABLE_GENERIC_TESTER
	bash docker-utils/setupNomadinDocker.sh
endif


# Stops the local testing infrastructure
stop-test-env:
	docker-compose -f core-compose.yml down --volumes --remove-orphans

# Helper target to build the image used by test-crates-in-docker. Allows splitting out build vs test time.
build-crates-tester-docker:
	DOCKER_BUILDKIT=1 COMPOSE_DOCKER_CLI_BUILD=1 docker-compose -f test-compose.yml build --progress=plain -- crates-tester

test-crates:
	cargo test --workspace

# Target used by CI as we can not assume that all infrastructure is hosted via localhost in that environment.
test-crates-in-docker:
	DOCKER_BUILDKIT=1 COMPOSE_DOCKER_CLI_BUILD=1 docker-compose -f test-compose.yml up --exit-code-from crates-tester crates-tester
