From 8cc858657a634958ef5cc0b66952896a46d07f1e Mon Sep 17 00:00:00 2001 From: Jiri Kucera Date: May 19 2022 12:53:20 +0000 Subject: Add tools simplifying tests development --- diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0f9a289 --- /dev/null +++ b/Makefile @@ -0,0 +1,47 @@ +# SPDX-License-Identifier: MIT + +ECHO ?= echo +TMT ?= tmt + +IMAGE ?= fedora:testenv + + +.PHONY: help lint build check + +help:: + @$(ECHO) "Usage: $(MAKE) TARGET [VARIABLES]" + @$(ECHO) "where TARGET is one of" + @$(ECHO) " help" + @$(ECHO) " print this screen" + +help:: + @$(ECHO) " lint" + @$(ECHO) " validate tests and plans" +lint: + $(TMT) -vvv lint + +help:: + @$(ECHO) " build" + @$(ECHO) " build a $(IMAGE) container image; a name of the image" \ + "can" + @$(ECHO) " be changed through IMAGE variable; it is required that" + @$(ECHO) " ./containers/Containerfile.[.] exists where" + @$(ECHO) " [:] is a name of the IMAGE" +build: + F="$(F)" ./scripts/build-image "$(IMAGE)" + +help:: + @$(ECHO) " check" + @$(ECHO) " run tests in the container; to specify the container" \ + "image," + @$(ECHO) " use IMAGE variable (default $(IMAGE)); other variables" + @$(ECHO) " customizing running the tests are:" + @$(ECHO) " F: force the IMAGE build (0 or 1, default 0)" + @$(ECHO) " T: specifies pattern for test selection (see tmt help)" + @$(ECHO) " L: print test protocol generated by beakerlib" \ + "(0 or 1, default 1, works only with blutils.sh)" +check: F = 0 +check: T = +check: L = 1 +check: build + L="$(L)" T="$(T)" ./scripts/tmt-run "$(IMAGE)" diff --git a/containers/Containerfile.fedora.testenv b/containers/Containerfile.fedora.testenv new file mode 100644 index 0000000..097b64b --- /dev/null +++ b/containers/Containerfile.fedora.testenv @@ -0,0 +1,7 @@ +# SPDX-License-Identifier: MIT + +FROM fedora:latest + +RUN dnf -y update && \ + dnf -y install beakerlib && \ + dnf clean all diff --git a/scripts/build-image b/scripts/build-image new file mode 100755 index 0000000..5bc48e2 --- /dev/null +++ b/scripts/build-image @@ -0,0 +1,65 @@ +#!/bin/bash +# SPDX-License-Identifier: MIT + +set -euxo pipefail + +HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" + + +function usage() { + cat <<-__USAGE__ + Usage: $0 IMAGE + where + IMAGE + image name with optional tag + Environment variables: + F: force the image build (0 or 1, default 1) + __USAGE__ +} + +function die() { + { + echo "$0: $*" + echo "" + usage + } >&2 + exit 1 +} + +function main() { + local force="${F:-1}" + local image="" + local containerfile="" + + if [[ "${force}" != [01] ]]; then + die "F should be either 0 or 1." + fi + + image="${1:-}" + image="${image%:latest}" + + if [[ -z "${image}" ]]; then + die "Missing image name." + fi + + if [[ ${force} -eq 1 ]]; then + if podman image exists "${image}"; then + podman rmi "${image}" + fi + fi + + if [[ ${force} -eq 0 ]] && podman image exists "${image}"; then + return + fi + + containerfile="${HERE}/../containers/Containerfile.${image/:/.}" + + if [[ ! -f "${containerfile}" ]]; then + die "${containerfile} does not exists." + fi + + podman build -t "${image}" -f "${containerfile}" . +} + + +main "$@" diff --git a/scripts/tmt-run b/scripts/tmt-run new file mode 100755 index 0000000..8c40d9f --- /dev/null +++ b/scripts/tmt-run @@ -0,0 +1,57 @@ +#!/bin/bash +# SPDX-License-Identifier: MIT + +set -euxo pipefail + +HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" + + +function usage() { + cat <<-__USAGE__ + Usage: $0 IMAGE + where + IMAGE + image name with optional tag + Environment variables: + T: pattern to select tests to be run + L: print test protocol (0 or 1, default 1, works only with + blutils.sh) + __USAGE__ +} + +function die() { + { + echo "$0: $*" + echo "" + usage + } >&2 + exit 1 +} + +function main() { + local image="${1:-}" + local tests_selector="${T:-}" + local test_protocol="${L:-1}" + local tmt_args=( -vvv run -a ) + + if [[ "${test_protocol}" != [01] ]]; then + die "L should be either 0 or 1." + fi + + if [[ -z "${image}" ]]; then + die "Missing image name." + fi + + if [[ ${test_protocol} -eq 0 ]]; then + tmt_args+=( -e BLUTILS_NO_SUMMARY=1 ) + fi + tmt_args+=( provision -h container -i "${image}" plans -n /plans/basic ) + if [[ "${tests_selector}" ]]; then + tmt_args+=( tests -n "${tests_selector}" ) + fi + + tmt "${tmt_args[@]}" +} + + +main "$@"