#! /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-0
# $ ./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/master/test/README.md
#
# for more information about the Cockpit integration test machinery.

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

TEST_NODE_MODULES = node_modules/chrome-remote-interface node_modules/sizzle

check: prepare
	integration-tests/check-subscriptions

reset:
	rm -f $(COCKPIT_TAR) $(SUBMAN_TAR) $(SMBEXT_TAR) $(VM_IMAGE)

prepare: reset $(VM_IMAGE) integration-test/common $(TEST_NODE_MODULES)

$(COCKPIT_TAR): cockpit/node_modules
	make -C cockpit dist-gzip

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

$(SMBEXT_TAR):
	tar czf $(SMBEXT_TAR) build_ext src/subscription_manager/gui/data/icons/hicolor

$(VM_IMAGE): $(COCKPIT_TAR) $(SUBMAN_TAR) $(SMBEXT_TAR) bots
	rm -f $(VM_IMAGE)
	bots/image-customize -v $(TEST_OS) \
          -u $(COCKPIT_TAR):/var/tmp/ \
          -u $(SUBMAN_TAR):/var/tmp/ \
          -u $(SMBEXT_TAR):/var/tmp/ \
          -s ./integration-tests/vm.install

# checkout Cockpit's bots for standard test VM images and API to launch them
# must be from master, as only that has current and existing images; but testvm.py API is stable
# support CI testing against a bots change
bots:
	git clone --quiet \
		--reference-if-able $${XDG_CACHE_HOME:-$$HOME/.cache}/cockpit-project/bots \
		https://github.com/cockpit-project/bots.git
	if [ -n "$$COCKPIT_BOTS_REF" ]; then \
	    git -C bots fetch --quiet --depth=1 origin "$$COCKPIT_BOTS_REF"; \
	    git -C bots checkout --quiet FETCH_HEAD; \
	fi
	@echo "checked out bots/ ref $$(git -C bots rev-parse HEAD)"

# checkout Cockpit's test API; this has no API stability guarantee, so check out a stable tag
# when you start a new project, use the latest relese, and update it from time to time
integration-test/common:
	git fetch --depth=1 https://github.com/cockpit-project/cockpit.git 204
	git archive FETCH_HEAD -- test/common | tar -x -C integration-tests --strip-components=1 -f -

node_modules:
	mkdir node_modules

node_modules/%: node_modules
	npm install $*

cockpit/node_modules:
	mkdir cockpit/node_modules
	cd cockpit/ && npm install

.PHONY: check prepare reset
