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

Run entry when it matches some condition? #3300

Open
iredmail opened this issue Oct 11, 2024 · 7 comments
Open

Run entry when it matches some condition? #3300

iredmail opened this issue Oct 11, 2024 · 7 comments
Labels
enhancement New feature or request

Comments

@iredmail
Copy link

iredmail commented Oct 11, 2024

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 iredmail added the enhancement New feature or request label Oct 11, 2024
@jcamiel
Copy link
Collaborator

jcamiel commented Oct 11, 2024

Hi @iredmail

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

@iredmail
Copy link
Author

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

@SilenLoc
Copy link

SilenLoc commented Oct 18, 2024

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

@marcodpt
Copy link

marcodpt commented Nov 14, 2024

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

@jcamiel
Copy link
Collaborator

jcamiel commented Nov 14, 2024

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

@marcodpt
Copy link

marcodpt commented Nov 14, 2024

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.

@iredmail
Copy link
Author

iredmail commented Nov 15, 2024

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

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

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

No branches or pull requests

4 participants