From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id 60F9D5A0277 for ; Tue, 27 Jun 2023 04:54:44 +0200 (CEST) Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4Qqq7J6gYxz4wqg; Tue, 27 Jun 2023 12:54:36 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1687834476; bh=LksaFc0c5xHqnj+0At34tfmq8uRQo9xuP0f7hPmoVb0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=A9J/WKQyWu8j/roSb5ahWeQhtIRD/G5CueUFUiMd9MbSxRFuprYlGZYGJmGP9TXEw Ej1KbqBRG9/duX5pl6KGXpTaWzHg5usS7LtyN6IZLvwXK8ETLWLUQZwcy2UKO9b1YZ k09B/rcq1+cH5QN7YAmmsMcx1ie3vRNMRuNsnzSs= From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH 08/27] tasst, avocado: Introduce library of common test helpers Date: Tue, 27 Jun 2023 12:54:09 +1000 Message-ID: <20230627025429.2209702-9-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230627025429.2209702-1-david@gibson.dropbear.id.au> References: <20230627025429.2209702-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: CPFA2L75JKSHTGJJO56675FWN52SUPXX X-Message-ID-Hash: CPFA2L75JKSHTGJJO56675FWN52SUPXX X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: crosa@redhat.com, jarichte@redhat.com, David Gibson X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Create a Python package "tasst" with common helper code for use in passt and pasta. Initially it just has some trivial typechecking helpers. Extend the avocado-meta tests to include selftests within the tasst library. This lets us test the functionality of the library itself without involving actual passt or pasta. Signed-off-by: David Gibson --- test/Makefile | 8 ++++--- test/tasst/__init__.py | 11 ++++++++++ test/tasst/typecheck.py | 47 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 test/tasst/__init__.py create mode 100644 test/tasst/typecheck.py diff --git a/test/Makefile b/test/Makefile index fc8d3a6f..c06d63b6 100644 --- a/test/Makefile +++ b/test/Makefile @@ -209,7 +209,8 @@ jammy-server-cloudimg-s390x.img: PYTHON = python3 VENV = venv PLUGIN = avocado_classless -PYPKGS = $(PLUGIN)/$(PLUGIN) $(wildcard $(PLUGIN)/*.py) +PYPKGS = $(PLUGIN)/$(PLUGIN) $(wildcard $(PLUGIN)/*.py) \ + tasst # Put this back if/when the plugin becomes available in upstream/system avocado #AVOCADO := $(shell which avocado) @@ -229,11 +230,11 @@ avocado-assets: .PHONY: avocado avocado: avocado-assets $(VENV) - $(AVOCADO) run avocado + PYTHONPATH=. $(AVOCADO) run avocado .PHONY: avocado-meta avocado-meta: avocado-assets $(VENV) - $(AVOCADO) run $(PLUGIN)/selftests.py + PYTHONPATH=. $(AVOCADO) run $(PLUGIN)/selftests.py tasst flake8: $(VENV) $(VENV)/bin/flake8 $(PYPKGS) @@ -251,6 +252,7 @@ pylint: $(VENV) .PHONY: check check: avocado check-legacy +.PHONY: clean clean: clean-legacy $(RM) -r $(VENV) find -name *~ | xargs $(RM) diff --git a/test/tasst/__init__.py b/test/tasst/__init__.py new file mode 100644 index 00000000..c1d5d9dd --- /dev/null +++ b/test/tasst/__init__.py @@ -0,0 +1,11 @@ +#! /usr/bin/env python3 + +# SPDX-License-Identifier: GPL-2.0-or-later +# +# Copyright Red Hat +# Author: David Gibson + +""" +Test A Simple Socket Transport +library of test helpers for passt & pasta +""" diff --git a/test/tasst/typecheck.py b/test/tasst/typecheck.py new file mode 100644 index 00000000..f97fc401 --- /dev/null +++ b/test/tasst/typecheck.py @@ -0,0 +1,47 @@ +#! /usr/bin/env avocado-runner-avocado-classless + +# SPDX-License-Identifier: GPL-2.0-or-later +# +# Copyright Red Hat +# Author: David Gibson + +""" +Test A Simple Socket Transport + +Type checking and related helpers +""" + +from avocado_classless.test import test, assert_eq, assert_raises + + +def typecheck(val, ty_): + """typecheck(val, ty_) + + Return val, raising TypeError if it does not have type ty_. + """ + if not isinstance(val, ty_): + raise TypeError(f'Expected {ty_} instead of {type(val)}') + return val + + +@test +def test_typecheck(): + assert_eq(typecheck(17, int), 17) + assert_eq(typecheck("hello", str), "hello") + assert_raises(TypeError, typecheck, 17, str) + + +def typecheck_default(val, ty_, default): + """typecheck_default(val, ty_, default) + + If val is None, return default. Otherwise return typecheck(val, ty_). + """ + if val is None: + return default + return typecheck(val, ty_) + + +@test +def test_typecheck_default(): + assert_eq(typecheck_default(17, int, None), 17) + assert_eq(typecheck_default(None, int, 17), 17) -- 2.41.0