Skip to content

Commit

Permalink
logFile until flag issue
Browse files Browse the repository at this point in the history
we were adding a negative duration in podman events, causing inputs like
-5s to be correct and 5s to be incorrect.

fixes containers#11158

Signed-off-by: cdoern <[email protected]>
  • Loading branch information
cdoern committed Aug 23, 2021
1 parent 04ab2b1 commit d06d285
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 12 deletions.
4 changes: 2 additions & 2 deletions cmd/podman/containers/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,15 @@ func logsFlags(cmd *cobra.Command) {
func logs(_ *cobra.Command, args []string) error {
if logsOptions.SinceRaw != "" {
// parse time, error out if something is wrong
since, err := util.ParseInputTime(logsOptions.SinceRaw)
since, err := util.ParseInputTime(logsOptions.SinceRaw, true)
if err != nil {
return errors.Wrapf(err, "error parsing --since %q", logsOptions.SinceRaw)
}
logsOptions.Since = since
}
if logsOptions.UntilRaw != "" {
// parse time, error out if something is wrong
until, err := util.ParseInputTime(logsOptions.UntilRaw)
until, err := util.ParseInputTime(logsOptions.UntilRaw, false)
if err != nil {
return errors.Wrapf(err, "error parsing --until %q", logsOptions.UntilRaw)
}
Expand Down
4 changes: 2 additions & 2 deletions libpod/events/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func generateEventFilters(filters []string, since, until string) (map[string][]E
}

if len(since) > 0 {
timeSince, err := util.ParseInputTime(since)
timeSince, err := util.ParseInputTime(since, true)
if err != nil {
return nil, errors.Wrapf(err, "unable to convert since time of %s", since)
}
Expand All @@ -144,7 +144,7 @@ func generateEventFilters(filters []string, since, until string) (map[string][]E
}

if len(until) > 0 {
timeUntil, err := util.ParseInputTime(until)
timeUntil, err := util.ParseInputTime(until, false)
if err != nil {
return nil, errors.Wrapf(err, "unable to convert until time of %s", until)
}
Expand Down
4 changes: 3 additions & 1 deletion libpod/events/journal_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,15 @@ func (e EventJournalD) Read(ctx context.Context, options ReadOptions) error {
if err != nil {
return errors.Wrapf(err, "failed to parse event filters")
}

var untilTime time.Time
if len(options.Until) > 0 {
untilTime, err = util.ParseInputTime(options.Until)
untilTime, err = util.ParseInputTime(options.Until, false)
if err != nil {
return err
}
}

j, err := sdjournal.NewJournal()
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion libpod/events/logfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (e EventLogFile) Read(ctx context.Context, options ReadOptions) error {
return err
}
if len(options.Until) > 0 {
untilTime, err := util.ParseInputTime(options.Until)
untilTime, err := util.ParseInputTime(options.Until, false)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/handlers/compat/containers_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func LogsFromContainer(w http.ResponseWriter, r *http.Request) {

var since time.Time
if _, found := r.URL.Query()["since"]; found {
since, err = util.ParseInputTime(query.Since)
since, err = util.ParseInputTime(query.Since, true)
if err != nil {
utils.BadRequest(w, "since", query.Since, err)
return
Expand All @@ -73,7 +73,7 @@ func LogsFromContainer(w http.ResponseWriter, r *http.Request) {
var until time.Time
if _, found := r.URL.Query()["until"]; found {
if query.Until != "0" {
until, err = util.ParseInputTime(query.Until)
until, err = util.ParseInputTime(query.Until, false)
if err != nil {
utils.BadRequest(w, "until", query.Until, err)
return
Expand Down
7 changes: 5 additions & 2 deletions pkg/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ func WriteStorageConfigFile(storageOpts *stypes.StoreOptions, storageConf string
// ParseInputTime takes the users input and to determine if it is valid and
// returns a time format and error. The input is compared to known time formats
// or a duration which implies no-duration
func ParseInputTime(inputTime string) (time.Time, error) {
func ParseInputTime(inputTime string, since bool) (time.Time, error) {
timeFormats := []string{time.RFC3339Nano, time.RFC3339, "2006-01-02T15:04:05", "2006-01-02T15:04:05.999999999",
"2006-01-02Z07:00", "2006-01-02"}
// iterate the supported time formats
Expand All @@ -542,7 +542,10 @@ func ParseInputTime(inputTime string) (time.Time, error) {
if err != nil {
return time.Time{}, errors.Errorf("unable to interpret time value")
}
return time.Now().Add(-duration), nil
if since {
return time.Now().Add(-duration), nil
}
return time.Now().Add(duration), nil
}

// OpenExclusiveFile opens a file for writing and ensure it doesn't already exist
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func TestPeriodAndQuotaToCores(t *testing.T) {
}

func TestParseInputTime(t *testing.T) {
tm, err := ParseInputTime("1.5")
tm, err := ParseInputTime("1.5", true)
if err != nil {
t.Errorf("expected error to be nil but was: %v", err)
}
Expand Down
13 changes: 13 additions & 0 deletions test/e2e/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,19 @@ var _ = Describe("Podman events", func() {
Expect(result.OutputToString()).To(ContainSubstring(name2))
Expect(result.OutputToString()).To(ContainSubstring(name3))

// string duration in 10 seconds
untilT := time.Now().Add(time.Second * 9)
result = podmanTest.Podman([]string{"events", "--since", "30s", "--until", "10s"})
result.Wait(11)
Expect(result).Should(Exit(0))
tEnd := time.Now()
outDur := tEnd.Sub(untilT)
diff := outDur.Seconds() > 0
Expect(diff).To(Equal(true))
Expect(result.OutputToString()).To(ContainSubstring(name1))
Expect(result.OutputToString()).To(ContainSubstring(name2))
Expect(result.OutputToString()).To(ContainSubstring(name3))

wg.Wait()
})
})
2 changes: 1 addition & 1 deletion test/e2e/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ var _ = Describe("Podman logs", func() {
results := podmanTest.Podman([]string{"logs", "--until", "10m", cid})
results.WaitWithDefaultTimeout()
Expect(results).To(Exit(0))
Expect(len(results.OutputToStringArray())).To(Equal(0))
Expect(len(results.OutputToStringArray())).To(Equal(3))
})

It("until time NOW: "+log, func() {
Expand Down

0 comments on commit d06d285

Please sign in to comment.