e2e test to cover logs -f managing service being added/scaled

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2023-05-26 10:42:08 +02:00
parent 01d91c490c
commit 0d6b99e6f9
No known key found for this signature in database
GPG Key ID: 9858809D6F8F6E7E
3 changed files with 45 additions and 9 deletions

View File

@ -45,15 +45,9 @@ func (s *composeService) Logs(
return err
}
project := options.Project
if project == nil {
project, err = s.getProjectWithResources(ctx, containers, projectName)
if err != nil {
return err
}
} else if len(options.Services) == 0 {
if options.Project != nil && len(options.Services) == 0 {
// we run with an explicit compose.yaml, so only consider services defined in this file
options.Services = project.ServiceNames()
options.Services = options.Project.ServiceNames()
containers = containers.filter(isService(options.Services...))
}

View File

@ -1,7 +1,7 @@
services:
ping:
image: alpine
command: ping localhost -c 1
command: ping localhost -c ${REPEAT:-1}
hello:
image: alpine
command: echo hello

View File

@ -17,10 +17,13 @@
package e2e
import (
"fmt"
"strings"
"testing"
"time"
"gotest.tools/v3/assert"
"gotest.tools/v3/poll"
"gotest.tools/v3/icmd"
)
@ -56,3 +59,42 @@ func TestLocalComposeLogs(t *testing.T) {
_ = c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
})
}
func TestLocalComposeLogsFollow(t *testing.T) {
c := NewCLI(t, WithEnv("REPEAT=20"))
const projectName = "compose-e2e-logs"
t.Cleanup(func() {
c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
})
c.RunDockerComposeCmd(t, "-f", "./fixtures/logs-test/compose.yaml", "--project-name", projectName, "up", "-d", "ping")
cmd := c.NewDockerComposeCmd(t, "--project-name", projectName, "logs", "-f")
res := icmd.StartCmd(cmd)
t.Cleanup(func() {
_ = res.Cmd.Process.Kill()
})
expected := fmt.Sprintf("%s-ping-1 ", projectName)
poll.WaitOn(t, expectOutput(res, expected), poll.WithDelay(100*time.Millisecond), poll.WithTimeout(1*time.Second))
c.RunDockerComposeCmd(t, "-f", "./fixtures/logs-test/compose.yaml", "--project-name", projectName, "up", "-d")
expected = fmt.Sprintf("%s-hello-1 ", projectName)
poll.WaitOn(t, expectOutput(res, expected), poll.WithDelay(100*time.Millisecond), poll.WithTimeout(1*time.Second))
c.RunDockerComposeCmd(t, "-f", "./fixtures/logs-test/compose.yaml", "--project-name", projectName, "up", "-d", "--scale", "ping=2", "ping")
expected = fmt.Sprintf("%s-ping-2 ", projectName)
poll.WaitOn(t, expectOutput(res, expected), poll.WithDelay(100*time.Millisecond), poll.WithTimeout(20*time.Second))
}
func expectOutput(res *icmd.Result, expected string) func(t poll.LogT) poll.Result {
return func(t poll.LogT) poll.Result {
if strings.Contains(res.Stdout(), expected) {
return poll.Success()
}
return poll.Continue("condition not met")
}
}