Skip to content

Commit

Permalink
test/e2e: switch to GinkgoWriter
Browse files Browse the repository at this point in the history
Directly writing to stdout/err is not safe when run in parallel.
Ginkgo v2 fixed this buffering the output and syncing the output so it
is not mangled between tests.

This means we should use the GinkgoWriter everywhere to make sure the
output stays in sync.

Signed-off-by: Paul Holzinger <[email protected]>
  • Loading branch information
Luap99 committed Apr 13, 2023
1 parent 8331cd2 commit de55faa
Show file tree
Hide file tree
Showing 19 changed files with 68 additions and 72 deletions.
48 changes: 24 additions & 24 deletions test/e2e/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ var _ = SynchronizedBeforeSuite(func() []byte {
// make cache dir
ImageCacheDir = filepath.Join(os.TempDir(), "imagecachedir")
if err := os.MkdirAll(ImageCacheDir, 0700); err != nil {
fmt.Printf("%q\n", err)
GinkgoWriter.Printf("%q\n", err)
os.Exit(1)
}

Expand All @@ -122,7 +122,7 @@ var _ = SynchronizedBeforeSuite(func() []byte {
}

if err := os.MkdirAll(filepath.Join(ImageCacheDir, podman.ImageCacheFS+"-images"), 0777); err != nil {
fmt.Printf("%q\n", err)
GinkgoWriter.Printf("%q\n", err)
os.Exit(1)
}
podman.Root = ImageCacheDir
Expand All @@ -134,19 +134,19 @@ var _ = SynchronizedBeforeSuite(func() []byte {
if host.Distribution == "rhel" && strings.HasPrefix(host.Version, "7") {
f, err := os.OpenFile("/proc/sys/user/max_user_namespaces", os.O_WRONLY, 0644)
if err != nil {
fmt.Println("Unable to enable userspace on RHEL 7")
GinkgoWriter.Println("Unable to enable userspace on RHEL 7")
os.Exit(1)
}
_, err = f.WriteString("15000")
if err != nil {
fmt.Println("Unable to enable userspace on RHEL 7")
GinkgoWriter.Println("Unable to enable userspace on RHEL 7")
os.Exit(1)
}
f.Close()
}
path, err := os.MkdirTemp("", "libpodlock")
if err != nil {
fmt.Println(err)
GinkgoWriter.Println(err)
os.Exit(1)
}

Expand All @@ -170,9 +170,9 @@ func (p *PodmanTestIntegration) Setup() {
var _ = SynchronizedAfterSuite(func() {},
func() {
sort.Sort(testResultsSortedLength{testResults})
fmt.Println("integration timing results")
GinkgoWriter.Println("integration timing results")
for _, result := range testResults {
fmt.Printf("%s\t\t%f\n", result.name, result.length)
GinkgoWriter.Printf("%s\t\t%f\n", result.name, result.length)
}

// previous runroot
Expand All @@ -184,7 +184,7 @@ var _ = SynchronizedAfterSuite(func() {},
defer os.RemoveAll(tempdir)

if err := os.RemoveAll(podmanTest.Root); err != nil {
fmt.Printf("%q\n", err)
GinkgoWriter.Printf("%q\n", err)
}

// If running remote, we need to stop the associated podman system service
Expand Down Expand Up @@ -376,17 +376,17 @@ func (p *PodmanTestIntegration) createArtifact(image string) {
}
destName := imageTarPath(image)
if _, err := os.Stat(destName); os.IsNotExist(err) {
fmt.Printf("Caching %s at %s...\n", image, destName)
GinkgoWriter.Printf("Caching %s at %s...\n", image, destName)
pull := p.PodmanNoCache([]string{"pull", image})
pull.Wait(440)
Expect(pull).Should(Exit(0))

save := p.PodmanNoCache([]string{"save", "-o", destName, image})
save.Wait(90)
Expect(save).Should(Exit(0))
fmt.Printf("\n")
GinkgoWriter.Printf("\n")
} else {
fmt.Printf("[image already cached: %s]\n", destName)
GinkgoWriter.Printf("[image already cached: %s]\n", destName)
}
}

Expand Down Expand Up @@ -419,7 +419,7 @@ func GetPortLock(port string) *lockfile.LockFile {
lockFile := filepath.Join(LockTmpDir, port)
lock, err := lockfile.GetLockFile(lockFile)
if err != nil {
fmt.Println(err)
GinkgoWriter.Println(err)
os.Exit(1)
}
lock.Lock()
Expand Down Expand Up @@ -534,7 +534,7 @@ func (p *PodmanTestIntegration) BuildImageWithLabel(dockerfile, imageName string
// PodmanPID execs podman and returns its PID
func (p *PodmanTestIntegration) PodmanPID(args []string) (*PodmanSessionIntegration, int) {
podmanOptions := p.MakeOptions(args, false, false)
fmt.Printf("Running: %s %s\n", p.PodmanBinary, strings.Join(podmanOptions, " "))
GinkgoWriter.Printf("Running: %s %s\n", p.PodmanBinary, strings.Join(podmanOptions, " "))

command := exec.Command(p.PodmanBinary, podmanOptions...)
session, err := Start(command, GinkgoWriter, GinkgoWriter)
Expand All @@ -546,7 +546,7 @@ func (p *PodmanTestIntegration) PodmanPID(args []string) (*PodmanSessionIntegrat
}

func (p *PodmanTestIntegration) Quadlet(args []string, sourceDir string) *PodmanSessionIntegration {
fmt.Printf("Running: %s %s with QUADLET_UNIT_DIRS=%s\n", p.QuadletBinary, strings.Join(args, " "), sourceDir)
GinkgoWriter.Printf("Running: %s %s with QUADLET_UNIT_DIRS=%s\n", p.QuadletBinary, strings.Join(args, " "), sourceDir)

command := exec.Command(p.QuadletBinary, args...)
command.Env = []string{fmt.Sprintf("QUADLET_UNIT_DIRS=%s", sourceDir)}
Expand Down Expand Up @@ -607,7 +607,7 @@ func (p *PodmanTestIntegration) CleanupSecrets() {

// Nuke tempdir
if err := os.RemoveAll(p.TempDir); err != nil {
fmt.Printf("%q\n", err)
GinkgoWriter.Printf("%q\n", err)
}
}

Expand Down Expand Up @@ -667,15 +667,15 @@ func (p *PodmanTestIntegration) RunHealthCheck(cid string) error {
ps.WaitWithDefaultTimeout()
if ps.ExitCode() == 0 {
if !strings.Contains(ps.OutputToString(), cid) {
fmt.Printf("Container %s is not running, restarting", cid)
GinkgoWriter.Printf("Container %s is not running, restarting", cid)
restart := p.Podman([]string{"restart", cid})
restart.WaitWithDefaultTimeout()
if restart.ExitCode() != 0 {
return fmt.Errorf("unable to restart %s", cid)
}
}
}
fmt.Printf("Waiting for %s to pass healthcheck\n", cid)
GinkgoWriter.Printf("Waiting for %s to pass healthcheck\n", cid)
time.Sleep(1 * time.Second)
}
return fmt.Errorf("unable to detect %s as running", cid)
Expand Down Expand Up @@ -865,7 +865,7 @@ func (p *PodmanTestIntegration) RestartRemoteService() {
func (p *PodmanTestIntegration) RestoreArtifactToCache(image string) error {
tarball := imageTarPath(image)
if _, err := os.Stat(tarball); err == nil {
fmt.Printf("Restoring %s...\n", image)
GinkgoWriter.Printf("Restoring %s...\n", image)
p.Root = p.ImageCacheDir
restore := p.PodmanNoEvents([]string{"load", "-q", "-i", tarball})
restore.WaitWithDefaultTimeout()
Expand All @@ -879,7 +879,7 @@ func populateCache(podman *PodmanTestIntegration) {
Expect(err).ToNot(HaveOccurred())
}
// logformatter uses this to recognize the first test
fmt.Printf("-----------------------------\n")
GinkgoWriter.Printf("-----------------------------\n")
}

func (p *PodmanTestIntegration) removeCache(path string) {
Expand All @@ -894,7 +894,7 @@ func (p *PodmanTestIntegration) removeCache(path string) {
}
} else {
if err := os.RemoveAll(path); err != nil {
fmt.Printf("%q\n", err)
GinkgoWriter.Printf("%q\n", err)
}
}
}
Expand Down Expand Up @@ -951,17 +951,17 @@ func (p *PodmanTestIntegration) makeOptions(args []string, noEvents, noCache boo
func writeConf(conf []byte, confPath string) {
if _, err := os.Stat(filepath.Dir(confPath)); os.IsNotExist(err) {
if err := os.MkdirAll(filepath.Dir(confPath), 0o777); err != nil {
fmt.Println(err)
GinkgoWriter.Println(err)
}
}
if err := os.WriteFile(confPath, conf, 0o777); err != nil {
fmt.Println(err)
GinkgoWriter.Println(err)
}
}

func removeConf(confPath string) {
if err := os.Remove(confPath); err != nil {
fmt.Println(err)
GinkgoWriter.Println(err)
}
}

Expand Down Expand Up @@ -1138,7 +1138,7 @@ func ncz(port int) bool {
timeout := 500 * time.Millisecond
for i := 0; i < 5; i++ {
ncCmd := []string{"-z", "localhost", fmt.Sprintf("%d", port)}
fmt.Printf("Running: nc %s\n", strings.Join(ncCmd, " "))
GinkgoWriter.Printf("Running: nc %s\n", strings.Join(ncCmd, " "))
check := SystemExec("nc", ncCmd)
if check.ExitCode() == 0 {
return true
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ var _ = Describe("Podman events", func() {
result.WaitWithDefaultTimeout()
Expect(result).Should(Exit(0))
events := result.OutputToStringArray()
fmt.Println(events)
GinkgoWriter.Println(events)
Expect(len(events)).To(BeNumerically(">=", 2), "Number of events")
Expect(events).To(ContainElement(ContainSubstring(" pod create ")))
Expect(events).To(ContainElement(ContainSubstring(" pod stop ")))
Expand Down
6 changes: 3 additions & 3 deletions test/e2e/libpod_suite_remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (p *PodmanTestIntegration) StartRemoteService() {
command := exec.Command(p.PodmanBinary, podmanOptions...)
command.Stdout = os.Stdout
command.Stderr = os.Stderr
fmt.Printf("Running: %s %s\n", p.PodmanBinary, strings.Join(podmanOptions, " "))
GinkgoWriter.Printf("Running: %s %s\n", p.PodmanBinary, strings.Join(podmanOptions, " "))
err := command.Start()
Expect(err).ToNot(HaveOccurred())
command.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
Expand Down Expand Up @@ -145,11 +145,11 @@ func getRemoteOptions(p *PodmanTestIntegration, args []string) []string {
func (p *PodmanTestIntegration) RestoreArtifact(image string) error {
tarball := imageTarPath(image)
if _, err := os.Stat(tarball); err == nil {
fmt.Printf("Restoring %s...\n", image)
GinkgoWriter.Printf("Restoring %s...\n", image)
args := []string{"load", "-q", "-i", tarball}
podmanOptions := getRemoteOptions(p, args)
command := exec.Command(p.PodmanBinary, podmanOptions...)
fmt.Printf("Running: %s %s\n", p.PodmanBinary, strings.Join(podmanOptions, " "))
GinkgoWriter.Printf("Running: %s %s\n", p.PodmanBinary, strings.Join(podmanOptions, " "))
if err := command.Start(); err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/libpod_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
package integration

import (
"fmt"
"os"
"path/filepath"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

Expand Down Expand Up @@ -62,7 +62,7 @@ func PodmanTestCreate(tempDir string) *PodmanTestIntegration {
func (p *PodmanTestIntegration) RestoreArtifact(image string) error {
tarball := imageTarPath(image)
if _, err := os.Stat(tarball); err == nil {
fmt.Printf("Restoring %s...\n", image)
GinkgoWriter.Printf("Restoring %s...\n", image)
restore := p.PodmanNoEvents([]string{"load", "-q", "-i", tarball})
restore.Wait(90)
}
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var _ = Describe("Podman load", func() {

images := podmanTest.Podman([]string{"images"})
images.WaitWithDefaultTimeout()
fmt.Println(images.OutputToStringArray())
GinkgoWriter.Println(images.OutputToStringArray())

save := podmanTest.Podman([]string{"save", "-o", outfile, ALPINE})
save.WaitWithDefaultTimeout()
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/login_logout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ var _ = Describe("Podman login and logout", func() {
var authInfo map[string]interface{}
err = json.Unmarshal(authBytes, &authInfo)
Expect(err).ToNot(HaveOccurred())
fmt.Println(authInfo)
GinkgoWriter.Println(authInfo)

const authsKey = "auths"
Expect(authInfo).To(HaveKey(authsKey))
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/play_kube_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2781,7 +2781,7 @@ var _ = Describe("Podman play kube", func() {
// expect play kube is expected to set a seccomp label if it's applied as an annotation
jsonFile, err := podmanTest.CreateSeccompJSON(seccompPwdEPERM)
if err != nil {
fmt.Println(err)
GinkgoWriter.Println(err)
Skip("Failed to prepare seccomp.json for test.")
}

Expand All @@ -2808,7 +2808,7 @@ var _ = Describe("Podman play kube", func() {
// expect play kube is expected to set a seccomp label if it's applied as an annotation
jsonFile, err := podmanTest.CreateSeccompJSON(seccompPwdEPERM)
if err != nil {
fmt.Println(err)
GinkgoWriter.Println(err)
Skip("Failed to prepare seccomp.json for test.")
}
defer os.Remove(jsonFile)
Expand Down
3 changes: 1 addition & 2 deletions test/e2e/pod_kill_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package integration

import (
"fmt"
"os"

. "github.com/containers/podman/v4/test/utils"
Expand Down Expand Up @@ -148,7 +147,7 @@ var _ = Describe("Podman pod kill", func() {

result := podmanTest.Podman([]string{"pod", "kill", "-a"})
result.WaitWithDefaultTimeout()
fmt.Println(result.OutputToString(), result.ErrorToString())
GinkgoWriter.Println(result.OutputToString(), result.ErrorToString())
Expect(result).Should(Exit(0))
Expect(podmanTest.NumberOfContainersRunning()).To(Equal(0))
})
Expand Down
9 changes: 4 additions & 5 deletions test/e2e/pod_pod_namespaces_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package integration

import (
"fmt"
"os"

. "github.com/containers/podman/v4/test/utils"
Expand Down Expand Up @@ -54,9 +53,9 @@ var _ = Describe("Podman pod create", func() {
Expect(outputArray).To(HaveLen(2))

NAMESPACE1 := outputArray[0]
fmt.Println("NAMESPACE1:", NAMESPACE1)
GinkgoWriter.Println("NAMESPACE1:", NAMESPACE1)
NAMESPACE2 := outputArray[1]
fmt.Println("NAMESPACE2:", NAMESPACE2)
GinkgoWriter.Println("NAMESPACE2:", NAMESPACE2)
Expect(NAMESPACE1).To(Equal(NAMESPACE2))
})

Expand Down Expand Up @@ -100,9 +99,9 @@ var _ = Describe("Podman pod create", func() {
Expect(outputArray).To(HaveLen(2))

NAMESPACE1 := outputArray[0]
fmt.Println("NAMESPACE1:", NAMESPACE1)
GinkgoWriter.Println("NAMESPACE1:", NAMESPACE1)
NAMESPACE2 := outputArray[1]
fmt.Println("NAMESPACE2:", NAMESPACE2)
GinkgoWriter.Println("NAMESPACE2:", NAMESPACE2)
Expect(NAMESPACE1).To(Not(Equal(NAMESPACE2)))
})

Expand Down
12 changes: 6 additions & 6 deletions test/e2e/pod_rm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,28 +116,28 @@ var _ = Describe("Podman pod rm", func() {
})

It("podman pod rm -a doesn't remove a running container", func() {
fmt.Printf("To start, there are %d pods\n", podmanTest.NumberOfPods())
GinkgoWriter.Printf("To start, there are %d pods\n", podmanTest.NumberOfPods())
_, ec, podid1 := podmanTest.CreatePod(nil)
Expect(ec).To(Equal(0))

_, ec, _ = podmanTest.CreatePod(nil)
Expect(ec).To(Equal(0))
fmt.Printf("Started %d pods\n", podmanTest.NumberOfPods())
GinkgoWriter.Printf("Started %d pods\n", podmanTest.NumberOfPods())

session := podmanTest.RunTopContainerInPod("", podid1)
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
podmanTest.WaitForContainer()
Expect(podmanTest.NumberOfContainersRunning()).To(Equal(1))
fmt.Printf("Started container running in one pod")
GinkgoWriter.Printf("Started container running in one pod")

numPods := podmanTest.NumberOfPods()
Expect(numPods).To(Equal(2))
ps := podmanTest.Podman([]string{"pod", "ps"})
ps.WaitWithDefaultTimeout()
fmt.Printf("Current %d pod(s):\n%s\n", numPods, ps.OutputToString())
GinkgoWriter.Printf("Current %d pod(s):\n%s\n", numPods, ps.OutputToString())

fmt.Printf("Removing all empty pods\n")
GinkgoWriter.Printf("Removing all empty pods\n")
result := podmanTest.Podman([]string{"pod", "rm", "-a"})
result.WaitWithDefaultTimeout()
Expect(result).To(ExitWithError())
Expand All @@ -147,7 +147,7 @@ var _ = Describe("Podman pod rm", func() {
numPods = podmanTest.NumberOfPods()
ps = podmanTest.Podman([]string{"pod", "ps"})
ps.WaitWithDefaultTimeout()
fmt.Printf("Final %d pod(s):\n%s\n", numPods, ps.OutputToString())
GinkgoWriter.Printf("Final %d pod(s):\n%s\n", numPods, ps.OutputToString())
Expect(numPods).To(Equal(1))
// Confirm top container still running inside remaining pod
Expect(podmanTest.NumberOfContainersRunning()).To(Equal(1))
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/pod_top_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ var _ = Describe("Podman top", func() {
Expect(session).Should(Exit(0))

for i := 0; i < 10; i++ {
fmt.Println("Waiting for containers to be running .... ")
GinkgoWriter.Println("Waiting for containers to be running .... ")
if podmanTest.NumberOfContainersRunning() == 2 {
break
}
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/quadlet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ var _ = Describe("quadlet system generator", func() {
// Print any stderr output
errs := session.ErrorToString()
if errs != "" {
fmt.Println("error:", session.ErrorToString())
GinkgoWriter.Println("error:", session.ErrorToString())
}

testcase.check(generatedDir, session)
Expand Down
Loading

0 comments on commit de55faa

Please sign in to comment.