tmt icon indicating copy to clipboard operation
tmt copied to clipboard

Enabled/Disable Prepare step by adjusting environment variables

Open qcheng-redhat opened this issue 1 year ago • 8 comments

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.

qcheng-redhat avatar Jul 19 '24 02:07 qcheng-redhat

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.

LecrisUT avatar Jul 19 '24 06:07 LecrisUT

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.

qcheng-redhat avatar Jul 19 '24 08:07 qcheng-redhat

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.

LecrisUT avatar Jul 19 '24 08:07 LecrisUT

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 ...

psss avatar Aug 09 '24 11:08 psss

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.

qcheng-redhat avatar Aug 09 '24 12:08 qcheng-redhat

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?

psss avatar Aug 12 '24 12:08 psss

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.

LecrisUT avatar Aug 12 '24 12:08 LecrisUT

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

qcheng-redhat avatar Aug 13 '24 08:08 qcheng-redhat

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?

psss avatar Aug 26 '25 08:08 psss

@qcheng-redhat, could you please confirm if your use case is covered by the proposed adjust rule above?

psss avatar Sep 09 '25 10:09 psss

@qcheng-redhat, could you please confirm if your use case is covered by the proposed adjust rule above?

Hi Sorry for the late reply. Yes, the current implementation works for us. Please feel free to close this issue.

qcheng-redhat avatar Sep 10 '25 08:09 qcheng-redhat

@qcheng-redhat, could you please confirm if your use case is covered by the proposed adjust rule 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.

happz avatar Sep 10 '25 08:09 happz