1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
15 "git.arvados.org/arvados.git/sdk/go/arvados"
17 check "gopkg.in/check.v1"
20 // newTestTimestamper wraps an io.Writer, inserting a predictable
21 // RFC3339NanoFixed timestamp at the beginning of each line.
22 func newTestTimestamper(w io.Writer) *prefixer {
26 prefixFunc: func() string {
28 return fmt.Sprintf("2015-12-29T15:51:45.%09dZ ", count)
33 type LoggingTestSuite struct {
34 client *arvados.Client
37 // Gocheck boilerplate
38 var _ = Suite(&LoggingTestSuite{})
40 func (s *LoggingTestSuite) SetUpTest(c *C) {
41 s.client = arvados.NewClientFromEnv()
42 crunchLogUpdatePeriod = time.Hour * 24 * 365
43 crunchLogUpdateSize = 1 << 50
46 func (s *LoggingTestSuite) TestWriteLogs(c *C) {
47 api := &ArvTestClient{}
48 kc := &KeepTestClient{}
50 cr, err := NewContainerRunner(s.client, api, kc, "zzzzz-dz642-zzzzzzzzzzzzzzz")
52 f, err := cr.openLogFile("crunch-run")
54 cr.CrunchLog = newLogWriter(newTestTimestamper(f))
56 cr.CrunchLog.Print("Hello world!")
57 cr.CrunchLog.Print("Goodbye")
59 c.Check(api.Calls, Equals, 0)
61 logs := logFileContent(c, cr, "crunch-run.txt")
62 c.Check(logs, Matches, `....-..-..T..:..:..\..........Z Hello world!\n`+
63 `....-..-..T..:..:..\..........Z Goodbye\n`)
66 func (s *LoggingTestSuite) TestWriteLogsLarge(c *C) {
70 api := &ArvTestClient{}
71 kc := &KeepTestClient{}
73 cr, err := NewContainerRunner(s.client, api, kc, "zzzzz-zzzzzzzzzzzzzzz")
75 f, err := cr.openLogFile("crunch-run")
77 cr.CrunchLog = newLogWriter(newTestTimestamper(f))
78 for i := 0; i < 2000000; i++ {
79 cr.CrunchLog.Printf("Hello %d", i)
81 cr.CrunchLog.Print("Goodbye")
83 logs := logFileContent(c, cr, "crunch-run.txt")
84 c.Check(strings.Count(logs, "\n"), Equals, 2000001)
85 // Redact most of the logs except the start/end for the regexp
86 // match -- otherwise, when the regexp fails, gocheck spams
87 // the test logs with tens of megabytes of quoted strings.
88 c.Assert(len(logs) > 10000, Equals, true)
89 c.Check(logs[:500]+"\n...\n"+logs[len(logs)-500:], Matches, `(?ms)2015-12-29T15:51:45.000000001Z Hello 0
90 2015-12-29T15:51:45.000000002Z Hello 1
91 2015-12-29T15:51:45.000000003Z Hello 2
92 2015-12-29T15:51:45.000000004Z Hello 3
94 2015-12-29T15:51:45.001999998Z Hello 1999997
95 2015-12-29T15:51:45.001999999Z Hello 1999998
96 2015-12-29T15:51:45.002000000Z Hello 1999999
97 2015-12-29T15:51:45.002000001Z Goodbye
100 mt, err := cr.LogCollection.MarshalManifest(".")
102 c.Check(mt, Equals, ". 9c2c05d1fae6aaa8af85113ba725716d+67108864 80b821383a07266c2a66a4566835e26e+21780065 0:88888929:crunch-run.txt\n")
105 func (s *LoggingTestSuite) TestLogUpdate(c *C) {
106 for _, trial := range []struct {
108 maxDuration time.Duration
110 {1000, 10 * time.Second},
111 {1000000, time.Millisecond},
113 c.Logf("max %d bytes, %s", trial.maxBytes, trial.maxDuration)
114 crunchLogUpdateSize = trial.maxBytes
115 crunchLogUpdatePeriod = trial.maxDuration
117 api := &ArvTestClient{}
118 kc := &KeepTestClient{}
120 cr, err := NewContainerRunner(s.client, api, kc, "zzzzz-dz642-zzzzzzzzzzzzzzz")
122 f, err := cr.openLogFile("crunch-run")
124 cr.CrunchLog = newLogWriter(newTestTimestamper(f))
125 stdout, err := cr.openLogFile("stdout")
128 c.Check(cr.logUUID, Equals, "")
129 cr.CrunchLog.Printf("Hello %1000s", "space")
130 for i, t := 0, time.NewTicker(time.Millisecond); i < 5000 && cr.logUUID == ""; i++ {
133 c.Check(cr.logUUID, Not(Equals), "")
134 cr.CrunchLog.Print("Goodbye")
135 fmt.Fprintln(stdout, "Goodbye")
137 c.Check(logFileContent(c, cr, "crunch-run.txt"), Matches, `....-..-..T..:..:............Z Hello {995}space\n`+
138 `....-..-..T..:..:............Z Goodbye\n`)
139 c.Check(logFileContent(c, cr, "stdout.txt"), Matches, `Goodbye\n`)
141 mt, err := cr.LogCollection.MarshalManifest(".")
143 c.Check(mt, Matches, `. 4dc76e0a212bfa30c39d76d8c16da0c0\+1038 5be52044a8c51e7b62dd62be07872968\+47 0:1077:crunch-run.txt 1077:8:stdout.txt\n`)
147 type filterSuite struct{}
149 var _ = Suite(&filterSuite{})
151 func (*filterSuite) TestFilterKeepstoreErrorsOnly(c *check.C) {
153 f := filterKeepstoreErrorsOnly{WriteCloser: nopCloser{&buf}}
154 for _, s := range []string{
156 "son\n" + `{"msg":"foo"}` + "\n{}\n" + `{"msg":"request"}` + "\n" + `{"msg":1234}` + "\n\n",
158 `{"msg":"response","respStatusCode":404,"foo": "bar"}` + "\n",
159 `{"msg":"response","respStatusCode":206}` + "\n",
163 c.Check(buf.String(), check.Equals, `not json
168 {"msg":"response","respStatusCode":404,"foo": "bar"}
172 type nopCloser struct {
176 func (nopCloser) Close() error { return nil }