Created
September 9, 2017 11:48
-
-
Save apahim/71d69e549df669db1203350453095359 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From 731c6331a382da673de9cba48091e19366706ab6 Mon Sep 17 00:00:00 2001 | |
From: Amador Pahim <[email protected]> | |
Date: Fri, 8 Sep 2017 19:01:29 +0200 | |
Subject: [PATCH] Combinatorial | |
Signed-off-by: Amador Pahim <[email protected]> | |
--- | |
avocado/core/combinatorial.py | 51 ++++++++++++++++++++++++++++++++++++++++ | |
avocado/core/job.py | 7 ++++++ | |
avocado/plugins/combinatorial.py | 45 +++++++++++++++++++++++++++++++++++ | |
setup.py | 1 + | |
4 files changed, 104 insertions(+) | |
create mode 100644 avocado/core/combinatorial.py | |
create mode 100644 avocado/plugins/combinatorial.py | |
diff --git a/avocado/core/combinatorial.py b/avocado/core/combinatorial.py | |
new file mode 100644 | |
index 00000000..b95f92c3 | |
--- /dev/null | |
+++ b/avocado/core/combinatorial.py | |
@@ -0,0 +1,51 @@ | |
+import json | |
+ | |
+from avocado.core import varianter | |
+ | |
+class Nwise(object): | |
+ | |
+ def __init__(self, variants, combined_elements=2): | |
+ """ | |
+ :param combinations: List of Avocado Variants | |
+ :param combined_elements: N-wise number of elements to be | |
+ combined together | |
+ """ | |
+ self.variants = variants | |
+ self.combined_elements = combined_elements | |
+ | |
+ def outcome(self): | |
+ | |
+ # Serializing the variants object and iterating the | |
+ # serialized resulting object | |
+ serialized_variants = self.variants.dump() | |
+ | |
+ # Printing the key/value of each combination (aka each variant) | |
+ print('=================') | |
+ for variant in serialized_variants: | |
+ print('COMBINATION: %s' % variant['variant_id']) | |
+ for node in variant['variant']: | |
+ for item in node[1]: | |
+ print(' KEY: %s, VALUE: %s' % (item[1] ,item[2])) | |
+ print '=================' | |
+ | |
+ # This is an example on how to loop all the combinations, test | |
+ # some condition and include that combination in a resulting | |
+ # list of combinations. | |
+ filtered_variants = [] | |
+ for variant in serialized_variants: | |
+ include_variant = False | |
+ | |
+ for node in variant['variant']: | |
+ for item in node[1]: | |
+ | |
+ # Fake test to check whether the variant should be | |
+ # included or not. | |
+ if item[1] == 'step' and item[2] is 5: | |
+ include_variant = True | |
+ | |
+ if include_variant: | |
+ filtered_variants.append(variant) | |
+ | |
+ # Creating a variants object out of the resulting list of | |
+ # combinations | |
+ return varianter.Varianter(state=filtered_variants) | |
diff --git a/avocado/core/job.py b/avocado/core/job.py | |
index d3edf249..7f97df2d 100644 | |
--- a/avocado/core/job.py | |
+++ b/avocado/core/job.py | |
@@ -18,6 +18,7 @@ Job module - describes a sequence of automated test operations. | |
""" | |
import argparse | |
+import json | |
import logging | |
import os | |
import re | |
@@ -27,6 +28,7 @@ import tempfile | |
import time | |
import traceback | |
+from . import combinatorial | |
from . import version | |
from . import data_dir | |
from . import dispatcher | |
@@ -433,6 +435,11 @@ class Job(object): | |
raise exceptions.OptionValidationError("Unable to parse " | |
"variant: %s" % details) | |
+ combined_elements = getattr(self.args, "combinatorial", None) | |
+ if combined_elements is not None: | |
+ nwise = combinatorial.Nwise(variant, combined_elements) | |
+ variant = nwise.outcome() | |
+ | |
self._make_test_runner() | |
self._start_sysinfo() | |
diff --git a/avocado/plugins/combinatorial.py b/avocado/plugins/combinatorial.py | |
new file mode 100644 | |
index 00000000..4e596ab7 | |
--- /dev/null | |
+++ b/avocado/plugins/combinatorial.py | |
@@ -0,0 +1,45 @@ | |
+# This program is free software; you can redistribute it and/or modify | |
+# it under the terms of the GNU General Public License as published by | |
+# the Free Software Foundation; either version 2 of the License, or | |
+# (at your option) any later version. | |
+# | |
+# This program is distributed in the hope that it will be useful, | |
+# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
+# | |
+# See LICENSE for more details. | |
+# | |
+# Copyright: Red Hat Inc. 2017 | |
+# Authors: Amador Pahim <[email protected]> | |
+ | |
+""" | |
+Avocado Plugin to create N-Wise Combinatorial Variants | |
+""" | |
+ | |
+ | |
+from avocado.core.plugin_interfaces import CLI | |
+ | |
+ | |
+class CombinatorialCLI(CLI): | |
+ | |
+ """ | |
+ Enable/Configure Combinatorial generation of Variants | |
+ """ | |
+ | |
+ name = 'combinatorial' | |
+ description = "Combinatorial options for 'run' subcommand" | |
+ | |
+ def configure(self, parser): | |
+ run_subcommand_parser = parser.subcommands.choices.get('run', None) | |
+ if run_subcommand_parser is None: | |
+ return | |
+ | |
+ msg = 'combinatorial options' | |
+ parser = run_subcommand_parser.add_argument_group(msg) | |
+ parser.add_argument('--combinatorial', | |
+ dest='combinatorial', default=None, | |
+ help='Specify the number of combined elements') | |
+ self.configured = True | |
+ | |
+ def run(self, args): | |
+ pass | |
diff --git a/setup.py b/setup.py | |
index 875000c3..e58c2e45 100755 | |
--- a/setup.py | |
+++ b/setup.py | |
@@ -167,6 +167,7 @@ if __name__ == '__main__': | |
'replay = avocado.plugins.replay:Replay', | |
'tap = avocado.plugins.tap:TAP', | |
'zip_archive = avocado.plugins.archive:ArchiveCLI', | |
+ 'combinatorial = avocado.plugins.combinatorial:CombinatorialCLI', | |
], | |
'avocado.plugins.cli.cmd': [ | |
'config = avocado.plugins.config:Config', | |
-- | |
2.13.5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment