goss-org / goss

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

Support for asserting against systemd template units? #345

Closed petemounce closed 4 years ago

petemounce commented 6 years ago

I'm setting up some buildkite agents. I'm running several instances per host, via their documentation.

I've got 8 running:

$ systemctl list-units | grep buildkite
buildkite-agent@1.service                                                                   loaded active running   Buildkite Agent
buildkite-agent@2.service                                                                   loaded active running   Buildkite Agent
buildkite-agent@3.service                                                                   loaded active running   Buildkite Agent
buildkite-agent@4.service                                                                   loaded active running   Buildkite Agent
buildkite-agent@5.service                                                                   loaded active running   Buildkite Agent
buildkite-agent@6.service                                                                   loaded active running   Buildkite Agent
buildkite-agent@7.service                                                                   loaded active running   Buildkite Agent
buildkite-agent@8.service                                                                   loaded active running   Buildkite Agent
system-buildkite\x2dagent.slice                                                             loaded active active    system-buildkite\x2dagent.slice

I've set up assertions as this, and they pass:

service:
  buildkite-agent:
    enabled: false
    running: false
  buildkite-agent@1: # I'd like to ideally avoid duplicating this 8 times where only the index differs, since that will mean adjusting my tests if I change my mind about the number I want running. So I'd prefer this to be config/parameter-driven.
    enabled: true
    running: true
process:
  buildkite-agent:
    running: true
$ goss v -f tap
1..5
ok 1 - Process: buildkite-agent: running: matches expectation: [true]
ok 2 - Service: buildkite-agent@1: enabled: matches expectation: [true]
ok 3 - Service: buildkite-agent@1: running: matches expectation: [true]
ok 4 - Service: buildkite-agent: enabled: matches expectation: [false]
ok 5 - Service: buildkite-agent: running: matches expectation: [false]

I'd like to be able to make assertions like (as in, this is a sketch, I don't know whether it's a good one):

process:
  buildkite-agent:
    running: true
    count: 8

service:
  buildkite-agent:
    enabled: true # when template, then I would expect this to pass only if all n are running
    running: true
    templated: true
    count: 8

I'd like to be able to supply the count as a parameter. Failing that, I'd like to template it in, somehow - I'm probably happy to do that externally rather than ask to complicate goss by building in templating, but I also don't know whether that's a reasonable thing to suggest or not.

What do you think?

petemounce commented 6 years ago

Oh, excuse me; I've just noticed the template feature. I'll experiment with that.

petemounce commented 6 years ago

The closest I've been able to come is

service:
  buildkite-agent:
    enabled: false
{{ range mkSlice 1 2 3 4 5 6 7 8 }}
  buildkite-agent@{{ . }}:
    enabled: true
    running: true
{{ end }}

I came across a stackoverflow and have opened a PR to add what I'd like. It's fledgling, because I wasn't sure it'd be a welcome contribution. If it is, I'd be happy to add tests and docs as you advise?

aelsabbahy commented 6 years ago

I like the PR you submitted, once the PR contains documentation it can be merged/released.

aelsabbahy commented 4 years ago

Was this fixed by the addition of sprig functions?

petemounce commented 4 years ago

Yes, I think I can use | repeat 8 (or some env-var). Thanks!