Skip to content

Commit

Permalink
podman pod logs -l no longer panics
Browse files Browse the repository at this point in the history
Fixed issue where executing the command `podman pod logs -l` would panic
because it was indexing into an empty arguments array.

Signed-off-by: Jake Correnti <jcorrenti13@gmail.com>
  • Loading branch information
Jake Correnti committed Sep 19, 2022
1 parent 4e14fa0 commit 8c6372f
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 5 deletions.
7 changes: 6 additions & 1 deletion cmd/podman/pods/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,10 @@ func logs(_ *cobra.Command, args []string) error {

logsPodOptions.StdoutWriter = os.Stdout
logsPodOptions.StderrWriter = os.Stderr
return registry.ContainerEngine().PodLogs(registry.GetContext(), args[0], logsPodOptions.PodLogsOptions)

podName := ""
if len(args) > 0 {
podName = args[0]
}
return registry.ContainerEngine().PodLogs(registry.GetContext(), podName, logsPodOptions.PodLogsOptions)
}
30 changes: 26 additions & 4 deletions pkg/domain/infra/abi/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"os"
"regexp"
"strconv"
"sync"
"time"
Expand Down Expand Up @@ -77,10 +78,31 @@ func getContainersAndInputByContext(all, latest bool, names []string, filters ma
}
}
case latest:
ctr, err = runtime.GetLatestContainer()
if err == nil {
rawInput = append(rawInput, ctr.ID())
ctrs = append(ctrs, ctr)
isPod := false
for _, name := range names {
obj := regexp.MustCompile("^[a-zA-Z0-9_.-]{12}-infra")
if obj.MatchString(name) {
isPod = true
break
}
}
if isPod {
pod, err := runtime.GetLatestPod()
if err == nil {
podCtrs, err := pod.AllContainers()
if err == nil {
for _, c := range podCtrs {
rawInput = append(rawInput, c.ID())
}
ctrs = podCtrs
}
}
} else {
ctr, err = runtime.GetLatestContainer()
if err == nil {
rawInput = append(rawInput, ctr.ID())
ctrs = append(ctrs, ctr)
}
}
default:
for _, n := range names {
Expand Down
62 changes: 62 additions & 0 deletions test/e2e/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,4 +490,66 @@ var _ = Describe("Podman logs", func() {
Expect(output[0]).To(MatchRegexp(`\x1b\[3[0-9a-z ]+\x1b\[0m`))
Expect(output[1]).To(MatchRegexp(`\x1b\[3[0-9a-z ]+\x1b\[0m`))
})
It("podman pod logs -l", func() {
SkipIfRemote("Remote can only process one container at a time")
SkipIfInContainer("journalctl inside a container doesn't work correctly")
podName := "testPod"
containerName1 := "container1"
containerName2 := "container2"

testPod := podmanTest.Podman([]string{"pod", "create", fmt.Sprintf("--name=%s", podName)})
testPod.WaitWithDefaultTimeout()
Expect(testPod).To(Exit(0))

log1 := podmanTest.Podman([]string{"run", "--name", containerName1, "-d", "--pod", podName, BB, "/bin/sh", "-c", "echo log1"})
log1.WaitWithDefaultTimeout()
Expect(log1).To(Exit(0))

log2 := podmanTest.Podman([]string{"run", "--name", containerName2, "-d", "--pod", podName, BB, "/bin/sh", "-c", "echo log2"})
log2.WaitWithDefaultTimeout()
Expect(log2).To(Exit(0))

results := podmanTest.Podman([]string{"pod", "logs", "-l"})
results.WaitWithDefaultTimeout()
Expect(results).To(Exit(0))
output := results.OutputToString()
Expect(output).To(ContainSubstring("log1"))
Expect(output).To(ContainSubstring("log2"))
})
It("podman pod logs -l with newer container created", func() {
SkipIfRemote("Remote can only process one container at a time")
SkipIfInContainer("journalctl inside a container doesn't work correctly")
podName := "testPod"
containerName1 := "container1"
containerName2 := "container2"
containerName3 := "container3"

testPod := podmanTest.Podman([]string{"pod", "create", fmt.Sprintf("--name=%s", podName)})
testPod.WaitWithDefaultTimeout()
Expect(testPod).To(Exit(0))

log1 := podmanTest.Podman([]string{"run", "--name", containerName1, "-d", "--pod", podName, BB, "/bin/sh", "-c", "echo log1"})
log1.WaitWithDefaultTimeout()
Expect(log1).To(Exit(0))

log2 := podmanTest.Podman([]string{"run", "--name", containerName2, "-d", "--pod", podName, BB, "/bin/sh", "-c", "echo log2"})
log2.WaitWithDefaultTimeout()
Expect(log2).To(Exit(0))

ctr := podmanTest.Podman([]string{"run", "--name", containerName3, "-d", BB, "date"})
ctr.WaitWithDefaultTimeout()
Expect(ctr).To(Exit(0))

results := podmanTest.Podman([]string{"pod", "logs", "-l"})
results.WaitWithDefaultTimeout()
Expect(results).To(Exit(0))
podOutput := results.OutputToString()

results = podmanTest.Podman([]string{"logs", "-l"})
results.WaitWithDefaultTimeout()
Expect(results).To(Exit(0))
ctrOutput := results.OutputToString()

Expect(podOutput).ToNot(Equal(ctrOutput))
})
})

0 comments on commit 8c6372f

Please sign in to comment.