Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFE] provide way to output machine parseable information about tests #2056

Open
pemensik opened this issue May 5, 2023 · 5 comments
Open

Comments

@pemensik
Copy link
Contributor

pemensik commented May 5, 2023

I am looking for a way to make tmt test show . to print information about test in current directory. I would like to have machine parseable values of selected fields.

The most interesting fields for me are require: and recommend: fields. Unfortunately processing current list of require packages from shell is difficult. Could be added some parameter to output those parameter in machine processable name? For packages list ideal would be just space-separated list of required packages. Without decorations like "and" between packages. Always printed the same way, regardless form used in fmf file.

I would like to use that to implement 1minutetip compatibility glue. But haven't found good alternatives.

@happz
Copy link
Collaborator

happz commented May 5, 2023

Would tmt * export suit your use case? I for one am used to jq or yq, tmt can be asked to export test in YAML format, so to create a list of "${testname} ${space-separated requirements}" entries I'd do this:

$ tmt test export --how yaml | yq -r '.[] | "\(.name) \(.require | join(" "))"'
/tests/provision/virtual/dependencies tmt-provision-virtual beakerlib
/tests/artemis/noop tmt
/tests/clean/chain tmt beakerlib
/tests/clean/guests tmt beakerlib
/tests/clean/runs tmt beakerlib
/tests/core/adjust tmt tmt-provision-container beakerlib
/tests/core/debug tmt beakerlib
/tests/core/docs tmt beakerlib
/tests/core/dry tmt beakerlib
...

@pemensik
Copy link
Contributor Author

pemensik commented May 5, 2023

Not sure where your yq command comes from, I haven't found that packaged in Fedora. I don't know tools working with yaml format directly, but it seems I would be able to use tmt test export --how dict to present python dictionary. Then put together some python code to print it in shell-like variables format. I guess json format would it make easier to me. But maybe just more plain key=value should be offered directly by tmt. Thanks for the export subcommand, I haven't looked there.

@happz
Copy link
Collaborator

happz commented May 5, 2023

  • jq is a tool for transforming JSON: https://stedolan.github.io/jq/
  • yq is a wrapper for jq, basically converts YAML to JSON and runs jq to take over from there.

JSON is not among supported export formats, but it should be trivial to add. Absolutely doable, let´s add it.

WRT the plain key=value, could you provide an example, how would it look like? It’s really easy to add new format, adding a well-defined one for tools happier with key=value rather than with YAML shouldn’t be complex.

@pemensik
Copy link
Contributor Author

pemensik commented May 5, 2023

I have put quick&dirty attempt to print values in shell-like manner.

#!/usr/bin/python3

import sys
import subprocess

"""
Convert tmt tests metadata to shell parseable output.

No proper escaping of strings is done!
"""

def print_keyval(keyname, value):
    escaped = value.replace('\'', '\\\'')
    print(keyname+'=\''+escaped+'\'')

def print_prop(test, prop, delimiter=' '):
    keyname = prop.upper()
    if isinstance(test[prop], list):
        print_keyval(keyname, delimiter.join(test[prop]))
    elif isinstance(test[prop], str):
        print_keyval(keyname, test[prop])
    else:
        print_keyval(keyname, str(test[prop]))

def print_one(test):
    for k in test.keys():
        print_prop(test, k)


process = subprocess.run(["tmt", "test", "export", "--how", "dict", "."], capture_output=True, check=True)
textdata = process.stdout
#print(textdata, type(textdata))
testdata = eval(textdata, {})

if len(testdata) > 1:
    raise("Invalid input data")

print_one(testdata[0])

I can do with it:

  • python tmt-shell.py > test.data
  • (source test.data && echo $SUMMARY && echo $REQUIRE $RECOMMEND)

Which is kind of what I were looking for.

@happz
Copy link
Collaborator

happz commented May 6, 2023

Proposed a JSON export in #2058, for a good measure. We already support YAML, there's no technical reason to not support JSON as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants