-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4544 from thaJeztah/24.0_backport_fix_events_json…
…_format [24.0 backport] cli/command/system: fix "docker events" not supporting --format=json
- Loading branch information
Showing
10 changed files
with
148 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package system | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/docker/cli/internal/test" | ||
"github.com/docker/docker/api/types" | ||
"github.com/docker/docker/api/types/events" | ||
"gotest.tools/v3/assert" | ||
"gotest.tools/v3/golden" | ||
) | ||
|
||
func TestEventsFormat(t *testing.T) { | ||
var evts []events.Message | ||
for i, action := range []string{"create", "start", "attach", "die"} { | ||
evts = append(evts, events.Message{ | ||
Status: 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, format string | ||
}{ | ||
{ | ||
name: "default", | ||
}, | ||
{ | ||
name: "json", | ||
format: "json", | ||
}, | ||
{ | ||
name: "json template", | ||
format: "{{ json . }}", | ||
}, | ||
{ | ||
name: "json action", | ||
format: "{{ json .Action }}", | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
tc := tc | ||
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, types.EventsOptions) (<-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) | ||
if tc.format != "" { | ||
cmd.Flags().Set("format", tc.format) | ||
} | ||
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() | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
1970-01-01T00:00:01.000000000Z container create abc123 (image=ubuntu:latest) | ||
1970-01-01T00:00:02.000000000Z container start abc123 (image=ubuntu:latest) | ||
1970-01-01T00:00:03.000000000Z container attach abc123 (image=ubuntu:latest) | ||
1970-01-01T00:00:04.000000000Z container die abc123 (image=ubuntu:latest) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
"create" | ||
"start" | ||
"attach" | ||
"die" |
4 changes: 4 additions & 0 deletions
4
cli/command/system/testdata/docker-events-json-template.golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{"status":"create","id":"abc123","from":"ubuntu:latest","Type":"container","Action":"create","Actor":{"ID":"abc123","Attributes":{"image":"ubuntu:latest"}},"scope":"local","time":1000000000,"timeNano":1000000000} | ||
{"status":"start","id":"abc123","from":"ubuntu:latest","Type":"container","Action":"start","Actor":{"ID":"abc123","Attributes":{"image":"ubuntu:latest"}},"scope":"local","time":2000000000,"timeNano":2000000000} | ||
{"status":"attach","id":"abc123","from":"ubuntu:latest","Type":"container","Action":"attach","Actor":{"ID":"abc123","Attributes":{"image":"ubuntu:latest"}},"scope":"local","time":3000000000,"timeNano":3000000000} | ||
{"status":"die","id":"abc123","from":"ubuntu:latest","Type":"container","Action":"die","Actor":{"ID":"abc123","Attributes":{"image":"ubuntu:latest"}},"scope":"local","time":4000000000,"timeNano":4000000000} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{"status":"create","id":"abc123","from":"ubuntu:latest","Type":"container","Action":"create","Actor":{"ID":"abc123","Attributes":{"image":"ubuntu:latest"}},"scope":"local","time":1000000000,"timeNano":1000000000} | ||
{"status":"start","id":"abc123","from":"ubuntu:latest","Type":"container","Action":"start","Actor":{"ID":"abc123","Attributes":{"image":"ubuntu:latest"}},"scope":"local","time":2000000000,"timeNano":2000000000} | ||
{"status":"attach","id":"abc123","from":"ubuntu:latest","Type":"container","Action":"attach","Actor":{"ID":"abc123","Attributes":{"image":"ubuntu:latest"}},"scope":"local","time":3000000000,"timeNano":3000000000} | ||
{"status":"die","id":"abc123","from":"ubuntu:latest","Type":"container","Action":"die","Actor":{"ID":"abc123","Attributes":{"image":"ubuntu:latest"}},"scope":"local","time":4000000000,"timeNano":4000000000} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters