goss-org / goss

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

semver-constraint usage with package version #772

Closed sfc-gh-ssudakovich closed 10 months ago

sfc-gh-ssudakovich commented 2 years ago

I am trying to make sure that a package is installed and the version is at least X.Y.Z or higher. For the sake of the example, let's say the package is td-agent and the minimal version is 3.8.2. What I came up with using the manual is this

package:
  td-agent:
    installed: true
    versions:
      and:
        - have-len: 1
        - contain-element:
            semver-constraint: ">3.8.2"

That results in

Package: td-agent: version:
Expected
    <[]string | len:1, cap:1>: ["3.8.1"]
to contain element matching
    <*matchers.BeSemverConstraintMatcher | 0xc0002ae230>: {
        Constraint: <string>">3.8.2",
    }

I feel like it is trying to compare an array to a matcher but I am not sure. Any suggestions?

aelsabbahy commented 10 months ago

The test is reporting thaht it found version 3.8.1 which does not meet the constraint >3.8.2

The reason it's an array is because there may be multiple versions installed of the same packcage.

Some examples (using latest version of goss):

package:
  td-agent:
    installed: true
    versions:
      and:
        - have-len: 1
        - contain-element:
            semver-constraint: ">3.8.2"
$ goss v
F

Failures/Skipped:

Matching: td-agent: matches:
Expected
    ["3.8.1"]
to contain element matching
    {"semver-constraint":">3.8.2"}

Total Duration: 0.000s
Count: 1, Failed: 1, Skipped: 0

If I change it to this:

matching:
  td-agent:
    content:
      - 3.8.1
      - 3.8.0
    matches:
      and:
        - have-len: 2
        - contain-element:
            semver-constraint: ">3.8.2"

It'll still fail with:


$ goss v
F

Failures/Skipped:

Matching: td-agent: matches:
Expected
    ["3.8.1","3.8.0"]
to contain element matching
    {"semver-constraint":">3.8.2"}

Total Duration: 0.000s
Count: 1, Failed: 1, Skipped: 0

However, the following will pass, since at least one element matches.

matching:
  td-agent:
    content:
      - 3.8.1
      - 3.8.3
    matches:
      and:
        - have-len: 2
        - contain-element:
            semver-constraint: ">3.8.2"

Without the contain-element, all elements must match:

matching:
  td-agent:
    content:
      - 3.8.4
      - 3.8.3
    matches:
      and:
        - have-len: 2
        - semver-constraint: ">3.8.2"