Guidelines

This site is for tech Q&A. Please keep your posts focused on the subject at hand.

Ask one question at a time. Don't conflate multiple problems into a single question.

Make sure to include all relevant information in your posts. Try to avoid linking to external sites.

Links to documentation are fine, but in addition you should also quote the relevant parts in your posts.

0 votes
437 views
437 views

For a system validation (using goss) I want to check if a particular Docker container is running. The command

docker ps -a --format '{{.Names}} {{.State}}'

gives me the output I want, but when I put it into a goss check

command:
  foo_status:
    exec: docker ps -a --format '{{.Names}} {{.State}}'
    exit-status: 0
    stdout:
    - foo running
    timeout: 5000

I'm getting the following error:

Error: template: test:52:36: executing "test" at <.Names>: can't evaluate field Names in type *goss.TmplVars

I tried escaping the curly brackets with backslashes

command:
  foo_status:
    exec: docker ps -a --format '\{\{.Names\}\} \{\{.State\}\}'
    ...

but that doesn't seem to work either, because now I'm getting the error that the pattern can't be found:

Failures/Skipped:

Command: foo_status: stdout: patterns not found: [foo running]

in Sysadmin
by (125)
3 20 38
edit history

Please log in or register to answer this question.

1 Answer

0 votes
 

Like Docker, Goss uses Go templates which allow defining so-called "actions" (expressions in pairs of double curly brackets that are evaluated/expanded by the template engine). This leads to premature evaluation of the actions in the command string (through Goss instead of Docker), which causes the error you observed.

Unfortunately Google in their infinite wisdom didn't deem it necessary to allow escaping curly brackets in Go templates. The common workaround for putting literal double curly brackets in such templates is to use a curly-bracket-action that contains a raw string with the double curly brackets:

{{`{{foo}}`}}

The outer pair of double curly brackets is the action, whereas the text between the backticks is the literal string "{{foo}}". Evaluating the action produces the nested string including the curly brackets.

Change your Goss check to this:

command:
  foo_status:
    exec: docker ps -a --format '{{`{{.Names}} {{.State}}`}}'
    ...

and the problem will disappear.

by (125)
3 20 38
edit history
...