How to use Logs method of wait Package

Best Testcontainers-go code snippet using wait.Logs

logs_test.go

Source:logs_test.go Github

copy

Full Screen

1package integration2import (3 "fmt"4 "os"5 "os/exec"6 "strings"7 "time"8 . "github.com/containers/podman/v4/test/utils"9 . "github.com/onsi/ginkgo"10 . "github.com/onsi/gomega"11 . "github.com/onsi/gomega/gexec"12)13func isEventBackendJournald(podmanTest *PodmanTestIntegration) bool {14 if !podmanTest.RemoteTest {15 // If not remote test, '--events-backend' is set to 'file' or 'none'16 return false17 }18 info := podmanTest.Podman([]string{"info", "--format", "{{.Host.EventLogger}}"})19 info.WaitWithDefaultTimeout()20 return info.OutputToString() == "journald"21}22var _ = Describe("Podman logs", func() {23 var (24 tempdir string25 err error26 podmanTest *PodmanTestIntegration27 )28 BeforeEach(func() {29 tempdir, err = CreateTempDirInTempDir()30 if err != nil {31 os.Exit(1)32 }33 podmanTest = PodmanTestCreate(tempdir)34 podmanTest.Setup()35 if err := podmanTest.SeedImages(); err != nil {36 os.Exit(1)37 }38 })39 AfterEach(func() {40 podmanTest.Cleanup()41 f := CurrentGinkgoTestDescription()42 processTestResult(f)43 })44 for _, log := range []string{"k8s-file", "journald", "json-file"} {45 // This is important to move the 'log' var to the correct scope under Ginkgo flow.46 log := log47 skipIfJournaldInContainer := func() {48 if log == "journald" {49 SkipIfInContainer("journalctl inside a container doesn't work correctly")50 }51 }52 It("all lines: "+log, func() {53 skipIfJournaldInContainer()54 logc := podmanTest.Podman([]string{"run", "--log-driver", log, "-dt", ALPINE, "sh", "-c", "echo podman; echo podman; echo podman"})55 logc.WaitWithDefaultTimeout()56 Expect(logc).To(Exit(0))57 cid := logc.OutputToString()58 results := podmanTest.Podman([]string{"logs", cid})59 results.WaitWithDefaultTimeout()60 Expect(results).To(Exit(0))61 Expect(results.OutputToStringArray()).To(HaveLen(3))62 Expect(results.OutputToString()).To(Equal("podman podman podman"))63 })64 It("tail two lines: "+log, func() {65 skipIfJournaldInContainer()66 logc := podmanTest.Podman([]string{"run", "--log-driver", log, "-dt", ALPINE, "sh", "-c", "echo podman; echo podman; echo podman"})67 logc.WaitWithDefaultTimeout()68 Expect(logc).To(Exit(0))69 cid := logc.OutputToString()70 results := podmanTest.Podman([]string{"logs", "--tail", "2", cid})71 results.WaitWithDefaultTimeout()72 Expect(results).To(Exit(0))73 Expect(results.OutputToStringArray()).To(HaveLen(2))74 })75 It("tail zero lines: "+log, func() {76 skipIfJournaldInContainer()77 logc := podmanTest.Podman([]string{"run", "--log-driver", log, "-dt", ALPINE, "sh", "-c", "echo podman; echo podman; echo podman"})78 logc.WaitWithDefaultTimeout()79 Expect(logc).To(Exit(0))80 cid := logc.OutputToString()81 results := podmanTest.Podman([]string{"logs", "--tail", "0", cid})82 results.WaitWithDefaultTimeout()83 Expect(results).To(Exit(0))84 Expect(results.OutputToStringArray()).To(BeEmpty())85 })86 It("tail 99 lines: "+log, func() {87 skipIfJournaldInContainer()88 logc := podmanTest.Podman([]string{"run", "--log-driver", log, "-dt", ALPINE, "sh", "-c", "echo podman; echo podman; echo podman"})89 logc.WaitWithDefaultTimeout()90 Expect(logc).To(Exit(0))91 cid := logc.OutputToString()92 results := podmanTest.Podman([]string{"logs", "--tail", "99", cid})93 results.WaitWithDefaultTimeout()94 Expect(results).To(Exit(0))95 Expect(results.OutputToStringArray()).To(HaveLen(3))96 })97 It("tail 800 lines: "+log, func() {98 skipIfJournaldInContainer()99 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"})100 logc.WaitWithDefaultTimeout()101 Expect(logc).To(Exit(0))102 cid := logc.OutputToString()103 results := podmanTest.Podman([]string{"logs", "--tail", "800", cid})104 results.WaitWithDefaultTimeout()105 Expect(results).To(Exit(0))106 Expect(results.OutputToStringArray()).To(HaveLen(800))107 })108 It("tail 2 lines with timestamps: "+log, func() {109 skipIfJournaldInContainer()110 logc := podmanTest.Podman([]string{"run", "--log-driver", log, "-dt", ALPINE, "sh", "-c", "echo podman; echo podman; echo podman"})111 logc.WaitWithDefaultTimeout()112 Expect(logc).To(Exit(0))113 cid := logc.OutputToString()114 results := podmanTest.Podman([]string{"logs", "--tail", "2", "-t", cid})115 results.WaitWithDefaultTimeout()116 Expect(results).To(Exit(0))117 Expect(results.OutputToStringArray()).To(HaveLen(2))118 })119 It("since time 2017-08-07: "+log, func() {120 skipIfJournaldInContainer()121 logc := podmanTest.Podman([]string{"run", "--log-driver", log, "-dt", ALPINE, "sh", "-c", "echo podman; echo podman; echo podman"})122 logc.WaitWithDefaultTimeout()123 Expect(logc).To(Exit(0))124 cid := logc.OutputToString()125 results := podmanTest.Podman([]string{"logs", "--since", "2017-08-07T10:10:09.056611202-04:00", cid})126 results.WaitWithDefaultTimeout()127 Expect(results).To(Exit(0))128 Expect(results.OutputToStringArray()).To(HaveLen(3))129 })130 It("since duration 10m: "+log, func() {131 skipIfJournaldInContainer()132 logc := podmanTest.Podman([]string{"run", "--log-driver", log, "-dt", ALPINE, "sh", "-c", "echo podman; echo podman; echo podman"})133 logc.WaitWithDefaultTimeout()134 Expect(logc).To(Exit(0))135 cid := logc.OutputToString()136 results := podmanTest.Podman([]string{"logs", "--since", "10m", cid})137 results.WaitWithDefaultTimeout()138 Expect(results).To(Exit(0))139 Expect(results.OutputToStringArray()).To(HaveLen(3))140 })141 It("until duration 10m: "+log, func() {142 skipIfJournaldInContainer()143 logc := podmanTest.Podman([]string{"run", "--log-driver", log, "-dt", ALPINE, "sh", "-c", "echo podman; echo podman; echo podman"})144 logc.WaitWithDefaultTimeout()145 Expect(logc).To(Exit(0))146 cid := logc.OutputToString()147 results := podmanTest.Podman([]string{"logs", "--until", "10m", cid})148 results.WaitWithDefaultTimeout()149 Expect(results).To(Exit(0))150 Expect(results.OutputToStringArray()).To(HaveLen(3))151 })152 It("until time NOW: "+log, func() {153 skipIfJournaldInContainer()154 logc := podmanTest.Podman([]string{"run", "--log-driver", log, "-dt", ALPINE, "sh", "-c", "echo podman; echo podman; echo podman"})155 logc.WaitWithDefaultTimeout()156 Expect(logc).To(Exit(0))157 cid := logc.OutputToString()158 now := time.Now()159 now = now.Add(time.Minute * 1)160 nowS := now.Format(time.RFC3339)161 results := podmanTest.Podman([]string{"logs", "--until", nowS, cid})162 results.WaitWithDefaultTimeout()163 Expect(results).To(Exit(0))164 Expect(results.OutputToStringArray()).To(HaveLen(3))165 })166 It("latest and container name should fail: "+log, func() {167 skipIfJournaldInContainer()168 results := podmanTest.Podman([]string{"logs", "-l", "foobar"})169 results.WaitWithDefaultTimeout()170 Expect(results).To(ExitWithError())171 })172 It("two containers showing short container IDs: "+log, func() {173 skipIfJournaldInContainer()174 SkipIfRemote("podman-remote logs does not support showing two containers at the same time")175 log1 := podmanTest.Podman([]string{"run", "--log-driver", log, "-dt", ALPINE, "sh", "-c", "echo podman; echo podman; echo podman"})176 log1.WaitWithDefaultTimeout()177 Expect(log1).Should(Exit(0))178 cid1 := log1.OutputToString()179 log2 := podmanTest.Podman([]string{"run", "--log-driver", log, "-dt", ALPINE, "sh", "-c", "echo podman; echo podman; echo podman"})180 log2.WaitWithDefaultTimeout()181 Expect(log2).Should(Exit(0))182 cid2 := log2.OutputToString()183 results := podmanTest.Podman([]string{"logs", cid1, cid2})184 results.WaitWithDefaultTimeout()185 Expect(results).Should(Exit(0))186 output := results.OutputToStringArray()187 Expect(output).To(HaveLen(6))188 Expect(strings.Contains(output[0], cid1[:12]) || strings.Contains(output[0], cid2[:12])).To(BeTrue())189 })190 It("podman logs on a created container should result in 0 exit code: "+log, func() {191 skipIfJournaldInContainer()192 session := podmanTest.Podman([]string{"create", "--log-driver", log, "-t", "--name", "log", ALPINE})193 session.WaitWithDefaultTimeout()194 Expect(session).To(Exit(0))195 results := podmanTest.Podman([]string{"logs", "log"})196 results.WaitWithDefaultTimeout()197 Expect(results).To(Exit(0))198 })199 It("streaming output: "+log, func() {200 skipIfJournaldInContainer()201 containerName := "logs-f"202 logc := podmanTest.Podman([]string{"run", "--log-driver", log, "--name", containerName, "-dt", ALPINE, "sh", "-c", "echo podman-1; sleep 1; echo podman-2"})203 logc.WaitWithDefaultTimeout()204 Expect(logc).To(Exit(0))205 results := podmanTest.Podman([]string{"logs", "-f", containerName})206 results.WaitWithDefaultTimeout()207 if log == "journald" && !isEventBackendJournald(podmanTest) {208 // --follow + journald log-driver is only supported with journald events-backend(PR #10431)209 Expect(results).To(Exit(125))210 Expect(results.ErrorToString()).To(ContainSubstring("using --follow with the journald --log-driver but without the journald --events-backend"))211 return212 }213 Expect(results).To(Exit(0))214 Expect(results.OutputToString()).To(ContainSubstring("podman-1"))215 Expect(results.OutputToString()).To(ContainSubstring("podman-2"))216 // Container should now be terminatING or terminatED, but we217 // have no guarantee of which: 'logs -f' does not necessarily218 // wait for cleanup. Run 'inspect' and accept either state.219 inspect := podmanTest.Podman([]string{"container", "inspect", "--format", "{{.State.Status}}", containerName})220 inspect.WaitWithDefaultTimeout()221 if inspect.ExitCode() == 0 {222 Expect(inspect.OutputToString()).To(Equal("exited"))223 // TODO: add 2-second wait loop to confirm cleanup224 } else {225 Expect(inspect.ErrorToString()).To(ContainSubstring("no such container"))226 }227 results = podmanTest.Podman([]string{"rm", "--time", "0", "-f", containerName})228 results.WaitWithDefaultTimeout()229 Expect(results).To(Exit(0))230 })231 It("follow output stopped container: "+log, func() {232 skipIfJournaldInContainer()233 containerName := "logs-f"234 logc := podmanTest.Podman([]string{"run", "--log-driver", log, "--name", containerName, "-d", ALPINE, "true"})235 logc.WaitWithDefaultTimeout()236 Expect(logc).To(Exit(0))237 results := podmanTest.Podman([]string{"logs", "-f", containerName})238 results.WaitWithDefaultTimeout()239 if log == "journald" && !isEventBackendJournald(podmanTest) {240 // --follow + journald log-driver is only supported with journald events-backend(PR #10431)241 Expect(results).To(Exit(125))242 return243 }244 Expect(results).To(Exit(0))245 })246 It("using container with container log-size: "+log, func() {247 skipIfJournaldInContainer()248 logc := podmanTest.Podman([]string{"run", "--log-driver", log, "--log-opt=max-size=10k", "-d", ALPINE, "sh", "-c", "echo podman podman podman"})249 logc.WaitWithDefaultTimeout()250 Expect(logc).To(Exit(0))251 cid := logc.OutputToString()252 wait := podmanTest.Podman([]string{"wait", cid})253 wait.WaitWithDefaultTimeout()254 Expect(wait).To(Exit(0))255 inspect := podmanTest.Podman([]string{"container", "inspect", "--format", "{{.HostConfig.LogConfig.Size}}", cid})256 inspect.WaitWithDefaultTimeout()257 Expect(inspect).To(Exit(0))258 Expect(inspect.OutputToString()).To(Equal("10kB"))259 results := podmanTest.Podman([]string{"logs", cid})260 results.WaitWithDefaultTimeout()261 Expect(results).To(Exit(0))262 Expect(results.OutputToString()).To(Equal("podman podman podman"))263 })264 It("Make sure logs match expected length: "+log, func() {265 skipIfJournaldInContainer()266 logc := podmanTest.Podman([]string{"run", "--log-driver", log, "-t", "--name", "test", ALPINE, "sh", "-c", "echo 1; echo 2"})267 logc.WaitWithDefaultTimeout()268 Expect(logc).To(Exit(0))269 wait := podmanTest.Podman([]string{"wait", "test"})270 wait.WaitWithDefaultTimeout()271 Expect(wait).To(Exit(0))272 results := podmanTest.Podman([]string{"logs", "test"})273 results.WaitWithDefaultTimeout()274 Expect(results).To(Exit(0))275 outlines := results.OutputToStringArray()276 Expect(outlines).To(HaveLen(2))277 Expect(outlines[0]).To(Equal("1\r"))278 Expect(outlines[1]).To(Equal("2\r"))279 })280 It("podman logs test stdout and stderr: "+log, func() {281 skipIfJournaldInContainer()282 cname := "log-test"283 logc := podmanTest.Podman([]string{"run", "--log-driver", log, "--name", cname, ALPINE, "sh", "-c", "echo stdout; echo stderr >&2"})284 logc.WaitWithDefaultTimeout()285 Expect(logc).To(Exit(0))286 wait := podmanTest.Podman([]string{"wait", cname})287 wait.WaitWithDefaultTimeout()288 Expect(wait).To(Exit(0))289 results := podmanTest.Podman([]string{"logs", cname})290 results.WaitWithDefaultTimeout()291 Expect(results).To(Exit(0))292 Expect(results.OutputToString()).To(Equal("stdout"))293 Expect(results.ErrorToString()).To(Equal("stderr"))294 })295 }296 It("using journald for container with container tag", func() {297 SkipIfInContainer("journalctl inside a container doesn't work correctly")298 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"})299 logc.WaitWithDefaultTimeout()300 Expect(logc).To(Exit(0))301 cid := logc.OutputToString()302 wait := podmanTest.Podman([]string{"wait", cid})303 wait.WaitWithDefaultTimeout()304 Expect(wait).To(Exit(0))305 cmd := exec.Command("journalctl", "--no-pager", "-o", "json", "--output-fields=CONTAINER_TAG", fmt.Sprintf("CONTAINER_ID_FULL=%s", cid))306 out, err := cmd.CombinedOutput()307 Expect(err).To(BeNil())308 Expect(string(out)).To(ContainSubstring("alpine"))309 })310 It("using journald container name", func() {311 SkipIfInContainer("journalctl inside a container doesn't work correctly")312 containerName := "inside-journal"313 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"})314 logc.WaitWithDefaultTimeout()315 Expect(logc).To(Exit(0))316 cid := logc.OutputToString()317 wait := podmanTest.Podman([]string{"wait", cid})318 wait.WaitWithDefaultTimeout()319 Expect(wait).To(Exit(0))320 cmd := exec.Command("journalctl", "--no-pager", "-o", "json", "--output-fields=CONTAINER_NAME", fmt.Sprintf("CONTAINER_ID_FULL=%s", cid))321 out, err := cmd.CombinedOutput()322 Expect(err).To(BeNil())323 Expect(string(out)).To(ContainSubstring(containerName))324 })325 It("podman logs with log-driver=none errors", func() {326 ctrName := "logsctr"327 logc := podmanTest.Podman([]string{"run", "--name", ctrName, "-d", "--log-driver", "none", ALPINE, "top"})328 logc.WaitWithDefaultTimeout()329 Expect(logc).To(Exit(0))330 logs := podmanTest.Podman([]string{"logs", "-f", ctrName})331 logs.WaitWithDefaultTimeout()332 Expect(logs).To(Not(Exit(0)))333 })334 It("podman pod logs with container names", func() {335 SkipIfRemote("Remote can only process one container at a time")336 SkipIfInContainer("journalctl inside a container doesn't work correctly")337 podName := "testPod"338 containerName1 := "container1"339 containerName2 := "container2"340 testPod := podmanTest.Podman([]string{"pod", "create", fmt.Sprintf("--name=%s", podName)})341 testPod.WaitWithDefaultTimeout()342 Expect(testPod).To(Exit(0))343 log1 := podmanTest.Podman([]string{"run", "--name", containerName1, "-d", "--pod", podName, BB, "/bin/sh", "-c", "echo log1"})344 log1.WaitWithDefaultTimeout()345 Expect(log1).To(Exit(0))346 log2 := podmanTest.Podman([]string{"run", "--name", containerName2, "-d", "--pod", podName, BB, "/bin/sh", "-c", "echo log2"})347 log2.WaitWithDefaultTimeout()348 Expect(log2).To(Exit(0))349 results := podmanTest.Podman([]string{"pod", "logs", "--names", podName})350 results.WaitWithDefaultTimeout()351 Expect(results).To(Exit(0))352 output := results.OutputToStringArray()353 Expect(output).To(HaveLen(2))354 Expect(output).To(ContainElement(ContainSubstring(containerName1)))355 Expect(output).To(ContainElement(ContainSubstring(containerName2)))356 })357})...

Full Screen

Full Screen

docker_cli_logs_test.go

Source:docker_cli_logs_test.go Github

copy

Full Screen

...12 "gotest.tools/assert"13 "gotest.tools/icmd"14)15// This used to work, it test a log of PageSize-1 (gh#4851)16func (s *DockerSuite) TestLogsContainerSmallerThanPage(c *testing.T) {17 testLogsContainerPagination(c, 32767)18}19// Regression test: When going over the PageSize, it used to panic (gh#4851)20func (s *DockerSuite) TestLogsContainerBiggerThanPage(c *testing.T) {21 testLogsContainerPagination(c, 32768)22}23// Regression test: When going much over the PageSize, it used to block (gh#4851)24func (s *DockerSuite) TestLogsContainerMuchBiggerThanPage(c *testing.T) {25 testLogsContainerPagination(c, 33000)26}27func testLogsContainerPagination(c *testing.T, testLen int) {28 out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo -n = >> a.a; done; echo >> a.a; cat a.a", testLen))29 id := strings.TrimSpace(out)30 dockerCmd(c, "wait", id)31 out, _ = dockerCmd(c, "logs", id)32 assert.Equal(c, len(out), testLen+1)33}34func (s *DockerSuite) TestLogsTimestamps(c *testing.T) {35 testLen := 10036 out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo = >> a.a; done; cat a.a", testLen))37 id := strings.TrimSpace(out)38 dockerCmd(c, "wait", id)39 out, _ = dockerCmd(c, "logs", "-t", id)40 lines := strings.Split(out, "\n")41 assert.Equal(c, len(lines), testLen+1)42 ts := regexp.MustCompile(`^.* `)43 for _, l := range lines {44 if l != "" {45 _, err := time.Parse(jsonmessage.RFC3339NanoFixed+" ", ts.FindString(l))46 assert.NilError(c, err, "Failed to parse timestamp from %v", l)47 // ensure we have padded 0's48 assert.Equal(c, l[29], uint8('Z'))49 }50 }51}52func (s *DockerSuite) TestLogsSeparateStderr(c *testing.T) {53 msg := "stderr_log"54 out := cli.DockerCmd(c, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("echo %s 1>&2", msg)).Combined()55 id := strings.TrimSpace(out)56 cli.DockerCmd(c, "wait", id)57 cli.DockerCmd(c, "logs", id).Assert(c, icmd.Expected{58 Out: "",59 Err: msg,60 })61}62func (s *DockerSuite) TestLogsStderrInStdout(c *testing.T) {63 // TODO Windows: Needs investigation why this fails. Obtained string includes64 // a bunch of ANSI escape sequences before the "stderr_log" message.65 testRequires(c, DaemonIsLinux)66 msg := "stderr_log"67 out := cli.DockerCmd(c, "run", "-d", "-t", "busybox", "sh", "-c", fmt.Sprintf("echo %s 1>&2", msg)).Combined()68 id := strings.TrimSpace(out)69 cli.DockerCmd(c, "wait", id)70 cli.DockerCmd(c, "logs", id).Assert(c, icmd.Expected{71 Out: msg,72 Err: "",73 })74}75func (s *DockerSuite) TestLogsTail(c *testing.T) {76 testLen := 10077 out := cli.DockerCmd(c, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo =; done;", testLen)).Combined()78 id := strings.TrimSpace(out)79 cli.DockerCmd(c, "wait", id)80 out = cli.DockerCmd(c, "logs", "--tail", "0", id).Combined()81 lines := strings.Split(out, "\n")82 assert.Equal(c, len(lines), 1)83 out = cli.DockerCmd(c, "logs", "--tail", "5", id).Combined()84 lines = strings.Split(out, "\n")85 assert.Equal(c, len(lines), 6)86 out = cli.DockerCmd(c, "logs", "--tail", "99", id).Combined()87 lines = strings.Split(out, "\n")88 assert.Equal(c, len(lines), 100)89 out = cli.DockerCmd(c, "logs", "--tail", "all", id).Combined()90 lines = strings.Split(out, "\n")91 assert.Equal(c, len(lines), testLen+1)92 out = cli.DockerCmd(c, "logs", "--tail", "-1", id).Combined()93 lines = strings.Split(out, "\n")94 assert.Equal(c, len(lines), testLen+1)95 out = cli.DockerCmd(c, "logs", "--tail", "random", id).Combined()96 lines = strings.Split(out, "\n")97 assert.Equal(c, len(lines), testLen+1)98}99func (s *DockerSuite) TestLogsFollowStopped(c *testing.T) {100 dockerCmd(c, "run", "--name=test", "busybox", "echo", "hello")101 id := getIDByName(c, "test")102 logsCmd := exec.Command(dockerBinary, "logs", "-f", id)103 assert.NilError(c, logsCmd.Start())104 errChan := make(chan error)105 go func() {106 errChan <- logsCmd.Wait()107 close(errChan)108 }()109 select {110 case err := <-errChan:111 assert.NilError(c, err)112 case <-time.After(30 * time.Second):113 c.Fatal("Following logs is hanged")114 }115}116func (s *DockerSuite) TestLogsSince(c *testing.T) {117 name := "testlogssince"118 dockerCmd(c, "run", "--name="+name, "busybox", "/bin/sh", "-c", "for i in $(seq 1 3); do sleep 2; echo log$i; done")119 out, _ := dockerCmd(c, "logs", "-t", name)120 log2Line := strings.Split(strings.Split(out, "\n")[1], " ")121 t, err := time.Parse(time.RFC3339Nano, log2Line[0]) // the timestamp log2 is written122 assert.NilError(c, err)123 since := t.Unix() + 1 // add 1s so log1 & log2 doesn't show up124 out, _ = dockerCmd(c, "logs", "-t", fmt.Sprintf("--since=%v", since), name)125 // Skip 2 seconds126 unexpected := []string{"log1", "log2"}127 for _, v := range unexpected {128 assert.Check(c, !strings.Contains(out, v), "unexpected log message returned, since=%v", since)129 }130 // Test to make sure a bad since format is caught by the client131 out, _, _ = dockerCmdWithError("logs", "-t", "--since=2006-01-02T15:04:0Z", name)132 assert.Assert(c, strings.Contains(out, `cannot parse "0Z" as "05"`), "bad since format passed to server")133 // Test with default value specified and parameter omitted134 expected := []string{"log1", "log2", "log3"}135 for _, cmd := range [][]string{136 {"logs", "-t", name},137 {"logs", "-t", "--since=0", name},138 } {139 result := icmd.RunCommand(dockerBinary, cmd...)140 result.Assert(c, icmd.Success)141 for _, v := range expected {142 assert.Check(c, strings.Contains(result.Combined(), v))143 }144 }145}146func (s *DockerSuite) TestLogsSinceFutureFollow(c *testing.T) {147 // TODO Windows TP5 - Figure out why this test is so flakey. Disabled for now.148 testRequires(c, DaemonIsLinux)149 name := "testlogssincefuturefollow"150 dockerCmd(c, "run", "-d", "--name", name, "busybox", "/bin/sh", "-c", `for i in $(seq 1 5); do echo log$i; sleep 1; done`)151 // Extract one timestamp from the log file to give us a starting point for152 // our `--since` argument. Because the log producer runs in the background,153 // we need to check repeatedly for some output to be produced.154 var timestamp string155 for i := 0; i != 100 && timestamp == ""; i++ {156 if out, _ := dockerCmd(c, "logs", "-t", name); out == "" {157 time.Sleep(time.Millisecond * 100) // Retry158 } else {159 timestamp = strings.Split(strings.Split(out, "\n")[0], " ")[0]160 }161 }162 assert.Assert(c, timestamp != "")163 t, err := time.Parse(time.RFC3339Nano, timestamp)164 assert.NilError(c, err)165 since := t.Unix() + 2166 out, _ := dockerCmd(c, "logs", "-t", "-f", fmt.Sprintf("--since=%v", since), name)167 assert.Assert(c, len(out) != 0, "cannot read from empty log")168 lines := strings.Split(strings.TrimSpace(out), "\n")169 for _, v := range lines {170 ts, err := time.Parse(time.RFC3339Nano, strings.Split(v, " ")[0])171 assert.NilError(c, err, "cannot parse timestamp output from log: '%v'", v)172 assert.Assert(c, ts.Unix() >= since, "earlier log found. since=%v logdate=%v", since, ts)173 }174}175// Regression test for #8832176func (s *DockerSuite) TestLogsFollowSlowStdoutConsumer(c *testing.T) {177 // TODO Windows: Fix this test for TP5.178 testRequires(c, DaemonIsLinux)179 expected := 150000180 out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", fmt.Sprintf("usleep 600000; yes X | head -c %d", expected))181 id := strings.TrimSpace(out)182 stopSlowRead := make(chan bool)183 go func() {184 dockerCmd(c, "wait", id)185 stopSlowRead <- true186 }()187 logCmd := exec.Command(dockerBinary, "logs", "-f", id)188 stdout, err := logCmd.StdoutPipe()189 assert.NilError(c, err)190 assert.NilError(c, logCmd.Start())191 defer func() { go logCmd.Wait() }()192 // First read slowly193 bytes1, err := ConsumeWithSpeed(stdout, 10, 50*time.Millisecond, stopSlowRead)194 assert.NilError(c, err)195 // After the container has finished we can continue reading fast196 bytes2, err := ConsumeWithSpeed(stdout, 32*1024, 0, nil)197 assert.NilError(c, err)198 assert.NilError(c, logCmd.Wait())199 actual := bytes1 + bytes2200 assert.Equal(c, actual, expected)201}202// ConsumeWithSpeed reads chunkSize bytes from reader before sleeping203// for interval duration. Returns total read bytes. Send true to the204// stop channel to return before reading to EOF on the reader.205func ConsumeWithSpeed(reader io.Reader, chunkSize int, interval time.Duration, stop chan bool) (n int, err error) {206 buffer := make([]byte, chunkSize)207 for {208 var readBytes int209 readBytes, err = reader.Read(buffer)210 n += readBytes211 if err != nil {212 if err == io.EOF {213 err = nil214 }215 return216 }217 select {218 case <-stop:219 return220 case <-time.After(interval):221 }222 }223}224func (s *DockerSuite) TestLogsFollowGoroutinesWithStdout(c *testing.T) {225 out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "while true; do echo hello; sleep 2; done")226 id := strings.TrimSpace(out)227 assert.NilError(c, waitRun(id))228 nroutines, err := getGoroutineNumber()229 assert.NilError(c, err)230 cmd := exec.Command(dockerBinary, "logs", "-f", id)231 r, w := io.Pipe()232 cmd.Stdout = w233 assert.NilError(c, cmd.Start())234 go cmd.Wait()235 // Make sure pipe is written to236 chErr := make(chan error)237 go func() {238 b := make([]byte, 1)239 _, err := r.Read(b)240 chErr <- err241 }()242 assert.NilError(c, <-chErr)243 assert.NilError(c, cmd.Process.Kill())244 r.Close()245 cmd.Wait()246 // NGoroutines is not updated right away, so we need to wait before failing247 assert.NilError(c, waitForGoroutines(nroutines))248}249func (s *DockerSuite) TestLogsFollowGoroutinesNoOutput(c *testing.T) {250 out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "while true; do sleep 2; done")251 id := strings.TrimSpace(out)252 assert.NilError(c, waitRun(id))253 nroutines, err := getGoroutineNumber()254 assert.NilError(c, err)255 cmd := exec.Command(dockerBinary, "logs", "-f", id)256 assert.NilError(c, cmd.Start())257 go cmd.Wait()258 time.Sleep(200 * time.Millisecond)259 assert.NilError(c, cmd.Process.Kill())260 cmd.Wait()261 // NGoroutines is not updated right away, so we need to wait before failing262 assert.NilError(c, waitForGoroutines(nroutines))263}264func (s *DockerSuite) TestLogsCLIContainerNotFound(c *testing.T) {265 name := "testlogsnocontainer"266 out, _, _ := dockerCmdWithError("logs", name)267 message := fmt.Sprintf("No such container: %s\n", name)268 assert.Assert(c, strings.Contains(out, message))269}270func (s *DockerSuite) TestLogsWithDetails(c *testing.T) {271 dockerCmd(c, "run", "--name=test", "--label", "foo=bar", "-e", "baz=qux", "--log-opt", "labels=foo", "--log-opt", "env=baz", "busybox", "echo", "hello")272 out, _ := dockerCmd(c, "logs", "--details", "--timestamps", "test")273 logFields := strings.Fields(strings.TrimSpace(out))274 assert.Equal(c, len(logFields), 3, out)275 details := strings.Split(logFields[1], ",")276 assert.Equal(c, len(details), 2)277 assert.Equal(c, details[0], "baz=qux")278 assert.Equal(c, details[1], "foo=bar")279}...

Full Screen

Full Screen

Logs

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 c1 := make(chan string)4 c2 := make(chan string)5 go func() {6 time.Sleep(time.Second * 1)7 }()8 go func() {9 time.Sleep(time.Second * 2)10 }()11 for i := 0; i < 2; i++ {12 select {13 fmt.Println("received", msg1)14 fmt.Println("received", msg2)15 }16 }17}18import (19func main() {20 tick := time.Tick(100 * time.Millisecond)21 boom := time.After(500 * time.Millisecond)22 for {23 select {24 fmt.Println("tick.")25 fmt.Println("BOOM!")26 fmt.Println(" .")27 time.Sleep(50 * time.Millisecond)28 }29 }30}

Full Screen

Full Screen

Logs

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 c1 := make(chan string)4 c2 := make(chan string)5 go func() {6 time.Sleep(time.Second * 1)7 }()8 go func() {9 time.Sleep(time.Second * 2)10 }()11 for i := 0; i < 2; i++ {12 select {13 fmt.Println("received", msg1)14 fmt.Println("received", msg2)15 }16 }17}18import (19func main() {20 tick := time.Tick(100 * time.Millisecond)21 boom := time.After(500 * time.Millisecond)22 for {23 select {24 fmt.Println("tick.")25 fmt.Println("BOOM!")26 fmt.Println(" .")27 time.Sleep(50 * time.Millisecond)28 }29 }30}31import (32func main() {33 tick := time.Tick(100 * time.Millisecond)34 boom := time.After(500 * time.Millisecond)35 for {36 select {37 fmt.Println("tick.")38 fmt.Println("BOOM!")

Full Screen

Full Screen

Logs

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 c1 := make(chan string)4 c2 := make(chan string)5 go func() {6 time.Sleep(time.Second * 1)7 }()8 go func() {9 time.Sleep(time.Second * 2)10 }()11 for i := 0; i < 2; i++ {12 select {13 fmt.Println("received", msg1)14 fmt.Println("received", msg2)15 }16 }17}

Full Screen

Full Screen

Logs

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 wait.Add(2)4 go func() {5 defer wait.Done()6 time.Sleep(1)7 }()8 go func() {9 defer wait.Done()10 time.Sleep(2)11 }()12 wait.Wait()13 fmt.Println("Terminating Program")14}15wg.Add(1)16wg.Done()17wg.Wait()18func (wg *WaitGroup) Add(delta int)19func (

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful