Package: avocado-framework-plugin-varianter-yaml-to-mux
avocado_varianter_yaml_to_muxThis plugin utilizes themultiplexationmechanism to
produce variants out of a yaml file. This section is example-based,
if you are interested in test parameters and/ormultiplexationoverview, please take a look attest-parameter.As mentioned earlier, it inherits from theavocado_varianter_yaml_to_mux.mux.MuxPluginand the only thing it implements is the argument parsing
to get some input and a customyamlparser (which is also capable of parsingjson).The YAML file is perfect for this task as it’s easily read by
both, humans and machines.  Let’s start with an example (line
numbers at the first columns are for documentation purposes only,
they are not part of the multiplex file format):1  hw:2      cpu:!mux3          intel:4              cpu_CFLAGS:'-march=core2'5          amd:6              cpu_CFLAGS:'-march=athlon64'7          arm:8              cpu_CFLAGS:'-mabi=apcs-gnu-march=armv8-a-mtune=arm8'9      disk:!mux10          scsi:11              disk_type:'scsi'12          virtio:13              disk_type:'virtio'14  distro:!mux15      fedora:16          init:'systemd'17      mint:18          init:'systemv'19  env:!mux20      debug:21          opt_CFLAGS:'-O0-g'22      prod:23          opt_CFLAGS:'-O2'WarningOn some architectures misbehaving versions of CYaml
Python library were reported and Avocado always fails withunacceptable character #x0000: control characters are not allowed. To workaround this issue you need to either update
the PyYaml to the version which works properly, or you need
to remove thepython2.7/site-packages/yaml/cyaml.pyor
disable CYaml import in Avocado sources. For details check
out theGithub issueThere are couple ofkey=>valuepairs (lines 4,6,8,11,13,…) and there are
named nodes which define scope (lines 1,2,3,5,7,9,…). There are also additional
flags (lines 2, 9, 14, 19) which modifies the behavior.NodesThey define context of thekey=>valuepairs allowing us to easily identify
for what this values might be used for and also it makes possible to define
multiple values of the same keys with different scope.Due to their purpose the YAML automatic type conversion for nodes names
is disabled, so the value of node name is always as written in the YAML
file (unlike values, whereyesconverts toTrueand such).Nodes are organized in parent-child relationship and together they create
a tree. To view this structure useavocado variants--tree-m<file>:┗━━ run
     ┣━━ hw
     ┃    ┣━━ cpu
     ┃    ┃    ╠══ intel
     ┃    ┃    ╠══ amd
     ┃    ┃    ╚══ arm
     ┃    ┗━━ disk
     ┃         ╠══ scsi
     ┃         ╚══ virtio
     ┣━━ distro
     ┃    ╠══ fedora
     ┃    ╚══ mint
     ┗━━ env
          ╠══ debug
          ╚══ prodYou can see thathwhas 2 childrencpuanddisk. All parameters
