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"
16 "git.arvados.org/arvados.git/sdk/go/arvadosclient"
18 check "gopkg.in/check.v1"
21 type LoggingTestSuite struct {
22 client *arvados.Client
25 type TestTimestamper struct {
29 func (stamper *TestTimestamper) Timestamp(t time.Time) string {
31 t, err := time.ParseInLocation(time.RFC3339Nano, fmt.Sprintf("2015-12-29T15:51:45.%09dZ", stamper.count), t.Location())
35 return RFC3339Timestamp(t)
38 // Gocheck boilerplate
39 var _ = Suite(&LoggingTestSuite{})
41 func (s *LoggingTestSuite) SetUpTest(c *C) {
42 s.client = arvados.NewClientFromEnv()
43 crunchLogUpdatePeriod = time.Hour * 24 * 365
44 crunchLogUpdateSize = 1 << 50
47 func (s *LoggingTestSuite) TestWriteLogs(c *C) {
48 api := &ArvTestClient{}
49 kc := &KeepTestClient{}
51 cr, err := NewContainerRunner(s.client, api, kc, "zzzzz-zzzzzzzzzzzzzzz")
53 cr.CrunchLog.Timestamper = (&TestTimestamper{}).Timestamp
55 cr.CrunchLog.Print("Hello world!")
56 cr.CrunchLog.Print("Goodbye")
59 c.Check(api.Calls, Equals, 1)
61 mt, err := cr.LogCollection.MarshalManifest(".")
63 c.Check(mt, Equals, ". 74561df9ae65ee9f35d5661d42454264+83 0:83:crunch-run.txt\n")
65 logtext := "2015-12-29T15:51:45.000000001Z Hello world!\n" +
66 "2015-12-29T15:51:45.000000002Z Goodbye\n"
68 c.Check(api.Content[0]["log"].(arvadosclient.Dict)["event_type"], Equals, "crunch-run")
69 c.Check(api.Content[0]["log"].(arvadosclient.Dict)["properties"].(map[string]string)["text"], Equals, logtext)
70 c.Check(string(kc.Content), Equals, logtext)
73 func (s *LoggingTestSuite) TestWriteLogsLarge(c *C) {
77 api := &ArvTestClient{}
78 kc := &KeepTestClient{}
80 cr, err := NewContainerRunner(s.client, api, kc, "zzzzz-zzzzzzzzzzzzzzz")
82 cr.CrunchLog.Timestamper = (&TestTimestamper{}).Timestamp
83 cr.CrunchLog.Immediate = nil
85 for i := 0; i < 2000000; i++ {
86 cr.CrunchLog.Printf("Hello %d", i)
88 cr.CrunchLog.Print("Goodbye")
91 c.Check(api.Calls > 0, Equals, true)
92 c.Check(api.Calls < 2000000, Equals, true)
94 mt, err := cr.LogCollection.MarshalManifest(".")
96 c.Check(mt, Equals, ". 9c2c05d1fae6aaa8af85113ba725716d+67108864 80b821383a07266c2a66a4566835e26e+21780065 0:88888929:crunch-run.txt\n")
99 func (s *LoggingTestSuite) TestWriteMultipleLogs(c *C) {
100 api := &ArvTestClient{}
101 kc := &KeepTestClient{}
103 cr, err := NewContainerRunner(s.client, api, kc, "zzzzz-zzzzzzzzzzzzzzz")
105 ts := &TestTimestamper{}
106 cr.CrunchLog.Timestamper = ts.Timestamp
107 w, err := cr.NewLogWriter("stdout")
109 stdout := NewThrottledLogger(w)
110 stdout.Timestamper = ts.Timestamp
112 cr.CrunchLog.Print("Hello world!")
113 stdout.Print("Doing stuff")
114 cr.CrunchLog.Print("Goodbye")
115 stdout.Print("Blurb")
119 logText := make(map[string]string)
120 for _, content := range api.Content {
121 log := content["log"].(arvadosclient.Dict)
122 logText[log["event_type"].(string)] += log["properties"].(map[string]string)["text"]
125 c.Check(logText["crunch-run"], Equals, `2015-12-29T15:51:45.000000001Z Hello world!
126 2015-12-29T15:51:45.000000003Z Goodbye
128 c.Check(logText["stdout"], Equals, `2015-12-29T15:51:45.000000002Z Doing stuff
129 2015-12-29T15:51:45.000000004Z Blurb
132 mt, err := cr.LogCollection.MarshalManifest(".")
134 c.Check(mt, Equals, ". 48f9023dc683a850b1c9b482b14c4b97+163 0:83:crunch-run.txt 83:80:stdout.txt\n")
137 func (s *LoggingTestSuite) TestLogUpdate(c *C) {
138 for _, trial := range []struct {
140 maxDuration time.Duration
142 {1000, 10 * time.Second},
143 {1000000, time.Millisecond},
145 c.Logf("max %d bytes, %s", trial.maxBytes, trial.maxDuration)
146 crunchLogUpdateSize = trial.maxBytes
147 crunchLogUpdatePeriod = trial.maxDuration
149 api := &ArvTestClient{}
150 kc := &KeepTestClient{}
152 cr, err := NewContainerRunner(s.client, api, kc, "zzzzz-zzzzzzzzzzzzzzz")
154 ts := &TestTimestamper{}
155 cr.CrunchLog.Timestamper = ts.Timestamp
156 w, err := cr.NewLogWriter("stdout")
158 stdout := NewThrottledLogger(w)
159 stdout.Timestamper = ts.Timestamp
161 c.Check(cr.logUUID, Equals, "")
162 cr.CrunchLog.Printf("Hello %1000s", "space")
163 for i, t := 0, time.NewTicker(time.Millisecond); i < 5000 && cr.logUUID == ""; i++ {
166 c.Check(cr.logUUID, Not(Equals), "")
167 cr.CrunchLog.Print("Goodbye")
168 fmt.Fprint(stdout, "Goodbye\n")
173 mt, err := cr.LogCollection.MarshalManifest(".")
175 // Block packing depends on whether there's an update
176 // between the two Goodbyes -- either way the first
177 // block will be 4dc76.
178 c.Check(mt, Matches, `. 4dc76e0a212bfa30c39d76d8c16da0c0\+1038 (afc503bc1b9a828b4bb543cb629e936c\+78|90699dc22545cd74a0664303f70bc05a\+39 276b49339fd5203d15a93ff3de11bfb9\+39) 0:1077:crunch-run.txt 1077:39:stdout.txt\n`)
182 func (s *LoggingTestSuite) TestWriteLogsWithRateLimitThrottleBytes(c *C) {
183 s.testWriteLogsWithRateLimit(c, "crunchLogThrottleBytes", 50, 65536, "Exceeded rate 50 bytes per 60 seconds")
186 func (s *LoggingTestSuite) TestWriteLogsWithRateLimitThrottleLines(c *C) {
187 s.testWriteLogsWithRateLimit(c, "crunchLogThrottleLines", 1, 1024, "Exceeded rate 1 lines per 60 seconds")
190 func (s *LoggingTestSuite) TestWriteLogsWithRateLimitThrottleBytesPerEvent(c *C) {
191 s.testWriteLogsWithRateLimit(c, "crunchLimitLogBytesPerJob", 50, 67108864, "Exceeded log limit 50 bytes (crunch_limit_log_bytes_per_job)")
194 func (s *LoggingTestSuite) TestWriteLogsWithZeroBytesPerJob(c *C) {
195 s.testWriteLogsWithRateLimit(c, "crunchLimitLogBytesPerJob", 0, 67108864, "Exceeded log limit 0 bytes (crunch_limit_log_bytes_per_job)")
198 func (s *LoggingTestSuite) testWriteLogsWithRateLimit(c *C, throttleParam string, throttleValue int, throttleDefault int, expected string) {
199 discoveryMap[throttleParam] = float64(throttleValue)
201 discoveryMap[throttleParam] = float64(throttleDefault)
204 api := &ArvTestClient{}
205 kc := &KeepTestClient{}
207 cr, err := NewContainerRunner(s.client, api, kc, "zzzzz-zzzzzzzzzzzzzzz")
209 cr.CrunchLog.Timestamper = (&TestTimestamper{}).Timestamp
211 cr.CrunchLog.Print("Hello world!")
212 cr.CrunchLog.Print("Goodbye")
215 c.Check(api.Calls, Equals, 1)
217 mt, err := cr.LogCollection.MarshalManifest(".")
219 c.Check(mt, Equals, ". 74561df9ae65ee9f35d5661d42454264+83 0:83:crunch-run.txt\n")
221 logtext := "2015-12-29T15:51:45.000000001Z Hello world!\n" +
222 "2015-12-29T15:51:45.000000002Z Goodbye\n"
224 c.Check(api.Content[0]["log"].(arvadosclient.Dict)["event_type"], Equals, "crunch-run")
225 stderrLog := api.Content[0]["log"].(arvadosclient.Dict)["properties"].(map[string]string)["text"]
226 c.Check(true, Equals, strings.Contains(stderrLog, expected))
227 c.Check(string(kc.Content), Equals, logtext)
230 type filterSuite struct{}
232 var _ = Suite(&filterSuite{})
234 func (*filterSuite) TestFilterKeepstoreErrorsOnly(c *check.C) {
236 f := filterKeepstoreErrorsOnly{WriteCloser: nopCloser{&buf}}
237 for _, s := range []string{
239 "son\n" + `{"msg":"foo"}` + "\n{}\n" + `{"msg":"request"}` + "\n" + `{"msg":1234}` + "\n\n",
241 `{"msg":"response","respStatusCode":404,"foo": "bar"}` + "\n",
242 `{"msg":"response","respStatusCode":206}` + "\n",
246 c.Check(buf.String(), check.Equals, `not json
251 {"msg":"response","respStatusCode":404,"foo": "bar"}
255 type nopCloser struct {
259 func (nopCloser) Close() error { return nil }