From c528d7a622267fcee0f38f25c94198b23deed9dd Mon Sep 17 00:00:00 2001 From: Matthew Heon Date: Thu, 11 Jul 2019 13:32:42 -0400 Subject: Use file-based eventer for integration tests This adds several top-level Podman flags for specifying different events backend types, which are then used in CI. It resolves a number of serious issues with events-based testing. Signed-off-by: Matthew Heon --- libpod/container_api.go | 2 +- libpod/container_internal.go | 8 +++++++- libpod/events/config.go | 2 ++ libpod/events/events.go | 23 +++++++++++++++++++++-- libpod/events/events_linux.go | 4 +++- libpod/events/logfile.go | 2 +- libpod/options.go | 20 ++++++++++++++++++++ libpod/runtime_ctr.go | 7 +------ 8 files changed, 56 insertions(+), 12 deletions(-) (limited to 'libpod') diff --git a/libpod/container_api.go b/libpod/container_api.go index 0e877d04e..c52b27db3 100644 --- a/libpod/container_api.go +++ b/libpod/container_api.go @@ -187,7 +187,7 @@ func (c *Container) StopWithTimeout(timeout uint) error { c.state.State == ContainerStateExited { return ErrCtrStopped } - defer c.newContainerEvent(events.Stop) + return c.stop(timeout) } diff --git a/libpod/container_internal.go b/libpod/container_internal.go index f6fe9cd0c..ab0ad6516 100644 --- a/libpod/container_internal.go +++ b/libpod/container_internal.go @@ -1044,7 +1044,13 @@ func (c *Container) stop(timeout uint) error { } // Wait until we have an exit file, and sync once we do - return c.waitForExitFileAndSync() + if err := c.waitForExitFileAndSync(); err != nil { + return err + } + + c.newContainerEvent(events.Stop) + + return nil } // Internal, non-locking function to pause a container diff --git a/libpod/events/config.go b/libpod/events/config.go index b9f01f3a5..96172d47b 100644 --- a/libpod/events/config.go +++ b/libpod/events/config.go @@ -14,6 +14,8 @@ const ( LogFile EventerType = iota // Journald indicates journald should be used to log events Journald EventerType = iota + // Null is a no-op events logger. It does not read or write events. + Null EventerType = iota ) // Event describes the attributes of a libpod event diff --git a/libpod/events/events.go b/libpod/events/events.go index 2bebff162..5e828bc8a 100644 --- a/libpod/events/events.go +++ b/libpod/events/events.go @@ -16,11 +16,30 @@ var ErrNoJournaldLogging = errors.New("No support for journald logging") // String returns a string representation of EventerType func (et EventerType) String() string { - if et == LogFile { + switch et { + case LogFile: return "file" + case Journald: + return "journald" + case Null: + return "none" + default: + return "invalid" + } +} +// IsValidEventer checks if the given string is a valid eventer type. +func IsValidEventer(eventer string) bool { + switch eventer { + case LogFile.String(): + return true + case Journald.String(): + return true + case Null.String(): + return true + default: + return false } - return "journald" } // NewEvent creates a event struct and populates with diff --git a/libpod/events/events_linux.go b/libpod/events/events_linux.go index 11f309574..ffb100be8 100644 --- a/libpod/events/events_linux.go +++ b/libpod/events/events_linux.go @@ -18,8 +18,10 @@ func NewEventer(options EventerOptions) (eventer Eventer, err error) { } case strings.ToUpper(LogFile.String()): eventer = EventLogFile{options} + case strings.ToUpper(Null.String()): + eventer = NewNullEventer() default: - return eventer, errors.Errorf("unknown event logger type: %s", strings.ToUpper(options.EventerType)) + return nil, errors.Errorf("unknown event logger type: %s", strings.ToUpper(options.EventerType)) } return eventer, nil } diff --git a/libpod/events/logfile.go b/libpod/events/logfile.go index e5efc09bb..30d72b9fc 100644 --- a/libpod/events/logfile.go +++ b/libpod/events/logfile.go @@ -55,7 +55,7 @@ func (e EventLogFile) Read(options ReadOptions) error { return err } switch event.Type { - case Image, Volume, Pod, Container: + case Image, Volume, Pod, System, Container: // no-op default: return errors.Errorf("event type %s is not valid in %s", event.Type.String(), e.options.LogFilePath) diff --git a/libpod/options.go b/libpod/options.go index cdac09654..62571957f 100644 --- a/libpod/options.go +++ b/libpod/options.go @@ -8,6 +8,7 @@ import ( "syscall" "github.com/containers/image/manifest" + "github.com/containers/libpod/libpod/events" "github.com/containers/libpod/pkg/namespaces" "github.com/containers/libpod/pkg/rootless" "github.com/containers/libpod/pkg/util" @@ -421,6 +422,25 @@ func WithDefaultInfraCommand(cmd string) RuntimeOption { } } +// WithEventsLogger sets the events backend to use. +// Currently supported values are "file" for file backend and "journald" for +// journald backend. +func WithEventsLogger(logger string) RuntimeOption { + return func(rt *Runtime) error { + if rt.valid { + return ErrRuntimeFinalized + } + + if !events.IsValidEventer(logger) { + return errors.Wrapf(ErrInvalidArg, "%q is not a valid events backend", logger) + } + + rt.config.EventsLogger = logger + + return nil + } +} + // WithRenumber instructs libpod to perform a lock renumbering while // initializing. This will handle migrations from early versions of libpod with // file locks to newer versions with SHM locking, as well as changes in the diff --git a/libpod/runtime_ctr.go b/libpod/runtime_ctr.go index 0871b83a7..7ef7b8828 100644 --- a/libpod/runtime_ctr.go +++ b/libpod/runtime_ctr.go @@ -376,14 +376,9 @@ func (r *Runtime) removeContainer(ctx context.Context, c *Container, force bool, // Check that the container's in a good state to be removed if c.state.State == ContainerStateRunning { - if err := r.ociRuntime.stopContainer(c, c.StopTimeout()); err != nil { + if err := c.stop(c.StopTimeout()); err != nil { return errors.Wrapf(err, "cannot remove container %s as it could not be stopped", c.ID()) } - - // Need to update container state to make sure we know it's stopped - if err := c.waitForExitFileAndSync(); err != nil { - return err - } } // Check that all of our exec sessions have finished -- cgit v1.2.3-54-g00ecf