goss-org / goss

Quick and Easy server testing/validation
https://goss.rocks
Apache License 2.0
5.58k stars 470 forks source link

Duplicate Key Override Behavior #865

Closed david-rse closed 8 months ago

david-rse commented 8 months ago

Business logic requires that a singular audit be granular in a way that only 1 condition is checked, however multiple audits may target the same object. In this situation, is it possible to override the key for each audit such that the "duplicate key detected" error is avoided?

Very basic example to demonstrate the problem:

check_1.yml

service:
  sshd:
    enabled: true

check_2.yml

service:
  sshd:
    running: true

Running goss ... validate ...: [WARN] Duplicate key detected: 'service: sshd'. The value from a later-loaded goss file has overwritten the previous value.

berney commented 8 months ago

Goss uses the yaml file format.

The issue you are hitting with duplicate keys warning is explained in the manual, here: https://github.com/goss-org/goss/blob/master/docs/manual.md#important-note-about-goss-file-format

I'm not sure I understand your opening comment:

Business logic requires that a singular audit be granular in a way that only 1 condition is checked, however multiple audits may target the same object.

If you re-wrote your examples above as following goss.yaml:

service:
  sshd:
    enabled: true
    running: true

You would be checking that the SSHd service:

  1. Is enabled
  2. Is running

To me this means the audits are both granular and only a single condition is checked per audit.

If you run goss validate --format documenation you will see there are two checks, not one that's testing multiple things.

Similar to the example in the manual.

Service: sshd: enabled: matches expectation: [true]
Service: sshd: running: matches expectation: [true]

Each line is a separate check, that can pass or fail.

The definition of the checks is defined in a tree structure. Duplicate keys in the tree will clobber each other, which is how yaml works, and goss is actually detecting this and issuing a warning.

aelsabbahy commented 8 months ago

@david-rse Is this what you're looking to do?

service:
    audit_test_1:
        name: sshd
        enabled: true
    audit_test_2:
        name: sshd
        running: true

That will work just fine, so long as the required assertions are present in every test.. so in some cases, there may be some duplicate assertions.

david-rse commented 8 months ago

@aelsabbahy It is. Thank you so much!