Merge pull request #9417 from ashley-cui/journald

Fix journald logs
This commit is contained in:
OpenShift Merge Robot
2021-02-21 05:03:46 -05:00
committed by GitHub
3 changed files with 379 additions and 307 deletions

View File

@ -8,11 +8,12 @@ import (
"fmt" "fmt"
"io" "io"
"math" "math"
"strings"
"time" "time"
"github.com/containers/podman/v2/libpod/define"
"github.com/containers/podman/v2/libpod/logs" "github.com/containers/podman/v2/libpod/logs"
journal "github.com/coreos/go-systemd/v22/sdjournal" journal "github.com/coreos/go-systemd/v22/sdjournal"
"github.com/hpcloud/tail/watch"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -34,10 +35,16 @@ func (c *Container) readFromJournal(ctx context.Context, options *logs.LogOption
var config journal.JournalReaderConfig var config journal.JournalReaderConfig
if options.Tail < 0 { if options.Tail < 0 {
config.NumFromTail = 0 config.NumFromTail = 0
} else if options.Tail == 0 {
config.NumFromTail = math.MaxUint64
} else { } else {
config.NumFromTail = uint64(options.Tail) config.NumFromTail = uint64(options.Tail)
} }
if options.Multi {
config.Formatter = journalFormatterWithID
} else {
config.Formatter = journalFormatter config.Formatter = journalFormatter
}
defaultTime := time.Time{} defaultTime := time.Time{}
if options.Since != defaultTime { if options.Since != defaultTime {
// coreos/go-systemd/sdjournal doesn't correctly handle requests for data in the future // coreos/go-systemd/sdjournal doesn't correctly handle requests for data in the future
@ -45,7 +52,7 @@ func (c *Container) readFromJournal(ctx context.Context, options *logs.LogOption
if time.Now().Before(options.Since) { if time.Now().Before(options.Since) {
return nil return nil
} }
config.Since = time.Since(options.Since) config.Since = -time.Since(options.Since)
} }
config.Matches = append(config.Matches, journal.Match{ config.Matches = append(config.Matches, journal.Match{
Field: "CONTAINER_ID_FULL", Field: "CONTAINER_ID_FULL",
@ -63,8 +70,12 @@ func (c *Container) readFromJournal(ctx context.Context, options *logs.LogOption
if options.Tail == math.MaxInt64 { if options.Tail == math.MaxInt64 {
r.Rewind() r.Rewind()
} }
state, err := c.State()
if err != nil {
return err
}
if options.Follow { if options.Follow && state == define.ContainerStateRunning {
go func() { go func() {
done := make(chan bool) done := make(chan bool)
until := make(chan time.Time) until := make(chan time.Time)
@ -76,6 +87,21 @@ func (c *Container) readFromJournal(ctx context.Context, options *logs.LogOption
// nothing to do anymore // nothing to do anymore
} }
}() }()
go func() {
for {
state, err := c.State()
if err != nil {
until <- time.Time{}
logrus.Error(err)
break
}
time.Sleep(watch.POLL_DURATION)
if state != define.ContainerStateRunning && state != define.ContainerStatePaused {
until <- time.Time{}
break
}
}
}()
follower := FollowBuffer{logChannel} follower := FollowBuffer{logChannel}
err := r.Follow(until, follower) err := r.Follow(until, follower)
if err != nil { if err != nil {
@ -114,7 +140,44 @@ func (c *Container) readFromJournal(ctx context.Context, options *logs.LogOption
return nil return nil
} }
func journalFormatterWithID(entry *journal.JournalEntry) (string, error) {
output, err := formatterPrefix(entry)
if err != nil {
return "", err
}
id, ok := entry.Fields["CONTAINER_ID_FULL"]
if !ok {
return "", fmt.Errorf("no CONTAINER_ID_FULL field present in journal entry")
}
if len(id) > 12 {
id = id[:12]
}
output += fmt.Sprintf("%s ", id)
// Append message
msg, err := formatterMessage(entry)
if err != nil {
return "", err
}
output += msg
return output, nil
}
func journalFormatter(entry *journal.JournalEntry) (string, error) { func journalFormatter(entry *journal.JournalEntry) (string, error) {
output, err := formatterPrefix(entry)
if err != nil {
return "", err
}
// Append message
msg, err := formatterMessage(entry)
if err != nil {
return "", err
}
output += msg
return output, nil
}
func formatterPrefix(entry *journal.JournalEntry) (string, error) {
usec := entry.RealtimeTimestamp usec := entry.RealtimeTimestamp
tsString := time.Unix(0, int64(usec)*int64(time.Microsecond)).Format(logs.LogTimeFormat) tsString := time.Unix(0, int64(usec)*int64(time.Microsecond)).Format(logs.LogTimeFormat)
output := fmt.Sprintf("%s ", tsString) output := fmt.Sprintf("%s ", tsString)
@ -137,13 +200,16 @@ func journalFormatter(entry *journal.JournalEntry) (string, error) {
output += fmt.Sprintf("%s ", logs.FullLogType) output += fmt.Sprintf("%s ", logs.FullLogType)
} }
return output, nil
}
func formatterMessage(entry *journal.JournalEntry) (string, error) {
// Finally, append the message // Finally, append the message
msg, ok := entry.Fields["MESSAGE"] msg, ok := entry.Fields["MESSAGE"]
if !ok { if !ok {
return "", fmt.Errorf("no MESSAGE field present in journal entry") return "", fmt.Errorf("no MESSAGE field present in journal entry")
} }
output += strings.TrimSpace(msg) return msg, nil
return output, nil
} }
type FollowBuffer struct { type FollowBuffer struct {

View File

@ -36,8 +36,9 @@ var _ = Describe("Podman logs", func() {
}) })
It("all lines", func() { for _, log := range []string{"k8s-file", "journald", "json-file"} {
logc := podmanTest.Podman([]string{"run", "-dt", ALPINE, "sh", "-c", "echo podman; echo podman; echo podman"}) It("all lines: "+log, func() {
logc := podmanTest.Podman([]string{"run", "--log-driver", log, "-dt", ALPINE, "sh", "-c", "echo podman; echo podman; echo podman"})
logc.WaitWithDefaultTimeout() logc.WaitWithDefaultTimeout()
Expect(logc).To(Exit(0)) Expect(logc).To(Exit(0))
@ -48,8 +49,8 @@ var _ = Describe("Podman logs", func() {
Expect(len(results.OutputToStringArray())).To(Equal(3)) Expect(len(results.OutputToStringArray())).To(Equal(3))
}) })
It("tail two lines", func() { It("tail two lines: "+log, func() {
logc := podmanTest.Podman([]string{"run", "-dt", ALPINE, "sh", "-c", "echo podman; echo podman; echo podman"}) logc := podmanTest.Podman([]string{"run", "--log-driver", log, "-dt", ALPINE, "sh", "-c", "echo podman; echo podman; echo podman"})
logc.WaitWithDefaultTimeout() logc.WaitWithDefaultTimeout()
Expect(logc).To(Exit(0)) Expect(logc).To(Exit(0))
cid := logc.OutputToString() cid := logc.OutputToString()
@ -60,8 +61,8 @@ var _ = Describe("Podman logs", func() {
Expect(len(results.OutputToStringArray())).To(Equal(2)) Expect(len(results.OutputToStringArray())).To(Equal(2))
}) })
It("tail zero lines", func() { It("tail zero lines: "+log, func() {
logc := podmanTest.Podman([]string{"run", "-dt", ALPINE, "sh", "-c", "echo podman; echo podman; echo podman"}) logc := podmanTest.Podman([]string{"run", "--log-driver", log, "-dt", ALPINE, "sh", "-c", "echo podman; echo podman; echo podman"})
logc.WaitWithDefaultTimeout() logc.WaitWithDefaultTimeout()
Expect(logc).To(Exit(0)) Expect(logc).To(Exit(0))
cid := logc.OutputToString() cid := logc.OutputToString()
@ -72,8 +73,8 @@ var _ = Describe("Podman logs", func() {
Expect(len(results.OutputToStringArray())).To(Equal(0)) Expect(len(results.OutputToStringArray())).To(Equal(0))
}) })
It("tail 800 lines", func() { It("tail 800 lines: "+log, func() {
logc := podmanTest.Podman([]string{"run", "-dt", ALPINE, "sh", "-c", "i=1; while [ \"$i\" -ne 1000 ]; do echo \"line $i\"; i=$((i + 1)); done"}) logc := podmanTest.Podman([]string{"run", "--log-driver", log, "-dt", ALPINE, "sh", "-c", "i=1; while [ \"$i\" -ne 1000 ]; do echo \"line $i\"; i=$((i + 1)); done"})
logc.WaitWithDefaultTimeout() logc.WaitWithDefaultTimeout()
Expect(logc).To(Exit(0)) Expect(logc).To(Exit(0))
cid := logc.OutputToString() cid := logc.OutputToString()
@ -84,8 +85,8 @@ var _ = Describe("Podman logs", func() {
Expect(len(results.OutputToStringArray())).To(Equal(800)) Expect(len(results.OutputToStringArray())).To(Equal(800))
}) })
It("tail 2 lines with timestamps", func() { It("tail 2 lines with timestamps: "+log, func() {
logc := podmanTest.Podman([]string{"run", "-dt", ALPINE, "sh", "-c", "echo podman; echo podman; echo podman"}) logc := podmanTest.Podman([]string{"run", "--log-driver", log, "-dt", ALPINE, "sh", "-c", "echo podman; echo podman; echo podman"})
logc.WaitWithDefaultTimeout() logc.WaitWithDefaultTimeout()
Expect(logc).To(Exit(0)) Expect(logc).To(Exit(0))
cid := logc.OutputToString() cid := logc.OutputToString()
@ -96,8 +97,8 @@ var _ = Describe("Podman logs", func() {
Expect(len(results.OutputToStringArray())).To(Equal(2)) Expect(len(results.OutputToStringArray())).To(Equal(2))
}) })
It("since time 2017-08-07", func() { It("since time 2017-08-07: "+log, func() {
logc := podmanTest.Podman([]string{"run", "-dt", ALPINE, "sh", "-c", "echo podman; echo podman; echo podman"}) logc := podmanTest.Podman([]string{"run", "--log-driver", log, "-dt", ALPINE, "sh", "-c", "echo podman; echo podman; echo podman"})
logc.WaitWithDefaultTimeout() logc.WaitWithDefaultTimeout()
Expect(logc).To(Exit(0)) Expect(logc).To(Exit(0))
cid := logc.OutputToString() cid := logc.OutputToString()
@ -108,8 +109,8 @@ var _ = Describe("Podman logs", func() {
Expect(len(results.OutputToStringArray())).To(Equal(3)) Expect(len(results.OutputToStringArray())).To(Equal(3))
}) })
It("since duration 10m", func() { It("since duration 10m: "+log, func() {
logc := podmanTest.Podman([]string{"run", "-dt", ALPINE, "sh", "-c", "echo podman; echo podman; echo podman"}) logc := podmanTest.Podman([]string{"run", "--log-driver", log, "-dt", ALPINE, "sh", "-c", "echo podman; echo podman; echo podman"})
logc.WaitWithDefaultTimeout() logc.WaitWithDefaultTimeout()
Expect(logc).To(Exit(0)) Expect(logc).To(Exit(0))
cid := logc.OutputToString() cid := logc.OutputToString()
@ -120,20 +121,20 @@ var _ = Describe("Podman logs", func() {
Expect(len(results.OutputToStringArray())).To(Equal(3)) Expect(len(results.OutputToStringArray())).To(Equal(3))
}) })
It("latest and container name should fail", func() { It("latest and container name should fail: "+log, func() {
results := podmanTest.Podman([]string{"logs", "-l", "foobar"}) results := podmanTest.Podman([]string{"logs", "-l", "foobar"})
results.WaitWithDefaultTimeout() results.WaitWithDefaultTimeout()
Expect(results).To(ExitWithError()) Expect(results).To(ExitWithError())
}) })
It("two containers showing short container IDs", func() { It("two containers showing short container IDs: "+log, func() {
SkipIfRemote("FIXME: podman-remote logs does not support showing two containers at the same time") SkipIfRemote("FIXME: podman-remote logs does not support showing two containers at the same time")
log1 := podmanTest.Podman([]string{"run", "-dt", ALPINE, "sh", "-c", "echo podman; echo podman; echo podman"}) log1 := podmanTest.Podman([]string{"run", "--log-driver", log, "-dt", ALPINE, "sh", "-c", "echo podman; echo podman; echo podman"})
log1.WaitWithDefaultTimeout() log1.WaitWithDefaultTimeout()
Expect(log1.ExitCode()).To(Equal(0)) Expect(log1.ExitCode()).To(Equal(0))
cid1 := log1.OutputToString() cid1 := log1.OutputToString()
log2 := podmanTest.Podman([]string{"run", "-dt", ALPINE, "sh", "-c", "echo podman; echo podman; echo podman"}) log2 := podmanTest.Podman([]string{"run", "--log-driver", log, "-dt", ALPINE, "sh", "-c", "echo podman; echo podman; echo podman"})
log2.WaitWithDefaultTimeout() log2.WaitWithDefaultTimeout()
Expect(log2.ExitCode()).To(Equal(0)) Expect(log2.ExitCode()).To(Equal(0))
cid2 := log2.OutputToString() cid2 := log2.OutputToString()
@ -147,7 +148,7 @@ var _ = Describe("Podman logs", func() {
Expect(strings.Contains(output[0], cid1[:12]) || strings.Contains(output[0], cid2[:12])).To(BeTrue()) Expect(strings.Contains(output[0], cid1[:12]) || strings.Contains(output[0], cid2[:12])).To(BeTrue())
}) })
It("podman logs on a created container should result in 0 exit code", func() { It("podman logs on a created container should result in 0 exit code: "+log, func() {
session := podmanTest.Podman([]string{"create", "-t", "--name", "log", ALPINE}) session := podmanTest.Podman([]string{"create", "-t", "--name", "log", ALPINE})
session.WaitWithDefaultTimeout() session.WaitWithDefaultTimeout()
Expect(session).To(Exit(0)) Expect(session).To(Exit(0))
@ -157,44 +158,8 @@ var _ = Describe("Podman logs", func() {
Expect(results).To(Exit(0)) Expect(results).To(Exit(0))
}) })
It("using journald for container with container tag", func() { It("for container: "+log, func() {
Skip("need to verify images have correct packages for journald") logc := podmanTest.Podman([]string{"run", "--log-driver", log, "-dt", ALPINE, "sh", "-c", "echo podman; echo podman; echo podman"})
logc := podmanTest.Podman([]string{"run", "--log-driver", "journald", "--log-opt=tag={{.ImageName}}", "-d", ALPINE, "sh", "-c", "echo podman; sleep 0.1; echo podman; sleep 0.1; echo podman"})
logc.WaitWithDefaultTimeout()
Expect(logc).To(Exit(0))
cid := logc.OutputToString()
wait := podmanTest.Podman([]string{"wait", "-l"})
wait.WaitWithDefaultTimeout()
Expect(wait).To(Exit(0))
cmd := exec.Command("journalctl", "--no-pager", "-o", "json", "--output-fields=CONTAINER_TAG", "-u", fmt.Sprintf("libpod-conmon-%s.scope", cid))
out, err := cmd.CombinedOutput()
Expect(err).To(BeNil())
Expect(string(out)).To(ContainSubstring("alpine"))
})
It("using journald for container name", func() {
Skip("need to verify images have correct packages for journald")
containerName := "inside-journal"
logc := podmanTest.Podman([]string{"run", "--log-driver", "journald", "-d", "--name", containerName, ALPINE, "sh", "-c", "echo podman; sleep 0.1; echo podman; sleep 0.1; echo podman"})
logc.WaitWithDefaultTimeout()
Expect(logc).To(Exit(0))
cid := logc.OutputToString()
wait := podmanTest.Podman([]string{"wait", "-l"})
wait.WaitWithDefaultTimeout()
Expect(wait).To(Exit(0))
cmd := exec.Command("journalctl", "--no-pager", "-o", "json", "--output-fields=CONTAINER_NAME", "-u", fmt.Sprintf("libpod-conmon-%s.scope", cid))
out, err := cmd.CombinedOutput()
Expect(err).To(BeNil())
Expect(string(out)).To(ContainSubstring(containerName))
})
It("using journald for container", func() {
Skip("need to verify images have correct packages for journald")
logc := podmanTest.Podman([]string{"run", "--log-driver", "journald", "-dt", ALPINE, "sh", "-c", "echo podman; echo podman; echo podman"})
logc.WaitWithDefaultTimeout() logc.WaitWithDefaultTimeout()
Expect(logc).To(Exit(0)) Expect(logc).To(Exit(0))
cid := logc.OutputToString() cid := logc.OutputToString()
@ -206,9 +171,8 @@ var _ = Describe("Podman logs", func() {
Expect(results.OutputToString()).To(Equal("podman podman podman")) Expect(results.OutputToString()).To(Equal("podman podman podman"))
}) })
It("using journald tail two lines", func() { It("tail two lines: "+log, func() {
Skip("need to verify images have correct packages for journald") logc := podmanTest.Podman([]string{"run", "--log-driver", log, "-dt", ALPINE, "sh", "-c", "echo podman; echo podman; echo podman"})
logc := podmanTest.Podman([]string{"run", "--log-driver", "journald", "-dt", ALPINE, "sh", "-c", "echo podman; echo podman; echo podman"})
logc.WaitWithDefaultTimeout() logc.WaitWithDefaultTimeout()
Expect(logc).To(Exit(0)) Expect(logc).To(Exit(0))
cid := logc.OutputToString() cid := logc.OutputToString()
@ -218,9 +182,8 @@ var _ = Describe("Podman logs", func() {
Expect(len(results.OutputToStringArray())).To(Equal(2)) Expect(len(results.OutputToStringArray())).To(Equal(2))
}) })
It("using journald tail 99 lines", func() { It("tail 99 lines: "+log, func() {
Skip("need to verify images have correct packages for journald") logc := podmanTest.Podman([]string{"run", "--log-driver", log, "-dt", ALPINE, "sh", "-c", "echo podman; echo podman; echo podman"})
logc := podmanTest.Podman([]string{"run", "--log-driver", "journald", "-dt", ALPINE, "sh", "-c", "echo podman; echo podman; echo podman"})
logc.WaitWithDefaultTimeout() logc.WaitWithDefaultTimeout()
Expect(logc).To(Exit(0)) Expect(logc).To(Exit(0))
cid := logc.OutputToString() cid := logc.OutputToString()
@ -231,9 +194,8 @@ var _ = Describe("Podman logs", func() {
Expect(len(results.OutputToStringArray())).To(Equal(3)) Expect(len(results.OutputToStringArray())).To(Equal(3))
}) })
It("using journald tail 2 lines with timestamps", func() { It("tail 2 lines with timestamps: "+log, func() {
Skip("need to verify images have correct packages for journald") logc := podmanTest.Podman([]string{"run", "--log-driver", log, "-dt", ALPINE, "sh", "-c", "echo podman; echo podman; echo podman"})
logc := podmanTest.Podman([]string{"run", "--log-driver", "journald", "-dt", ALPINE, "sh", "-c", "echo podman; echo podman; echo podman"})
logc.WaitWithDefaultTimeout() logc.WaitWithDefaultTimeout()
Expect(logc).To(Exit(0)) Expect(logc).To(Exit(0))
cid := logc.OutputToString() cid := logc.OutputToString()
@ -244,9 +206,8 @@ var _ = Describe("Podman logs", func() {
Expect(len(results.OutputToStringArray())).To(Equal(2)) Expect(len(results.OutputToStringArray())).To(Equal(2))
}) })
It("using journald since time 2017-08-07", func() { It("since time 2017-08-07: "+log, func() {
Skip("need to verify images have correct packages for journald") logc := podmanTest.Podman([]string{"run", "--log-driver", log, "-dt", ALPINE, "sh", "-c", "echo podman; echo podman; echo podman"})
logc := podmanTest.Podman([]string{"run", "--log-driver", "journald", "-dt", ALPINE, "sh", "-c", "echo podman; echo podman; echo podman"})
logc.WaitWithDefaultTimeout() logc.WaitWithDefaultTimeout()
Expect(logc).To(Exit(0)) Expect(logc).To(Exit(0))
cid := logc.OutputToString() cid := logc.OutputToString()
@ -257,9 +218,8 @@ var _ = Describe("Podman logs", func() {
Expect(len(results.OutputToStringArray())).To(Equal(3)) Expect(len(results.OutputToStringArray())).To(Equal(3))
}) })
It("using journald with duration 10m", func() { It("with duration 10m: "+log, func() {
Skip("need to verify images have correct packages for journald") logc := podmanTest.Podman([]string{"run", "--log-driver", log, "-dt", ALPINE, "sh", "-c", "echo podman; echo podman; echo podman"})
logc := podmanTest.Podman([]string{"run", "--log-driver", "journald", "-dt", ALPINE, "sh", "-c", "echo podman; echo podman; echo podman"})
logc.WaitWithDefaultTimeout() logc.WaitWithDefaultTimeout()
Expect(logc).To(Exit(0)) Expect(logc).To(Exit(0))
cid := logc.OutputToString() cid := logc.OutputToString()
@ -270,10 +230,10 @@ var _ = Describe("Podman logs", func() {
Expect(len(results.OutputToStringArray())).To(Equal(3)) Expect(len(results.OutputToStringArray())).To(Equal(3))
}) })
It("streaming output", func() { It("streaming output: "+log, func() {
containerName := "logs-f-rm" containerName := "logs-f-rm"
logc := podmanTest.Podman([]string{"run", "--rm", "--name", containerName, "-dt", ALPINE, "sh", "-c", "echo podman; sleep 1; echo podman"}) logc := podmanTest.Podman([]string{"run", "--log-driver", log, "--rm", "--name", containerName, "-dt", ALPINE, "sh", "-c", "echo podman; sleep 1; echo podman"})
logc.WaitWithDefaultTimeout() logc.WaitWithDefaultTimeout()
Expect(logc).To(Exit(0)) Expect(logc).To(Exit(0))
@ -299,9 +259,9 @@ var _ = Describe("Podman logs", func() {
} }
}) })
It("podman logs with log-driver=none errors", func() { It("podman logs with log-driver=none errors: "+log, func() {
ctrName := "logsctr" ctrName := "logsctr"
logc := podmanTest.Podman([]string{"run", "--name", ctrName, "-d", "--log-driver", "none", ALPINE, "top"}) logc := podmanTest.Podman([]string{"run", "--log-driver", log, "--name", ctrName, "-d", "--log-driver", "none", ALPINE, "top"})
logc.WaitWithDefaultTimeout() logc.WaitWithDefaultTimeout()
Expect(logc).To(Exit(0)) Expect(logc).To(Exit(0))
@ -310,10 +270,10 @@ var _ = Describe("Podman logs", func() {
Expect(logs).To(Not(Exit(0))) Expect(logs).To(Not(Exit(0)))
}) })
It("follow output stopped container", func() { It("follow output stopped container: "+log, func() {
containerName := "logs-f" containerName := "logs-f"
logc := podmanTest.Podman([]string{"run", "--name", containerName, "-d", ALPINE, "true"}) logc := podmanTest.Podman([]string{"run", "--log-driver", log, "--name", containerName, "-d", ALPINE, "true"})
logc.WaitWithDefaultTimeout() logc.WaitWithDefaultTimeout()
Expect(logc).To(Exit(0)) Expect(logc).To(Exit(0))
@ -322,8 +282,8 @@ var _ = Describe("Podman logs", func() {
Expect(results).To(Exit(0)) Expect(results).To(Exit(0))
}) })
It("using container with container log-size", func() { It("using container with container log-size: "+log, func() {
logc := podmanTest.Podman([]string{"run", "--log-opt=max-size=10k", "-d", ALPINE, "sh", "-c", "echo podman podman podman"}) logc := podmanTest.Podman([]string{"run", "--log-driver", log, "--log-opt=max-size=10k", "-d", ALPINE, "sh", "-c", "echo podman podman podman"})
logc.WaitWithDefaultTimeout() logc.WaitWithDefaultTimeout()
Expect(logc).To(Exit(0)) Expect(logc).To(Exit(0))
cid := logc.OutputToString() cid := logc.OutputToString()
@ -343,8 +303,8 @@ var _ = Describe("Podman logs", func() {
Expect(results.OutputToString()).To(Equal("podman podman podman")) Expect(results.OutputToString()).To(Equal("podman podman podman"))
}) })
It("Make sure logs match expected length", func() { It("Make sure logs match expected length: "+log, func() {
logc := podmanTest.Podman([]string{"run", "-t", "--name", "test", ALPINE, "sh", "-c", "echo 1; echo 2"}) logc := podmanTest.Podman([]string{"run", "--log-driver", log, "-t", "--name", "test", ALPINE, "sh", "-c", "echo 1; echo 2"})
logc.WaitWithDefaultTimeout() logc.WaitWithDefaultTimeout()
Expect(logc).To(Exit(0)) Expect(logc).To(Exit(0))
@ -361,9 +321,9 @@ var _ = Describe("Podman logs", func() {
Expect(outlines[1]).To(Equal("2\r")) Expect(outlines[1]).To(Equal("2\r"))
}) })
It("podman logs test stdout and stderr", func() { It("podman logs test stdout and stderr: "+log, func() {
cname := "log-test" cname := "log-test"
logc := podmanTest.Podman([]string{"run", "--name", cname, ALPINE, "sh", "-c", "echo stdout; echo stderr >&2"}) logc := podmanTest.Podman([]string{"run", "--log-driver", log, "--name", cname, ALPINE, "sh", "-c", "echo stdout; echo stderr >&2"})
logc.WaitWithDefaultTimeout() logc.WaitWithDefaultTimeout()
Expect(logc).To(Exit(0)) Expect(logc).To(Exit(0))
@ -377,4 +337,40 @@ var _ = Describe("Podman logs", func() {
Expect(results.OutputToString()).To(Equal("stdout")) Expect(results.OutputToString()).To(Equal("stdout"))
Expect(results.ErrorToString()).To(Equal("stderr")) Expect(results.ErrorToString()).To(Equal("stderr"))
}) })
}
It("using journald for container with container tag", func() {
SkipIfInContainer("journalctl inside a container doesn't work correctly")
logc := podmanTest.Podman([]string{"run", "--log-driver", "journald", "--log-opt=tag={{.ImageName}}", "-d", ALPINE, "sh", "-c", "echo podman; sleep 0.1; echo podman; sleep 0.1; echo podman"})
logc.WaitWithDefaultTimeout()
Expect(logc).To(Exit(0))
cid := logc.OutputToString()
wait := podmanTest.Podman([]string{"wait", cid})
wait.WaitWithDefaultTimeout()
Expect(wait).To(Exit(0))
cmd := exec.Command("journalctl", "--no-pager", "-o", "json", "--output-fields=CONTAINER_TAG", fmt.Sprintf("CONTAINER_ID_FULL=%s", cid))
out, err := cmd.CombinedOutput()
Expect(err).To(BeNil())
Expect(string(out)).To(ContainSubstring("alpine"))
})
It("using journald container name", func() {
SkipIfInContainer("journalctl inside a container doesn't work correctly")
containerName := "inside-journal"
logc := podmanTest.Podman([]string{"run", "--log-driver", "journald", "-d", "--name", containerName, ALPINE, "sh", "-c", "echo podman; sleep 0.1; echo podman; sleep 0.1; echo podman"})
logc.WaitWithDefaultTimeout()
Expect(logc).To(Exit(0))
cid := logc.OutputToString()
wait := podmanTest.Podman([]string{"wait", cid})
wait.WaitWithDefaultTimeout()
Expect(wait).To(Exit(0))
cmd := exec.Command("journalctl", "--no-pager", "-o", "json", "--output-fields=CONTAINER_NAME", fmt.Sprintf("CONTAINER_ID_FULL=%s", cid))
out, err := cmd.CombinedOutput()
Expect(err).To(BeNil())
Expect(string(out)).To(ContainSubstring(containerName))
})
}) })

View File

@ -482,3 +482,13 @@ func RandomString(n int) string {
} }
return string(b) return string(b)
} }
//SkipIfInContainer skips a test if the test is run inside a container
func SkipIfInContainer(reason string) {
if len(reason) < 5 {
panic("SkipIfInContainer must specify a reason to skip")
}
if os.Getenv("TEST_ENVIRON") == "container" {
Skip("[container]: " + reason)
}
}