#! /usr/bin/make -f

include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/pkg-info.mk

export PYBUILD_NAME=locust
export SETUPTOOLS_SCM_PRETEND_VERSION=$(DEB_VERSION_UPSTREAM)

# tests excluded from the build and the autopkgtest:
# test_stuff, test_verify_false_succeeds_with_bad_cert,
#   test_verify_true_fails_with_bad_cert, test_locustfile_from_url - need
#   network access
# test_constructor - expects localhost to also resolve to an IPv6 address,
#   which is not the case on every builder
# test_default_wait_time - timing sensitive, flaky on loaded builders
# StandaloneIntegrationTests, DistributedIntegrationTests, TelemetryTests -
#   start locust processes and stop them with SIGINT, which fails at random
#   even on amd64: the KeyboardInterrupt lands in whatever greenlet runs, and
#   some swallow it, leaving the master running and its --processes workers
#   joining the masters of the following tests, while others print it as a
#   traceback the tests look for
# --deselect matches node id prefixes: the trailing :: keeps a class from
# also matching the classes whose name starts with it; a test name can't be
# ended that way, so test_constant_throughput below also deselects the
# argument check test_constant_throughput_invalid_rate
PYTEST_ARGS = -v -k 'not test_stuff and not test_verify_false_succeeds_with_bad_cert \
	and not test_verify_true_fails_with_bad_cert and not test_locustfile_from_url \
	and not test_constructor and not test_default_wait_time' \
	--deselect locust/test/test_main.py::StandaloneIntegrationTests:: \
	--deselect locust/test/test_main.py::DistributedIntegrationTests:: \
	--deselect locust/test/test_main.py::TelemetryTests::
# tests with fixed timeouts, sleeps or elapsed time tolerances, excluded from
# the autopkgtest except on amd64 and i386: they keep failing on the slower or
# loaded riscv64, arm64, ppc64el and s390x workers. The build, arch all,
# always runs them.
ifdef PYBUILD_AUTOPKGTEST
ifeq (,$(filter $(DEB_HOST_ARCH),amd64 i386))
PYTEST_ARGS += \
	--deselect locust/test/test_runners.py::TestStopTimeout:: \
	--deselect locust/test/test_runners.py::TestMasterWorkerRunners:: \
	--deselect locust/test/test_runners.py::TestLocustRunner::test_can_call_stop_endpoint_if_currently_swarming \
	--deselect locust/test/test_runners.py::TestLocustRunner::test_runner_quit_can_run_on_stop_for_multiple_users_concurrently \
	--deselect locust/test/test_runners.py::TestLocustRunner::test_stop_users_count \
	--deselect locust/test/test_runners.py::TestLocustRunner::test_stop_users_with_spawn_rate \
	--deselect locust/test/test_runners.py::TestLocustRunner::test_swarm_endpoint_is_non_blocking \
	--deselect locust/test/test_runners.py::TestLocustRunner::test_user_count_starts_from_specified_amount_when_creating_new_test_after_previous_step_has_been_stopped \
	--deselect locust/test/test_runners.py::TestLocustRunner::test_users_can_call_runner_quit_without_deadlocking \
	--deselect locust/test/test_runners.py::TestMasterRunner::test_spawn_zero_locusts \
	--deselect locust/test/test_runners.py::TestMasterRunner::test_wait_for_workers_report_after_ramp_up \
	--deselect locust/test/test_runners.py::TestMasterRunner::test_worker_missing_after_heartbeat_dead_interval \
	--deselect locust/test/test_runners.py::TestWorkerRunner::test_worker_heartbeat_messages_sent_to_master \
	--deselect locust/test/test_dispatch.py::TestRampUpUsersFromZero:: \
	--deselect locust/test/test_dispatch.py::TestRampDownUsersToZero:: \
	--deselect locust/test/test_dispatch.py::TestAddWorker:: \
	--deselect locust/test/test_dispatch.py::TestDispatchUsersToWorkersHavingTheSameUsersAsTheTarget:: \
	--deselect locust/test/test_dispatch.py::TestRemoveWorker::test_remove_two_workers_between_two_ramp_ups \
	--deselect locust/test/test_dispatch.py::TestRemoveWorker::test_remove_two_workers_during_ramp_down \
	--deselect locust/test/test_dispatch.py::TestRemoveWorker::test_remove_two_workers_during_ramp_up \
	--deselect locust/test/test_dispatch.py::TestRemoveWorker::test_remove_worker_between_two_ramp_ups \
	--deselect locust/test/test_dispatch.py::TestRemoveWorker::test_remove_worker_during_ramp_down \
	--deselect locust/test/test_dispatch.py::TestRemoveWorker::test_remove_worker_during_ramp_up \
	--deselect locust/test/test_dispatch.py::TestRemoveWorker::test_remove_worker_during_ramp_up_with_fixed_user \
	--deselect locust/test/test_dispatch.py::TestRampUpDifferentUsers::test_add_worker_during_ramp_up_custom_classes \
	--deselect locust/test/test_dispatch.py::TestRampUpDifferentUsers::test_remove_worker_during_ramp_up_custom_classes \
	--deselect locust/test/test_dispatch.py::TestLargeScale::test_distribute_users \
	--deselect locust/test/test_dispatch.py::TestLargeScale::test_ramp_up_from_0_to_100_000_users_with_50_user_classes_and_1000_workers_and_5000_spawn_rate \
	--deselect locust/test/test_fasthttp.py::TestFastHttpSession::test_error_message_with_name_replacement \
	--deselect locust/test/test_fasthttp.py::TestFastHttpSession::test_streaming_response \
	--deselect locust/test/test_fasthttp.py::TestFastHttpSession::test_streaming_response_catch_response \
	--deselect locust/test/test_fasthttp.py::TestFastHttpUserClass::test_client_pool_concurrency \
	--deselect locust/test/test_http.py::TestHttpSession::test_catch_response_timeout \
	--deselect locust/test/test_http.py::TestHttpSession::test_error_message_with_name_replacement \
	--deselect locust/test/test_http.py::TestHttpSession::test_event_measure \
	--deselect locust/test/test_http.py::TestHttpSession::test_streaming_response \
	--deselect locust/test/test_locust_class.py::TestLocustClass::test_locust_graceful_stop \
	--deselect locust/test/test_locust_class.py::TestLocustClass::test_locust_start \
	--deselect locust/test/test_wait_time.py::TestWaitTime::test_constant_throughput
