Uploaded image for project: 'Red Hat OpenStack Services on OpenShift'
  1. Red Hat OpenStack Services on OpenShift
  2. OSPRH-13139

[fvt] Incorrect fail condition in the file test

XMLWordPrintable

    • CloudOps 2025 Sprint 1, CloudOps 2025 Sprint 2
    • 2
    • Important

       

      https://github.com/infrawatch/feature-verification-tests/pull/204

       

      So, what happened to the file tests.

       

      The failed_when requires that all the conditions be met in order to fail.

      The intent when writing was that all had to be met to pass, so any condition not met should lead to a failure.

      The failed_when should explicitly use `or` in a single list item, rather than providing a list of conditions.

       

      Two options present themselves.

      1. Change the failed_when to use `or` in a single list item
      2. Add a second task that uses the assert module, which requires all list items be true in order to pass, and provide it with the negatives of the fail conditions

       

      Another option is also available, which would provide more information about the failures, since currently, the value that  is added to the XML is the `stat` structure returned by the stat module, which provides no information about which condition was not met.

      Option 1: 

       

       

      -  failed_when:
      
      -    - fstats.stat.pw_name != "root"
      -    - fstats.stat.size | int < 300
      -    - not fstats.stat.exists
      -    - not fstats.stat.isreg
      +  failed_when:
      +    - ( (not fstats.stat.exists) or
      +      (fstats.stat.pw_name != "root") or
      +      (fstats.stat.size | int < 300) or
      +      (not fstats.stat.isreg) )
      
      

       

       

      Option 2:

      -  failed_when:
      -   - fstats.stat.pw_name != "root"
      -   - fstats.stat.size | int < 300
      -   - not fstats.stat.exists
      -   - not fstats.stat.isreg
      +
      +- name: Use assert instead of failed_when
      +  ansible.builtin.assert:
      +   that:
      +     - fstats.stat.exists | bool
      +     - fstats.stat.pw_name == "root"
      +     - fstats.stat.size | int > 300
      +     - fstats.stat.isreg | bool
      

       

      Bonus option

      -  failed_when:
      -    - fstats.stat.pw_name != "root"
      -    - fstats.stat.size | int < 300
      -    - not fstats.stat.exists
      -    - not fstats.stat.isreg
      +
      +- name: "Record if the file is non-existant"
      +  ansible.builtin.set_fact:
      + fail_reason: "{{ [ fail_reason | default([]),  ['file does not exist'] ] | flatten }}"
      +  when: not (fstats.stat.exists | default(false))
      +
      +- name: "Record if the file is not owner by root"
      +  ansible.builtin.set_fact:
      + fail_reason: "{{ [ fail_reason | default([]),  ['file is not owned by root'] ] | flatten }}"
      +  when: fstats.stat.pw_name != "root"
      +
      +- name: "Record if the file is too small"
      +  ansible.builtin.set_fact:
      + fail_reason: "{{ [ fail_reason | default([]),  ['file is less than 300B'] ] | flatten }}"
      +  when: fstats.stat.size | int < 300
      +
      +- name: "Record if the file is irregular"
      +  ansible.builtin.set_fact:
      + fail_reason: "{{ [ fail_reason | default([]),  ['file is not a regular file'] ] | flatten }}"
      +  when: not fstats.stat.isreg
      +
      +- name: |
      + TEST Verify the file {{ item }}
      + {{ common_file_test_id }}
      +  ansible.builtin.assert:
      + that:
      +  - fail_reason | length == 0
      + fail_msg: "The file \"{{ item }}\" failed for the following reason(s): {{ fail_reason }}"
      + success_msg: "The test passed"
      

       

      I like the bonus option because it makes troubleshooting the failure easier.

      It looks a bit fiddly though, I will admit that, and it may not be immediately obvious why this is being done.

              efoley@redhat.com Emma Foley
              efoley@redhat.com Emma Foley
              rhos-dfg-cloudops
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated:
                Resolved: