Enabled/Disable Prepare step by adjusting environment variables
In our TMT plan, we want to define a Prepare step with specific conditions. It would be helpful if the step can be enabled/disabled by adjusting an environment variable, and this environment variable value could be used in the Prepare step.
What setup do you have? Usually I would consider using adjust and context to enable/disable it, but I think there is no usable mechanism for lists of steps here other than the - operator.
In the prepare step, we want to setup a repo to install some pkgs. E.g. when the test_repo url is defined in environment variable, we do the setup, otherwise we skip this step.
Maybe you can try to use $ to expand the environment variable, not sure at which stage this is done. Some fields like environment do not expand it. Otherwise it also depends on how do you setup the environment variable and where do you initialize the tmt tests from, because these might need to be defined at the parsing of the fmf files.
Perhaps, the following example from the adjust key specification can be inspirative for your use case?
# Enable a custom dnf repository based on the context value
adjust:
- when: repo is defined
prepare+:
- script: |
cat > /etc/yum.repos.d/custom-repo.repo <<EOF
[custom-repo]
baseurl=$@repo
gpgcheck=0
EOF
# The corresponding command line can look like this
tmt --context repo=https://custom.url/fedora-38-x86_64 run ...
Hi @psss, thank you for for the example. Yes, I have prepared patch in our internal project repository, use the same script. In early time, I did not use $@ to get the value of context. Now I can use the context value to setup repo on host. But in our case, we also want to setup the same repo on guest vm, so I convert the context to environment variables and use it in our guest plan to setup repo in my current solution.
I see. So, I guess, your use case is covered? Can we close the issue? Or do you have an idea of a better / more straightforward approach?
I guess one issue would be with documentation. Iiuc $@ is not defined in the fmf spec, so it is quite ambiguous where do $@context_var or even worse $env_var get expanded, and that is after getting a familiarity with the distinction between fmf and tmt.
For $@ I read the example from https://tmt.readthedocs.io/en/stable/examples.html#parametrize-plans, context parametrization part. Use context to adjust Prepare step is ok now. Thanks @psss and @LecrisUT for your example and discussion.
For a test, if the test needs some environment variables, is it possible to enable/disable it by adjusting one environment variable is given or not ? Now I am using context and write it into the file:
echo env_var=$@{context_var} >> $TMT_PLAN_ENVIRONMENT_FILE
For
$@I read the example from https://tmt.readthedocs.io/en/stable/examples.html#parametrize-plans, context parametrization part. Use context to adjust Prepare step is ok now. Thanks @psss and @LecrisUT for your example and discussion.
Ok, thanks, so this should be covered.
For a test, if the test needs some environment variables, is it possible to enable/disable it by adjusting one environment variable is given or not ? Now I am using context and write it into the file:
Currently it's only possible to enable or disable test based on the context, for example:
test: ./test.sh
enabled: true
adjust:
- enabled: false
when: arch == s390x
Is this sufficient for your use case?
@qcheng-redhat, could you please confirm if your use case is covered by the proposed adjust rule above?
@qcheng-redhat, could you please confirm if your use case is covered by the proposed
adjustrule above?
Hi Sorry for the late reply. Yes, the current implementation works for us. Please feel free to close this issue.
@qcheng-redhat, could you please confirm if your use case is covered by the proposed
adjustrule above?Hi Sorry for the late reply. Yes, the current implementation works for us. Please feel free to close this issue.
Perfect, thanks for confirming.