endif
endif

# the tests are not installed: run them from symlinks to the installed
# package, with locust/test copied next to them; every interpreter gets the
# same {build_dir}, so the tree of the previous one is removed first
ifdef PYBUILD_AUTOPKGTEST
# empty, not unset: pybuild would set them to a dead proxy, which also
# catches the requests the tests make to 127.0.0.1
export http_proxy =
export https_proxy =
export PYBUILD_BEFORE_TEST = rm -rf {build_dir}/locust && mkdir -p {build_dir} && cp -as /usr/lib/python3/dist-packages/locust {build_dir}/ && cp -a {dir}/locust/test {build_dir}/locust/
export PYBUILD_TEST_ARGS = $(PYTEST_ARGS) locust/test
endif

%:
	dh $@ --buildsystem=pybuild

execute_before_dh_auto_build:
	# prebuilt web UI from the webui component tarball
	cp -a webui/locust/webui/dist locust/webui/
	python3 debian/remove-azure-ad.py locust/webui/dist/assets/src-*.js

override_dh_auto_test:
	http_proxy= https_proxy= PATH=$(PATH):$(CURDIR)/debian PYBUILD_SYSTEM=custom PYBUILD_TEST_ARGS="PYTHONPATH={build_dir} {interpreter} -m pytest $(PYTEST_ARGS)" dh_auto_test

execute_before_dh_installman:
	# COLUMNS keeps argparse from wrapping the help text, and the URLs in it;
	# the package is still in the versioned dist-packages before dh_python3
	COLUMNS=1000 PYTHONPATH=$(CURDIR)/debian/python3-locust/usr/lib/python$(shell py3versions -dv)/dist-packages \
		help2man --no-info --version-string=$(DEB_VERSION_UPSTREAM) \
		--name="developer-friendly load testing framework" \
		--output=debian/locust.1 debian/python3-locust/usr/bin/locust

override_dh_installexamples:
	dh_installexamples -X.pyc

override_dh_clean:
	rm -rf locust/_version.py locust/webui/dist web_test_*.csv locust_output.json debian/locust.1
	dh_clean