defined in parent node are inherited to children and extended/overwritten by
their values up to the leaf nodes. The leaf nodes (intel,amd,arm,scsi, …) are the most important as after multiplexation they form the
parameters available in tests.Keys and ValuesEvery value other than dict (4,6,8,11) is used as value of the antecedent
node.Each node can define key/value pairs (lines 4,6,8,11,…). Additionally
each children node inherits values of it’s parent and the result is called
nodeenvironment.Given the node structure below:devtools:compiler:'cc'flags:-'-O2'debug:'-g'fedora:compiler:'gcc'flags:-'-Wall'osx:compiler:'clang'flags:-'-archi386'-'-archx86_64'And the rules defined as:Scalar values (Booleans, Numbers and Strings) are overwritten by walking from the root until the final node.Lists are appended (to the tail) whenever we walk from the root to the final node.The environment created for the nodesfedoraandosxare:Node//devtools/fedoraenvironmentcompiler: 'gcc',flags:['-O2','-Wall']Node//devtools/osxenvironmentcompiler: 'clang',flags:['-O2','-archi386','-archx86_64']Note that due to different usage of key and values in environment we disabled
the automatic value conversion for keys while keeping it enabled for values.
This means that the key is always a string and the value can be YAML value,
eg. bool, list, custom type, or string. Please be aware that due to limitation
None type can be provided in yaml specifically as string ‘null’.VariantsIn the end all leaves are gathered and turned into parameters, more specifically intoAvocadoParams:setup:graphic:user:"guest"password:"pass"text:user:"root"password:"123456"produces[graphic, text]. In the test code you’ll be able to query only
those leaves. Intermediary or root nodes are available.The example above generates a single test execution with parameters separated
by path. But the most powerful multiplexer feature is that it can generate
multiple variants. To do that you need to tag a node whose children are
meant to be multiplexed. Effectively it returns only leaves of one child at the
time.In order to generate all possible variants multiplexer creates cartesian
product of all of these variants:cpu:!muxintel:amd:arm:fmt:!muxqcow2:raw:Produces 6 variants:/cpu/intel, /fmt/qcow2
/cpu/intel, /fmt/raw
...
/cpu/arm, /fmt/rawThe !mux evaluation is recursive so one variant can expand to multiple
ones:fmt:!muxqcow:!mux2:2v3:raw:Results in:/fmt/qcow2/2
/fmt/qcow2/2v3
/rawResolution orderYou can see that only leaves are part of the test parameters. It might happen
that some of these leaves contain different values of the same key. Then
you need to make sure your queries separate them by different paths. When
the path matches multiple results with different origin, an exception is raised
as it’s impossible to guess which key was originally intended.To avoid these problems it’s recommended to use unique names in test parameters if
possible, to avoid the mentioned clashes. It also makes it easier to extend or mix
multiple YAML files for a test.For multiplex YAML files that are part of a framework, contain default
configurations, or serve as plugin configurations and other advanced setups it is
possible and commonly desirable to use non-unique names. But always keep those points
in mind and provide sensible paths.Multiplexer also supports default paths. By default it’s/run/*but it can
be overridden by--mux-path, which accepts multiple arguments. What it does
it splits leaves by the provided paths. Each query goes one by one through
those sub-trees and first one to hit the match returns the result. It might not
solve all problems, but it can help to combine existing YAML files with your
ones:qa:# large and complex read-only file, content injected into /qatests:timeout:10...my_variants:!mux# your YAML file injected into /my_variantsshort:timeout:1long:timeout:1000You want to use an existing test which usesparams.get('timeout','*').  Then you
can use--mux-path'/my_variants/*''/qa/*'and it’ll first look in your
variants. If no matches are found, then it would proceed to/qa/*Keep in mind that only slices defined in mux-path are taken into account for
relative paths (the ones starting with*)Injecting filesYou can run any test with any YAML file by:avocado run sleeptest.py --mux-yaml file.yamlThis puts the content offile.yamlinto/runlocation, which as mentioned in previous section, is the defaultmux-pathpath. For most simple cases this is the expected behavior as your files
are available in the default path and you can safely useparams.get(key).When you need to put a file into a different location, for example
when you have two files and you don’t want the content to be merged into
a single place becoming effectively a single blob, you can do that by
giving a name to your YAML file:avocado run sleeptest.py --mux-yaml duration:duration.yamlThe content ofduration.yamlis injected into/run/duration. Still when
keys from other files don’t clash, you can useparams.get(key)and retrieve
from this location as it’s in the default path, only extended by thedurationintermediary node. Another benefit is you can merge or separate
multiple files by using the same or different name, or even a complex
(relative) path.Last but not least, advanced users can inject the file into whatever location
they prefer by:avocado run sleeptest.py --mux-yaml /my/variants/duration:duration.yamlSimpleparams.get(key)won’t look in this location, which might be the
intention of the test writer. There are several ways to access the values:absolute locationparams.get(key, '/my/variants/duration')absolute location with wildcardsparams.get(key,'/my/*)(or/*/duration/*…)set the mux-pathavocado run ...--mux-path/my/*and use relative pathIt’s recommended to use the simple injection for single YAML files, relative
injection for multiple simple YAML files and the last option is for very
advanced setups when you either can’t modify the YAML files and you need to
specify custom resolution order or you are specifying non-test parameters, for
example parameters for your plugin, which you need to separate from the test
parameters.Special valuesAs you might have noticed, we are using mapping/dicts to define the structure
of the params. To avoid surprises we disallowed the smart typing of mapping
keys so:on:onWon’t becomeTrue: True, but the key will be preserved as stringon: True.You might also want to use dict as values in your params. This is also
supported but as we can’t easily distinguish whether that value is
a value or a node (structure), you have to either embed it into another
object (list, ..) or you have to clearly state the type (yaml tag!!python/dict). Even then the value won’t be a standard dictionary,
but it’ll becollections.OrderedDictand similarly to nodes
structure all keys are preserved as strings and no smart type detection
is used. Apart from that it should behave similarly as dict, only you
get the values ordered by the order they appear in the file.Multiple filesYou can provide multiple files. In such scenario final tree is a combination
of the provided files where later nodes with the same name override values of
the preceding corresponding node. New nodes are appended as new children:file-1.yaml:debug:CFLAGS:'-O0-g'prod:CFLAGS:'-O2'file-2.yaml:prod:CFLAGS:'-Os'fast:CFLAGS:'-Ofast'results in:debug:CFLAGS:'-O0-g'prod:CFLAGS:'-Os'# overriddenfast:CFLAGS:'-Ofast'# appendedIt’s also possible to include existing file into another a given node in another
file. This is done by the!include : $pathdirective:os:fedora:!include:fedora.yamlgentoo:!include:gentoo.yamlWarningDue to YAML nature, it’smandatoryto put space between!includeand the colon (:) that must follow it.The file location can be either absolute path or relative path to the YAML
file where the!includeis called (even when it’s nested).Whole file ismergedinto the node where it’s defined.Advanced YAML tagsThere are additional features related to YAML files. Most of them require values
separated by":". Again, in all such cases it’s mandatory to add a white space
(" ") between the tag and the":", otherwise":"is part of the tag
name and the parsing fails.!includeIncludes other file and injects it into the node it’s specified in:my_other_file:!include:other.yamlThe content of/my_other_filewould be parsed from theother.yaml. It’s
the hardcoded equivalent of the-m$using:$path.Relative paths start from the original file’s directory.!usingPrepends path to the node it’s defined in:!using:/foobar:!using:bazbaris put intobazbecoming/baz/barand everything is put into/foo. So the final path ofbaris/foo/baz/bar.!remove_nodeRemoves node if it existed during the merge. It can be used to extend
incompatible YAML files:os:fedora:windows:3.11:95:os:!remove_node:windowswindows:win3.11:win95:Removes thewindowsnode from structure. It’s different fromfilter-outas it really removes the node (and all children) from the tree and
it can be replaced by you new structure as shown in the example. It removeswindowswith all children and then replaces this structure with slightly
modified version.As!remove_nodeis processed during merge, when you reverse the order,
windows is not removed and you end-up with/windows/{win3.11,win95,3.11,95}nodes.!remove_valueIt’s similar to!remove_nodeonly with values.!muxChildren of this node will be multiplexed. This means that in first variant
it’ll return leaves of the first child, in second the leaves of the second
child, etc. Example is in sectionVariants!filter-onlyDefines internal filters. They are inherited by children and evaluated
during multiplexation. It allows one to specify the only compatible branch
of the tree with the current variant, for example:cpu:
    arm:
        !filter-only : /disk/virtio
disk:
    virtio:
    scsi:will skip the[arm, scsi]variant and result only in[arm, virtio]_Note: It’s possible to use!filter-onlymultiple times with the same
parent and all allowed variants will be included (unless they are
filtered-out by!filter-out)__Note2: The evaluation order is 1. filter-out, 2. filter-only. This means when
you booth filter-out and filter-only a branch it won’t take part in the
multiplexed variants._!filter-outSimilarly to!filter-onlyonly it skips the specified branches and leaves
the remaining ones. (in the same example the use of!filter-out: /disk/scsiresults in the same behavior). The difference
is when a new disk type is introduced,!filter-onlystill allows just
the specified variants, while!filter-outonly removes the specified
ones.As for the speed optimization, currently Avocado is strongly optimized
towards fast!filter-outso it’s highly recommended using them
rather than!filter-only, which takes significantly longer to
process.Complete exampleLet’s take a second look at the first example:1    hw:
 2        cpu: !mux
 3            intel:
 4                cpu_CFLAGS: '-march=core2'
 5            amd:
 6                cpu_CFLAGS: '-march=athlon64'
 7            arm:
 8                cpu_CFLAGS: '-mabi=apcs-gnu -march=armv8-a -mtune=arm8'
 9        disk: !mux
10            scsi:
11                disk_type: 'scsi'
12            virtio:
13                disk_type: 'virtio'
14    distro: !mux
15        fedora:
16            init: 'systemd'
17        mint:
18            init: 'systemv'
19    env: !mux
20        debug:
21            opt_CFLAGS: '-O0 -g'
22        prod:
23            opt_CFLAGS: '-O2'After filters are applied (simply removes non-matching variants), leaves
are gathered and all variants are generated:$ avocado variants -m selftests/.data/mux-environment.yaml
Variants generated:
Variant 1:    /hw/cpu/intel, /hw/disk/scsi, /distro/fedora, /env/debug
Variant 2:    /hw/cpu/intel, /hw/disk/scsi, /distro/fedora, /env/prod
Variant 3:    /hw/cpu/intel, /hw/disk/scsi, /distro/mint, /env/debug
Variant 4:    /hw/cpu/intel, /hw/disk/scsi, /distro/mint, /env/prod
Variant 5:    /hw/cpu/intel, /hw/disk/virtio, /distro/fedora, /env/debug
Variant 6:    /hw/cpu/intel, /hw/disk/virtio, /distro/fedora, /env/prod
Variant 7:    /hw/cpu/intel, /hw/disk/virtio, /distro/mint, /env/debug
Variant 8:    /hw/cpu/intel, /hw/disk/virtio, /distro/mint, /env/prod
Variant 9:    /hw/cpu/amd, /hw/disk/scsi, /distro/fedora, /env/debug
Variant 10:    /hw/cpu/amd, /hw/disk/scsi, /distro/fedora, /env/prod
Variant 11:    /hw/cpu/amd, /hw/disk/scsi, /distro/mint, /env/debug
Variant 12:    /hw/cpu/amd, /hw/disk/scsi, /distro/mint, /env/prod
Variant 13:    /hw/cpu/amd, /hw/disk/virtio, /distro/fedora, /env/debug
Variant 14:    /hw/cpu/amd, /hw/disk/virtio, /distro/fedora, /env/prod
Variant 15:    /hw/cpu/amd, /hw/disk/virtio, /distro/mint, /env/debug
Variant 16:    /hw/cpu/amd, /hw/disk/virtio, /distro/mint, /env/prod
Variant 17:    /hw/cpu/arm, /hw/disk/scsi, /distro/fedora, /env/debug
Variant 18:    /hw/cpu/arm, /hw/disk/scsi, /distro/fedora, /env/prod
Variant 19:    /hw/cpu/arm, /hw/disk/scsi, /distro/mint, /env/debug
Variant 20:    /hw/cpu/arm, /hw/disk/scsi, /distro/mint, /env/prod
Variant 21:    /hw/cpu/arm, /hw/disk/virtio, /distro/fedora, /env/debug
Variant 22:    /hw/cpu/arm, /hw/disk/virtio, /distro/fedora, /env/prod
Variant 23:    /hw/cpu/arm, /hw/disk/virtio, /distro/mint, /env/debug
Variant 24:    /hw/cpu/arm, /hw/disk/virtio, /distro/mint, /env/prodWhere the first variant contains:/hw/cpu/intel/  => cpu_CFLAGS: -march=core2
/hw/disk/       => disk_type: scsi
/distro/fedora/ => init: systemd
/env/debug/     => opt_CFLAGS: -O0 -gThe second one:/hw/cpu/intel/  => cpu_CFLAGS: -march=core2
/hw/disk/       => disk_type: scsi
/distro/fedora/ => init: systemd
/env/prod/      => opt_CFLAGS: -O2From this example you can see that querying for/env/debugworks only in
the first variant, but returns nothing in the second variant. Keep this in mind
and when you use the!muxflag always query for the pre-mux path,/env/*in this example.Injecting valuesBeyond the values injected by YAML files specified it’s also possible
inject values directly from command line to the final multiplex tree.
It’s done by the argument--mux-inject. The format of expected
value is[path:]key:node_value.WarningWhen no path is specified to--mux-injectthe parameter
is added under tree root/. For example: running avocado passing--mux-injectmy_key:my_valuethe parameter can be accessed callingself.params.get('my_key'). If the test writer wants to put the injected
value in any other path location, like extending the/runpath, it needs
to be informed on avocado run call.  For example:--mux-inject/run/:my_key:my_valuemakes possible to access the parameters
callingself.params.get('my_key','/run')A test that gets parameters without a defined path, such asexamples/tests/multiplextest.py:os_type = self.params.get('os_type', default='linux')Running it:$ avocado --show=test run -- examples/tests/multiplextest.py  | grep os_type
PARAMS (key=os_type, path=*, default=linux) => 'linux'Now, injecting a value, by default will put it in /, which is not in the
default list of paths searched for:$ avocado --show=test run --mux-inject os_type:myos -- examples/tests/multiplextest.py  | grep os_type
PARAMS (key=os_type, path=*, default=linux) => 'linux'A path that is searched for by default is /run. To set the value to that path use:$ avocado --show=test run --mux-inject /run:os_type:myos -- examples/tests/multiplextest.py  | grep os_type
PARAMS (key=os_type, path=*, default=linux) => 'myos'Or, add the / to the list of paths searched for by default:$ avocado --show=test run --mux-inject os_type:myos --mux-path / -- examples/tests/multiplextest.py  | grep os_type
PARAMS (key=os_type, path=*, default=linux) => 'myos'WarningBy default, the values are parsed for the respective data types.
When not possible, it falls back to string. If you want to maintain some
value as string, enclose within quotes, properly escaped, and eclose that
again in quotes.
For example: a value of1is treated as integer, a value of1,2is
treated as list, a value ofabcis treated as string, a value of1,2,5-10is treated as list of integers as1,2,-5. If you want to
maintain this as string, provide the value as"\"1,2,5-10\""Multiplexeravocado_varianter_yaml_to_mux.muxMultiplexeror simplyMuxis an abstract concept, which was
the basic idea behind the tree-like params structure with the support
to produce all possible variants. There is a core implementation of
basic building blocks that can be used when creating a custom plugin.
There is a demonstration version of plugin using this concept inavocado_varianter_yaml_to_muxwhich adds a parser and then
uses this multiplexer concept to define an Avocado plugin to produce
variants fromyaml(orjson) files.Multiplexer conceptAs mentioned earlier, this is an in-core implementation of building
blocks intended for writingvarianter-pluginsbased on a tree
withMultiplex domainsdefined. The available blocks are:MuxTree- Object which represents a part of the tree and handles
the multiplexation, which means producing all possible variants
from a tree-like object.MuxPlugin- Base class to buildvarianter-pluginsMuxTreeNode- Inherits fromtree-nodeand adds the support for
control flags (MuxTreeNode.ctrl) and multiplex domains
(MuxTreeNode.multiplex).And some support classes and methods eg. for filtering and so on.Multiplex domainsA defaultavocado-paramstree with variables could look like this:Multiplex tree representation:
 ┣━━ paths
 ┃     → tmp: /var/tmp
 ┃     → qemu: /usr/libexec/qemu-kvm
 ┗━━ environ
     → debug: FalseThe multiplexer wants to produce similar structure, but also to be able
to define not just one variant, but to define all possible combinations
and then report the slices as variants. We use the termMultiplex domainsto define that children of this node are not just
different paths, but they are different values and we only want one at
a time. In the representation we use double-line to visibly distinguish
between normal relation and multiplexed relation. Let’s modify our
example a bit:Multiplex tree representation:
 ┣━━ paths
 ┃     → tmp: /var/tmp
 ┃     → qemu: /usr/libexec/qemu-kvm
 ┗━━ environ
      ╠══ production
      ║     → debug: False
      ╚══ debug
            → debug: TrueThe difference is thatenvironis now amultiplexnode and it’s
children will be yielded one at a time producing two variants:Variant 1:
 ┣━━ paths
 ┃     → tmp: /var/tmp
 ┃     → qemu: /usr/libexec/qemu-kvm
 ┗━━ environ
      ┗━━ production
            → debug: False
Variant 2:
 ┣━━ paths
 ┃     → tmp: /var/tmp
 ┃     → qemu: /usr/libexec/qemu-kvm
 ┗━━ environ
      ┗━━ debug
            → debug: FalseNote that themultiplexis only about direct children, therefore
the number of leaves in variants might differ:Multiplex tree representation:
 ┣━━ paths
 ┃     → tmp: /var/tmp
 ┃     → qemu: /usr/libexec/qemu-kvm
 ┗━━ environ
      ╠══ production
      ║     → debug: False
      ╚══ debug
           ┣━━ system
           ┃     → debug: False
           ┗━━ program
                 → debug: TrueProduces one variant with/pathsand/environ/productionand
other variant with/paths,/environ/debug/systemand/environ/debug/program.As mentioned earlier the power is not in producing one variant, but
in defining huge scenarios with all possible variants. By using
tree-structure with multiplex domains you can avoid most of the
ugly filters you might know from Jenkins sparse matrix jobs.
For comparison let’s have a look at the same example in Avocado:Multiplex tree representation:
 ┗━━ os
      ┣━━ distro
      ┃    ┗━━ redhat
      ┃         ╠══ fedora
      ┃         ║    ┣━━ version
      ┃         ║    ┃    ╠══ 20
      ┃         ║    ┃    ╚══ 21
      ┃         ║    ┗━━ flavor
      ┃         ║         ╠══ workstation
      ┃         ║         ╚══ cloud
      ┃         ╚══ rhel
      ┃              ╠══ 5
      ┃              ╚══ 6
      ┗━━ arch
           ╠══ i386
           ╚══ x86_64Which produces:Variant 1:    /os/distro/redhat/fedora/version/20, /os/distro/redhat/fedora/flavor/workstation, /os/arch/i386
Variant 2:    /os/distro/redhat/fedora/version/20, /os/distro/redhat/fedora/flavor/workstation, /os/arch/x86_64
Variant 3:    /os/distro/redhat/fedora/version/20, /os/distro/redhat/fedora/flavor/cloud, /os/arch/i386
Variant 4:    /os/distro/redhat/fedora/version/20, /os/distro/redhat/fedora/flavor/cloud, /os/arch/x86_64
Variant 5:    /os/distro/redhat/fedora/version/21, /os/distro/redhat/fedora/flavor/workstation, /os/arch/i386
Variant 6:    /os/distro/redhat/fedora/version/21, /os/distro/redhat/fedora/flavor/workstation, /os/arch/x86_64
Variant 7:    /os/distro/redhat/fedora/version/21, /os/distro/redhat/fedora/flavor/cloud, /os/arch/i386
Variant 8:    /os/distro/redhat/fedora/version/21, /os/distro/redhat/fedora/flavor/cloud, /os/arch/x86_64
Variant 9:    /os/distro/redhat/rhel/5, /os/arch/i386
Variant 10:    /os/distro/redhat/rhel/5, /os/arch/x86_64
Variant 11:    /os/distro/redhat/rhel/6, /os/arch/i386
Variant 12:    /os/distro/redhat/rhel/6, /os/arch/x86_64Versus Jenkins sparse matrix:os_version = fedora20 fedora21 rhel5 rhel6
os_flavor = none workstation cloud
arch = i386 x86_64

filter = ((os_version == "rhel5").implies(os_flavor == "none") &&
          (os_version == "rhel6").implies(os_flavor == "none")) &&
         !(os_version == "fedora20" && os_flavor == "none") &&
         !(os_version == "fedora21" && os_flavor == "none")Which is still relatively simple example, but it grows dramatically with
inner-dependencies.MuxPluginavocado_varianter_yaml_to_mux.mux.MuxPluginDefines the full interface required byavocado.core.plugin_interfaces.Varianter. The plugin writer
should inherit from thisMuxPlugin, then from theVarianterand call the:self.initialize_mux(root, paths, debug)Where:root - is the root of your params tree (compound oftree-node-like
nodes)paths - is theparameter-pathsto be used in test with all variantsdebug - whether to use debug mode (requires the passed tree to be
compound ofTreeNodeDebug-like nodes which stores the origin
of the variant/value/environment as the value for listing purposes
and is __NOT__ intended for test execution.This method must be called before thevarianter’s second
stage. TheMuxPlugin’s code will take care of the rest.MuxTreeThis is the core feature where the hard work happens. It walks the tree
and remembers all leaf nodes or uses list ofMuxTreeswhen another
multiplex domain is reached while searching for a leaf.When it’s asked to report variants, it combines one variant of each
remembered item (leaf node always stays the same, butMuxTreecircles
through it’s values) which recursively produces all possible variants
of differentmultiplex domains.