GINKGO       ?= "github.com/onsi/ginkgo/v2/ginkgo"
GINKGO_ARGS  ?= -v --fail-fast -r --timeout=3h

.PHONY: prepare-test
prepare-test:
ifeq ("$(DISK)","")
	@echo "No disk image found, run 'make build-disk' first"
	@exit 1
endif
	@scripts/run_vm.sh start $(DISK)
	@echo "VM started from $(DISK)"

.PHONY: prepare-upgrade-test
prepare-upgrade-test: 
ifeq ("$(UPGRADE_DISK_CHECK)","")
	mkdir -p $(ROOT_DIR)/build
	@echo "Downloading disk from $(UPGRADE_DISK_URL) ..."
	@wget -q --tries=3 $(UPGRADE_DISK_URL) -O $(ROOT_DIR)/build/$(UPGRADE_DISK)
endif
	@scripts/run_registry.sh start
	@scripts/run_vm.sh start $(ROOT_DIR)/build/$(UPGRADE_DISK)
	@scripts/run_registry.sh push "$(TOOLKIT_REPO):$(VERSION)" "$(REPO):$(VERSION)"
	@echo "VM started from $(ROOT_DIR)/build/$(UPGRADE_DISK)"

.PHONY: test-active
test-active: prepare-test
	VM_PID=$$(scripts/run_vm.sh vmpid) COS_PASS=cos go run $(GINKGO) $(GINKGO_ARGS) ./tests/wait-active

.PHONY: prepare-installer-test
prepare-installer-test:
ifeq ("$(ISO)","")
	@echo "No ISO image found, run 'make build-iso' first"
	@exit 1
endif
	@scripts/run_vm.sh start $(ISO)
	@echo "VM started from $(ISO)"

.PHONY: test-clean
test-clean: test-stop
	@scripts/run_registry.sh stop
	@scripts/run_vm.sh clean

.PHONY: test-stop
test-stop:
	@scripts/run_vm.sh stop

.PHONY: test-installer
test-installer: prepare-installer-test
	VM_PID=$$(scripts/run_vm.sh vmpid) go run $(GINKGO) $(GINKGO_ARGS) ./tests/installer
	VM_PID=$$(scripts/run_vm.sh vmpid) go run $(GINKGO) $(GINKGO_ARGS) ./tests/smoke

.PHONY: test-smoke
test-smoke: test-active
	VM_PID=$$(scripts/run_vm.sh vmpid) go run $(GINKGO) $(GINKGO_ARGS) ./tests/smoke

.PHONY: test-grubfallback
test-grubfallback: test-active
	VM_PID=$$(scripts/run_vm.sh vmpid) go run $(GINKGO) $(GINKGO_ARGS) ./tests/grubfallback

.PHONY: test-recovery
test-recovery: test-active
	VM_PID=$$(scripts/run_vm.sh vmpid) go run $(GINKGO) $(GINKGO_ARGS) ./tests/recovery

.PHONY: test-fallback
test-fallback: test-active
	VM_PID=$$(scripts/run_vm.sh vmpid) go run $(GINKGO) $(GINKGO_ARGS) ./tests/fallback

.PHONY: test-fsck
test-fsck: test-active
	VM_PID=$$(scripts/run_vm.sh vmpid) go run $(GINKGO) $(GINKGO_ARGS) ./tests/fsck

.PHONY: test-downgrade
test-downgrade: test-active
	VM_PID=$$(scripts/run_vm.sh vmpid) go run $(GINKGO) $(GINKGO_ARGS) ./tests/downgrade -- $(UPGRADE_ARGS)

.PHONY: test-upgrade
test-upgrade: prepare-upgrade-test
	@scripts/run_upgradetest.sh start $(GINKGO) "$(GINKGO_ARGS)" "$(TOOLKIT_REPO):$(VERSION)" "$(REPO):$(VERSION)"

.PHONY: test-cli
test-cli:
	go generate ./...
	go run $(GINKGO) --label-filter '!root' --fail-fast --race --covermode=atomic --coverprofile=coverage.txt --coverpkg=github.com/rancher/elemental-toolkit/... -p -r ${PKG}

.PHONY: test-root
test-root:
ifneq ($(shell id -u), 0)
	@echo "This tests require root/sudo to run."
	@exit 1
else
	go run $(GINKGO) run --label-filter root --fail-fast --race --covermode=atomic --coverprofile=coverage_root.txt --coverpkg=github.com/rancher/elemental-toolkit/... -procs=1 -r ${PKG}
endif
