hurl icon indicating copy to clipboard operation
hurl copied to clipboard

Run entry when it matches some condition?

Open iredmail opened this issue 1 year ago • 8 comments

Problem to solve

Our application supports MySQL and OpenLDAP backends, there're few differences between them, we'd like to run certain hurl entries when it's using X backend. I didn't find condition support in hurl document.

Proposal

Any plan to support such conditions in hurl so that we don't need to handle it with shell script?

Like Ansible, use when:: https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_conditionals.html#basic-conditionals-with-when

tasks:
  - name: Shut down Debian flavored systems
    ansible.builtin.command: /sbin/shutdown -t now
    when: ansible_facts['os_family'] == "Debian"

And hurl:

POST {{server}}/domain/abc.io
WHEN: my_variable=my_value

HTTP 200
[Asserts]
...

iredmail avatar Oct 11 '24 02:10 iredmail

Hi @iredmail

Could you use skip option on some requests (by inverting some logic)?

jcamiel avatar Oct 11 '24 11:10 jcamiel

Hi @jcamiel,

Thanks for the reply.

  • How do i define when it should be true/false? For example: skip: my_var=my_value?
  • What if i need to skip for more cases and execute it for less cases? It will require more skip: checks.

A when: like Ansible might be a lot easier. :)

iredmail avatar Oct 12 '24 01:10 iredmail

FYI: An "if" was asked twice when I did my Hurl talk

SilenLoc avatar Oct 18 '24 10:10 SilenLoc

Unfortunately I'm trying to do a dynamic skip and I depend on knowing if an entry exists.

Then I perform a search (GET) and do a capture.

And then it would execute an insertion (POST) if the GET does not return a result.

However, capture only supports query and filter, and none of the filters have the ability to return true or false.

The solution in my opinion would be to easily add a toBool filter similar to toInt that allows you to convert non-zero values ​​to true and zero to false.

Or the more precise solution would be for captures to support the predicate generating a boolean variable that can be used in skip.

With dynamic repeat and skip working correctly I don't see any need for if/else and for loops

marcodpt avatar Nov 14 '24 13:11 marcodpt

Hi @marcodpt

We can implement a toBool filter (should we decide how we compute "trusly" and "falsy"), but I wondering if we shouldn't try to use existing notion of predicates == , !=, contains etc...

skip only supports for the moment literal value or expression like {{skip}} but we could imagine something like:

GET https://foo.com/search?q=toto
HTTP 200
[Captures]
name: jsonpath "$.results"

# Only create if we have no result
POST https://foo.com/create
[Options]
skip: {{ results isEmpty }}
HTTP 200

I think we get the use case, we just need to find something that is not completely alien from the current model. skip is already something a little hacky, it's the only option that can be set on [Options]section and not on CLI. We really want to make something coherant, useful but not too complicated...

jcamiel avatar Nov 14 '24 13:11 jcamiel

Hi @jcamiel,

Thank you very much for your comment.

If captures supports predicates, we have assertions and captures with an absolutely identical syntax.

The advantage is that it is possible to generate Boolean expressions to use in skip.

And another advantage is that it simplifies the syntax of .hurl files as captures and assertions will be absolutely identical.

This allows me to do this:

GET https://foo.com/search?q=toto
HTTP 200
[Captures]
name: jsonpath "$.results" isEmpty

# Only create if we have no result
POST https://foo.com/create
[Options]
skip: {{results}}
HTTP 200

Note that in this case captures and assertions have identical syntax and that only the isEmpty predicate was moved from your example

Reference for predicates

The toBool filter proposal was just a hack if it was not feasible to change the parser syntax.

I want to make it very clear that I am extremely grateful for your work and these are just opinions to improve the hurl even further.

Take as much time as necessary and prioritize the stability and correctness of the hurl.

marcodpt avatar Nov 14 '24 20:11 marcodpt

Can we support var == value syntax in skip:? Just like Ansible when:. for example:

skip: {{ my_var }} == "abc"

iredmail avatar Nov 15 '24 03:11 iredmail

also looking for this options to skip a test or set proxy for a test when its run in CI vs locally

ojsef39 avatar Dec 12 '25 15:12 ojsef39