docker-cli/cli/command/system/events_test.go
Sebastiaan van Stijn 73ff81b651
cli/command/system: TestEventsFormat: set cmd.Args to prevent test-failures
When running the tests with options set, such as `-update` for updating
"golden" files, this test would pick up test arguments because no arguments
were set to invoke the command;

    go test . -update
    Error: unknown shorthand flag: 'u' in -update
    Usage:
      events [OPTIONS] [flags]

    Flags:
      -f, --filter filter   Filter output based on conditions provided
          --format string   Format output using a custom template:
                            'json':             Print in JSON format
                            'TEMPLATE':         Print output using the given Go template.
                            Refer to https://docs.docker.com/go/formatting/ for more information about formatting output with templates
      -h, --help            help for events
          --since string    Show all events created since timestamp
          --until string    Stream events until this timestamp

    --- FAIL: TestEventsFormat (0.00s)
        --- FAIL: TestEventsFormat/default (0.00s)
            events_test.go:75: assertion failed: error is not nil: unknown shorthand flag: 'u' in -update
        --- FAIL: TestEventsFormat/json (0.00s)
            events_test.go:75: assertion failed: error is not nil: unknown shorthand flag: 'u' in -update
        --- FAIL: TestEventsFormat/json_template (0.00s)
            events_test.go:75: assertion failed: error is not nil: unknown shorthand flag: 'u' in -update
        --- FAIL: TestEventsFormat/json_action (0.00s)
            events_test.go:75: assertion failed: error is not nil: unknown shorthand flag: 'u' in -update

This patch:

- changes the test to use command-arguments instead of manually setting the
  flag options; this also adds test-coverage for parsing actual command arguments.
- discards stdout/stderr of the command to prevent noise in test output

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-12-14 13:46:53 +01:00

84 lines
2.0 KiB
Go

package system
import (
"context"
"fmt"
"io"
"strings"
"testing"
"time"
"github.com/docker/cli/internal/test"
"github.com/docker/docker/api/types/events"
"gotest.tools/v3/assert"
"gotest.tools/v3/golden"
)
func TestEventsFormat(t *testing.T) {
var evts []events.Message //nolint:prealloc
for i, action := range []events.Action{events.ActionCreate, events.ActionStart, events.ActionAttach, events.ActionDie} {
evts = append(evts, events.Message{
Status: string(action),
ID: "abc123",
From: "ubuntu:latest",
Type: events.ContainerEventType,
Action: action,
Actor: events.Actor{
ID: "abc123",
Attributes: map[string]string{"image": "ubuntu:latest"},
},
Scope: "local",
Time: int64(time.Second) * int64(i+1),
TimeNano: int64(time.Second) * int64(i+1),
})
}
tests := []struct {
name string
args []string
}{
{
name: "default",
args: []string{},
},
{
name: "json",
args: []string{"--format", "json"},
},
{
name: "json template",
args: []string{"--format", "{{ json . }}"},
},
{
name: "json action",
args: []string{"--format", "{{ json .Action }}"},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
// Set to UTC timezone as timestamps in output are
// printed in the current timezone
t.Setenv("TZ", "UTC")
cli := test.NewFakeCli(&fakeClient{eventsFn: func(context.Context, events.ListOptions) (<-chan events.Message, <-chan error) {
messages := make(chan events.Message)
errs := make(chan error, 1)
go func() {
for _, msg := range evts {
messages <- msg
}
errs <- io.EOF
}()
return messages, errs
}})
cmd := NewEventsCommand(cli)
cmd.SetArgs(tc.args)
cmd.SetOut(io.Discard)
cmd.SetErr(io.Discard)
assert.Check(t, cmd.Execute())
out := cli.OutBuffer().String()
assert.Check(t, golden.String(out, fmt.Sprintf("docker-events-%s.golden", strings.ReplaceAll(tc.name, " ", "-"))))
cli.OutBuffer().Reset()
})
}
}