Skip to content

Commit

Permalink
event backend none: return an error when reading events
Browse files Browse the repository at this point in the history
podman --events-backend none events should return with an error since it
will never be able to actually list events.

Fixes part three of containers#15688

Signed-off-by: Paul Holzinger <[email protected]>
  • Loading branch information
Luap99 committed Sep 12, 2022
1 parent c5bdb6a commit 138b09c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 25 deletions.
39 changes: 21 additions & 18 deletions cmd/podman/system/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,25 +99,28 @@ func eventsCmd(cmd *cobra.Command, _ []string) error {
errChannel <- err
}()

for event := range eventChannel {
switch {
case event == nil:
// no-op
case doJSON:
jsonStr, err := event.ToJSONString()
if err != nil {
return err
for {
select {
case err := <-errChannel:
return err
case event := <-eventChannel:
switch {
case event == nil:
// no-op
case doJSON:
jsonStr, err := event.ToJSONString()
if err != nil {
return err
}
fmt.Println(jsonStr)
case cmd.Flags().Changed("format"):
if err := rpt.Execute(event); err != nil {
return err
}
os.Stdout.WriteString("\n")
default:
fmt.Println(event.ToHumanReadable(!noTrunc))
}
fmt.Println(jsonStr)
case cmd.Flags().Changed("format"):
if err := rpt.Execute(event); err != nil {
return err
}
os.Stdout.WriteString("\n")
default:
fmt.Println(event.ToHumanReadable(!noTrunc))
}
}

return <-errChannel
}
2 changes: 1 addition & 1 deletion libpod/events/events_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func NewEventer(options EventerOptions) (Eventer, error) {
case strings.ToUpper(LogFile.String()):
return newLogFileEventer(options)
case strings.ToUpper(Null.String()):
return NewNullEventer(), nil
return newNullEventer(), nil
case strings.ToUpper(Memory.String()):
return NewMemoryEventer(), nil
default:
Expand Down
13 changes: 7 additions & 6 deletions libpod/events/nullout.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,26 @@ package events

import (
"context"
"errors"
)

// EventToNull is an eventer type that only performs write operations
// and only writes to /dev/null. It is meant for unittests only
// EventToNull is an eventer type that does nothing.
// It is meant for unittests only
type EventToNull struct{}

// Write eats the event and always returns nil
func (e EventToNull) Write(ee Event) error {
return nil
}

// Read does nothing. Do not use it.
// Read does nothing and returns an error.
func (e EventToNull) Read(ctx context.Context, options ReadOptions) error {
return nil
return errors.New("cannot read events with the \"none\" backend")
}

// NewNullEventer returns a new null eventer. You should only do this for
// newNullEventer returns a new null eventer. You should only do this for
// the purposes of internal libpod testing.
func NewNullEventer() Eventer {
func newNullEventer() Eventer {
return EventToNull{}
}

Expand Down
9 changes: 9 additions & 0 deletions test/system/090-events.bats
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,12 @@ EOF
--format="{{.Attributes.$lname}}"
assert "$output" = "$lvalue" "podman-events output includes container label"
}

@test "events - backend none should error" {
skip_if_remote "remote does not support --events-backend"

run_podman 125 --events-backend none events
is "$output" "Error: cannot read events with the \"none\" backend" "correct error message"
run_podman 125 --events-backend none events --stream=false
is "$output" "Error: cannot read events with the \"none\" backend" "correct error message"
}

0 comments on commit 138b09c

Please sign in to comment.