[elbe-devel] [PATCH 2/6] Creation of "elbepack/junit.py".

Torben Hohn torben.hohn at linutronix.de
Wed Aug 7 11:03:47 CEST 2019


On Thu, Jul 18, 2019 at 02:02:42PM +0200, dion at linutronix.de wrote:
> From: Olivier Dion <dion at linutronix.de>
> 
> This is a wrapper around the 'junit_xml' package.
> 
> * TestSuite
> 
>   Wrapper around 'junit_xml.TestSuite'.
> 
> ** __init__
> 
>   A TestSuite needs a node and a target filesystem.
> 
> ** __call__
> 
>    Calling the TestSuite will automatically iterate over its children
>    nodes, execute them and return a 'junit_xml.TestSuite' object.
> 
> ** Other methods
> 
>    Other methods are private and not meant to be use.
> 
> * BaseTest
> 
>   This is the base class that all tests should inherit from.  All
>   daugther classes from BaseTest should overload the __call__ method.
>   The __call__ method should return a 'junit_xml.TestCase' object.
> 
>   It's also important to register the daugther classes to the
>   TestSuite class.  The registration's name should reflect the actual
>   <test-case> option's name.
> 
>   Finally, classes that inherit from BaseTest have access to the
>   self.node, self.target and self.tag attributes.
> 
> * Example
> 
>   This is an hypotical test that would execute a shell script on the
>   filesystem and assert that the return exit code value of the script
>   is 0.
> 
>   For the sake of the example, let's assume that the tag's name for
>   the <test-case> choice is <script>.
> 
>   --------------------------------------------------------------------
>   @TestSuite.register("script")
>   class TestScript(BaseTest):
> 
>       def __call__(self):
>           path = self.node.et.text
> 	  test = junit.TestCase(name=path, classname=self.tag)
>           with self.target:
> 	      p = Popen(path, stdout=PIPE, stderr=STDOUT, shell=True)
> 	      out, _ = p.communicate()
> 	      if p.returncode:
>                   msg = "[%s] - %s" % (errno.errocode[p.returncode],
> 		                       os.strerror(p.returncode))
> 		  test.add_failure_info(message=msg, output=out)
>           return test
>   --------------------------------------------------------------------
> 
> Signed-off-by: Olivier Dion <dion at linutronix.de>

Reviewed-by: Torben Hohn <torben.hohn at linutronix.de>

see comments anyways...

> ---
>  elbepack/junit.py | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 76 insertions(+)
>  create mode 100644 elbepack/junit.py
> 
> diff --git a/elbepack/junit.py b/elbepack/junit.py
> new file mode 100644
> index 00000000..ab759332
> --- /dev/null
> +++ b/elbepack/junit.py
> @@ -0,0 +1,76 @@
> +# ELBE - Debian Based Embedded Rootfilesystem Builder
> +# Copyright (c) 2019 Olivier Dion <dion at linutronix.de>
> +#
> +# SPDX-License-Identifier: GPL-3.0-or-later
> +
> +
> +import junit_xml as junit
> +
> +
> +class TestException(Exception):
> +    pass
> +
> +
> +class TestSuite(object):
> +
> +    test_dict = {}
> +
> +    def __init__(self, node, target):
> +        super(TestSuite, self).__init__()
> +        self.node = node
> +        self.target = target
> +
> +    @staticmethod
> +    def to_file(output, tss):
> +        with open(output, 'w') as f:
> +            junit.TestSuite.to_file(f, tss, prettyprint=True)
> +
> +    @classmethod
> +    def register(cls, tag, register=True):
> +        def _register(test):
> +            test.tag = tag
> +            if register is True:
> +                cls.test_dict[tag] = test
> +            return test
> +        return _register
> +
> +    def do_test(self, node, target):
> +        if node.tag not in self.test_dict:
> +            raise TestException("Invalid Test %s" % node.tag)
> +        test = self.test_dict[node.tag]
> +        return test(node, target)()
> +
> +    def __call__(self):
> +        test_cases = []
> +        for test in self.node:
> +            try:
> +                test_cases.append(self.do_test(test, self.target))
> +            except TestException as E:
> +                pass # TODO - Handle me!
> +        ts = junit.TestSuite(name=self.node.et.attrib["name"],
> +                             test_cases=test_cases)
> +        return ts
> +
> +
> +#pylint: disable=too-few-public-methods,no-member
> + at TestSuite.register("BaseTest", register=False)
> +class BaseTest(object):
> +
> +    def __init__(self, node, target):
> +        super(BaseTest, self).__init__()
> +        self.node = node
> +        self.target = target
> +
> +    def __call__(self):
> +        raise TestException("Unimplemented Test %s" % self.tag)
> +
> +
> + at TestSuite.register("file-exists")
> +class TestFileExists(BaseTest):

this checks, whether the target file exists.
i can imagine <buildimage-file-exists> and <project-file-exists>

validating, whether the images have been generated, etc.
maybe we should also add some checks for file sizes of dynamic
files like tar.gz or cpio.

hmm... but the target is probably the primary we are interested in...
so this is probably fine.

> +
> +    def __call__(self):
> +        path = self.node.et.text
> +        test = junit.TestCase(name=path, classname=self.tag)
> +        if not self.target.exists(path):
> +            test.add_failure_info(message="FAILED")
> +        return test
> -- 
> 2.11.0
> 
> 
> _______________________________________________
> elbe-devel mailing list
> elbe-devel at linutronix.de
> https://lists.linutronix.de/mailman/listinfo/elbe-devel

-- 
Torben Hohn
Linutronix GmbH | Bahnhofstrasse 3 | D-88690 Uhldingen-Mühlhofen
Phone: +49 7556 25 999 18; Fax.: +49 7556 25 999 99

Hinweise zum Datenschutz finden Sie hier (Informations on data privacy 
can be found here): https://linutronix.de/kontakt/Datenschutz.php

Linutronix GmbH | Firmensitz (Registered Office): Uhldingen-Mühlhofen | 
Registergericht (Registration Court): Amtsgericht Freiburg i.Br., HRB700 
806 | Geschäftsführer (Managing Directors): Heinz Egger, Thomas Gleixner



More information about the elbe-devel mailing list