#! /usr/bin/make -f

# This is the entry point for the Cockpit integration test machinery.
#
# To run the tests manually and individually, you can do something
# like this:
#
# $ export TEST_OS=rhel-8-4
# $ ./integration-tests/run prepare
# $ ./integration-tests/check-subscriptions -v -j1 -t
#
# You need a couple of things installed for that.  At least Python 3,
# a Chromium browser, and libvirtd.  See
#
#     https://github.com/cockpit-project/cockpit/blob/main/test/README.md
#
# for more information about the Cockpit integration test machinery.

ifeq ($(TEST_OS),)
TEST_OS = rhel-8-4
endif
export TEST_OS
SUB_MAN_COCKPIT=$(CURDIR)/integration-tests/submancockpit
VM_IMAGE=$(SUB_MAN_COCKPIT)/test/images/$(TEST_OS).qcow2
SUBMAN_TAR=$(CURDIR)/dist/subscription-manager.tar.gz
SMBEXT_TAR=$(CURDIR)/dist/subscription-manager-build-extra.tar.gz

check: prepare
	# make sure CWD is the cockpit source dir;
	# set the reference branch to empty (i.e. none), otherwise run-tests
	# will try to use the currently tested sub-man branch in sub-man-cockpit
	cd $(SUB_MAN_COCKPIT) && test/common/run-tests --base ''

reset:
	rm -f $(SUBMAN_TAR) $(SMBEXT_TAR)
	rm -rf $(SUB_MAN_COCKPIT)

prepare: $(VM_IMAGE)

$(SUBMAN_TAR):
	fn=$$(python3 ./setup.py --fullname); \
	python3 ./setup.py sdist && \
        mv dist/$$fn.tar.gz $(SUBMAN_TAR)

$(SMBEXT_TAR):
	tar czf $(SMBEXT_TAR) build_ext

$(VM_IMAGE): $(SUB_MAN_COCKPIT) $(SUBMAN_TAR) $(SMBEXT_TAR) integration-tests/vm.install
	rm -f $(VM_IMAGE)
	# setup the test image from subscription-manager-cockpit without
	# a custom subscription-manager, which will be installed manually
	TEST_SCENARIO=system make -C $(SUB_MAN_COCKPIT) prepare-check
	# install the custom subscription-manager on the test image
	cd $(SUB_MAN_COCKPIT) && bots/image-customize -v $(TEST_OS) \
          -u $(SUBMAN_TAR):/var/tmp/ \
          -u $(SMBEXT_TAR):/var/tmp/ \
          -s ../../integration-tests/vm.install

$(SUB_MAN_COCKPIT):
	git clone --quiet https://github.com/candlepin/subscription-manager-cockpit.git $(SUB_MAN_COCKPIT)
	@echo "checked out $(SUB_MAN_COCKPIT) ref $$(git -C $(SUB_MAN_COCKPIT) rev-parse HEAD)"

.PHONY: check prepare reset
