From 3dae8a03fcdd1c1532a69f5366f9eef2e59de528 Mon Sep 17 00:00:00 2001 From: Jaime Soriano Pastor Date: Wed, 18 Mar 2020 13:00:27 +0100 Subject: [PATCH 1/4] Add support of TEST_TAGS in python tests Add a `tag(tag)` decorator that skips a test if the tag is not included in the comma-separated list of `TEST_TAGS` environment variable. This offers an initial support for the similar implementation added for mage in #16937. --- metricbeat/tests/system/metricbeat.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/metricbeat/tests/system/metricbeat.py b/metricbeat/tests/system/metricbeat.py index ed21ecfe3e28..75ad93b1cdad 100644 --- a/metricbeat/tests/system/metricbeat.py +++ b/metricbeat/tests/system/metricbeat.py @@ -1,6 +1,7 @@ +import os import re import sys -import os +import unittest import yaml sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../libbeat/tests/system'))) @@ -139,3 +140,26 @@ def parameterized_with_supported_versions(base_class): variants = supported_versions(versions_path) decorator = parameterized_class(['COMPOSE_ENV'], variants) decorator(base_class) + + +def tag(tag): + """ + Decorates a test function with a tag following go build tags semantics, + if the tag is not included in TEST_TAGS environment variable, the test is + skipped. + TEST_TAGS can be a comma-separated list of tags, e.g: TEST_TAGS=oracle,mssql. + """ + def decorator(func): + def wrapper(*args, **kwargs): + set_tags = [ + tag.strip() for tag in os.environ.get("TEST_TAGS", "").split(",") + if tag.strip() != "" + ] + if not tag in set_tags: + raise unittest.SkipTest("tag '{}' is not included in TEST_TAGS".format(tag)) + return func(*args, **kwargs) + wrapper.__name__ = func.__name__ + wrapper.__doc__ = func.__doc__ + return wrapper + + return decorator From 4c13f398ae8a582650b72725b75fbbdb6e8d9d8e Mon Sep 17 00:00:00 2001 From: Jaime Soriano Pastor Date: Wed, 18 Mar 2020 13:34:59 +0100 Subject: [PATCH 2/4] Move the code to libbeat --- libbeat/tests/system/beat/tags.py | 24 ++++++++++++++++++++++++ metricbeat/module/redis/test_redis.py | 1 + metricbeat/tests/system/metricbeat.py | 25 +------------------------ 3 files changed, 26 insertions(+), 24 deletions(-) create mode 100644 libbeat/tests/system/beat/tags.py diff --git a/libbeat/tests/system/beat/tags.py b/libbeat/tests/system/beat/tags.py new file mode 100644 index 000000000000..21b542420cf7 --- /dev/null +++ b/libbeat/tests/system/beat/tags.py @@ -0,0 +1,24 @@ +import os +import unittest + +def tag(tag): + """ + Decorates a test function with a tag following go build tags semantics, + if the tag is not included in TEST_TAGS environment variable, the test is + skipped. + TEST_TAGS can be a comma-separated list of tags, e.g: TEST_TAGS=oracle,mssql. + """ + def decorator(func): + def wrapper(*args, **kwargs): + set_tags = [ + tag.strip() for tag in os.environ.get("TEST_TAGS", "").split(",") + if tag.strip() != "" + ] + if not tag in set_tags: + raise unittest.SkipTest("tag '{}' is not included in TEST_TAGS".format(tag)) + return func(*args, **kwargs) + wrapper.__name__ = func.__name__ + wrapper.__doc__ = func.__doc__ + return wrapper + + return decorator diff --git a/metricbeat/module/redis/test_redis.py b/metricbeat/module/redis/test_redis.py index 98ffb982ca30..a5f52e2b31cf 100644 --- a/metricbeat/module/redis/test_redis.py +++ b/metricbeat/module/redis/test_redis.py @@ -29,6 +29,7 @@ class Test(metricbeat.BaseTest): COMPOSE_SERVICES = ['redis'] @unittest.skipUnless(metricbeat.INTEGRATION_TESTS, "integration test") + @metricbeat.tag('redis') @attr('integration') def test_info(self): """ diff --git a/metricbeat/tests/system/metricbeat.py b/metricbeat/tests/system/metricbeat.py index 75ad93b1cdad..b0a0a232b0c9 100644 --- a/metricbeat/tests/system/metricbeat.py +++ b/metricbeat/tests/system/metricbeat.py @@ -1,12 +1,12 @@ import os import re import sys -import unittest import yaml sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../libbeat/tests/system'))) from beat.beat import TestCase +from beat.tags import tag from parameterized import parameterized_class COMMON_FIELDS = ["@timestamp", "agent", "metricset.name", "metricset.host", @@ -140,26 +140,3 @@ def parameterized_with_supported_versions(base_class): variants = supported_versions(versions_path) decorator = parameterized_class(['COMPOSE_ENV'], variants) decorator(base_class) - - -def tag(tag): - """ - Decorates a test function with a tag following go build tags semantics, - if the tag is not included in TEST_TAGS environment variable, the test is - skipped. - TEST_TAGS can be a comma-separated list of tags, e.g: TEST_TAGS=oracle,mssql. - """ - def decorator(func): - def wrapper(*args, **kwargs): - set_tags = [ - tag.strip() for tag in os.environ.get("TEST_TAGS", "").split(",") - if tag.strip() != "" - ] - if not tag in set_tags: - raise unittest.SkipTest("tag '{}' is not included in TEST_TAGS".format(tag)) - return func(*args, **kwargs) - wrapper.__name__ = func.__name__ - wrapper.__doc__ = func.__doc__ - return wrapper - - return decorator From 4e67be34151ad5e2aad062b10c0e5668ab2ddec9 Mon Sep 17 00:00:00 2001 From: Jaime Soriano Pastor Date: Wed, 18 Mar 2020 14:58:56 +0100 Subject: [PATCH 3/4] Fix format --- libbeat/tests/system/beat/tags.py | 1 + 1 file changed, 1 insertion(+) diff --git a/libbeat/tests/system/beat/tags.py b/libbeat/tests/system/beat/tags.py index 21b542420cf7..6bcb33bffd52 100644 --- a/libbeat/tests/system/beat/tags.py +++ b/libbeat/tests/system/beat/tags.py @@ -1,6 +1,7 @@ import os import unittest + def tag(tag): """ Decorates a test function with a tag following go build tags semantics, From 553063231158b4e5ef50a02a6c06ec2a5060f5fd Mon Sep 17 00:00:00 2001 From: Jaime Soriano Pastor Date: Wed, 18 Mar 2020 20:16:21 +0100 Subject: [PATCH 4/4] Remove line added by mistake --- metricbeat/module/redis/test_redis.py | 1 - 1 file changed, 1 deletion(-) diff --git a/metricbeat/module/redis/test_redis.py b/metricbeat/module/redis/test_redis.py index a5f52e2b31cf..98ffb982ca30 100644 --- a/metricbeat/module/redis/test_redis.py +++ b/metricbeat/module/redis/test_redis.py @@ -29,7 +29,6 @@ class Test(metricbeat.BaseTest): COMPOSE_SERVICES = ['redis'] @unittest.skipUnless(metricbeat.INTEGRATION_TESTS, "integration test") - @metricbeat.tag('redis') @attr('integration') def test_info(self): """